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