blob: 5cc76b4cd49f82402cb5992dd5735ee960f42a9b [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 Agopian589ce852010-07-13 22:21:56 -0700101size_t Sensor::getFlattenedSize() const
102{
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
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);
Mathias Agopian2ebc4d62012-05-04 15:47:13 -0700144 offset += write(buffer, offset, mVersion);
Mathias Agopian589ce852010-07-13 22:21:56 -0700145 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);
Mathias Agopiana48bcf62010-07-29 16:51:38 -0700151 offset += write(buffer, offset, mMinDelay);
Mathias Agopian589ce852010-07-13 22:21:56 -0700152
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);
Mathias Agopian2ebc4d62012-05-04 15:47:13 -0700183 offset += read(buffer, offset, &mVersion);
Mathias Agopian589ce852010-07-13 22:21:56 -0700184 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);
Mathias Agopiana48bcf62010-07-29 16:51:38 -0700190 offset += read(buffer, offset, &mMinDelay);
Mathias Agopian589ce852010-07-13 22:21:56 -0700191
192 return NO_ERROR;
193}
194
195// ----------------------------------------------------------------------------
196}; // namespace android