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; |
| 38 | |
| 39 | /*****************************************************************************/ |
| 40 | |
| 41 | ASensorManager* ASensorManager_getInstance() |
| 42 | { |
| 43 | return &SensorManager::getInstance(); |
| 44 | } |
| 45 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 46 | int ASensorManager_getSensorList(ASensorManager* manager, |
| 47 | ASensorList* list) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 48 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 49 | Sensor const* const* l; |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 50 | int c = static_cast<SensorManager*>(manager)->getSensorList(&l); |
| 51 | if (list) { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 52 | *list = reinterpret_cast<ASensorList>(l); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 53 | } |
| 54 | return c; |
| 55 | } |
| 56 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 57 | ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 58 | { |
| 59 | return static_cast<SensorManager*>(manager)->getDefaultSensor(type); |
| 60 | } |
| 61 | |
| 62 | ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager, |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 63 | ALooper* looper, int ident, ALooper_callbackFunc callback, void* data) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 64 | { |
| 65 | sp<SensorEventQueue> queue = |
| 66 | static_cast<SensorManager*>(manager)->createEventQueue(); |
| 67 | if (queue != 0) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 68 | ALooper_addFd(looper, queue->getFd(), ident, ALOOPER_EVENT_INPUT, callback, data); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 69 | queue->looper = looper; |
| 70 | queue->incStrong(manager); |
| 71 | } |
| 72 | return static_cast<ASensorEventQueue*>(queue.get()); |
| 73 | } |
| 74 | |
| 75 | int ASensorManager_destroyEventQueue(ASensorManager* manager, |
| 76 | ASensorEventQueue* inQueue) |
| 77 | { |
| 78 | sp<SensorEventQueue> queue = static_cast<SensorEventQueue*>(inQueue); |
| 79 | ALooper_removeFd(queue->looper, queue->getFd()); |
| 80 | queue->decStrong(manager); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /*****************************************************************************/ |
| 85 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 86 | int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 87 | { |
| 88 | return static_cast<SensorEventQueue*>(queue)->enableSensor( |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 89 | static_cast<Sensor const*>(sensor)); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 92 | int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 93 | { |
| 94 | return static_cast<SensorEventQueue*>(queue)->disableSensor( |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 95 | static_cast<Sensor const*>(sensor)); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 98 | int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 99 | int32_t usec) |
| 100 | { |
| 101 | return static_cast<SensorEventQueue*>(queue)->setEventRate( |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 102 | static_cast<Sensor const*>(sensor), us2ns(usec)); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int ASensorEventQueue_hasEvents(ASensorEventQueue* queue) |
| 106 | { |
| 107 | struct pollfd pfd; |
| 108 | pfd.fd = static_cast<SensorEventQueue*>(queue)->getFd(); |
| 109 | pfd.events = POLLIN; |
| 110 | pfd.revents = 0; |
| 111 | |
| 112 | int nfd = poll(&pfd, 1, 0); |
| 113 | |
| 114 | if (nfd < 0) |
| 115 | return -errno; |
| 116 | |
| 117 | if (pfd.revents != POLLIN) |
| 118 | return -1; |
| 119 | |
| 120 | return (nfd == 0) ? 0 : 1; |
| 121 | } |
| 122 | |
| 123 | ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, |
| 124 | ASensorEvent* events, size_t count) |
| 125 | { |
Aravind Akella | 35187bd | 2014-02-11 18:44:42 -0800 | [diff] [blame] | 126 | ssize_t actual = static_cast<SensorEventQueue*>(queue)->read(events, count); |
| 127 | if (actual > 0) { |
| 128 | static_cast<SensorEventQueue*>(queue)->sendAck(events, actual); |
| 129 | } |
| 130 | return actual; |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 133 | /*****************************************************************************/ |
| 134 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 135 | const char* ASensor_getName(ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 136 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 137 | return static_cast<Sensor const*>(sensor)->getName().string(); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 140 | const char* ASensor_getVendor(ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 141 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 142 | return static_cast<Sensor const*>(sensor)->getVendor().string(); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 145 | int ASensor_getType(ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 146 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 147 | return static_cast<Sensor const*>(sensor)->getType(); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 150 | float ASensor_getResolution(ASensor const* sensor) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 151 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 152 | return static_cast<Sensor const*>(sensor)->getResolution(); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 155 | int ASensor_getMinDelay(ASensor const* sensor) |
| 156 | { |
| 157 | return static_cast<Sensor const*>(sensor)->getMinDelay(); |
| 158 | } |
Aravind Akella | cd9a7bb | 2014-04-07 22:55:21 +0000 | [diff] [blame] | 159 | |
| 160 | int ASensor_getFifoMaxEventCount(ASensor const* sensor) |
| 161 | { |
| 162 | return static_cast<Sensor const*>(sensor)->getFifoMaxEventCount(); |
| 163 | } |
| 164 | |
| 165 | int ASensor_getFifoReservedEventCount(ASensor const* sensor) |
| 166 | { |
| 167 | return static_cast<Sensor const*>(sensor)->getFifoReservedEventCount(); |
| 168 | } |
| 169 | |
| 170 | const char* ASensor_getStringType(ASensor const* sensor) |
| 171 | { |
| 172 | return static_cast<Sensor const*>(sensor)->getStringType().string(); |
| 173 | } |
| 174 | |
| 175 | const char* ASensor_getRequiredPermission(ASensor const* sensor) |
| 176 | { |
| 177 | return static_cast<Sensor const*>(sensor)->getRequiredPermission().string(); |
| 178 | } |