blob: c52a88f120ac8f6fce180d7bc56bac7ddc34484c [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 Agopian8683fca2012-08-12 19:37:16 -0700101size_t Sensor::getSize() const
Mathias Agopian589ce852010-07-13 22:21:56 -0700102{
103 return sizeof(int32_t) + ((mName.length() + 3) & ~3) +
104 sizeof(int32_t) + ((mVendor.length() + 3) & ~3) +
Mathias Agopian2ebc4d62012-05-04 15:47:13 -0700105 sizeof(int32_t) * 3 +
Mathias Agopiana48bcf62010-07-29 16:51:38 -0700106 sizeof(float) * 4 +
107 sizeof(int32_t);
Mathias Agopian589ce852010-07-13 22:21:56 -0700108}
109
Mathias Agopian589ce852010-07-13 22:21:56 -0700110static inline
111size_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
116static inline
117size_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
122static inline
123size_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
Mathias Agopian8683fca2012-08-12 19:37:16 -0700128status_t Sensor::flatten(void* buffer) const
Mathias Agopian589ce852010-07-13 22:21:56 -0700129{
Mathias Agopian589ce852010-07-13 22:21:56 -0700130 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);
Mathias Agopian2ebc4d62012-05-04 15:47:13 -0700135 offset += write(buffer, offset, mVersion);
Mathias Agopian589ce852010-07-13 22:21:56 -0700136 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);
Mathias Agopiana48bcf62010-07-29 16:51:38 -0700142 offset += write(buffer, offset, mMinDelay);
Mathias Agopian589ce852010-07-13 22:21:56 -0700143 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
Mathias Agopian8683fca2012-08-12 19:37:16 -0700164status_t Sensor::unflatten(void const* buffer, size_t size)
Mathias Agopian589ce852010-07-13 22:21:56 -0700165{
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);
Mathias Agopian2ebc4d62012-05-04 15:47:13 -0700172 offset += read(buffer, offset, &mVersion);
Mathias Agopian589ce852010-07-13 22:21:56 -0700173 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);
Mathias Agopiana48bcf62010-07-29 16:51:38 -0700179 offset += read(buffer, offset, &mMinDelay);
Mathias Agopian589ce852010-07-13 22:21:56 -0700180 return NO_ERROR;
181}
182
183// ----------------------------------------------------------------------------
184}; // namespace android