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