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