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 | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 92 | size_t Sensor::getFlattenedSize() const |
| 93 | { |
| 94 | return sizeof(int32_t) + ((mName.length() + 3) & ~3) + |
| 95 | sizeof(int32_t) + ((mVendor.length() + 3) & ~3) + |
| 96 | sizeof(int32_t) * 2 + |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 97 | sizeof(float) * 4 + |
| 98 | sizeof(int32_t); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | size_t Sensor::getFdCount() const |
| 102 | { |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static inline |
| 107 | size_t write(void* buffer, size_t offset, const String8& value) { |
| 108 | memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length()); |
| 109 | return (value.length() + 3) & ~3; |
| 110 | } |
| 111 | |
| 112 | static inline |
| 113 | size_t write(void* buffer, size_t offset, float value) { |
| 114 | *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value; |
| 115 | return sizeof(float); |
| 116 | } |
| 117 | |
| 118 | static inline |
| 119 | size_t write(void* buffer, size_t offset, int32_t value) { |
| 120 | *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value; |
| 121 | return sizeof(int32_t); |
| 122 | } |
| 123 | |
| 124 | status_t Sensor::flatten(void* buffer, size_t size, |
| 125 | int fds[], size_t count) const |
| 126 | { |
| 127 | if (size < Sensor::getFlattenedSize()) |
| 128 | return -ENOMEM; |
| 129 | |
| 130 | size_t offset = 0; |
| 131 | offset += write(buffer, offset, int32_t(mName.length())); |
| 132 | offset += write(buffer, offset, mName); |
| 133 | offset += write(buffer, offset, int32_t(mVendor.length())); |
| 134 | offset += write(buffer, offset, mVendor); |
| 135 | offset += write(buffer, offset, mHandle); |
| 136 | offset += write(buffer, offset, mType); |
| 137 | offset += write(buffer, offset, mMinValue); |
| 138 | offset += write(buffer, offset, mMaxValue); |
| 139 | offset += write(buffer, offset, mResolution); |
| 140 | offset += write(buffer, offset, mPower); |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 141 | offset += write(buffer, offset, mMinDelay); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 142 | |
| 143 | return NO_ERROR; |
| 144 | } |
| 145 | |
| 146 | static inline |
| 147 | size_t read(void const* buffer, size_t offset, String8* value, int32_t len) { |
| 148 | value->setTo(static_cast<char const*>(buffer) + offset, len); |
| 149 | return (len + 3) & ~3; |
| 150 | } |
| 151 | |
| 152 | static inline |
| 153 | size_t read(void const* buffer, size_t offset, float* value) { |
| 154 | *value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset); |
| 155 | return sizeof(float); |
| 156 | } |
| 157 | |
| 158 | static inline |
| 159 | size_t read(void const* buffer, size_t offset, int32_t* value) { |
| 160 | *value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset); |
| 161 | return sizeof(int32_t); |
| 162 | } |
| 163 | |
| 164 | status_t Sensor::unflatten(void const* buffer, size_t size, |
| 165 | int fds[], size_t count) |
| 166 | { |
| 167 | int32_t len; |
| 168 | size_t offset = 0; |
| 169 | offset += read(buffer, offset, &len); |
| 170 | offset += read(buffer, offset, &mName, len); |
| 171 | offset += read(buffer, offset, &len); |
| 172 | offset += read(buffer, offset, &mVendor, len); |
| 173 | offset += read(buffer, offset, &mHandle); |
| 174 | offset += read(buffer, offset, &mType); |
| 175 | offset += read(buffer, offset, &mMinValue); |
| 176 | offset += read(buffer, offset, &mMaxValue); |
| 177 | offset += read(buffer, offset, &mResolution); |
| 178 | offset += read(buffer, offset, &mPower); |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 179 | offset += read(buffer, offset, &mMinDelay); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 180 | |
| 181 | return NO_ERROR; |
| 182 | } |
| 183 | |
| 184 | // ---------------------------------------------------------------------------- |
| 185 | }; // namespace android |