Home | History | Annotate | Download | only in gui
      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 <stdint.h>
     18 #include <sys/types.h>
     19 
     20 #include <utils/Errors.h>
     21 #include <utils/String8.h>
     22 #include <utils/Flattenable.h>
     23 
     24 #include <hardware/sensors.h>
     25 
     26 #include <gui/Sensor.h>
     27 
     28 // ----------------------------------------------------------------------------
     29 namespace android {
     30 // ----------------------------------------------------------------------------
     31 
     32 Sensor::Sensor()
     33     : mHandle(0), mType(0),
     34       mMinValue(0), mMaxValue(0), mResolution(0),
     35       mPower(0), mMinDelay(0), mFifoReservedEventCount(0), mFifoMaxEventCount(0)
     36 {
     37 }
     38 
     39 Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion)
     40 {
     41     mName = hwSensor->name;
     42     mVendor = hwSensor->vendor;
     43     mVersion = hwSensor->version;
     44     mHandle = hwSensor->handle;
     45     mType = hwSensor->type;
     46     mMinValue = 0;                      // FIXME: minValue
     47     mMaxValue = hwSensor->maxRange;     // FIXME: maxValue
     48     mResolution = hwSensor->resolution;
     49     mPower = hwSensor->power;
     50     mMinDelay = hwSensor->minDelay;
     51     // Set fifo event count zero for older devices which do not support batching. Fused
     52     // sensors also have their fifo counts set to zero.
     53     if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
     54         mFifoReservedEventCount = hwSensor->fifoReservedEventCount;
     55         mFifoMaxEventCount = hwSensor->fifoMaxEventCount;
     56     } else {
     57         mFifoReservedEventCount = 0;
     58         mFifoMaxEventCount = 0;
     59     }
     60 }
     61 
     62 Sensor::~Sensor()
     63 {
     64 }
     65 
     66 const String8& Sensor::getName() const {
     67     return mName;
     68 }
     69 
     70 const String8& Sensor::getVendor() const {
     71     return mVendor;
     72 }
     73 
     74 int32_t Sensor::getHandle() const {
     75     return mHandle;
     76 }
     77 
     78 int32_t Sensor::getType() const {
     79     return mType;
     80 }
     81 
     82 float Sensor::getMinValue() const {
     83     return mMinValue;
     84 }
     85 
     86 float Sensor::getMaxValue() const {
     87     return mMaxValue;
     88 }
     89 
     90 float Sensor::getResolution() const {
     91     return mResolution;
     92 }
     93 
     94 float Sensor::getPowerUsage() const {
     95     return mPower;
     96 }
     97 
     98 int32_t Sensor::getMinDelay() const {
     99     return mMinDelay;
    100 }
    101 
    102 nsecs_t Sensor::getMinDelayNs() const {
    103     return getMinDelay() * 1000;
    104 }
    105 
    106 int32_t Sensor::getVersion() const {
    107     return mVersion;
    108 }
    109 
    110 int32_t Sensor::getFifoReservedEventCount() const {
    111     return mFifoReservedEventCount;
    112 }
    113 
    114 int32_t Sensor::getFifoMaxEventCount() const {
    115     return mFifoMaxEventCount;
    116 }
    117 
    118 size_t Sensor::getFlattenedSize() const
    119 {
    120     size_t fixedSize =
    121             sizeof(int32_t) * 3 +
    122             sizeof(float) * 4 +
    123             sizeof(int32_t) * 3;
    124 
    125     size_t variableSize =
    126             sizeof(int32_t) + FlattenableUtils::align<4>(mName.length()) +
    127             sizeof(int32_t) + FlattenableUtils::align<4>(mVendor.length());
    128 
    129     return fixedSize + variableSize;
    130 }
    131 
    132 status_t Sensor::flatten(void* buffer, size_t size) const {
    133     if (size < getFlattenedSize()) {
    134         return NO_MEMORY;
    135     }
    136 
    137     FlattenableUtils::write(buffer, size, mName.length());
    138     memcpy(static_cast<char*>(buffer), mName.string(), mName.length());
    139     FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(mName.length()));
    140 
    141     FlattenableUtils::write(buffer, size, mVendor.length());
    142     memcpy(static_cast<char*>(buffer), mVendor.string(), mVendor.length());
    143     FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(mVendor.length()));
    144 
    145     FlattenableUtils::write(buffer, size, mVersion);
    146     FlattenableUtils::write(buffer, size, mHandle);
    147     FlattenableUtils::write(buffer, size, mType);
    148     FlattenableUtils::write(buffer, size, mMinValue);
    149     FlattenableUtils::write(buffer, size, mMaxValue);
    150     FlattenableUtils::write(buffer, size, mResolution);
    151     FlattenableUtils::write(buffer, size, mPower);
    152     FlattenableUtils::write(buffer, size, mMinDelay);
    153     FlattenableUtils::write(buffer, size, mFifoReservedEventCount);
    154     FlattenableUtils::write(buffer, size, mFifoMaxEventCount);
    155     return NO_ERROR;
    156 }
    157 
    158 status_t Sensor::unflatten(void const* buffer, size_t size) {
    159     size_t len;
    160 
    161     if (size < sizeof(size_t)) {
    162         return NO_MEMORY;
    163     }
    164     FlattenableUtils::read(buffer, size, len);
    165     if (size < len) {
    166         return NO_MEMORY;
    167     }
    168     mName.setTo(static_cast<char const*>(buffer), len);
    169     FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
    170 
    171 
    172     if (size < sizeof(size_t)) {
    173         return NO_MEMORY;
    174     }
    175     FlattenableUtils::read(buffer, size, len);
    176     if (size < len) {
    177         return NO_MEMORY;
    178     }
    179     mVendor.setTo(static_cast<char const*>(buffer), len);
    180     FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len));
    181 
    182     size_t fixedSize =
    183             sizeof(int32_t) * 3 +
    184             sizeof(float) * 4 +
    185             sizeof(int32_t) * 3;
    186 
    187     if (size < fixedSize) {
    188         return NO_MEMORY;
    189     }
    190 
    191     FlattenableUtils::read(buffer, size, mVersion);
    192     FlattenableUtils::read(buffer, size, mHandle);
    193     FlattenableUtils::read(buffer, size, mType);
    194     FlattenableUtils::read(buffer, size, mMinValue);
    195     FlattenableUtils::read(buffer, size, mMaxValue);
    196     FlattenableUtils::read(buffer, size, mResolution);
    197     FlattenableUtils::read(buffer, size, mPower);
    198     FlattenableUtils::read(buffer, size, mMinDelay);
    199     FlattenableUtils::read(buffer, size, mFifoReservedEventCount);
    200     FlattenableUtils::read(buffer, size, mFifoMaxEventCount);
    201     return NO_ERROR;
    202 }
    203 
    204 // ----------------------------------------------------------------------------
    205 }; // namespace android
    206