blob: 4d6013fac40feb5852e1754581a132492d94bf02 [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);
Peng Xu15b43e92017-04-27 15:45:44 -0700199 int inject_sensor_data(const sensors_event_t *data);
200 int register_direct_channel(const struct sensors_direct_mem_t* mem,
201 int channel_handle);
202 int config_direct_report(int sensor_handle,
203 int channel_handle,
204 const struct sensors_direct_cfg_t *config);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700205 int close();
206
207 std::vector<hw_device_t*> sub_hw_devices;
208 std::vector<SensorEventQueue*> queues;
209 std::vector<pthread_t> threads;
210 int nextReadIndex;
211
212 sensors_poll_device_t* get_v0_device_by_handle(int global_handle);
213 sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle);
Peng Xu15b43e92017-04-27 15:45:44 -0700214 sensors_poll_device_1_t* get_primary_v1_device();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700215 int get_device_version_by_handle(int global_handle);
216
217 void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index);
218};
219
220void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) {
221 ALOGV("addSubHwDevice");
222 this->sub_hw_devices.push_back(sub_hw_device);
223
224 SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY);
225 this->queues.push_back(queue);
226
227 TaskContext* taskContext = new TaskContext();
228 taskContext->device = (sensors_poll_device_t*) sub_hw_device;
229 taskContext->queue = queue;
230
231 pthread_t writerThread;
232 pthread_create(&writerThread, NULL, writerTask, taskContext);
233 this->threads.push_back(writerThread);
234}
235
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700236// Returns the device pointer, or NULL if the global handle is invalid.
237sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) {
238 int sub_index = get_module_index(global_handle);
Nick Vaccaroa889c092016-04-15 10:17:07 -0700239 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700240 return NULL;
241 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700242 return (sensors_poll_device_t*) this->sub_hw_devices[sub_index];
243}
244
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700245// Returns the device pointer, or NULL if the global handle is invalid.
246sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) {
247 int sub_index = get_module_index(global_handle);
Nick Vaccaroa889c092016-04-15 10:17:07 -0700248 if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700249 return NULL;
250 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700251 return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
252}
253
Peng Xu15b43e92017-04-27 15:45:44 -0700254// Returns the device pointer, or NULL if primary hal does not exist
255sensors_poll_device_1_t* sensors_poll_context_t::get_primary_v1_device() {
256 if (sub_hw_devices.size() < 1) {
257 return nullptr;
258 }
259 return (sensors_poll_device_1_t*) this->sub_hw_devices[0];
260}
261
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700262// Returns the device version, or -1 if the handle is invalid.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700263int sensors_poll_context_t::get_device_version_by_handle(int handle) {
264 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700265 if (v0) {
266 return v0->common.version;
267 } else {
268 return -1;
269 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700270}
271
Ashutosh Joshi9eb5bbd2017-01-09 17:09:35 -0800272// Android N and hire require sensor HALs to be at least 1_3 compliant
Nick Vaccaroe914d692014-10-10 13:32:38 -0700273#define HAL_VERSION_IS_COMPLIANT(version) \
Ashutosh Joshi9eb5bbd2017-01-09 17:09:35 -0800274 (version >= SENSORS_DEVICE_API_VERSION_1_3)
Nick Vaccaroe914d692014-10-10 13:32:38 -0700275
276// Returns true if HAL is compliant, false if HAL is not compliant or if handle is invalid
277static bool halIsCompliant(sensors_poll_context_t *ctx, int handle) {
278 int version = ctx->get_device_version_by_handle(handle);
279 return version != -1 && HAL_VERSION_IS_COMPLIANT(version);
280}
281
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700282static bool halIsAPILevelCompliant(sensors_poll_context_t *ctx, int handle, int level) {
283 int version = ctx->get_device_version_by_handle(handle);
284 return version != -1 && (version >= level);
285}
286
Peng Xu15b43e92017-04-27 15:45:44 -0700287static bool halSupportDirectSensorReport(sensors_poll_device_1_t* v1) {
288 return v1 != nullptr && HAL_VERSION_IS_COMPLIANT(v1->common.version) &&
289 v1->register_direct_channel != nullptr && v1->config_direct_report != nullptr;
290}
291
Nick Vaccaroe914d692014-10-10 13:32:38 -0700292const char *apiNumToStr(int version) {
293 switch(version) {
294 case SENSORS_DEVICE_API_VERSION_1_0:
295 return "SENSORS_DEVICE_API_VERSION_1_0";
296 case SENSORS_DEVICE_API_VERSION_1_1:
297 return "SENSORS_DEVICE_API_VERSION_1_1";
298 case SENSORS_DEVICE_API_VERSION_1_2:
299 return "SENSORS_DEVICE_API_VERSION_1_2";
300 case SENSORS_DEVICE_API_VERSION_1_3:
301 return "SENSORS_DEVICE_API_VERSION_1_3";
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700302 case SENSORS_DEVICE_API_VERSION_1_4:
303 return "SENSORS_DEVICE_API_VERSION_1_4";
Nick Vaccaroe914d692014-10-10 13:32:38 -0700304 default:
305 return "UNKNOWN";
306 }
307}
308
Mike Lockwoodf8477622013-10-17 08:05:00 -0700309int sensors_poll_context_t::activate(int handle, int enabled) {
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700310 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700311 ALOGV("activate");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700312 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700313 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700314 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700315 retval = v0->activate(v0, local_handle, enabled);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700316 } else {
317 ALOGE("IGNORING activate(enable %d) call to non-API-compliant sensor handle=%d !",
318 enabled, handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700319 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700320 ALOGV("retval %d", retval);
321 return retval;
322}
323
324int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700325 int retval = -EINVAL;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700326 ALOGV("setDelay");
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700327 int local_handle = get_local_handle(handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700328 sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700329 if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700330 retval = v0->setDelay(v0, local_handle, ns);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700331 } else {
332 ALOGE("IGNORING setDelay() call for non-API-compliant sensor handle=%d !", handle);
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700333 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700334 ALOGV("retval %d", retval);
335 return retval;
336}
337
338void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src,
339 int sub_index) {
340 memcpy(dest, src, sizeof(struct sensors_event_t));
341 // A normal event's "sensor" field is a local handle. Convert it to a global handle.
342 // A meta-data event must have its sensor set to 0, but it has a nested event
343 // with a local handle that needs to be converted to a global handle.
344 FullHandle full_handle;
345 full_handle.moduleIndex = sub_index;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700346
Mike Lockwoodf8477622013-10-17 08:05:00 -0700347 // If it's a metadata event, rewrite the inner payload, not the sensor field.
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700348 // If the event's sensor field is unregistered for any reason, rewrite the sensor field
349 // with a -1, instead of writing an incorrect but plausible sensor number, because
350 // get_global_handle() returns -1 for unknown FullHandles.
Mike Lockwoodf8477622013-10-17 08:05:00 -0700351 if (dest->type == SENSOR_TYPE_META_DATA) {
352 full_handle.localHandle = dest->meta_data.sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700353 dest->meta_data.sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700354 } else {
355 full_handle.localHandle = dest->sensor;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700356 dest->sensor = get_global_handle(&full_handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700357 }
358}
359
360int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) {
361 ALOGV("poll");
362 int empties = 0;
Nick Vaccaroc384b182014-06-10 18:33:07 -0700363 int queueCount = 0;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700364 int eventsRead = 0;
365
366 pthread_mutex_lock(&queue_mutex);
Nick Vaccaroc384b182014-06-10 18:33:07 -0700367 queueCount = (int)this->queues.size();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700368 while (eventsRead == 0) {
369 while (empties < queueCount && eventsRead < maxReads) {
370 SensorEventQueue* queue = this->queues.at(this->nextReadIndex);
371 sensors_event_t* event = queue->peek();
372 if (event == NULL) {
373 empties++;
374 } else {
375 empties = 0;
Nick Vaccaroc384b182014-06-10 18:33:07 -0700376 this->copy_event_remap_handle(&data[eventsRead], event, nextReadIndex);
Peng Xu15b43e92017-04-27 15:45:44 -0700377 if (data[eventsRead].sensor == SENSORS_HANDLE_BASE - 1) {
Nick Vaccaroc384b182014-06-10 18:33:07 -0700378 // Bad handle, do not pass corrupted event upstream !
379 ALOGW("Dropping bad local handle event packet on the floor");
380 } else {
381 eventsRead++;
382 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700383 queue->dequeue();
384 }
385 this->nextReadIndex = (this->nextReadIndex + 1) % queueCount;
386 }
387 if (eventsRead == 0) {
388 // The queues have been scanned and none contain data, so wait.
389 ALOGV("poll stopping to wait for data");
390 waiting_for_data = true;
391 pthread_cond_wait(&data_available_cond, &queue_mutex);
392 waiting_for_data = false;
393 empties = 0;
394 }
395 }
396 pthread_mutex_unlock(&queue_mutex);
397 ALOGV("poll returning %d events.", eventsRead);
398
399 return eventsRead;
400}
401
402int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) {
403 ALOGV("batch");
404 int retval = -EINVAL;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700405 int local_handle = get_local_handle(handle);
406 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700407 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700408 retval = v1->batch(v1, local_handle, flags, period_ns, timeout);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700409 } else {
410 ALOGE("IGNORING batch() call to non-API-compliant sensor handle=%d !", handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700411 }
412 ALOGV("retval %d", retval);
413 return retval;
414}
415
416int sensors_poll_context_t::flush(int handle) {
417 ALOGV("flush");
418 int retval = -EINVAL;
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700419 int local_handle = get_local_handle(handle);
420 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700421 if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
Aaron Whyte4d7ac522014-04-09 15:56:54 -0700422 retval = v1->flush(v1, local_handle);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700423 } else {
424 ALOGE("IGNORING flush() call to non-API-compliant sensor handle=%d !", handle);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700425 }
426 ALOGV("retval %d", retval);
427 return retval;
428}
429
Peng Xu15b43e92017-04-27 15:45:44 -0700430int sensors_poll_context_t::inject_sensor_data(const sensors_event_t *data) {
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700431 int retval = -EINVAL;
432 ALOGV("inject_sensor_data");
Peng Xu15b43e92017-04-27 15:45:44 -0700433 if (data->sensor == -1) {
434 // operational parameter
435 sensors_poll_device_1_t* v1 = get_primary_v1_device();
436 if (v1 && v1->common.version >= SENSORS_DEVICE_API_VERSION_1_4) {
437 retval = v1->inject_sensor_data(v1, data);
438 } else {
439 ALOGE("IGNORED inject_sensor_data(operational param) call to non-API-compliant sensor");
440 return -ENOSYS;
441 }
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700442 } else {
Peng Xu15b43e92017-04-27 15:45:44 -0700443 // Get handle for the sensor owning the event being injected
444 int local_handle = get_local_handle(data->sensor);
445 sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(data->sensor);
446 if (halIsAPILevelCompliant(this, data->sensor, SENSORS_DEVICE_API_VERSION_1_4) &&
447 local_handle >= 0 && v1) {
448 retval = v1->inject_sensor_data(v1, data);
449 } else {
450 ALOGE("IGNORED inject_sensor_data(type=%d, handle=%d) call to non-API-compliant sensor",
451 data->type, data->sensor);
452 retval = -ENOSYS;
453 }
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700454 }
455 ALOGV("retval %d", retval);
456 return retval;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700457}
458
Peng Xu15b43e92017-04-27 15:45:44 -0700459int sensors_poll_context_t::register_direct_channel(const struct sensors_direct_mem_t* mem,
460 int channel_handle) {
461 int retval = -EINVAL;
462 ALOGV("register_direct_channel");
463 sensors_poll_device_1_t* v1 = get_primary_v1_device();
464 if (v1 && halSupportDirectSensorReport(v1)) {
465 retval = v1->register_direct_channel(v1, mem, channel_handle);
466 } else {
467 ALOGE("IGNORED register_direct_channel(mem=%p, handle=%d) call to non-API-compliant sensor",
468 mem, channel_handle);
469 retval = -ENOSYS;
470 }
471 ALOGV("retval %d", retval);
472 return retval;
473}
474
475int sensors_poll_context_t::config_direct_report(int sensor_handle,
476 int channel_handle,
477 const struct sensors_direct_cfg_t *config) {
478 int retval = -EINVAL;
479 ALOGV("config_direct_report");
480
481 if (config != nullptr) {
482 int local_handle = get_local_handle(sensor_handle);
483 sensors_poll_device_1_t* v1 = get_primary_v1_device();
484 if (v1 && halSupportDirectSensorReport(v1)) {
485 retval = v1->config_direct_report(v1, local_handle, channel_handle, config);
486 } else {
487 ALOGE("IGNORED config_direct_report(sensor=%d, channel=%d, rate_level=%d) call to "
488 "non-API-compliant sensor", sensor_handle, channel_handle, config->rate_level);
489 retval = -ENOSYS;
490 }
491 }
492 ALOGV("retval %d", retval);
493 return retval;
494}
Mike Lockwoodf8477622013-10-17 08:05:00 -0700495int sensors_poll_context_t::close() {
496 ALOGV("close");
497 for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
498 it != this->sub_hw_devices.end(); it++) {
499 hw_device_t* dev = *it;
500 int retval = dev->close(dev);
501 ALOGV("retval %d", retval);
502 }
503 return 0;
504}
505
506
507static int device__close(struct hw_device_t *dev) {
508 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
509 if (ctx != NULL) {
510 int retval = ctx->close();
511 delete ctx;
512 }
513 return 0;
514}
515
516static int device__activate(struct sensors_poll_device_t *dev, int handle,
517 int enabled) {
518 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
519 return ctx->activate(handle, enabled);
520}
521
522static int device__setDelay(struct sensors_poll_device_t *dev, int handle,
523 int64_t ns) {
524 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
525 return ctx->setDelay(handle, ns);
526}
527
528static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data,
529 int count) {
530 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
531 return ctx->poll(data, count);
532}
533
534static int device__batch(struct sensors_poll_device_1 *dev, int handle,
535 int flags, int64_t period_ns, int64_t timeout) {
536 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
537 return ctx->batch(handle, flags, period_ns, timeout);
538}
539
540static int device__flush(struct sensors_poll_device_1 *dev, int handle) {
541 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
542 return ctx->flush(handle);
543}
544
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700545static int device__inject_sensor_data(struct sensors_poll_device_1 *dev,
546 const sensors_event_t *data) {
547 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
Peng Xu15b43e92017-04-27 15:45:44 -0700548 return ctx->inject_sensor_data(data);
549}
550
551static int device__register_direct_channel(struct sensors_poll_device_1 *dev,
552 const struct sensors_direct_mem_t* mem,
553 int channel_handle) {
554 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
555 return ctx->register_direct_channel(mem, channel_handle);
556}
557
558static int device__config_direct_report(struct sensors_poll_device_1 *dev,
559 int sensor_handle,
560 int channel_handle,
561 const struct sensors_direct_cfg_t *config) {
562 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
563 return ctx->config_direct_report(sensor_handle, channel_handle, config);
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700564}
565
Mike Lockwoodf8477622013-10-17 08:05:00 -0700566static int open_sensors(const struct hw_module_t* module, const char* name,
567 struct hw_device_t** device);
568
569static bool starts_with(const char* s, const char* prefix) {
570 if (s == NULL || prefix == NULL) {
571 return false;
572 }
573 size_t s_size = strlen(s);
574 size_t prefix_size = strlen(prefix);
575 return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0;
576}
577
578/*
579 * Adds valid paths from the config file to the vector passed in.
580 * The vector must not be null.
581 */
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800582static void get_so_paths(std::vector<std::string> *so_paths) {
Peng Xu900cfac2017-04-22 14:29:06 -0700583 const std::vector<const char *> config_path_list(
584 { MULTI_HAL_CONFIG_FILE_PATH, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH });
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800585
Peng Xu900cfac2017-04-22 14:29:06 -0700586 std::ifstream stream;
587 const char *path = nullptr;
588 for (auto i : config_path_list) {
589 std::ifstream f(i);
590 if (f) {
591 stream = std::move(f);
592 path = i;
593 break;
594 }
595 }
596 if(!stream) {
597 ALOGW("No multihal config file found");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700598 return;
599 }
Peng Xu900cfac2017-04-22 14:29:06 -0700600
601 ALOGE_IF(strcmp(path, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH) == 0,
602 "Multihal configuration file path %s is not compatible with Treble "
603 "requirements. Please move it to %s.",
604 path, MULTI_HAL_CONFIG_FILE_PATH);
605
606 ALOGV("Multihal config file found at %s", path);
607 std::string line;
608 while (std::getline(stream, line)) {
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800609 ALOGV("config file line: '%s'", line.c_str());
610 so_paths->push_back(line);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700611 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700612}
613
614/*
615 * Ensures that the sub-module array is initialized.
616 * This can be first called from get_sensors_list or from open_sensors.
617 */
618static void lazy_init_modules() {
619 pthread_mutex_lock(&init_modules_mutex);
620 if (sub_hw_modules != NULL) {
621 pthread_mutex_unlock(&init_modules_mutex);
622 return;
623 }
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800624 std::vector<std::string> *so_paths = new std::vector<std::string>();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700625 get_so_paths(so_paths);
626
627 // dlopen the module files and cache their module symbols in sub_hw_modules
628 sub_hw_modules = new std::vector<hw_module_t *>();
629 dlerror(); // clear any old errors
630 const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800631 for (std::vector<std::string>::iterator it = so_paths->begin(); it != so_paths->end(); it++) {
632 const char* path = it->c_str();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700633 void* lib_handle = dlopen(path, RTLD_LAZY);
634 if (lib_handle == NULL) {
635 ALOGW("dlerror(): %s", dlerror());
636 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700637 ALOGI("Loaded library from %s", path);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700638 ALOGV("Opening symbol \"%s\"", sym);
639 // clear old errors
640 dlerror();
641 struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym);
642 const char* error;
643 if ((error = dlerror()) != NULL) {
644 ALOGW("Error calling dlsym: %s", error);
645 } else if (module == NULL) {
646 ALOGW("module == NULL");
647 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700648 ALOGV("Loaded symbols from \"%s\"", sym);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700649 sub_hw_modules->push_back(module);
650 }
651 }
652 }
653 pthread_mutex_unlock(&init_modules_mutex);
654}
655
656/*
657 * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles.
658 */
659static void lazy_init_sensors_list() {
660 ALOGV("lazy_init_sensors_list");
661 pthread_mutex_lock(&init_sensors_mutex);
662 if (global_sensors_list != NULL) {
663 // already initialized
664 pthread_mutex_unlock(&init_sensors_mutex);
665 ALOGV("lazy_init_sensors_list - early return");
666 return;
667 }
668
669 ALOGV("lazy_init_sensors_list needs to do work");
670 lazy_init_modules();
671
672 // Count all the sensors, then allocate an array of blanks.
673 global_sensors_count = 0;
674 const struct sensor_t *subhal_sensors_list;
675 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
676 it != sub_hw_modules->end(); it++) {
677 struct sensors_module_t *module = (struct sensors_module_t*) *it;
678 global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list);
679 ALOGV("increased global_sensors_count to %d", global_sensors_count);
680 }
681
682 // The global_sensors_list is full of consts.
683 // Manipulate this non-const list, and point the const one to it when we're done.
684 sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count];
685
686 // index of the next sensor to set in mutable_sensor_list
687 int mutable_sensor_index = 0;
688 int module_index = 0;
689
690 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
691 it != sub_hw_modules->end(); it++) {
692 hw_module_t *hw_module = *it;
693 ALOGV("examine one module");
694 // Read the sub-module's sensor list.
695 struct sensors_module_t *module = (struct sensors_module_t*) hw_module;
696 int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list);
697 ALOGV("the module has %d sensors", module_sensor_count);
698
699 // Copy the HAL's sensor list into global_sensors_list,
700 // with the handle changed to be a global handle.
701 for (int i = 0; i < module_sensor_count; i++) {
702 ALOGV("examining one sensor");
703 const struct sensor_t *local_sensor = &subhal_sensors_list[i];
704 int local_handle = local_sensor->handle;
705 memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
706 sizeof(struct sensor_t));
707
Peng Xu15b43e92017-04-27 15:45:44 -0700708 // sensor direct report is only for primary module
709 if (module_index != 0) {
710 mutable_sensor_list[mutable_sensor_index].flags &=
711 ~(SENSOR_FLAG_MASK_DIRECT_REPORT | SENSOR_FLAG_MASK_DIRECT_CHANNEL);
712 }
713
Mike Lockwoodf8477622013-10-17 08:05:00 -0700714 // Overwrite the global version's handle with a global handle.
715 int global_handle = assign_global_handle(module_index, local_handle);
716
717 mutable_sensor_list[mutable_sensor_index].handle = global_handle;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700718 ALOGV("module_index %d, local_handle %d, global_handle %d",
Mike Lockwoodf8477622013-10-17 08:05:00 -0700719 module_index, local_handle, global_handle);
720
721 mutable_sensor_index++;
722 }
723 module_index++;
724 }
725 // Set the const static global_sensors_list to the mutable one allocated by this function.
726 global_sensors_list = mutable_sensor_list;
727
728 pthread_mutex_unlock(&init_sensors_mutex);
729 ALOGV("end lazy_init_sensors_list");
730}
731
Nick Vaccaroe914d692014-10-10 13:32:38 -0700732static int module__get_sensors_list(__unused struct sensors_module_t* module,
Mike Lockwoodf8477622013-10-17 08:05:00 -0700733 struct sensor_t const** list) {
734 ALOGV("module__get_sensors_list start");
735 lazy_init_sensors_list();
736 *list = global_sensors_list;
737 ALOGV("global_sensors_count: %d", global_sensors_count);
738 for (int i = 0; i < global_sensors_count; i++) {
739 ALOGV("sensor type: %d", global_sensors_list[i].type);
740 }
741 return global_sensors_count;
742}
743
744static struct hw_module_methods_t sensors_module_methods = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700745 .open = open_sensors
Mike Lockwoodf8477622013-10-17 08:05:00 -0700746};
747
748struct sensors_module_t HAL_MODULE_INFO_SYM = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700749 .common = {
750 .tag = HARDWARE_MODULE_TAG,
751 .version_major = 1,
752 .version_minor = 1,
753 .id = SENSORS_HARDWARE_MODULE_ID,
754 .name = "MultiHal Sensor Module",
755 .author = "Google, Inc",
756 .methods = &sensors_module_methods,
757 .dso = NULL,
758 .reserved = {0},
Mike Lockwoodf8477622013-10-17 08:05:00 -0700759 },
Nick Vaccaroa889c092016-04-15 10:17:07 -0700760 .get_sensors_list = module__get_sensors_list
Mike Lockwoodf8477622013-10-17 08:05:00 -0700761};
762
Nick Vaccaro8a837082016-12-12 16:50:57 -0800763struct sensors_module_t *get_multi_hal_module_info() {
764 return (&HAL_MODULE_INFO_SYM);
765}
766
Mike Lockwoodf8477622013-10-17 08:05:00 -0700767static int open_sensors(const struct hw_module_t* hw_module, const char* name,
768 struct hw_device_t** hw_device_out) {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700769 ALOGV("open_sensors begin...");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700770
771 lazy_init_modules();
772
773 // Create proxy device, to return later.
774 sensors_poll_context_t *dev = new sensors_poll_context_t();
775 memset(dev, 0, sizeof(sensors_poll_device_1_t));
776 dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700777 dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_4;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700778 dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
779 dev->proxy_device.common.close = device__close;
780 dev->proxy_device.activate = device__activate;
781 dev->proxy_device.setDelay = device__setDelay;
782 dev->proxy_device.poll = device__poll;
783 dev->proxy_device.batch = device__batch;
784 dev->proxy_device.flush = device__flush;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700785 dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
Peng Xu15b43e92017-04-27 15:45:44 -0700786 dev->proxy_device.register_direct_channel = device__register_direct_channel;
787 dev->proxy_device.config_direct_report = device__config_direct_report;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700788
789 dev->nextReadIndex = 0;
790
791 // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
792 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
793 it != sub_hw_modules->end(); it++) {
794 sensors_module_t *sensors_module = (sensors_module_t*) *it;
795 struct hw_device_t* sub_hw_device;
796 int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700797 if (!sub_open_result) {
798 if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) {
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700799 ALOGE("SENSORS_DEVICE_API_VERSION_1_3 or newer is required for all sensor HALs");
Nick Vaccaroe914d692014-10-10 13:32:38 -0700800 ALOGE("This HAL reports non-compliant API level : %s",
801 apiNumToStr(sub_hw_device->version));
802 ALOGE("Sensors belonging to this HAL will get ignored !");
803 }
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700804 dev->addSubHwDevice(sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700805 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700806 }
807
808 // Prepare the output param and return
809 *hw_device_out = &dev->proxy_device.common;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700810 ALOGV("...open_sensors end");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700811 return 0;
812}