Sensor.cpp revision e142428a9c8b9d2380032cd4d7b55ee440fe8770
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    size_t fixedSize =
104            sizeof(int32_t) * 3 +
105            sizeof(float) * 4 +
106            sizeof(int32_t);
107
108    size_t variableSize =
109            sizeof(int32_t) + FlattenableUtils::align<4>(mName.length()) +
110            sizeof(int32_t) + FlattenableUtils::align<4>(mVendor.length());
111
112    return fixedSize + variableSize;
113}
114
115status_t Sensor::flatten(void* buffer, size_t size) const {
116    if (size < getFlattenedSize()) {
117        return NO_MEMORY;
118    }
119
120    FlattenableUtils::write(buffer, size, mName.length());
121    memcpy(static_cast<char*>(buffer), mName.string(), mName.length());
122    FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(mName.length()));
123
124    FlattenableUtils::write(buffer, size, mVendor.length());
125    memcpy(static_cast<char*>(buffer), mVendor.string(), mVendor.length());
126    FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(mVendor.length()));
127
128    FlattenableUtils::write(buffer, size, mVersion);
129    FlattenableUtils::write(buffer, size, mHandle);
130    FlattenableUtils::write(buffer, size, mType);
131    FlattenableUtils::write(buffer, size, mMinValue);
132    FlattenableUtils::write(buffer, size, mMaxValue);
133    FlattenableUtils::write(buffer, size, mResolution);
134    FlattenableUtils::write(buffer, size, mPower);
135    FlattenableUtils::write(buffer, size, mMinDelay);
136    return NO_ERROR;
137}
138
139status_t Sensor::unflatten(void const* buffer, size_t size) {
140    size_t len;
141
142    if (size < sizeof(size_t)) {
143        return NO_MEMORY;
144    }
145    FlattenableUtils::read(buffer, size, len);
146    if (size < len) {
147        return NO_MEMORY;
148    }
149    mName.setTo(static_cast<char const*>(buffer), len);
150    FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
151
152
153    if (size < sizeof(size_t)) {
154        return NO_MEMORY;
155    }
156    FlattenableUtils::read(buffer, size, len);
157    if (size < len) {
158        return NO_MEMORY;
159    }
160    mVendor.setTo(static_cast<char const*>(buffer), len);
161    FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
162
163    size_t fixedSize =
164            sizeof(int32_t) * 3 +
165            sizeof(float) * 4 +
166            sizeof(int32_t);
167
168    if (size < fixedSize) {
169        return NO_MEMORY;
170    }
171
172    FlattenableUtils::read(buffer, size, mVersion);
173    FlattenableUtils::read(buffer, size, mHandle);
174    FlattenableUtils::read(buffer, size, mType);
175    FlattenableUtils::read(buffer, size, mMinValue);
176    FlattenableUtils::read(buffer, size, mMaxValue);
177    FlattenableUtils::read(buffer, size, mResolution);
178    FlattenableUtils::read(buffer, size, mPower);
179    FlattenableUtils::read(buffer, size, mMinDelay);
180    return NO_ERROR;
181}
182
183// ----------------------------------------------------------------------------
184}; // namespace android
185