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 namespace android {
27 // ----------------------------------------------------------------------------
28
Sensor(const char * name)29 Sensor::Sensor(const char * name) :
30 mName(name), mHandle(0), mType(0),
31 mMinValue(0), mMaxValue(0), mResolution(0),
32 mPower(0), mMinDelay(0), mVersion(0), mFifoReservedEventCount(0),
33 mFifoMaxEventCount(0), mRequiredAppOp(-1),
34 mMaxDelay(0), mFlags(0) {
35 }
36
Sensor(struct sensor_t const * hwSensor,int halVersion)37 Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion) :
38 Sensor(*hwSensor, uuid_t(), halVersion) {
39 }
40
Sensor(struct sensor_t const & hwSensor,const uuid_t & uuid,int halVersion)41 Sensor::Sensor(struct sensor_t const& hwSensor, const uuid_t& uuid, int halVersion) :
42 Sensor("") {
43 mName = hwSensor.name;
44 mVendor = hwSensor.vendor;
45 mVersion = hwSensor.version;
46 mHandle = hwSensor.handle;
47 mType = hwSensor.type;
48 mMinValue = 0; // FIXME: minValue
49 mMaxValue = hwSensor.maxRange; // FIXME: maxValue
50 mResolution = hwSensor.resolution;
51 mPower = hwSensor.power;
52 mMinDelay = hwSensor.minDelay;
53 mFlags = 0;
54 mUuid = uuid;
55
56 // Set fifo event count zero for older devices which do not support batching. Fused
57 // sensors also have their fifo counts set to zero.
58 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) {
59 mFifoReservedEventCount = hwSensor.fifoReservedEventCount;
60 mFifoMaxEventCount = hwSensor.fifoMaxEventCount;
61 } else {
62 mFifoReservedEventCount = 0;
63 mFifoMaxEventCount = 0;
64 }
65
66 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
67 if (hwSensor.maxDelay > INT_MAX) {
68 // Max delay is declared as a 64 bit integer for 64 bit architectures. But it should
69 // always fit in a 32 bit integer, log error and cap it to INT_MAX.
70 ALOGE("Sensor maxDelay overflow error %s %" PRId64, mName.string(),
71 static_cast<int64_t>(hwSensor.maxDelay));
72 mMaxDelay = INT_MAX;
73 } else {
74 mMaxDelay = static_cast<int32_t>(hwSensor.maxDelay);
75 }
76 } else {
77 // For older hals set maxDelay to 0.
78 mMaxDelay = 0;
79 }
80
81 // Ensure existing sensors have correct string type, required permissions and reporting mode.
82 // Set reportingMode for all android defined sensor types, set wake-up flag only for proximity
83 // sensor, significant motion, tilt, pick_up gesture, wake gesture and glance gesture on older
84 // HALs. Newer HALs can define both wake-up and non wake-up proximity sensors.
85 // All the OEM defined defined sensors have flags set to whatever is provided by the HAL.
86 switch (mType) {
87 case SENSOR_TYPE_ACCELEROMETER:
88 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER;
89 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
90 break;
91 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
92 mStringType = SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
93 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
94 break;
95 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
96 mStringType = SENSOR_STRING_TYPE_GAME_ROTATION_VECTOR;
97 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
98 break;
99 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
100 mStringType = SENSOR_STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR;
101 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
102 break;
103 case SENSOR_TYPE_GRAVITY:
104 mStringType = SENSOR_STRING_TYPE_GRAVITY;
105 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
106 break;
107 case SENSOR_TYPE_GYROSCOPE:
108 mStringType = SENSOR_STRING_TYPE_GYROSCOPE;
109 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
110 break;
111 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
112 mStringType = SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
113 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
114 break;
115 case SENSOR_TYPE_HEART_RATE: {
116 mStringType = SENSOR_STRING_TYPE_HEART_RATE;
117 mRequiredPermission = SENSOR_PERMISSION_BODY_SENSORS;
118 AppOpsManager appOps;
119 mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS));
120 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
121 } break;
122 case SENSOR_TYPE_LIGHT:
123 mStringType = SENSOR_STRING_TYPE_LIGHT;
124 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
125 break;
126 case SENSOR_TYPE_LINEAR_ACCELERATION:
127 mStringType = SENSOR_STRING_TYPE_LINEAR_ACCELERATION;
128 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
129 break;
130 case SENSOR_TYPE_MAGNETIC_FIELD:
131 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD;
132 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
133 break;
134 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
135 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
136 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
137 break;
138 case SENSOR_TYPE_ORIENTATION:
139 mStringType = SENSOR_STRING_TYPE_ORIENTATION;
140 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
141 break;
142 case SENSOR_TYPE_PRESSURE:
143 mStringType = SENSOR_STRING_TYPE_PRESSURE;
144 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
145 break;
146 case SENSOR_TYPE_PROXIMITY:
147 mStringType = SENSOR_STRING_TYPE_PROXIMITY;
148 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
149 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
150 mFlags |= SENSOR_FLAG_WAKE_UP;
151 }
152 break;
153 case SENSOR_TYPE_RELATIVE_HUMIDITY:
154 mStringType = SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
155 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
156 break;
157 case SENSOR_TYPE_ROTATION_VECTOR:
158 mStringType = SENSOR_STRING_TYPE_ROTATION_VECTOR;
159 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
160 break;
161 case SENSOR_TYPE_SIGNIFICANT_MOTION:
162 mStringType = SENSOR_STRING_TYPE_SIGNIFICANT_MOTION;
163 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
164 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
165 mFlags |= SENSOR_FLAG_WAKE_UP;
166 }
167 break;
168 case SENSOR_TYPE_STEP_COUNTER:
169 mStringType = SENSOR_STRING_TYPE_STEP_COUNTER;
170 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
171 break;
172 case SENSOR_TYPE_STEP_DETECTOR:
173 mStringType = SENSOR_STRING_TYPE_STEP_DETECTOR;
174 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
175 break;
176 case SENSOR_TYPE_TEMPERATURE:
177 mStringType = SENSOR_STRING_TYPE_TEMPERATURE;
178 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
179 break;
180 case SENSOR_TYPE_TILT_DETECTOR:
181 mStringType = SENSOR_STRING_TYPE_TILT_DETECTOR;
182 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
183 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
184 mFlags |= SENSOR_FLAG_WAKE_UP;
185 }
186 break;
187 case SENSOR_TYPE_WAKE_GESTURE:
188 mStringType = SENSOR_STRING_TYPE_WAKE_GESTURE;
189 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
190 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
191 mFlags |= SENSOR_FLAG_WAKE_UP;
192 }
193 break;
194 case SENSOR_TYPE_GLANCE_GESTURE:
195 mStringType = SENSOR_STRING_TYPE_GLANCE_GESTURE;
196 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
197 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
198 mFlags |= SENSOR_FLAG_WAKE_UP;
199 }
200 break;
201 case SENSOR_TYPE_PICK_UP_GESTURE:
202 mStringType = SENSOR_STRING_TYPE_PICK_UP_GESTURE;
203 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
204 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
205 mFlags |= SENSOR_FLAG_WAKE_UP;
206 }
207 break;
208 case SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT:
209 mStringType = SENSOR_STRING_TYPE_LOW_LATENCY_OFFBODY_DETECT;
210 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
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 case SENSOR_TYPE_DYNAMIC_SENSOR_META:
220 mStringType = SENSOR_STRING_TYPE_DYNAMIC_SENSOR_META;
221 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE; // special trigger
222 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
223 mFlags |= SENSOR_FLAG_WAKE_UP;
224 }
225 break;
226 case SENSOR_TYPE_POSE_6DOF:
227 mStringType = SENSOR_STRING_TYPE_POSE_6DOF;
228 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
229 break;
230 case SENSOR_TYPE_STATIONARY_DETECT:
231 mStringType = SENSOR_STRING_TYPE_STATIONARY_DETECT;
232 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
233 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
234 mFlags |= SENSOR_FLAG_WAKE_UP;
235 }
236 break;
237 case SENSOR_TYPE_MOTION_DETECT:
238 mStringType = SENSOR_STRING_TYPE_MOTION_DETECT;
239 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
240 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
241 mFlags |= SENSOR_FLAG_WAKE_UP;
242 }
243 break;
244 case SENSOR_TYPE_HEART_BEAT:
245 mStringType = SENSOR_STRING_TYPE_HEART_BEAT;
246 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
247 break;
248
249 // TODO: Placeholder for LLOB sensor type
250
251
252 case SENSOR_TYPE_ACCELEROMETER_UNCALIBRATED:
253 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER_UNCALIBRATED;
254 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
255 break;
256 default:
257 // Only pipe the stringType, requiredPermission and flags for custom sensors.
258 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.stringType) {
259 mStringType = hwSensor.stringType;
260 }
261 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.requiredPermission) {
262 mRequiredPermission = hwSensor.requiredPermission;
263 if (!strcmp(mRequiredPermission, SENSOR_PERMISSION_BODY_SENSORS)) {
264 AppOpsManager appOps;
265 mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS));
266 }
267 }
268
269 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
270 mFlags = static_cast<uint32_t>(hwSensor.flags);
271 } else {
272 // This is an OEM defined sensor on an older HAL. Use minDelay to determine the
273 // reporting mode of the sensor.
274 if (mMinDelay > 0) {
275 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
276 } else if (mMinDelay == 0) {
277 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
278 } else if (mMinDelay < 0) {
279 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
280 }
281 }
282 break;
283 }
284
285 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
286 // Wake-up flag of HAL 1.3 and above is set here
287 mFlags |= (hwSensor.flags & SENSOR_FLAG_WAKE_UP);
288
289 // Log error if the reporting mode is not as expected, but respect HAL setting.
290 int actualReportingMode = (hwSensor.flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
291 int expectedReportingMode = (mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
292 if (actualReportingMode != expectedReportingMode) {
293 ALOGE("Reporting Mode incorrect: sensor %s handle=%#010" PRIx32 " type=%" PRId32 " "
294 "actual=%d expected=%d",
295 mName.string(), mHandle, mType, actualReportingMode, expectedReportingMode);
296 }
297 }
298
299 // Feature flags
300 // Set DYNAMIC_SENSOR_MASK and ADDITIONAL_INFO_MASK flag here. Compatible with HAL 1_3.
301 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
302 mFlags |= hwSensor.flags & (DYNAMIC_SENSOR_MASK | ADDITIONAL_INFO_MASK);
303 }
304 // Set DIRECT_REPORT_MASK and DIRECT_CHANNEL_MASK flags. Compatible with HAL 1_3.
305 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
306 // only on continuous sensors direct report mode is defined
307 if ((mFlags & REPORTING_MODE_MASK) == SENSOR_FLAG_CONTINUOUS_MODE) {
308 mFlags |= hwSensor.flags
309 & (SENSOR_FLAG_MASK_DIRECT_REPORT | SENSOR_FLAG_MASK_DIRECT_CHANNEL);
310 }
311 }
312 // Set DATA_INJECTION flag here. Defined in HAL 1_4.
313 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_4) {
314 mFlags |= (hwSensor.flags & DATA_INJECTION_MASK);
315 }
316
317 if (mRequiredPermission.length() > 0) {
318 // If the sensor is protected by a permission we need to know if it is
319 // a runtime one to determine whether we can use the permission cache.
320 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
321 if (binder != 0) {
322 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
323 mRequiredPermissionRuntime = permCtrl->isRuntimePermission(
324 String16(mRequiredPermission));
325 }
326 }
327 }
328
~Sensor()329 Sensor::~Sensor() {
330 }
331
getName() const332 const String8& Sensor::getName() const {
333 return mName;
334 }
335
getVendor() const336 const String8& Sensor::getVendor() const {
337 return mVendor;
338 }
339
getHandle() const340 int32_t Sensor::getHandle() const {
341 return mHandle;
342 }
343
getType() const344 int32_t Sensor::getType() const {
345 return mType;
346 }
347
getMinValue() const348 float Sensor::getMinValue() const {
349 return mMinValue;
350 }
351
getMaxValue() const352 float Sensor::getMaxValue() const {
353 return mMaxValue;
354 }
355
getResolution() const356 float Sensor::getResolution() const {
357 return mResolution;
358 }
359
getPowerUsage() const360 float Sensor::getPowerUsage() const {
361 return mPower;
362 }
363
getMinDelay() const364 int32_t Sensor::getMinDelay() const {
365 return mMinDelay;
366 }
367
getMinDelayNs() const368 nsecs_t Sensor::getMinDelayNs() const {
369 return getMinDelay() * 1000;
370 }
371
getVersion() const372 int32_t Sensor::getVersion() const {
373 return mVersion;
374 }
375
getFifoReservedEventCount() const376 uint32_t Sensor::getFifoReservedEventCount() const {
377 return mFifoReservedEventCount;
378 }
379
getFifoMaxEventCount() const380 uint32_t Sensor::getFifoMaxEventCount() const {
381 return mFifoMaxEventCount;
382 }
383
getStringType() const384 const String8& Sensor::getStringType() const {
385 return mStringType;
386 }
387
getRequiredPermission() const388 const String8& Sensor::getRequiredPermission() const {
389 return mRequiredPermission;
390 }
391
isRequiredPermissionRuntime() const392 bool Sensor::isRequiredPermissionRuntime() const {
393 return mRequiredPermissionRuntime;
394 }
395
getRequiredAppOp() const396 int32_t Sensor::getRequiredAppOp() const {
397 return mRequiredAppOp;
398 }
399
getMaxDelay() const400 int32_t Sensor::getMaxDelay() const {
401 return mMaxDelay;
402 }
403
getFlags() const404 uint32_t Sensor::getFlags() const {
405 return mFlags;
406 }
407
isWakeUpSensor() const408 bool Sensor::isWakeUpSensor() const {
409 return (mFlags & SENSOR_FLAG_WAKE_UP) != 0;
410 }
411
isDynamicSensor() const412 bool Sensor::isDynamicSensor() const {
413 return (mFlags & SENSOR_FLAG_DYNAMIC_SENSOR) != 0;
414 }
415
isDataInjectionSupported() const416 bool Sensor::isDataInjectionSupported() const {
417 return (mFlags & SENSOR_FLAG_DATA_INJECTION) != 0;
418 }
419
hasAdditionalInfo() const420 bool Sensor::hasAdditionalInfo() const {
421 return (mFlags & SENSOR_FLAG_ADDITIONAL_INFO) != 0;
422 }
423
getHighestDirectReportRateLevel() const424 int32_t Sensor::getHighestDirectReportRateLevel() const {
425 return ((mFlags & SENSOR_FLAG_MASK_DIRECT_REPORT) >> SENSOR_FLAG_SHIFT_DIRECT_REPORT);
426 }
427
isDirectChannelTypeSupported(int32_t sharedMemType) const428 bool Sensor::isDirectChannelTypeSupported(int32_t sharedMemType) const {
429 switch (sharedMemType) {
430 case SENSOR_DIRECT_MEM_TYPE_ASHMEM:
431 return mFlags & SENSOR_FLAG_DIRECT_CHANNEL_ASHMEM;
432 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
433 return mFlags & SENSOR_FLAG_DIRECT_CHANNEL_GRALLOC;
434 default:
435 return false;
436 }
437 }
438
getReportingMode() const439 int32_t Sensor::getReportingMode() const {
440 return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT);
441 }
442
getUuid() const443 const Sensor::uuid_t& Sensor::getUuid() const {
444 return mUuid;
445 }
446
setId(int32_t id)447 void Sensor::setId(int32_t id) {
448 mUuid.i64[0] = id;
449 mUuid.i64[1] = 0;
450 }
451
getId() const452 int32_t Sensor::getId() const {
453 return int32_t(mUuid.i64[0]);
454 }
455
getFlattenedSize() const456 size_t Sensor::getFlattenedSize() const {
457 size_t fixedSize =
458 sizeof(mVersion) + sizeof(mHandle) + sizeof(mType) +
459 sizeof(mMinValue) + sizeof(mMaxValue) + sizeof(mResolution) +
460 sizeof(mPower) + sizeof(mMinDelay) + sizeof(mFifoMaxEventCount) +
461 sizeof(mFifoMaxEventCount) + sizeof(mRequiredPermissionRuntime) +
462 sizeof(mRequiredAppOp) + sizeof(mMaxDelay) + sizeof(mFlags) + sizeof(mUuid);
463
464 size_t variableSize =
465 sizeof(uint32_t) + FlattenableUtils::align<4>(mName.length()) +
466 sizeof(uint32_t) + FlattenableUtils::align<4>(mVendor.length()) +
467 sizeof(uint32_t) + FlattenableUtils::align<4>(mStringType.length()) +
468 sizeof(uint32_t) + FlattenableUtils::align<4>(mRequiredPermission.length());
469
470 return fixedSize + variableSize;
471 }
472
flatten(void * buffer,size_t size) const473 status_t Sensor::flatten(void* buffer, size_t size) const {
474 if (size < getFlattenedSize()) {
475 return NO_MEMORY;
476 }
477
478 flattenString8(buffer, size, mName);
479 flattenString8(buffer, size, mVendor);
480 FlattenableUtils::write(buffer, size, mVersion);
481 FlattenableUtils::write(buffer, size, mHandle);
482 FlattenableUtils::write(buffer, size, mType);
483 FlattenableUtils::write(buffer, size, mMinValue);
484 FlattenableUtils::write(buffer, size, mMaxValue);
485 FlattenableUtils::write(buffer, size, mResolution);
486 FlattenableUtils::write(buffer, size, mPower);
487 FlattenableUtils::write(buffer, size, mMinDelay);
488 FlattenableUtils::write(buffer, size, mFifoReservedEventCount);
489 FlattenableUtils::write(buffer, size, mFifoMaxEventCount);
490 flattenString8(buffer, size, mStringType);
491 flattenString8(buffer, size, mRequiredPermission);
492 FlattenableUtils::write(buffer, size, mRequiredPermissionRuntime);
493 FlattenableUtils::write(buffer, size, mRequiredAppOp);
494 FlattenableUtils::write(buffer, size, mMaxDelay);
495 FlattenableUtils::write(buffer, size, mFlags);
496 if (mUuid.i64[1] != 0) {
497 // We should never hit this case with our current API, but we
498 // could via a careless API change. If that happens,
499 // this code will keep us from leaking our UUID (while probably
500 // breaking dynamic sensors). See b/29547335.
501 ALOGW("Sensor with UUID being flattened; sending 0. Expect "
502 "bad dynamic sensor behavior");
503 uuid_t tmpUuid; // default constructor makes this 0.
504 FlattenableUtils::write(buffer, size, tmpUuid);
505 } else {
506 FlattenableUtils::write(buffer, size, mUuid);
507 }
508 return NO_ERROR;
509 }
510
unflatten(void const * buffer,size_t size)511 status_t Sensor::unflatten(void const* buffer, size_t size) {
512 if (!unflattenString8(buffer, size, mName)) {
513 return NO_MEMORY;
514 }
515 if (!unflattenString8(buffer, size, mVendor)) {
516 return NO_MEMORY;
517 }
518
519 size_t fixedSize1 =
520 sizeof(mVersion) + sizeof(mHandle) + sizeof(mType) + sizeof(mMinValue) +
521 sizeof(mMaxValue) + sizeof(mResolution) + sizeof(mPower) + sizeof(mMinDelay) +
522 sizeof(mFifoMaxEventCount) + sizeof(mFifoMaxEventCount);
523 if (size < fixedSize1) {
524 return NO_MEMORY;
525 }
526
527 FlattenableUtils::read(buffer, size, mVersion);
528 FlattenableUtils::read(buffer, size, mHandle);
529 FlattenableUtils::read(buffer, size, mType);
530 FlattenableUtils::read(buffer, size, mMinValue);
531 FlattenableUtils::read(buffer, size, mMaxValue);
532 FlattenableUtils::read(buffer, size, mResolution);
533 FlattenableUtils::read(buffer, size, mPower);
534 FlattenableUtils::read(buffer, size, mMinDelay);
535 FlattenableUtils::read(buffer, size, mFifoReservedEventCount);
536 FlattenableUtils::read(buffer, size, mFifoMaxEventCount);
537
538 if (!unflattenString8(buffer, size, mStringType)) {
539 return NO_MEMORY;
540 }
541 if (!unflattenString8(buffer, size, mRequiredPermission)) {
542 return NO_MEMORY;
543 }
544
545 size_t fixedSize2 =
546 sizeof(mRequiredPermissionRuntime) + sizeof(mRequiredAppOp) + sizeof(mMaxDelay) +
547 sizeof(mFlags) + sizeof(mUuid);
548 if (size < fixedSize2) {
549 return NO_MEMORY;
550 }
551
552 FlattenableUtils::read(buffer, size, mRequiredPermissionRuntime);
553 FlattenableUtils::read(buffer, size, mRequiredAppOp);
554 FlattenableUtils::read(buffer, size, mMaxDelay);
555 FlattenableUtils::read(buffer, size, mFlags);
556 FlattenableUtils::read(buffer, size, mUuid);
557 return NO_ERROR;
558 }
559
flattenString8(void * & buffer,size_t & size,const String8 & string8)560 void Sensor::flattenString8(void*& buffer, size_t& size,
561 const String8& string8) {
562 uint32_t len = static_cast<uint32_t>(string8.length());
563 FlattenableUtils::write(buffer, size, len);
564 memcpy(static_cast<char*>(buffer), string8.string(), len);
565 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
566 }
567
unflattenString8(void const * & buffer,size_t & size,String8 & outputString8)568 bool Sensor::unflattenString8(void const*& buffer, size_t& size, String8& outputString8) {
569 uint32_t len;
570 if (size < sizeof(len)) {
571 return false;
572 }
573 FlattenableUtils::read(buffer, size, len);
574 if (size < len) {
575 return false;
576 }
577 outputString8.setTo(static_cast<char const*>(buffer), len);
578 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
579 return true;
580 }
581
582 // ----------------------------------------------------------------------------
583 }; // namespace android
584