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
18 #include <binder/AppOpsManager.h>
19 #include <binder/IServiceManager.h>
20 #include <gui/Sensor.h>
21 #include <hardware/sensors.h>
22 #include <log/log.h>
23 #include <utils/Errors.h>
24 #include <utils/String8.h>
25 #include <utils/Flattenable.h>
26
27 #include <inttypes.h>
28 #include <stdint.h>
29 #include <sys/types.h>
30 #include <sys/limits.h>
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(0),
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 mName = hwSensor.name;
50 mVendor = hwSensor.vendor;
51 mVersion = hwSensor.version;
52 mHandle = hwSensor.handle;
53 mType = hwSensor.type;
54 mMinValue = 0; // FIXME: minValue
55 mMaxValue = hwSensor.maxRange; // FIXME: maxValue
56 mResolution = hwSensor.resolution;
57 mPower = hwSensor.power;
58 mMinDelay = hwSensor.minDelay;
59 mFlags = 0;
60 mUuid = uuid;
61
62 // Set fifo event count zero for older devices which do not support batching. Fused
63 // sensors also have their fifo counts set to zero.
64 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) {
65 mFifoReservedEventCount = hwSensor.fifoReservedEventCount;
66 mFifoMaxEventCount = hwSensor.fifoMaxEventCount;
67 } else {
68 mFifoReservedEventCount = 0;
69 mFifoMaxEventCount = 0;
70 }
71
72 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
73 if (hwSensor.maxDelay > INT_MAX) {
74 // Max delay is declared as a 64 bit integer for 64 bit architectures. But it should
75 // always fit in a 32 bit integer, log error and cap it to INT_MAX.
76 ALOGE("Sensor maxDelay overflow error %s %" PRId64, mName.string(),
77 static_cast<int64_t>(hwSensor.maxDelay));
78 mMaxDelay = INT_MAX;
79 } else {
80 mMaxDelay = static_cast<int32_t>(hwSensor.maxDelay);
81 }
82 } else {
83 // For older hals set maxDelay to 0.
84 mMaxDelay = 0;
85 }
86
87 // Ensure existing sensors have correct string type, required permissions and reporting mode.
88 // Set reportingMode for all android defined sensor types, set wake-up flag only for proximity
89 // sensor, significant motion, tilt, pick_up gesture, wake gesture and glance gesture on older
90 // HALs. Newer HALs can define both wake-up and non wake-up proximity sensors.
91 // All the OEM defined defined sensors have flags set to whatever is provided by the HAL.
92 switch (mType) {
93 case SENSOR_TYPE_ACCELEROMETER:
94 mStringType = SENSOR_STRING_TYPE_ACCELEROMETER;
95 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
96 break;
97 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
98 mStringType = SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
99 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
100 break;
101 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
102 mStringType = SENSOR_STRING_TYPE_GAME_ROTATION_VECTOR;
103 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
104 break;
105 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
106 mStringType = SENSOR_STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR;
107 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
108 break;
109 case SENSOR_TYPE_GRAVITY:
110 mStringType = SENSOR_STRING_TYPE_GRAVITY;
111 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
112 break;
113 case SENSOR_TYPE_GYROSCOPE:
114 mStringType = SENSOR_STRING_TYPE_GYROSCOPE;
115 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
116 break;
117 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
118 mStringType = SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
119 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
120 break;
121 case SENSOR_TYPE_HEART_RATE: {
122 mStringType = SENSOR_STRING_TYPE_HEART_RATE;
123 mRequiredPermission = SENSOR_PERMISSION_BODY_SENSORS;
124 AppOpsManager appOps;
125 mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS));
126 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
127 } break;
128 case SENSOR_TYPE_LIGHT:
129 mStringType = SENSOR_STRING_TYPE_LIGHT;
130 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
131 break;
132 case SENSOR_TYPE_LINEAR_ACCELERATION:
133 mStringType = SENSOR_STRING_TYPE_LINEAR_ACCELERATION;
134 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
135 break;
136 case SENSOR_TYPE_MAGNETIC_FIELD:
137 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD;
138 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
139 break;
140 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
141 mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
142 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
143 break;
144 case SENSOR_TYPE_ORIENTATION:
145 mStringType = SENSOR_STRING_TYPE_ORIENTATION;
146 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
147 break;
148 case SENSOR_TYPE_PRESSURE:
149 mStringType = SENSOR_STRING_TYPE_PRESSURE;
150 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
151 break;
152 case SENSOR_TYPE_PROXIMITY:
153 mStringType = SENSOR_STRING_TYPE_PROXIMITY;
154 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
155 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
156 mFlags |= SENSOR_FLAG_WAKE_UP;
157 }
158 break;
159 case SENSOR_TYPE_RELATIVE_HUMIDITY:
160 mStringType = SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
161 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
162 break;
163 case SENSOR_TYPE_ROTATION_VECTOR:
164 mStringType = SENSOR_STRING_TYPE_ROTATION_VECTOR;
165 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
166 break;
167 case SENSOR_TYPE_SIGNIFICANT_MOTION:
168 mStringType = SENSOR_STRING_TYPE_SIGNIFICANT_MOTION;
169 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
170 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
171 mFlags |= SENSOR_FLAG_WAKE_UP;
172 }
173 break;
174 case SENSOR_TYPE_STEP_COUNTER:
175 mStringType = SENSOR_STRING_TYPE_STEP_COUNTER;
176 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
177 break;
178 case SENSOR_TYPE_STEP_DETECTOR:
179 mStringType = SENSOR_STRING_TYPE_STEP_DETECTOR;
180 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
181 break;
182 case SENSOR_TYPE_TEMPERATURE:
183 mStringType = SENSOR_STRING_TYPE_TEMPERATURE;
184 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
185 break;
186 case SENSOR_TYPE_TILT_DETECTOR:
187 mStringType = SENSOR_STRING_TYPE_TILT_DETECTOR;
188 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
189 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
190 mFlags |= SENSOR_FLAG_WAKE_UP;
191 }
192 break;
193 case SENSOR_TYPE_WAKE_GESTURE:
194 mStringType = SENSOR_STRING_TYPE_WAKE_GESTURE;
195 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
196 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
197 mFlags |= SENSOR_FLAG_WAKE_UP;
198 }
199 break;
200 case SENSOR_TYPE_GLANCE_GESTURE:
201 mStringType = SENSOR_STRING_TYPE_GLANCE_GESTURE;
202 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
203 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
204 mFlags |= SENSOR_FLAG_WAKE_UP;
205 }
206 break;
207 case SENSOR_TYPE_PICK_UP_GESTURE:
208 mStringType = SENSOR_STRING_TYPE_PICK_UP_GESTURE;
209 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
210 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
211 mFlags |= SENSOR_FLAG_WAKE_UP;
212 }
213 break;
214 case SENSOR_TYPE_WRIST_TILT_GESTURE:
215 mStringType = SENSOR_STRING_TYPE_WRIST_TILT_GESTURE;
216 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
217 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
218 mFlags |= SENSOR_FLAG_WAKE_UP;
219 }
220 break;
221 case SENSOR_TYPE_DYNAMIC_SENSOR_META:
222 mStringType = SENSOR_STRING_TYPE_DYNAMIC_SENSOR_META;
223 mFlags = SENSOR_FLAG_SPECIAL_REPORTING_MODE; // special trigger and non-wake up
224 break;
225 case SENSOR_TYPE_POSE_6DOF:
226 mStringType = SENSOR_STRING_TYPE_POSE_6DOF;
227 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
228 break;
229 case SENSOR_TYPE_STATIONARY_DETECT:
230 mStringType = SENSOR_STRING_TYPE_STATIONARY_DETECT;
231 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
232 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
233 mFlags |= SENSOR_FLAG_WAKE_UP;
234 }
235 break;
236 case SENSOR_TYPE_MOTION_DETECT:
237 mStringType = SENSOR_STRING_TYPE_MOTION_DETECT;
238 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
239 if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) {
240 mFlags |= SENSOR_FLAG_WAKE_UP;
241 }
242 break;
243 case SENSOR_TYPE_HEART_BEAT:
244 mStringType = SENSOR_STRING_TYPE_HEART_BEAT;
245 mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
246 break;
247 default:
248 // Only pipe the stringType, requiredPermission and flags for custom sensors.
249 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.stringType) {
250 mStringType = hwSensor.stringType;
251 }
252 if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor.requiredPermission) {
253 mRequiredPermission = hwSensor.requiredPermission;
254 if (!strcmp(mRequiredPermission, SENSOR_PERMISSION_BODY_SENSORS)) {
255 AppOpsManager appOps;
256 mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS));
257 }
258 }
259
260 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
261 mFlags = static_cast<uint32_t>(hwSensor.flags);
262 } else {
263 // This is an OEM defined sensor on an older HAL. Use minDelay to determine the
264 // reporting mode of the sensor.
265 if (mMinDelay > 0) {
266 mFlags |= SENSOR_FLAG_CONTINUOUS_MODE;
267 } else if (mMinDelay == 0) {
268 mFlags |= SENSOR_FLAG_ON_CHANGE_MODE;
269 } else if (mMinDelay < 0) {
270 mFlags |= SENSOR_FLAG_ONE_SHOT_MODE;
271 }
272 }
273 break;
274 }
275
276 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
277 // Wake-up flag of HAL 1.3 and above is set here
278 mFlags |= (hwSensor.flags & SENSOR_FLAG_WAKE_UP);
279
280 // Log error if the reporting mode is not as expected, but respect HAL setting.
281 int actualReportingMode = (hwSensor.flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
282 int expectedReportingMode = (mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT;
283 if (actualReportingMode != expectedReportingMode) {
284 ALOGE("Reporting Mode incorrect: sensor %s handle=%#010" PRIx32 " type=%" PRId32 " "
285 "actual=%d expected=%d",
286 mName.string(), mHandle, mType, actualReportingMode, expectedReportingMode);
287 }
288 }
289
290 // Feature flags
291 // Set DYNAMIC_SENSOR_MASK and ADDITIONAL_INFO_MASK flag here. Compatible with HAL 1_3.
292 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) {
293 mFlags |= (hwSensor.flags & (DYNAMIC_SENSOR_MASK | ADDITIONAL_INFO_MASK));
294 }
295 // Set DATA_INJECTION flag here. Defined in HAL 1_4.
296 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_4) {
297 mFlags |= (hwSensor.flags & DATA_INJECTION_MASK);
298 }
299
300 if (mRequiredPermission.length() > 0) {
301 // If the sensor is protected by a permission we need to know if it is
302 // a runtime one to determine whether we can use the permission cache.
303 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
304 if (binder != 0) {
305 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
306 mRequiredPermissionRuntime = permCtrl->isRuntimePermission(
307 String16(mRequiredPermission));
308 }
309 }
310 }
311
~Sensor()312 Sensor::~Sensor() {
313 }
314
getName() const315 const String8& Sensor::getName() const {
316 return mName;
317 }
318
getVendor() const319 const String8& Sensor::getVendor() const {
320 return mVendor;
321 }
322
getHandle() const323 int32_t Sensor::getHandle() const {
324 return mHandle;
325 }
326
getType() const327 int32_t Sensor::getType() const {
328 return mType;
329 }
330
getMinValue() const331 float Sensor::getMinValue() const {
332 return mMinValue;
333 }
334
getMaxValue() const335 float Sensor::getMaxValue() const {
336 return mMaxValue;
337 }
338
getResolution() const339 float Sensor::getResolution() const {
340 return mResolution;
341 }
342
getPowerUsage() const343 float Sensor::getPowerUsage() const {
344 return mPower;
345 }
346
getMinDelay() const347 int32_t Sensor::getMinDelay() const {
348 return mMinDelay;
349 }
350
getMinDelayNs() const351 nsecs_t Sensor::getMinDelayNs() const {
352 return getMinDelay() * 1000;
353 }
354
getVersion() const355 int32_t Sensor::getVersion() const {
356 return mVersion;
357 }
358
getFifoReservedEventCount() const359 uint32_t Sensor::getFifoReservedEventCount() const {
360 return mFifoReservedEventCount;
361 }
362
getFifoMaxEventCount() const363 uint32_t Sensor::getFifoMaxEventCount() const {
364 return mFifoMaxEventCount;
365 }
366
getStringType() const367 const String8& Sensor::getStringType() const {
368 return mStringType;
369 }
370
getRequiredPermission() const371 const String8& Sensor::getRequiredPermission() const {
372 return mRequiredPermission;
373 }
374
isRequiredPermissionRuntime() const375 bool Sensor::isRequiredPermissionRuntime() const {
376 return mRequiredPermissionRuntime;
377 }
378
getRequiredAppOp() const379 int32_t Sensor::getRequiredAppOp() const {
380 return mRequiredAppOp;
381 }
382
getMaxDelay() const383 int32_t Sensor::getMaxDelay() const {
384 return mMaxDelay;
385 }
386
getFlags() const387 uint32_t Sensor::getFlags() const {
388 return mFlags;
389 }
390
isWakeUpSensor() const391 bool Sensor::isWakeUpSensor() const {
392 return (mFlags & SENSOR_FLAG_WAKE_UP) != 0;
393 }
394
isDynamicSensor() const395 bool Sensor::isDynamicSensor() const {
396 return (mFlags & SENSOR_FLAG_DYNAMIC_SENSOR) != 0;
397 }
398
hasAdditionalInfo() const399 bool Sensor::hasAdditionalInfo() const {
400 return (mFlags & SENSOR_FLAG_ADDITIONAL_INFO) != 0;
401 }
402
getReportingMode() const403 int32_t Sensor::getReportingMode() const {
404 return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT);
405 }
406
getUuid() const407 const Sensor::uuid_t& Sensor::getUuid() const {
408 return mUuid;
409 }
410
setId(int32_t id)411 void Sensor::setId(int32_t id) {
412 mUuid.i64[0] = id;
413 mUuid.i64[1] = 0;
414 }
415
getId() const416 int32_t Sensor::getId() const {
417 return int32_t(mUuid.i64[0]);
418 }
419
getFlattenedSize() const420 size_t Sensor::getFlattenedSize() const {
421 size_t fixedSize =
422 sizeof(mVersion) + sizeof(mHandle) + sizeof(mType) +
423 sizeof(mMinValue) + sizeof(mMaxValue) + sizeof(mResolution) +
424 sizeof(mPower) + sizeof(mMinDelay) + sizeof(mFifoMaxEventCount) +
425 sizeof(mFifoMaxEventCount) + sizeof(mRequiredPermissionRuntime) +
426 sizeof(mRequiredAppOp) + sizeof(mMaxDelay) + sizeof(mFlags) + sizeof(mUuid);
427
428 size_t variableSize =
429 sizeof(uint32_t) + FlattenableUtils::align<4>(mName.length()) +
430 sizeof(uint32_t) + FlattenableUtils::align<4>(mVendor.length()) +
431 sizeof(uint32_t) + FlattenableUtils::align<4>(mStringType.length()) +
432 sizeof(uint32_t) + FlattenableUtils::align<4>(mRequiredPermission.length());
433
434 return fixedSize + variableSize;
435 }
436
flatten(void * buffer,size_t size) const437 status_t Sensor::flatten(void* buffer, size_t size) const {
438 if (size < getFlattenedSize()) {
439 return NO_MEMORY;
440 }
441
442 flattenString8(buffer, size, mName);
443 flattenString8(buffer, size, mVendor);
444 FlattenableUtils::write(buffer, size, mVersion);
445 FlattenableUtils::write(buffer, size, mHandle);
446 FlattenableUtils::write(buffer, size, mType);
447 FlattenableUtils::write(buffer, size, mMinValue);
448 FlattenableUtils::write(buffer, size, mMaxValue);
449 FlattenableUtils::write(buffer, size, mResolution);
450 FlattenableUtils::write(buffer, size, mPower);
451 FlattenableUtils::write(buffer, size, mMinDelay);
452 FlattenableUtils::write(buffer, size, mFifoReservedEventCount);
453 FlattenableUtils::write(buffer, size, mFifoMaxEventCount);
454 flattenString8(buffer, size, mStringType);
455 flattenString8(buffer, size, mRequiredPermission);
456 FlattenableUtils::write(buffer, size, mRequiredPermissionRuntime);
457 FlattenableUtils::write(buffer, size, mRequiredAppOp);
458 FlattenableUtils::write(buffer, size, mMaxDelay);
459 FlattenableUtils::write(buffer, size, mFlags);
460 if (mUuid.i64[1] != 0) {
461 // We should never hit this case with our current API, but we
462 // could via a careless API change. If that happens,
463 // this code will keep us from leaking our UUID (while probably
464 // breaking dynamic sensors). See b/29547335.
465 ALOGW("Sensor with UUID being flattened; sending 0. Expect "
466 "bad dynamic sensor behavior");
467 uuid_t tmpUuid; // default constructor makes this 0.
468 FlattenableUtils::write(buffer, size, tmpUuid);
469 } else {
470 FlattenableUtils::write(buffer, size, mUuid);
471 }
472 return NO_ERROR;
473 }
474
unflatten(void const * buffer,size_t size)475 status_t Sensor::unflatten(void const* buffer, size_t size) {
476 if (!unflattenString8(buffer, size, mName)) {
477 return NO_MEMORY;
478 }
479 if (!unflattenString8(buffer, size, mVendor)) {
480 return NO_MEMORY;
481 }
482
483 size_t fixedSize1 =
484 sizeof(mVersion) + sizeof(mHandle) + sizeof(mType) + sizeof(mMinValue) +
485 sizeof(mMaxValue) + sizeof(mResolution) + sizeof(mPower) + sizeof(mMinDelay) +
486 sizeof(mFifoMaxEventCount) + sizeof(mFifoMaxEventCount);
487 if (size < fixedSize1) {
488 return NO_MEMORY;
489 }
490
491 FlattenableUtils::read(buffer, size, mVersion);
492 FlattenableUtils::read(buffer, size, mHandle);
493 FlattenableUtils::read(buffer, size, mType);
494 FlattenableUtils::read(buffer, size, mMinValue);
495 FlattenableUtils::read(buffer, size, mMaxValue);
496 FlattenableUtils::read(buffer, size, mResolution);
497 FlattenableUtils::read(buffer, size, mPower);
498 FlattenableUtils::read(buffer, size, mMinDelay);
499 FlattenableUtils::read(buffer, size, mFifoReservedEventCount);
500 FlattenableUtils::read(buffer, size, mFifoMaxEventCount);
501
502 if (!unflattenString8(buffer, size, mStringType)) {
503 return NO_MEMORY;
504 }
505 if (!unflattenString8(buffer, size, mRequiredPermission)) {
506 return NO_MEMORY;
507 }
508
509 size_t fixedSize2 =
510 sizeof(mRequiredPermissionRuntime) + sizeof(mRequiredAppOp) + sizeof(mMaxDelay) +
511 sizeof(mFlags) + sizeof(mUuid);
512 if (size < fixedSize2) {
513 return NO_MEMORY;
514 }
515
516 FlattenableUtils::read(buffer, size, mRequiredPermissionRuntime);
517 FlattenableUtils::read(buffer, size, mRequiredAppOp);
518 FlattenableUtils::read(buffer, size, mMaxDelay);
519 FlattenableUtils::read(buffer, size, mFlags);
520 FlattenableUtils::read(buffer, size, mUuid);
521 return NO_ERROR;
522 }
523
flattenString8(void * & buffer,size_t & size,const String8 & string8)524 void Sensor::flattenString8(void*& buffer, size_t& size,
525 const String8& string8) {
526 uint32_t len = static_cast<uint32_t>(string8.length());
527 FlattenableUtils::write(buffer, size, len);
528 memcpy(static_cast<char*>(buffer), string8.string(), len);
529 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
530 }
531
unflattenString8(void const * & buffer,size_t & size,String8 & outputString8)532 bool Sensor::unflattenString8(void const*& buffer, size_t& size, String8& outputString8) {
533 uint32_t len;
534 if (size < sizeof(len)) {
535 return false;
536 }
537 FlattenableUtils::read(buffer, size, len);
538 if (size < len) {
539 return false;
540 }
541 outputString8.setTo(static_cast<char const*>(buffer), len);
542 FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
543 return true;
544 }
545
546 // ----------------------------------------------------------------------------
547 }; // namespace android
548