blob: b5360cc88be27bab0257db390583099a7e190787 [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
Peng Xud8871a72017-08-09 23:02:23 -070052// Vector of sub modules, whose indexes are referred to in this file as module_index.
53static std::vector<hw_module_t *> *sub_hw_modules = nullptr;
54
55// Vector of sub modules shared object handles
56static std::vector<void *> *so_handles = nullptr;
Mike Lockwoodf8477622013-10-17 08:05:00 -070057
58/*
59 * Comparable class that globally identifies a sensor, by module index and local handle.
60 * A module index is the module's index in sub_hw_modules.
61 * A local handle is the handle the sub-module assigns to a sensor.
62 */
63struct FullHandle {
64 int moduleIndex;
65 int localHandle;
66
67 bool operator<(const FullHandle &that) const {
68 if (moduleIndex < that.moduleIndex) {
69 return true;
70 }
71 if (moduleIndex > that.moduleIndex) {
72 return false;
73 }
74 return localHandle < that.localHandle;
75 }
76
77 bool operator==(const FullHandle &that) const {
78 return moduleIndex == that.moduleIndex && localHandle == that.localHandle;
79 }
80};
81
82std::map<int, FullHandle> global_to_full;
83std::map<FullHandle, int> full_to_global;
84int next_global_handle = 1;
85
86static int assign_global_handle(int module_index, int local_handle) {
87 int global_handle = next_global_handle++;
88 FullHandle full_handle;
89 full_handle.moduleIndex = module_index;
90 full_handle.localHandle = local_handle;
91 full_to_global[full_handle] = global_handle;
92 global_to_full[global_handle] = full_handle;
93 return global_handle;
94}
95
Aaron Whyte4d7ac522014-04-09 15:56:54 -070096// Returns the local handle, or -1 if it does not exist.
Mike Lockwoodf8477622013-10-17 08:05:00 -070097static int get_local_handle(int global_handle) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -070098 if (global_to_full.count(global_handle) == 0) {
99 ALOGW("Unknown global_handle %d", global_handle);
100 return -1;
101 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700102 return global_to_full[global_handle].localHandle;
103}
104
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700105// Returns the sub_hw_modules index of the module that contains the sensor associates with this
106// global_handle, or -1 if that global_handle does not exist.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700107static int get_module_index(int global_handle) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700108 if (global_to_full.count(global_handle) == 0) {
109 ALOGW("Unknown global_handle %d", global_handle);
110 return -1;
111 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700112 FullHandle f = global_to_full[global_handle];
113 ALOGV("FullHandle for global_handle %d: moduleIndex %d, localHandle %d",
114 global_handle, f.moduleIndex, f.localHandle);
115 return f.moduleIndex;
116}
117
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700118// Returns the global handle for this full_handle, or -1 if the full_handle is unknown.
119static int get_global_handle(FullHandle* full_handle) {
120 int global_handle = -1;
121 if (full_to_global.count(*full_handle)) {
122 global_handle = full_to_global[*full_handle];
123 } else {
124 ALOGW("Unknown FullHandle: moduleIndex %d, localHandle %d",
125 full_handle->moduleIndex, full_handle->localHandle);
126 }
127 return global_handle;
128}
129
Nick Vaccaro98d5e942016-02-09 14:56:17 -0800130static const int SENSOR_EVENT_QUEUE_CAPACITY = 36;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700131
132struct TaskContext {
133 sensors_poll_device_t* device;
134 SensorEventQueue* queue;
135};
136
137void *writerTask(void* ptr) {
138 ALOGV("writerTask STARTS");
139 TaskContext* ctx = (TaskContext*)ptr;
140 sensors_poll_device_t* device = ctx->device;
141 SensorEventQueue* queue = ctx->queue;
142 sensors_event_t* buffer;
143 int eventsPolled;
144 while (1) {
145 pthread_mutex_lock(&queue_mutex);
146 if (queue->waitForSpace(&queue_mutex)) {
147 ALOGV("writerTask waited for space");
148 }
149 int bufferSize = queue->getWritableRegion(SENSOR_EVENT_QUEUE_CAPACITY, &buffer);
150 // Do blocking poll outside of lock
151 pthread_mutex_unlock(&queue_mutex);
152
153 ALOGV("writerTask before poll() - bufferSize = %d", bufferSize);
154 eventsPolled = device->poll(device, buffer, bufferSize);
155 ALOGV("writerTask poll() got %d events.", eventsPolled);
Nick Vaccaro59d9fb42016-07-19 10:34:00 -0700156 if (eventsPolled <= 0) {
157 if (eventsPolled < 0) {
158 ALOGV("writerTask ignored error %d from %s", eventsPolled, device->common.module->name);
159 ALOGE("ERROR: Fix %s so it does not return error from poll()", device->common.module->name);
160 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700161 continue;
162 }
163 pthread_mutex_lock(&queue_mutex);
164 queue->markAsWritten(eventsPolled);
165 ALOGV("writerTask wrote %d events", eventsPolled);
166 if (waiting_for_data) {
167 ALOGV("writerTask - broadcast data_available_cond");
168 pthread_cond_broadcast(&data_available_cond);
169 }
170 pthread_mutex_unlock(&queue_mutex);
171 }
172 // never actually returns
173 return NULL;
174}
175
176/*
177 * Cache of all sensors, with original handles replaced by global handles.
178 * This will be handled to get_sensors_list() callers.
179 */
180static struct sensor_t const* global_sensors_list = NULL;
181static int global_sensors_count = -1;
182
183/*
184 * Extends a sensors_poll_device_1 by including all the sub-module's devices.
185 */
186struct sensors_poll_context_t {
187 /*
188 * This is the device that SensorDevice.cpp uses to make API calls
189 * to the multihal, which fans them out to sub-HALs.
190 */
191 sensors_poll_device_1 proxy_device; // must be first
192
193 void addSubHwDevice(struct hw_device_t*);
194
195 int activate(int handle, int enabled);
196 int setDelay(int handle, int64_t ns);
197 int poll(sensors_event_t* data, int count);
198 int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
199 int flush(int handle);
Peng Xu15b43e92017-04-27 15:45:44 -0700200 int inject_sensor_data(const sensors_event_t *data);
201 int register_direct_channel(const struct sensors_direct_mem_t* mem,
202 int channel_handle);
203 int config_direct_report(int sensor_handle,
204 int channel_handle,
205 const struct sensors_direct_cfg_t *config);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700206 int close();
207
208 std::vector<hw_device_t*> sub_hw_devices;
209 std::vector<SensorEventQueue*> queues;
210 std::vector<pthread_t> threads;
211 int nextReadIndex;
212
213 sensors_poll_device_t* get_v0_device_by_handle(int global_handle);
214 sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle);
Peng Xu15b43e92017-04-27 15:45:44 -0700215 sensors_poll_device_1_t* get_primary_v1_device();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700216 int get_device_version_by_handle(int global_handle);
217
218 void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index);
219};
220
221void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) {
222 ALOGV("addSubHwDevice");
223 this->sub_hw_devices.push_back(sub_hw_device);
224
225 SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY);
226 this->queues.push_back(queue);
227
228 TaskContext* taskContext = new TaskContext();
229 taskContext->device = (sensors_poll_device_t*) sub_hw_device;
230 taskContext->queue = queue;
231
232 pthread_t writerThread;
233 pthread_create(&writerThread, NULL, writerTask, taskContext);
234 this->threads.push_back(writerThread);
235}
236
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700237// Returns the device pointer, or NULL if the global handle is invalid.
238sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) {
239 int sub_index = get_module_index(global_handle);
Nick Vaccaroa889c092016-04-15 10:17:07 -0700240 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700241 return NULL;
242 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700243 return (sensors_poll_device_t*) this->sub_hw_devices[sub_index];
244}
245
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700246// Returns the device pointer, or NULL if the global handle is invalid.
247sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) {
248 int sub_index = get_module_index(global_handle);
Nick Vaccaroa889c092016-04-15 10:17:07 -0700249 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700250 return NULL;
251 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700252 return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
253}
254
Peng Xu15b43e92017-04-27 15:45:44 -0700255// Returns the device pointer, or NULL if primary hal does not exist
256sensors_poll_device_1_t* sensors_poll_context_t::get_primary_v1_device() {
257 if (sub_hw_devices.size() < 1) {
258 return nullptr;
259 }
260 return (sensors_poll_device_1_t*) this->sub_hw_devices[0];
261}
262
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700263// Returns the device version, or -1 if the handle is invalid.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700264int sensors_poll_context_t::get_device_version_by_handle(int handle) {
265 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700266 if (v0) {
267 return v0->common.version;
268 } else {
269 return -1;
270 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700271}
272
Ashutosh Joshi9eb5bbd2017-01-09 17:09:35 -0800273// Android N and hire require sensor HALs to be at least 1_3 compliant
Nick Vaccaroe914d692014-10-10 13:32:38 -0700274#define HAL_VERSION_IS_COMPLIANT(version) \
Ashutosh Joshi9eb5bbd2017-01-09 17:09:35 -0800275 (version >= SENSORS_DEVICE_API_VERSION_1_3)
Nick Vaccaroe914d692014-10-10 13:32:38 -0700276
277// Returns true if HAL is compliant, false if HAL is not compliant or if handle is invalid
278static bool halIsCompliant(sensors_poll_context_t *ctx, int handle) {
279 int version = ctx->get_device_version_by_handle(handle);
280 return version != -1 && HAL_VERSION_IS_COMPLIANT(version);
281}
282
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700283static bool halIsAPILevelCompliant(sensors_poll_context_t *ctx, int handle, int level) {
284 int version = ctx->get_device_version_by_handle(handle);
285 return version != -1 && (version >= level);
286}
287
Peng Xu15b43e92017-04-27 15:45:44 -0700288static bool halSupportDirectSensorReport(sensors_poll_device_1_t* v1) {
289 return v1 != nullptr && HAL_VERSION_IS_COMPLIANT(v1->common.version) &&
290 v1->register_direct_channel != nullptr && v1->config_direct_report != nullptr;
291}
292
Nick Vaccaroe914d692014-10-10 13:32:38 -0700293const char *apiNumToStr(int version) {
294 switch(version) {
295 case SENSORS_DEVICE_API_VERSION_1_0:
296 return "SENSORS_DEVICE_API_VERSION_1_0";
297 case SENSORS_DEVICE_API_VERSION_1_1:
298 return "SENSORS_DEVICE_API_VERSION_1_1";
299 case SENSORS_DEVICE_API_VERSION_1_2:
300 return "SENSORS_DEVICE_API_VERSION_1_2";
301 case SENSORS_DEVICE_API_VERSION_1_3:
302 return "SENSORS_DEVICE_API_VERSION_1_3";
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700303 case SENSORS_DEVICE_API_VERSION_1_4:
304 return "SENSORS_DEVICE_API_VERSION_1_4";
Nick Vaccaroe914d692014-10-10 13:32:38 -0700305 default:
306 return "UNKNOWN";
307 }
308}
309
Mike Lockwoodf8477622013-10-17 08:05:00 -0700310int sensors_poll_context_t::activate(int handle, int enabled) {
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700311 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700312 ALOGV("activate");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700313 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700314 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700315 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700316 retval = v0->activate(v0, local_handle, enabled);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700317 } else {
318 ALOGE("IGNORING activate(enable %d) call to non-API-compliant sensor handle=%d !",
319 enabled, handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700320 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700321 ALOGV("retval %d", retval);
322 return retval;
323}
324
325int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700326 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700327 ALOGV("setDelay");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700328 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700329 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700330 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700331 retval = v0->setDelay(v0, local_handle, ns);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700332 } else {
333 ALOGE("IGNORING setDelay() call for non-API-compliant sensor handle=%d !", handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700334 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700335 ALOGV("retval %d", retval);
336 return retval;
337}
338
339void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src,
340 int sub_index) {
341 memcpy(dest, src, sizeof(struct sensors_event_t));
342 // A normal event's "sensor" field is a local handle. Convert it to a global handle.
343 // A meta-data event must have its sensor set to 0, but it has a nested event
344 // with a local handle that needs to be converted to a global handle.
345 FullHandle full_handle;
346 full_handle.moduleIndex = sub_index;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700347
Mike Lockwoodf8477622013-10-17 08:05:00 -0700348 // If it's a metadata event, rewrite the inner payload, not the sensor field.
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700349 // If the event's sensor field is unregistered for any reason, rewrite the sensor field
350 // with a -1, instead of writing an incorrect but plausible sensor number, because
351 // get_global_handle() returns -1 for unknown FullHandles.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700352 if (dest->type == SENSOR_TYPE_META_DATA) {
353 full_handle.localHandle = dest->meta_data.sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700354 dest->meta_data.sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700355 } else {
356 full_handle.localHandle = dest->sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700357 dest->sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700358 }
359}
360
361int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) {
362 ALOGV("poll");
363 int empties = 0;
Nick Vaccaroc384b182014-06-10 18:33:07 -0700364 int queueCount = 0;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700365 int eventsRead = 0;
366
367 pthread_mutex_lock(&queue_mutex);
Nick Vaccaroc384b182014-06-10 18:33:07 -0700368 queueCount = (int)this->queues.size();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700369 while (eventsRead == 0) {
370 while (empties < queueCount && eventsRead < maxReads) {
371 SensorEventQueue* queue = this->queues.at(this->nextReadIndex);
372 sensors_event_t* event = queue->peek();
373 if (event == NULL) {
374 empties++;
375 } else {
376 empties = 0;
Nick Vaccaroc384b182014-06-10 18:33:07 -0700377 this->copy_event_remap_handle(&data[eventsRead], event, nextReadIndex);
Peng Xu15b43e92017-04-27 15:45:44 -0700378 if (data[eventsRead].sensor == SENSORS_HANDLE_BASE - 1) {
Nick Vaccaroc384b182014-06-10 18:33:07 -0700379 // Bad handle, do not pass corrupted event upstream !
380 ALOGW("Dropping bad local handle event packet on the floor");
381 } else {
382 eventsRead++;
383 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700384 queue->dequeue();
385 }
386 this->nextReadIndex = (this->nextReadIndex + 1) % queueCount;
387 }
388 if (eventsRead == 0) {
389 // The queues have been scanned and none contain data, so wait.
390 ALOGV("poll stopping to wait for data");
391 waiting_for_data = true;
392 pthread_cond_wait(&data_available_cond, &queue_mutex);
393 waiting_for_data = false;
394 empties = 0;
395 }
396 }
397 pthread_mutex_unlock(&queue_mutex);
398 ALOGV("poll returning %d events.", eventsRead);
399
400 return eventsRead;
401}
402
403int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) {
404 ALOGV("batch");
405 int retval = -EINVAL;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700406 int local_handle = get_local_handle(handle);
407 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700408 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700409 retval = v1->batch(v1, local_handle, flags, period_ns, timeout);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700410 } else {
411 ALOGE("IGNORING batch() call to non-API-compliant sensor handle=%d !", handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700412 }
413 ALOGV("retval %d", retval);
414 return retval;
415}
416
417int sensors_poll_context_t::flush(int handle) {
418 ALOGV("flush");
419 int retval = -EINVAL;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700420 int local_handle = get_local_handle(handle);
421 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700422 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700423 retval = v1->flush(v1, local_handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700424 } else {
425 ALOGE("IGNORING flush() call to non-API-compliant sensor handle=%d !", handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700426 }
427 ALOGV("retval %d", retval);
428 return retval;
429}
430
Peng Xu15b43e92017-04-27 15:45:44 -0700431int sensors_poll_context_t::inject_sensor_data(const sensors_event_t *data) {
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700432 int retval = -EINVAL;
433 ALOGV("inject_sensor_data");
Peng Xu15b43e92017-04-27 15:45:44 -0700434 if (data->sensor == -1) {
435 // operational parameter
436 sensors_poll_device_1_t* v1 = get_primary_v1_device();
437 if (v1 && v1->common.version >= SENSORS_DEVICE_API_VERSION_1_4) {
438 retval = v1->inject_sensor_data(v1, data);
439 } else {
440 ALOGE("IGNORED inject_sensor_data(operational param) call to non-API-compliant sensor");
441 return -ENOSYS;
442 }
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700443 } else {
Peng Xu15b43e92017-04-27 15:45:44 -0700444 // Get handle for the sensor owning the event being injected
445 int local_handle = get_local_handle(data->sensor);
446 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(data->sensor);
447 if (halIsAPILevelCompliant(this, data->sensor, SENSORS_DEVICE_API_VERSION_1_4) &&
448 local_handle >= 0 && v1) {
Alexey Polyudov516ef862017-05-23 18:58:25 -0700449 // if specific sensor is used, we have to replace global sensor handle
450 // with local one, before passing to concrete HAL
451 sensors_event_t data_copy = *data;
452 data_copy.sensor = local_handle;
453 retval = v1->inject_sensor_data(v1, &data_copy);
Peng Xu15b43e92017-04-27 15:45:44 -0700454 } else {
455 ALOGE("IGNORED inject_sensor_data(type=%d, handle=%d) call to non-API-compliant sensor",
456 data->type, data->sensor);
457 retval = -ENOSYS;
458 }
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700459 }
460 ALOGV("retval %d", retval);
461 return retval;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700462}
463
Peng Xu15b43e92017-04-27 15:45:44 -0700464int sensors_poll_context_t::register_direct_channel(const struct sensors_direct_mem_t* mem,
465 int channel_handle) {
466 int retval = -EINVAL;
467 ALOGV("register_direct_channel");
468 sensors_poll_device_1_t* v1 = get_primary_v1_device();
469 if (v1 && halSupportDirectSensorReport(v1)) {
470 retval = v1->register_direct_channel(v1, mem, channel_handle);
471 } else {
472 ALOGE("IGNORED register_direct_channel(mem=%p, handle=%d) call to non-API-compliant sensor",
473 mem, channel_handle);
474 retval = -ENOSYS;
475 }
476 ALOGV("retval %d", retval);
477 return retval;
478}
479
480int sensors_poll_context_t::config_direct_report(int sensor_handle,
481 int channel_handle,
482 const struct sensors_direct_cfg_t *config) {
483 int retval = -EINVAL;
484 ALOGV("config_direct_report");
485
486 if (config != nullptr) {
487 int local_handle = get_local_handle(sensor_handle);
488 sensors_poll_device_1_t* v1 = get_primary_v1_device();
489 if (v1 && halSupportDirectSensorReport(v1)) {
490 retval = v1->config_direct_report(v1, local_handle, channel_handle, config);
491 } else {
492 ALOGE("IGNORED config_direct_report(sensor=%d, channel=%d, rate_level=%d) call to "
493 "non-API-compliant sensor", sensor_handle, channel_handle, config->rate_level);
494 retval = -ENOSYS;
495 }
496 }
497 ALOGV("retval %d", retval);
498 return retval;
499}
Mike Lockwoodf8477622013-10-17 08:05:00 -0700500int sensors_poll_context_t::close() {
501 ALOGV("close");
502 for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
503 it != this->sub_hw_devices.end(); it++) {
504 hw_device_t* dev = *it;
505 int retval = dev->close(dev);
506 ALOGV("retval %d", retval);
507 }
508 return 0;
509}
510
511
512static int device__close(struct hw_device_t *dev) {
Peng Xud8871a72017-08-09 23:02:23 -0700513 pthread_mutex_lock(&init_modules_mutex);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700514 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
515 if (ctx != NULL) {
516 int retval = ctx->close();
517 delete ctx;
518 }
Peng Xud8871a72017-08-09 23:02:23 -0700519
520 if (sub_hw_modules != nullptr) {
521 delete sub_hw_modules;
522 sub_hw_modules = nullptr;
523 }
524
525 if (so_handles != nullptr) {
526 for (auto handle : *so_handles) {
527 dlclose(handle);
528 }
529 delete so_handles;
530 so_handles = nullptr;
531 }
532 pthread_mutex_unlock(&init_modules_mutex);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700533 return 0;
534}
535
536static int device__activate(struct sensors_poll_device_t *dev, int handle,
537 int enabled) {
538 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
539 return ctx->activate(handle, enabled);
540}
541
542static int device__setDelay(struct sensors_poll_device_t *dev, int handle,
543 int64_t ns) {
544 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
545 return ctx->setDelay(handle, ns);
546}
547
548static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data,
549 int count) {
550 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
551 return ctx->poll(data, count);
552}
553
554static int device__batch(struct sensors_poll_device_1 *dev, int handle,
555 int flags, int64_t period_ns, int64_t timeout) {
556 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
557 return ctx->batch(handle, flags, period_ns, timeout);
558}
559
560static int device__flush(struct sensors_poll_device_1 *dev, int handle) {
561 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
562 return ctx->flush(handle);
563}
564
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700565static int device__inject_sensor_data(struct sensors_poll_device_1 *dev,
566 const sensors_event_t *data) {
567 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
Peng Xu15b43e92017-04-27 15:45:44 -0700568 return ctx->inject_sensor_data(data);
569}
570
571static int device__register_direct_channel(struct sensors_poll_device_1 *dev,
572 const struct sensors_direct_mem_t* mem,
573 int channel_handle) {
574 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
575 return ctx->register_direct_channel(mem, channel_handle);
576}
577
578static int device__config_direct_report(struct sensors_poll_device_1 *dev,
579 int sensor_handle,
580 int channel_handle,
581 const struct sensors_direct_cfg_t *config) {
582 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
583 return ctx->config_direct_report(sensor_handle, channel_handle, config);
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700584}
585
Mike Lockwoodf8477622013-10-17 08:05:00 -0700586static int open_sensors(const struct hw_module_t* module, const char* name,
587 struct hw_device_t** device);
588
589static bool starts_with(const char* s, const char* prefix) {
590 if (s == NULL || prefix == NULL) {
591 return false;
592 }
593 size_t s_size = strlen(s);
594 size_t prefix_size = strlen(prefix);
595 return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0;
596}
597
598/*
599 * Adds valid paths from the config file to the vector passed in.
600 * The vector must not be null.
601 */
Peng Xud8871a72017-08-09 23:02:23 -0700602static std::vector<std::string> get_so_paths() {
603 std::vector<std::string> so_paths;
604
Peng Xu900cfac2017-04-22 14:29:06 -0700605 const std::vector<const char *> config_path_list(
606 { MULTI_HAL_CONFIG_FILE_PATH, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH });
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800607
Peng Xu900cfac2017-04-22 14:29:06 -0700608 std::ifstream stream;
609 const char *path = nullptr;
610 for (auto i : config_path_list) {
611 std::ifstream f(i);
612 if (f) {
613 stream = std::move(f);
614 path = i;
615 break;
616 }
617 }
618 if(!stream) {
619 ALOGW("No multihal config file found");
Peng Xud8871a72017-08-09 23:02:23 -0700620 return so_paths;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700621 }
Peng Xu900cfac2017-04-22 14:29:06 -0700622
623 ALOGE_IF(strcmp(path, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH) == 0,
624 "Multihal configuration file path %s is not compatible with Treble "
625 "requirements. Please move it to %s.",
626 path, MULTI_HAL_CONFIG_FILE_PATH);
627
628 ALOGV("Multihal config file found at %s", path);
629 std::string line;
630 while (std::getline(stream, line)) {
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800631 ALOGV("config file line: '%s'", line.c_str());
Peng Xud8871a72017-08-09 23:02:23 -0700632 so_paths.push_back(line);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700633 }
Peng Xud8871a72017-08-09 23:02:23 -0700634 return so_paths;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700635}
636
637/*
638 * Ensures that the sub-module array is initialized.
639 * This can be first called from get_sensors_list or from open_sensors.
640 */
641static void lazy_init_modules() {
642 pthread_mutex_lock(&init_modules_mutex);
643 if (sub_hw_modules != NULL) {
644 pthread_mutex_unlock(&init_modules_mutex);
645 return;
646 }
Peng Xud8871a72017-08-09 23:02:23 -0700647 std::vector<std::string> so_paths(get_so_paths());
Mike Lockwoodf8477622013-10-17 08:05:00 -0700648
649 // dlopen the module files and cache their module symbols in sub_hw_modules
650 sub_hw_modules = new std::vector<hw_module_t *>();
Peng Xud8871a72017-08-09 23:02:23 -0700651 so_handles = new std::vector<void *>();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700652 dlerror(); // clear any old errors
653 const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
Peng Xud8871a72017-08-09 23:02:23 -0700654 for (const auto &s : so_paths) {
655 const char* path = s.c_str();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700656 void* lib_handle = dlopen(path, RTLD_LAZY);
657 if (lib_handle == NULL) {
658 ALOGW("dlerror(): %s", dlerror());
659 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700660 ALOGI("Loaded library from %s", path);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700661 ALOGV("Opening symbol \"%s\"", sym);
662 // clear old errors
663 dlerror();
664 struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym);
665 const char* error;
666 if ((error = dlerror()) != NULL) {
667 ALOGW("Error calling dlsym: %s", error);
668 } else if (module == NULL) {
669 ALOGW("module == NULL");
670 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700671 ALOGV("Loaded symbols from \"%s\"", sym);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700672 sub_hw_modules->push_back(module);
Peng Xud8871a72017-08-09 23:02:23 -0700673 so_handles->push_back(lib_handle);
674 lib_handle = nullptr;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700675 }
676 }
Peng Xud8871a72017-08-09 23:02:23 -0700677 if (lib_handle != nullptr) {
678 dlclose(lib_handle);
679 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700680 }
681 pthread_mutex_unlock(&init_modules_mutex);
682}
683
684/*
685 * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles.
686 */
687static void lazy_init_sensors_list() {
688 ALOGV("lazy_init_sensors_list");
689 pthread_mutex_lock(&init_sensors_mutex);
690 if (global_sensors_list != NULL) {
691 // already initialized
692 pthread_mutex_unlock(&init_sensors_mutex);
693 ALOGV("lazy_init_sensors_list - early return");
694 return;
695 }
696
697 ALOGV("lazy_init_sensors_list needs to do work");
698 lazy_init_modules();
699
700 // Count all the sensors, then allocate an array of blanks.
701 global_sensors_count = 0;
702 const struct sensor_t *subhal_sensors_list;
703 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
704 it != sub_hw_modules->end(); it++) {
705 struct sensors_module_t *module = (struct sensors_module_t*) *it;
706 global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list);
707 ALOGV("increased global_sensors_count to %d", global_sensors_count);
708 }
709
710 // The global_sensors_list is full of consts.
711 // Manipulate this non-const list, and point the const one to it when we're done.
712 sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count];
713
714 // index of the next sensor to set in mutable_sensor_list
715 int mutable_sensor_index = 0;
716 int module_index = 0;
717
718 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
719 it != sub_hw_modules->end(); it++) {
720 hw_module_t *hw_module = *it;
721 ALOGV("examine one module");
722 // Read the sub-module's sensor list.
723 struct sensors_module_t *module = (struct sensors_module_t*) hw_module;
724 int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list);
725 ALOGV("the module has %d sensors", module_sensor_count);
726
727 // Copy the HAL's sensor list into global_sensors_list,
728 // with the handle changed to be a global handle.
729 for (int i = 0; i < module_sensor_count; i++) {
730 ALOGV("examining one sensor");
731 const struct sensor_t *local_sensor = &subhal_sensors_list[i];
732 int local_handle = local_sensor->handle;
733 memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
734 sizeof(struct sensor_t));
735
Peng Xu15b43e92017-04-27 15:45:44 -0700736 // sensor direct report is only for primary module
737 if (module_index != 0) {
738 mutable_sensor_list[mutable_sensor_index].flags &=
739 ~(SENSOR_FLAG_MASK_DIRECT_REPORT | SENSOR_FLAG_MASK_DIRECT_CHANNEL);
740 }
741
Mike Lockwoodf8477622013-10-17 08:05:00 -0700742 // Overwrite the global version's handle with a global handle.
743 int global_handle = assign_global_handle(module_index, local_handle);
744
745 mutable_sensor_list[mutable_sensor_index].handle = global_handle;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700746 ALOGV("module_index %d, local_handle %d, global_handle %d",
Mike Lockwoodf8477622013-10-17 08:05:00 -0700747 module_index, local_handle, global_handle);
748
749 mutable_sensor_index++;
750 }
751 module_index++;
752 }
753 // Set the const static global_sensors_list to the mutable one allocated by this function.
754 global_sensors_list = mutable_sensor_list;
755
756 pthread_mutex_unlock(&init_sensors_mutex);
757 ALOGV("end lazy_init_sensors_list");
758}
759
Nick Vaccaroe914d692014-10-10 13:32:38 -0700760static int module__get_sensors_list(__unused struct sensors_module_t* module,
Mike Lockwoodf8477622013-10-17 08:05:00 -0700761 struct sensor_t const** list) {
762 ALOGV("module__get_sensors_list start");
763 lazy_init_sensors_list();
764 *list = global_sensors_list;
765 ALOGV("global_sensors_count: %d", global_sensors_count);
766 for (int i = 0; i < global_sensors_count; i++) {
767 ALOGV("sensor type: %d", global_sensors_list[i].type);
768 }
769 return global_sensors_count;
770}
771
772static struct hw_module_methods_t sensors_module_methods = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700773 .open = open_sensors
Mike Lockwoodf8477622013-10-17 08:05:00 -0700774};
775
776struct sensors_module_t HAL_MODULE_INFO_SYM = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700777 .common = {
778 .tag = HARDWARE_MODULE_TAG,
779 .version_major = 1,
780 .version_minor = 1,
781 .id = SENSORS_HARDWARE_MODULE_ID,
782 .name = "MultiHal Sensor Module",
783 .author = "Google, Inc",
784 .methods = &sensors_module_methods,
785 .dso = NULL,
786 .reserved = {0},
Mike Lockwoodf8477622013-10-17 08:05:00 -0700787 },
Nick Vaccaroa889c092016-04-15 10:17:07 -0700788 .get_sensors_list = module__get_sensors_list
Mike Lockwoodf8477622013-10-17 08:05:00 -0700789};
790
Nick Vaccaro8a837082016-12-12 16:50:57 -0800791struct sensors_module_t *get_multi_hal_module_info() {
792 return (&HAL_MODULE_INFO_SYM);
793}
794
Mike Lockwoodf8477622013-10-17 08:05:00 -0700795static int open_sensors(const struct hw_module_t* hw_module, const char* name,
796 struct hw_device_t** hw_device_out) {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700797 ALOGV("open_sensors begin...");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700798
799 lazy_init_modules();
800
801 // Create proxy device, to return later.
802 sensors_poll_context_t *dev = new sensors_poll_context_t();
803 memset(dev, 0, sizeof(sensors_poll_device_1_t));
804 dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700805 dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_4;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700806 dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
807 dev->proxy_device.common.close = device__close;
808 dev->proxy_device.activate = device__activate;
809 dev->proxy_device.setDelay = device__setDelay;
810 dev->proxy_device.poll = device__poll;
811 dev->proxy_device.batch = device__batch;
812 dev->proxy_device.flush = device__flush;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700813 dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
Peng Xu15b43e92017-04-27 15:45:44 -0700814 dev->proxy_device.register_direct_channel = device__register_direct_channel;
815 dev->proxy_device.config_direct_report = device__config_direct_report;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700816
817 dev->nextReadIndex = 0;
818
819 // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
820 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
821 it != sub_hw_modules->end(); it++) {
822 sensors_module_t *sensors_module = (sensors_module_t*) *it;
823 struct hw_device_t* sub_hw_device;
824 int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700825 if (!sub_open_result) {
826 if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) {
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700827 ALOGE("SENSORS_DEVICE_API_VERSION_1_3 or newer is required for all sensor HALs");
Nick Vaccaroe914d692014-10-10 13:32:38 -0700828 ALOGE("This HAL reports non-compliant API level : %s",
829 apiNumToStr(sub_hw_device->version));
830 ALOGE("Sensors belonging to this HAL will get ignored !");
831 }
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700832 dev->addSubHwDevice(sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700833 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700834 }
835
836 // Prepare the output param and return
837 *hw_device_out = &dev->proxy_device.common;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700838 ALOGV("...open_sensors end");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700839 return 0;
840}