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), 35 mPower(0), mMinDelay(0) 36 { 37 } 38 39 Sensor::Sensor(struct sensor_t const* hwSensor) 40 { 41 mName = hwSensor->name; 42 mVendor = hwSensor->vendor; 43 mVersion = hwSensor->version; 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; 50 mMinDelay = hwSensor->minDelay; 51 } 52 53 Sensor::~Sensor() 54 { 55 } 56 57 const String8& Sensor::getName() const { 58 return mName; 59 } 60 61 const String8& Sensor::getVendor() const { 62 return mVendor; 63 } 64 65 int32_t Sensor::getHandle() const { 66 return mHandle; 67 } 68 69 int32_t Sensor::getType() const { 70 return mType; 71 } 72 73 float Sensor::getMinValue() const { 74 return mMinValue; 75 } 76 77 float Sensor::getMaxValue() const { 78 return mMaxValue; 79 } 80 81 float Sensor::getResolution() const { 82 return mResolution; 83 } 84 85 float Sensor::getPowerUsage() const { 86 return mPower; 87 } 88 89 int32_t Sensor::getMinDelay() const { 90 return mMinDelay; 91 } 92 93 nsecs_t Sensor::getMinDelayNs() const { 94 return getMinDelay() * 1000; 95 } 96 97 int32_t Sensor::getVersion() const { 98 return mVersion; 99 } 100 101 size_t Sensor::getSize() const 102 { 103 return sizeof(int32_t) + ((mName.length() + 3) & ~3) + 104 sizeof(int32_t) + ((mVendor.length() + 3) & ~3) + 105 sizeof(int32_t) * 3 + 106 sizeof(float) * 4 + 107 sizeof(int32_t); 108 } 109 110 static inline 111 size_t write(void* buffer, size_t offset, const String8& value) { 112 memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length()); 113 return (value.length() + 3) & ~3; 114 } 115 116 static inline 117 size_t write(void* buffer, size_t offset, float value) { 118 *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value; 119 return sizeof(float); 120 } 121 122 static inline 123 size_t write(void* buffer, size_t offset, int32_t value) { 124 *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value; 125 return sizeof(int32_t); 126 } 127 128 status_t Sensor::flatten(void* buffer) const 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, mVersion); 136 offset += write(buffer, offset, mHandle); 137 offset += write(buffer, offset, mType); 138 offset += write(buffer, offset, mMinValue); 139 offset += write(buffer, offset, mMaxValue); 140 offset += write(buffer, offset, mResolution); 141 offset += write(buffer, offset, mPower); 142 offset += write(buffer, offset, mMinDelay); 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 { 166 int32_t len; 167 size_t offset = 0; 168 offset += read(buffer, offset, &len); 169 offset += read(buffer, offset, &mName, len); 170 offset += read(buffer, offset, &len); 171 offset += read(buffer, offset, &mVendor, len); 172 offset += read(buffer, offset, &mVersion); 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); 179 offset += read(buffer, offset, &mMinDelay); 180 return NO_ERROR; 181 } 182 183 // ---------------------------------------------------------------------------- 184 }; // namespace android 185