Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 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 <hardware/sensors.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <errno.h> |
| 20 | #include <dirent.h> |
| 21 | #include <math.h> |
| 22 | #include <poll.h> |
| 23 | #include <pthread.h> |
| 24 | #include <cutils/atomic.h> |
| 25 | |
| 26 | #define LOG_NDEBUG 1 |
| 27 | #include <cutils/log.h> |
| 28 | |
| 29 | #include <vector> |
| 30 | #include <map> |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <dlfcn.h> |
| 34 | #include <SensorEventQueue.h> |
| 35 | |
| 36 | |
| 37 | static const char* CONFIG_FILENAME = "/system/etc/sensors/hals.conf"; |
| 38 | static const char* LEGAL_SUBHAL_PATH_PREFIX = "/system/lib/hw/"; |
| 39 | static const int MAX_CONF_LINE_LENGTH = 1024; |
| 40 | |
| 41 | static pthread_mutex_t init_modules_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 42 | static pthread_mutex_t init_sensors_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 43 | |
| 44 | // This mutex is shared by all queues |
| 45 | static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 46 | |
| 47 | // Used to pause the multihal poll(). Broadcasted by sub-polling tasks if waiting_for_data. |
| 48 | static pthread_cond_t data_available_cond = PTHREAD_COND_INITIALIZER; |
| 49 | bool waiting_for_data = false; |
| 50 | |
| 51 | /* |
| 52 | * Vector of sub modules, whose indexes are referred to ni this file as module_index. |
| 53 | */ |
| 54 | static std::vector<hw_module_t *> *sub_hw_modules = NULL; |
| 55 | |
| 56 | /* |
| 57 | * Comparable class that globally identifies a sensor, by module index and local handle. |
| 58 | * A module index is the module's index in sub_hw_modules. |
| 59 | * A local handle is the handle the sub-module assigns to a sensor. |
| 60 | */ |
| 61 | struct FullHandle { |
| 62 | int moduleIndex; |
| 63 | int localHandle; |
| 64 | |
| 65 | bool operator<(const FullHandle &that) const { |
| 66 | if (moduleIndex < that.moduleIndex) { |
| 67 | return true; |
| 68 | } |
| 69 | if (moduleIndex > that.moduleIndex) { |
| 70 | return false; |
| 71 | } |
| 72 | return localHandle < that.localHandle; |
| 73 | } |
| 74 | |
| 75 | bool operator==(const FullHandle &that) const { |
| 76 | return moduleIndex == that.moduleIndex && localHandle == that.localHandle; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | std::map<int, FullHandle> global_to_full; |
| 81 | std::map<FullHandle, int> full_to_global; |
| 82 | int next_global_handle = 1; |
| 83 | |
| 84 | static int assign_global_handle(int module_index, int local_handle) { |
| 85 | int global_handle = next_global_handle++; |
| 86 | FullHandle full_handle; |
| 87 | full_handle.moduleIndex = module_index; |
| 88 | full_handle.localHandle = local_handle; |
| 89 | full_to_global[full_handle] = global_handle; |
| 90 | global_to_full[global_handle] = full_handle; |
| 91 | return global_handle; |
| 92 | } |
| 93 | |
| 94 | static int get_local_handle(int global_handle) { |
| 95 | return global_to_full[global_handle].localHandle; |
| 96 | } |
| 97 | |
| 98 | static int get_module_index(int global_handle) { |
| 99 | FullHandle f = global_to_full[global_handle]; |
| 100 | ALOGV("FullHandle for global_handle %d: moduleIndex %d, localHandle %d", |
| 101 | global_handle, f.moduleIndex, f.localHandle); |
| 102 | return f.moduleIndex; |
| 103 | } |
| 104 | |
| 105 | static const int SENSOR_EVENT_QUEUE_CAPACITY = 20; |
| 106 | |
| 107 | struct TaskContext { |
| 108 | sensors_poll_device_t* device; |
| 109 | SensorEventQueue* queue; |
| 110 | }; |
| 111 | |
| 112 | void *writerTask(void* ptr) { |
| 113 | ALOGV("writerTask STARTS"); |
| 114 | TaskContext* ctx = (TaskContext*)ptr; |
| 115 | sensors_poll_device_t* device = ctx->device; |
| 116 | SensorEventQueue* queue = ctx->queue; |
| 117 | sensors_event_t* buffer; |
| 118 | int eventsPolled; |
| 119 | while (1) { |
| 120 | pthread_mutex_lock(&queue_mutex); |
| 121 | if (queue->waitForSpace(&queue_mutex)) { |
| 122 | ALOGV("writerTask waited for space"); |
| 123 | } |
| 124 | int bufferSize = queue->getWritableRegion(SENSOR_EVENT_QUEUE_CAPACITY, &buffer); |
| 125 | // Do blocking poll outside of lock |
| 126 | pthread_mutex_unlock(&queue_mutex); |
| 127 | |
| 128 | ALOGV("writerTask before poll() - bufferSize = %d", bufferSize); |
| 129 | eventsPolled = device->poll(device, buffer, bufferSize); |
| 130 | ALOGV("writerTask poll() got %d events.", eventsPolled); |
| 131 | if (eventsPolled == 0) { |
| 132 | continue; |
| 133 | } |
| 134 | pthread_mutex_lock(&queue_mutex); |
| 135 | queue->markAsWritten(eventsPolled); |
| 136 | ALOGV("writerTask wrote %d events", eventsPolled); |
| 137 | if (waiting_for_data) { |
| 138 | ALOGV("writerTask - broadcast data_available_cond"); |
| 139 | pthread_cond_broadcast(&data_available_cond); |
| 140 | } |
| 141 | pthread_mutex_unlock(&queue_mutex); |
| 142 | } |
| 143 | // never actually returns |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Cache of all sensors, with original handles replaced by global handles. |
| 149 | * This will be handled to get_sensors_list() callers. |
| 150 | */ |
| 151 | static struct sensor_t const* global_sensors_list = NULL; |
| 152 | static int global_sensors_count = -1; |
| 153 | |
| 154 | /* |
| 155 | * Extends a sensors_poll_device_1 by including all the sub-module's devices. |
| 156 | */ |
| 157 | struct sensors_poll_context_t { |
| 158 | /* |
| 159 | * This is the device that SensorDevice.cpp uses to make API calls |
| 160 | * to the multihal, which fans them out to sub-HALs. |
| 161 | */ |
| 162 | sensors_poll_device_1 proxy_device; // must be first |
| 163 | |
| 164 | void addSubHwDevice(struct hw_device_t*); |
| 165 | |
| 166 | int activate(int handle, int enabled); |
| 167 | int setDelay(int handle, int64_t ns); |
| 168 | int poll(sensors_event_t* data, int count); |
| 169 | int batch(int handle, int flags, int64_t period_ns, int64_t timeout); |
| 170 | int flush(int handle); |
| 171 | int close(); |
| 172 | |
| 173 | std::vector<hw_device_t*> sub_hw_devices; |
| 174 | std::vector<SensorEventQueue*> queues; |
| 175 | std::vector<pthread_t> threads; |
| 176 | int nextReadIndex; |
| 177 | |
| 178 | sensors_poll_device_t* get_v0_device_by_handle(int global_handle); |
| 179 | sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle); |
| 180 | int get_device_version_by_handle(int global_handle); |
| 181 | |
| 182 | void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index); |
| 183 | }; |
| 184 | |
| 185 | void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) { |
| 186 | ALOGV("addSubHwDevice"); |
| 187 | this->sub_hw_devices.push_back(sub_hw_device); |
| 188 | |
| 189 | SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY); |
| 190 | this->queues.push_back(queue); |
| 191 | |
| 192 | TaskContext* taskContext = new TaskContext(); |
| 193 | taskContext->device = (sensors_poll_device_t*) sub_hw_device; |
| 194 | taskContext->queue = queue; |
| 195 | |
| 196 | pthread_t writerThread; |
| 197 | pthread_create(&writerThread, NULL, writerTask, taskContext); |
| 198 | this->threads.push_back(writerThread); |
| 199 | } |
| 200 | |
| 201 | sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int handle) { |
| 202 | int sub_index = get_module_index(handle); |
| 203 | return (sensors_poll_device_t*) this->sub_hw_devices[sub_index]; |
| 204 | } |
| 205 | |
| 206 | sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int handle) { |
| 207 | int sub_index = get_module_index(handle); |
| 208 | return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index]; |
| 209 | } |
| 210 | |
| 211 | int sensors_poll_context_t::get_device_version_by_handle(int handle) { |
| 212 | sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle); |
| 213 | return v0->common.version; |
| 214 | } |
| 215 | |
| 216 | int sensors_poll_context_t::activate(int handle, int enabled) { |
| 217 | ALOGV("activate"); |
| 218 | sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle); |
| 219 | int retval = v0->activate(v0, get_local_handle(handle), enabled); |
| 220 | ALOGV("retval %d", retval); |
| 221 | return retval; |
| 222 | } |
| 223 | |
| 224 | int sensors_poll_context_t::setDelay(int handle, int64_t ns) { |
| 225 | ALOGV("setDelay"); |
| 226 | sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle); |
| 227 | int retval = v0->setDelay(v0, get_local_handle(handle), ns); |
| 228 | ALOGV("retval %d", retval); |
| 229 | return retval; |
| 230 | } |
| 231 | |
| 232 | void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src, |
| 233 | int sub_index) { |
| 234 | memcpy(dest, src, sizeof(struct sensors_event_t)); |
| 235 | // A normal event's "sensor" field is a local handle. Convert it to a global handle. |
| 236 | // A meta-data event must have its sensor set to 0, but it has a nested event |
| 237 | // with a local handle that needs to be converted to a global handle. |
| 238 | FullHandle full_handle; |
| 239 | full_handle.moduleIndex = sub_index; |
| 240 | // If it's a metadata event, rewrite the inner payload, not the sensor field. |
| 241 | if (dest->type == SENSOR_TYPE_META_DATA) { |
| 242 | full_handle.localHandle = dest->meta_data.sensor; |
| 243 | dest->meta_data.sensor = full_to_global[full_handle]; |
| 244 | } else { |
| 245 | full_handle.localHandle = dest->sensor; |
| 246 | dest->sensor = full_to_global[full_handle]; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) { |
| 251 | ALOGV("poll"); |
| 252 | int empties = 0; |
| 253 | int queueCount = (int)this->queues.size(); |
| 254 | int eventsRead = 0; |
| 255 | |
| 256 | pthread_mutex_lock(&queue_mutex); |
| 257 | while (eventsRead == 0) { |
| 258 | while (empties < queueCount && eventsRead < maxReads) { |
| 259 | SensorEventQueue* queue = this->queues.at(this->nextReadIndex); |
| 260 | sensors_event_t* event = queue->peek(); |
| 261 | if (event == NULL) { |
| 262 | empties++; |
| 263 | } else { |
| 264 | empties = 0; |
| 265 | this->copy_event_remap_handle(&data[eventsRead++], event, nextReadIndex); |
| 266 | queue->dequeue(); |
| 267 | } |
| 268 | this->nextReadIndex = (this->nextReadIndex + 1) % queueCount; |
| 269 | } |
| 270 | if (eventsRead == 0) { |
| 271 | // The queues have been scanned and none contain data, so wait. |
| 272 | ALOGV("poll stopping to wait for data"); |
| 273 | waiting_for_data = true; |
| 274 | pthread_cond_wait(&data_available_cond, &queue_mutex); |
| 275 | waiting_for_data = false; |
| 276 | empties = 0; |
| 277 | } |
| 278 | } |
| 279 | pthread_mutex_unlock(&queue_mutex); |
| 280 | ALOGV("poll returning %d events.", eventsRead); |
| 281 | |
| 282 | return eventsRead; |
| 283 | } |
| 284 | |
| 285 | int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) { |
| 286 | ALOGV("batch"); |
| 287 | int retval = -EINVAL; |
| 288 | int version = this->get_device_version_by_handle(handle); |
| 289 | if (version >= SENSORS_DEVICE_API_VERSION_1_0) { |
| 290 | sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle); |
| 291 | retval = v1->batch(v1, get_local_handle(handle), flags, period_ns, timeout); |
| 292 | } |
| 293 | ALOGV("retval %d", retval); |
| 294 | return retval; |
| 295 | } |
| 296 | |
| 297 | int sensors_poll_context_t::flush(int handle) { |
| 298 | ALOGV("flush"); |
| 299 | int retval = -EINVAL; |
| 300 | int version = this->get_device_version_by_handle(handle); |
| 301 | if (version >= SENSORS_DEVICE_API_VERSION_1_0) { |
| 302 | sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle); |
| 303 | retval = v1->flush(v1, get_local_handle(handle)); |
| 304 | } |
| 305 | ALOGV("retval %d", retval); |
| 306 | return retval; |
| 307 | } |
| 308 | |
| 309 | int sensors_poll_context_t::close() { |
| 310 | ALOGV("close"); |
| 311 | for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin(); |
| 312 | it != this->sub_hw_devices.end(); it++) { |
| 313 | hw_device_t* dev = *it; |
| 314 | int retval = dev->close(dev); |
| 315 | ALOGV("retval %d", retval); |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | static int device__close(struct hw_device_t *dev) { |
| 322 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 323 | if (ctx != NULL) { |
| 324 | int retval = ctx->close(); |
| 325 | delete ctx; |
| 326 | } |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int device__activate(struct sensors_poll_device_t *dev, int handle, |
| 331 | int enabled) { |
| 332 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 333 | return ctx->activate(handle, enabled); |
| 334 | } |
| 335 | |
| 336 | static int device__setDelay(struct sensors_poll_device_t *dev, int handle, |
| 337 | int64_t ns) { |
| 338 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 339 | return ctx->setDelay(handle, ns); |
| 340 | } |
| 341 | |
| 342 | static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data, |
| 343 | int count) { |
| 344 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 345 | return ctx->poll(data, count); |
| 346 | } |
| 347 | |
| 348 | static int device__batch(struct sensors_poll_device_1 *dev, int handle, |
| 349 | int flags, int64_t period_ns, int64_t timeout) { |
| 350 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 351 | return ctx->batch(handle, flags, period_ns, timeout); |
| 352 | } |
| 353 | |
| 354 | static int device__flush(struct sensors_poll_device_1 *dev, int handle) { |
| 355 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 356 | return ctx->flush(handle); |
| 357 | } |
| 358 | |
| 359 | static int open_sensors(const struct hw_module_t* module, const char* name, |
| 360 | struct hw_device_t** device); |
| 361 | |
| 362 | static bool starts_with(const char* s, const char* prefix) { |
| 363 | if (s == NULL || prefix == NULL) { |
| 364 | return false; |
| 365 | } |
| 366 | size_t s_size = strlen(s); |
| 367 | size_t prefix_size = strlen(prefix); |
| 368 | return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Adds valid paths from the config file to the vector passed in. |
| 373 | * The vector must not be null. |
| 374 | */ |
| 375 | static void get_so_paths(std::vector<char*> *so_paths) { |
| 376 | FILE *conf_file = fopen(CONFIG_FILENAME, "r"); |
| 377 | if (conf_file == NULL) { |
| 378 | ALOGW("No multihal config file found at %s", CONFIG_FILENAME); |
| 379 | return; |
| 380 | } |
| 381 | ALOGI("Multihal config file found at %s", CONFIG_FILENAME); |
| 382 | char *line = NULL; |
| 383 | size_t len = 0; |
| 384 | int line_count = 0; |
| 385 | while (getline(&line, &len, conf_file) != -1) { |
| 386 | // overwrite trailing eoln with null char |
| 387 | char* pch = strchr(line, '\n'); |
| 388 | if (pch != NULL) { |
| 389 | *pch = '\0'; |
| 390 | } |
| 391 | ALOGV("config file line #%d: '%s'", ++line_count, line); |
| 392 | char *real_path = realpath(line, NULL); |
| 393 | if (starts_with(real_path, LEGAL_SUBHAL_PATH_PREFIX)) { |
| 394 | ALOGI("accepting valid path '%s'", real_path); |
| 395 | char* compact_line = new char[strlen(real_path) + 1]; |
| 396 | strcpy(compact_line, real_path); |
| 397 | so_paths->push_back(compact_line); |
| 398 | } else { |
| 399 | ALOGW("rejecting path '%s' because it does not start with '%s'", |
| 400 | real_path, LEGAL_SUBHAL_PATH_PREFIX); |
| 401 | } |
| 402 | free(real_path); |
| 403 | } |
| 404 | free(line); |
| 405 | fclose(conf_file); |
| 406 | ALOGV("hals.conf contained %d lines", line_count); |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Ensures that the sub-module array is initialized. |
| 411 | * This can be first called from get_sensors_list or from open_sensors. |
| 412 | */ |
| 413 | static void lazy_init_modules() { |
| 414 | pthread_mutex_lock(&init_modules_mutex); |
| 415 | if (sub_hw_modules != NULL) { |
| 416 | pthread_mutex_unlock(&init_modules_mutex); |
| 417 | return; |
| 418 | } |
| 419 | std::vector<char*> *so_paths = new std::vector<char*>(); |
| 420 | get_so_paths(so_paths); |
| 421 | |
| 422 | // dlopen the module files and cache their module symbols in sub_hw_modules |
| 423 | sub_hw_modules = new std::vector<hw_module_t *>(); |
| 424 | dlerror(); // clear any old errors |
| 425 | const char* sym = HAL_MODULE_INFO_SYM_AS_STR; |
| 426 | for (std::vector<char*>::iterator it = so_paths->begin(); it != so_paths->end(); it++) { |
| 427 | char* path = *it; |
| 428 | void* lib_handle = dlopen(path, RTLD_LAZY); |
| 429 | if (lib_handle == NULL) { |
| 430 | ALOGW("dlerror(): %s", dlerror()); |
| 431 | } else { |
| 432 | ALOGI("hal lib was loaded: %s", path); |
| 433 | ALOGV("Opening symbol \"%s\"", sym); |
| 434 | // clear old errors |
| 435 | dlerror(); |
| 436 | struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym); |
| 437 | const char* error; |
| 438 | if ((error = dlerror()) != NULL) { |
| 439 | ALOGW("Error calling dlsym: %s", error); |
| 440 | } else if (module == NULL) { |
| 441 | ALOGW("module == NULL"); |
| 442 | } else { |
| 443 | ALOGI("OK, dlsym()'ed \"%s\"", sym); |
| 444 | sub_hw_modules->push_back(module); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | pthread_mutex_unlock(&init_modules_mutex); |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles. |
| 453 | */ |
| 454 | static void lazy_init_sensors_list() { |
| 455 | ALOGV("lazy_init_sensors_list"); |
| 456 | pthread_mutex_lock(&init_sensors_mutex); |
| 457 | if (global_sensors_list != NULL) { |
| 458 | // already initialized |
| 459 | pthread_mutex_unlock(&init_sensors_mutex); |
| 460 | ALOGV("lazy_init_sensors_list - early return"); |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | ALOGV("lazy_init_sensors_list needs to do work"); |
| 465 | lazy_init_modules(); |
| 466 | |
| 467 | // Count all the sensors, then allocate an array of blanks. |
| 468 | global_sensors_count = 0; |
| 469 | const struct sensor_t *subhal_sensors_list; |
| 470 | for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin(); |
| 471 | it != sub_hw_modules->end(); it++) { |
| 472 | struct sensors_module_t *module = (struct sensors_module_t*) *it; |
| 473 | global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list); |
| 474 | ALOGV("increased global_sensors_count to %d", global_sensors_count); |
| 475 | } |
| 476 | |
| 477 | // The global_sensors_list is full of consts. |
| 478 | // Manipulate this non-const list, and point the const one to it when we're done. |
| 479 | sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count]; |
| 480 | |
| 481 | // index of the next sensor to set in mutable_sensor_list |
| 482 | int mutable_sensor_index = 0; |
| 483 | int module_index = 0; |
| 484 | |
| 485 | for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin(); |
| 486 | it != sub_hw_modules->end(); it++) { |
| 487 | hw_module_t *hw_module = *it; |
| 488 | ALOGV("examine one module"); |
| 489 | // Read the sub-module's sensor list. |
| 490 | struct sensors_module_t *module = (struct sensors_module_t*) hw_module; |
| 491 | int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list); |
| 492 | ALOGV("the module has %d sensors", module_sensor_count); |
| 493 | |
| 494 | // Copy the HAL's sensor list into global_sensors_list, |
| 495 | // with the handle changed to be a global handle. |
| 496 | for (int i = 0; i < module_sensor_count; i++) { |
| 497 | ALOGV("examining one sensor"); |
| 498 | const struct sensor_t *local_sensor = &subhal_sensors_list[i]; |
| 499 | int local_handle = local_sensor->handle; |
| 500 | memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor, |
| 501 | sizeof(struct sensor_t)); |
| 502 | |
| 503 | // Overwrite the global version's handle with a global handle. |
| 504 | int global_handle = assign_global_handle(module_index, local_handle); |
| 505 | |
| 506 | mutable_sensor_list[mutable_sensor_index].handle = global_handle; |
| 507 | ALOGI("module_index %d, local_handle %d, global_handle %d", |
| 508 | module_index, local_handle, global_handle); |
| 509 | |
| 510 | mutable_sensor_index++; |
| 511 | } |
| 512 | module_index++; |
| 513 | } |
| 514 | // Set the const static global_sensors_list to the mutable one allocated by this function. |
| 515 | global_sensors_list = mutable_sensor_list; |
| 516 | |
| 517 | pthread_mutex_unlock(&init_sensors_mutex); |
| 518 | ALOGV("end lazy_init_sensors_list"); |
| 519 | } |
| 520 | |
| 521 | static int module__get_sensors_list(struct sensors_module_t* module, |
| 522 | struct sensor_t const** list) { |
| 523 | ALOGV("module__get_sensors_list start"); |
| 524 | lazy_init_sensors_list(); |
| 525 | *list = global_sensors_list; |
| 526 | ALOGV("global_sensors_count: %d", global_sensors_count); |
| 527 | for (int i = 0; i < global_sensors_count; i++) { |
| 528 | ALOGV("sensor type: %d", global_sensors_list[i].type); |
| 529 | } |
| 530 | return global_sensors_count; |
| 531 | } |
| 532 | |
| 533 | static struct hw_module_methods_t sensors_module_methods = { |
| 534 | open : open_sensors |
| 535 | }; |
| 536 | |
| 537 | struct sensors_module_t HAL_MODULE_INFO_SYM = { |
| 538 | common :{ |
| 539 | tag : HARDWARE_MODULE_TAG, |
| 540 | version_major : 1, |
| 541 | version_minor : 0, |
| 542 | id : SENSORS_HARDWARE_MODULE_ID, |
| 543 | name : "MultiHal Sensor Module", |
| 544 | author : "Google, Inc", |
| 545 | methods : &sensors_module_methods, |
| 546 | dso : NULL, |
| 547 | reserved : {0}, |
| 548 | }, |
| 549 | get_sensors_list : module__get_sensors_list |
| 550 | }; |
| 551 | |
| 552 | static int open_sensors(const struct hw_module_t* hw_module, const char* name, |
| 553 | struct hw_device_t** hw_device_out) { |
| 554 | ALOGI("open_sensors begin..."); |
| 555 | |
| 556 | lazy_init_modules(); |
| 557 | |
| 558 | // Create proxy device, to return later. |
| 559 | sensors_poll_context_t *dev = new sensors_poll_context_t(); |
| 560 | memset(dev, 0, sizeof(sensors_poll_device_1_t)); |
| 561 | dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG; |
| 562 | dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_0; |
| 563 | dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module); |
| 564 | dev->proxy_device.common.close = device__close; |
| 565 | dev->proxy_device.activate = device__activate; |
| 566 | dev->proxy_device.setDelay = device__setDelay; |
| 567 | dev->proxy_device.poll = device__poll; |
| 568 | dev->proxy_device.batch = device__batch; |
| 569 | dev->proxy_device.flush = device__flush; |
| 570 | |
| 571 | dev->nextReadIndex = 0; |
| 572 | |
| 573 | // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules. |
| 574 | for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin(); |
| 575 | it != sub_hw_modules->end(); it++) { |
| 576 | sensors_module_t *sensors_module = (sensors_module_t*) *it; |
| 577 | struct hw_device_t* sub_hw_device; |
| 578 | int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device); |
| 579 | dev->addSubHwDevice(sub_hw_device); |
| 580 | } |
| 581 | |
| 582 | // Prepare the output param and return |
| 583 | *hw_device_out = &dev->proxy_device.common; |
| 584 | ALOGI("...open_sensors end"); |
| 585 | return 0; |
| 586 | } |