1 /*
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 <inttypes.h>
18 #include <stdint.h>
19 #include <sys/types.h>
20 #include <sys/limits.h>
21
22 #include <utils/Errors.h>
23 #include <utils/String8.h>
24 #include <utils/Flattenable.h>
25
26 #include <hardware/sensors.h>
27
28 #include <gui/Sensor.h>
29 #include <log/log.h>
30
31 // ----------------------------------------------------------------------------
32 namespace android {
33 // ----------------------------------------------------------------------------
34
Sensor()35 Sensor::Sensor()
36 : mHandle(0), mType(0),
37 mMinValue(0), mMaxValue(0), mResolution(0),
38 mPower(0), mMinDelay(0), mFifoReservedEventCount(0), mFifoMaxEventCount(0),
39 mMaxDelay(0), mFlags(0)
40 {
41 }
42
Sensor(struct sensor_t const * hwSensor,int halVersion)43 Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion)
44 {
45 mName = hwSensor->name;
46 mVendor = hwSensor->vendor;
47 mVersion = hwSensor->version;
48 mHandle = hwSensor->handle;
49 mType = hwSensor->type;
50 mMinValue = 0; // FIXME: minValue
51 mMaxValue = hwSensor->maxRange; // FIXME: maxValue
52 mResolution = hwSensor->resolution;
53 mPower = hwSensor->power;
54 mMinDelay = hwSensor->minDelay;
55 mFlags = 0;
56
57 // Set fifo event count zero for older devices which do not support batching. Fused
58 // sensors also have their fifo counts set to zero.
59 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) {
60 mFifoReservedEventCount = hwSensor->fifoReservedEventCount;
61 mFifoMaxEventCount = hwSensor->fifoMaxEventCount;
62 } else {
63 mFifoReservedEventCount = 0;
64 mFifoMaxEventCount = 0;
65 }
66
67 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
68 if (hwSensor->maxDelay > INT_MAX) {
69 // Max delay is declared as a 64 bit integer for 64 bit architectures. But it should
70 // always fit in a 32 bit integer, log error and cap it to INT_MAX.
71 ALOGE("Sensor maxDelay overflow error %s %" PRId64, mName.string(),
72 static_cast<int64_t>(hwSensor->maxDelay));
73 mMaxDelay = INT_MAX;
74 } else {
75 mMaxDelay = (int32_t) hwSensor->maxDelay;
76 }
77 } else {
78 // For older hals set maxDelay to 0.
79 mMaxDelay = 0;
80 }
81
82 // Ensure existing sensors have correct string type, required permissions and reporting mode.
83 // Set reportingMode for all android defined sensor types, set wake-up flag only for proximity
84 // sensor, significant motion, tilt, pick_up gesture, wake gesture and glance gesture on older
85 // HALs. Newer HALs can define both wake-up and non wake-up proximity sensors.
86 // All the OEM defined defined sensors have flags set to whatever is provided by the HAL.
87 switch (mType) {
88 case SENSOR_TYPE_ACCELEROMETER:
89 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER;
90 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
91 break;
92 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
93 mStringType = SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
94 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
95 break;
96 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
97 mStringType = SENSOR_STRING_TYPE_GAME_ROTATION_VECTOR;
98 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
99 break;
100 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
101 mStringType = SENSOR_STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR;
102 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
103 break;
104 case SENSOR_TYPE_GRAVITY:
105 mStringType = SENSOR_STRING_TYPE_GRAVITY;
106 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
107 break;
108 case SENSOR_TYPE_GYROSCOPE:
109 mStringType = SENSOR_STRING_TYPE_GYROSCOPE;
110 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
111 break;
112 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
113 mStringType = SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
114 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
115 break;
116 case SENSOR_TYPE_HEART_RATE:
117 mStringType = SENSOR_STRING_TYPE_HEART_RATE;
118 mRequiredPermission = SENSOR_PERMISSION_BODY_SENSORS;
119 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
120 break;
121 case SENSOR_TYPE_LIGHT:
122 mStringType = SENSOR_STRING_TYPE_LIGHT;
123 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
124 break;
125 case SENSOR_TYPE_LINEAR_ACCELERATION:
126 mStringType = SENSOR_STRING_TYPE_LINEAR_ACCELERATION;
127 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
128 break;
129 case SENSOR_TYPE_MAGNETIC_FIELD:
130 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD;
131 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
132 break;
133 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
134 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
135 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
136 break;
137 case SENSOR_TYPE_ORIENTATION:
138 mStringType = SENSOR_STRING_TYPE_ORIENTATION;
139 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
140 break;
141 case SENSOR_TYPE_PRESSURE:
142 mStringType = SENSOR_STRING_TYPE_PRESSURE;
143 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
144 break;
145 case SENSOR_TYPE_PROXIMITY:
146 mStringType = SENSOR_STRING_TYPE_PROXIMITY;
147 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
148 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
149 mFlags |= SENSOR_FLAG_WAKE_UP;
150 }
151 break;
152 case SENSOR_TYPE_RELATIVE_HUMIDITY:
153 mStringType = SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
154 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
155 break;
156 case SENSOR_TYPE_ROTATION_VECTOR:
157 mStringType = SENSOR_STRING_TYPE_ROTATION_VECTOR;
158 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
159 break;
160 case SENSOR_TYPE_SIGNIFICANT_MOTION:
161 mStringType = SENSOR_STRING_TYPE_SIGNIFICANT_MOTION;
162 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
163 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
164 mFlags |= SENSOR_FLAG_WAKE_UP;
165 }
166 break;
167 case SENSOR_TYPE_STEP_COUNTER:
168 mStringType = SENSOR_STRING_TYPE_STEP_COUNTER;
169 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
170 break;
171 case SENSOR_TYPE_STEP_DETECTOR:
172 mStringType = SENSOR_STRING_TYPE_STEP_DETECTOR;
173 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
174 break;
175 case SENSOR_TYPE_TEMPERATURE:
176 mStringType = SENSOR_STRING_TYPE_TEMPERATURE;
177 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
178 break;
179 case SENSOR_TYPE_TILT_DETECTOR:
180 mStringType = SENSOR_STRING_TYPE_TILT_DETECTOR;
181 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
182 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
183 mFlags |= SENSOR_FLAG_WAKE_UP;
184 }
185 break;
186 case SENSOR_TYPE_WAKE_GESTURE:
187 mStringType = SENSOR_STRING_TYPE_WAKE_GESTURE;
188 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
189 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
190 mFlags |= SENSOR_FLAG_WAKE_UP;
191 }
192 break;
193 case SENSOR_TYPE_GLANCE_GESTURE:
194 mStringType = SENSOR_STRING_TYPE_GLANCE_GESTURE;
195 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
196 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
197 mFlags |= SENSOR_FLAG_WAKE_UP;
198 }
199 break;
200 case SENSOR_TYPE_PICK_UP_GESTURE:
201 mStringType = SENSOR_STRING_TYPE_PICK_UP_GESTURE;
202 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
203 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
204 mFlags |= SENSOR_FLAG_WAKE_UP;
205 }
206 break;
207 default:
208 // Only pipe the stringType, requiredPermission and flags for custom sensors.
209 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor->stringType) {
210 mStringType = hwSensor->stringType;
211 }
212 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor->requiredPermission) {
213 mRequiredPermission = hwSensor->requiredPermission;
214 }
215
216 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
217 mFlags = (int32_t) hwSensor->flags;
218 } else {
219 // This is an OEM defined sensor on an older HAL. Use minDelay to determine the
220 // reporting mode of the sensor.
221 if (mMinDelay > 0) {
222 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
223 } else if (mMinDelay == 0) {
224 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
225 } else if (mMinDelay < 0) {
226 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
227 }
228 }
229 break;
230 }
231
232 // For the newer HALs log errors if reporting mask flags are set incorrectly.
233 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
234 // Wake-up flag is set here.
235 mFlags |= (hwSensor->flags & SENSOR_FLAG_WAKE_UP);
236 if (mFlags != hwSensor->flags) {
237 int actualReportingMode =
238 (hwSensor->flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
239 int expectedReportingMode = (mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
240 if (actualReportingMode != expectedReportingMode) {
241 ALOGE("Reporting Mode incorrect: sensor %s handle=%d type=%d "
242 "actual=%d expected=%d",
243 mName.string(), mHandle, mType, actualReportingMode, expectedReportingMode);
244 }
245
246 }
247 }
248 }
249
~Sensor()250 Sensor::~Sensor()
251 {
252 }
253
getName() const254 const String8& Sensor::getName() const {
255 return mName;
256 }
257
getVendor() const258 const String8& Sensor::getVendor() const {
259 return mVendor;
260 }
261
getHandle() const262 int32_t Sensor::getHandle() const {
263 return mHandle;
264 }
265
getType() const266 int32_t Sensor::getType() const {
267 return mType;
268 }
269
getMinValue() const270 float Sensor::getMinValue() const {
271 return mMinValue;
272 }
273
getMaxValue() const274 float Sensor::getMaxValue() const {
275 return mMaxValue;
276 }
277
getResolution() const278 float Sensor::getResolution() const {
279 return mResolution;
280 }
281
getPowerUsage() const282 float Sensor::getPowerUsage() const {
283 return mPower;
284 }
285
getMinDelay() const286 int32_t Sensor::getMinDelay() const {
287 return mMinDelay;
288 }
289
getMinDelayNs() const290 nsecs_t Sensor::getMinDelayNs() const {
291 return getMinDelay() * 1000;
292 }
293
getVersion() const294 int32_t Sensor::getVersion() const {
295 return mVersion;
296 }
297
getFifoReservedEventCount() const298 int32_t Sensor::getFifoReservedEventCount() const {
299 return mFifoReservedEventCount;
300 }
301
getFifoMaxEventCount() const302 int32_t Sensor::getFifoMaxEventCount() const {
303 return mFifoMaxEventCount;
304 }
305
getStringType() const306 const String8& Sensor::getStringType() const {
307 return mStringType;
308 }
309
getRequiredPermission() const310 const String8& Sensor::getRequiredPermission() const {
311 return mRequiredPermission;
312 }
313
getMaxDelay() const314 int32_t Sensor::getMaxDelay() const {
315 return mMaxDelay;
316 }
317
getFlags() const318 int32_t Sensor::getFlags() const {
319 return mFlags;
320 }
321
isWakeUpSensor() const322 bool Sensor::isWakeUpSensor() const {
323 return mFlags & SENSOR_FLAG_WAKE_UP;
324 }
325
getReportingMode() const326 int32_t Sensor::getReportingMode() const {
327 return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT);
328 }
329
getFlattenedSize() const330 size_t Sensor::getFlattenedSize() const
331 {
332 size_t fixedSize =
333 sizeof(int32_t) * 3 +
334 sizeof(float) * 4 +
335 sizeof(int32_t) * 5;
336
337 size_t variableSize =
338 sizeof(uint32_t) + FlattenableUtils::align<4>(mName.length()) +
339 sizeof(uint32_t) + FlattenableUtils::align<4>(mVendor.length()) +
340 sizeof(uint32_t) + FlattenableUtils::align<4>(mStringType.length()) +
341 sizeof(uint32_t) + FlattenableUtils::align<4>(mRequiredPermission.length());
342
343 return fixedSize + variableSize;
344 }
345
flatten(void * buffer,size_t size) const346 status_t Sensor::flatten(void* buffer, size_t size) const {
347 if (size < getFlattenedSize()) {
348 return NO_MEMORY;
349 }
350
351 flattenString8(buffer, size, mName);
352 flattenString8(buffer, size, mVendor);
353 FlattenableUtils::write(buffer, size, mVersion);
354 FlattenableUtils::write(buffer, size, mHandle);
355 FlattenableUtils::write(buffer, size, mType);
356 FlattenableUtils::write(buffer, size, mMinValue);
357 FlattenableUtils::write(buffer, size, mMaxValue);
358 FlattenableUtils::write(buffer, size, mResolution);
359 FlattenableUtils::write(buffer, size, mPower);
360 FlattenableUtils::write(buffer, size, mMinDelay);
361 FlattenableUtils::write(buffer, size, mFifoReservedEventCount);
362 FlattenableUtils::write(buffer, size, mFifoMaxEventCount);
363 flattenString8(buffer, size, mStringType);
364 flattenString8(buffer, size, mRequiredPermission);
365 FlattenableUtils::write(buffer, size, mMaxDelay);
366 FlattenableUtils::write(buffer, size, mFlags);
367 return NO_ERROR;
368 }
369
unflatten(void const * buffer,size_t size)370 status_t Sensor::unflatten(void const* buffer, size_t size) {
371 if (!unflattenString8(buffer, size, mName)) {
372 return NO_MEMORY;
373 }
374 if (!unflattenString8(buffer, size, mVendor)) {
375 return NO_MEMORY;
376 }
377
378 size_t fixedSize =
379 sizeof(int32_t) * 3 +
380 sizeof(float) * 4 +
381 sizeof(int32_t) * 5;
382 if (size < fixedSize) {
383 return NO_MEMORY;
384 }
385
386 FlattenableUtils::read(buffer, size, mVersion);
387 FlattenableUtils::read(buffer, size, mHandle);
388 FlattenableUtils::read(buffer, size, mType);
389 FlattenableUtils::read(buffer, size, mMinValue);
390 FlattenableUtils::read(buffer, size, mMaxValue);
391 FlattenableUtils::read(buffer, size, mResolution);
392 FlattenableUtils::read(buffer, size, mPower);
393 FlattenableUtils::read(buffer, size, mMinDelay);
394 FlattenableUtils::read(buffer, size, mFifoReservedEventCount);
395 FlattenableUtils::read(buffer, size, mFifoMaxEventCount);
396
397 if (!unflattenString8(buffer, size, mStringType)) {
398 return NO_MEMORY;
399 }
400 if (!unflattenString8(buffer, size, mRequiredPermission)) {
401 return NO_MEMORY;
402 }
403 FlattenableUtils::read(buffer, size, mMaxDelay);
404 FlattenableUtils::read(buffer, size, mFlags);
405 return NO_ERROR;
406 }
407
flattenString8(void * & buffer,size_t & size,const String8 & string8)408 void Sensor::flattenString8(void*& buffer, size_t& size,
409 const String8& string8) {
410 uint32_t len = string8.length();
411 FlattenableUtils::write(buffer, size, len);
412 memcpy(static_cast<char*>(buffer), string8.string(), len);
413 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
414 }
415
unflattenString8(void const * & buffer,size_t & size,String8 & outputString8)416 bool Sensor::unflattenString8(void const*& buffer, size_t& size, String8& outputString8) {
417 uint32_t len;
418 if (size < sizeof(len)) {
419 return false;
420 }
421 FlattenableUtils::read(buffer, size, len);
422 if (size < len) {
423 return false;
424 }
425 outputString8.setTo(static_cast<char const*>(buffer), len);
426 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
427 return true;
428 }
429
430 // ----------------------------------------------------------------------------
431 }; // namespace android
432