2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
20 #include <utils/Errors.h>
21 #include <utils/String8.h>
22 #include <utils/Flattenable.h>
24 #include <hardware/sensors.h>
26 #include <gui/Sensor.h>
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
33 : mHandle(0), mType(0),
34 mMinValue(0), mMaxValue(0), mResolution(0),
35 mPower(0), mMinDelay(0), mFifoReservedEventCount(0), mFifoMaxEventCount(0)
39 Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion)
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;
52 // Set fifo event count zero for older devices which do not support batching. Fused
53 // sensors also have their fifo counts set to zero.
54 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
55 mFifoReservedEventCount = hwSensor->fifoReservedEventCount;
56 mFifoMaxEventCount = hwSensor->fifoMaxEventCount;
58 mFifoReservedEventCount = 0;
59 mFifoMaxEventCount = 0;
62 // Ensure existing sensors have correct string type and required
65 case SENSOR_TYPE_ACCELEROMETER:
66 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER;
68 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
69 mStringType = SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
71 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
72 mStringType = SENSOR_STRING_TYPE_GAME_ROTATION_VECTOR;
74 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
75 mStringType = SENSOR_STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR;
77 case SENSOR_TYPE_GRAVITY:
78 mStringType = SENSOR_STRING_TYPE_GRAVITY;
80 case SENSOR_TYPE_GYROSCOPE:
81 mStringType = SENSOR_STRING_TYPE_GYROSCOPE;
83 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
84 mStringType = SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
86 case SENSOR_TYPE_HEART_RATE:
87 mStringType = SENSOR_STRING_TYPE_HEART_RATE;
88 mRequiredPermission = SENSOR_PERMISSION_BODY_SENSORS;
90 case SENSOR_TYPE_LIGHT:
91 mStringType = SENSOR_STRING_TYPE_LIGHT;
93 case SENSOR_TYPE_LINEAR_ACCELERATION:
94 mStringType = SENSOR_STRING_TYPE_LINEAR_ACCELERATION;
96 case SENSOR_TYPE_MAGNETIC_FIELD:
97 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD;
99 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
100 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
102 case SENSOR_TYPE_ORIENTATION:
103 mStringType = SENSOR_STRING_TYPE_ORIENTATION;
105 case SENSOR_TYPE_PRESSURE:
106 mStringType = SENSOR_STRING_TYPE_PRESSURE;
108 case SENSOR_TYPE_PROXIMITY:
109 mStringType = SENSOR_STRING_TYPE_PROXIMITY;
111 case SENSOR_TYPE_RELATIVE_HUMIDITY:
112 mStringType = SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
114 case SENSOR_TYPE_ROTATION_VECTOR:
115 mStringType = SENSOR_STRING_TYPE_ROTATION_VECTOR;
117 case SENSOR_TYPE_SIGNIFICANT_MOTION:
118 mStringType = SENSOR_STRING_TYPE_SIGNIFICANT_MOTION;
120 case SENSOR_TYPE_STEP_COUNTER:
121 mStringType = SENSOR_STRING_TYPE_STEP_COUNTER;
123 case SENSOR_TYPE_STEP_DETECTOR:
124 mStringType = SENSOR_STRING_TYPE_STEP_DETECTOR;
126 case SENSOR_TYPE_TEMPERATURE:
127 mStringType = SENSOR_STRING_TYPE_TEMPERATURE;
130 // Only pipe the stringType and requiredPermission for custom sensors.
131 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_2 && hwSensor->stringType) {
132 mStringType = hwSensor->stringType;
134 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_2 && hwSensor->requiredPermission) {
135 mRequiredPermission = hwSensor->requiredPermission;
145 const String8& Sensor::getName() const {
149 const String8& Sensor::getVendor() const {
153 int32_t Sensor::getHandle() const {
157 int32_t Sensor::getType() const {
161 float Sensor::getMinValue() const {
165 float Sensor::getMaxValue() const {
169 float Sensor::getResolution() const {
173 float Sensor::getPowerUsage() const {
177 int32_t Sensor::getMinDelay() const {
181 nsecs_t Sensor::getMinDelayNs() const {
182 return getMinDelay() * 1000;
185 int32_t Sensor::getVersion() const {
189 int32_t Sensor::getFifoReservedEventCount() const {
190 return mFifoReservedEventCount;
193 int32_t Sensor::getFifoMaxEventCount() const {
194 return mFifoMaxEventCount;
197 const String8& Sensor::getStringType() const {
201 const String8& Sensor::getRequiredPermission() const {
202 return mRequiredPermission;
205 size_t Sensor::getFlattenedSize() const
208 sizeof(int32_t) * 3 +
212 size_t variableSize =
213 sizeof(uint32_t) + FlattenableUtils::align<4>(mName.length()) +
214 sizeof(uint32_t) + FlattenableUtils::align<4>(mVendor.length()) +
215 sizeof(uint32_t) + FlattenableUtils::align<4>(mStringType.length()) +
216 sizeof(uint32_t) + FlattenableUtils::align<4>(mRequiredPermission.length());
218 return fixedSize + variableSize;
221 status_t Sensor::flatten(void* buffer, size_t size) const {
222 if (size < getFlattenedSize()) {
226 flattenString8(buffer, size, mName);
227 flattenString8(buffer, size, mVendor);
228 FlattenableUtils::write(buffer, size, mVersion);
229 FlattenableUtils::write(buffer, size, mHandle);
230 FlattenableUtils::write(buffer, size, mType);
231 FlattenableUtils::write(buffer, size, mMinValue);
232 FlattenableUtils::write(buffer, size, mMaxValue);
233 FlattenableUtils::write(buffer, size, mResolution);
234 FlattenableUtils::write(buffer, size, mPower);
235 FlattenableUtils::write(buffer, size, mMinDelay);
236 FlattenableUtils::write(buffer, size, mFifoReservedEventCount);
237 FlattenableUtils::write(buffer, size, mFifoMaxEventCount);
238 flattenString8(buffer, size, mStringType);
239 flattenString8(buffer, size, mRequiredPermission);
243 status_t Sensor::unflatten(void const* buffer, size_t size) {
244 if (!unflattenString8(buffer, size, mName)) {
247 if (!unflattenString8(buffer, size, mVendor)) {
252 sizeof(int32_t) * 3 +
255 if (size < fixedSize) {
259 FlattenableUtils::read(buffer, size, mVersion);
260 FlattenableUtils::read(buffer, size, mHandle);
261 FlattenableUtils::read(buffer, size, mType);
262 FlattenableUtils::read(buffer, size, mMinValue);
263 FlattenableUtils::read(buffer, size, mMaxValue);
264 FlattenableUtils::read(buffer, size, mResolution);
265 FlattenableUtils::read(buffer, size, mPower);
266 FlattenableUtils::read(buffer, size, mMinDelay);
267 FlattenableUtils::read(buffer, size, mFifoReservedEventCount);
268 FlattenableUtils::read(buffer, size, mFifoMaxEventCount);
270 if (!unflattenString8(buffer, size, mStringType)) {
273 if (!unflattenString8(buffer, size, mRequiredPermission)) {
279 void Sensor::flattenString8(void*& buffer, size_t& size,
280 const String8& string8) {
281 uint32_t len = string8.length();
282 FlattenableUtils::write(buffer, size, len);
283 memcpy(static_cast<char*>(buffer), string8.string(), len);
284 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
287 bool Sensor::unflattenString8(void const*& buffer, size_t& size, String8& outputString8) {
289 if (size < sizeof(len)) {
292 FlattenableUtils::read(buffer, size, len);
296 outputString8.setTo(static_cast<char const*>(buffer), len);
297 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
301 // ----------------------------------------------------------------------------
302 }; // namespace android