blob: f9a2c046d7f39516c8f25328094f4cacda1e6e01 [file] [log] [blame]
Mathias Agopianb957b9d2010-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 Agopian050b5622010-07-29 16:51:38 -070035 mPower(0), mMinDelay(0)
Mathias Agopianb957b9d2010-07-13 22:21:56 -070036{
37}
38
Mathias Agopian1bf79782010-07-14 23:41:37 -070039Sensor::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;
Mathias Agopian050b5622010-07-29 16:51:38 -070049 mMinDelay = hwSensor->minDelay;
Mathias Agopian1bf79782010-07-14 23:41:37 -070050}
51
Mathias Agopianb957b9d2010-07-13 22:21:56 -070052Sensor::~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
Mathias Agopian050b5622010-07-29 16:51:38 -070088int32_t Sensor::getMinDelay() const {
89 return mMinDelay;
90}
91
Mathias Agopian73e0bc82011-05-17 22:54:42 -070092nsecs_t Sensor::getMinDelayNs() const {
93 return getMinDelay() * 1000;
94}
95
96int32_t Sensor::getVersion() const {
97 return mVersion;
98}
99
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700100size_t Sensor::getFlattenedSize() const
101{
102 return sizeof(int32_t) + ((mName.length() + 3) & ~3) +
103 sizeof(int32_t) + ((mVendor.length() + 3) & ~3) +
104 sizeof(int32_t) * 2 +
Mathias Agopian050b5622010-07-29 16:51:38 -0700105 sizeof(float) * 4 +
106 sizeof(int32_t);
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700107}
108
109size_t Sensor::getFdCount() const
110{
111 return 0;
112}
113
114static inline
115size_t write(void* buffer, size_t offset, const String8& value) {
116 memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length());
117 return (value.length() + 3) & ~3;
118}
119
120static inline
121size_t write(void* buffer, size_t offset, float value) {
122 *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value;
123 return sizeof(float);
124}
125
126static inline
127size_t write(void* buffer, size_t offset, int32_t value) {
128 *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value;
129 return sizeof(int32_t);
130}
131
132status_t Sensor::flatten(void* buffer, size_t size,
133 int fds[], size_t count) const
134{
135 if (size < Sensor::getFlattenedSize())
136 return -ENOMEM;
137
138 size_t offset = 0;
139 offset += write(buffer, offset, int32_t(mName.length()));
140 offset += write(buffer, offset, mName);
141 offset += write(buffer, offset, int32_t(mVendor.length()));
142 offset += write(buffer, offset, mVendor);
143 offset += write(buffer, offset, mHandle);
144 offset += write(buffer, offset, mType);
145 offset += write(buffer, offset, mMinValue);
146 offset += write(buffer, offset, mMaxValue);
147 offset += write(buffer, offset, mResolution);
148 offset += write(buffer, offset, mPower);
Mathias Agopian050b5622010-07-29 16:51:38 -0700149 offset += write(buffer, offset, mMinDelay);
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700150
151 return NO_ERROR;
152}
153
154static inline
155size_t read(void const* buffer, size_t offset, String8* value, int32_t len) {
156 value->setTo(static_cast<char const*>(buffer) + offset, len);
157 return (len + 3) & ~3;
158}
159
160static inline
161size_t read(void const* buffer, size_t offset, float* value) {
162 *value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset);
163 return sizeof(float);
164}
165
166static inline
167size_t read(void const* buffer, size_t offset, int32_t* value) {
168 *value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset);
169 return sizeof(int32_t);
170}
171
172status_t Sensor::unflatten(void const* buffer, size_t size,
173 int fds[], size_t count)
174{
175 int32_t len;
176 size_t offset = 0;
177 offset += read(buffer, offset, &len);
178 offset += read(buffer, offset, &mName, len);
179 offset += read(buffer, offset, &len);
180 offset += read(buffer, offset, &mVendor, len);
181 offset += read(buffer, offset, &mHandle);
182 offset += read(buffer, offset, &mType);
183 offset += read(buffer, offset, &mMinValue);
184 offset += read(buffer, offset, &mMaxValue);
185 offset += read(buffer, offset, &mResolution);
186 offset += read(buffer, offset, &mPower);
Mathias Agopian050b5622010-07-29 16:51:38 -0700187 offset += read(buffer, offset, &mMinDelay);
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700188
189 return NO_ERROR;
190}
191
192// ----------------------------------------------------------------------------
193}; // namespace android