Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #define LOG_TAG "sensor" |
| 18 | #include <utils/Log.h> |
| 19 | |
| 20 | #include <android/looper.h> |
| 21 | #include <android/sensor.h> |
| 22 | |
| 23 | #include <utils/RefBase.h> |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 24 | #include <utils/Looper.h> |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 25 | #include <utils/Timers.h> |
| 26 | |
| 27 | #include <gui/Sensor.h> |
| 28 | #include <gui/SensorManager.h> |
| 29 | #include <gui/SensorEventQueue.h> |
| 30 | |
| 31 | #include <poll.h> |
| 32 | |
| 33 | using android::sp; |
| 34 | using android::Sensor; |
| 35 | using android::SensorManager; |
| 36 | using android::SensorEventQueue; |
| 37 | using android::String8; |
Svet Ganov | cbb56a8 | 2015-05-07 10:49:07 -0700 | [diff] [blame] | 38 | using android::String16; |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 39 | |
| 40 | /*****************************************************************************/ |
Svet Ganov | cbb56a8 | 2015-05-07 10:49:07 -0700 | [diff] [blame] | 41 | ASensorManager* ASensorManager_getInstance() |
| 42 | { |
| 43 | return ASensorManager_getInstanceForPackage(NULL); |
| 44 | } |
| 45 | |
| 46 | ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName) |
| 47 | { |
| 48 | if (packageName) { |
| 49 | return &SensorManager::getInstanceForPackage(String16(packageName)); |
| 50 | } else { |
| 51 | return &SensorManager::getInstanceForPackage(String16()); |
| 52 | } |
| 53 | } |
| 54 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 55 | int ASensorManager_getSensorList(ASensorManager* manager, |
| 56 | ASensorList* list) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 57 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 58 | Sensor const* const* l; |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 59 | int c = static_cast<SensorManager*>(manager)->getSensorList(&l); |
| 60 | if (list) { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 61 | *list = reinterpret_cast<ASensorList>(l); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 62 | } |
| 63 | return c; |
| 64 | } |
| 65 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 66 | ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 67 | { |
| 68 | return static_cast<SensorManager*>(manager)->getDefaultSensor(type); |
| 69 | } |
| 70 | |
Aravind Akella | 99fe165 | 2014-08-05 15:02:11 -0700 | [diff] [blame] | 71 | ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, |
| 72 | int type, bool wakeUp) { |
| 73 | Sensor const* const* sensorList; |
| 74 | size_t size = static_cast<SensorManager*>(manager)->getSensorList(&sensorList); |
| 75 | for (size_t i = 0; i < size; ++i) { |
| 76 | if (ASensor_getType(sensorList[i]) == type && |
| 77 | ASensor_isWakeUpSensor(sensorList[i]) == wakeUp) { |
| 78 | return reinterpret_cast<ASensor const *>(sensorList[i]); |
| 79 | } |
| 80 | } |
| 81 | return NULL; |
| 82 | } |
| 83 | |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 84 | ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager, |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 85 | ALooper* looper, int ident, ALooper_callbackFunc callback, void* data) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 86 | { |
| 87 | sp<SensorEventQueue> queue = |
| 88 | static_cast<SensorManager*>(manager)->createEventQueue(); |
| 89 | if (queue != 0) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 90 | ALooper_addFd(looper, queue->getFd(), ident, ALOOPER_EVENT_INPUT, callback, data); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 91 | queue->looper = looper; |
| 92 | queue->incStrong(manager); |
| 93 | } |
| 94 | return static_cast<ASensorEventQueue*>(queue.get()); |
| 95 | } |
| 96 | |
| 97 | int ASensorManager_destroyEventQueue(ASensorManager* manager, |
| 98 | ASensorEventQueue* inQueue) |
| 99 | { |
| 100 | sp<SensorEventQueue> queue = static_cast<SensorEventQueue*>(inQueue); |
| 101 | ALooper_removeFd(queue->looper, queue->getFd()); |
| 102 | queue->decStrong(manager); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /*****************************************************************************/ |
| 107 | |
Aniroop Mathur | ee5c18f | 2015-11-03 02:02:09 +0530 | [diff] [blame^] | 108 | int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor, |
| 109 | int32_t samplingPeriodUs, int maxBatchReportLatencyUs) |
| 110 | { |
| 111 | return static_cast<SensorEventQueue*>(queue)->enableSensor( |
| 112 | static_cast<Sensor const*>(sensor)->getHandle(), samplingPeriodUs, |
| 113 | maxBatchReportLatencyUs, 0); |
| 114 | } |
| 115 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 116 | int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 117 | { |
| 118 | return static_cast<SensorEventQueue*>(queue)->enableSensor( |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 119 | static_cast<Sensor const*>(sensor)); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 122 | int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 123 | { |
| 124 | return static_cast<SensorEventQueue*>(queue)->disableSensor( |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 125 | static_cast<Sensor const*>(sensor)); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 128 | int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 129 | int32_t usec) |
| 130 | { |
| 131 | return static_cast<SensorEventQueue*>(queue)->setEventRate( |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 132 | static_cast<Sensor const*>(sensor), us2ns(usec)); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | int ASensorEventQueue_hasEvents(ASensorEventQueue* queue) |
| 136 | { |
| 137 | struct pollfd pfd; |
| 138 | pfd.fd = static_cast<SensorEventQueue*>(queue)->getFd(); |
| 139 | pfd.events = POLLIN; |
| 140 | pfd.revents = 0; |
| 141 | |
| 142 | int nfd = poll(&pfd, 1, 0); |
| 143 | |
| 144 | if (nfd < 0) |
| 145 | return -errno; |
| 146 | |
| 147 | if (pfd.revents != POLLIN) |
| 148 | return -1; |
| 149 | |
| 150 | return (nfd == 0) ? 0 : 1; |
| 151 | } |
| 152 | |
| 153 | ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, |
| 154 | ASensorEvent* events, size_t count) |
| 155 | { |
Aravind Akella | 35187bd | 2014-02-11 18:44:42 -0800 | [diff] [blame] | 156 | ssize_t actual = static_cast<SensorEventQueue*>(queue)->read(events, count); |
| 157 | if (actual > 0) { |
| 158 | static_cast<SensorEventQueue*>(queue)->sendAck(events, actual); |
| 159 | } |
| 160 | return actual; |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 163 | /*****************************************************************************/ |
| 164 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 165 | const char* ASensor_getName(ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 166 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 167 | return static_cast<Sensor const*>(sensor)->getName().string(); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 170 | const char* ASensor_getVendor(ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 171 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 172 | return static_cast<Sensor const*>(sensor)->getVendor().string(); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 175 | int ASensor_getType(ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 176 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 177 | return static_cast<Sensor const*>(sensor)->getType(); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 180 | float ASensor_getResolution(ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 181 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 182 | return static_cast<Sensor const*>(sensor)->getResolution(); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 185 | int ASensor_getMinDelay(ASensor const* sensor) |
| 186 | { |
| 187 | return static_cast<Sensor const*>(sensor)->getMinDelay(); |
| 188 | } |
Aravind Akella | cd9a7bb | 2014-04-07 22:55:21 +0000 | [diff] [blame] | 189 | |
| 190 | int ASensor_getFifoMaxEventCount(ASensor const* sensor) |
| 191 | { |
| 192 | return static_cast<Sensor const*>(sensor)->getFifoMaxEventCount(); |
| 193 | } |
| 194 | |
| 195 | int ASensor_getFifoReservedEventCount(ASensor const* sensor) |
| 196 | { |
| 197 | return static_cast<Sensor const*>(sensor)->getFifoReservedEventCount(); |
| 198 | } |
| 199 | |
| 200 | const char* ASensor_getStringType(ASensor const* sensor) |
| 201 | { |
| 202 | return static_cast<Sensor const*>(sensor)->getStringType().string(); |
| 203 | } |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 204 | |
| 205 | int ASensor_getReportingMode(ASensor const* sensor) |
| 206 | { |
| 207 | return static_cast<Sensor const*>(sensor)->getReportingMode(); |
| 208 | } |
Aravind Akella | 99fe165 | 2014-08-05 15:02:11 -0700 | [diff] [blame] | 209 | |
| 210 | bool ASensor_isWakeUpSensor(ASensor const* sensor) |
| 211 | { |
| 212 | return static_cast<Sensor const*>(sensor)->isWakeUpSensor(); |
Aravind Akella | 2af91d0 | 2015-07-29 18:39:52 -0700 | [diff] [blame] | 213 | } |