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