blob: d84c370a177fc875d2244f4fac4b679a0c4954e2 [file] [log] [blame]
Mathias Agopian589ce852010-07-13 22:21:56 -07001/*
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),
Mathias Agopiana48bcf62010-07-29 16:51:38 -070035 mPower(0), mMinDelay(0)
Mathias Agopian589ce852010-07-13 22:21:56 -070036{
37}
38
Mathias Agopiana7352c92010-07-14 23:41:37 -070039Sensor::Sensor(struct sensor_t const* hwSensor)
40{
41 mName = hwSensor->name;
42 mVendor = hwSensor->vendor;
Mathias Agopian2ebc4d62012-05-04 15:47:13 -070043 mVersion = hwSensor->version;
Mathias Agopiana7352c92010-07-14 23:41:37 -070044 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;
Mathias Agopiana48bcf62010-07-29 16:51:38 -070050 mMinDelay = hwSensor->minDelay;
Mathias Agopiana7352c92010-07-14 23:41:37 -070051}
52
Mathias Agopian589ce852010-07-13 22:21:56 -070053Sensor::~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
Mathias Agopiana48bcf62010-07-29 16:51:38 -070089int32_t Sensor::getMinDelay() const {
90 return mMinDelay;
91}
92
Mathias Agopianb62013f2011-05-17 22:54:42 -070093nsecs_t Sensor::getMinDelayNs() const {
94 return getMinDelay() * 1000;
95}
96
97int32_t Sensor::getVersion() const {
98 return mVersion;
99}
100
Mathias Agopiane1424282013-07-29 21:24:40 -0700101size_t Sensor::getFlattenedSize() const
Mathias Agopian589ce852010-07-13 22:21:56 -0700102{
Mathias Agopiane1424282013-07-29 21:24:40 -0700103 size_t fixedSize =
Mathias Agopian2ebc4d62012-05-04 15:47:13 -0700104 sizeof(int32_t) * 3 +
Mathias Agopiana48bcf62010-07-29 16:51:38 -0700105 sizeof(float) * 4 +
106 sizeof(int32_t);
Mathias Agopiane1424282013-07-29 21:24:40 -0700107
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;
Mathias Agopian589ce852010-07-13 22:21:56 -0700113}
114
Mathias Agopiane1424282013-07-29 21:24:40 -0700115status_t Sensor::flatten(void* buffer, size_t size) const {
116 if (size < getFlattenedSize()) {
117 return NO_MEMORY;
118 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700119
Mathias Agopiane1424282013-07-29 21:24:40 -0700120 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()));
Mathias Agopian589ce852010-07-13 22:21:56 -0700123
Mathias Agopiane1424282013-07-29 21:24:40 -0700124 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()));
Mathias Agopian589ce852010-07-13 22:21:56 -0700127
Mathias Agopiane1424282013-07-29 21:24:40 -0700128 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);
Mathias Agopian589ce852010-07-13 22:21:56 -0700136 return NO_ERROR;
137}
138
Mathias Agopiane1424282013-07-29 21:24:40 -0700139status_t Sensor::unflatten(void const* buffer, size_t size) {
140 size_t len;
Mathias Agopian589ce852010-07-13 22:21:56 -0700141
Mathias Agopiane1424282013-07-29 21:24:40 -0700142 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));
Mathias Agopian589ce852010-07-13 22:21:56 -0700151
Mathias Agopian589ce852010-07-13 22:21:56 -0700152
Mathias Agopiane1424282013-07-29 21:24:40 -0700153 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);
Mathias Agopian589ce852010-07-13 22:21:56 -0700180 return NO_ERROR;
181}
182
183// ----------------------------------------------------------------------------
184}; // namespace android