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 <binder/AppOpsManager.h>
29 #include <binder/IServiceManager.h>
30
31 #include <gui/Sensor.h>
32 #include <log/log.h>
33
34 // ----------------------------------------------------------------------------
35 namespace android {
36 // ----------------------------------------------------------------------------
37
Sensor()38 Sensor::Sensor()
39 : mHandle(0), mType(0),
40 mMinValue(0), mMaxValue(0), mResolution(0),
41 mPower(0), mMinDelay(0), mFifoReservedEventCount(0), mFifoMaxEventCount(0),
42 mMaxDelay(0), mFlags(0)
43 {
44 }
45
Sensor(struct sensor_t const * hwSensor,int halVersion)46 Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion)
47 {
48 mName = hwSensor->name;
49 mVendor = hwSensor->vendor;
50 mVersion = hwSensor->version;
51 mHandle = hwSensor->handle;
52 mType = hwSensor->type;
53 mMinValue = 0; // FIXME: minValue
54 mMaxValue = hwSensor->maxRange; // FIXME: maxValue
55 mResolution = hwSensor->resolution;
56 mPower = hwSensor->power;
57 mMinDelay = hwSensor->minDelay;
58 mFlags = 0;
59
60 // Set fifo event count zero for older devices which do not support batching. Fused
61 // sensors also have their fifo counts set to zero.
62 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) {
63 mFifoReservedEventCount = hwSensor->fifoReservedEventCount;
64 mFifoMaxEventCount = hwSensor->fifoMaxEventCount;
65 } else {
66 mFifoReservedEventCount = 0;
67 mFifoMaxEventCount = 0;
68 }
69
70 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
71 if (hwSensor->maxDelay > INT_MAX) {
72 // Max delay is declared as a 64 bit integer for 64 bit architectures. But it should
73 // always fit in a 32 bit integer, log error and cap it to INT_MAX.
74 ALOGE("Sensor maxDelay overflow error %s %" PRId64, mName.string(),
75 static_cast<int64_t>(hwSensor->maxDelay));
76 mMaxDelay = INT_MAX;
77 } else {
78 mMaxDelay = static_cast<int32_t>(hwSensor->maxDelay);
79 }
80 } else {
81 // For older hals set maxDelay to 0.
82 mMaxDelay = 0;
83 }
84
85 // Ensure existing sensors have correct string type, required permissions and reporting mode.
86 // Set reportingMode for all android defined sensor types, set wake-up flag only for proximity
87 // sensor, significant motion, tilt, pick_up gesture, wake gesture and glance gesture on older
88 // HALs. Newer HALs can define both wake-up and non wake-up proximity sensors.
89 // All the OEM defined defined sensors have flags set to whatever is provided by the HAL.
90 switch (mType) {
91 case SENSOR_TYPE_ACCELEROMETER:
92 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER;
93 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
94 break;
95 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
96 mStringType = SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
97 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
98 break;
99 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
100 mStringType = SENSOR_STRING_TYPE_GAME_ROTATION_VECTOR;
101 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
102 break;
103 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
104 mStringType = SENSOR_STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR;
105 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
106 break;
107 case SENSOR_TYPE_GRAVITY:
108 mStringType = SENSOR_STRING_TYPE_GRAVITY;
109 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
110 break;
111 case SENSOR_TYPE_GYROSCOPE:
112 mStringType = SENSOR_STRING_TYPE_GYROSCOPE;
113 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
114 break;
115 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
116 mStringType = SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
117 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
118 break;
119 case SENSOR_TYPE_HEART_RATE: {
120 mStringType = SENSOR_STRING_TYPE_HEART_RATE;
121 mRequiredPermission = SENSOR_PERMISSION_BODY_SENSORS;
122 AppOpsManager appOps;
123 mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS));
124 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
125 } break;
126 case SENSOR_TYPE_LIGHT:
127 mStringType = SENSOR_STRING_TYPE_LIGHT;
128 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
129 break;
130 case SENSOR_TYPE_LINEAR_ACCELERATION:
131 mStringType = SENSOR_STRING_TYPE_LINEAR_ACCELERATION;
132 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
133 break;
134 case SENSOR_TYPE_MAGNETIC_FIELD:
135 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD;
136 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
137 break;
138 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
139 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
140 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
141 break;
142 case SENSOR_TYPE_ORIENTATION:
143 mStringType = SENSOR_STRING_TYPE_ORIENTATION;
144 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
145 break;
146 case SENSOR_TYPE_PRESSURE:
147 mStringType = SENSOR_STRING_TYPE_PRESSURE;
148 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
149 break;
150 case SENSOR_TYPE_PROXIMITY:
151 mStringType = SENSOR_STRING_TYPE_PROXIMITY;
152 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
153 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
154 mFlags |= SENSOR_FLAG_WAKE_UP;
155 }
156 break;
157 case SENSOR_TYPE_RELATIVE_HUMIDITY:
158 mStringType = SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
159 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
160 break;
161 case SENSOR_TYPE_ROTATION_VECTOR:
162 mStringType = SENSOR_STRING_TYPE_ROTATION_VECTOR;
163 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
164 break;
165 case SENSOR_TYPE_SIGNIFICANT_MOTION:
166 mStringType = SENSOR_STRING_TYPE_SIGNIFICANT_MOTION;
167 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
168 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
169 mFlags |= SENSOR_FLAG_WAKE_UP;
170 }
171 break;
172 case SENSOR_TYPE_STEP_COUNTER:
173 mStringType = SENSOR_STRING_TYPE_STEP_COUNTER;
174 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
175 break;
176 case SENSOR_TYPE_STEP_DETECTOR:
177 mStringType = SENSOR_STRING_TYPE_STEP_DETECTOR;
178 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
179 break;
180 case SENSOR_TYPE_TEMPERATURE:
181 mStringType = SENSOR_STRING_TYPE_TEMPERATURE;
182 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
183 break;
184 case SENSOR_TYPE_TILT_DETECTOR:
185 mStringType = SENSOR_STRING_TYPE_TILT_DETECTOR;
186 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
187 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
188 mFlags |= SENSOR_FLAG_WAKE_UP;
189 }
190 break;
191 case SENSOR_TYPE_WAKE_GESTURE:
192 mStringType = SENSOR_STRING_TYPE_WAKE_GESTURE;
193 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
194 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
195 mFlags |= SENSOR_FLAG_WAKE_UP;
196 }
197 break;
198 case SENSOR_TYPE_GLANCE_GESTURE:
199 mStringType = SENSOR_STRING_TYPE_GLANCE_GESTURE;
200 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
201 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
202 mFlags |= SENSOR_FLAG_WAKE_UP;
203 }
204 break;
205 case SENSOR_TYPE_PICK_UP_GESTURE:
206 mStringType = SENSOR_STRING_TYPE_PICK_UP_GESTURE;
207 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
208 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
209 mFlags |= SENSOR_FLAG_WAKE_UP;
210 }
211 break;
212 case SENSOR_TYPE_WRIST_TILT_GESTURE:
213 mStringType = SENSOR_STRING_TYPE_WRIST_TILT_GESTURE;
214 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
215 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
216 mFlags |= SENSOR_FLAG_WAKE_UP;
217 }
218 break;
219 default:
220 // Only pipe the stringType, requiredPermission and flags for custom sensors.
221 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor->stringType) {
222 mStringType = hwSensor->stringType;
223 }
224 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor->requiredPermission) {
225 mRequiredPermission = hwSensor->requiredPermission;
226 if (!strcmp(mRequiredPermission, SENSOR_PERMISSION_BODY_SENSORS)) {
227 AppOpsManager appOps;
228 mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS));
229 }
230 }
231
232 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
233 mFlags = static_cast<uint32_t>(hwSensor->flags);
234 } else {
235 // This is an OEM defined sensor on an older HAL. Use minDelay to determine the
236 // reporting mode of the sensor.
237 if (mMinDelay > 0) {
238 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
239 } else if (mMinDelay == 0) {
240 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
241 } else if (mMinDelay < 0) {
242 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
243 }
244 }
245 break;
246 }
247
248 // For the newer HALs log errors if reporting mask flags are set incorrectly.
249 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
250 // Wake-up flag is set here.
251 mFlags |= (hwSensor->flags & SENSOR_FLAG_WAKE_UP);
252 if (mFlags != hwSensor->flags) {
253 int actualReportingMode =
254 (hwSensor->flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
255 int expectedReportingMode = (mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
256 if (actualReportingMode != expectedReportingMode) {
257 ALOGE("Reporting Mode incorrect: sensor %s handle=%d type=%d "
258 "actual=%d expected=%d",
259 mName.string(), mHandle, mType, actualReportingMode, expectedReportingMode);
260 }
261
262 }
263 }
264
265 if (mRequiredPermission.length() > 0) {
266 // If the sensor is protected by a permission we need to know if it is
267 // a runtime one to determine whether we can use the permission cache.
268 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
269 if (binder != 0) {
270 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
271 mRequiredPermissionRuntime = permCtrl->isRuntimePermission(
272 String16(mRequiredPermission));
273 }
274 }
275 }
276
~Sensor()277 Sensor::~Sensor()
278 {
279 }
280
getName() const281 const String8& Sensor::getName() const {
282 return mName;
283 }
284
getVendor() const285 const String8& Sensor::getVendor() const {
286 return mVendor;
287 }
288
getHandle() const289 int32_t Sensor::getHandle() const {
290 return mHandle;
291 }
292
getType() const293 int32_t Sensor::getType() const {
294 return mType;
295 }
296
getMinValue() const297 float Sensor::getMinValue() const {
298 return mMinValue;
299 }
300
getMaxValue() const301 float Sensor::getMaxValue() const {
302 return mMaxValue;
303 }
304
getResolution() const305 float Sensor::getResolution() const {
306 return mResolution;
307 }
308
getPowerUsage() const309 float Sensor::getPowerUsage() const {
310 return mPower;
311 }
312
getMinDelay() const313 int32_t Sensor::getMinDelay() const {
314 return mMinDelay;
315 }
316
getMinDelayNs() const317 nsecs_t Sensor::getMinDelayNs() const {
318 return getMinDelay() * 1000;
319 }
320
getVersion() const321 int32_t Sensor::getVersion() const {
322 return mVersion;
323 }
324
getFifoReservedEventCount() const325 uint32_t Sensor::getFifoReservedEventCount() const {
326 return mFifoReservedEventCount;
327 }
328
getFifoMaxEventCount() const329 uint32_t Sensor::getFifoMaxEventCount() const {
330 return mFifoMaxEventCount;
331 }
332
getStringType() const333 const String8& Sensor::getStringType() const {
334 return mStringType;
335 }
336
getRequiredPermission() const337 const String8& Sensor::getRequiredPermission() const {
338 return mRequiredPermission;
339 }
340
isRequiredPermissionRuntime() const341 bool Sensor::isRequiredPermissionRuntime() const {
342 return mRequiredPermissionRuntime;
343 }
344
getRequiredAppOp() const345 int32_t Sensor::getRequiredAppOp() const {
346 return mRequiredAppOp;
347 }
348
getMaxDelay() const349 int32_t Sensor::getMaxDelay() const {
350 return mMaxDelay;
351 }
352
getFlags() const353 uint32_t Sensor::getFlags() const {
354 return mFlags;
355 }
356
isWakeUpSensor() const357 bool Sensor::isWakeUpSensor() const {
358 return mFlags & SENSOR_FLAG_WAKE_UP;
359 }
360
getReportingMode() const361 int32_t Sensor::getReportingMode() const {
362 return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT);
363 }
364
getFlattenedSize() const365 size_t Sensor::getFlattenedSize() const
366 {
367 size_t fixedSize =
368 sizeof(int32_t) * 3 +
369 sizeof(float) * 4 +
370 sizeof(int32_t) * 6 +
371 sizeof(bool);
372
373 size_t variableSize =
374 sizeof(uint32_t) + FlattenableUtils::align<4>(mName.length()) +
375 sizeof(uint32_t) + FlattenableUtils::align<4>(mVendor.length()) +
376 sizeof(uint32_t) + FlattenableUtils::align<4>(mStringType.length()) +
377 sizeof(uint32_t) + FlattenableUtils::align<4>(mRequiredPermission.length());
378
379 return fixedSize + variableSize;
380 }
381
flatten(void * buffer,size_t size) const382 status_t Sensor::flatten(void* buffer, size_t size) const {
383 if (size < getFlattenedSize()) {
384 return NO_MEMORY;
385 }
386
387 flattenString8(buffer, size, mName);
388 flattenString8(buffer, size, mVendor);
389 FlattenableUtils::write(buffer, size, mVersion);
390 FlattenableUtils::write(buffer, size, mHandle);
391 FlattenableUtils::write(buffer, size, mType);
392 FlattenableUtils::write(buffer, size, mMinValue);
393 FlattenableUtils::write(buffer, size, mMaxValue);
394 FlattenableUtils::write(buffer, size, mResolution);
395 FlattenableUtils::write(buffer, size, mPower);
396 FlattenableUtils::write(buffer, size, mMinDelay);
397 FlattenableUtils::write(buffer, size, mFifoReservedEventCount);
398 FlattenableUtils::write(buffer, size, mFifoMaxEventCount);
399 flattenString8(buffer, size, mStringType);
400 flattenString8(buffer, size, mRequiredPermission);
401 FlattenableUtils::write(buffer, size, mRequiredPermissionRuntime);
402 FlattenableUtils::write(buffer, size, mRequiredAppOp);
403 FlattenableUtils::write(buffer, size, mMaxDelay);
404 FlattenableUtils::write(buffer, size, mFlags);
405 return NO_ERROR;
406 }
407
unflatten(void const * buffer,size_t size)408 status_t Sensor::unflatten(void const* buffer, size_t size) {
409 if (!unflattenString8(buffer, size, mName)) {
410 return NO_MEMORY;
411 }
412 if (!unflattenString8(buffer, size, mVendor)) {
413 return NO_MEMORY;
414 }
415
416 size_t fixedSize =
417 sizeof(int32_t) * 3 +
418 sizeof(float) * 4 +
419 sizeof(int32_t) * 5;
420 if (size < fixedSize) {
421 return NO_MEMORY;
422 }
423
424 FlattenableUtils::read(buffer, size, mVersion);
425 FlattenableUtils::read(buffer, size, mHandle);
426 FlattenableUtils::read(buffer, size, mType);
427 FlattenableUtils::read(buffer, size, mMinValue);
428 FlattenableUtils::read(buffer, size, mMaxValue);
429 FlattenableUtils::read(buffer, size, mResolution);
430 FlattenableUtils::read(buffer, size, mPower);
431 FlattenableUtils::read(buffer, size, mMinDelay);
432 FlattenableUtils::read(buffer, size, mFifoReservedEventCount);
433 FlattenableUtils::read(buffer, size, mFifoMaxEventCount);
434
435 if (!unflattenString8(buffer, size, mStringType)) {
436 return NO_MEMORY;
437 }
438 if (!unflattenString8(buffer, size, mRequiredPermission)) {
439 return NO_MEMORY;
440 }
441 FlattenableUtils::read(buffer, size, mRequiredPermissionRuntime);
442 FlattenableUtils::read(buffer, size, mRequiredAppOp);
443 FlattenableUtils::read(buffer, size, mMaxDelay);
444 FlattenableUtils::read(buffer, size, mFlags);
445 return NO_ERROR;
446 }
447
flattenString8(void * & buffer,size_t & size,const String8 & string8)448 void Sensor::flattenString8(void*& buffer, size_t& size,
449 const String8& string8) {
450 uint32_t len = static_cast<uint32_t>(string8.length());
451 FlattenableUtils::write(buffer, size, len);
452 memcpy(static_cast<char*>(buffer), string8.string(), len);
453 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
454 }
455
unflattenString8(void const * & buffer,size_t & size,String8 & outputString8)456 bool Sensor::unflattenString8(void const*& buffer, size_t& size, String8& outputString8) {
457 uint32_t len;
458 if (size < sizeof(len)) {
459 return false;
460 }
461 FlattenableUtils::read(buffer, size, len);
462 if (size < len) {
463 return false;
464 }
465 outputString8.setTo(static_cast<char const*>(buffer), len);
466 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
467 return true;
468 }
469
470 // ----------------------------------------------------------------------------
471 }; // namespace android
472