Sensor.cpp revision a48bcf62b6a26f24a0bdd2b44bb39fadce499e92
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    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;
49    mMinDelay = hwSensor->minDelay;
50}
51
52Sensor::~Sensor()
53{
54}
55
56const String8& Sensor::getName() const {
57    return mName;
58}
59
60const String8& Sensor::getVendor() const {
61    return mVendor;
62}
63
64int32_t Sensor::getHandle() const {
65    return mHandle;
66}
67
68int32_t Sensor::getType() const {
69    return mType;
70}
71
72float Sensor::getMinValue() const {
73    return mMinValue;
74}
75
76float Sensor::getMaxValue() const {
77    return mMaxValue;
78}
79
80float Sensor::getResolution() const {
81    return mResolution;
82}
83
84float Sensor::getPowerUsage() const {
85    return mPower;
86}
87
88int32_t Sensor::getMinDelay() const {
89    return mMinDelay;
90}
91
92size_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 +
97            sizeof(float) * 4 +
98            sizeof(int32_t);
99}
100
101size_t Sensor::getFdCount() const
102{
103    return 0;
104}
105
106static inline
107size_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
112static inline
113size_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
118static inline
119size_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
124status_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);
141    offset += write(buffer, offset, mMinDelay);
142
143    return NO_ERROR;
144}
145
146static inline
147size_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
152static inline
153size_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
158static inline
159size_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
164status_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);
179    offset += read(buffer, offset, &mMinDelay);
180
181    return NO_ERROR;
182}
183
184// ----------------------------------------------------------------------------
185}; // namespace android
186