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