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