blob: 5fd500a410d8f501ed851bb5472733d39a7fb9f6 [file] [log] [blame]
Mike Lockwoodf8477622013-10-17 08:05:00 -07001/*
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
37static const char* CONFIG_FILENAME = "/system/etc/sensors/hals.conf";
38static const char* LEGAL_SUBHAL_PATH_PREFIX = "/system/lib/hw/";
Nick Vaccaroad70dc42014-05-15 14:14:43 -070039static const char* LEGAL_SUBHAL_ALTERNATE_PATH_PREFIX = "/system/vendor/lib/";
Mike Lockwoodf8477622013-10-17 08:05:00 -070040static const int MAX_CONF_LINE_LENGTH = 1024;
41
42static pthread_mutex_t init_modules_mutex = PTHREAD_MUTEX_INITIALIZER;
43static pthread_mutex_t init_sensors_mutex = PTHREAD_MUTEX_INITIALIZER;
44
45// This mutex is shared by all queues
46static 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.
49static pthread_cond_t data_available_cond = PTHREAD_COND_INITIALIZER;
50bool waiting_for_data = false;
51
52/*
Aaron Whyte4d7ac522014-04-09 15:56:54 -070053 * Vector of sub modules, whose indexes are referred to in this file as module_index.
Mike Lockwoodf8477622013-10-17 08:05:00 -070054 */
55static 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 */
62struct 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
81std::map<int, FullHandle> global_to_full;
82std::map<FullHandle, int> full_to_global;
83int next_global_handle = 1;
84
85static 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
Aaron Whyte4d7ac522014-04-09 15:56:54 -070095// Returns the local handle, or -1 if it does not exist.
Mike Lockwoodf8477622013-10-17 08:05:00 -070096static int get_local_handle(int global_handle) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -070097 if (global_to_full.count(global_handle) == 0) {
98 ALOGW("Unknown global_handle %d", global_handle);
99 return -1;
100 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700101 return global_to_full[global_handle].localHandle;
102}
103
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700104// 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.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700106static int get_module_index(int global_handle) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700107 if (global_to_full.count(global_handle) == 0) {
108 ALOGW("Unknown global_handle %d", global_handle);
109 return -1;
110 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700111 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
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700117// Returns the global handle for this full_handle, or -1 if the full_handle is unknown.
118static 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
Mike Lockwoodf8477622013-10-17 08:05:00 -0700129static const int SENSOR_EVENT_QUEUE_CAPACITY = 20;
130
131struct TaskContext {
132 sensors_poll_device_t* device;
133 SensorEventQueue* queue;
134};
135
136void *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 continue;
157 }
158 pthread_mutex_lock(&queue_mutex);
159 queue->markAsWritten(eventsPolled);
160 ALOGV("writerTask wrote %d events", eventsPolled);
161 if (waiting_for_data) {
162 ALOGV("writerTask - broadcast data_available_cond");
163 pthread_cond_broadcast(&data_available_cond);
164 }
165 pthread_mutex_unlock(&queue_mutex);
166 }
167 // never actually returns
168 return NULL;
169}
170
171/*
172 * Cache of all sensors, with original handles replaced by global handles.
173 * This will be handled to get_sensors_list() callers.
174 */
175static struct sensor_t const* global_sensors_list = NULL;
176static int global_sensors_count = -1;
177
178/*
179 * Extends a sensors_poll_device_1 by including all the sub-module's devices.
180 */
181struct sensors_poll_context_t {
182 /*
183 * This is the device that SensorDevice.cpp uses to make API calls
184 * to the multihal, which fans them out to sub-HALs.
185 */
186 sensors_poll_device_1 proxy_device; // must be first
187
188 void addSubHwDevice(struct hw_device_t*);
189
190 int activate(int handle, int enabled);
191 int setDelay(int handle, int64_t ns);
192 int poll(sensors_event_t* data, int count);
193 int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
194 int flush(int handle);
195 int close();
196
197 std::vector<hw_device_t*> sub_hw_devices;
198 std::vector<SensorEventQueue*> queues;
199 std::vector<pthread_t> threads;
200 int nextReadIndex;
201
202 sensors_poll_device_t* get_v0_device_by_handle(int global_handle);
203 sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle);
204 int get_device_version_by_handle(int global_handle);
205
206 void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index);
207};
208
209void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) {
210 ALOGV("addSubHwDevice");
211 this->sub_hw_devices.push_back(sub_hw_device);
212
213 SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY);
214 this->queues.push_back(queue);
215
216 TaskContext* taskContext = new TaskContext();
217 taskContext->device = (sensors_poll_device_t*) sub_hw_device;
218 taskContext->queue = queue;
219
220 pthread_t writerThread;
221 pthread_create(&writerThread, NULL, writerTask, taskContext);
222 this->threads.push_back(writerThread);
223}
224
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700225// Returns the device pointer, or NULL if the global handle is invalid.
226sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) {
227 int sub_index = get_module_index(global_handle);
228 if (sub_index < 0 || sub_index >= this->sub_hw_devices.size()) {
229 return NULL;
230 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700231 return (sensors_poll_device_t*) this->sub_hw_devices[sub_index];
232}
233
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700234// Returns the device pointer, or NULL if the global handle is invalid.
235sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) {
236 int sub_index = get_module_index(global_handle);
237 if (sub_index < 0 || sub_index >= this->sub_hw_devices.size()) {
238 return NULL;
239 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700240 return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
241}
242
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700243// Returns the device version, or -1 if the handle is invalid.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700244int sensors_poll_context_t::get_device_version_by_handle(int handle) {
245 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700246 if (v0) {
247 return v0->common.version;
248 } else {
249 return -1;
250 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700251}
252
253int sensors_poll_context_t::activate(int handle, int enabled) {
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700254 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700255 ALOGV("activate");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700256 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700257 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700258 if (local_handle >= 0 && v0) {
259 retval = v0->activate(v0, local_handle, enabled);
260 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700261 ALOGV("retval %d", retval);
262 return retval;
263}
264
265int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700266 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700267 ALOGV("setDelay");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700268 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700269 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700270 if (local_handle >= 0 && v0) {
271 retval = v0->setDelay(v0, local_handle, ns);
272 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700273 ALOGV("retval %d", retval);
274 return retval;
275}
276
277void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src,
278 int sub_index) {
279 memcpy(dest, src, sizeof(struct sensors_event_t));
280 // A normal event's "sensor" field is a local handle. Convert it to a global handle.
281 // A meta-data event must have its sensor set to 0, but it has a nested event
282 // with a local handle that needs to be converted to a global handle.
283 FullHandle full_handle;
284 full_handle.moduleIndex = sub_index;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700285
Mike Lockwoodf8477622013-10-17 08:05:00 -0700286 // If it's a metadata event, rewrite the inner payload, not the sensor field.
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700287 // If the event's sensor field is unregistered for any reason, rewrite the sensor field
288 // with a -1, instead of writing an incorrect but plausible sensor number, because
289 // get_global_handle() returns -1 for unknown FullHandles.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700290 if (dest->type == SENSOR_TYPE_META_DATA) {
291 full_handle.localHandle = dest->meta_data.sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700292 dest->meta_data.sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700293 } else {
294 full_handle.localHandle = dest->sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700295 dest->sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700296 }
297}
298
299int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) {
300 ALOGV("poll");
301 int empties = 0;
302 int queueCount = (int)this->queues.size();
303 int eventsRead = 0;
304
305 pthread_mutex_lock(&queue_mutex);
306 while (eventsRead == 0) {
307 while (empties < queueCount && eventsRead < maxReads) {
308 SensorEventQueue* queue = this->queues.at(this->nextReadIndex);
309 sensors_event_t* event = queue->peek();
310 if (event == NULL) {
311 empties++;
312 } else {
313 empties = 0;
314 this->copy_event_remap_handle(&data[eventsRead++], event, nextReadIndex);
315 queue->dequeue();
316 }
317 this->nextReadIndex = (this->nextReadIndex + 1) % queueCount;
318 }
319 if (eventsRead == 0) {
320 // The queues have been scanned and none contain data, so wait.
321 ALOGV("poll stopping to wait for data");
322 waiting_for_data = true;
323 pthread_cond_wait(&data_available_cond, &queue_mutex);
324 waiting_for_data = false;
325 empties = 0;
326 }
327 }
328 pthread_mutex_unlock(&queue_mutex);
329 ALOGV("poll returning %d events.", eventsRead);
330
331 return eventsRead;
332}
333
334int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) {
335 ALOGV("batch");
336 int retval = -EINVAL;
337 int version = this->get_device_version_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700338 int local_handle = get_local_handle(handle);
339 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
340 if (version >= SENSORS_DEVICE_API_VERSION_1_0 && local_handle >= 0 && v1) {
341 retval = v1->batch(v1, local_handle, flags, period_ns, timeout);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700342 }
343 ALOGV("retval %d", retval);
344 return retval;
345}
346
347int sensors_poll_context_t::flush(int handle) {
348 ALOGV("flush");
349 int retval = -EINVAL;
350 int version = this->get_device_version_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700351 int local_handle = get_local_handle(handle);
352 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
353 if (version >= SENSORS_DEVICE_API_VERSION_1_0 && local_handle >= 0 && v1) {
354 retval = v1->flush(v1, local_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700355 }
356 ALOGV("retval %d", retval);
357 return retval;
358}
359
360int sensors_poll_context_t::close() {
361 ALOGV("close");
362 for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
363 it != this->sub_hw_devices.end(); it++) {
364 hw_device_t* dev = *it;
365 int retval = dev->close(dev);
366 ALOGV("retval %d", retval);
367 }
368 return 0;
369}
370
371
372static int device__close(struct hw_device_t *dev) {
373 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
374 if (ctx != NULL) {
375 int retval = ctx->close();
376 delete ctx;
377 }
378 return 0;
379}
380
381static int device__activate(struct sensors_poll_device_t *dev, int handle,
382 int enabled) {
383 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
384 return ctx->activate(handle, enabled);
385}
386
387static int device__setDelay(struct sensors_poll_device_t *dev, int handle,
388 int64_t ns) {
389 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
390 return ctx->setDelay(handle, ns);
391}
392
393static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data,
394 int count) {
395 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
396 return ctx->poll(data, count);
397}
398
399static int device__batch(struct sensors_poll_device_1 *dev, int handle,
400 int flags, int64_t period_ns, int64_t timeout) {
401 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
402 return ctx->batch(handle, flags, period_ns, timeout);
403}
404
405static int device__flush(struct sensors_poll_device_1 *dev, int handle) {
406 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
407 return ctx->flush(handle);
408}
409
410static int open_sensors(const struct hw_module_t* module, const char* name,
411 struct hw_device_t** device);
412
413static bool starts_with(const char* s, const char* prefix) {
414 if (s == NULL || prefix == NULL) {
415 return false;
416 }
417 size_t s_size = strlen(s);
418 size_t prefix_size = strlen(prefix);
419 return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0;
420}
421
422/*
423 * Adds valid paths from the config file to the vector passed in.
424 * The vector must not be null.
425 */
426static void get_so_paths(std::vector<char*> *so_paths) {
427 FILE *conf_file = fopen(CONFIG_FILENAME, "r");
428 if (conf_file == NULL) {
429 ALOGW("No multihal config file found at %s", CONFIG_FILENAME);
430 return;
431 }
Aaron Whytef22e1c12014-04-15 15:41:38 -0700432 ALOGV("Multihal config file found at %s", CONFIG_FILENAME);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700433 char *line = NULL;
434 size_t len = 0;
435 int line_count = 0;
436 while (getline(&line, &len, conf_file) != -1) {
437 // overwrite trailing eoln with null char
438 char* pch = strchr(line, '\n');
439 if (pch != NULL) {
440 *pch = '\0';
441 }
442 ALOGV("config file line #%d: '%s'", ++line_count, line);
443 char *real_path = realpath(line, NULL);
Nick Vaccaroad70dc42014-05-15 14:14:43 -0700444 if (starts_with(real_path, LEGAL_SUBHAL_PATH_PREFIX) ||
445 starts_with(real_path, LEGAL_SUBHAL_ALTERNATE_PATH_PREFIX)) {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700446 ALOGV("accepting valid path '%s'", real_path);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700447 char* compact_line = new char[strlen(real_path) + 1];
448 strcpy(compact_line, real_path);
449 so_paths->push_back(compact_line);
450 } else {
Nick Vaccaroad70dc42014-05-15 14:14:43 -0700451 ALOGW("rejecting path '%s' because it does not start with '%s' or '%s'",
452 real_path, LEGAL_SUBHAL_PATH_PREFIX, LEGAL_SUBHAL_ALTERNATE_PATH_PREFIX);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700453 }
454 free(real_path);
455 }
456 free(line);
457 fclose(conf_file);
458 ALOGV("hals.conf contained %d lines", line_count);
459}
460
461/*
462 * Ensures that the sub-module array is initialized.
463 * This can be first called from get_sensors_list or from open_sensors.
464 */
465static void lazy_init_modules() {
466 pthread_mutex_lock(&init_modules_mutex);
467 if (sub_hw_modules != NULL) {
468 pthread_mutex_unlock(&init_modules_mutex);
469 return;
470 }
471 std::vector<char*> *so_paths = new std::vector<char*>();
472 get_so_paths(so_paths);
473
474 // dlopen the module files and cache their module symbols in sub_hw_modules
475 sub_hw_modules = new std::vector<hw_module_t *>();
476 dlerror(); // clear any old errors
477 const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
478 for (std::vector<char*>::iterator it = so_paths->begin(); it != so_paths->end(); it++) {
479 char* path = *it;
480 void* lib_handle = dlopen(path, RTLD_LAZY);
481 if (lib_handle == NULL) {
482 ALOGW("dlerror(): %s", dlerror());
483 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700484 ALOGI("Loaded library from %s", path);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700485 ALOGV("Opening symbol \"%s\"", sym);
486 // clear old errors
487 dlerror();
488 struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym);
489 const char* error;
490 if ((error = dlerror()) != NULL) {
491 ALOGW("Error calling dlsym: %s", error);
492 } else if (module == NULL) {
493 ALOGW("module == NULL");
494 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700495 ALOGV("Loaded symbols from \"%s\"", sym);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700496 sub_hw_modules->push_back(module);
497 }
498 }
499 }
500 pthread_mutex_unlock(&init_modules_mutex);
501}
502
503/*
504 * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles.
505 */
506static void lazy_init_sensors_list() {
507 ALOGV("lazy_init_sensors_list");
508 pthread_mutex_lock(&init_sensors_mutex);
509 if (global_sensors_list != NULL) {
510 // already initialized
511 pthread_mutex_unlock(&init_sensors_mutex);
512 ALOGV("lazy_init_sensors_list - early return");
513 return;
514 }
515
516 ALOGV("lazy_init_sensors_list needs to do work");
517 lazy_init_modules();
518
519 // Count all the sensors, then allocate an array of blanks.
520 global_sensors_count = 0;
521 const struct sensor_t *subhal_sensors_list;
522 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
523 it != sub_hw_modules->end(); it++) {
524 struct sensors_module_t *module = (struct sensors_module_t*) *it;
525 global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list);
526 ALOGV("increased global_sensors_count to %d", global_sensors_count);
527 }
528
529 // The global_sensors_list is full of consts.
530 // Manipulate this non-const list, and point the const one to it when we're done.
531 sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count];
532
533 // index of the next sensor to set in mutable_sensor_list
534 int mutable_sensor_index = 0;
535 int module_index = 0;
536
537 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
538 it != sub_hw_modules->end(); it++) {
539 hw_module_t *hw_module = *it;
540 ALOGV("examine one module");
541 // Read the sub-module's sensor list.
542 struct sensors_module_t *module = (struct sensors_module_t*) hw_module;
543 int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list);
544 ALOGV("the module has %d sensors", module_sensor_count);
545
546 // Copy the HAL's sensor list into global_sensors_list,
547 // with the handle changed to be a global handle.
548 for (int i = 0; i < module_sensor_count; i++) {
549 ALOGV("examining one sensor");
550 const struct sensor_t *local_sensor = &subhal_sensors_list[i];
551 int local_handle = local_sensor->handle;
552 memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
553 sizeof(struct sensor_t));
554
555 // Overwrite the global version's handle with a global handle.
556 int global_handle = assign_global_handle(module_index, local_handle);
557
558 mutable_sensor_list[mutable_sensor_index].handle = global_handle;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700559 ALOGV("module_index %d, local_handle %d, global_handle %d",
Mike Lockwoodf8477622013-10-17 08:05:00 -0700560 module_index, local_handle, global_handle);
561
562 mutable_sensor_index++;
563 }
564 module_index++;
565 }
566 // Set the const static global_sensors_list to the mutable one allocated by this function.
567 global_sensors_list = mutable_sensor_list;
568
569 pthread_mutex_unlock(&init_sensors_mutex);
570 ALOGV("end lazy_init_sensors_list");
571}
572
573static int module__get_sensors_list(struct sensors_module_t* module,
574 struct sensor_t const** list) {
575 ALOGV("module__get_sensors_list start");
576 lazy_init_sensors_list();
577 *list = global_sensors_list;
578 ALOGV("global_sensors_count: %d", global_sensors_count);
579 for (int i = 0; i < global_sensors_count; i++) {
580 ALOGV("sensor type: %d", global_sensors_list[i].type);
581 }
582 return global_sensors_count;
583}
584
585static struct hw_module_methods_t sensors_module_methods = {
586 open : open_sensors
587};
588
589struct sensors_module_t HAL_MODULE_INFO_SYM = {
590 common :{
591 tag : HARDWARE_MODULE_TAG,
592 version_major : 1,
Nick Vaccarod34ed322014-05-05 17:23:21 -0700593 version_minor : 1,
Mike Lockwoodf8477622013-10-17 08:05:00 -0700594 id : SENSORS_HARDWARE_MODULE_ID,
595 name : "MultiHal Sensor Module",
596 author : "Google, Inc",
597 methods : &sensors_module_methods,
598 dso : NULL,
599 reserved : {0},
600 },
601 get_sensors_list : module__get_sensors_list
602};
603
604static int open_sensors(const struct hw_module_t* hw_module, const char* name,
605 struct hw_device_t** hw_device_out) {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700606 ALOGV("open_sensors begin...");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700607
608 lazy_init_modules();
609
610 // Create proxy device, to return later.
611 sensors_poll_context_t *dev = new sensors_poll_context_t();
612 memset(dev, 0, sizeof(sensors_poll_device_1_t));
613 dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
Nick Vaccarod34ed322014-05-05 17:23:21 -0700614 dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_1;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700615 dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
616 dev->proxy_device.common.close = device__close;
617 dev->proxy_device.activate = device__activate;
618 dev->proxy_device.setDelay = device__setDelay;
619 dev->proxy_device.poll = device__poll;
620 dev->proxy_device.batch = device__batch;
621 dev->proxy_device.flush = device__flush;
622
623 dev->nextReadIndex = 0;
624
625 // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
626 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
627 it != sub_hw_modules->end(); it++) {
628 sensors_module_t *sensors_module = (sensors_module_t*) *it;
629 struct hw_device_t* sub_hw_device;
630 int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700631 if (!sub_open_result)
632 dev->addSubHwDevice(sub_hw_device);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700633 }
634
635 // Prepare the output param and return
636 *hw_device_out = &dev->proxy_device.common;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700637 ALOGV("...open_sensors end");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700638 return 0;
639}