Mathias Agopian | b957b9d | 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), |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 35 | mPower(0), mMinDelay(0) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 39 | Sensor::Sensor(struct sensor_t const* hwSensor) |
| 40 | { |
| 41 | mName = hwSensor->name; |
| 42 | mVendor = hwSensor->vendor; |
| 43 | mHandle = hwSensor->handle; |
| 44 | mType = hwSensor->type; |
| 45 | mMinValue = 0; // FIXME: minValue |
| 46 | mMaxValue = hwSensor->maxRange; // FIXME: maxValue |
| 47 | mResolution = hwSensor->resolution; |
| 48 | mPower = hwSensor->power; |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 49 | mMinDelay = hwSensor->minDelay; |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 52 | Sensor::~Sensor() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | const String8& Sensor::getName() const { |
| 57 | return mName; |
| 58 | } |
| 59 | |
| 60 | const String8& Sensor::getVendor() const { |
| 61 | return mVendor; |
| 62 | } |
| 63 | |
| 64 | int32_t Sensor::getHandle() const { |
| 65 | return mHandle; |
| 66 | } |
| 67 | |
| 68 | int32_t Sensor::getType() const { |
| 69 | return mType; |
| 70 | } |
| 71 | |
| 72 | float Sensor::getMinValue() const { |
| 73 | return mMinValue; |
| 74 | } |
| 75 | |
| 76 | float Sensor::getMaxValue() const { |
| 77 | return mMaxValue; |
| 78 | } |
| 79 | |
| 80 | float Sensor::getResolution() const { |
| 81 | return mResolution; |
| 82 | } |
| 83 | |
| 84 | float Sensor::getPowerUsage() const { |
| 85 | return mPower; |
| 86 | } |
| 87 | |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 88 | int32_t Sensor::getMinDelay() const { |
| 89 | return mMinDelay; |
| 90 | } |
| 91 | |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 92 | nsecs_t Sensor::getMinDelayNs() const { |
| 93 | return getMinDelay() * 1000; |
| 94 | } |
| 95 | |
| 96 | int32_t Sensor::getVersion() const { |
| 97 | return mVersion; |
| 98 | } |
| 99 | |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 100 | size_t Sensor::getFlattenedSize() const |
| 101 | { |
| 102 | return sizeof(int32_t) + ((mName.length() + 3) & ~3) + |
| 103 | sizeof(int32_t) + ((mVendor.length() + 3) & ~3) + |
| 104 | sizeof(int32_t) * 2 + |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 105 | sizeof(float) * 4 + |
| 106 | sizeof(int32_t); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | size_t Sensor::getFdCount() const |
| 110 | { |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static inline |
| 115 | size_t write(void* buffer, size_t offset, const String8& value) { |
| 116 | memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length()); |
| 117 | return (value.length() + 3) & ~3; |
| 118 | } |
| 119 | |
| 120 | static inline |
| 121 | size_t write(void* buffer, size_t offset, float value) { |
| 122 | *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value; |
| 123 | return sizeof(float); |
| 124 | } |
| 125 | |
| 126 | static inline |
| 127 | size_t write(void* buffer, size_t offset, int32_t value) { |
| 128 | *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value; |
| 129 | return sizeof(int32_t); |
| 130 | } |
| 131 | |
| 132 | status_t Sensor::flatten(void* buffer, size_t size, |
| 133 | int fds[], size_t count) const |
| 134 | { |
| 135 | if (size < Sensor::getFlattenedSize()) |
| 136 | return -ENOMEM; |
| 137 | |
| 138 | size_t offset = 0; |
| 139 | offset += write(buffer, offset, int32_t(mName.length())); |
| 140 | offset += write(buffer, offset, mName); |
| 141 | offset += write(buffer, offset, int32_t(mVendor.length())); |
| 142 | offset += write(buffer, offset, mVendor); |
| 143 | offset += write(buffer, offset, mHandle); |
| 144 | offset += write(buffer, offset, mType); |
| 145 | offset += write(buffer, offset, mMinValue); |
| 146 | offset += write(buffer, offset, mMaxValue); |
| 147 | offset += write(buffer, offset, mResolution); |
| 148 | offset += write(buffer, offset, mPower); |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 149 | offset += write(buffer, offset, mMinDelay); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 150 | |
| 151 | return NO_ERROR; |
| 152 | } |
| 153 | |
| 154 | static inline |
| 155 | size_t read(void const* buffer, size_t offset, String8* value, int32_t len) { |
| 156 | value->setTo(static_cast<char const*>(buffer) + offset, len); |
| 157 | return (len + 3) & ~3; |
| 158 | } |
| 159 | |
| 160 | static inline |
| 161 | size_t read(void const* buffer, size_t offset, float* value) { |
| 162 | *value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset); |
| 163 | return sizeof(float); |
| 164 | } |
| 165 | |
| 166 | static inline |
| 167 | size_t read(void const* buffer, size_t offset, int32_t* value) { |
| 168 | *value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset); |
| 169 | return sizeof(int32_t); |
| 170 | } |
| 171 | |
| 172 | status_t Sensor::unflatten(void const* buffer, size_t size, |
| 173 | int fds[], size_t count) |
| 174 | { |
| 175 | int32_t len; |
| 176 | size_t offset = 0; |
| 177 | offset += read(buffer, offset, &len); |
| 178 | offset += read(buffer, offset, &mName, len); |
| 179 | offset += read(buffer, offset, &len); |
| 180 | offset += read(buffer, offset, &mVendor, len); |
| 181 | offset += read(buffer, offset, &mHandle); |
| 182 | offset += read(buffer, offset, &mType); |
| 183 | offset += read(buffer, offset, &mMinValue); |
| 184 | offset += read(buffer, offset, &mMaxValue); |
| 185 | offset += read(buffer, offset, &mResolution); |
| 186 | offset += read(buffer, offset, &mPower); |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 187 | offset += read(buffer, offset, &mMinDelay); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 188 | |
| 189 | return NO_ERROR; |
| 190 | } |
| 191 | |
| 192 | // ---------------------------------------------------------------------------- |
| 193 | }; // namespace android |