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 <sensor/Sensor.h>
18
19 #include <inttypes.h>
20
21 #include <binder/AppOpsManager.h>
22 #include <binder/IPermissionController.h>
23 #include <binder/IServiceManager.h>
24
25 /*
26 * The permission to use for activity recognition sensors (like step counter).
27 * See sensor types for more details on what sensors should require this
28 * permission.
29 */
30 #define SENSOR_PERMISSION_ACTIVITY_RECOGNITION "android.permission.ACTIVITY_RECOGNITION"
31
32 // ----------------------------------------------------------------------------
33 namespace android {
34 // ----------------------------------------------------------------------------
35
Sensor(const char * name)36 Sensor::Sensor(const char * name) :
37 mName(name), mHandle(0), mType(0),
38 mMinValue(0), mMaxValue(0), mResolution(0),
39 mPower(0), mMinDelay(0), mVersion(0), mFifoReservedEventCount(0),
40 mFifoMaxEventCount(0), mRequiredAppOp(-1),
41 mMaxDelay(0), mFlags(0) {
42 }
43
Sensor(struct sensor_t const * hwSensor,int halVersion)44 Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion) :
45 Sensor(*hwSensor, uuid_t(), halVersion) {
46 }
47
Sensor(struct sensor_t const & hwSensor,const uuid_t & uuid,int halVersion)48 Sensor::Sensor(struct sensor_t const& hwSensor, const uuid_t& uuid, int halVersion) :
49 Sensor("") {
50 mName = hwSensor.name;
51 mVendor = hwSensor.vendor;
52 mVersion = hwSensor.version;
53 mHandle = hwSensor.handle;
54 mType = hwSensor.type;
55 mMinValue = 0; // FIXME: minValue
56 mMaxValue = hwSensor.maxRange; // FIXME: maxValue
57 mResolution = hwSensor.resolution;
58 mPower = hwSensor.power;
59 mMinDelay = hwSensor.minDelay;
60 mFlags = 0;
61 mUuid = uuid;
62
63 // Set fifo event count zero for older devices which do not support batching. Fused
64 // sensors also have their fifo counts set to zero.
65 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) {
66 mFifoReservedEventCount = hwSensor.fifoReservedEventCount;
67 mFifoMaxEventCount = hwSensor.fifoMaxEventCount;
68 } else {
69 mFifoReservedEventCount = 0;
70 mFifoMaxEventCount = 0;
71 }
72
73 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
74 if (hwSensor.maxDelay > INT_MAX) {
75 // Max delay is declared as a 64 bit integer for 64 bit architectures. But it should
76 // always fit in a 32 bit integer, log error and cap it to INT_MAX.
77 ALOGE("Sensor maxDelay overflow error %s %" PRId64, mName.string(),
78 static_cast<int64_t>(hwSensor.maxDelay));
79 mMaxDelay = INT_MAX;
80 } else {
81 mMaxDelay = static_cast<int32_t>(hwSensor.maxDelay);
82 }
83 } else {
84 // For older hals set maxDelay to 0.
85 mMaxDelay = 0;
86 }
87
88 // Ensure existing sensors have correct string type, required permissions and reporting mode.
89 // Set reportingMode for all android defined sensor types, set wake-up flag only for proximity
90 // sensor, significant motion, tilt, pick_up gesture, wake gesture and glance gesture on older
91 // HALs. Newer HALs can define both wake-up and non wake-up proximity sensors.
92 // All the OEM defined defined sensors have flags set to whatever is provided by the HAL.
93 switch (mType) {
94 case SENSOR_TYPE_ACCELEROMETER:
95 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER;
96 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
97 break;
98 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
99 mStringType = SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
100 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
101 break;
102 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
103 mStringType = SENSOR_STRING_TYPE_GAME_ROTATION_VECTOR;
104 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
105 break;
106 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
107 mStringType = SENSOR_STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR;
108 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
109 break;
110 case SENSOR_TYPE_GRAVITY:
111 mStringType = SENSOR_STRING_TYPE_GRAVITY;
112 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
113 break;
114 case SENSOR_TYPE_GYROSCOPE:
115 mStringType = SENSOR_STRING_TYPE_GYROSCOPE;
116 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
117 break;
118 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
119 mStringType = SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
120 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
121 break;
122 case SENSOR_TYPE_HEART_RATE: {
123 mStringType = SENSOR_STRING_TYPE_HEART_RATE;
124 mRequiredPermission = SENSOR_PERMISSION_BODY_SENSORS;
125 AppOpsManager appOps;
126 mRequiredAppOp = appOps.permissionToOpCode(String16(mRequiredPermission));
127 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
128 } break;
129 case SENSOR_TYPE_LIGHT:
130 mStringType = SENSOR_STRING_TYPE_LIGHT;
131 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
132 break;
133 case SENSOR_TYPE_LINEAR_ACCELERATION:
134 mStringType = SENSOR_STRING_TYPE_LINEAR_ACCELERATION;
135 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
136 break;
137 case SENSOR_TYPE_MAGNETIC_FIELD:
138 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD;
139 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
140 break;
141 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
142 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
143 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
144 break;
145 case SENSOR_TYPE_ORIENTATION:
146 mStringType = SENSOR_STRING_TYPE_ORIENTATION;
147 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
148 break;
149 case SENSOR_TYPE_PRESSURE:
150 mStringType = SENSOR_STRING_TYPE_PRESSURE;
151 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
152 break;
153 case SENSOR_TYPE_PROXIMITY:
154 mStringType = SENSOR_STRING_TYPE_PROXIMITY;
155 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
156 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
157 mFlags |= SENSOR_FLAG_WAKE_UP;
158 }
159 break;
160 case SENSOR_TYPE_RELATIVE_HUMIDITY:
161 mStringType = SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
162 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
163 break;
164 case SENSOR_TYPE_ROTATION_VECTOR:
165 mStringType = SENSOR_STRING_TYPE_ROTATION_VECTOR;
166 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
167 break;
168 case SENSOR_TYPE_SIGNIFICANT_MOTION:
169 mStringType = SENSOR_STRING_TYPE_SIGNIFICANT_MOTION;
170 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
171 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
172 mFlags |= SENSOR_FLAG_WAKE_UP;
173 }
174 break;
175 case SENSOR_TYPE_STEP_COUNTER: {
176 mStringType = SENSOR_STRING_TYPE_STEP_COUNTER;
177 mRequiredPermission = SENSOR_PERMISSION_ACTIVITY_RECOGNITION;
178 AppOpsManager appOps;
179 mRequiredAppOp =
180 appOps.permissionToOpCode(String16(mRequiredPermission));
181 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
182 } break;
183 case SENSOR_TYPE_STEP_DETECTOR: {
184 mStringType = SENSOR_STRING_TYPE_STEP_DETECTOR;
185 mRequiredPermission = SENSOR_PERMISSION_ACTIVITY_RECOGNITION;
186 AppOpsManager appOps;
187 mRequiredAppOp =
188 appOps.permissionToOpCode(String16(mRequiredPermission));
189 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
190 } break;
191 case SENSOR_TYPE_TEMPERATURE:
192 mStringType = SENSOR_STRING_TYPE_TEMPERATURE;
193 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
194 break;
195 case SENSOR_TYPE_TILT_DETECTOR:
196 mStringType = SENSOR_STRING_TYPE_TILT_DETECTOR;
197 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
198 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
199 mFlags |= SENSOR_FLAG_WAKE_UP;
200 }
201 break;
202 case SENSOR_TYPE_WAKE_GESTURE:
203 mStringType = SENSOR_STRING_TYPE_WAKE_GESTURE;
204 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
205 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
206 mFlags |= SENSOR_FLAG_WAKE_UP;
207 }
208 break;
209 case SENSOR_TYPE_GLANCE_GESTURE:
210 mStringType = SENSOR_STRING_TYPE_GLANCE_GESTURE;
211 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
212 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
213 mFlags |= SENSOR_FLAG_WAKE_UP;
214 }
215 break;
216 case SENSOR_TYPE_PICK_UP_GESTURE:
217 mStringType = SENSOR_STRING_TYPE_PICK_UP_GESTURE;
218 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
219 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
220 mFlags |= SENSOR_FLAG_WAKE_UP;
221 }
222 break;
223 case SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT:
224 mStringType = SENSOR_STRING_TYPE_LOW_LATENCY_OFFBODY_DETECT;
225 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
226 break;
227 case SENSOR_TYPE_WRIST_TILT_GESTURE:
228 mStringType = SENSOR_STRING_TYPE_WRIST_TILT_GESTURE;
229 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
230 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
231 mFlags |= SENSOR_FLAG_WAKE_UP;
232 }
233 break;
234 case SENSOR_TYPE_DEVICE_ORIENTATION:
235 mStringType = SENSOR_STRING_TYPE_DEVICE_ORIENTATION;
236 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
237 break;
238 case SENSOR_TYPE_DYNAMIC_SENSOR_META:
239 mStringType = SENSOR_STRING_TYPE_DYNAMIC_SENSOR_META;
240 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE; // special trigger
241 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
242 mFlags |= SENSOR_FLAG_WAKE_UP;
243 }
244 break;
245 case SENSOR_TYPE_POSE_6DOF:
246 mStringType = SENSOR_STRING_TYPE_POSE_6DOF;
247 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
248 break;
249 case SENSOR_TYPE_STATIONARY_DETECT:
250 mStringType = SENSOR_STRING_TYPE_STATIONARY_DETECT;
251 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
252 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
253 mFlags |= SENSOR_FLAG_WAKE_UP;
254 }
255 break;
256 case SENSOR_TYPE_MOTION_DETECT:
257 mStringType = SENSOR_STRING_TYPE_MOTION_DETECT;
258 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
259 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
260 mFlags |= SENSOR_FLAG_WAKE_UP;
261 }
262 break;
263 case SENSOR_TYPE_HEART_BEAT:
264 mStringType = SENSOR_STRING_TYPE_HEART_BEAT;
265 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
266 break;
267
268 // TODO: Placeholder for LLOB sensor type
269
270
271 case SENSOR_TYPE_ACCELEROMETER_UNCALIBRATED:
272 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER_UNCALIBRATED;
273 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
274 break;
275 case SENSOR_TYPE_HINGE_ANGLE:
276 mStringType = SENSOR_STRING_TYPE_HINGE_ANGLE;
277 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
278 break;
279 case SENSOR_TYPE_HEAD_TRACKER:
280 mStringType = SENSOR_STRING_TYPE_HEAD_TRACKER;
281 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
282 break;
283 case SENSOR_TYPE_ACCELEROMETER_LIMITED_AXES:
284 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER_LIMITED_AXES;
285 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
286 break;
287 case SENSOR_TYPE_GYROSCOPE_LIMITED_AXES:
288 mStringType = SENSOR_STRING_TYPE_GYROSCOPE_LIMITED_AXES;
289 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
290 break;
291 case SENSOR_TYPE_ACCELEROMETER_LIMITED_AXES_UNCALIBRATED:
292 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER_LIMITED_AXES_UNCALIBRATED;
293 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
294 break;
295 case SENSOR_TYPE_GYROSCOPE_LIMITED_AXES_UNCALIBRATED:
296 mStringType = SENSOR_STRING_TYPE_GYROSCOPE_LIMITED_AXES_UNCALIBRATED;
297 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
298 break;
299 case SENSOR_TYPE_HEADING:
300 mStringType = SENSOR_STRING_TYPE_HEADING;
301 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
302 break;
303 default:
304 // Only pipe the stringType, requiredPermission and flags for custom sensors.
305 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.stringType) {
306 mStringType = hwSensor.stringType;
307 }
308 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.requiredPermission) {
309 mRequiredPermission = hwSensor.requiredPermission;
310 if (!strcmp(mRequiredPermission, SENSOR_PERMISSION_BODY_SENSORS)) {
311 AppOpsManager appOps;
312 mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS));
313 }
314 }
315
316 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
317 mFlags = static_cast<uint32_t>(hwSensor.flags);
318 } else {
319 // This is an OEM defined sensor on an older HAL. Use minDelay to determine the
320 // reporting mode of the sensor.
321 if (mMinDelay > 0) {
322 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
323 } else if (mMinDelay == 0) {
324 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
325 } else if (mMinDelay < 0) {
326 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
327 }
328 }
329 break;
330 }
331
332 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
333 // Wake-up flag of HAL 1.3 and above is set here
334 mFlags |= (hwSensor.flags & SENSOR_FLAG_WAKE_UP);
335
336 // Log error if the reporting mode is not as expected, but respect HAL setting.
337 int actualReportingMode = (hwSensor.flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
338 int expectedReportingMode = (mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
339 if (actualReportingMode != expectedReportingMode) {
340 ALOGE("Reporting Mode incorrect: sensor %s handle=%#010" PRIx32 " type=%" PRId32 " "
341 "actual=%d expected=%d",
342 mName.string(), mHandle, mType, actualReportingMode, expectedReportingMode);
343 }
344 }
345
346 // Feature flags
347 // Set DYNAMIC_SENSOR_MASK and ADDITIONAL_INFO_MASK flag here. Compatible with HAL 1_3.
348 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
349 mFlags |= hwSensor.flags & (DYNAMIC_SENSOR_MASK | ADDITIONAL_INFO_MASK);
350 }
351 // Set DIRECT_REPORT_MASK and DIRECT_CHANNEL_MASK flags. Compatible with HAL 1_3.
352 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
353 // only on continuous sensors direct report mode is defined
354 if ((mFlags & REPORTING_MODE_MASK) == SENSOR_FLAG_CONTINUOUS_MODE) {
355 mFlags |= hwSensor.flags
356 & (SENSOR_FLAG_MASK_DIRECT_REPORT | SENSOR_FLAG_MASK_DIRECT_CHANNEL);
357 }
358 }
359 // Set DATA_INJECTION flag here. Defined in HAL 1_4.
360 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_4) {
361 mFlags |= (hwSensor.flags & DATA_INJECTION_MASK);
362 }
363
364 if (mRequiredPermission.length() > 0) {
365 // If the sensor is protected by a permission we need to know if it is
366 // a runtime one to determine whether we can use the permission cache.
367 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
368 if (binder != nullptr) {
369 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
370 mRequiredPermissionRuntime = permCtrl->isRuntimePermission(
371 String16(mRequiredPermission));
372 }
373 }
374 }
375
~Sensor()376 Sensor::~Sensor() {
377 }
378
getName() const379 const String8& Sensor::getName() const {
380 return mName;
381 }
382
getVendor() const383 const String8& Sensor::getVendor() const {
384 return mVendor;
385 }
386
getHandle() const387 int32_t Sensor::getHandle() const {
388 return mHandle;
389 }
390
getType() const391 int32_t Sensor::getType() const {
392 return mType;
393 }
394
getMinValue() const395 float Sensor::getMinValue() const {
396 return mMinValue;
397 }
398
getMaxValue() const399 float Sensor::getMaxValue() const {
400 return mMaxValue;
401 }
402
getResolution() const403 float Sensor::getResolution() const {
404 return mResolution;
405 }
406
getPowerUsage() const407 float Sensor::getPowerUsage() const {
408 return mPower;
409 }
410
getMinDelay() const411 int32_t Sensor::getMinDelay() const {
412 return mMinDelay;
413 }
414
getMinDelayNs() const415 nsecs_t Sensor::getMinDelayNs() const {
416 return getMinDelay() * 1000;
417 }
418
getVersion() const419 int32_t Sensor::getVersion() const {
420 return mVersion;
421 }
422
getFifoReservedEventCount() const423 uint32_t Sensor::getFifoReservedEventCount() const {
424 return mFifoReservedEventCount;
425 }
426
getFifoMaxEventCount() const427 uint32_t Sensor::getFifoMaxEventCount() const {
428 return mFifoMaxEventCount;
429 }
430
getStringType() const431 const String8& Sensor::getStringType() const {
432 return mStringType;
433 }
434
getRequiredPermission() const435 const String8& Sensor::getRequiredPermission() const {
436 return mRequiredPermission;
437 }
438
isRequiredPermissionRuntime() const439 bool Sensor::isRequiredPermissionRuntime() const {
440 return mRequiredPermissionRuntime;
441 }
442
getRequiredAppOp() const443 int32_t Sensor::getRequiredAppOp() const {
444 return mRequiredAppOp;
445 }
446
getMaxDelay() const447 int32_t Sensor::getMaxDelay() const {
448 return mMaxDelay;
449 }
450
getFlags() const451 uint32_t Sensor::getFlags() const {
452 return mFlags;
453 }
454
isWakeUpSensor() const455 bool Sensor::isWakeUpSensor() const {
456 return (mFlags & SENSOR_FLAG_WAKE_UP) != 0;
457 }
458
isDynamicSensor() const459 bool Sensor::isDynamicSensor() const {
460 return (mFlags & SENSOR_FLAG_DYNAMIC_SENSOR) != 0;
461 }
462
isDataInjectionSupported() const463 bool Sensor::isDataInjectionSupported() const {
464 return (mFlags & SENSOR_FLAG_DATA_INJECTION) != 0;
465 }
466
hasAdditionalInfo() const467 bool Sensor::hasAdditionalInfo() const {
468 return (mFlags & SENSOR_FLAG_ADDITIONAL_INFO) != 0;
469 }
470
getHighestDirectReportRateLevel() const471 int32_t Sensor::getHighestDirectReportRateLevel() const {
472 return ((mFlags & SENSOR_FLAG_MASK_DIRECT_REPORT) >> SENSOR_FLAG_SHIFT_DIRECT_REPORT);
473 }
474
isDirectChannelTypeSupported(int32_t sharedMemType) const475 bool Sensor::isDirectChannelTypeSupported(int32_t sharedMemType) const {
476 switch (sharedMemType) {
477 case SENSOR_DIRECT_MEM_TYPE_ASHMEM:
478 return mFlags & SENSOR_FLAG_DIRECT_CHANNEL_ASHMEM;
479 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
480 return mFlags & SENSOR_FLAG_DIRECT_CHANNEL_GRALLOC;
481 default:
482 return false;
483 }
484 }
485
getReportingMode() const486 int32_t Sensor::getReportingMode() const {
487 return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT);
488 }
489
getUuid() const490 const Sensor::uuid_t& Sensor::getUuid() const {
491 return mUuid;
492 }
493
setId(int32_t id)494 void Sensor::setId(int32_t id) {
495 mId = id;
496 }
497
getId() const498 int32_t Sensor::getId() const {
499 return mId;
500 }
501
anonymizeUuid()502 void Sensor::anonymizeUuid() {
503 mUuid.i64[0] = mId;
504 mUuid.i64[1] = 0;
505 }
506
capMinDelayMicros(int32_t cappedMinDelay)507 void Sensor::capMinDelayMicros(int32_t cappedMinDelay) {
508 if (mMinDelay < cappedMinDelay) {
509 mMinDelay = cappedMinDelay;
510 }
511 }
512
capHighestDirectReportRateLevel(int32_t cappedRateLevel)513 void Sensor::capHighestDirectReportRateLevel(int32_t cappedRateLevel) {
514 if (cappedRateLevel < getHighestDirectReportRateLevel()) {
515 mFlags &= ~SENSOR_FLAG_MASK_DIRECT_REPORT;
516 mFlags |= cappedRateLevel << SENSOR_FLAG_SHIFT_DIRECT_REPORT;
517 }
518 }
519
getFlattenedSize() const520 size_t Sensor::getFlattenedSize() const {
521 size_t fixedSize =
522 sizeof(mVersion) + sizeof(mHandle) + sizeof(mType) +
523 sizeof(mMinValue) + sizeof(mMaxValue) + sizeof(mResolution) +
524 sizeof(mPower) + sizeof(mMinDelay) + sizeof(mFifoMaxEventCount) +
525 sizeof(mFifoMaxEventCount) + sizeof(mRequiredPermissionRuntime) +
526 sizeof(mRequiredAppOp) + sizeof(mMaxDelay) + sizeof(mFlags) +
527 sizeof(mUuid) + sizeof(mId);
528
529 size_t variableSize =
530 sizeof(uint32_t) + FlattenableUtils::align<4>(mName.length()) +
531 sizeof(uint32_t) + FlattenableUtils::align<4>(mVendor.length()) +
532 sizeof(uint32_t) + FlattenableUtils::align<4>(mStringType.length()) +
533 sizeof(uint32_t) + FlattenableUtils::align<4>(mRequiredPermission.length());
534
535 return fixedSize + variableSize;
536 }
537
flatten(void * buffer,size_t size) const538 status_t Sensor::flatten(void* buffer, size_t size) const {
539 if (size < getFlattenedSize()) {
540 return NO_MEMORY;
541 }
542
543 flattenString8(buffer, size, mName);
544 flattenString8(buffer, size, mVendor);
545 FlattenableUtils::write(buffer, size, mVersion);
546 FlattenableUtils::write(buffer, size, mHandle);
547 FlattenableUtils::write(buffer, size, mType);
548 FlattenableUtils::write(buffer, size, mMinValue);
549 FlattenableUtils::write(buffer, size, mMaxValue);
550 FlattenableUtils::write(buffer, size, mResolution);
551 FlattenableUtils::write(buffer, size, mPower);
552 FlattenableUtils::write(buffer, size, mMinDelay);
553 FlattenableUtils::write(buffer, size, mFifoReservedEventCount);
554 FlattenableUtils::write(buffer, size, mFifoMaxEventCount);
555 flattenString8(buffer, size, mStringType);
556 flattenString8(buffer, size, mRequiredPermission);
557 FlattenableUtils::write(buffer, size, mRequiredPermissionRuntime);
558 FlattenableUtils::write(buffer, size, mRequiredAppOp);
559 FlattenableUtils::write(buffer, size, mMaxDelay);
560 FlattenableUtils::write(buffer, size, mFlags);
561 FlattenableUtils::write(buffer, size, mUuid);
562 FlattenableUtils::write(buffer, size, mId);
563 return NO_ERROR;
564 }
565
unflatten(void const * buffer,size_t size)566 status_t Sensor::unflatten(void const* buffer, size_t size) {
567 if (!unflattenString8(buffer, size, mName)) {
568 return NO_MEMORY;
569 }
570 if (!unflattenString8(buffer, size, mVendor)) {
571 return NO_MEMORY;
572 }
573
574 size_t fixedSize1 =
575 sizeof(mVersion) + sizeof(mHandle) + sizeof(mType) + sizeof(mMinValue) +
576 sizeof(mMaxValue) + sizeof(mResolution) + sizeof(mPower) + sizeof(mMinDelay) +
577 sizeof(mFifoMaxEventCount) + sizeof(mFifoMaxEventCount);
578 if (size < fixedSize1) {
579 return NO_MEMORY;
580 }
581
582 FlattenableUtils::read(buffer, size, mVersion);
583 FlattenableUtils::read(buffer, size, mHandle);
584 FlattenableUtils::read(buffer, size, mType);
585 FlattenableUtils::read(buffer, size, mMinValue);
586 FlattenableUtils::read(buffer, size, mMaxValue);
587 FlattenableUtils::read(buffer, size, mResolution);
588 FlattenableUtils::read(buffer, size, mPower);
589 FlattenableUtils::read(buffer, size, mMinDelay);
590 FlattenableUtils::read(buffer, size, mFifoReservedEventCount);
591 FlattenableUtils::read(buffer, size, mFifoMaxEventCount);
592
593 if (!unflattenString8(buffer, size, mStringType)) {
594 return NO_MEMORY;
595 }
596 if (!unflattenString8(buffer, size, mRequiredPermission)) {
597 return NO_MEMORY;
598 }
599
600 size_t fixedSize2 =
601 sizeof(mRequiredPermissionRuntime) + sizeof(mRequiredAppOp) + sizeof(mMaxDelay) +
602 sizeof(mFlags) + sizeof(mUuid) + sizeof(mId);
603 if (size < fixedSize2) {
604 return NO_MEMORY;
605 }
606
607 FlattenableUtils::read(buffer, size, mRequiredPermissionRuntime);
608 FlattenableUtils::read(buffer, size, mRequiredAppOp);
609 FlattenableUtils::read(buffer, size, mMaxDelay);
610 FlattenableUtils::read(buffer, size, mFlags);
611 FlattenableUtils::read(buffer, size, mUuid);
612 FlattenableUtils::read(buffer, size, mId);
613 return NO_ERROR;
614 }
615
flattenString8(void * & buffer,size_t & size,const String8 & string8)616 void Sensor::flattenString8(void*& buffer, size_t& size,
617 const String8& string8) {
618 uint32_t len = static_cast<uint32_t>(string8.length());
619 FlattenableUtils::write(buffer, size, len);
620 memcpy(static_cast<char*>(buffer), string8.string(), len);
621 FlattenableUtils::advance(buffer, size, len);
622 size -= FlattenableUtils::align<4>(buffer);
623 }
624
unflattenString8(void const * & buffer,size_t & size,String8 & outputString8)625 bool Sensor::unflattenString8(void const*& buffer, size_t& size, String8& outputString8) {
626 uint32_t len;
627 if (size < sizeof(len)) {
628 return false;
629 }
630 FlattenableUtils::read(buffer, size, len);
631 if (size < len) {
632 return false;
633 }
634 outputString8.setTo(static_cast<char const*>(buffer), len);
635
636 if (size < FlattenableUtils::align<4>(len)) {
637 ALOGE("Malformed Sensor String8 field. Should be in a 4-byte aligned buffer but is not.");
638 return false;
639 }
640 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
641
642 return true;
643 }
644
645 // ----------------------------------------------------------------------------
646 }; // namespace android
647