Sensor.cpp revision 2ebc4d6d947ef488198b8777d9190c93904b4b9a
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// ----------------------------------------------------------------------------
29namespace android {
30// ----------------------------------------------------------------------------
31
32Sensor::Sensor()
33    : mHandle(0), mType(0),
34      mMinValue(0), mMaxValue(0), mResolution(0),
35      mPower(0), mMinDelay(0)
36{
37}
38
39Sensor::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
53Sensor::~Sensor()
54{
55}
56
57const String8& Sensor::getName() const {
58    return mName;
59}
60
61const String8& Sensor::getVendor() const {
62    return mVendor;
63}
64
65int32_t Sensor::getHandle() const {
66    return mHandle;
67}
68
69int32_t Sensor::getType() const {
70    return mType;
71}
72
73float Sensor::getMinValue() const {
74    return mMinValue;
75}
76
77float Sensor::getMaxValue() const {
78    return mMaxValue;
79}
80
81float Sensor::getResolution() const {
82    return mResolution;
83}
84
85float Sensor::getPowerUsage() const {
86    return mPower;
87}
88
89int32_t Sensor::getMinDelay() const {
90    return mMinDelay;
91}
92
93nsecs_t Sensor::getMinDelayNs() const {
94    return getMinDelay() * 1000;
95}
96
97int32_t Sensor::getVersion() const {
98    return mVersion;
99}
100
101size_t Sensor::getFlattenedSize() 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
110size_t Sensor::getFdCount() const
111{
112    return 0;
113}
114
115static inline
116size_t write(void* buffer, size_t offset, const String8& value) {
117    memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length());
118    return (value.length() + 3) & ~3;
119}
120
121static inline
122size_t write(void* buffer, size_t offset, float value) {
123    *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value;
124    return sizeof(float);
125}
126
127static inline
128size_t write(void* buffer, size_t offset, int32_t value) {
129    *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value;
130    return sizeof(int32_t);
131}
132
133status_t Sensor::flatten(void* buffer, size_t size,
134        int fds[], size_t count) const
135{
136    if (size < Sensor::getFlattenedSize())
137        return -ENOMEM;
138
139    size_t offset = 0;
140    offset += write(buffer, offset, int32_t(mName.length()));
141    offset += write(buffer, offset, mName);
142    offset += write(buffer, offset, int32_t(mVendor.length()));
143    offset += write(buffer, offset, mVendor);
144    offset += write(buffer, offset, mVersion);
145    offset += write(buffer, offset, mHandle);
146    offset += write(buffer, offset, mType);
147    offset += write(buffer, offset, mMinValue);
148    offset += write(buffer, offset, mMaxValue);
149    offset += write(buffer, offset, mResolution);
150    offset += write(buffer, offset, mPower);
151    offset += write(buffer, offset, mMinDelay);
152
153    return NO_ERROR;
154}
155
156static inline
157size_t read(void const* buffer, size_t offset, String8* value, int32_t len) {
158    value->setTo(static_cast<char const*>(buffer) + offset, len);
159    return (len + 3) & ~3;
160}
161
162static inline
163size_t read(void const* buffer, size_t offset, float* value) {
164    *value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset);
165    return sizeof(float);
166}
167
168static inline
169size_t read(void const* buffer, size_t offset, int32_t* value) {
170    *value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset);
171    return sizeof(int32_t);
172}
173
174status_t Sensor::unflatten(void const* buffer, size_t size,
175        int fds[], size_t count)
176{
177    int32_t len;
178    size_t offset = 0;
179    offset += read(buffer, offset, &len);
180    offset += read(buffer, offset, &mName, len);
181    offset += read(buffer, offset, &len);
182    offset += read(buffer, offset, &mVendor, len);
183    offset += read(buffer, offset, &mVersion);
184    offset += read(buffer, offset, &mHandle);
185    offset += read(buffer, offset, &mType);
186    offset += read(buffer, offset, &mMinValue);
187    offset += read(buffer, offset, &mMaxValue);
188    offset += read(buffer, offset, &mResolution);
189    offset += read(buffer, offset, &mPower);
190    offset += read(buffer, offset, &mMinDelay);
191
192    return NO_ERROR;
193}
194
195// ----------------------------------------------------------------------------
196}; // namespace android
197