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