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