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