blob: f7177a3cfde63958adada17b3e052ef5a546f306 [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/";
39static const int MAX_CONF_LINE_LENGTH = 1024;
40
41static pthread_mutex_t init_modules_mutex = PTHREAD_MUTEX_INITIALIZER;
42static pthread_mutex_t init_sensors_mutex = PTHREAD_MUTEX_INITIALIZER;
43
44// This mutex is shared by all queues
45static 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.
48static pthread_cond_t data_available_cond = PTHREAD_COND_INITIALIZER;
49bool waiting_for_data = false;
50
51/*
Aaron Whyte4d7ac522014-04-09 15:56:54 -070052 * Vector of sub modules, whose indexes are referred to in this file as module_index.
Mike Lockwoodf8477622013-10-17 08:05:00 -070053 */
54static 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 */
61struct 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
80std::map<int, FullHandle> global_to_full;
81std::map<FullHandle, int> full_to_global;
82int next_global_handle = 1;
83
84static 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
Aaron Whyte4d7ac522014-04-09 15:56:54 -070094// Returns the local handle, or -1 if it does not exist.
Mike Lockwoodf8477622013-10-17 08:05:00 -070095static int get_local_handle(int global_handle) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -070096 if (global_to_full.count(global_handle) == 0) {
97 ALOGW("Unknown global_handle %d", global_handle);
98 return -1;
99 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700100 return global_to_full[global_handle].localHandle;
101}
102
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700103// Returns the sub_hw_modules index of the module that contains the sensor associates with this
104// global_handle, or -1 if that global_handle does not exist.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700105static int get_module_index(int global_handle) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700106 if (global_to_full.count(global_handle) == 0) {
107 ALOGW("Unknown global_handle %d", global_handle);
108 return -1;
109 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700110 FullHandle f = global_to_full[global_handle];
111 ALOGV("FullHandle for global_handle %d: moduleIndex %d, localHandle %d",
112 global_handle, f.moduleIndex, f.localHandle);
113 return f.moduleIndex;
114}
115
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700116// Returns the global handle for this full_handle, or -1 if the full_handle is unknown.
117static int get_global_handle(FullHandle* full_handle) {
118 int global_handle = -1;
119 if (full_to_global.count(*full_handle)) {
120 global_handle = full_to_global[*full_handle];
121 } else {
122 ALOGW("Unknown FullHandle: moduleIndex %d, localHandle %d",
123 full_handle->moduleIndex, full_handle->localHandle);
124 }
125 return global_handle;
126}
127
Mike Lockwoodf8477622013-10-17 08:05:00 -0700128static const int SENSOR_EVENT_QUEUE_CAPACITY = 20;
129
130struct TaskContext {
131 sensors_poll_device_t* device;
132 SensorEventQueue* queue;
133};
134
135void *writerTask(void* ptr) {
136 ALOGV("writerTask STARTS");
137 TaskContext* ctx = (TaskContext*)ptr;
138 sensors_poll_device_t* device = ctx->device;
139 SensorEventQueue* queue = ctx->queue;
140 sensors_event_t* buffer;
141 int eventsPolled;
142 while (1) {
143 pthread_mutex_lock(&queue_mutex);
144 if (queue->waitForSpace(&queue_mutex)) {
145 ALOGV("writerTask waited for space");
146 }
147 int bufferSize = queue->getWritableRegion(SENSOR_EVENT_QUEUE_CAPACITY, &buffer);
148 // Do blocking poll outside of lock
149 pthread_mutex_unlock(&queue_mutex);
150
151 ALOGV("writerTask before poll() - bufferSize = %d", bufferSize);
152 eventsPolled = device->poll(device, buffer, bufferSize);
153 ALOGV("writerTask poll() got %d events.", eventsPolled);
154 if (eventsPolled == 0) {
155 continue;
156 }
157 pthread_mutex_lock(&queue_mutex);
158 queue->markAsWritten(eventsPolled);
159 ALOGV("writerTask wrote %d events", eventsPolled);
160 if (waiting_for_data) {
161 ALOGV("writerTask - broadcast data_available_cond");
162 pthread_cond_broadcast(&data_available_cond);
163 }
164 pthread_mutex_unlock(&queue_mutex);
165 }
166 // never actually returns
167 return NULL;
168}
169
170/*
171 * Cache of all sensors, with original handles replaced by global handles.
172 * This will be handled to get_sensors_list() callers.
173 */
174static struct sensor_t const* global_sensors_list = NULL;
175static int global_sensors_count = -1;
176
177/*
178 * Extends a sensors_poll_device_1 by including all the sub-module's devices.
179 */
180struct sensors_poll_context_t {
181 /*
182 * This is the device that SensorDevice.cpp uses to make API calls
183 * to the multihal, which fans them out to sub-HALs.
184 */
185 sensors_poll_device_1 proxy_device; // must be first
186
187 void addSubHwDevice(struct hw_device_t*);
188
189 int activate(int handle, int enabled);
190 int setDelay(int handle, int64_t ns);
191 int poll(sensors_event_t* data, int count);
192 int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
193 int flush(int handle);
194 int close();
195
196 std::vector<hw_device_t*> sub_hw_devices;
197 std::vector<SensorEventQueue*> queues;
198 std::vector<pthread_t> threads;
199 int nextReadIndex;
200
201 sensors_poll_device_t* get_v0_device_by_handle(int global_handle);
202 sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle);
203 int get_device_version_by_handle(int global_handle);
204
205 void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index);
206};
207
208void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) {
209 ALOGV("addSubHwDevice");
210 this->sub_hw_devices.push_back(sub_hw_device);
211
212 SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY);
213 this->queues.push_back(queue);
214
215 TaskContext* taskContext = new TaskContext();
216 taskContext->device = (sensors_poll_device_t*) sub_hw_device;
217 taskContext->queue = queue;
218
219 pthread_t writerThread;
220 pthread_create(&writerThread, NULL, writerTask, taskContext);
221 this->threads.push_back(writerThread);
222}
223
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700224// Returns the device pointer, or NULL if the global handle is invalid.
225sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) {
226 int sub_index = get_module_index(global_handle);
227 if (sub_index < 0 || sub_index >= this->sub_hw_devices.size()) {
228 return NULL;
229 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700230 return (sensors_poll_device_t*) this->sub_hw_devices[sub_index];
231}
232
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700233// Returns the device pointer, or NULL if the global handle is invalid.
234sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) {
235 int sub_index = get_module_index(global_handle);
236 if (sub_index < 0 || sub_index >= this->sub_hw_devices.size()) {
237 return NULL;
238 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700239 return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
240}
241
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700242// Returns the device version, or -1 if the handle is invalid.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700243int sensors_poll_context_t::get_device_version_by_handle(int handle) {
244 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700245 if (v0) {
246 return v0->common.version;
247 } else {
248 return -1;
249 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700250}
251
252int sensors_poll_context_t::activate(int handle, int enabled) {
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700253 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700254 ALOGV("activate");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700255 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700256 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700257 if (local_handle >= 0 && v0) {
258 retval = v0->activate(v0, local_handle, enabled);
259 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700260 ALOGV("retval %d", retval);
261 return retval;
262}
263
264int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700265 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700266 ALOGV("setDelay");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700267 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700268 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700269 if (local_handle >= 0 && v0) {
270 retval = v0->setDelay(v0, local_handle, ns);
271 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700272 ALOGV("retval %d", retval);
273 return retval;
274}
275
276void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src,
277 int sub_index) {
278 memcpy(dest, src, sizeof(struct sensors_event_t));
279 // A normal event's "sensor" field is a local handle. Convert it to a global handle.
280 // A meta-data event must have its sensor set to 0, but it has a nested event
281 // with a local handle that needs to be converted to a global handle.
282 FullHandle full_handle;
283 full_handle.moduleIndex = sub_index;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700284
Mike Lockwoodf8477622013-10-17 08:05:00 -0700285 // If it's a metadata event, rewrite the inner payload, not the sensor field.
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700286 // If the event's sensor field is unregistered for any reason, rewrite the sensor field
287 // with a -1, instead of writing an incorrect but plausible sensor number, because
288 // get_global_handle() returns -1 for unknown FullHandles.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700289 if (dest->type == SENSOR_TYPE_META_DATA) {
290 full_handle.localHandle = dest->meta_data.sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700291 dest->meta_data.sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700292 } else {
293 full_handle.localHandle = dest->sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700294 dest->sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700295 }
296}
297
298int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) {
299 ALOGV("poll");
300 int empties = 0;
301 int queueCount = (int)this->queues.size();
302 int eventsRead = 0;
303
304 pthread_mutex_lock(&queue_mutex);
305 while (eventsRead == 0) {
306 while (empties < queueCount && eventsRead < maxReads) {
307 SensorEventQueue* queue = this->queues.at(this->nextReadIndex);
308 sensors_event_t* event = queue->peek();
309 if (event == NULL) {
310 empties++;
311 } else {
312 empties = 0;
313 this->copy_event_remap_handle(&data[eventsRead++], event, nextReadIndex);
314 queue->dequeue();
315 }
316 this->nextReadIndex = (this->nextReadIndex + 1) % queueCount;
317 }
318 if (eventsRead == 0) {
319 // The queues have been scanned and none contain data, so wait.
320 ALOGV("poll stopping to wait for data");
321 waiting_for_data = true;
322 pthread_cond_wait(&data_available_cond, &queue_mutex);
323 waiting_for_data = false;
324 empties = 0;
325 }
326 }
327 pthread_mutex_unlock(&queue_mutex);
328 ALOGV("poll returning %d events.", eventsRead);
329
330 return eventsRead;
331}
332
333int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) {
334 ALOGV("batch");
335 int retval = -EINVAL;
336 int version = this->get_device_version_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700337 int local_handle = get_local_handle(handle);
338 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
339 if (version >= SENSORS_DEVICE_API_VERSION_1_0 && local_handle >= 0 && v1) {
340 retval = v1->batch(v1, local_handle, flags, period_ns, timeout);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700341 }
342 ALOGV("retval %d", retval);
343 return retval;
344}
345
346int sensors_poll_context_t::flush(int handle) {
347 ALOGV("flush");
348 int retval = -EINVAL;
349 int version = this->get_device_version_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700350 int local_handle = get_local_handle(handle);
351 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
352 if (version >= SENSORS_DEVICE_API_VERSION_1_0 && local_handle >= 0 && v1) {
353 retval = v1->flush(v1, local_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700354 }
355 ALOGV("retval %d", retval);
356 return retval;
357}
358
359int sensors_poll_context_t::close() {
360 ALOGV("close");
361 for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
362 it != this->sub_hw_devices.end(); it++) {
363 hw_device_t* dev = *it;
364 int retval = dev->close(dev);
365 ALOGV("retval %d", retval);
366 }
367 return 0;
368}
369
370
371static int device__close(struct hw_device_t *dev) {
372 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
373 if (ctx != NULL) {
374 int retval = ctx->close();
375 delete ctx;
376 }
377 return 0;
378}
379
380static int device__activate(struct sensors_poll_device_t *dev, int handle,
381 int enabled) {
382 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
383 return ctx->activate(handle, enabled);
384}
385
386static int device__setDelay(struct sensors_poll_device_t *dev, int handle,
387 int64_t ns) {
388 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
389 return ctx->setDelay(handle, ns);
390}
391
392static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data,
393 int count) {
394 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
395 return ctx->poll(data, count);
396}
397
398static int device__batch(struct sensors_poll_device_1 *dev, int handle,
399 int flags, int64_t period_ns, int64_t timeout) {
400 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
401 return ctx->batch(handle, flags, period_ns, timeout);
402}
403
404static int device__flush(struct sensors_poll_device_1 *dev, int handle) {
405 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
406 return ctx->flush(handle);
407}
408
409static int open_sensors(const struct hw_module_t* module, const char* name,
410 struct hw_device_t** device);
411
412static bool starts_with(const char* s, const char* prefix) {
413 if (s == NULL || prefix == NULL) {
414 return false;
415 }
416 size_t s_size = strlen(s);
417 size_t prefix_size = strlen(prefix);
418 return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0;
419}
420
421/*
422 * Adds valid paths from the config file to the vector passed in.
423 * The vector must not be null.
424 */
425static void get_so_paths(std::vector<char*> *so_paths) {
426 FILE *conf_file = fopen(CONFIG_FILENAME, "r");
427 if (conf_file == NULL) {
428 ALOGW("No multihal config file found at %s", CONFIG_FILENAME);
429 return;
430 }
Aaron Whytef22e1c12014-04-15 15:41:38 -0700431 ALOGV("Multihal config file found at %s", CONFIG_FILENAME);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700432 char *line = NULL;
433 size_t len = 0;
434 int line_count = 0;
435 while (getline(&line, &len, conf_file) != -1) {
436 // overwrite trailing eoln with null char
437 char* pch = strchr(line, '\n');
438 if (pch != NULL) {
439 *pch = '\0';
440 }
441 ALOGV("config file line #%d: '%s'", ++line_count, line);
442 char *real_path = realpath(line, NULL);
443 if (starts_with(real_path, LEGAL_SUBHAL_PATH_PREFIX)) {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700444 ALOGV("accepting valid path '%s'", real_path);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700445 char* compact_line = new char[strlen(real_path) + 1];
446 strcpy(compact_line, real_path);
447 so_paths->push_back(compact_line);
448 } else {
449 ALOGW("rejecting path '%s' because it does not start with '%s'",
450 real_path, LEGAL_SUBHAL_PATH_PREFIX);
451 }
452 free(real_path);
453 }
454 free(line);
455 fclose(conf_file);
456 ALOGV("hals.conf contained %d lines", line_count);
457}
458
459/*
460 * Ensures that the sub-module array is initialized.
461 * This can be first called from get_sensors_list or from open_sensors.
462 */
463static void lazy_init_modules() {
464 pthread_mutex_lock(&init_modules_mutex);
465 if (sub_hw_modules != NULL) {
466 pthread_mutex_unlock(&init_modules_mutex);
467 return;
468 }
469 std::vector<char*> *so_paths = new std::vector<char*>();
470 get_so_paths(so_paths);
471
472 // dlopen the module files and cache their module symbols in sub_hw_modules
473 sub_hw_modules = new std::vector<hw_module_t *>();
474 dlerror(); // clear any old errors
475 const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
476 for (std::vector<char*>::iterator it = so_paths->begin(); it != so_paths->end(); it++) {
477 char* path = *it;
478 void* lib_handle = dlopen(path, RTLD_LAZY);
479 if (lib_handle == NULL) {
480 ALOGW("dlerror(): %s", dlerror());
481 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700482 ALOGI("Loaded library from %s", path);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700483 ALOGV("Opening symbol \"%s\"", sym);
484 // clear old errors
485 dlerror();
486 struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym);
487 const char* error;
488 if ((error = dlerror()) != NULL) {
489 ALOGW("Error calling dlsym: %s", error);
490 } else if (module == NULL) {
491 ALOGW("module == NULL");
492 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700493 ALOGV("Loaded symbols from \"%s\"", sym);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700494 sub_hw_modules->push_back(module);
495 }
496 }
497 }
498 pthread_mutex_unlock(&init_modules_mutex);
499}
500
501/*
502 * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles.
503 */
504static void lazy_init_sensors_list() {
505 ALOGV("lazy_init_sensors_list");
506 pthread_mutex_lock(&init_sensors_mutex);
507 if (global_sensors_list != NULL) {
508 // already initialized
509 pthread_mutex_unlock(&init_sensors_mutex);
510 ALOGV("lazy_init_sensors_list - early return");
511 return;
512 }
513
514 ALOGV("lazy_init_sensors_list needs to do work");
515 lazy_init_modules();
516
517 // Count all the sensors, then allocate an array of blanks.
518 global_sensors_count = 0;
519 const struct sensor_t *subhal_sensors_list;
520 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
521 it != sub_hw_modules->end(); it++) {
522 struct sensors_module_t *module = (struct sensors_module_t*) *it;
523 global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list);
524 ALOGV("increased global_sensors_count to %d", global_sensors_count);
525 }
526
527 // The global_sensors_list is full of consts.
528 // Manipulate this non-const list, and point the const one to it when we're done.
529 sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count];
530
531 // index of the next sensor to set in mutable_sensor_list
532 int mutable_sensor_index = 0;
533 int module_index = 0;
534
535 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
536 it != sub_hw_modules->end(); it++) {
537 hw_module_t *hw_module = *it;
538 ALOGV("examine one module");
539 // Read the sub-module's sensor list.
540 struct sensors_module_t *module = (struct sensors_module_t*) hw_module;
541 int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list);
542 ALOGV("the module has %d sensors", module_sensor_count);
543
544 // Copy the HAL's sensor list into global_sensors_list,
545 // with the handle changed to be a global handle.
546 for (int i = 0; i < module_sensor_count; i++) {
547 ALOGV("examining one sensor");
548 const struct sensor_t *local_sensor = &subhal_sensors_list[i];
549 int local_handle = local_sensor->handle;
550 memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
551 sizeof(struct sensor_t));
552
553 // Overwrite the global version's handle with a global handle.
554 int global_handle = assign_global_handle(module_index, local_handle);
555
556 mutable_sensor_list[mutable_sensor_index].handle = global_handle;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700557 ALOGV("module_index %d, local_handle %d, global_handle %d",
Mike Lockwoodf8477622013-10-17 08:05:00 -0700558 module_index, local_handle, global_handle);
559
560 mutable_sensor_index++;
561 }
562 module_index++;
563 }
564 // Set the const static global_sensors_list to the mutable one allocated by this function.
565 global_sensors_list = mutable_sensor_list;
566
567 pthread_mutex_unlock(&init_sensors_mutex);
568 ALOGV("end lazy_init_sensors_list");
569}
570
571static int module__get_sensors_list(struct sensors_module_t* module,
572 struct sensor_t const** list) {
573 ALOGV("module__get_sensors_list start");
574 lazy_init_sensors_list();
575 *list = global_sensors_list;
576 ALOGV("global_sensors_count: %d", global_sensors_count);
577 for (int i = 0; i < global_sensors_count; i++) {
578 ALOGV("sensor type: %d", global_sensors_list[i].type);
579 }
580 return global_sensors_count;
581}
582
583static struct hw_module_methods_t sensors_module_methods = {
584 open : open_sensors
585};
586
587struct sensors_module_t HAL_MODULE_INFO_SYM = {
588 common :{
589 tag : HARDWARE_MODULE_TAG,
590 version_major : 1,
591 version_minor : 0,
592 id : SENSORS_HARDWARE_MODULE_ID,
593 name : "MultiHal Sensor Module",
594 author : "Google, Inc",
595 methods : &sensors_module_methods,
596 dso : NULL,
597 reserved : {0},
598 },
599 get_sensors_list : module__get_sensors_list
600};
601
602static int open_sensors(const struct hw_module_t* hw_module, const char* name,
603 struct hw_device_t** hw_device_out) {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700604 ALOGV("open_sensors begin...");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700605
606 lazy_init_modules();
607
608 // Create proxy device, to return later.
609 sensors_poll_context_t *dev = new sensors_poll_context_t();
610 memset(dev, 0, sizeof(sensors_poll_device_1_t));
611 dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
612 dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_0;
613 dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
614 dev->proxy_device.common.close = device__close;
615 dev->proxy_device.activate = device__activate;
616 dev->proxy_device.setDelay = device__setDelay;
617 dev->proxy_device.poll = device__poll;
618 dev->proxy_device.batch = device__batch;
619 dev->proxy_device.flush = device__flush;
620
621 dev->nextReadIndex = 0;
622
623 // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
624 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
625 it != sub_hw_modules->end(); it++) {
626 sensors_module_t *sensors_module = (sensors_module_t*) *it;
627 struct hw_device_t* sub_hw_device;
628 int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700629 if (!sub_open_result)
630 dev->addSubHwDevice(sub_hw_device);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700631 }
632
633 // Prepare the output param and return
634 *hw_device_out = &dev->proxy_device.common;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700635 ALOGV("...open_sensors end");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700636 return 0;
637}