blob: 1cea7aeb42f8794faefcae93fe53f7c0f4c294f3 [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) {
Alexey Polyudov516ef862017-05-23 18:58:25 -0700448 // if specific sensor is used, we have to replace global sensor handle
449 // with local one, before passing to concrete HAL
450 sensors_event_t data_copy = *data;
451 data_copy.sensor = local_handle;
452 retval = v1->inject_sensor_data(v1, &data_copy);
Peng Xu15b43e92017-04-27 15:45:44 -0700453 } else {
454 ALOGE("IGNORED inject_sensor_data(type=%d, handle=%d) call to non-API-compliant sensor",
455 data->type, data->sensor);
456 retval = -ENOSYS;
457 }
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700458 }
459 ALOGV("retval %d", retval);
460 return retval;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700461}
462
Peng Xu15b43e92017-04-27 15:45:44 -0700463int sensors_poll_context_t::register_direct_channel(const struct sensors_direct_mem_t* mem,
464 int channel_handle) {
465 int retval = -EINVAL;
466 ALOGV("register_direct_channel");
467 sensors_poll_device_1_t* v1 = get_primary_v1_device();
468 if (v1 && halSupportDirectSensorReport(v1)) {
469 retval = v1->register_direct_channel(v1, mem, channel_handle);
470 } else {
471 ALOGE("IGNORED register_direct_channel(mem=%p, handle=%d) call to non-API-compliant sensor",
472 mem, channel_handle);
473 retval = -ENOSYS;
474 }
475 ALOGV("retval %d", retval);
476 return retval;
477}
478
479int sensors_poll_context_t::config_direct_report(int sensor_handle,
480 int channel_handle,
481 const struct sensors_direct_cfg_t *config) {
482 int retval = -EINVAL;
483 ALOGV("config_direct_report");
484
485 if (config != nullptr) {
486 int local_handle = get_local_handle(sensor_handle);
487 sensors_poll_device_1_t* v1 = get_primary_v1_device();
488 if (v1 && halSupportDirectSensorReport(v1)) {
489 retval = v1->config_direct_report(v1, local_handle, channel_handle, config);
490 } else {
491 ALOGE("IGNORED config_direct_report(sensor=%d, channel=%d, rate_level=%d) call to "
492 "non-API-compliant sensor", sensor_handle, channel_handle, config->rate_level);
493 retval = -ENOSYS;
494 }
495 }
496 ALOGV("retval %d", retval);
497 return retval;
498}
Mike Lockwoodf8477622013-10-17 08:05:00 -0700499int sensors_poll_context_t::close() {
500 ALOGV("close");
501 for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
502 it != this->sub_hw_devices.end(); it++) {
503 hw_device_t* dev = *it;
504 int retval = dev->close(dev);
505 ALOGV("retval %d", retval);
506 }
507 return 0;
508}
509
510
511static int device__close(struct hw_device_t *dev) {
512 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
513 if (ctx != NULL) {
514 int retval = ctx->close();
515 delete ctx;
516 }
517 return 0;
518}
519
520static int device__activate(struct sensors_poll_device_t *dev, int handle,
521 int enabled) {
522 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
523 return ctx->activate(handle, enabled);
524}
525
526static int device__setDelay(struct sensors_poll_device_t *dev, int handle,
527 int64_t ns) {
528 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
529 return ctx->setDelay(handle, ns);
530}
531
532static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data,
533 int count) {
534 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
535 return ctx->poll(data, count);
536}
537
538static int device__batch(struct sensors_poll_device_1 *dev, int handle,
539 int flags, int64_t period_ns, int64_t timeout) {
540 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
541 return ctx->batch(handle, flags, period_ns, timeout);
542}
543
544static int device__flush(struct sensors_poll_device_1 *dev, int handle) {
545 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
546 return ctx->flush(handle);
547}
548
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700549static int device__inject_sensor_data(struct sensors_poll_device_1 *dev,
550 const sensors_event_t *data) {
551 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
Peng Xu15b43e92017-04-27 15:45:44 -0700552 return ctx->inject_sensor_data(data);
553}
554
555static int device__register_direct_channel(struct sensors_poll_device_1 *dev,
556 const struct sensors_direct_mem_t* mem,
557 int channel_handle) {
558 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
559 return ctx->register_direct_channel(mem, channel_handle);
560}
561
562static int device__config_direct_report(struct sensors_poll_device_1 *dev,
563 int sensor_handle,
564 int channel_handle,
565 const struct sensors_direct_cfg_t *config) {
566 sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
567 return ctx->config_direct_report(sensor_handle, channel_handle, config);
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700568}
569
Mike Lockwoodf8477622013-10-17 08:05:00 -0700570static int open_sensors(const struct hw_module_t* module, const char* name,
571 struct hw_device_t** device);
572
573static bool starts_with(const char* s, const char* prefix) {
574 if (s == NULL || prefix == NULL) {
575 return false;
576 }
577 size_t s_size = strlen(s);
578 size_t prefix_size = strlen(prefix);
579 return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0;
580}
581
582/*
583 * Adds valid paths from the config file to the vector passed in.
584 * The vector must not be null.
585 */
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800586static void get_so_paths(std::vector<std::string> *so_paths) {
Peng Xu900cfac2017-04-22 14:29:06 -0700587 const std::vector<const char *> config_path_list(
588 { MULTI_HAL_CONFIG_FILE_PATH, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH });
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800589
Peng Xu900cfac2017-04-22 14:29:06 -0700590 std::ifstream stream;
591 const char *path = nullptr;
592 for (auto i : config_path_list) {
593 std::ifstream f(i);
594 if (f) {
595 stream = std::move(f);
596 path = i;
597 break;
598 }
599 }
600 if(!stream) {
601 ALOGW("No multihal config file found");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700602 return;
603 }
Peng Xu900cfac2017-04-22 14:29:06 -0700604
605 ALOGE_IF(strcmp(path, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH) == 0,
606 "Multihal configuration file path %s is not compatible with Treble "
607 "requirements. Please move it to %s.",
608 path, MULTI_HAL_CONFIG_FILE_PATH);
609
610 ALOGV("Multihal config file found at %s", path);
611 std::string line;
612 while (std::getline(stream, line)) {
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800613 ALOGV("config file line: '%s'", line.c_str());
614 so_paths->push_back(line);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700615 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700616}
617
618/*
619 * Ensures that the sub-module array is initialized.
620 * This can be first called from get_sensors_list or from open_sensors.
621 */
622static void lazy_init_modules() {
623 pthread_mutex_lock(&init_modules_mutex);
624 if (sub_hw_modules != NULL) {
625 pthread_mutex_unlock(&init_modules_mutex);
626 return;
627 }
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800628 std::vector<std::string> *so_paths = new std::vector<std::string>();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700629 get_so_paths(so_paths);
630
631 // dlopen the module files and cache their module symbols in sub_hw_modules
632 sub_hw_modules = new std::vector<hw_module_t *>();
633 dlerror(); // clear any old errors
634 const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
Satya Durga Srinivasu Prabhala1fd36182015-01-20 18:53:35 -0800635 for (std::vector<std::string>::iterator it = so_paths->begin(); it != so_paths->end(); it++) {
636 const char* path = it->c_str();
Mike Lockwoodf8477622013-10-17 08:05:00 -0700637 void* lib_handle = dlopen(path, RTLD_LAZY);
638 if (lib_handle == NULL) {
639 ALOGW("dlerror(): %s", dlerror());
640 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700641 ALOGI("Loaded library from %s", path);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700642 ALOGV("Opening symbol \"%s\"", sym);
643 // clear old errors
644 dlerror();
645 struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym);
646 const char* error;
647 if ((error = dlerror()) != NULL) {
648 ALOGW("Error calling dlsym: %s", error);
649 } else if (module == NULL) {
650 ALOGW("module == NULL");
651 } else {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700652 ALOGV("Loaded symbols from \"%s\"", sym);
Mike Lockwoodf8477622013-10-17 08:05:00 -0700653 sub_hw_modules->push_back(module);
654 }
655 }
656 }
657 pthread_mutex_unlock(&init_modules_mutex);
658}
659
660/*
661 * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles.
662 */
663static void lazy_init_sensors_list() {
664 ALOGV("lazy_init_sensors_list");
665 pthread_mutex_lock(&init_sensors_mutex);
666 if (global_sensors_list != NULL) {
667 // already initialized
668 pthread_mutex_unlock(&init_sensors_mutex);
669 ALOGV("lazy_init_sensors_list - early return");
670 return;
671 }
672
673 ALOGV("lazy_init_sensors_list needs to do work");
674 lazy_init_modules();
675
676 // Count all the sensors, then allocate an array of blanks.
677 global_sensors_count = 0;
678 const struct sensor_t *subhal_sensors_list;
679 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
680 it != sub_hw_modules->end(); it++) {
681 struct sensors_module_t *module = (struct sensors_module_t*) *it;
682 global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list);
683 ALOGV("increased global_sensors_count to %d", global_sensors_count);
684 }
685
686 // The global_sensors_list is full of consts.
687 // Manipulate this non-const list, and point the const one to it when we're done.
688 sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count];
689
690 // index of the next sensor to set in mutable_sensor_list
691 int mutable_sensor_index = 0;
692 int module_index = 0;
693
694 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
695 it != sub_hw_modules->end(); it++) {
696 hw_module_t *hw_module = *it;
697 ALOGV("examine one module");
698 // Read the sub-module's sensor list.
699 struct sensors_module_t *module = (struct sensors_module_t*) hw_module;
700 int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list);
701 ALOGV("the module has %d sensors", module_sensor_count);
702
703 // Copy the HAL's sensor list into global_sensors_list,
704 // with the handle changed to be a global handle.
705 for (int i = 0; i < module_sensor_count; i++) {
706 ALOGV("examining one sensor");
707 const struct sensor_t *local_sensor = &subhal_sensors_list[i];
708 int local_handle = local_sensor->handle;
709 memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
710 sizeof(struct sensor_t));
711
Peng Xu15b43e92017-04-27 15:45:44 -0700712 // sensor direct report is only for primary module
713 if (module_index != 0) {
714 mutable_sensor_list[mutable_sensor_index].flags &=
715 ~(SENSOR_FLAG_MASK_DIRECT_REPORT | SENSOR_FLAG_MASK_DIRECT_CHANNEL);
716 }
717
Mike Lockwoodf8477622013-10-17 08:05:00 -0700718 // Overwrite the global version's handle with a global handle.
719 int global_handle = assign_global_handle(module_index, local_handle);
720
721 mutable_sensor_list[mutable_sensor_index].handle = global_handle;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700722 ALOGV("module_index %d, local_handle %d, global_handle %d",
Mike Lockwoodf8477622013-10-17 08:05:00 -0700723 module_index, local_handle, global_handle);
724
725 mutable_sensor_index++;
726 }
727 module_index++;
728 }
729 // Set the const static global_sensors_list to the mutable one allocated by this function.
730 global_sensors_list = mutable_sensor_list;
731
732 pthread_mutex_unlock(&init_sensors_mutex);
733 ALOGV("end lazy_init_sensors_list");
734}
735
Nick Vaccaroe914d692014-10-10 13:32:38 -0700736static int module__get_sensors_list(__unused struct sensors_module_t* module,
Mike Lockwoodf8477622013-10-17 08:05:00 -0700737 struct sensor_t const** list) {
738 ALOGV("module__get_sensors_list start");
739 lazy_init_sensors_list();
740 *list = global_sensors_list;
741 ALOGV("global_sensors_count: %d", global_sensors_count);
742 for (int i = 0; i < global_sensors_count; i++) {
743 ALOGV("sensor type: %d", global_sensors_list[i].type);
744 }
745 return global_sensors_count;
746}
747
748static struct hw_module_methods_t sensors_module_methods = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700749 .open = open_sensors
Mike Lockwoodf8477622013-10-17 08:05:00 -0700750};
751
752struct sensors_module_t HAL_MODULE_INFO_SYM = {
Nick Vaccaroa889c092016-04-15 10:17:07 -0700753 .common = {
754 .tag = HARDWARE_MODULE_TAG,
755 .version_major = 1,
756 .version_minor = 1,
757 .id = SENSORS_HARDWARE_MODULE_ID,
758 .name = "MultiHal Sensor Module",
759 .author = "Google, Inc",
760 .methods = &sensors_module_methods,
761 .dso = NULL,
762 .reserved = {0},
Mike Lockwoodf8477622013-10-17 08:05:00 -0700763 },
Nick Vaccaroa889c092016-04-15 10:17:07 -0700764 .get_sensors_list = module__get_sensors_list
Mike Lockwoodf8477622013-10-17 08:05:00 -0700765};
766
Nick Vaccaro8a837082016-12-12 16:50:57 -0800767struct sensors_module_t *get_multi_hal_module_info() {
768 return (&HAL_MODULE_INFO_SYM);
769}
770
Mike Lockwoodf8477622013-10-17 08:05:00 -0700771static int open_sensors(const struct hw_module_t* hw_module, const char* name,
772 struct hw_device_t** hw_device_out) {
Aaron Whytef22e1c12014-04-15 15:41:38 -0700773 ALOGV("open_sensors begin...");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700774
775 lazy_init_modules();
776
777 // Create proxy device, to return later.
778 sensors_poll_context_t *dev = new sensors_poll_context_t();
779 memset(dev, 0, sizeof(sensors_poll_device_1_t));
780 dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700781 dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_4;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700782 dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
783 dev->proxy_device.common.close = device__close;
784 dev->proxy_device.activate = device__activate;
785 dev->proxy_device.setDelay = device__setDelay;
786 dev->proxy_device.poll = device__poll;
787 dev->proxy_device.batch = device__batch;
788 dev->proxy_device.flush = device__flush;
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700789 dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
Peng Xu15b43e92017-04-27 15:45:44 -0700790 dev->proxy_device.register_direct_channel = device__register_direct_channel;
791 dev->proxy_device.config_direct_report = device__config_direct_report;
Mike Lockwoodf8477622013-10-17 08:05:00 -0700792
793 dev->nextReadIndex = 0;
794
795 // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
796 for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
797 it != sub_hw_modules->end(); it++) {
798 sensors_module_t *sensors_module = (sensors_module_t*) *it;
799 struct hw_device_t* sub_hw_device;
800 int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700801 if (!sub_open_result) {
802 if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) {
Nick Vaccaroc1ded2a2016-10-14 10:24:10 -0700803 ALOGE("SENSORS_DEVICE_API_VERSION_1_3 or newer is required for all sensor HALs");
Nick Vaccaroe914d692014-10-10 13:32:38 -0700804 ALOGE("This HAL reports non-compliant API level : %s",
805 apiNumToStr(sub_hw_device->version));
806 ALOGE("Sensors belonging to this HAL will get ignored !");
807 }
Nick Vaccaro93bf9962014-03-17 13:05:09 -0700808 dev->addSubHwDevice(sub_hw_device);
Nick Vaccaroe914d692014-10-10 13:32:38 -0700809 }
Mike Lockwoodf8477622013-10-17 08:05:00 -0700810 }
811
812 // Prepare the output param and return
813 *hw_device_out = &dev->proxy_device.common;
Aaron Whytef22e1c12014-04-15 15:41:38 -0700814 ALOGV("...open_sensors end");
Mike Lockwoodf8477622013-10-17 08:05:00 -0700815 return 0;
816}