blob: f38d90d2271ea8c98531189c39ce56021ed73493 [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>
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -080030#include <string>
31#include <fstream>
Mike Lockwoodf8477622013-10-17 08:05:00 -070032#include <map>
Nick Vaccaroe914d692014-10-10 13:32:38 -070033#include <string>
Mike Lockwoodf8477622013-10-17 08:05:00 -070034
35#include <stdio.h>
36#include <dlfcn.h>
37#include <SensorEventQueue.h>
38
Ching Tzung Lin4fd217a2015-07-23 10:18:06 -070039#include <limits.h>
40#include <stdlib.h>
Mike Lockwoodf8477622013-10-17 08:05:00 -070041
42static const char* CONFIG_FILENAME = "/system/etc/sensors/hals.conf";
Mike Lockwoodf8477622013-10-17 08:05:00 -070043static const int MAX_CONF_LINE_LENGTH = 1024;
44
45static pthread_mutex_t init_modules_mutex = PTHREAD_MUTEX_INITIALIZER;
46static pthread_mutex_t init_sensors_mutex = PTHREAD_MUTEX_INITIALIZER;
47
48// This mutex is shared by all queues
49static 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.
52static pthread_cond_t data_available_cond = PTHREAD_COND_INITIALIZER;
53bool waiting_for_data = false;
54
55/*
Aaron Whyte4d7ac522014-04-09 15:56:54 -070056 * Vector of sub modules, whose indexes are referred to in this file as module_index.
Mike Lockwoodf8477622013-10-17 08:05:00 -070057 */
58static 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 */
65struct FullHandle {
66 int moduleIndex;
67 int localHandle;
68
69 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
79 bool operator==(const FullHandle &that) const {
80 return moduleIndex == that.moduleIndex && localHandle == that.localHandle;
81 }
82};
83
84std::map<int, FullHandle> global_to_full;
85std::map<FullHandle, int> full_to_global;
86int next_global_handle = 1;
87
88static 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
Aaron Whyte4d7ac522014-04-09 15:56:54 -070098// Returns the local handle, or -1 if it does not exist.
Mike Lockwoodf8477622013-10-17 08:05:00 -070099static int get_local_handle(int global_handle) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700100 if (global_to_full.count(global_handle) == 0) {
101 ALOGW("Unknown global_handle %d", global_handle);
102 return -1;
103 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700104 return global_to_full[global_handle].localHandle;
105}
106
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700107// 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.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700109static int get_module_index(int global_handle) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700110 if (global_to_full.count(global_handle) == 0) {
111 ALOGW("Unknown global_handle %d", global_handle);
112 return -1;
113 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700114 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
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700120// Returns the global handle for this full_handle, or -1 if the full_handle is unknown.
121static 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
Nick Vaccaro98d5e942016-02-09 14:56:17 -0800132static const int SENSOR_EVENT_QUEUE_CAPACITY = 36;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700133
134struct TaskContext {
135 sensors_poll_device_t* device;
136 SensorEventQueue* queue;
137};
138
139void *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);
Nick Vaccaro59d9fb42016-07-19 10:34:00 -0700158 if (eventsPolled <= 0) {
159 if (eventsPolled < 0) {
160 ALOGV("writerTask ignored error %d from %s", eventsPolled, device->common.module->name);
161 ALOGE("ERROR: Fix %s so it does not return error from poll()", device->common.module->name);
162 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700163 continue;
164 }
165 pthread_mutex_lock(&queue_mutex);
166 queue->markAsWritten(eventsPolled);
167 ALOGV("writerTask wrote %d events", eventsPolled);
168 if (waiting_for_data) {
169 ALOGV("writerTask - broadcast data_available_cond");
170 pthread_cond_broadcast(&data_available_cond);
171 }
172 pthread_mutex_unlock(&queue_mutex);
173 }
174 // never actually returns
175 return NULL;
176}
177
178/*
179 * Cache of all sensors, with original handles replaced by global handles.
180 * This will be handled to get_sensors_list() callers.
181 */
182static struct sensor_t const* global_sensors_list = NULL;
183static int global_sensors_count = -1;
184
185/*
186 * Extends a sensors_poll_device_1 by including all the sub-module's devices.
187 */
188struct sensors_poll_context_t {
189 /*
190 * This is the device that SensorDevice.cpp uses to make API calls
191 * to the multihal, which fans them out to sub-HALs.
192 */
193 sensors_poll_device_1 proxy_device; // must be first
194
195 void addSubHwDevice(struct hw_device_t*);
196
197 int activate(int handle, int enabled);
198 int setDelay(int handle, int64_t ns);
199 int poll(sensors_event_t* data, int count);
200 int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
201 int flush(int handle);
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700202 int inject_sensor_data(struct sensors_poll_device_1 *dev, const sensors_event_t *data);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700203 int close();
204
205 std::vector<hw_device_t*> sub_hw_devices;
206 std::vector<SensorEventQueue*> queues;
207 std::vector<pthread_t> threads;
208 int nextReadIndex;
209
210 sensors_poll_device_t* get_v0_device_by_handle(int global_handle);
211 sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle);
212 int get_device_version_by_handle(int global_handle);
213
214 void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index);
215};
216
217void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) {
218 ALOGV("addSubHwDevice");
219 this->sub_hw_devices.push_back(sub_hw_device);
220
221 SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY);
222 this->queues.push_back(queue);
223
224 TaskContext* taskContext = new TaskContext();
225 taskContext->device = (sensors_poll_device_t*) sub_hw_device;
226 taskContext->queue = queue;
227
228 pthread_t writerThread;
229 pthread_create(&writerThread, NULL, writerTask, taskContext);
230 this->threads.push_back(writerThread);
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_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) {
235 int sub_index = get_module_index(global_handle);
Nick Vaccaroa889c092016-04-15 10:17:07 -0700236 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700237 return NULL;
238 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700239 return (sensors_poll_device_t*) this->sub_hw_devices[sub_index];
240}
241
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700242// Returns the device pointer, or NULL if the global handle is invalid.
243sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) {
244 int sub_index = get_module_index(global_handle);
Nick Vaccaroa889c092016-04-15 10:17:07 -0700245 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700246 return NULL;
247 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700248 return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
249}
250
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700251// Returns the device version, or -1 if the handle is invalid.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700252int sensors_poll_context_t::get_device_version_by_handle(int handle) {
253 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700254 if (v0) {
255 return v0->common.version;
256 } else {
257 return -1;
258 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700259}
260
Nick Vaccaroe914d692014-10-10 13:32:38 -0700261// Android L requires sensor HALs to be either 1_0 or 1_3 compliant
262#define HAL_VERSION_IS_COMPLIANT(version) \
263 (version == SENSORS_DEVICE_API_VERSION_1_0 || version >= SENSORS_DEVICE_API_VERSION_1_3)
264
265// Returns true if HAL is compliant, false if HAL is not compliant or if handle is invalid
266static bool halIsCompliant(sensors_poll_context_t *ctx, int handle) {
267 int version = ctx->get_device_version_by_handle(handle);
268 return version != -1 && HAL_VERSION_IS_COMPLIANT(version);
269}
270
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700271static bool halIsAPILevelCompliant(sensors_poll_context_t *ctx, int handle, int level) {
272 int version = ctx->get_device_version_by_handle(handle);
273 return version != -1 && (version >= level);
274}
275
Nick Vaccaroe914d692014-10-10 13:32:38 -0700276const char *apiNumToStr(int version) {
277 switch(version) {
278 case SENSORS_DEVICE_API_VERSION_1_0:
279 return "SENSORS_DEVICE_API_VERSION_1_0";
280 case SENSORS_DEVICE_API_VERSION_1_1:
281 return "SENSORS_DEVICE_API_VERSION_1_1";
282 case SENSORS_DEVICE_API_VERSION_1_2:
283 return "SENSORS_DEVICE_API_VERSION_1_2";
284 case SENSORS_DEVICE_API_VERSION_1_3:
285 return "SENSORS_DEVICE_API_VERSION_1_3";
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700286 case SENSORS_DEVICE_API_VERSION_1_4:
287 return "SENSORS_DEVICE_API_VERSION_1_4";
Nick Vaccaroe914d692014-10-10 13:32:38 -0700288 default:
289 return "UNKNOWN";
290 }
291}
292
Mike Lockwoodf8477622013-10-17 08:05:00 -0700293int sensors_poll_context_t::activate(int handle, int enabled) {
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700294 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700295 ALOGV("activate");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700296 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700297 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700298 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700299 retval = v0->activate(v0, local_handle, enabled);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700300 } else {
301 ALOGE("IGNORING activate(enable %d) call to non-API-compliant sensor handle=%d !",
302 enabled, handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700303 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700304 ALOGV("retval %d", retval);
305 return retval;
306}
307
308int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700309 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700310 ALOGV("setDelay");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700311 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700312 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700313 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700314 retval = v0->setDelay(v0, local_handle, ns);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700315 } else {
316 ALOGE("IGNORING setDelay() call for non-API-compliant sensor handle=%d !", handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700317 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700318 ALOGV("retval %d", retval);
319 return retval;
320}
321
322void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src,
323 int sub_index) {
324 memcpy(dest, src, sizeof(struct sensors_event_t));
325 // A normal event's "sensor" field is a local handle. Convert it to a global handle.
326 // A meta-data event must have its sensor set to 0, but it has a nested event
327 // with a local handle that needs to be converted to a global handle.
328 FullHandle full_handle;
329 full_handle.moduleIndex = sub_index;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700330
Mike Lockwoodf8477622013-10-17 08:05:00 -0700331 // If it's a metadata event, rewrite the inner payload, not the sensor field.
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700332 // If the event's sensor field is unregistered for any reason, rewrite the sensor field
333 // with a -1, instead of writing an incorrect but plausible sensor number, because
334 // get_global_handle() returns -1 for unknown FullHandles.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700335 if (dest->type == SENSOR_TYPE_META_DATA) {
336 full_handle.localHandle = dest->meta_data.sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700337 dest->meta_data.sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700338 } else {
339 full_handle.localHandle = dest->sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700340 dest->sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700341 }
342}
343
344int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) {
345 ALOGV("poll");
346 int empties = 0;
Nick Vaccaroc384b182014-06-10 18:33:07 -0700347 int queueCount = 0;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700348 int eventsRead = 0;
349
350 pthread_mutex_lock(&queue_mutex);
Nick Vaccaroc384b182014-06-10 18:33:07 -0700351 queueCount = (int)this->queues.size();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700352 while (eventsRead == 0) {
353 while (empties < queueCount && eventsRead < maxReads) {
354 SensorEventQueue* queue = this->queues.at(this->nextReadIndex);
355 sensors_event_t* event = queue->peek();
356 if (event == NULL) {
357 empties++;
358 } else {
359 empties = 0;
Nick Vaccaroc384b182014-06-10 18:33:07 -0700360 this->copy_event_remap_handle(&data[eventsRead], event, nextReadIndex);
361 if (data[eventsRead].sensor == -1) {
362 // Bad handle, do not pass corrupted event upstream !
363 ALOGW("Dropping bad local handle event packet on the floor");
364 } else {
365 eventsRead++;
366 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700367 queue->dequeue();
368 }
369 this->nextReadIndex = (this->nextReadIndex + 1) % queueCount;
370 }
371 if (eventsRead == 0) {
372 // The queues have been scanned and none contain data, so wait.
373 ALOGV("poll stopping to wait for data");
374 waiting_for_data = true;
375 pthread_cond_wait(&data_available_cond, &queue_mutex);
376 waiting_for_data = false;
377 empties = 0;
378 }
379 }
380 pthread_mutex_unlock(&queue_mutex);
381 ALOGV("poll returning %d events.", eventsRead);
382
383 return eventsRead;
384}
385
386int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) {
387 ALOGV("batch");
388 int retval = -EINVAL;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700389 int local_handle = get_local_handle(handle);
390 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700391 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700392 retval = v1->batch(v1, local_handle, flags, period_ns, timeout);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700393 } else {
394 ALOGE("IGNORING batch() call to non-API-compliant sensor handle=%d !", handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700395 }
396 ALOGV("retval %d", retval);
397 return retval;
398}
399
400int sensors_poll_context_t::flush(int handle) {
401 ALOGV("flush");
402 int retval = -EINVAL;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700403 int local_handle = get_local_handle(handle);
404 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700405 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700406 retval = v1->flush(v1, local_handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700407 } else {
408 ALOGE("IGNORING flush() call to non-API-compliant sensor handle=%d !", handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700409 }
410 ALOGV("retval %d", retval);
411 return retval;
412}
413
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700414int sensors_poll_context_t::inject_sensor_data(struct sensors_poll_device_1 *dev,
415 const sensors_event_t *data) {
416 int retval = -EINVAL;
417 ALOGV("inject_sensor_data");
418 // Get handle for the sensor owning the event being injected
419 int local_handle = get_local_handle(data->sensor);
420 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(data->sensor);
421 if (halIsAPILevelCompliant(this, data->sensor, SENSORS_DEVICE_API_VERSION_1_4) &&
422 local_handle >= 0 && v1) {
423 retval = v1->inject_sensor_data(dev, data);
424 } else {
425 ALOGE("IGNORED inject_sensor_data(type=%d, handle=%d) call to non-API-compliant sensor",
426 data->type, data->sensor);
427 }
428 ALOGV("retval %d", retval);
429 return retval;
430
431}
432
Mike Lockwoodf8477622013-10-17 08:05:00 -0700433int sensors_poll_context_t::close() {
434 ALOGV("close");
435 for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
436 it != this->sub_hw_devices.end(); it++) {
437 hw_device_t* dev = *it;
438 int retval = dev->close(dev);
439 ALOGV("retval %d", retval);
440 }
441 return 0;
442}
443
444
445static int device__close(struct hw_device_t *dev) {
446 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
447 if (ctx != NULL) {
448 int retval = ctx->close();
449 delete ctx;
450 }
451 return 0;
452}
453
454static int device__activate(struct sensors_poll_device_t *dev, int handle,
455 int enabled) {
456 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
457 return ctx->activate(handle, enabled);
458}
459
460static int device__setDelay(struct sensors_poll_device_t *dev, int handle,
461 int64_t ns) {
462 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
463 return ctx->setDelay(handle, ns);
464}
465
466static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data,
467 int count) {
468 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
469 return ctx->poll(data, count);
470}
471
472static int device__batch(struct sensors_poll_device_1 *dev, int handle,
473 int flags, int64_t period_ns, int64_t timeout) {
474 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
475 return ctx->batch(handle, flags, period_ns, timeout);
476}
477
478static int device__flush(struct sensors_poll_device_1 *dev, int handle) {
479 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
480 return ctx->flush(handle);
481}
482
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700483static int device__inject_sensor_data(struct sensors_poll_device_1 *dev,
484 const sensors_event_t *data) {
485 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
486 return ctx->inject_sensor_data(dev, data);
487}
488
Mike Lockwoodf8477622013-10-17 08:05:00 -0700489static int open_sensors(const struct hw_module_t* module, const char* name,
490 struct hw_device_t** device);
491
492static bool starts_with(const char* s, const char* prefix) {
493 if (s == NULL || prefix == NULL) {
494 return false;
495 }
496 size_t s_size = strlen(s);
497 size_t prefix_size = strlen(prefix);
498 return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0;
499}
500
501/*
502 * Adds valid paths from the config file to the vector passed in.
503 * The vector must not be null.
504 */
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800505static void get_so_paths(std::vector<std::string> *so_paths) {
506 std::string line;
507 std::ifstream conf_file(CONFIG_FILENAME);
508
509 if(!conf_file) {
Mike Lockwoodf8477622013-10-17 08:05:00 -0700510 ALOGW("No multihal config file found at %s", CONFIG_FILENAME);
511 return;
512 }
Aaron Whytef22e1c12014-04-15 15:41:38 -0700513 ALOGV("Multihal config file found at %s", CONFIG_FILENAME);
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800514 while (std::getline(conf_file, line)) {
515 ALOGV("config file line: '%s'", line.c_str());
516 so_paths->push_back(line);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700517 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700518}
519
520/*
521 * Ensures that the sub-module array is initialized.
522 * This can be first called from get_sensors_list or from open_sensors.
523 */
524static void lazy_init_modules() {
525 pthread_mutex_lock(&init_modules_mutex);
526 if (sub_hw_modules != NULL) {
527 pthread_mutex_unlock(&init_modules_mutex);
528 return;
529 }
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800530 std::vector<std::string> *so_paths = new std::vector<std::string>();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700531 get_so_paths(so_paths);
532
533 // dlopen the module files and cache their module symbols in sub_hw_modules
534 sub_hw_modules = new std::vector<hw_module_t *>();
535 dlerror(); // clear any old errors
536 const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800537 for (std::vector<std::string>::iterator it = so_paths->begin(); it != so_paths->end(); it++) {
538 const char* path = it->c_str();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700539 void* lib_handle = dlopen(path, RTLD_LAZY);
540 if (lib_handle == NULL) {
541 ALOGW("dlerror(): %s", dlerror());
542 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700543 ALOGI("Loaded library from %s", path);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700544 ALOGV("Opening symbol \"%s\"", sym);
545 // clear old errors
546 dlerror();
547 struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym);
548 const char* error;
549 if ((error = dlerror()) != NULL) {
550 ALOGW("Error calling dlsym: %s", error);
551 } else if (module == NULL) {
552 ALOGW("module == NULL");
553 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700554 ALOGV("Loaded symbols from \"%s\"", sym);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700555 sub_hw_modules->push_back(module);
556 }
557 }
558 }
559 pthread_mutex_unlock(&init_modules_mutex);
560}
561
562/*
563 * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles.
564 */
565static void lazy_init_sensors_list() {
566 ALOGV("lazy_init_sensors_list");
567 pthread_mutex_lock(&init_sensors_mutex);
568 if (global_sensors_list != NULL) {
569 // already initialized
570 pthread_mutex_unlock(&init_sensors_mutex);
571 ALOGV("lazy_init_sensors_list - early return");
572 return;
573 }
574
575 ALOGV("lazy_init_sensors_list needs to do work");
576 lazy_init_modules();
577
578 // Count all the sensors, then allocate an array of blanks.
579 global_sensors_count = 0;
580 const struct sensor_t *subhal_sensors_list;
581 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
582 it != sub_hw_modules->end(); it++) {
583 struct sensors_module_t *module = (struct sensors_module_t*) *it;
584 global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list);
585 ALOGV("increased global_sensors_count to %d", global_sensors_count);
586 }
587
588 // The global_sensors_list is full of consts.
589 // Manipulate this non-const list, and point the const one to it when we're done.
590 sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count];
591
592 // index of the next sensor to set in mutable_sensor_list
593 int mutable_sensor_index = 0;
594 int module_index = 0;
595
596 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
597 it != sub_hw_modules->end(); it++) {
598 hw_module_t *hw_module = *it;
599 ALOGV("examine one module");
600 // Read the sub-module's sensor list.
601 struct sensors_module_t *module = (struct sensors_module_t*) hw_module;
602 int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list);
603 ALOGV("the module has %d sensors", module_sensor_count);
604
605 // Copy the HAL's sensor list into global_sensors_list,
606 // with the handle changed to be a global handle.
607 for (int i = 0; i < module_sensor_count; i++) {
608 ALOGV("examining one sensor");
609 const struct sensor_t *local_sensor = &subhal_sensors_list[i];
610 int local_handle = local_sensor->handle;
611 memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
612 sizeof(struct sensor_t));
613
614 // Overwrite the global version's handle with a global handle.
615 int global_handle = assign_global_handle(module_index, local_handle);
616
617 mutable_sensor_list[mutable_sensor_index].handle = global_handle;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700618 ALOGV("module_index %d, local_handle %d, global_handle %d",
Mike Lockwoodf8477622013-10-17 08:05:00 -0700619 module_index, local_handle, global_handle);
620
621 mutable_sensor_index++;
622 }
623 module_index++;
624 }
625 // Set the const static global_sensors_list to the mutable one allocated by this function.
626 global_sensors_list = mutable_sensor_list;
627
628 pthread_mutex_unlock(&init_sensors_mutex);
629 ALOGV("end lazy_init_sensors_list");
630}
631
Nick Vaccaroe914d692014-10-10 13:32:38 -0700632static int module__get_sensors_list(__unused struct sensors_module_t* module,
Mike Lockwoodf8477622013-10-17 08:05:00 -0700633 struct sensor_t const** list) {
634 ALOGV("module__get_sensors_list start");
635 lazy_init_sensors_list();
636 *list = global_sensors_list;
637 ALOGV("global_sensors_count: %d", global_sensors_count);
638 for (int i = 0; i < global_sensors_count; i++) {
639 ALOGV("sensor type: %d", global_sensors_list[i].type);
640 }
641 return global_sensors_count;
642}
643
644static struct hw_module_methods_t sensors_module_methods = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700645 .open = open_sensors
Mike Lockwoodf8477622013-10-17 08:05:00 -0700646};
647
648struct sensors_module_t HAL_MODULE_INFO_SYM = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700649 .common = {
650 .tag = HARDWARE_MODULE_TAG,
651 .version_major = 1,
652 .version_minor = 1,
653 .id = SENSORS_HARDWARE_MODULE_ID,
654 .name = "MultiHal Sensor Module",
655 .author = "Google, Inc",
656 .methods = &sensors_module_methods,
657 .dso = NULL,
658 .reserved = {0},
Mike Lockwoodf8477622013-10-17 08:05:00 -0700659 },
Nick Vaccaroa889c092016-04-15 10:17:07 -0700660 .get_sensors_list = module__get_sensors_list
Mike Lockwoodf8477622013-10-17 08:05:00 -0700661};
662
663static int open_sensors(const struct hw_module_t* hw_module, const char* name,
664 struct hw_device_t** hw_device_out) {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700665 ALOGV("open_sensors begin...");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700666
667 lazy_init_modules();
668
669 // Create proxy device, to return later.
670 sensors_poll_context_t *dev = new sensors_poll_context_t();
671 memset(dev, 0, sizeof(sensors_poll_device_1_t));
672 dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700673 dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_4;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700674 dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
675 dev->proxy_device.common.close = device__close;
676 dev->proxy_device.activate = device__activate;
677 dev->proxy_device.setDelay = device__setDelay;
678 dev->proxy_device.poll = device__poll;
679 dev->proxy_device.batch = device__batch;
680 dev->proxy_device.flush = device__flush;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700681 dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700682
683 dev->nextReadIndex = 0;
684
685 // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
686 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
687 it != sub_hw_modules->end(); it++) {
688 sensors_module_t *sensors_module = (sensors_module_t*) *it;
689 struct hw_device_t* sub_hw_device;
690 int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700691 if (!sub_open_result) {
692 if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) {
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700693 ALOGE("SENSORS_DEVICE_API_VERSION_1_3 or newer is required for all sensor HALs");
Nick Vaccaroe914d692014-10-10 13:32:38 -0700694 ALOGE("This HAL reports non-compliant API level : %s",
695 apiNumToStr(sub_hw_device->version));
696 ALOGE("Sensors belonging to this HAL will get ignored !");
697 }
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700698 dev->addSubHwDevice(sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700699 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700700 }
701
702 // Prepare the output param and return
703 *hw_device_out = &dev->proxy_device.common;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700704 ALOGV("...open_sensors end");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700705 return 0;
706}