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 default:
280 // Only pipe the stringType, requiredPermission and flags for custom sensors.
281 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.stringType) {
282 mStringType = hwSensor.stringType;
283 }
284 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.requiredPermission) {
285 mRequiredPermission = hwSensor.requiredPermission;
286 if (!strcmp(mRequiredPermission, SENSOR_PERMISSION_BODY_SENSORS)) {
287 AppOpsManager appOps;
288 mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS));
289 }
290 }
291
292 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
293 mFlags = static_cast<uint32_t>(hwSensor.flags);
294 } else {
295 // This is an OEM defined sensor on an older HAL. Use minDelay to determine the
296 // reporting mode of the sensor.
297 if (mMinDelay > 0) {
298 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
299 } else if (mMinDelay == 0) {
300 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
301 } else if (mMinDelay < 0) {
302 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
303 }
304 }
305 break;
306 }
307
308 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
309 // Wake-up flag of HAL 1.3 and above is set here
310 mFlags |= (hwSensor.flags & SENSOR_FLAG_WAKE_UP);
311
312 // Log error if the reporting mode is not as expected, but respect HAL setting.
313 int actualReportingMode = (hwSensor.flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
314 int expectedReportingMode = (mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
315 if (actualReportingMode != expectedReportingMode) {
316 ALOGE("Reporting Mode incorrect: sensor %s handle=%#010" PRIx32 " type=%" PRId32 " "
317 "actual=%d expected=%d",
318 mName.string(), mHandle, mType, actualReportingMode, expectedReportingMode);
319 }
320 }
321
322 // Feature flags
323 // Set DYNAMIC_SENSOR_MASK and ADDITIONAL_INFO_MASK flag here. Compatible with HAL 1_3.
324 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
325 mFlags |= hwSensor.flags & (DYNAMIC_SENSOR_MASK | ADDITIONAL_INFO_MASK);
326 }
327 // Set DIRECT_REPORT_MASK and DIRECT_CHANNEL_MASK flags. Compatible with HAL 1_3.
328 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
329 // only on continuous sensors direct report mode is defined
330 if ((mFlags & REPORTING_MODE_MASK) == SENSOR_FLAG_CONTINUOUS_MODE) {
331 mFlags |= hwSensor.flags
332 & (SENSOR_FLAG_MASK_DIRECT_REPORT | SENSOR_FLAG_MASK_DIRECT_CHANNEL);
333 }
334 }
335 // Set DATA_INJECTION flag here. Defined in HAL 1_4.
336 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_4) {
337 mFlags |= (hwSensor.flags & DATA_INJECTION_MASK);
338 }
339
340 if (mRequiredPermission.length() > 0) {
341 // If the sensor is protected by a permission we need to know if it is
342 // a runtime one to determine whether we can use the permission cache.
343 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
344 if (binder != nullptr) {
345 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
346 mRequiredPermissionRuntime = permCtrl->isRuntimePermission(
347 String16(mRequiredPermission));
348 }
349 }
350 }
351
~Sensor()352 Sensor::~Sensor() {
353 }
354
getName() const355 const String8& Sensor::getName() const {
356 return mName;
357 }
358
getVendor() const359 const String8& Sensor::getVendor() const {
360 return mVendor;
361 }
362
getHandle() const363 int32_t Sensor::getHandle() const {
364 return mHandle;
365 }
366
getType() const367 int32_t Sensor::getType() const {
368 return mType;
369 }
370
getMinValue() const371 float Sensor::getMinValue() const {
372 return mMinValue;
373 }
374
getMaxValue() const375 float Sensor::getMaxValue() const {
376 return mMaxValue;
377 }
378
getResolution() const379 float Sensor::getResolution() const {
380 return mResolution;
381 }
382
getPowerUsage() const383 float Sensor::getPowerUsage() const {
384 return mPower;
385 }
386
getMinDelay() const387 int32_t Sensor::getMinDelay() const {
388 return mMinDelay;
389 }
390
getMinDelayNs() const391 nsecs_t Sensor::getMinDelayNs() const {
392 return getMinDelay() * 1000;
393 }
394
getVersion() const395 int32_t Sensor::getVersion() const {
396 return mVersion;
397 }
398
getFifoReservedEventCount() const399 uint32_t Sensor::getFifoReservedEventCount() const {
400 return mFifoReservedEventCount;
401 }
402
getFifoMaxEventCount() const403 uint32_t Sensor::getFifoMaxEventCount() const {
404 return mFifoMaxEventCount;
405 }
406
getStringType() const407 const String8& Sensor::getStringType() const {
408 return mStringType;
409 }
410
getRequiredPermission() const411 const String8& Sensor::getRequiredPermission() const {
412 return mRequiredPermission;
413 }
414
isRequiredPermissionRuntime() const415 bool Sensor::isRequiredPermissionRuntime() const {
416 return mRequiredPermissionRuntime;
417 }
418
getRequiredAppOp() const419 int32_t Sensor::getRequiredAppOp() const {
420 return mRequiredAppOp;
421 }
422
getMaxDelay() const423 int32_t Sensor::getMaxDelay() const {
424 return mMaxDelay;
425 }
426
getFlags() const427 uint32_t Sensor::getFlags() const {
428 return mFlags;
429 }
430
isWakeUpSensor() const431 bool Sensor::isWakeUpSensor() const {
432 return (mFlags & SENSOR_FLAG_WAKE_UP) != 0;
433 }
434
isDynamicSensor() const435 bool Sensor::isDynamicSensor() const {
436 return (mFlags & SENSOR_FLAG_DYNAMIC_SENSOR) != 0;
437 }
438
isDataInjectionSupported() const439 bool Sensor::isDataInjectionSupported() const {
440 return (mFlags & SENSOR_FLAG_DATA_INJECTION) != 0;
441 }
442
hasAdditionalInfo() const443 bool Sensor::hasAdditionalInfo() const {
444 return (mFlags & SENSOR_FLAG_ADDITIONAL_INFO) != 0;
445 }
446
getHighestDirectReportRateLevel() const447 int32_t Sensor::getHighestDirectReportRateLevel() const {
448 return ((mFlags & SENSOR_FLAG_MASK_DIRECT_REPORT) >> SENSOR_FLAG_SHIFT_DIRECT_REPORT);
449 }
450
isDirectChannelTypeSupported(int32_t sharedMemType) const451 bool Sensor::isDirectChannelTypeSupported(int32_t sharedMemType) const {
452 switch (sharedMemType) {
453 case SENSOR_DIRECT_MEM_TYPE_ASHMEM:
454 return mFlags & SENSOR_FLAG_DIRECT_CHANNEL_ASHMEM;
455 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
456 return mFlags & SENSOR_FLAG_DIRECT_CHANNEL_GRALLOC;
457 default:
458 return false;
459 }
460 }
461
getReportingMode() const462 int32_t Sensor::getReportingMode() const {
463 return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT);
464 }
465
getUuid() const466 const Sensor::uuid_t& Sensor::getUuid() const {
467 return mUuid;
468 }
469
setId(int32_t id)470 void Sensor::setId(int32_t id) {
471 mUuid.i64[0] = id;
472 mUuid.i64[1] = 0;
473 }
474
capMinDelayMicros(int32_t cappedMinDelay)475 void Sensor::capMinDelayMicros(int32_t cappedMinDelay) {
476 if (mMinDelay < cappedMinDelay) {
477 mMinDelay = cappedMinDelay;
478 }
479 }
480
capHighestDirectReportRateLevel(int32_t cappedRateLevel)481 void Sensor::capHighestDirectReportRateLevel(int32_t cappedRateLevel) {
482 if (cappedRateLevel < getHighestDirectReportRateLevel()) {
483 mFlags &= ~SENSOR_FLAG_MASK_DIRECT_REPORT;
484 mFlags |= cappedRateLevel << SENSOR_FLAG_SHIFT_DIRECT_REPORT;
485 }
486 }
487
getId() const488 int32_t Sensor::getId() const {
489 return int32_t(mUuid.i64[0]);
490 }
491
getFlattenedSize() const492 size_t Sensor::getFlattenedSize() const {
493 size_t fixedSize =
494 sizeof(mVersion) + sizeof(mHandle) + sizeof(mType) +
495 sizeof(mMinValue) + sizeof(mMaxValue) + sizeof(mResolution) +
496 sizeof(mPower) + sizeof(mMinDelay) + sizeof(mFifoMaxEventCount) +
497 sizeof(mFifoMaxEventCount) + sizeof(mRequiredPermissionRuntime) +
498 sizeof(mRequiredAppOp) + sizeof(mMaxDelay) + sizeof(mFlags) + sizeof(mUuid);
499
500 size_t variableSize =
501 sizeof(uint32_t) + FlattenableUtils::align<4>(mName.length()) +
502 sizeof(uint32_t) + FlattenableUtils::align<4>(mVendor.length()) +
503 sizeof(uint32_t) + FlattenableUtils::align<4>(mStringType.length()) +
504 sizeof(uint32_t) + FlattenableUtils::align<4>(mRequiredPermission.length());
505
506 return fixedSize + variableSize;
507 }
508
flatten(void * buffer,size_t size) const509 status_t Sensor::flatten(void* buffer, size_t size) const {
510 if (size < getFlattenedSize()) {
511 return NO_MEMORY;
512 }
513
514 flattenString8(buffer, size, mName);
515 flattenString8(buffer, size, mVendor);
516 FlattenableUtils::write(buffer, size, mVersion);
517 FlattenableUtils::write(buffer, size, mHandle);
518 FlattenableUtils::write(buffer, size, mType);
519 FlattenableUtils::write(buffer, size, mMinValue);
520 FlattenableUtils::write(buffer, size, mMaxValue);
521 FlattenableUtils::write(buffer, size, mResolution);
522 FlattenableUtils::write(buffer, size, mPower);
523 FlattenableUtils::write(buffer, size, mMinDelay);
524 FlattenableUtils::write(buffer, size, mFifoReservedEventCount);
525 FlattenableUtils::write(buffer, size, mFifoMaxEventCount);
526 flattenString8(buffer, size, mStringType);
527 flattenString8(buffer, size, mRequiredPermission);
528 FlattenableUtils::write(buffer, size, mRequiredPermissionRuntime);
529 FlattenableUtils::write(buffer, size, mRequiredAppOp);
530 FlattenableUtils::write(buffer, size, mMaxDelay);
531 FlattenableUtils::write(buffer, size, mFlags);
532 if (mUuid.i64[1] != 0) {
533 // We should never hit this case with our current API, but we
534 // could via a careless API change. If that happens,
535 // this code will keep us from leaking our UUID (while probably
536 // breaking dynamic sensors). See b/29547335.
537 ALOGW("Sensor with UUID being flattened; sending 0. Expect "
538 "bad dynamic sensor behavior");
539 uuid_t tmpUuid; // default constructor makes this 0.
540 FlattenableUtils::write(buffer, size, tmpUuid);
541 } else {
542 FlattenableUtils::write(buffer, size, mUuid);
543 }
544 return NO_ERROR;
545 }
546
unflatten(void const * buffer,size_t size)547 status_t Sensor::unflatten(void const* buffer, size_t size) {
548 if (!unflattenString8(buffer, size, mName)) {
549 return NO_MEMORY;
550 }
551 if (!unflattenString8(buffer, size, mVendor)) {
552 return NO_MEMORY;
553 }
554
555 size_t fixedSize1 =
556 sizeof(mVersion) + sizeof(mHandle) + sizeof(mType) + sizeof(mMinValue) +
557 sizeof(mMaxValue) + sizeof(mResolution) + sizeof(mPower) + sizeof(mMinDelay) +
558 sizeof(mFifoMaxEventCount) + sizeof(mFifoMaxEventCount);
559 if (size < fixedSize1) {
560 return NO_MEMORY;
561 }
562
563 FlattenableUtils::read(buffer, size, mVersion);
564 FlattenableUtils::read(buffer, size, mHandle);
565 FlattenableUtils::read(buffer, size, mType);
566 FlattenableUtils::read(buffer, size, mMinValue);
567 FlattenableUtils::read(buffer, size, mMaxValue);
568 FlattenableUtils::read(buffer, size, mResolution);
569 FlattenableUtils::read(buffer, size, mPower);
570 FlattenableUtils::read(buffer, size, mMinDelay);
571 FlattenableUtils::read(buffer, size, mFifoReservedEventCount);
572 FlattenableUtils::read(buffer, size, mFifoMaxEventCount);
573
574 if (!unflattenString8(buffer, size, mStringType)) {
575 return NO_MEMORY;
576 }
577 if (!unflattenString8(buffer, size, mRequiredPermission)) {
578 return NO_MEMORY;
579 }
580
581 size_t fixedSize2 =
582 sizeof(mRequiredPermissionRuntime) + sizeof(mRequiredAppOp) + sizeof(mMaxDelay) +
583 sizeof(mFlags) + sizeof(mUuid);
584 if (size < fixedSize2) {
585 return NO_MEMORY;
586 }
587
588 FlattenableUtils::read(buffer, size, mRequiredPermissionRuntime);
589 FlattenableUtils::read(buffer, size, mRequiredAppOp);
590 FlattenableUtils::read(buffer, size, mMaxDelay);
591 FlattenableUtils::read(buffer, size, mFlags);
592 FlattenableUtils::read(buffer, size, mUuid);
593 return NO_ERROR;
594 }
595
flattenString8(void * & buffer,size_t & size,const String8 & string8)596 void Sensor::flattenString8(void*& buffer, size_t& size,
597 const String8& string8) {
598 uint32_t len = static_cast<uint32_t>(string8.length());
599 FlattenableUtils::write(buffer, size, len);
600 memcpy(static_cast<char*>(buffer), string8.string(), len);
601 FlattenableUtils::advance(buffer, size, len);
602 size -= FlattenableUtils::align<4>(buffer);
603 }
604
unflattenString8(void const * & buffer,size_t & size,String8 & outputString8)605 bool Sensor::unflattenString8(void const*& buffer, size_t& size, String8& outputString8) {
606 uint32_t len;
607 if (size < sizeof(len)) {
608 return false;
609 }
610 FlattenableUtils::read(buffer, size, len);
611 if (size < len) {
612 return false;
613 }
614 outputString8.setTo(static_cast<char const*>(buffer), len);
615 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
616 return true;
617 }
618
619 // ----------------------------------------------------------------------------
620 }; // namespace android
621