1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker * Copyright (C) 2013 The Android Open Source Project
3*e01b6f76SAndroid Build Coastguard Worker *
4*e01b6f76SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*e01b6f76SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*e01b6f76SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*e01b6f76SAndroid Build Coastguard Worker *
8*e01b6f76SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*e01b6f76SAndroid Build Coastguard Worker *
10*e01b6f76SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*e01b6f76SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*e01b6f76SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e01b6f76SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*e01b6f76SAndroid Build Coastguard Worker * limitations under the License.
15*e01b6f76SAndroid Build Coastguard Worker */
16*e01b6f76SAndroid Build Coastguard Worker
17*e01b6f76SAndroid Build Coastguard Worker #include "SensorEventQueue.h"
18*e01b6f76SAndroid Build Coastguard Worker #include "multihal.h"
19*e01b6f76SAndroid Build Coastguard Worker
20*e01b6f76SAndroid Build Coastguard Worker #define LOG_NDEBUG 1
21*e01b6f76SAndroid Build Coastguard Worker #include <log/log.h>
22*e01b6f76SAndroid Build Coastguard Worker #include <cutils/atomic.h>
23*e01b6f76SAndroid Build Coastguard Worker #include <hardware/sensors.h>
24*e01b6f76SAndroid Build Coastguard Worker
25*e01b6f76SAndroid Build Coastguard Worker #include <vector>
26*e01b6f76SAndroid Build Coastguard Worker #include <string>
27*e01b6f76SAndroid Build Coastguard Worker #include <fstream>
28*e01b6f76SAndroid Build Coastguard Worker #include <map>
29*e01b6f76SAndroid Build Coastguard Worker
30*e01b6f76SAndroid Build Coastguard Worker #include <dirent.h>
31*e01b6f76SAndroid Build Coastguard Worker #include <dlfcn.h>
32*e01b6f76SAndroid Build Coastguard Worker #include <errno.h>
33*e01b6f76SAndroid Build Coastguard Worker #include <fcntl.h>
34*e01b6f76SAndroid Build Coastguard Worker #include <limits.h>
35*e01b6f76SAndroid Build Coastguard Worker #include <math.h>
36*e01b6f76SAndroid Build Coastguard Worker #include <poll.h>
37*e01b6f76SAndroid Build Coastguard Worker #include <pthread.h>
38*e01b6f76SAndroid Build Coastguard Worker #include <stdio.h>
39*e01b6f76SAndroid Build Coastguard Worker #include <stdlib.h>
40*e01b6f76SAndroid Build Coastguard Worker
41*e01b6f76SAndroid Build Coastguard Worker
42*e01b6f76SAndroid Build Coastguard Worker static pthread_mutex_t init_modules_mutex = PTHREAD_MUTEX_INITIALIZER;
43*e01b6f76SAndroid Build Coastguard Worker static pthread_mutex_t init_sensors_mutex = PTHREAD_MUTEX_INITIALIZER;
44*e01b6f76SAndroid Build Coastguard Worker
45*e01b6f76SAndroid Build Coastguard Worker // This mutex is shared by all queues
46*e01b6f76SAndroid Build Coastguard Worker static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
47*e01b6f76SAndroid Build Coastguard Worker
48*e01b6f76SAndroid Build Coastguard Worker // Used to pause the multihal poll(). Broadcasted by sub-polling tasks if waiting_for_data.
49*e01b6f76SAndroid Build Coastguard Worker static pthread_cond_t data_available_cond = PTHREAD_COND_INITIALIZER;
50*e01b6f76SAndroid Build Coastguard Worker bool waiting_for_data = false;
51*e01b6f76SAndroid Build Coastguard Worker
52*e01b6f76SAndroid Build Coastguard Worker // Vector of sub modules, whose indexes are referred to in this file as module_index.
53*e01b6f76SAndroid Build Coastguard Worker static std::vector<hw_module_t *> *sub_hw_modules = nullptr;
54*e01b6f76SAndroid Build Coastguard Worker
55*e01b6f76SAndroid Build Coastguard Worker // Vector of sub modules shared object handles
56*e01b6f76SAndroid Build Coastguard Worker static std::vector<void *> *so_handles = nullptr;
57*e01b6f76SAndroid Build Coastguard Worker
58*e01b6f76SAndroid Build Coastguard Worker /*
59*e01b6f76SAndroid Build Coastguard Worker * Comparable class that globally identifies a sensor, by module index and local handle.
60*e01b6f76SAndroid Build Coastguard Worker * A module index is the module's index in sub_hw_modules.
61*e01b6f76SAndroid Build Coastguard Worker * A local handle is the handle the sub-module assigns to a sensor.
62*e01b6f76SAndroid Build Coastguard Worker */
63*e01b6f76SAndroid Build Coastguard Worker struct FullHandle {
64*e01b6f76SAndroid Build Coastguard Worker int moduleIndex;
65*e01b6f76SAndroid Build Coastguard Worker int localHandle;
66*e01b6f76SAndroid Build Coastguard Worker
operator <FullHandle67*e01b6f76SAndroid Build Coastguard Worker bool operator<(const FullHandle &that) const {
68*e01b6f76SAndroid Build Coastguard Worker if (moduleIndex < that.moduleIndex) {
69*e01b6f76SAndroid Build Coastguard Worker return true;
70*e01b6f76SAndroid Build Coastguard Worker }
71*e01b6f76SAndroid Build Coastguard Worker if (moduleIndex > that.moduleIndex) {
72*e01b6f76SAndroid Build Coastguard Worker return false;
73*e01b6f76SAndroid Build Coastguard Worker }
74*e01b6f76SAndroid Build Coastguard Worker return localHandle < that.localHandle;
75*e01b6f76SAndroid Build Coastguard Worker }
76*e01b6f76SAndroid Build Coastguard Worker
operator ==FullHandle77*e01b6f76SAndroid Build Coastguard Worker bool operator==(const FullHandle &that) const {
78*e01b6f76SAndroid Build Coastguard Worker return moduleIndex == that.moduleIndex && localHandle == that.localHandle;
79*e01b6f76SAndroid Build Coastguard Worker }
80*e01b6f76SAndroid Build Coastguard Worker };
81*e01b6f76SAndroid Build Coastguard Worker
82*e01b6f76SAndroid Build Coastguard Worker std::map<int, FullHandle> global_to_full;
83*e01b6f76SAndroid Build Coastguard Worker std::map<FullHandle, int> full_to_global;
84*e01b6f76SAndroid Build Coastguard Worker int next_global_handle = 1;
85*e01b6f76SAndroid Build Coastguard Worker
assign_global_handle(int module_index,int local_handle)86*e01b6f76SAndroid Build Coastguard Worker static int assign_global_handle(int module_index, int local_handle) {
87*e01b6f76SAndroid Build Coastguard Worker int global_handle = next_global_handle++;
88*e01b6f76SAndroid Build Coastguard Worker FullHandle full_handle;
89*e01b6f76SAndroid Build Coastguard Worker full_handle.moduleIndex = module_index;
90*e01b6f76SAndroid Build Coastguard Worker full_handle.localHandle = local_handle;
91*e01b6f76SAndroid Build Coastguard Worker full_to_global[full_handle] = global_handle;
92*e01b6f76SAndroid Build Coastguard Worker global_to_full[global_handle] = full_handle;
93*e01b6f76SAndroid Build Coastguard Worker return global_handle;
94*e01b6f76SAndroid Build Coastguard Worker }
95*e01b6f76SAndroid Build Coastguard Worker
96*e01b6f76SAndroid Build Coastguard Worker // Returns the local handle, or -1 if it does not exist.
get_local_handle(int global_handle)97*e01b6f76SAndroid Build Coastguard Worker static int get_local_handle(int global_handle) {
98*e01b6f76SAndroid Build Coastguard Worker if (global_to_full.count(global_handle) == 0) {
99*e01b6f76SAndroid Build Coastguard Worker ALOGW("Unknown global_handle %d", global_handle);
100*e01b6f76SAndroid Build Coastguard Worker return -1;
101*e01b6f76SAndroid Build Coastguard Worker }
102*e01b6f76SAndroid Build Coastguard Worker return global_to_full[global_handle].localHandle;
103*e01b6f76SAndroid Build Coastguard Worker }
104*e01b6f76SAndroid Build Coastguard Worker
105*e01b6f76SAndroid Build Coastguard Worker // Returns the sub_hw_modules index of the module that contains the sensor associates with this
106*e01b6f76SAndroid Build Coastguard Worker // global_handle, or -1 if that global_handle does not exist.
get_module_index(int global_handle)107*e01b6f76SAndroid Build Coastguard Worker static int get_module_index(int global_handle) {
108*e01b6f76SAndroid Build Coastguard Worker if (global_to_full.count(global_handle) == 0) {
109*e01b6f76SAndroid Build Coastguard Worker ALOGW("Unknown global_handle %d", global_handle);
110*e01b6f76SAndroid Build Coastguard Worker return -1;
111*e01b6f76SAndroid Build Coastguard Worker }
112*e01b6f76SAndroid Build Coastguard Worker FullHandle f = global_to_full[global_handle];
113*e01b6f76SAndroid Build Coastguard Worker ALOGV("FullHandle for global_handle %d: moduleIndex %d, localHandle %d",
114*e01b6f76SAndroid Build Coastguard Worker global_handle, f.moduleIndex, f.localHandle);
115*e01b6f76SAndroid Build Coastguard Worker return f.moduleIndex;
116*e01b6f76SAndroid Build Coastguard Worker }
117*e01b6f76SAndroid Build Coastguard Worker
118*e01b6f76SAndroid Build Coastguard Worker // Returns the global handle for this full_handle, or -1 if the full_handle is unknown.
get_global_handle(FullHandle * full_handle)119*e01b6f76SAndroid Build Coastguard Worker static int get_global_handle(FullHandle* full_handle) {
120*e01b6f76SAndroid Build Coastguard Worker int global_handle = -1;
121*e01b6f76SAndroid Build Coastguard Worker if (full_to_global.count(*full_handle)) {
122*e01b6f76SAndroid Build Coastguard Worker global_handle = full_to_global[*full_handle];
123*e01b6f76SAndroid Build Coastguard Worker } else {
124*e01b6f76SAndroid Build Coastguard Worker ALOGW("Unknown FullHandle: moduleIndex %d, localHandle %d",
125*e01b6f76SAndroid Build Coastguard Worker full_handle->moduleIndex, full_handle->localHandle);
126*e01b6f76SAndroid Build Coastguard Worker }
127*e01b6f76SAndroid Build Coastguard Worker return global_handle;
128*e01b6f76SAndroid Build Coastguard Worker }
129*e01b6f76SAndroid Build Coastguard Worker
130*e01b6f76SAndroid Build Coastguard Worker static const int SENSOR_EVENT_QUEUE_CAPACITY = 36;
131*e01b6f76SAndroid Build Coastguard Worker
132*e01b6f76SAndroid Build Coastguard Worker struct TaskContext {
133*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_t* device;
134*e01b6f76SAndroid Build Coastguard Worker SensorEventQueue* queue;
135*e01b6f76SAndroid Build Coastguard Worker };
136*e01b6f76SAndroid Build Coastguard Worker
writerTask(void * ptr)137*e01b6f76SAndroid Build Coastguard Worker void *writerTask(void* ptr) {
138*e01b6f76SAndroid Build Coastguard Worker ALOGV("writerTask STARTS");
139*e01b6f76SAndroid Build Coastguard Worker TaskContext* ctx = (TaskContext*)ptr;
140*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_t* device = ctx->device;
141*e01b6f76SAndroid Build Coastguard Worker SensorEventQueue* queue = ctx->queue;
142*e01b6f76SAndroid Build Coastguard Worker sensors_event_t* buffer;
143*e01b6f76SAndroid Build Coastguard Worker int eventsPolled;
144*e01b6f76SAndroid Build Coastguard Worker while (1) {
145*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_lock(&queue_mutex);
146*e01b6f76SAndroid Build Coastguard Worker if (queue->waitForSpace(&queue_mutex)) {
147*e01b6f76SAndroid Build Coastguard Worker ALOGV("writerTask waited for space");
148*e01b6f76SAndroid Build Coastguard Worker }
149*e01b6f76SAndroid Build Coastguard Worker int bufferSize = queue->getWritableRegion(SENSOR_EVENT_QUEUE_CAPACITY, &buffer);
150*e01b6f76SAndroid Build Coastguard Worker // Do blocking poll outside of lock
151*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&queue_mutex);
152*e01b6f76SAndroid Build Coastguard Worker
153*e01b6f76SAndroid Build Coastguard Worker ALOGV("writerTask before poll() - bufferSize = %d", bufferSize);
154*e01b6f76SAndroid Build Coastguard Worker eventsPolled = device->poll(device, buffer, bufferSize);
155*e01b6f76SAndroid Build Coastguard Worker ALOGV("writerTask poll() got %d events.", eventsPolled);
156*e01b6f76SAndroid Build Coastguard Worker if (eventsPolled <= 0) {
157*e01b6f76SAndroid Build Coastguard Worker if (eventsPolled < 0) {
158*e01b6f76SAndroid Build Coastguard Worker ALOGV("writerTask ignored error %d from %s", eventsPolled, device->common.module->name);
159*e01b6f76SAndroid Build Coastguard Worker ALOGE("ERROR: Fix %s so it does not return error from poll()", device->common.module->name);
160*e01b6f76SAndroid Build Coastguard Worker }
161*e01b6f76SAndroid Build Coastguard Worker continue;
162*e01b6f76SAndroid Build Coastguard Worker }
163*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_lock(&queue_mutex);
164*e01b6f76SAndroid Build Coastguard Worker queue->markAsWritten(eventsPolled);
165*e01b6f76SAndroid Build Coastguard Worker ALOGV("writerTask wrote %d events", eventsPolled);
166*e01b6f76SAndroid Build Coastguard Worker if (waiting_for_data) {
167*e01b6f76SAndroid Build Coastguard Worker ALOGV("writerTask - broadcast data_available_cond");
168*e01b6f76SAndroid Build Coastguard Worker pthread_cond_broadcast(&data_available_cond);
169*e01b6f76SAndroid Build Coastguard Worker }
170*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&queue_mutex);
171*e01b6f76SAndroid Build Coastguard Worker }
172*e01b6f76SAndroid Build Coastguard Worker // never actually returns
173*e01b6f76SAndroid Build Coastguard Worker return NULL;
174*e01b6f76SAndroid Build Coastguard Worker }
175*e01b6f76SAndroid Build Coastguard Worker
176*e01b6f76SAndroid Build Coastguard Worker /*
177*e01b6f76SAndroid Build Coastguard Worker * Cache of all sensors, with original handles replaced by global handles.
178*e01b6f76SAndroid Build Coastguard Worker * This will be handled to get_sensors_list() callers.
179*e01b6f76SAndroid Build Coastguard Worker */
180*e01b6f76SAndroid Build Coastguard Worker static struct sensor_t const* global_sensors_list = NULL;
181*e01b6f76SAndroid Build Coastguard Worker static int global_sensors_count = -1;
182*e01b6f76SAndroid Build Coastguard Worker
183*e01b6f76SAndroid Build Coastguard Worker /*
184*e01b6f76SAndroid Build Coastguard Worker * Extends a sensors_poll_device_1 by including all the sub-module's devices.
185*e01b6f76SAndroid Build Coastguard Worker */
186*e01b6f76SAndroid Build Coastguard Worker struct sensors_poll_context_t {
187*e01b6f76SAndroid Build Coastguard Worker /*
188*e01b6f76SAndroid Build Coastguard Worker * This is the device that SensorDevice.cpp uses to make API calls
189*e01b6f76SAndroid Build Coastguard Worker * to the multihal, which fans them out to sub-HALs.
190*e01b6f76SAndroid Build Coastguard Worker */
191*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1 proxy_device; // must be first
192*e01b6f76SAndroid Build Coastguard Worker
193*e01b6f76SAndroid Build Coastguard Worker void addSubHwDevice(struct hw_device_t*);
194*e01b6f76SAndroid Build Coastguard Worker
195*e01b6f76SAndroid Build Coastguard Worker int activate(int handle, int enabled);
196*e01b6f76SAndroid Build Coastguard Worker int setDelay(int handle, int64_t ns);
197*e01b6f76SAndroid Build Coastguard Worker int poll(sensors_event_t* data, int count);
198*e01b6f76SAndroid Build Coastguard Worker int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
199*e01b6f76SAndroid Build Coastguard Worker int flush(int handle);
200*e01b6f76SAndroid Build Coastguard Worker int inject_sensor_data(const sensors_event_t *data);
201*e01b6f76SAndroid Build Coastguard Worker int register_direct_channel(const struct sensors_direct_mem_t* mem,
202*e01b6f76SAndroid Build Coastguard Worker int channel_handle);
203*e01b6f76SAndroid Build Coastguard Worker int config_direct_report(int sensor_handle,
204*e01b6f76SAndroid Build Coastguard Worker int channel_handle,
205*e01b6f76SAndroid Build Coastguard Worker const struct sensors_direct_cfg_t *config);
206*e01b6f76SAndroid Build Coastguard Worker int close();
207*e01b6f76SAndroid Build Coastguard Worker
208*e01b6f76SAndroid Build Coastguard Worker std::vector<hw_device_t*> sub_hw_devices;
209*e01b6f76SAndroid Build Coastguard Worker std::vector<SensorEventQueue*> queues;
210*e01b6f76SAndroid Build Coastguard Worker std::vector<pthread_t> threads;
211*e01b6f76SAndroid Build Coastguard Worker int nextReadIndex;
212*e01b6f76SAndroid Build Coastguard Worker
213*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_t* get_v0_device_by_handle(int global_handle);
214*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle);
215*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* get_primary_v1_device();
216*e01b6f76SAndroid Build Coastguard Worker int get_device_version_by_handle(int global_handle);
217*e01b6f76SAndroid Build Coastguard Worker
218*e01b6f76SAndroid Build Coastguard Worker void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index);
219*e01b6f76SAndroid Build Coastguard Worker };
220*e01b6f76SAndroid Build Coastguard Worker
addSubHwDevice(struct hw_device_t * sub_hw_device)221*e01b6f76SAndroid Build Coastguard Worker void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) {
222*e01b6f76SAndroid Build Coastguard Worker ALOGV("addSubHwDevice");
223*e01b6f76SAndroid Build Coastguard Worker this->sub_hw_devices.push_back(sub_hw_device);
224*e01b6f76SAndroid Build Coastguard Worker
225*e01b6f76SAndroid Build Coastguard Worker SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY);
226*e01b6f76SAndroid Build Coastguard Worker this->queues.push_back(queue);
227*e01b6f76SAndroid Build Coastguard Worker
228*e01b6f76SAndroid Build Coastguard Worker TaskContext* taskContext = new TaskContext();
229*e01b6f76SAndroid Build Coastguard Worker taskContext->device = (sensors_poll_device_t*) sub_hw_device;
230*e01b6f76SAndroid Build Coastguard Worker taskContext->queue = queue;
231*e01b6f76SAndroid Build Coastguard Worker
232*e01b6f76SAndroid Build Coastguard Worker pthread_t writerThread;
233*e01b6f76SAndroid Build Coastguard Worker pthread_create(&writerThread, NULL, writerTask, taskContext);
234*e01b6f76SAndroid Build Coastguard Worker this->threads.push_back(writerThread);
235*e01b6f76SAndroid Build Coastguard Worker }
236*e01b6f76SAndroid Build Coastguard Worker
237*e01b6f76SAndroid Build Coastguard Worker // Returns the device pointer, or NULL if the global handle is invalid.
get_v0_device_by_handle(int global_handle)238*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) {
239*e01b6f76SAndroid Build Coastguard Worker int sub_index = get_module_index(global_handle);
240*e01b6f76SAndroid Build Coastguard Worker if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
241*e01b6f76SAndroid Build Coastguard Worker return NULL;
242*e01b6f76SAndroid Build Coastguard Worker }
243*e01b6f76SAndroid Build Coastguard Worker return (sensors_poll_device_t*) this->sub_hw_devices[sub_index];
244*e01b6f76SAndroid Build Coastguard Worker }
245*e01b6f76SAndroid Build Coastguard Worker
246*e01b6f76SAndroid Build Coastguard Worker // Returns the device pointer, or NULL if the global handle is invalid.
get_v1_device_by_handle(int global_handle)247*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) {
248*e01b6f76SAndroid Build Coastguard Worker int sub_index = get_module_index(global_handle);
249*e01b6f76SAndroid Build Coastguard Worker if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
250*e01b6f76SAndroid Build Coastguard Worker return NULL;
251*e01b6f76SAndroid Build Coastguard Worker }
252*e01b6f76SAndroid Build Coastguard Worker return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
253*e01b6f76SAndroid Build Coastguard Worker }
254*e01b6f76SAndroid Build Coastguard Worker
255*e01b6f76SAndroid Build Coastguard Worker // Returns the device pointer, or NULL if primary hal does not exist
get_primary_v1_device()256*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* sensors_poll_context_t::get_primary_v1_device() {
257*e01b6f76SAndroid Build Coastguard Worker if (sub_hw_devices.size() < 1) {
258*e01b6f76SAndroid Build Coastguard Worker return nullptr;
259*e01b6f76SAndroid Build Coastguard Worker }
260*e01b6f76SAndroid Build Coastguard Worker return (sensors_poll_device_1_t*) this->sub_hw_devices[0];
261*e01b6f76SAndroid Build Coastguard Worker }
262*e01b6f76SAndroid Build Coastguard Worker
263*e01b6f76SAndroid Build Coastguard Worker // Returns the device version, or -1 if the handle is invalid.
get_device_version_by_handle(int handle)264*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::get_device_version_by_handle(int handle) {
265*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
266*e01b6f76SAndroid Build Coastguard Worker if (v0) {
267*e01b6f76SAndroid Build Coastguard Worker return v0->common.version;
268*e01b6f76SAndroid Build Coastguard Worker } else {
269*e01b6f76SAndroid Build Coastguard Worker return -1;
270*e01b6f76SAndroid Build Coastguard Worker }
271*e01b6f76SAndroid Build Coastguard Worker }
272*e01b6f76SAndroid Build Coastguard Worker
273*e01b6f76SAndroid Build Coastguard Worker // Android N and hire require sensor HALs to be at least 1_3 compliant
274*e01b6f76SAndroid Build Coastguard Worker #define HAL_VERSION_IS_COMPLIANT(version) \
275*e01b6f76SAndroid Build Coastguard Worker (version >= SENSORS_DEVICE_API_VERSION_1_3)
276*e01b6f76SAndroid Build Coastguard Worker
277*e01b6f76SAndroid Build Coastguard Worker // Returns true if HAL is compliant, false if HAL is not compliant or if handle is invalid
halIsCompliant(sensors_poll_context_t * ctx,int handle)278*e01b6f76SAndroid Build Coastguard Worker static bool halIsCompliant(sensors_poll_context_t *ctx, int handle) {
279*e01b6f76SAndroid Build Coastguard Worker int version = ctx->get_device_version_by_handle(handle);
280*e01b6f76SAndroid Build Coastguard Worker return version != -1 && HAL_VERSION_IS_COMPLIANT(version);
281*e01b6f76SAndroid Build Coastguard Worker }
282*e01b6f76SAndroid Build Coastguard Worker
halIsAPILevelCompliant(sensors_poll_context_t * ctx,int handle,int level)283*e01b6f76SAndroid Build Coastguard Worker static bool halIsAPILevelCompliant(sensors_poll_context_t *ctx, int handle, int level) {
284*e01b6f76SAndroid Build Coastguard Worker int version = ctx->get_device_version_by_handle(handle);
285*e01b6f76SAndroid Build Coastguard Worker return version != -1 && (version >= level);
286*e01b6f76SAndroid Build Coastguard Worker }
287*e01b6f76SAndroid Build Coastguard Worker
halSupportDirectSensorReport(sensors_poll_device_1_t * v1)288*e01b6f76SAndroid Build Coastguard Worker static bool halSupportDirectSensorReport(sensors_poll_device_1_t* v1) {
289*e01b6f76SAndroid Build Coastguard Worker return v1 != nullptr && HAL_VERSION_IS_COMPLIANT(v1->common.version) &&
290*e01b6f76SAndroid Build Coastguard Worker v1->register_direct_channel != nullptr && v1->config_direct_report != nullptr;
291*e01b6f76SAndroid Build Coastguard Worker }
292*e01b6f76SAndroid Build Coastguard Worker
apiNumToStr(int version)293*e01b6f76SAndroid Build Coastguard Worker const char *apiNumToStr(int version) {
294*e01b6f76SAndroid Build Coastguard Worker switch(version) {
295*e01b6f76SAndroid Build Coastguard Worker case SENSORS_DEVICE_API_VERSION_1_0:
296*e01b6f76SAndroid Build Coastguard Worker return "SENSORS_DEVICE_API_VERSION_1_0";
297*e01b6f76SAndroid Build Coastguard Worker case SENSORS_DEVICE_API_VERSION_1_1:
298*e01b6f76SAndroid Build Coastguard Worker return "SENSORS_DEVICE_API_VERSION_1_1";
299*e01b6f76SAndroid Build Coastguard Worker case SENSORS_DEVICE_API_VERSION_1_2:
300*e01b6f76SAndroid Build Coastguard Worker return "SENSORS_DEVICE_API_VERSION_1_2";
301*e01b6f76SAndroid Build Coastguard Worker case SENSORS_DEVICE_API_VERSION_1_3:
302*e01b6f76SAndroid Build Coastguard Worker return "SENSORS_DEVICE_API_VERSION_1_3";
303*e01b6f76SAndroid Build Coastguard Worker case SENSORS_DEVICE_API_VERSION_1_4:
304*e01b6f76SAndroid Build Coastguard Worker return "SENSORS_DEVICE_API_VERSION_1_4";
305*e01b6f76SAndroid Build Coastguard Worker default:
306*e01b6f76SAndroid Build Coastguard Worker return "UNKNOWN";
307*e01b6f76SAndroid Build Coastguard Worker }
308*e01b6f76SAndroid Build Coastguard Worker }
309*e01b6f76SAndroid Build Coastguard Worker
activate(int handle,int enabled)310*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::activate(int handle, int enabled) {
311*e01b6f76SAndroid Build Coastguard Worker int retval = -EINVAL;
312*e01b6f76SAndroid Build Coastguard Worker ALOGV("activate");
313*e01b6f76SAndroid Build Coastguard Worker int local_handle = get_local_handle(handle);
314*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
315*e01b6f76SAndroid Build Coastguard Worker if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
316*e01b6f76SAndroid Build Coastguard Worker retval = v0->activate(v0, local_handle, enabled);
317*e01b6f76SAndroid Build Coastguard Worker } else {
318*e01b6f76SAndroid Build Coastguard Worker ALOGE("IGNORING activate(enable %d) call to non-API-compliant sensor handle=%d !",
319*e01b6f76SAndroid Build Coastguard Worker enabled, handle);
320*e01b6f76SAndroid Build Coastguard Worker }
321*e01b6f76SAndroid Build Coastguard Worker ALOGV("retval %d", retval);
322*e01b6f76SAndroid Build Coastguard Worker return retval;
323*e01b6f76SAndroid Build Coastguard Worker }
324*e01b6f76SAndroid Build Coastguard Worker
setDelay(int handle,int64_t ns)325*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
326*e01b6f76SAndroid Build Coastguard Worker int retval = -EINVAL;
327*e01b6f76SAndroid Build Coastguard Worker ALOGV("setDelay");
328*e01b6f76SAndroid Build Coastguard Worker int local_handle = get_local_handle(handle);
329*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
330*e01b6f76SAndroid Build Coastguard Worker if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
331*e01b6f76SAndroid Build Coastguard Worker retval = v0->setDelay(v0, local_handle, ns);
332*e01b6f76SAndroid Build Coastguard Worker } else {
333*e01b6f76SAndroid Build Coastguard Worker ALOGE("IGNORING setDelay() call for non-API-compliant sensor handle=%d !", handle);
334*e01b6f76SAndroid Build Coastguard Worker }
335*e01b6f76SAndroid Build Coastguard Worker ALOGV("retval %d", retval);
336*e01b6f76SAndroid Build Coastguard Worker return retval;
337*e01b6f76SAndroid Build Coastguard Worker }
338*e01b6f76SAndroid Build Coastguard Worker
copy_event_remap_handle(sensors_event_t * dest,sensors_event_t * src,int sub_index)339*e01b6f76SAndroid Build Coastguard Worker void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src,
340*e01b6f76SAndroid Build Coastguard Worker int sub_index) {
341*e01b6f76SAndroid Build Coastguard Worker memcpy(dest, src, sizeof(struct sensors_event_t));
342*e01b6f76SAndroid Build Coastguard Worker // A normal event's "sensor" field is a local handle. Convert it to a global handle.
343*e01b6f76SAndroid Build Coastguard Worker // A meta-data event must have its sensor set to 0, but it has a nested event
344*e01b6f76SAndroid Build Coastguard Worker // with a local handle that needs to be converted to a global handle.
345*e01b6f76SAndroid Build Coastguard Worker FullHandle full_handle;
346*e01b6f76SAndroid Build Coastguard Worker full_handle.moduleIndex = sub_index;
347*e01b6f76SAndroid Build Coastguard Worker
348*e01b6f76SAndroid Build Coastguard Worker // If it's a metadata event, rewrite the inner payload, not the sensor field.
349*e01b6f76SAndroid Build Coastguard Worker // If the event's sensor field is unregistered for any reason, rewrite the sensor field
350*e01b6f76SAndroid Build Coastguard Worker // with a -1, instead of writing an incorrect but plausible sensor number, because
351*e01b6f76SAndroid Build Coastguard Worker // get_global_handle() returns -1 for unknown FullHandles.
352*e01b6f76SAndroid Build Coastguard Worker if (dest->type == SENSOR_TYPE_META_DATA) {
353*e01b6f76SAndroid Build Coastguard Worker full_handle.localHandle = dest->meta_data.sensor;
354*e01b6f76SAndroid Build Coastguard Worker dest->meta_data.sensor = get_global_handle(&full_handle);
355*e01b6f76SAndroid Build Coastguard Worker } else {
356*e01b6f76SAndroid Build Coastguard Worker full_handle.localHandle = dest->sensor;
357*e01b6f76SAndroid Build Coastguard Worker dest->sensor = get_global_handle(&full_handle);
358*e01b6f76SAndroid Build Coastguard Worker }
359*e01b6f76SAndroid Build Coastguard Worker }
360*e01b6f76SAndroid Build Coastguard Worker
poll(sensors_event_t * data,int maxReads)361*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) {
362*e01b6f76SAndroid Build Coastguard Worker ALOGV("poll");
363*e01b6f76SAndroid Build Coastguard Worker int empties = 0;
364*e01b6f76SAndroid Build Coastguard Worker int queueCount = 0;
365*e01b6f76SAndroid Build Coastguard Worker int eventsRead = 0;
366*e01b6f76SAndroid Build Coastguard Worker
367*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_lock(&queue_mutex);
368*e01b6f76SAndroid Build Coastguard Worker queueCount = (int)this->queues.size();
369*e01b6f76SAndroid Build Coastguard Worker while (eventsRead == 0) {
370*e01b6f76SAndroid Build Coastguard Worker while (empties < queueCount && eventsRead < maxReads) {
371*e01b6f76SAndroid Build Coastguard Worker SensorEventQueue* queue = this->queues.at(this->nextReadIndex);
372*e01b6f76SAndroid Build Coastguard Worker sensors_event_t* event = queue->peek();
373*e01b6f76SAndroid Build Coastguard Worker if (event == NULL) {
374*e01b6f76SAndroid Build Coastguard Worker empties++;
375*e01b6f76SAndroid Build Coastguard Worker } else {
376*e01b6f76SAndroid Build Coastguard Worker empties = 0;
377*e01b6f76SAndroid Build Coastguard Worker this->copy_event_remap_handle(&data[eventsRead], event, nextReadIndex);
378*e01b6f76SAndroid Build Coastguard Worker if (data[eventsRead].sensor == SENSORS_HANDLE_BASE - 1) {
379*e01b6f76SAndroid Build Coastguard Worker // Bad handle, do not pass corrupted event upstream !
380*e01b6f76SAndroid Build Coastguard Worker ALOGW("Dropping bad local handle event packet on the floor");
381*e01b6f76SAndroid Build Coastguard Worker } else {
382*e01b6f76SAndroid Build Coastguard Worker eventsRead++;
383*e01b6f76SAndroid Build Coastguard Worker }
384*e01b6f76SAndroid Build Coastguard Worker queue->dequeue();
385*e01b6f76SAndroid Build Coastguard Worker }
386*e01b6f76SAndroid Build Coastguard Worker this->nextReadIndex = (this->nextReadIndex + 1) % queueCount;
387*e01b6f76SAndroid Build Coastguard Worker }
388*e01b6f76SAndroid Build Coastguard Worker if (eventsRead == 0) {
389*e01b6f76SAndroid Build Coastguard Worker // The queues have been scanned and none contain data, so wait.
390*e01b6f76SAndroid Build Coastguard Worker ALOGV("poll stopping to wait for data");
391*e01b6f76SAndroid Build Coastguard Worker waiting_for_data = true;
392*e01b6f76SAndroid Build Coastguard Worker pthread_cond_wait(&data_available_cond, &queue_mutex);
393*e01b6f76SAndroid Build Coastguard Worker waiting_for_data = false;
394*e01b6f76SAndroid Build Coastguard Worker empties = 0;
395*e01b6f76SAndroid Build Coastguard Worker }
396*e01b6f76SAndroid Build Coastguard Worker }
397*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&queue_mutex);
398*e01b6f76SAndroid Build Coastguard Worker ALOGV("poll returning %d events.", eventsRead);
399*e01b6f76SAndroid Build Coastguard Worker
400*e01b6f76SAndroid Build Coastguard Worker return eventsRead;
401*e01b6f76SAndroid Build Coastguard Worker }
402*e01b6f76SAndroid Build Coastguard Worker
batch(int handle,int flags,int64_t period_ns,int64_t timeout)403*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) {
404*e01b6f76SAndroid Build Coastguard Worker ALOGV("batch");
405*e01b6f76SAndroid Build Coastguard Worker int retval = -EINVAL;
406*e01b6f76SAndroid Build Coastguard Worker int local_handle = get_local_handle(handle);
407*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
408*e01b6f76SAndroid Build Coastguard Worker if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
409*e01b6f76SAndroid Build Coastguard Worker retval = v1->batch(v1, local_handle, flags, period_ns, timeout);
410*e01b6f76SAndroid Build Coastguard Worker } else {
411*e01b6f76SAndroid Build Coastguard Worker ALOGE("IGNORING batch() call to non-API-compliant sensor handle=%d !", handle);
412*e01b6f76SAndroid Build Coastguard Worker }
413*e01b6f76SAndroid Build Coastguard Worker ALOGV("retval %d", retval);
414*e01b6f76SAndroid Build Coastguard Worker return retval;
415*e01b6f76SAndroid Build Coastguard Worker }
416*e01b6f76SAndroid Build Coastguard Worker
flush(int handle)417*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::flush(int handle) {
418*e01b6f76SAndroid Build Coastguard Worker ALOGV("flush");
419*e01b6f76SAndroid Build Coastguard Worker int retval = -EINVAL;
420*e01b6f76SAndroid Build Coastguard Worker int local_handle = get_local_handle(handle);
421*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
422*e01b6f76SAndroid Build Coastguard Worker if (halIsCompliant(this, handle) && local_handle >= 0 && v1) {
423*e01b6f76SAndroid Build Coastguard Worker retval = v1->flush(v1, local_handle);
424*e01b6f76SAndroid Build Coastguard Worker } else {
425*e01b6f76SAndroid Build Coastguard Worker ALOGE("IGNORING flush() call to non-API-compliant sensor handle=%d !", handle);
426*e01b6f76SAndroid Build Coastguard Worker }
427*e01b6f76SAndroid Build Coastguard Worker ALOGV("retval %d", retval);
428*e01b6f76SAndroid Build Coastguard Worker return retval;
429*e01b6f76SAndroid Build Coastguard Worker }
430*e01b6f76SAndroid Build Coastguard Worker
inject_sensor_data(const sensors_event_t * data)431*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::inject_sensor_data(const sensors_event_t *data) {
432*e01b6f76SAndroid Build Coastguard Worker int retval = -EINVAL;
433*e01b6f76SAndroid Build Coastguard Worker ALOGV("inject_sensor_data");
434*e01b6f76SAndroid Build Coastguard Worker if (data->sensor == -1) {
435*e01b6f76SAndroid Build Coastguard Worker // operational parameter
436*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* v1 = get_primary_v1_device();
437*e01b6f76SAndroid Build Coastguard Worker if (v1 && v1->common.version >= SENSORS_DEVICE_API_VERSION_1_4) {
438*e01b6f76SAndroid Build Coastguard Worker retval = v1->inject_sensor_data(v1, data);
439*e01b6f76SAndroid Build Coastguard Worker } else {
440*e01b6f76SAndroid Build Coastguard Worker ALOGE("IGNORED inject_sensor_data(operational param) call to non-API-compliant sensor");
441*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
442*e01b6f76SAndroid Build Coastguard Worker }
443*e01b6f76SAndroid Build Coastguard Worker } else {
444*e01b6f76SAndroid Build Coastguard Worker // Get handle for the sensor owning the event being injected
445*e01b6f76SAndroid Build Coastguard Worker int local_handle = get_local_handle(data->sensor);
446*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(data->sensor);
447*e01b6f76SAndroid Build Coastguard Worker if (halIsAPILevelCompliant(this, data->sensor, SENSORS_DEVICE_API_VERSION_1_4) &&
448*e01b6f76SAndroid Build Coastguard Worker local_handle >= 0 && v1) {
449*e01b6f76SAndroid Build Coastguard Worker // if specific sensor is used, we have to replace global sensor handle
450*e01b6f76SAndroid Build Coastguard Worker // with local one, before passing to concrete HAL
451*e01b6f76SAndroid Build Coastguard Worker sensors_event_t data_copy = *data;
452*e01b6f76SAndroid Build Coastguard Worker data_copy.sensor = local_handle;
453*e01b6f76SAndroid Build Coastguard Worker retval = v1->inject_sensor_data(v1, &data_copy);
454*e01b6f76SAndroid Build Coastguard Worker } else {
455*e01b6f76SAndroid Build Coastguard Worker ALOGE("IGNORED inject_sensor_data(type=%d, handle=%d) call to non-API-compliant sensor",
456*e01b6f76SAndroid Build Coastguard Worker data->type, data->sensor);
457*e01b6f76SAndroid Build Coastguard Worker retval = -ENOSYS;
458*e01b6f76SAndroid Build Coastguard Worker }
459*e01b6f76SAndroid Build Coastguard Worker }
460*e01b6f76SAndroid Build Coastguard Worker ALOGV("retval %d", retval);
461*e01b6f76SAndroid Build Coastguard Worker return retval;
462*e01b6f76SAndroid Build Coastguard Worker }
463*e01b6f76SAndroid Build Coastguard Worker
register_direct_channel(const struct sensors_direct_mem_t * mem,int channel_handle)464*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::register_direct_channel(const struct sensors_direct_mem_t* mem,
465*e01b6f76SAndroid Build Coastguard Worker int channel_handle) {
466*e01b6f76SAndroid Build Coastguard Worker int retval = -EINVAL;
467*e01b6f76SAndroid Build Coastguard Worker ALOGV("register_direct_channel");
468*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* v1 = get_primary_v1_device();
469*e01b6f76SAndroid Build Coastguard Worker if (v1 && halSupportDirectSensorReport(v1)) {
470*e01b6f76SAndroid Build Coastguard Worker retval = v1->register_direct_channel(v1, mem, channel_handle);
471*e01b6f76SAndroid Build Coastguard Worker } else {
472*e01b6f76SAndroid Build Coastguard Worker ALOGE("IGNORED register_direct_channel(mem=%p, handle=%d) call to non-API-compliant sensor",
473*e01b6f76SAndroid Build Coastguard Worker mem, channel_handle);
474*e01b6f76SAndroid Build Coastguard Worker retval = -ENOSYS;
475*e01b6f76SAndroid Build Coastguard Worker }
476*e01b6f76SAndroid Build Coastguard Worker ALOGV("retval %d", retval);
477*e01b6f76SAndroid Build Coastguard Worker return retval;
478*e01b6f76SAndroid Build Coastguard Worker }
479*e01b6f76SAndroid Build Coastguard Worker
config_direct_report(int sensor_handle,int channel_handle,const struct sensors_direct_cfg_t * config)480*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::config_direct_report(int sensor_handle,
481*e01b6f76SAndroid Build Coastguard Worker int channel_handle,
482*e01b6f76SAndroid Build Coastguard Worker const struct sensors_direct_cfg_t *config) {
483*e01b6f76SAndroid Build Coastguard Worker int retval = -EINVAL;
484*e01b6f76SAndroid Build Coastguard Worker ALOGV("config_direct_report");
485*e01b6f76SAndroid Build Coastguard Worker
486*e01b6f76SAndroid Build Coastguard Worker if (config != nullptr) {
487*e01b6f76SAndroid Build Coastguard Worker int local_handle = get_local_handle(sensor_handle);
488*e01b6f76SAndroid Build Coastguard Worker sensors_poll_device_1_t* v1 = get_primary_v1_device();
489*e01b6f76SAndroid Build Coastguard Worker if (v1 && halSupportDirectSensorReport(v1)) {
490*e01b6f76SAndroid Build Coastguard Worker retval = v1->config_direct_report(v1, local_handle, channel_handle, config);
491*e01b6f76SAndroid Build Coastguard Worker } else {
492*e01b6f76SAndroid Build Coastguard Worker ALOGE("IGNORED config_direct_report(sensor=%d, channel=%d, rate_level=%d) call to "
493*e01b6f76SAndroid Build Coastguard Worker "non-API-compliant sensor", sensor_handle, channel_handle, config->rate_level);
494*e01b6f76SAndroid Build Coastguard Worker retval = -ENOSYS;
495*e01b6f76SAndroid Build Coastguard Worker }
496*e01b6f76SAndroid Build Coastguard Worker }
497*e01b6f76SAndroid Build Coastguard Worker ALOGV("retval %d", retval);
498*e01b6f76SAndroid Build Coastguard Worker return retval;
499*e01b6f76SAndroid Build Coastguard Worker }
close()500*e01b6f76SAndroid Build Coastguard Worker int sensors_poll_context_t::close() {
501*e01b6f76SAndroid Build Coastguard Worker ALOGV("close");
502*e01b6f76SAndroid Build Coastguard Worker for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
503*e01b6f76SAndroid Build Coastguard Worker it != this->sub_hw_devices.end(); it++) {
504*e01b6f76SAndroid Build Coastguard Worker hw_device_t* dev = *it;
505*e01b6f76SAndroid Build Coastguard Worker int retval = dev->close(dev);
506*e01b6f76SAndroid Build Coastguard Worker ALOGV("retval %d", retval);
507*e01b6f76SAndroid Build Coastguard Worker }
508*e01b6f76SAndroid Build Coastguard Worker return 0;
509*e01b6f76SAndroid Build Coastguard Worker }
510*e01b6f76SAndroid Build Coastguard Worker
511*e01b6f76SAndroid Build Coastguard Worker
device__close(struct hw_device_t * dev)512*e01b6f76SAndroid Build Coastguard Worker static int device__close(struct hw_device_t *dev) {
513*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_lock(&init_modules_mutex);
514*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
515*e01b6f76SAndroid Build Coastguard Worker if (ctx != NULL) {
516*e01b6f76SAndroid Build Coastguard Worker int retval = ctx->close();
517*e01b6f76SAndroid Build Coastguard Worker delete ctx;
518*e01b6f76SAndroid Build Coastguard Worker return retval;
519*e01b6f76SAndroid Build Coastguard Worker }
520*e01b6f76SAndroid Build Coastguard Worker
521*e01b6f76SAndroid Build Coastguard Worker if (sub_hw_modules != nullptr) {
522*e01b6f76SAndroid Build Coastguard Worker delete sub_hw_modules;
523*e01b6f76SAndroid Build Coastguard Worker sub_hw_modules = nullptr;
524*e01b6f76SAndroid Build Coastguard Worker }
525*e01b6f76SAndroid Build Coastguard Worker
526*e01b6f76SAndroid Build Coastguard Worker if (so_handles != nullptr) {
527*e01b6f76SAndroid Build Coastguard Worker for (auto handle : *so_handles) {
528*e01b6f76SAndroid Build Coastguard Worker dlclose(handle);
529*e01b6f76SAndroid Build Coastguard Worker }
530*e01b6f76SAndroid Build Coastguard Worker delete so_handles;
531*e01b6f76SAndroid Build Coastguard Worker so_handles = nullptr;
532*e01b6f76SAndroid Build Coastguard Worker }
533*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&init_modules_mutex);
534*e01b6f76SAndroid Build Coastguard Worker return 0;
535*e01b6f76SAndroid Build Coastguard Worker }
536*e01b6f76SAndroid Build Coastguard Worker
device__activate(struct sensors_poll_device_t * dev,int handle,int enabled)537*e01b6f76SAndroid Build Coastguard Worker static int device__activate(struct sensors_poll_device_t *dev, int handle,
538*e01b6f76SAndroid Build Coastguard Worker int enabled) {
539*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
540*e01b6f76SAndroid Build Coastguard Worker return ctx->activate(handle, enabled);
541*e01b6f76SAndroid Build Coastguard Worker }
542*e01b6f76SAndroid Build Coastguard Worker
device__setDelay(struct sensors_poll_device_t * dev,int handle,int64_t ns)543*e01b6f76SAndroid Build Coastguard Worker static int device__setDelay(struct sensors_poll_device_t *dev, int handle,
544*e01b6f76SAndroid Build Coastguard Worker int64_t ns) {
545*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
546*e01b6f76SAndroid Build Coastguard Worker return ctx->setDelay(handle, ns);
547*e01b6f76SAndroid Build Coastguard Worker }
548*e01b6f76SAndroid Build Coastguard Worker
device__poll(struct sensors_poll_device_t * dev,sensors_event_t * data,int count)549*e01b6f76SAndroid Build Coastguard Worker static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data,
550*e01b6f76SAndroid Build Coastguard Worker int count) {
551*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
552*e01b6f76SAndroid Build Coastguard Worker return ctx->poll(data, count);
553*e01b6f76SAndroid Build Coastguard Worker }
554*e01b6f76SAndroid Build Coastguard Worker
device__batch(struct sensors_poll_device_1 * dev,int handle,int flags,int64_t period_ns,int64_t timeout)555*e01b6f76SAndroid Build Coastguard Worker static int device__batch(struct sensors_poll_device_1 *dev, int handle,
556*e01b6f76SAndroid Build Coastguard Worker int flags, int64_t period_ns, int64_t timeout) {
557*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
558*e01b6f76SAndroid Build Coastguard Worker return ctx->batch(handle, flags, period_ns, timeout);
559*e01b6f76SAndroid Build Coastguard Worker }
560*e01b6f76SAndroid Build Coastguard Worker
device__flush(struct sensors_poll_device_1 * dev,int handle)561*e01b6f76SAndroid Build Coastguard Worker static int device__flush(struct sensors_poll_device_1 *dev, int handle) {
562*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
563*e01b6f76SAndroid Build Coastguard Worker return ctx->flush(handle);
564*e01b6f76SAndroid Build Coastguard Worker }
565*e01b6f76SAndroid Build Coastguard Worker
device__inject_sensor_data(struct sensors_poll_device_1 * dev,const sensors_event_t * data)566*e01b6f76SAndroid Build Coastguard Worker static int device__inject_sensor_data(struct sensors_poll_device_1 *dev,
567*e01b6f76SAndroid Build Coastguard Worker const sensors_event_t *data) {
568*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
569*e01b6f76SAndroid Build Coastguard Worker return ctx->inject_sensor_data(data);
570*e01b6f76SAndroid Build Coastguard Worker }
571*e01b6f76SAndroid Build Coastguard Worker
device__register_direct_channel(struct sensors_poll_device_1 * dev,const struct sensors_direct_mem_t * mem,int channel_handle)572*e01b6f76SAndroid Build Coastguard Worker static int device__register_direct_channel(struct sensors_poll_device_1 *dev,
573*e01b6f76SAndroid Build Coastguard Worker const struct sensors_direct_mem_t* mem,
574*e01b6f76SAndroid Build Coastguard Worker int channel_handle) {
575*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
576*e01b6f76SAndroid Build Coastguard Worker return ctx->register_direct_channel(mem, channel_handle);
577*e01b6f76SAndroid Build Coastguard Worker }
578*e01b6f76SAndroid Build Coastguard Worker
device__config_direct_report(struct sensors_poll_device_1 * dev,int sensor_handle,int channel_handle,const struct sensors_direct_cfg_t * config)579*e01b6f76SAndroid Build Coastguard Worker static int device__config_direct_report(struct sensors_poll_device_1 *dev,
580*e01b6f76SAndroid Build Coastguard Worker int sensor_handle,
581*e01b6f76SAndroid Build Coastguard Worker int channel_handle,
582*e01b6f76SAndroid Build Coastguard Worker const struct sensors_direct_cfg_t *config) {
583*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
584*e01b6f76SAndroid Build Coastguard Worker return ctx->config_direct_report(sensor_handle, channel_handle, config);
585*e01b6f76SAndroid Build Coastguard Worker }
586*e01b6f76SAndroid Build Coastguard Worker
587*e01b6f76SAndroid Build Coastguard Worker static int open_sensors(const struct hw_module_t* module, const char* name,
588*e01b6f76SAndroid Build Coastguard Worker struct hw_device_t** device);
589*e01b6f76SAndroid Build Coastguard Worker
590*e01b6f76SAndroid Build Coastguard Worker /*
591*e01b6f76SAndroid Build Coastguard Worker * Adds valid paths from the config file to the vector passed in.
592*e01b6f76SAndroid Build Coastguard Worker * The vector must not be null.
593*e01b6f76SAndroid Build Coastguard Worker */
get_so_paths()594*e01b6f76SAndroid Build Coastguard Worker static std::vector<std::string> get_so_paths() {
595*e01b6f76SAndroid Build Coastguard Worker std::vector<std::string> so_paths;
596*e01b6f76SAndroid Build Coastguard Worker
597*e01b6f76SAndroid Build Coastguard Worker const std::vector<const char *> config_path_list(
598*e01b6f76SAndroid Build Coastguard Worker { MULTI_HAL_CONFIG_FILE_PATH, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH });
599*e01b6f76SAndroid Build Coastguard Worker
600*e01b6f76SAndroid Build Coastguard Worker std::ifstream stream;
601*e01b6f76SAndroid Build Coastguard Worker const char *path = nullptr;
602*e01b6f76SAndroid Build Coastguard Worker for (auto i : config_path_list) {
603*e01b6f76SAndroid Build Coastguard Worker std::ifstream f(i);
604*e01b6f76SAndroid Build Coastguard Worker if (f) {
605*e01b6f76SAndroid Build Coastguard Worker stream = std::move(f);
606*e01b6f76SAndroid Build Coastguard Worker path = i;
607*e01b6f76SAndroid Build Coastguard Worker break;
608*e01b6f76SAndroid Build Coastguard Worker }
609*e01b6f76SAndroid Build Coastguard Worker }
610*e01b6f76SAndroid Build Coastguard Worker if(!stream) {
611*e01b6f76SAndroid Build Coastguard Worker ALOGW("No multihal config file found");
612*e01b6f76SAndroid Build Coastguard Worker return so_paths;
613*e01b6f76SAndroid Build Coastguard Worker }
614*e01b6f76SAndroid Build Coastguard Worker
615*e01b6f76SAndroid Build Coastguard Worker ALOGE_IF(strcmp(path, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH) == 0,
616*e01b6f76SAndroid Build Coastguard Worker "Multihal configuration file path %s is not compatible with Treble "
617*e01b6f76SAndroid Build Coastguard Worker "requirements. Please move it to %s.",
618*e01b6f76SAndroid Build Coastguard Worker path, MULTI_HAL_CONFIG_FILE_PATH);
619*e01b6f76SAndroid Build Coastguard Worker
620*e01b6f76SAndroid Build Coastguard Worker ALOGV("Multihal config file found at %s", path);
621*e01b6f76SAndroid Build Coastguard Worker std::string line;
622*e01b6f76SAndroid Build Coastguard Worker while (std::getline(stream, line)) {
623*e01b6f76SAndroid Build Coastguard Worker ALOGV("config file line: '%s'", line.c_str());
624*e01b6f76SAndroid Build Coastguard Worker so_paths.push_back(line);
625*e01b6f76SAndroid Build Coastguard Worker }
626*e01b6f76SAndroid Build Coastguard Worker return so_paths;
627*e01b6f76SAndroid Build Coastguard Worker }
628*e01b6f76SAndroid Build Coastguard Worker
629*e01b6f76SAndroid Build Coastguard Worker /*
630*e01b6f76SAndroid Build Coastguard Worker * Ensures that the sub-module array is initialized.
631*e01b6f76SAndroid Build Coastguard Worker * This can be first called from get_sensors_list or from open_sensors.
632*e01b6f76SAndroid Build Coastguard Worker */
lazy_init_modules()633*e01b6f76SAndroid Build Coastguard Worker static void lazy_init_modules() {
634*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_lock(&init_modules_mutex);
635*e01b6f76SAndroid Build Coastguard Worker if (sub_hw_modules != NULL) {
636*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&init_modules_mutex);
637*e01b6f76SAndroid Build Coastguard Worker return;
638*e01b6f76SAndroid Build Coastguard Worker }
639*e01b6f76SAndroid Build Coastguard Worker std::vector<std::string> so_paths(get_so_paths());
640*e01b6f76SAndroid Build Coastguard Worker
641*e01b6f76SAndroid Build Coastguard Worker // dlopen the module files and cache their module symbols in sub_hw_modules
642*e01b6f76SAndroid Build Coastguard Worker sub_hw_modules = new std::vector<hw_module_t *>();
643*e01b6f76SAndroid Build Coastguard Worker so_handles = new std::vector<void *>();
644*e01b6f76SAndroid Build Coastguard Worker dlerror(); // clear any old errors
645*e01b6f76SAndroid Build Coastguard Worker const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
646*e01b6f76SAndroid Build Coastguard Worker for (const auto &s : so_paths) {
647*e01b6f76SAndroid Build Coastguard Worker const char* path = s.c_str();
648*e01b6f76SAndroid Build Coastguard Worker void* lib_handle = dlopen(path, RTLD_LAZY);
649*e01b6f76SAndroid Build Coastguard Worker if (lib_handle == NULL) {
650*e01b6f76SAndroid Build Coastguard Worker ALOGW("dlerror(): %s", dlerror());
651*e01b6f76SAndroid Build Coastguard Worker } else {
652*e01b6f76SAndroid Build Coastguard Worker ALOGI("Loaded library from %s", path);
653*e01b6f76SAndroid Build Coastguard Worker ALOGV("Opening symbol \"%s\"", sym);
654*e01b6f76SAndroid Build Coastguard Worker // clear old errors
655*e01b6f76SAndroid Build Coastguard Worker dlerror();
656*e01b6f76SAndroid Build Coastguard Worker struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym);
657*e01b6f76SAndroid Build Coastguard Worker const char* error;
658*e01b6f76SAndroid Build Coastguard Worker if ((error = dlerror()) != NULL) {
659*e01b6f76SAndroid Build Coastguard Worker ALOGW("Error calling dlsym: %s", error);
660*e01b6f76SAndroid Build Coastguard Worker } else if (module == NULL) {
661*e01b6f76SAndroid Build Coastguard Worker ALOGW("module == NULL");
662*e01b6f76SAndroid Build Coastguard Worker } else {
663*e01b6f76SAndroid Build Coastguard Worker ALOGV("Loaded symbols from \"%s\"", sym);
664*e01b6f76SAndroid Build Coastguard Worker sub_hw_modules->push_back(module);
665*e01b6f76SAndroid Build Coastguard Worker so_handles->push_back(lib_handle);
666*e01b6f76SAndroid Build Coastguard Worker lib_handle = nullptr;
667*e01b6f76SAndroid Build Coastguard Worker }
668*e01b6f76SAndroid Build Coastguard Worker }
669*e01b6f76SAndroid Build Coastguard Worker if (lib_handle != nullptr) {
670*e01b6f76SAndroid Build Coastguard Worker dlclose(lib_handle);
671*e01b6f76SAndroid Build Coastguard Worker }
672*e01b6f76SAndroid Build Coastguard Worker }
673*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&init_modules_mutex);
674*e01b6f76SAndroid Build Coastguard Worker }
675*e01b6f76SAndroid Build Coastguard Worker
676*e01b6f76SAndroid Build Coastguard Worker /*
677*e01b6f76SAndroid Build Coastguard Worker * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles.
678*e01b6f76SAndroid Build Coastguard Worker */
lazy_init_sensors_list()679*e01b6f76SAndroid Build Coastguard Worker static void lazy_init_sensors_list() {
680*e01b6f76SAndroid Build Coastguard Worker ALOGV("lazy_init_sensors_list");
681*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_lock(&init_sensors_mutex);
682*e01b6f76SAndroid Build Coastguard Worker if (global_sensors_list != NULL) {
683*e01b6f76SAndroid Build Coastguard Worker // already initialized
684*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&init_sensors_mutex);
685*e01b6f76SAndroid Build Coastguard Worker ALOGV("lazy_init_sensors_list - early return");
686*e01b6f76SAndroid Build Coastguard Worker return;
687*e01b6f76SAndroid Build Coastguard Worker }
688*e01b6f76SAndroid Build Coastguard Worker
689*e01b6f76SAndroid Build Coastguard Worker ALOGV("lazy_init_sensors_list needs to do work");
690*e01b6f76SAndroid Build Coastguard Worker lazy_init_modules();
691*e01b6f76SAndroid Build Coastguard Worker
692*e01b6f76SAndroid Build Coastguard Worker // Count all the sensors, then allocate an array of blanks.
693*e01b6f76SAndroid Build Coastguard Worker global_sensors_count = 0;
694*e01b6f76SAndroid Build Coastguard Worker const struct sensor_t *subhal_sensors_list;
695*e01b6f76SAndroid Build Coastguard Worker for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
696*e01b6f76SAndroid Build Coastguard Worker it != sub_hw_modules->end(); it++) {
697*e01b6f76SAndroid Build Coastguard Worker struct sensors_module_t *module = (struct sensors_module_t*) *it;
698*e01b6f76SAndroid Build Coastguard Worker global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list);
699*e01b6f76SAndroid Build Coastguard Worker ALOGV("increased global_sensors_count to %d", global_sensors_count);
700*e01b6f76SAndroid Build Coastguard Worker }
701*e01b6f76SAndroid Build Coastguard Worker
702*e01b6f76SAndroid Build Coastguard Worker // The global_sensors_list is full of consts.
703*e01b6f76SAndroid Build Coastguard Worker // Manipulate this non-const list, and point the const one to it when we're done.
704*e01b6f76SAndroid Build Coastguard Worker sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count];
705*e01b6f76SAndroid Build Coastguard Worker
706*e01b6f76SAndroid Build Coastguard Worker // index of the next sensor to set in mutable_sensor_list
707*e01b6f76SAndroid Build Coastguard Worker int mutable_sensor_index = 0;
708*e01b6f76SAndroid Build Coastguard Worker int module_index = 0;
709*e01b6f76SAndroid Build Coastguard Worker
710*e01b6f76SAndroid Build Coastguard Worker for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
711*e01b6f76SAndroid Build Coastguard Worker it != sub_hw_modules->end(); it++) {
712*e01b6f76SAndroid Build Coastguard Worker hw_module_t *hw_module = *it;
713*e01b6f76SAndroid Build Coastguard Worker ALOGV("examine one module");
714*e01b6f76SAndroid Build Coastguard Worker // Read the sub-module's sensor list.
715*e01b6f76SAndroid Build Coastguard Worker struct sensors_module_t *module = (struct sensors_module_t*) hw_module;
716*e01b6f76SAndroid Build Coastguard Worker int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list);
717*e01b6f76SAndroid Build Coastguard Worker ALOGV("the module has %d sensors", module_sensor_count);
718*e01b6f76SAndroid Build Coastguard Worker
719*e01b6f76SAndroid Build Coastguard Worker // Copy the HAL's sensor list into global_sensors_list,
720*e01b6f76SAndroid Build Coastguard Worker // with the handle changed to be a global handle.
721*e01b6f76SAndroid Build Coastguard Worker for (int i = 0; i < module_sensor_count; i++) {
722*e01b6f76SAndroid Build Coastguard Worker ALOGV("examining one sensor");
723*e01b6f76SAndroid Build Coastguard Worker const struct sensor_t *local_sensor = &subhal_sensors_list[i];
724*e01b6f76SAndroid Build Coastguard Worker int local_handle = local_sensor->handle;
725*e01b6f76SAndroid Build Coastguard Worker memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
726*e01b6f76SAndroid Build Coastguard Worker sizeof(struct sensor_t));
727*e01b6f76SAndroid Build Coastguard Worker
728*e01b6f76SAndroid Build Coastguard Worker // sensor direct report is only for primary module
729*e01b6f76SAndroid Build Coastguard Worker if (module_index != 0) {
730*e01b6f76SAndroid Build Coastguard Worker mutable_sensor_list[mutable_sensor_index].flags &=
731*e01b6f76SAndroid Build Coastguard Worker ~(SENSOR_FLAG_MASK_DIRECT_REPORT | SENSOR_FLAG_MASK_DIRECT_CHANNEL);
732*e01b6f76SAndroid Build Coastguard Worker }
733*e01b6f76SAndroid Build Coastguard Worker
734*e01b6f76SAndroid Build Coastguard Worker // Overwrite the global version's handle with a global handle.
735*e01b6f76SAndroid Build Coastguard Worker int global_handle = assign_global_handle(module_index, local_handle);
736*e01b6f76SAndroid Build Coastguard Worker
737*e01b6f76SAndroid Build Coastguard Worker mutable_sensor_list[mutable_sensor_index].handle = global_handle;
738*e01b6f76SAndroid Build Coastguard Worker ALOGV("module_index %d, local_handle %d, global_handle %d",
739*e01b6f76SAndroid Build Coastguard Worker module_index, local_handle, global_handle);
740*e01b6f76SAndroid Build Coastguard Worker
741*e01b6f76SAndroid Build Coastguard Worker mutable_sensor_index++;
742*e01b6f76SAndroid Build Coastguard Worker }
743*e01b6f76SAndroid Build Coastguard Worker module_index++;
744*e01b6f76SAndroid Build Coastguard Worker }
745*e01b6f76SAndroid Build Coastguard Worker // Set the const static global_sensors_list to the mutable one allocated by this function.
746*e01b6f76SAndroid Build Coastguard Worker global_sensors_list = mutable_sensor_list;
747*e01b6f76SAndroid Build Coastguard Worker
748*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&init_sensors_mutex);
749*e01b6f76SAndroid Build Coastguard Worker ALOGV("end lazy_init_sensors_list");
750*e01b6f76SAndroid Build Coastguard Worker }
751*e01b6f76SAndroid Build Coastguard Worker
module__get_sensors_list(__unused struct sensors_module_t * module,struct sensor_t const ** list)752*e01b6f76SAndroid Build Coastguard Worker static int module__get_sensors_list(__unused struct sensors_module_t* module,
753*e01b6f76SAndroid Build Coastguard Worker struct sensor_t const** list) {
754*e01b6f76SAndroid Build Coastguard Worker ALOGV("module__get_sensors_list start");
755*e01b6f76SAndroid Build Coastguard Worker lazy_init_sensors_list();
756*e01b6f76SAndroid Build Coastguard Worker *list = global_sensors_list;
757*e01b6f76SAndroid Build Coastguard Worker ALOGV("global_sensors_count: %d", global_sensors_count);
758*e01b6f76SAndroid Build Coastguard Worker for (int i = 0; i < global_sensors_count; i++) {
759*e01b6f76SAndroid Build Coastguard Worker ALOGV("sensor type: %d", global_sensors_list[i].type);
760*e01b6f76SAndroid Build Coastguard Worker }
761*e01b6f76SAndroid Build Coastguard Worker return global_sensors_count;
762*e01b6f76SAndroid Build Coastguard Worker }
763*e01b6f76SAndroid Build Coastguard Worker
764*e01b6f76SAndroid Build Coastguard Worker static struct hw_module_methods_t sensors_module_methods = {
765*e01b6f76SAndroid Build Coastguard Worker .open = open_sensors
766*e01b6f76SAndroid Build Coastguard Worker };
767*e01b6f76SAndroid Build Coastguard Worker
768*e01b6f76SAndroid Build Coastguard Worker struct sensors_module_t HAL_MODULE_INFO_SYM = {
769*e01b6f76SAndroid Build Coastguard Worker .common = {
770*e01b6f76SAndroid Build Coastguard Worker .tag = HARDWARE_MODULE_TAG,
771*e01b6f76SAndroid Build Coastguard Worker .version_major = 1,
772*e01b6f76SAndroid Build Coastguard Worker .version_minor = 1,
773*e01b6f76SAndroid Build Coastguard Worker .id = SENSORS_HARDWARE_MODULE_ID,
774*e01b6f76SAndroid Build Coastguard Worker .name = "MultiHal Sensor Module",
775*e01b6f76SAndroid Build Coastguard Worker .author = "Google, Inc",
776*e01b6f76SAndroid Build Coastguard Worker .methods = &sensors_module_methods,
777*e01b6f76SAndroid Build Coastguard Worker .dso = NULL,
778*e01b6f76SAndroid Build Coastguard Worker .reserved = {0},
779*e01b6f76SAndroid Build Coastguard Worker },
780*e01b6f76SAndroid Build Coastguard Worker .get_sensors_list = module__get_sensors_list
781*e01b6f76SAndroid Build Coastguard Worker };
782*e01b6f76SAndroid Build Coastguard Worker
get_multi_hal_module_info()783*e01b6f76SAndroid Build Coastguard Worker struct sensors_module_t *get_multi_hal_module_info() {
784*e01b6f76SAndroid Build Coastguard Worker return (&HAL_MODULE_INFO_SYM);
785*e01b6f76SAndroid Build Coastguard Worker }
786*e01b6f76SAndroid Build Coastguard Worker
open_sensors(const struct hw_module_t * hw_module,const char * name,struct hw_device_t ** hw_device_out)787*e01b6f76SAndroid Build Coastguard Worker static int open_sensors(const struct hw_module_t* hw_module, const char* name,
788*e01b6f76SAndroid Build Coastguard Worker struct hw_device_t** hw_device_out) {
789*e01b6f76SAndroid Build Coastguard Worker ALOGV("open_sensors begin...");
790*e01b6f76SAndroid Build Coastguard Worker
791*e01b6f76SAndroid Build Coastguard Worker lazy_init_modules();
792*e01b6f76SAndroid Build Coastguard Worker
793*e01b6f76SAndroid Build Coastguard Worker // Create proxy device, to return later.
794*e01b6f76SAndroid Build Coastguard Worker sensors_poll_context_t *dev = new sensors_poll_context_t();
795*e01b6f76SAndroid Build Coastguard Worker memset(dev, 0, sizeof(sensors_poll_device_1_t));
796*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
797*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_4;
798*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
799*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.common.close = device__close;
800*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.activate = device__activate;
801*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.setDelay = device__setDelay;
802*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.poll = device__poll;
803*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.batch = device__batch;
804*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.flush = device__flush;
805*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
806*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.register_direct_channel = device__register_direct_channel;
807*e01b6f76SAndroid Build Coastguard Worker dev->proxy_device.config_direct_report = device__config_direct_report;
808*e01b6f76SAndroid Build Coastguard Worker
809*e01b6f76SAndroid Build Coastguard Worker dev->nextReadIndex = 0;
810*e01b6f76SAndroid Build Coastguard Worker
811*e01b6f76SAndroid Build Coastguard Worker // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
812*e01b6f76SAndroid Build Coastguard Worker for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
813*e01b6f76SAndroid Build Coastguard Worker it != sub_hw_modules->end(); it++) {
814*e01b6f76SAndroid Build Coastguard Worker sensors_module_t *sensors_module = (sensors_module_t*) *it;
815*e01b6f76SAndroid Build Coastguard Worker struct hw_device_t* sub_hw_device;
816*e01b6f76SAndroid Build Coastguard Worker int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
817*e01b6f76SAndroid Build Coastguard Worker if (!sub_open_result) {
818*e01b6f76SAndroid Build Coastguard Worker if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) {
819*e01b6f76SAndroid Build Coastguard Worker ALOGE("SENSORS_DEVICE_API_VERSION_1_3 or newer is required for all sensor HALs");
820*e01b6f76SAndroid Build Coastguard Worker ALOGE("This HAL reports non-compliant API level : %s",
821*e01b6f76SAndroid Build Coastguard Worker apiNumToStr(sub_hw_device->version));
822*e01b6f76SAndroid Build Coastguard Worker ALOGE("Sensors belonging to this HAL will get ignored !");
823*e01b6f76SAndroid Build Coastguard Worker }
824*e01b6f76SAndroid Build Coastguard Worker dev->addSubHwDevice(sub_hw_device);
825*e01b6f76SAndroid Build Coastguard Worker }
826*e01b6f76SAndroid Build Coastguard Worker }
827*e01b6f76SAndroid Build Coastguard Worker
828*e01b6f76SAndroid Build Coastguard Worker // Prepare the output param and return
829*e01b6f76SAndroid Build Coastguard Worker *hw_device_out = &dev->proxy_device.common;
830*e01b6f76SAndroid Build Coastguard Worker ALOGV("...open_sensors end");
831*e01b6f76SAndroid Build Coastguard Worker return 0;
832*e01b6f76SAndroid Build Coastguard Worker }
833