blob: 2810118faee90f4db006b93832a2bd05bdd430b1 [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
Nick Vaccaro8a837082016-12-12 16:50:57 -080017#include "SensorEventQueue.h"
18#include "multihal.h"
Mike Lockwoodf8477622013-10-17 08:05:00 -070019
20#define LOG_NDEBUG 1
21#include <cutils/log.h>
Nick Vaccaro8a837082016-12-12 16:50:57 -080022#include <cutils/atomic.h>
23#include <hardware/sensors.h>
Mike Lockwoodf8477622013-10-17 08:05:00 -070024
25#include <vector>
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -080026#include <string>
27#include <fstream>
Mike Lockwoodf8477622013-10-17 08:05:00 -070028#include <map>
29
Nick Vaccaro8a837082016-12-12 16:50:57 -080030#include <dirent.h>
Mike Lockwoodf8477622013-10-17 08:05:00 -070031#include <dlfcn.h>
Nick Vaccaro8a837082016-12-12 16:50:57 -080032#include <errno.h>
33#include <fcntl.h>
Ching Tzung Lin4fd217a2015-07-23 10:18:06 -070034#include <limits.h>
Nick Vaccaro8a837082016-12-12 16:50:57 -080035#include <math.h>
36#include <poll.h>
37#include <pthread.h>
38#include <stdio.h>
Ching Tzung Lin4fd217a2015-07-23 10:18:06 -070039#include <stdlib.h>
Mike Lockwoodf8477622013-10-17 08:05:00 -070040
Mike Lockwoodf8477622013-10-17 08:05:00 -070041
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
Nick Vaccaro98d5e942016-02-09 14:56:17 -0800129static const int SENSOR_EVENT_QUEUE_CAPACITY = 36;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700130
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);
Nick Vaccaro59d9fb42016-07-19 10:34:00 -0700155 if (eventsPolled <= 0) {
156 if (eventsPolled < 0) {
157 ALOGV("writerTask ignored error %d from %s", eventsPolled, device->common.module->name);
158 ALOGE("ERROR: Fix %s so it does not return error from poll()", device->common.module->name);
159 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700160 continue;
161 }
162 pthread_mutex_lock(&queue_mutex);
163 queue->markAsWritten(eventsPolled);
164 ALOGV("writerTask wrote %d events", eventsPolled);
165 if (waiting_for_data) {
166 ALOGV("writerTask - broadcast data_available_cond");
167 pthread_cond_broadcast(&data_available_cond);
168 }
169 pthread_mutex_unlock(&queue_mutex);
170 }
171 // never actually returns
172 return NULL;
173}
174
175/*
176 * Cache of all sensors, with original handles replaced by global handles.
177 * This will be handled to get_sensors_list() callers.
178 */
179static struct sensor_t const* global_sensors_list = NULL;
180static int global_sensors_count = -1;
181
182/*
183 * Extends a sensors_poll_device_1 by including all the sub-module's devices.
184 */
185struct sensors_poll_context_t {
186 /*
187 * This is the device that SensorDevice.cpp uses to make API calls
188 * to the multihal, which fans them out to sub-HALs.
189 */
190 sensors_poll_device_1 proxy_device; // must be first
191
192 void addSubHwDevice(struct hw_device_t*);
193
194 int activate(int handle, int enabled);
195 int setDelay(int handle, int64_t ns);
196 int poll(sensors_event_t* data, int count);
197 int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
198 int flush(int handle);
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700199 int inject_sensor_data(struct sensors_poll_device_1 *dev, const sensors_event_t *data);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700200 int close();
201
202 std::vector<hw_device_t*> sub_hw_devices;
203 std::vector<SensorEventQueue*> queues;
204 std::vector<pthread_t> threads;
205 int nextReadIndex;
206
207 sensors_poll_device_t* get_v0_device_by_handle(int global_handle);
208 sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle);
209 int get_device_version_by_handle(int global_handle);
210
211 void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index);
212};
213
214void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) {
215 ALOGV("addSubHwDevice");
216 this->sub_hw_devices.push_back(sub_hw_device);
217
218 SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY);
219 this->queues.push_back(queue);
220
221 TaskContext* taskContext = new TaskContext();
222 taskContext->device = (sensors_poll_device_t*) sub_hw_device;
223 taskContext->queue = queue;
224
225 pthread_t writerThread;
226 pthread_create(&writerThread, NULL, writerTask, taskContext);
227 this->threads.push_back(writerThread);
228}
229
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700230// Returns the device pointer, or NULL if the global handle is invalid.
231sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) {
232 int sub_index = get_module_index(global_handle);
Nick Vaccaroa889c092016-04-15 10:17:07 -0700233 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700234 return NULL;
235 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700236 return (sensors_poll_device_t*) this->sub_hw_devices[sub_index];
237}
238
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700239// Returns the device pointer, or NULL if the global handle is invalid.
240sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) {
241 int sub_index = get_module_index(global_handle);
Nick Vaccaroa889c092016-04-15 10:17:07 -0700242 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700243 return NULL;
244 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700245 return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
246}
247
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700248// Returns the device version, or -1 if the handle is invalid.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700249int sensors_poll_context_t::get_device_version_by_handle(int handle) {
250 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700251 if (v0) {
252 return v0->common.version;
253 } else {
254 return -1;
255 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700256}
257
Ashutosh Joshi9eb5bbd2017-01-09 17:09:35 -0800258// Android N and hire require sensor HALs to be at least 1_3 compliant
Nick Vaccaroe914d692014-10-10 13:32:38 -0700259#define HAL_VERSION_IS_COMPLIANT(version) \
Ashutosh Joshi9eb5bbd2017-01-09 17:09:35 -0800260 (version >= SENSORS_DEVICE_API_VERSION_1_3)
Nick Vaccaroe914d692014-10-10 13:32:38 -0700261
262// Returns true if HAL is compliant, false if HAL is not compliant or if handle is invalid
263static bool halIsCompliant(sensors_poll_context_t *ctx, int handle) {
264 int version = ctx->get_device_version_by_handle(handle);
265 return version != -1 && HAL_VERSION_IS_COMPLIANT(version);
266}
267
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700268static bool halIsAPILevelCompliant(sensors_poll_context_t *ctx, int handle, int level) {
269 int version = ctx->get_device_version_by_handle(handle);
270 return version != -1 && (version >= level);
271}
272
Nick Vaccaroe914d692014-10-10 13:32:38 -0700273const char *apiNumToStr(int version) {
274 switch(version) {
275 case SENSORS_DEVICE_API_VERSION_1_0:
276 return "SENSORS_DEVICE_API_VERSION_1_0";
277 case SENSORS_DEVICE_API_VERSION_1_1:
278 return "SENSORS_DEVICE_API_VERSION_1_1";
279 case SENSORS_DEVICE_API_VERSION_1_2:
280 return "SENSORS_DEVICE_API_VERSION_1_2";
281 case SENSORS_DEVICE_API_VERSION_1_3:
282 return "SENSORS_DEVICE_API_VERSION_1_3";
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700283 case SENSORS_DEVICE_API_VERSION_1_4:
284 return "SENSORS_DEVICE_API_VERSION_1_4";
Nick Vaccaroe914d692014-10-10 13:32:38 -0700285 default:
286 return "UNKNOWN";
287 }
288}
289
Mike Lockwoodf8477622013-10-17 08:05:00 -0700290int sensors_poll_context_t::activate(int handle, int enabled) {
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700291 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700292 ALOGV("activate");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700293 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700294 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700295 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700296 retval = v0->activate(v0, local_handle, enabled);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700297 } else {
298 ALOGE("IGNORING activate(enable %d) call to non-API-compliant sensor handle=%d !",
299 enabled, handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700300 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700301 ALOGV("retval %d", retval);
302 return retval;
303}
304
305int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700306 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700307 ALOGV("setDelay");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700308 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700309 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700310 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700311 retval = v0->setDelay(v0, local_handle, ns);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700312 } else {
313 ALOGE("IGNORING setDelay() call for non-API-compliant sensor handle=%d !", handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700314 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700315 ALOGV("retval %d", retval);
316 return retval;
317}
318
319void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src,
320 int sub_index) {
321 memcpy(dest, src, sizeof(struct sensors_event_t));
322 // A normal event's "sensor" field is a local handle. Convert it to a global handle.
323 // A meta-data event must have its sensor set to 0, but it has a nested event
324 // with a local handle that needs to be converted to a global handle.
325 FullHandle full_handle;
326 full_handle.moduleIndex = sub_index;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700327
Mike Lockwoodf8477622013-10-17 08:05:00 -0700328 // If it's a metadata event, rewrite the inner payload, not the sensor field.
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700329 // If the event's sensor field is unregistered for any reason, rewrite the sensor field
330 // with a -1, instead of writing an incorrect but plausible sensor number, because
331 // get_global_handle() returns -1 for unknown FullHandles.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700332 if (dest->type == SENSOR_TYPE_META_DATA) {
333 full_handle.localHandle = dest->meta_data.sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700334 dest->meta_data.sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700335 } else {
336 full_handle.localHandle = dest->sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700337 dest->sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700338 }
339}
340
341int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) {
342 ALOGV("poll");
343 int empties = 0;
Nick Vaccaroc384b182014-06-10 18:33:07 -0700344 int queueCount = 0;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700345 int eventsRead = 0;
346
347 pthread_mutex_lock(&queue_mutex);
Nick Vaccaroc384b182014-06-10 18:33:07 -0700348 queueCount = (int)this->queues.size();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700349 while (eventsRead == 0) {
350 while (empties < queueCount && eventsRead < maxReads) {
351 SensorEventQueue* queue = this->queues.at(this->nextReadIndex);
352 sensors_event_t* event = queue->peek();
353 if (event == NULL) {
354 empties++;
355 } else {
356 empties = 0;
Nick Vaccaroc384b182014-06-10 18:33:07 -0700357 this->copy_event_remap_handle(&data[eventsRead], event, nextReadIndex);
358 if (data[eventsRead].sensor == -1) {
359 // Bad handle, do not pass corrupted event upstream !
360 ALOGW("Dropping bad local handle event packet on the floor");
361 } else {
362 eventsRead++;
363 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700364 queue->dequeue();
365 }
366 this->nextReadIndex = (this->nextReadIndex + 1) % queueCount;
367 }
368 if (eventsRead == 0) {
369 // The queues have been scanned and none contain data, so wait.
370 ALOGV("poll stopping to wait for data");
371 waiting_for_data = true;
372 pthread_cond_wait(&data_available_cond, &queue_mutex);
373 waiting_for_data = false;
374 empties = 0;
375 }
376 }
377 pthread_mutex_unlock(&queue_mutex);
378 ALOGV("poll returning %d events.", eventsRead);
379
380 return eventsRead;
381}
382
383int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) {
384 ALOGV("batch");
385 int retval = -EINVAL;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700386 int local_handle = get_local_handle(handle);
387 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700388 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700389 retval = v1->batch(v1, local_handle, flags, period_ns, timeout);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700390 } else {
391 ALOGE("IGNORING batch() call to non-API-compliant sensor handle=%d !", handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700392 }
393 ALOGV("retval %d", retval);
394 return retval;
395}
396
397int sensors_poll_context_t::flush(int handle) {
398 ALOGV("flush");
399 int retval = -EINVAL;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700400 int local_handle = get_local_handle(handle);
401 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700402 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700403 retval = v1->flush(v1, local_handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700404 } else {
405 ALOGE("IGNORING flush() call to non-API-compliant sensor handle=%d !", handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700406 }
407 ALOGV("retval %d", retval);
408 return retval;
409}
410
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700411int sensors_poll_context_t::inject_sensor_data(struct sensors_poll_device_1 *dev,
412 const sensors_event_t *data) {
413 int retval = -EINVAL;
414 ALOGV("inject_sensor_data");
415 // Get handle for the sensor owning the event being injected
416 int local_handle = get_local_handle(data->sensor);
417 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(data->sensor);
418 if (halIsAPILevelCompliant(this, data->sensor, SENSORS_DEVICE_API_VERSION_1_4) &&
419 local_handle >= 0 && v1) {
420 retval = v1->inject_sensor_data(dev, data);
421 } else {
422 ALOGE("IGNORED inject_sensor_data(type=%d, handle=%d) call to non-API-compliant sensor",
423 data->type, data->sensor);
424 }
425 ALOGV("retval %d", retval);
426 return retval;
427
428}
429
Mike Lockwoodf8477622013-10-17 08:05:00 -0700430int sensors_poll_context_t::close() {
431 ALOGV("close");
432 for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
433 it != this->sub_hw_devices.end(); it++) {
434 hw_device_t* dev = *it;
435 int retval = dev->close(dev);
436 ALOGV("retval %d", retval);
437 }
438 return 0;
439}
440
441
442static int device__close(struct hw_device_t *dev) {
443 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
444 if (ctx != NULL) {
445 int retval = ctx->close();
446 delete ctx;
447 }
448 return 0;
449}
450
451static int device__activate(struct sensors_poll_device_t *dev, int handle,
452 int enabled) {
453 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
454 return ctx->activate(handle, enabled);
455}
456
457static int device__setDelay(struct sensors_poll_device_t *dev, int handle,
458 int64_t ns) {
459 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
460 return ctx->setDelay(handle, ns);
461}
462
463static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data,
464 int count) {
465 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
466 return ctx->poll(data, count);
467}
468
469static int device__batch(struct sensors_poll_device_1 *dev, int handle,
470 int flags, int64_t period_ns, int64_t timeout) {
471 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
472 return ctx->batch(handle, flags, period_ns, timeout);
473}
474
475static int device__flush(struct sensors_poll_device_1 *dev, int handle) {
476 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
477 return ctx->flush(handle);
478}
479
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700480static int device__inject_sensor_data(struct sensors_poll_device_1 *dev,
481 const sensors_event_t *data) {
482 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
483 return ctx->inject_sensor_data(dev, data);
484}
485
Mike Lockwoodf8477622013-10-17 08:05:00 -0700486static int open_sensors(const struct hw_module_t* module, const char* name,
487 struct hw_device_t** device);
488
489static bool starts_with(const char* s, const char* prefix) {
490 if (s == NULL || prefix == NULL) {
491 return false;
492 }
493 size_t s_size = strlen(s);
494 size_t prefix_size = strlen(prefix);
495 return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0;
496}
497
498/*
499 * Adds valid paths from the config file to the vector passed in.
500 * The vector must not be null.
501 */
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800502static void get_so_paths(std::vector<std::string> *so_paths) {
503 std::string line;
Nick Vaccaro8a837082016-12-12 16:50:57 -0800504 std::ifstream conf_file(MULTI_HAL_CONFIG_FILE_PATH);
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800505
506 if(!conf_file) {
Nick Vaccaro8a837082016-12-12 16:50:57 -0800507 ALOGW("No multihal config file found at %s", MULTI_HAL_CONFIG_FILE_PATH);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700508 return;
509 }
Nick Vaccaro8a837082016-12-12 16:50:57 -0800510 ALOGV("Multihal config file found at %s", MULTI_HAL_CONFIG_FILE_PATH);
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800511 while (std::getline(conf_file, line)) {
512 ALOGV("config file line: '%s'", line.c_str());
513 so_paths->push_back(line);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700514 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700515}
516
517/*
518 * Ensures that the sub-module array is initialized.
519 * This can be first called from get_sensors_list or from open_sensors.
520 */
521static void lazy_init_modules() {
522 pthread_mutex_lock(&init_modules_mutex);
523 if (sub_hw_modules != NULL) {
524 pthread_mutex_unlock(&init_modules_mutex);
525 return;
526 }
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800527 std::vector<std::string> *so_paths = new std::vector<std::string>();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700528 get_so_paths(so_paths);
529
530 // dlopen the module files and cache their module symbols in sub_hw_modules
531 sub_hw_modules = new std::vector<hw_module_t *>();
532 dlerror(); // clear any old errors
533 const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800534 for (std::vector<std::string>::iterator it = so_paths->begin(); it != so_paths->end(); it++) {
535 const char* path = it->c_str();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700536 void* lib_handle = dlopen(path, RTLD_LAZY);
537 if (lib_handle == NULL) {
538 ALOGW("dlerror(): %s", dlerror());
539 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700540 ALOGI("Loaded library from %s", path);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700541 ALOGV("Opening symbol \"%s\"", sym);
542 // clear old errors
543 dlerror();
544 struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym);
545 const char* error;
546 if ((error = dlerror()) != NULL) {
547 ALOGW("Error calling dlsym: %s", error);
548 } else if (module == NULL) {
549 ALOGW("module == NULL");
550 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700551 ALOGV("Loaded symbols from \"%s\"", sym);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700552 sub_hw_modules->push_back(module);
553 }
554 }
555 }
556 pthread_mutex_unlock(&init_modules_mutex);
557}
558
559/*
560 * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles.
561 */
562static void lazy_init_sensors_list() {
563 ALOGV("lazy_init_sensors_list");
564 pthread_mutex_lock(&init_sensors_mutex);
565 if (global_sensors_list != NULL) {
566 // already initialized
567 pthread_mutex_unlock(&init_sensors_mutex);
568 ALOGV("lazy_init_sensors_list - early return");
569 return;
570 }
571
572 ALOGV("lazy_init_sensors_list needs to do work");
573 lazy_init_modules();
574
575 // Count all the sensors, then allocate an array of blanks.
576 global_sensors_count = 0;
577 const struct sensor_t *subhal_sensors_list;
578 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
579 it != sub_hw_modules->end(); it++) {
580 struct sensors_module_t *module = (struct sensors_module_t*) *it;
581 global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list);
582 ALOGV("increased global_sensors_count to %d", global_sensors_count);
583 }
584
585 // The global_sensors_list is full of consts.
586 // Manipulate this non-const list, and point the const one to it when we're done.
587 sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count];
588
589 // index of the next sensor to set in mutable_sensor_list
590 int mutable_sensor_index = 0;
591 int module_index = 0;
592
593 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
594 it != sub_hw_modules->end(); it++) {
595 hw_module_t *hw_module = *it;
596 ALOGV("examine one module");
597 // Read the sub-module's sensor list.
598 struct sensors_module_t *module = (struct sensors_module_t*) hw_module;
599 int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list);
600 ALOGV("the module has %d sensors", module_sensor_count);
601
602 // Copy the HAL's sensor list into global_sensors_list,
603 // with the handle changed to be a global handle.
604 for (int i = 0; i < module_sensor_count; i++) {
605 ALOGV("examining one sensor");
606 const struct sensor_t *local_sensor = &subhal_sensors_list[i];
607 int local_handle = local_sensor->handle;
608 memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
609 sizeof(struct sensor_t));
610
611 // Overwrite the global version's handle with a global handle.
612 int global_handle = assign_global_handle(module_index, local_handle);
613
614 mutable_sensor_list[mutable_sensor_index].handle = global_handle;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700615 ALOGV("module_index %d, local_handle %d, global_handle %d",
Mike Lockwoodf8477622013-10-17 08:05:00 -0700616 module_index, local_handle, global_handle);
617
618 mutable_sensor_index++;
619 }
620 module_index++;
621 }
622 // Set the const static global_sensors_list to the mutable one allocated by this function.
623 global_sensors_list = mutable_sensor_list;
624
625 pthread_mutex_unlock(&init_sensors_mutex);
626 ALOGV("end lazy_init_sensors_list");
627}
628
Nick Vaccaroe914d692014-10-10 13:32:38 -0700629static int module__get_sensors_list(__unused struct sensors_module_t* module,
Mike Lockwoodf8477622013-10-17 08:05:00 -0700630 struct sensor_t const** list) {
631 ALOGV("module__get_sensors_list start");
632 lazy_init_sensors_list();
633 *list = global_sensors_list;
634 ALOGV("global_sensors_count: %d", global_sensors_count);
635 for (int i = 0; i < global_sensors_count; i++) {
636 ALOGV("sensor type: %d", global_sensors_list[i].type);
637 }
638 return global_sensors_count;
639}
640
641static struct hw_module_methods_t sensors_module_methods = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700642 .open = open_sensors
Mike Lockwoodf8477622013-10-17 08:05:00 -0700643};
644
645struct sensors_module_t HAL_MODULE_INFO_SYM = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700646 .common = {
647 .tag = HARDWARE_MODULE_TAG,
648 .version_major = 1,
649 .version_minor = 1,
650 .id = SENSORS_HARDWARE_MODULE_ID,
651 .name = "MultiHal Sensor Module",
652 .author = "Google, Inc",
653 .methods = &sensors_module_methods,
654 .dso = NULL,
655 .reserved = {0},
Mike Lockwoodf8477622013-10-17 08:05:00 -0700656 },
Nick Vaccaroa889c092016-04-15 10:17:07 -0700657 .get_sensors_list = module__get_sensors_list
Mike Lockwoodf8477622013-10-17 08:05:00 -0700658};
659
Nick Vaccaro8a837082016-12-12 16:50:57 -0800660struct sensors_module_t *get_multi_hal_module_info() {
661 return (&HAL_MODULE_INFO_SYM);
662}
663
Mike Lockwoodf8477622013-10-17 08:05:00 -0700664static int open_sensors(const struct hw_module_t* hw_module, const char* name,
665 struct hw_device_t** hw_device_out) {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700666 ALOGV("open_sensors begin...");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700667
668 lazy_init_modules();
669
670 // Create proxy device, to return later.
671 sensors_poll_context_t *dev = new sensors_poll_context_t();
672 memset(dev, 0, sizeof(sensors_poll_device_1_t));
673 dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700674 dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_4;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700675 dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
676 dev->proxy_device.common.close = device__close;
677 dev->proxy_device.activate = device__activate;
678 dev->proxy_device.setDelay = device__setDelay;
679 dev->proxy_device.poll = device__poll;
680 dev->proxy_device.batch = device__batch;
681 dev->proxy_device.flush = device__flush;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700682 dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700683
684 dev->nextReadIndex = 0;
685
686 // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
687 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
688 it != sub_hw_modules->end(); it++) {
689 sensors_module_t *sensors_module = (sensors_module_t*) *it;
690 struct hw_device_t* sub_hw_device;
691 int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700692 if (!sub_open_result) {
693 if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) {
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700694 ALOGE("SENSORS_DEVICE_API_VERSION_1_3 or newer is required for all sensor HALs");
Nick Vaccaroe914d692014-10-10 13:32:38 -0700695 ALOGE("This HAL reports non-compliant API level : %s",
696 apiNumToStr(sub_hw_device->version));
697 ALOGE("Sensors belonging to this HAL will get ignored !");
698 }
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700699 dev->addSubHwDevice(sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700700 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700701 }
702
703 // Prepare the output param and return
704 *hw_device_out = &dev->proxy_device.common;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700705 ALOGV("...open_sensors end");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700706 return 0;
707}