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