Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 <stdint.h> |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #include <utils/Errors.h> |
| 21 | #include <utils/String8.h> |
| 22 | #include <utils/Flattenable.h> |
| 23 | |
| 24 | #include <hardware/sensors.h> |
| 25 | |
| 26 | #include <gui/Sensor.h> |
| 27 | |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | namespace android { |
| 30 | // ---------------------------------------------------------------------------- |
| 31 | |
| 32 | Sensor::Sensor() |
| 33 | : mHandle(0), mType(0), |
| 34 | mMinValue(0), mMaxValue(0), mResolution(0), |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 35 | mPower(0), mMinDelay(0), mFifoReservedEventCount(0), mFifoMaxEventCount(0) |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 39 | Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion) |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 40 | { |
| 41 | mName = hwSensor->name; |
| 42 | mVendor = hwSensor->vendor; |
Mathias Agopian | 2ebc4d6 | 2012-05-04 15:47:13 -0700 | [diff] [blame] | 43 | mVersion = hwSensor->version; |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 44 | mHandle = hwSensor->handle; |
| 45 | mType = hwSensor->type; |
| 46 | mMinValue = 0; // FIXME: minValue |
| 47 | mMaxValue = hwSensor->maxRange; // FIXME: maxValue |
| 48 | mResolution = hwSensor->resolution; |
| 49 | mPower = hwSensor->power; |
Mathias Agopian | a48bcf6 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 50 | mMinDelay = hwSensor->minDelay; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 51 | // Set fifo event count zero for older devices which do not support batching. Fused |
| 52 | // sensors also have their fifo counts set to zero. |
| 53 | if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { |
| 54 | mFifoReservedEventCount = hwSensor->fifoReservedEventCount; |
| 55 | mFifoMaxEventCount = hwSensor->fifoMaxEventCount; |
Etienne Le Grand | 4369a4e | 2014-04-05 05:02:22 +0000 | [diff] [blame^] | 56 | } else { |
| 57 | mFifoReservedEventCount = 0; |
| 58 | mFifoMaxEventCount = 0; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 59 | } |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 62 | Sensor::~Sensor() |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | const String8& Sensor::getName() const { |
| 67 | return mName; |
| 68 | } |
| 69 | |
| 70 | const String8& Sensor::getVendor() const { |
| 71 | return mVendor; |
| 72 | } |
| 73 | |
| 74 | int32_t Sensor::getHandle() const { |
| 75 | return mHandle; |
| 76 | } |
| 77 | |
| 78 | int32_t Sensor::getType() const { |
| 79 | return mType; |
| 80 | } |
| 81 | |
| 82 | float Sensor::getMinValue() const { |
| 83 | return mMinValue; |
| 84 | } |
| 85 | |
| 86 | float Sensor::getMaxValue() const { |
| 87 | return mMaxValue; |
| 88 | } |
| 89 | |
| 90 | float Sensor::getResolution() const { |
| 91 | return mResolution; |
| 92 | } |
| 93 | |
| 94 | float Sensor::getPowerUsage() const { |
| 95 | return mPower; |
| 96 | } |
| 97 | |
Mathias Agopian | a48bcf6 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 98 | int32_t Sensor::getMinDelay() const { |
| 99 | return mMinDelay; |
| 100 | } |
| 101 | |
Mathias Agopian | b62013f | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 102 | nsecs_t Sensor::getMinDelayNs() const { |
| 103 | return getMinDelay() * 1000; |
| 104 | } |
| 105 | |
| 106 | int32_t Sensor::getVersion() const { |
| 107 | return mVersion; |
| 108 | } |
| 109 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 110 | int32_t Sensor::getFifoReservedEventCount() const { |
| 111 | return mFifoReservedEventCount; |
| 112 | } |
| 113 | |
| 114 | int32_t Sensor::getFifoMaxEventCount() const { |
| 115 | return mFifoMaxEventCount; |
| 116 | } |
| 117 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 118 | size_t Sensor::getFlattenedSize() const |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 119 | { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 120 | size_t fixedSize = |
Mathias Agopian | 2ebc4d6 | 2012-05-04 15:47:13 -0700 | [diff] [blame] | 121 | sizeof(int32_t) * 3 + |
Mathias Agopian | a48bcf6 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 122 | sizeof(float) * 4 + |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 123 | sizeof(int32_t) * 3; |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 124 | |
| 125 | size_t variableSize = |
Etienne Le Grand | 4369a4e | 2014-04-05 05:02:22 +0000 | [diff] [blame^] | 126 | sizeof(int32_t) + FlattenableUtils::align<4>(mName.length()) + |
| 127 | sizeof(int32_t) + FlattenableUtils::align<4>(mVendor.length()); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 128 | |
| 129 | return fixedSize + variableSize; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 132 | status_t Sensor::flatten(void* buffer, size_t size) const { |
| 133 | if (size < getFlattenedSize()) { |
| 134 | return NO_MEMORY; |
| 135 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 136 | |
Etienne Le Grand | 4369a4e | 2014-04-05 05:02:22 +0000 | [diff] [blame^] | 137 | FlattenableUtils::write(buffer, size, mName.length()); |
| 138 | memcpy(static_cast<char*>(buffer), mName.string(), mName.length()); |
| 139 | FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(mName.length())); |
| 140 | |
| 141 | FlattenableUtils::write(buffer, size, mVendor.length()); |
| 142 | memcpy(static_cast<char*>(buffer), mVendor.string(), mVendor.length()); |
| 143 | FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(mVendor.length())); |
| 144 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 145 | FlattenableUtils::write(buffer, size, mVersion); |
| 146 | FlattenableUtils::write(buffer, size, mHandle); |
| 147 | FlattenableUtils::write(buffer, size, mType); |
| 148 | FlattenableUtils::write(buffer, size, mMinValue); |
| 149 | FlattenableUtils::write(buffer, size, mMaxValue); |
| 150 | FlattenableUtils::write(buffer, size, mResolution); |
| 151 | FlattenableUtils::write(buffer, size, mPower); |
| 152 | FlattenableUtils::write(buffer, size, mMinDelay); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 153 | FlattenableUtils::write(buffer, size, mFifoReservedEventCount); |
| 154 | FlattenableUtils::write(buffer, size, mFifoMaxEventCount); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 155 | return NO_ERROR; |
| 156 | } |
| 157 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 158 | status_t Sensor::unflatten(void const* buffer, size_t size) { |
Etienne Le Grand | 4369a4e | 2014-04-05 05:02:22 +0000 | [diff] [blame^] | 159 | size_t len; |
| 160 | |
| 161 | if (size < sizeof(size_t)) { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 162 | return NO_MEMORY; |
| 163 | } |
Etienne Le Grand | 4369a4e | 2014-04-05 05:02:22 +0000 | [diff] [blame^] | 164 | FlattenableUtils::read(buffer, size, len); |
| 165 | if (size < len) { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 166 | return NO_MEMORY; |
| 167 | } |
Etienne Le Grand | 4369a4e | 2014-04-05 05:02:22 +0000 | [diff] [blame^] | 168 | mName.setTo(static_cast<char const*>(buffer), len); |
| 169 | FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len)); |
| 170 | |
| 171 | |
| 172 | if (size < sizeof(size_t)) { |
| 173 | return NO_MEMORY; |
| 174 | } |
| 175 | FlattenableUtils::read(buffer, size, len); |
| 176 | if (size < len) { |
| 177 | return NO_MEMORY; |
| 178 | } |
| 179 | mVendor.setTo(static_cast<char const*>(buffer), len); |
| 180 | FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len)); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 181 | |
| 182 | size_t fixedSize = |
| 183 | sizeof(int32_t) * 3 + |
| 184 | sizeof(float) * 4 + |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 185 | sizeof(int32_t) * 3; |
Etienne Le Grand | 4369a4e | 2014-04-05 05:02:22 +0000 | [diff] [blame^] | 186 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 187 | if (size < fixedSize) { |
| 188 | return NO_MEMORY; |
| 189 | } |
| 190 | |
| 191 | FlattenableUtils::read(buffer, size, mVersion); |
| 192 | FlattenableUtils::read(buffer, size, mHandle); |
| 193 | FlattenableUtils::read(buffer, size, mType); |
| 194 | FlattenableUtils::read(buffer, size, mMinValue); |
| 195 | FlattenableUtils::read(buffer, size, mMaxValue); |
| 196 | FlattenableUtils::read(buffer, size, mResolution); |
| 197 | FlattenableUtils::read(buffer, size, mPower); |
| 198 | FlattenableUtils::read(buffer, size, mMinDelay); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 199 | FlattenableUtils::read(buffer, size, mFifoReservedEventCount); |
| 200 | FlattenableUtils::read(buffer, size, mFifoMaxEventCount); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 201 | return NO_ERROR; |
| 202 | } |
| 203 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 204 | // ---------------------------------------------------------------------------- |
| 205 | }; // namespace android |