1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "SensorEventQueue.h" 18 #include "multihal.h" 19 20 #define LOG_NDEBUG 1 21 #include <cutils/log.h> 22 #include <cutils/atomic.h> 23 #include <hardware/sensors.h> 24 25 #include <vector> 26 #include <string> 27 #include <fstream> 28 #include <map> 29 30 #include <dirent.h> 31 #include <dlfcn.h> 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <limits.h> 35 #include <math.h> 36 #include <poll.h> 37 #include <pthread.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 41 42 static pthread_mutex_t init_modules_mutex = PTHREAD_MUTEX_INITIALIZER; 43 static pthread_mutex_t init_sensors_mutex = PTHREAD_MUTEX_INITIALIZER; 44 45 // This mutex is shared by all queues 46 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER; 47 48 // Used to pause the multihal poll(). Broadcasted by sub-polling tasks if waiting_for_data. 49 static pthread_cond_t data_available_cond = PTHREAD_COND_INITIALIZER; 50 bool waiting_for_data = false; 51 52 // Vector of sub modules, whose indexes are referred to in this file as module_index. 53 static std::vector<hw_module_t *> *sub_hw_modules = nullptr; 54 55 // Vector of sub modules shared object handles 56 static std::vector<void *> *so_handles = nullptr; 57 58 /* 59 * Comparable class that globally identifies a sensor, by module index and local handle. 60 * A module index is the module's index in sub_hw_modules. 61 * A local handle is the handle the sub-module assigns to a sensor. 62 */ 63 struct FullHandle { 64 int moduleIndex; 65 int localHandle; 66 67 bool operator<(const FullHandle &that) const { 68 if (moduleIndex < that.moduleIndex) { 69 return true; 70 } 71 if (moduleIndex > that.moduleIndex) { 72 return false; 73 } 74 return localHandle < that.localHandle; 75 } 76 77 bool operator==(const FullHandle &that) const { 78 return moduleIndex == that.moduleIndex && localHandle == that.localHandle; 79 } 80 }; 81 82 std::map<int, FullHandle> global_to_full; 83 std::map<FullHandle, int> full_to_global; 84 int next_global_handle = 1; 85 86 static int assign_global_handle(int module_index, int local_handle) { 87 int global_handle = next_global_handle++; 88 FullHandle full_handle; 89 full_handle.moduleIndex = module_index; 90 full_handle.localHandle = local_handle; 91 full_to_global[full_handle] = global_handle; 92 global_to_full[global_handle] = full_handle; 93 return global_handle; 94 } 95 96 // Returns the local handle, or -1 if it does not exist. 97 static int get_local_handle(int global_handle) { 98 if (global_to_full.count(global_handle) == 0) { 99 ALOGW("Unknown global_handle %d", global_handle); 100 return -1; 101 } 102 return global_to_full[global_handle].localHandle; 103 } 104 105 // Returns the sub_hw_modules index of the module that contains the sensor associates with this 106 // global_handle, or -1 if that global_handle does not exist. 107 static int get_module_index(int global_handle) { 108 if (global_to_full.count(global_handle) == 0) { 109 ALOGW("Unknown global_handle %d", global_handle); 110 return -1; 111 } 112 FullHandle f = global_to_full[global_handle]; 113 ALOGV("FullHandle for global_handle %d: moduleIndex %d, localHandle %d", 114 global_handle, f.moduleIndex, f.localHandle); 115 return f.moduleIndex; 116 } 117 118 // Returns the global handle for this full_handle, or -1 if the full_handle is unknown. 119 static int get_global_handle(FullHandle* full_handle) { 120 int global_handle = -1; 121 if (full_to_global.count(*full_handle)) { 122 global_handle = full_to_global[*full_handle]; 123 } else { 124 ALOGW("Unknown FullHandle: moduleIndex %d, localHandle %d", 125 full_handle->moduleIndex, full_handle->localHandle); 126 } 127 return global_handle; 128 } 129 130 static const int SENSOR_EVENT_QUEUE_CAPACITY = 36; 131 132 struct TaskContext { 133 sensors_poll_device_t* device; 134 SensorEventQueue* queue; 135 }; 136 137 void *writerTask(void* ptr) { 138 ALOGV("writerTask STARTS"); 139 TaskContext* ctx = (TaskContext*)ptr; 140 sensors_poll_device_t* device = ctx->device; 141 SensorEventQueue* queue = ctx->queue; 142 sensors_event_t* buffer; 143 int eventsPolled; 144 while (1) { 145 pthread_mutex_lock(&queue_mutex); 146 if (queue->waitForSpace(&queue_mutex)) { 147 ALOGV("writerTask waited for space"); 148 } 149 int bufferSize = queue->getWritableRegion(SENSOR_EVENT_QUEUE_CAPACITY, &buffer); 150 // Do blocking poll outside of lock 151 pthread_mutex_unlock(&queue_mutex); 152 153 ALOGV("writerTask before poll() - bufferSize = %d", bufferSize); 154 eventsPolled = device->poll(device, buffer, bufferSize); 155 ALOGV("writerTask poll() got %d events.", eventsPolled); 156 if (eventsPolled <= 0) { 157 if (eventsPolled < 0) { 158 ALOGV("writerTask ignored error %d from %s", eventsPolled, device->common.module->name); 159 ALOGE("ERROR: Fix %s so it does not return error from poll()", device->common.module->name); 160 } 161 continue; 162 } 163 pthread_mutex_lock(&queue_mutex); 164 queue->markAsWritten(eventsPolled); 165 ALOGV("writerTask wrote %d events", eventsPolled); 166 if (waiting_for_data) { 167 ALOGV("writerTask - broadcast data_available_cond"); 168 pthread_cond_broadcast(&data_available_cond); 169 } 170 pthread_mutex_unlock(&queue_mutex); 171 } 172 // never actually returns 173 return NULL; 174 } 175 176 /* 177 * Cache of all sensors, with original handles replaced by global handles. 178 * This will be handled to get_sensors_list() callers. 179 */ 180 static struct sensor_t const* global_sensors_list = NULL; 181 static int global_sensors_count = -1; 182 183 /* 184 * Extends a sensors_poll_device_1 by including all the sub-module's devices. 185 */ 186 struct sensors_poll_context_t { 187 /* 188 * This is the device that SensorDevice.cpp uses to make API calls 189 * to the multihal, which fans them out to sub-HALs. 190 */ 191 sensors_poll_device_1 proxy_device; // must be first 192 193 void addSubHwDevice(struct hw_device_t*); 194 195 int activate(int handle, int enabled); 196 int setDelay(int handle, int64_t ns); 197 int poll(sensors_event_t* data, int count); 198 int batch(int handle, int flags, int64_t period_ns, int64_t timeout); 199 int flush(int handle); 200 int inject_sensor_data(const sensors_event_t *data); 201 int register_direct_channel(const struct sensors_direct_mem_t* mem, 202 int channel_handle); 203 int config_direct_report(int sensor_handle, 204 int channel_handle, 205 const struct sensors_direct_cfg_t *config); 206 int close(); 207 208 std::vector<hw_device_t*> sub_hw_devices; 209 std::vector<SensorEventQueue*> queues; 210 std::vector<pthread_t> threads; 211 int nextReadIndex; 212 213 sensors_poll_device_t* get_v0_device_by_handle(int global_handle); 214 sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle); 215 sensors_poll_device_1_t* get_primary_v1_device(); 216 int get_device_version_by_handle(int global_handle); 217 218 void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index); 219 }; 220 221 void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) { 222 ALOGV("addSubHwDevice"); 223 this->sub_hw_devices.push_back(sub_hw_device); 224 225 SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY); 226 this->queues.push_back(queue); 227 228 TaskContext* taskContext = new TaskContext(); 229 taskContext->device = (sensors_poll_device_t*) sub_hw_device; 230 taskContext->queue = queue; 231 232 pthread_t writerThread; 233 pthread_create(&writerThread, NULL, writerTask, taskContext); 234 this->threads.push_back(writerThread); 235 } 236 237 // Returns the device pointer, or NULL if the global handle is invalid. 238 sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) { 239 int sub_index = get_module_index(global_handle); 240 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) { 241 return NULL; 242 } 243 return (sensors_poll_device_t*) this->sub_hw_devices[sub_index]; 244 } 245 246 // Returns the device pointer, or NULL if the global handle is invalid. 247 sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) { 248 int sub_index = get_module_index(global_handle); 249 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) { 250 return NULL; 251 } 252 return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index]; 253 } 254 255 // Returns the device pointer, or NULL if primary hal does not exist 256 sensors_poll_device_1_t* sensors_poll_context_t::get_primary_v1_device() { 257 if (sub_hw_devices.size() < 1) { 258 return nullptr; 259 } 260 return (sensors_poll_device_1_t*) this->sub_hw_devices[0]; 261 } 262 263 // Returns the device version, or -1 if the handle is invalid. 264 int sensors_poll_context_t::get_device_version_by_handle(int handle) { 265 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle); 266 if (v0) { 267 return v0->common.version; 268 } else { 269 return -1; 270 } 271 } 272 273 // Android N and hire require sensor HALs to be at least 1_3 compliant 274 #define HAL_VERSION_IS_COMPLIANT(version) \ 275 (version >= SENSORS_DEVICE_API_VERSION_1_3) 276 277 // Returns true if HAL is compliant, false if HAL is not compliant or if handle is invalid 278 static bool halIsCompliant(sensors_poll_context_t *ctx, int handle) { 279 int version = ctx->get_device_version_by_handle(handle); 280 return version != -1 && HAL_VERSION_IS_COMPLIANT(version); 281 } 282 283 static bool halIsAPILevelCompliant(sensors_poll_context_t *ctx, int handle, int level) { 284 int version = ctx->get_device_version_by_handle(handle); 285 return version != -1 && (version >= level); 286 } 287 288 static bool halSupportDirectSensorReport(sensors_poll_device_1_t* v1) { 289 return v1 != nullptr && HAL_VERSION_IS_COMPLIANT(v1->common.version) && 290 v1->register_direct_channel != nullptr && v1->config_direct_report != nullptr; 291 } 292 293 const char *apiNumToStr(int version) { 294 switch(version) { 295 case SENSORS_DEVICE_API_VERSION_1_0: 296 return "SENSORS_DEVICE_API_VERSION_1_0"; 297 case SENSORS_DEVICE_API_VERSION_1_1: 298 return "SENSORS_DEVICE_API_VERSION_1_1"; 299 case SENSORS_DEVICE_API_VERSION_1_2: 300 return "SENSORS_DEVICE_API_VERSION_1_2"; 301 case SENSORS_DEVICE_API_VERSION_1_3: 302 return "SENSORS_DEVICE_API_VERSION_1_3"; 303 case SENSORS_DEVICE_API_VERSION_1_4: 304 return "SENSORS_DEVICE_API_VERSION_1_4"; 305 default: 306 return "UNKNOWN"; 307 } 308 } 309 310 int sensors_poll_context_t::activate(int handle, int enabled) { 311 int retval = -EINVAL; 312 ALOGV("activate"); 313 int local_handle = get_local_handle(handle); 314 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle); 315 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) { 316 retval = v0->activate(v0, local_handle, enabled); 317 } else { 318 ALOGE("IGNORING activate(enable %d) call to non-API-compliant sensor handle=%d !", 319 enabled, handle); 320 } 321 ALOGV("retval %d", retval); 322 return retval; 323 } 324 325 int sensors_poll_context_t::setDelay(int handle, int64_t ns) { 326 int retval = -EINVAL; 327 ALOGV("setDelay"); 328 int local_handle = get_local_handle(handle); 329 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle); 330 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) { 331 retval = v0->setDelay(v0, local_handle, ns); 332 } else { 333 ALOGE("IGNORING setDelay() call for non-API-compliant sensor handle=%d !", handle); 334 } 335 ALOGV("retval %d", retval); 336 return retval; 337 } 338 339 void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src, 340 int sub_index) { 341 memcpy(dest, src, sizeof(struct sensors_event_t)); 342 // A normal event's "sensor" field is a local handle. Convert it to a global handle. 343 // A meta-data event must have its sensor set to 0, but it has a nested event 344 // with a local handle that needs to be converted to a global handle. 345 FullHandle full_handle; 346 full_handle.moduleIndex = sub_index; 347 348 // If it's a metadata event, rewrite the inner payload, not the sensor field. 349 // If the event's sensor field is unregistered for any reason, rewrite the sensor field 350 // with a -1, instead of writing an incorrect but plausible sensor number, because 351 // get_global_handle() returns -1 for unknown FullHandles. 352 if (dest->type == SENSOR_TYPE_META_DATA) { 353 full_handle.localHandle = dest->meta_data.sensor; 354 dest->meta_data.sensor = get_global_handle(&full_handle); 355 } else { 356 full_handle.localHandle = dest->sensor; 357 dest->sensor = get_global_handle(&full_handle); 358 } 359 } 360 361 int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) { 362 ALOGV("poll"); 363 int empties = 0; 364 int queueCount = 0; 365 int eventsRead = 0; 366 367 pthread_mutex_lock(&queue_mutex); 368 queueCount = (int)this->queues.size(); 369 while (eventsRead == 0) { 370 while (empties < queueCount && eventsRead < maxReads) { 371 SensorEventQueue* queue = this->queues.at(this->nextReadIndex); 372 sensors_event_t* event = queue->peek(); 373 if (event == NULL) { 374 empties++; 375 } else { 376 empties = 0; 377 this->copy_event_remap_handle(&data[eventsRead], event, nextReadIndex); 378 if (data[eventsRead].sensor == SENSORS_HANDLE_BASE - 1) { 379 // Bad handle, do not pass corrupted event upstream ! 380 ALOGW("Dropping bad local handle event packet on the floor"); 381 } else { 382 eventsRead++; 383 } 384 queue->dequeue(); 385 } 386 this->nextReadIndex = (this->nextReadIndex + 1) % queueCount; 387 } 388 if (eventsRead == 0) { 389 // The queues have been scanned and none contain data, so wait. 390 ALOGV("poll stopping to wait for data"); 391 waiting_for_data = true; 392 pthread_cond_wait(&data_available_cond, &queue_mutex); 393 waiting_for_data = false; 394 empties = 0; 395 } 396 } 397 pthread_mutex_unlock(&queue_mutex); 398 ALOGV("poll returning %d events.", eventsRead); 399 400 return eventsRead; 401 } 402 403 int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) { 404 ALOGV("batch"); 405 int retval = -EINVAL; 406 int local_handle = get_local_handle(handle); 407 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle); 408 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) { 409 retval = v1->batch(v1, local_handle, flags, period_ns, timeout); 410 } else { 411 ALOGE("IGNORING batch() call to non-API-compliant sensor handle=%d !", handle); 412 } 413 ALOGV("retval %d", retval); 414 return retval; 415 } 416 417 int sensors_poll_context_t::flush(int handle) { 418 ALOGV("flush"); 419 int retval = -EINVAL; 420 int local_handle = get_local_handle(handle); 421 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle); 422 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) { 423 retval = v1->flush(v1, local_handle); 424 } else { 425 ALOGE("IGNORING flush() call to non-API-compliant sensor handle=%d !", handle); 426 } 427 ALOGV("retval %d", retval); 428 return retval; 429 } 430 431 int sensors_poll_context_t::inject_sensor_data(const sensors_event_t *data) { 432 int retval = -EINVAL; 433 ALOGV("inject_sensor_data"); 434 if (data->sensor == -1) { 435 // operational parameter 436 sensors_poll_device_1_t* v1 = get_primary_v1_device(); 437 if (v1 && v1->common.version >= SENSORS_DEVICE_API_VERSION_1_4) { 438 retval = v1->inject_sensor_data(v1, data); 439 } else { 440 ALOGE("IGNORED inject_sensor_data(operational param) call to non-API-compliant sensor"); 441 return -ENOSYS; 442 } 443 } else { 444 // Get handle for the sensor owning the event being injected 445 int local_handle = get_local_handle(data->sensor); 446 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(data->sensor); 447 if (halIsAPILevelCompliant(this, data->sensor, SENSORS_DEVICE_API_VERSION_1_4) && 448 local_handle >= 0 && v1) { 449 // if specific sensor is used, we have to replace global sensor handle 450 // with local one, before passing to concrete HAL 451 sensors_event_t data_copy = *data; 452 data_copy.sensor = local_handle; 453 retval = v1->inject_sensor_data(v1, &data_copy); 454 } else { 455 ALOGE("IGNORED inject_sensor_data(type=%d, handle=%d) call to non-API-compliant sensor", 456 data->type, data->sensor); 457 retval = -ENOSYS; 458 } 459 } 460 ALOGV("retval %d", retval); 461 return retval; 462 } 463 464 int sensors_poll_context_t::register_direct_channel(const struct sensors_direct_mem_t* mem, 465 int channel_handle) { 466 int retval = -EINVAL; 467 ALOGV("register_direct_channel"); 468 sensors_poll_device_1_t* v1 = get_primary_v1_device(); 469 if (v1 && halSupportDirectSensorReport(v1)) { 470 retval = v1->register_direct_channel(v1, mem, channel_handle); 471 } else { 472 ALOGE("IGNORED register_direct_channel(mem=%p, handle=%d) call to non-API-compliant sensor", 473 mem, channel_handle); 474 retval = -ENOSYS; 475 } 476 ALOGV("retval %d", retval); 477 return retval; 478 } 479 480 int sensors_poll_context_t::config_direct_report(int sensor_handle, 481 int channel_handle, 482 const struct sensors_direct_cfg_t *config) { 483 int retval = -EINVAL; 484 ALOGV("config_direct_report"); 485 486 if (config != nullptr) { 487 int local_handle = get_local_handle(sensor_handle); 488 sensors_poll_device_1_t* v1 = get_primary_v1_device(); 489 if (v1 && halSupportDirectSensorReport(v1)) { 490 retval = v1->config_direct_report(v1, local_handle, channel_handle, config); 491 } else { 492 ALOGE("IGNORED config_direct_report(sensor=%d, channel=%d, rate_level=%d) call to " 493 "non-API-compliant sensor", sensor_handle, channel_handle, config->rate_level); 494 retval = -ENOSYS; 495 } 496 } 497 ALOGV("retval %d", retval); 498 return retval; 499 } 500 int sensors_poll_context_t::close() { 501 ALOGV("close"); 502 for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin(); 503 it != this->sub_hw_devices.end(); it++) { 504 hw_device_t* dev = *it; 505 int retval = dev->close(dev); 506 ALOGV("retval %d", retval); 507 } 508 return 0; 509 } 510 511 512 static int device__close(struct hw_device_t *dev) { 513 pthread_mutex_lock(&init_modules_mutex); 514 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; 515 if (ctx != NULL) { 516 int retval = ctx->close(); 517 delete ctx; 518 } 519 520 if (sub_hw_modules != nullptr) { 521 delete sub_hw_modules; 522 sub_hw_modules = nullptr; 523 } 524 525 if (so_handles != nullptr) { 526 for (auto handle : *so_handles) { 527 dlclose(handle); 528 } 529 delete so_handles; 530 so_handles = nullptr; 531 } 532 pthread_mutex_unlock(&init_modules_mutex); 533 return 0; 534 } 535 536 static int device__activate(struct sensors_poll_device_t *dev, int handle, 537 int enabled) { 538 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; 539 return ctx->activate(handle, enabled); 540 } 541 542 static int device__setDelay(struct sensors_poll_device_t *dev, int handle, 543 int64_t ns) { 544 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; 545 return ctx->setDelay(handle, ns); 546 } 547 548 static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data, 549 int count) { 550 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; 551 return ctx->poll(data, count); 552 } 553 554 static int device__batch(struct sensors_poll_device_1 *dev, int handle, 555 int flags, int64_t period_ns, int64_t timeout) { 556 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; 557 return ctx->batch(handle, flags, period_ns, timeout); 558 } 559 560 static int device__flush(struct sensors_poll_device_1 *dev, int handle) { 561 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; 562 return ctx->flush(handle); 563 } 564 565 static int device__inject_sensor_data(struct sensors_poll_device_1 *dev, 566 const sensors_event_t *data) { 567 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; 568 return ctx->inject_sensor_data(data); 569 } 570 571 static int device__register_direct_channel(struct sensors_poll_device_1 *dev, 572 const struct sensors_direct_mem_t* mem, 573 int channel_handle) { 574 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; 575 return ctx->register_direct_channel(mem, channel_handle); 576 } 577 578 static int device__config_direct_report(struct sensors_poll_device_1 *dev, 579 int sensor_handle, 580 int channel_handle, 581 const struct sensors_direct_cfg_t *config) { 582 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; 583 return ctx->config_direct_report(sensor_handle, channel_handle, config); 584 } 585 586 static int open_sensors(const struct hw_module_t* module, const char* name, 587 struct hw_device_t** device); 588 589 static bool starts_with(const char* s, const char* prefix) { 590 if (s == NULL || prefix == NULL) { 591 return false; 592 } 593 size_t s_size = strlen(s); 594 size_t prefix_size = strlen(prefix); 595 return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0; 596 } 597 598 /* 599 * Adds valid paths from the config file to the vector passed in. 600 * The vector must not be null. 601 */ 602 static std::vector<std::string> get_so_paths() { 603 std::vector<std::string> so_paths; 604 605 const std::vector<const char *> config_path_list( 606 { MULTI_HAL_CONFIG_FILE_PATH, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH }); 607 608 std::ifstream stream; 609 const char *path = nullptr; 610 for (auto i : config_path_list) { 611 std::ifstream f(i); 612 if (f) { 613 stream = std::move(f); 614 path = i; 615 break; 616 } 617 } 618 if(!stream) { 619 ALOGW("No multihal config file found"); 620 return so_paths; 621 } 622 623 ALOGE_IF(strcmp(path, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH) == 0, 624 "Multihal configuration file path %s is not compatible with Treble " 625 "requirements. Please move it to %s.", 626 path, MULTI_HAL_CONFIG_FILE_PATH); 627 628 ALOGV("Multihal config file found at %s", path); 629 std::string line; 630 while (std::getline(stream, line)) { 631 ALOGV("config file line: '%s'", line.c_str()); 632 so_paths.push_back(line); 633 } 634 return so_paths; 635 } 636 637 /* 638 * Ensures that the sub-module array is initialized. 639 * This can be first called from get_sensors_list or from open_sensors. 640 */ 641 static void lazy_init_modules() { 642 pthread_mutex_lock(&init_modules_mutex); 643 if (sub_hw_modules != NULL) { 644 pthread_mutex_unlock(&init_modules_mutex); 645 return; 646 } 647 std::vector<std::string> so_paths(get_so_paths()); 648 649 // dlopen the module files and cache their module symbols in sub_hw_modules 650 sub_hw_modules = new std::vector<hw_module_t *>(); 651 so_handles = new std::vector<void *>(); 652 dlerror(); // clear any old errors 653 const char* sym = HAL_MODULE_INFO_SYM_AS_STR; 654 for (const auto &s : so_paths) { 655 const char* path = s.c_str(); 656 void* lib_handle = dlopen(path, RTLD_LAZY); 657 if (lib_handle == NULL) { 658 ALOGW("dlerror(): %s", dlerror()); 659 } else { 660 ALOGI("Loaded library from %s", path); 661 ALOGV("Opening symbol \"%s\"", sym); 662 // clear old errors 663 dlerror(); 664 struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym); 665 const char* error; 666 if ((error = dlerror()) != NULL) { 667 ALOGW("Error calling dlsym: %s", error); 668 } else if (module == NULL) { 669 ALOGW("module == NULL"); 670 } else { 671 ALOGV("Loaded symbols from \"%s\"", sym); 672 sub_hw_modules->push_back(module); 673 so_handles->push_back(lib_handle); 674 lib_handle = nullptr; 675 } 676 } 677 if (lib_handle != nullptr) { 678 dlclose(lib_handle); 679 } 680 } 681 pthread_mutex_unlock(&init_modules_mutex); 682 } 683 684 /* 685 * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles. 686 */ 687 static void lazy_init_sensors_list() { 688 ALOGV("lazy_init_sensors_list"); 689 pthread_mutex_lock(&init_sensors_mutex); 690 if (global_sensors_list != NULL) { 691 // already initialized 692 pthread_mutex_unlock(&init_sensors_mutex); 693 ALOGV("lazy_init_sensors_list - early return"); 694 return; 695 } 696 697 ALOGV("lazy_init_sensors_list needs to do work"); 698 lazy_init_modules(); 699 700 // Count all the sensors, then allocate an array of blanks. 701 global_sensors_count = 0; 702 const struct sensor_t *subhal_sensors_list; 703 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin(); 704 it != sub_hw_modules->end(); it++) { 705 struct sensors_module_t *module = (struct sensors_module_t*) *it; 706 global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list); 707 ALOGV("increased global_sensors_count to %d", global_sensors_count); 708 } 709 710 // The global_sensors_list is full of consts. 711 // Manipulate this non-const list, and point the const one to it when we're done. 712 sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count]; 713 714 // index of the next sensor to set in mutable_sensor_list 715 int mutable_sensor_index = 0; 716 int module_index = 0; 717 718 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin(); 719 it != sub_hw_modules->end(); it++) { 720 hw_module_t *hw_module = *it; 721 ALOGV("examine one module"); 722 // Read the sub-module's sensor list. 723 struct sensors_module_t *module = (struct sensors_module_t*) hw_module; 724 int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list); 725 ALOGV("the module has %d sensors", module_sensor_count); 726 727 // Copy the HAL's sensor list into global_sensors_list, 728 // with the handle changed to be a global handle. 729 for (int i = 0; i < module_sensor_count; i++) { 730 ALOGV("examining one sensor"); 731 const struct sensor_t *local_sensor = &subhal_sensors_list[i]; 732 int local_handle = local_sensor->handle; 733 memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor, 734 sizeof(struct sensor_t)); 735 736 // sensor direct report is only for primary module 737 if (module_index != 0) { 738 mutable_sensor_list[mutable_sensor_index].flags &= 739 ~(SENSOR_FLAG_MASK_DIRECT_REPORT | SENSOR_FLAG_MASK_DIRECT_CHANNEL); 740 } 741 742 // Overwrite the global version's handle with a global handle. 743 int global_handle = assign_global_handle(module_index, local_handle); 744 745 mutable_sensor_list[mutable_sensor_index].handle = global_handle; 746 ALOGV("module_index %d, local_handle %d, global_handle %d", 747 module_index, local_handle, global_handle); 748 749 mutable_sensor_index++; 750 } 751 module_index++; 752 } 753 // Set the const static global_sensors_list to the mutable one allocated by this function. 754 global_sensors_list = mutable_sensor_list; 755 756 pthread_mutex_unlock(&init_sensors_mutex); 757 ALOGV("end lazy_init_sensors_list"); 758 } 759 760 static int module__get_sensors_list(__unused struct sensors_module_t* module, 761 struct sensor_t const** list) { 762 ALOGV("module__get_sensors_list start"); 763 lazy_init_sensors_list(); 764 *list = global_sensors_list; 765 ALOGV("global_sensors_count: %d", global_sensors_count); 766 for (int i = 0; i < global_sensors_count; i++) { 767 ALOGV("sensor type: %d", global_sensors_list[i].type); 768 } 769 return global_sensors_count; 770 } 771 772 static struct hw_module_methods_t sensors_module_methods = { 773 .open = open_sensors 774 }; 775 776 struct sensors_module_t HAL_MODULE_INFO_SYM = { 777 .common = { 778 .tag = HARDWARE_MODULE_TAG, 779 .version_major = 1, 780 .version_minor = 1, 781 .id = SENSORS_HARDWARE_MODULE_ID, 782 .name = "MultiHal Sensor Module", 783 .author = "Google, Inc", 784 .methods = &sensors_module_methods, 785 .dso = NULL, 786 .reserved = {0}, 787 }, 788 .get_sensors_list = module__get_sensors_list 789 }; 790 791 struct sensors_module_t *get_multi_hal_module_info() { 792 return (&HAL_MODULE_INFO_SYM); 793 } 794 795 static int open_sensors(const struct hw_module_t* hw_module, const char* name, 796 struct hw_device_t** hw_device_out) { 797 ALOGV("open_sensors begin..."); 798 799 lazy_init_modules(); 800 801 // Create proxy device, to return later. 802 sensors_poll_context_t *dev = new sensors_poll_context_t(); 803 memset(dev, 0, sizeof(sensors_poll_device_1_t)); 804 dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG; 805 dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_4; 806 dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module); 807 dev->proxy_device.common.close = device__close; 808 dev->proxy_device.activate = device__activate; 809 dev->proxy_device.setDelay = device__setDelay; 810 dev->proxy_device.poll = device__poll; 811 dev->proxy_device.batch = device__batch; 812 dev->proxy_device.flush = device__flush; 813 dev->proxy_device.inject_sensor_data = device__inject_sensor_data; 814 dev->proxy_device.register_direct_channel = device__register_direct_channel; 815 dev->proxy_device.config_direct_report = device__config_direct_report; 816 817 dev->nextReadIndex = 0; 818 819 // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules. 820 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin(); 821 it != sub_hw_modules->end(); it++) { 822 sensors_module_t *sensors_module = (sensors_module_t*) *it; 823 struct hw_device_t* sub_hw_device; 824 int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device); 825 if (!sub_open_result) { 826 if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) { 827 ALOGE("SENSORS_DEVICE_API_VERSION_1_3 or newer is required for all sensor HALs"); 828 ALOGE("This HAL reports non-compliant API level : %s", 829 apiNumToStr(sub_hw_device->version)); 830 ALOGE("Sensors belonging to this HAL will get ignored !"); 831 } 832 dev->addSubHwDevice(sub_hw_device); 833 } 834 } 835 836 // Prepare the output param and return 837 *hw_device_out = &dev->proxy_device.common; 838 ALOGV("...open_sensors end"); 839 return 0; 840 } 841