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