ASensorManager.cpp revision 6f96cb345a8ebfd9fef5f32931373776a798a193
1/*
2 * Copyright (C) 2017 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
17#include "ALooper.h"
18#include "ASensorEventQueue.h"
19#include "ASensorManager.h"
20
21#define LOG_TAG "libsensorndkbridge"
22#include <android-base/logging.h>
23#include <android/looper.h>
24#include <sensors/convert.h>
25
26using android::hardware::sensors::V1_0::SensorInfo;
27using android::frameworks::sensorservice::V1_0::IEventQueue;
28using android::frameworks::sensorservice::V1_0::ISensorManager;
29using android::frameworks::sensorservice::V1_0::Result;
30using android::hardware::sensors::V1_0::SensorType;
31using android::sp;
32using android::Mutex;
33using android::status_t;
34using android::OK;
35using android::NO_INIT;
36using android::BAD_VALUE;
37using android::hardware::hidl_vec;
38using android::hardware::Return;
39
40static Mutex gLock;
41
42// static
43ASensorManager *ASensorManager::sInstance = NULL;
44
45// static
46ASensorManager *ASensorManager::getInstance() {
47    Mutex::Autolock autoLock(gLock);
48    if (sInstance == NULL) {
49        sInstance = new ASensorManager;
50        if (sInstance->initCheck() != OK) {
51            delete sInstance;
52            sInstance = NULL;
53        }
54    }
55    return sInstance;
56}
57
58ASensorManager::ASensorManager()
59    : mInitCheck(NO_INIT) {
60    mManager = ISensorManager::getService();
61    if (mManager != NULL) {
62        mInitCheck = OK;
63    }
64}
65
66status_t ASensorManager::initCheck() const {
67    return mInitCheck;
68}
69
70int ASensorManager::getSensorList(ASensorList *out) {
71    LOG(VERBOSE) << "ASensorManager::getSensorList";
72
73    Mutex::Autolock autoLock(mLock);
74
75    if (mSensorList == NULL) {
76        Return<void> ret =
77            mManager->getSensorList([&](const auto &list, auto result) {
78                if (result != Result::OK) {
79                    return;
80                }
81
82                mSensors = list;
83        });
84
85        (void)ret.isOk();
86
87        mSensorList.reset(new ASensorRef[mSensors.size()]);
88        for (size_t i = 0; i < mSensors.size(); ++i) {
89            mSensorList.get()[i] =
90                reinterpret_cast<ASensorRef>(&mSensors[i]);
91        }
92    }
93
94    if (out) {
95        *out = reinterpret_cast<ASensorList>(mSensorList.get());
96    }
97
98    return mSensors.size();
99}
100
101ASensorRef ASensorManager::getDefaultSensor(int type) {
102    (void)getSensorList(NULL /* list */);
103
104    ASensorRef defaultSensor = NULL;
105
106    Return<void> ret = mManager->getDefaultSensor(
107            static_cast<SensorType>(type),
108            [&](const auto &sensor, auto result) {
109                if (result != Result::OK) {
110                    return;
111                }
112
113                for (size_t i = 0; i < mSensors.size(); ++i) {
114                    if (sensor == mSensors[i]) {
115                        defaultSensor =
116                             reinterpret_cast<ASensorRef>(&mSensors[i]);
117
118                        break;
119                    }
120                }
121            });
122
123    (void)ret.isOk();
124
125    return defaultSensor;
126}
127
128ASensorRef ASensorManager::getDefaultSensorEx(
129        int /* type */, bool /* wakeup */) {
130    // XXX ISensorManager's getDefaultSensorEx() lacks a "wakeup" parameter.
131    return NULL;
132}
133
134ASensorEventQueue *ASensorManager::createEventQueue(
135        ALooper *looper, int ident, ALooper_callbackFunc callback, void *data) {
136    LOG(VERBOSE) << "ASensorManager::createEventQueue";
137
138    sp<ASensorEventQueue> queue =
139        new ASensorEventQueue(looper, ident, callback, data);
140
141    Result result;
142    Return<void> ret =
143        mManager->createEventQueue(
144                queue, [&](const sp<IEventQueue> &queueImpl, auto tmpResult) {
145                    result = tmpResult;
146                    if (result != Result::OK) {
147                        return;
148                    }
149
150                    queue->setImpl(queueImpl);
151                });
152
153    if (!ret.isOk() || result != Result::OK) {
154        LOG(ERROR) << "FAILED to create event queue";
155        return NULL;
156    }
157
158    queue->incStrong(NULL);
159
160    LOG(VERBOSE) << "Returning event queue " << queue.get();
161    return queue.get();
162}
163
164void ASensorManager::destroyEventQueue(ASensorEventQueue *queue) {
165    LOG(VERBOSE) << "ASensorManager::destroyEventQueue(" << queue << ")";
166
167    queue->invalidate();
168
169    delete queue;
170    queue = NULL;
171}
172
173////////////////////////////////////////////////////////////////////////////////
174
175ASensorManager *ASensorManager_getInstance() {
176    return ASensorManager::getInstance();
177}
178
179ASensorManager *ASensorManager_getInstanceForPackage(
180        const char* /* packageName */) {
181    return ASensorManager::getInstance();
182}
183
184#define RETURN_IF_MANAGER_IS_NULL(x)    \
185    do {                                \
186        if (manager == NULL) {          \
187            return x;                   \
188        }                               \
189    } while (0)
190
191#define RETURN_IF_QUEUE_IS_NULL(x)      \
192    do {                                \
193        if (queue == NULL) {            \
194            return x;                   \
195        }                               \
196    } while (0)
197
198#define RETURN_IF_SENSOR_IS_NULL(x)     \
199    do {                                \
200        if (sensor == NULL) {           \
201            return x;                   \
202        }                               \
203    } while (0)
204
205int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list) {
206    RETURN_IF_MANAGER_IS_NULL(BAD_VALUE);
207    return manager->getSensorList(list);
208}
209
210ASensor const* ASensorManager_getDefaultSensor(
211        ASensorManager* manager, int type) {
212    RETURN_IF_MANAGER_IS_NULL(NULL);
213
214    return manager->getDefaultSensor(type);
215}
216
217#if 0
218ASensor const* ASensorManager_getDefaultSensorEx(
219        ASensorManager* manager, int type, bool wakeUp) {
220    RETURN_IF_MANAGER_IS_NULL(NULL);
221
222    return manager->getDefaultSensorEx(type, wakeUp);
223}
224#endif
225
226ASensorEventQueue* ASensorManager_createEventQueue(
227        ASensorManager* manager,
228        ALooper* looper,
229        int ident,
230        ALooper_callbackFunc callback,
231        void* data) {
232    RETURN_IF_MANAGER_IS_NULL(NULL);
233
234    if (looper == NULL) {
235        return NULL;
236    }
237
238    return manager->createEventQueue(looper, ident, callback, data);
239}
240
241int ASensorManager_destroyEventQueue(
242        ASensorManager* manager, ASensorEventQueue* queue) {
243    RETURN_IF_MANAGER_IS_NULL(BAD_VALUE);
244    RETURN_IF_QUEUE_IS_NULL(BAD_VALUE);
245
246    manager->destroyEventQueue(queue);
247    queue = NULL;
248
249    return OK;
250}
251
252#if 0
253int ASensorManager_createSharedMemoryDirectChannel(
254        ASensorManager* manager, int fd, size_t size) {
255    RETURN_IF_MANAGER_IS_NULL(BAD_VALUE);
256
257    return OK;
258}
259
260int ASensorManager_createHardwareBufferDirectChannel(
261        ASensorManager* manager, AHardwareBuffer const * buffer, size_t size) {
262    RETURN_IF_MANAGER_IS_NULL(BAD_VALUE);
263
264    return OK;
265}
266
267void ASensorManager_destroyDirectChannel(
268        ASensorManager* manager, int channelId) {
269}
270
271int ASensorManager_configureDirectReport(
272        ASensorManager* manager,
273        ASensor const* sensor,
274        int channelId,int rate) {
275    RETURN_IF_MANAGER_IS_NULL(BAD_VALUE);
276    return OK;
277}
278#endif
279
280int ASensorEventQueue_registerSensor(
281        ASensorEventQueue* queue,
282        ASensor const* sensor,
283        int32_t samplingPeriodUs,
284        int64_t maxBatchReportLatencyUs) {
285    LOG(VERBOSE) << "ASensorEventQueue_registerSensor";
286    RETURN_IF_QUEUE_IS_NULL(BAD_VALUE);
287    return queue->registerSensor(
288            sensor, samplingPeriodUs, maxBatchReportLatencyUs);
289}
290
291int ASensorEventQueue_enableSensor(
292        ASensorEventQueue* queue, ASensor const* sensor) {
293    LOG(VERBOSE) << "ASensorEventQueue_enableSensor(queue " << queue << ")";
294    RETURN_IF_QUEUE_IS_NULL(BAD_VALUE);
295    return queue->enableSensor(sensor);
296}
297
298int ASensorEventQueue_disableSensor(
299        ASensorEventQueue* queue, ASensor const* sensor) {
300    LOG(VERBOSE) << "ASensorEventQueue_disableSensor";
301    RETURN_IF_QUEUE_IS_NULL(BAD_VALUE);
302    return queue->disableSensor(sensor);
303}
304
305int ASensorEventQueue_setEventRate(
306        ASensorEventQueue* queue,
307        ASensor const* sensor,
308        int32_t usec) {
309    RETURN_IF_QUEUE_IS_NULL(BAD_VALUE);
310    return queue->setEventRate(sensor, usec);
311}
312
313int ASensorEventQueue_hasEvents(ASensorEventQueue* queue) {
314    RETURN_IF_QUEUE_IS_NULL(BAD_VALUE);
315    return queue->hasEvents();
316}
317
318ssize_t ASensorEventQueue_getEvents(
319        ASensorEventQueue* queue, ASensorEvent* events, size_t count) {
320    LOG(VERBOSE) << "ASensorEventQueue_getEvents";
321    RETURN_IF_QUEUE_IS_NULL(BAD_VALUE);
322    return queue->getEvents(events, count);
323}
324
325const char *ASensor_getName(ASensor const* sensor) {
326    RETURN_IF_SENSOR_IS_NULL(NULL);
327    return reinterpret_cast<const SensorInfo *>(sensor)->name.c_str();
328}
329
330const char *ASensor_getVendor(ASensor const* sensor) {
331    RETURN_IF_SENSOR_IS_NULL(NULL);
332    return reinterpret_cast<const SensorInfo *>(sensor)->vendor.c_str();
333}
334
335int ASensor_getType(ASensor const* sensor) {
336    RETURN_IF_SENSOR_IS_NULL(ASENSOR_TYPE_INVALID);
337    return static_cast<int>(
338            reinterpret_cast<const SensorInfo *>(sensor)->type);
339}
340
341float ASensor_getResolution(ASensor const* sensor) {
342    RETURN_IF_SENSOR_IS_NULL(ASENSOR_RESOLUTION_INVALID);
343    return reinterpret_cast<const SensorInfo *>(sensor)->resolution;
344}
345
346int ASensor_getMinDelay(ASensor const* sensor) {
347    RETURN_IF_SENSOR_IS_NULL(ASENSOR_DELAY_INVALID);
348    return reinterpret_cast<const SensorInfo *>(sensor)->minDelay;
349}
350
351int ASensor_getFifoMaxEventCount(ASensor const* sensor) {
352    RETURN_IF_SENSOR_IS_NULL(ASENSOR_FIFO_COUNT_INVALID);
353    return reinterpret_cast<const SensorInfo *>(sensor)->fifoMaxEventCount;
354}
355
356int ASensor_getFifoReservedEventCount(ASensor const* sensor) {
357    RETURN_IF_SENSOR_IS_NULL(ASENSOR_FIFO_COUNT_INVALID);
358    return reinterpret_cast<const SensorInfo *>(sensor)->fifoReservedEventCount;
359}
360
361const char* ASensor_getStringType(ASensor const* sensor) {
362    RETURN_IF_SENSOR_IS_NULL(NULL);
363    return reinterpret_cast<const SensorInfo *>(sensor)->typeAsString.c_str();
364}
365
366#if 0
367int ASensor_getReportingMode(ASensor const* sensor) {
368    RETURN_IF_SENSOR_IS_NULL(AREPORTING_MODE_INVALID);
369    return 0;
370}
371
372bool ASensor_isWakeUpSensor(ASensor const* sensor) {
373    RETURN_IF_SENSOR_IS_NULL(false);
374    return false;
375}
376
377bool ASensor_isDirectChannelTypeSupported(
378        ASensor const* sensor, int channelType) {
379    RETURN_IF_SENSOR_IS_NULL(false);
380    return false;
381}
382
383int ASensor_getHighestDirectReportRateLevel(ASensor const* sensor) {
384    RETURN_IF_SENSOR_IS_NULL(ASENSOR_DIRECT_RATE_STOP);
385    return 0;
386}
387#endif
388
389static ALooper *getTheLooper() {
390    static ALooper *sLooper = NULL;
391
392    Mutex::Autolock autoLock(gLock);
393    if (sLooper == NULL) {
394        sLooper = new ALooper;
395    }
396
397    return sLooper;
398}
399
400
401ALooper *ALooper_forThread() {
402    LOG(VERBOSE) << "ALooper_forThread";
403    return getTheLooper();
404}
405
406ALooper *ALooper_prepare(int /* opts */) {
407    LOG(VERBOSE) << "ALooper_prepare";
408    return getTheLooper();
409}
410
411int ALooper_pollOnce(
412        int timeoutMillis, int* outFd, int* outEvents, void** outData) {
413    int res = getTheLooper()->pollOnce(timeoutMillis, outFd, outEvents, outData);
414    LOG(VERBOSE) << "ALooper_pollOnce => " << res;
415    return res;
416}
417
418void ALooper_wake(ALooper* looper) {
419    LOG(VERBOSE) << "ALooper_wake";
420    looper->wake();
421}
422