Home | History | Annotate | Download | only in sensors
      1 /*
      2  * Copyright (C) 2017 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 #include "guest/hals/sensors/vsoc_sensors.h"
     17 
     18 #include <limits>
     19 
     20 #include "guest/hals/sensors/sensors.h"
     21 
     22 namespace cvd {
     23 namespace {
     24 const cvd::time::Milliseconds kDefaultSamplingRate(200);
     25 
     26 #if !VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_3)
     27 namespace {
     28 const int SENSOR_FLAG_WAKE_UP = 0;
     29 }
     30 #endif
     31 
     32 timespec infinity() {
     33   timespec ts;
     34   ts.tv_sec = std::numeric_limits<time_t>::max();
     35   ts.tv_nsec = 0;
     36   return ts;
     37 }
     38 }  // namespace
     39 
     40 const cvd::time::MonotonicTimePoint SensorState::kInfinity =
     41     cvd::time::MonotonicTimePoint(infinity());
     42 
     43 SensorState::SensorState(SensorInfo info)
     44     : enabled_(false),
     45       event_(),
     46       deadline_(kInfinity),
     47       sampling_period_(kDefaultSamplingRate) {
     48   event_.sensor = info.handle;
     49   event_.type = info.type;
     50 }
     51 
     52 SensorInfo::SensorInfo(const char* name, const char* vendor, int version,
     53                        int handle, int type, float max_range, float resolution,
     54                        float power, int32_t min_delay,
     55                        uint32_t fifo_reserved_event_count,
     56                        uint32_t fifo_max_event_count, const char* string_type,
     57                        const char* required_permission, int32_t max_delay,
     58                        uint32_t reporting_mode) {
     59   this->name = name;
     60   this->vendor = vendor;
     61   this->version = version;
     62   this->handle = handle;
     63   this->type = type;
     64   this->maxRange = max_range;
     65   this->resolution = resolution;
     66   this->power = power;
     67   this->minDelay = min_delay;
     68 #if VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_1)
     69   this->fifoReservedEventCount = fifo_reserved_event_count;
     70   this->fifoMaxEventCount = fifo_max_event_count;
     71 #endif
     72 #if VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_2)
     73   this->stringType = string_type;
     74   this->requiredPermission = required_permission;
     75 #endif
     76 #if VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_3)
     77   this->maxDelay = max_delay;
     78   this->flags = reporting_mode;
     79 #endif
     80 }
     81 
     82 namespace sc = sensors_constants;
     83 
     84 SensorInfo AccelerometerSensor() {
     85   uint32_t flags = sc::kAccelerometerReportingMode |
     86       (sc::kAccelerometerIsWakeup ? SENSOR_FLAG_WAKE_UP : 0);
     87 
     88   return SensorInfo(sc::kAccelerometerName, sc::kVendor, sc::kVersion,
     89                     sc::kAccelerometerHandle, SENSOR_TYPE_ACCELEROMETER,
     90                     sc::kAccelerometerMaxRange, sc::kAccelerometerResolution,
     91                     sc::kAccelerometerPower, sc::kAccelerometerMinDelay,
     92                     sc::kFifoReservedEventCount, sc::kFifoMaxEventCount,
     93                     sc::kAccelerometerStringType, sc::kRequiredPermission,
     94                     sc::kMaxDelay, flags);
     95 }
     96 
     97 SensorInfo GyroscopeSensor() {
     98   uint32_t flags = sc::kGyroscopeReportingMode |
     99       (sc::kGyroscopeIsWakeup ? SENSOR_FLAG_WAKE_UP : 0);
    100 
    101   return SensorInfo(
    102       sc::kGyroscopeName, sc::kVendor, sc::kVersion, sc::kGyroscopeHandle,
    103       SENSOR_TYPE_GYROSCOPE, sc::kGyroscopeMaxRange, sc::kGyroscopeResolution,
    104       sc::kGyroscopePower, sc::kGyroscopeMinDelay, sc::kFifoReservedEventCount,
    105       sc::kFifoMaxEventCount, sc::kGyroscopeStringType, sc::kRequiredPermission,
    106       sc::kMaxDelay, flags);
    107 }
    108 
    109 SensorInfo LightSensor() {
    110   uint32_t flags = sc::kLightReportingMode |
    111       (sc::kLightIsWakeup ? SENSOR_FLAG_WAKE_UP : 0);
    112 
    113   return SensorInfo(sc::kLightName, sc::kVendor, sc::kVersion, sc::kLightHandle,
    114                     SENSOR_TYPE_LIGHT, sc::kLightMaxRange, sc::kLightResolution,
    115                     sc::kLightPower, sc::kLightMinDelay,
    116                     sc::kFifoReservedEventCount, sc::kFifoMaxEventCount,
    117                     sc::kLightStringType, sc::kRequiredPermission,
    118                     sc::kMaxDelay, flags);
    119 }
    120 
    121 SensorInfo MagneticFieldSensor() {
    122   uint32_t flags = sc::kMagneticFieldReportingMode |
    123       (sc::kMagneticFieldIsWakeup ? SENSOR_FLAG_WAKE_UP : 0);
    124 
    125   return SensorInfo(sc::kMagneticFieldName, sc::kVendor, sc::kVersion,
    126                     sc::kMagneticFieldHandle, SENSOR_TYPE_MAGNETIC_FIELD,
    127                     sc::kMagneticFieldMaxRange, sc::kMagneticFieldResolution,
    128                     sc::kMagneticFieldPower, sc::kMagneticFieldMinDelay,
    129                     sc::kFifoReservedEventCount, sc::kFifoMaxEventCount,
    130                     sc::kMagneticFieldStringType, sc::kRequiredPermission,
    131                     sc::kMaxDelay, flags);
    132 }
    133 
    134 SensorInfo PressureSensor() {
    135   uint32_t flags = sc::kPressureReportingMode |
    136       (sc::kPressureIsWakeup ? SENSOR_FLAG_WAKE_UP : 0);
    137 
    138   return SensorInfo(
    139       sc::kPressureName, sc::kVendor, sc::kVersion, sc::kPressureHandle,
    140       SENSOR_TYPE_PRESSURE, sc::kPressureMaxRange, sc::kPressureResolution,
    141       sc::kPressurePower, sc::kPressureMinDelay, sc::kFifoReservedEventCount,
    142       sc::kFifoMaxEventCount, sc::kPressureStringType, sc::kRequiredPermission,
    143       sc::kMaxDelay, flags);
    144 }
    145 
    146 SensorInfo ProximitySensor() {
    147   uint32_t flags = sc::kProximityReportingMode |
    148       (sc::kProximityIsWakeup ? SENSOR_FLAG_WAKE_UP : 0);
    149 
    150   return SensorInfo(
    151       sc::kProximityName, sc::kVendor, sc::kVersion, sc::kProximityHandle,
    152       SENSOR_TYPE_PROXIMITY, sc::kProximityMaxRange, sc::kProximityResolution,
    153       sc::kProximityPower, sc::kProximityMinDelay, sc::kFifoReservedEventCount,
    154       sc::kFifoMaxEventCount, sc::kProximityStringType, sc::kRequiredPermission,
    155       sc::kMaxDelay, flags);
    156 }
    157 
    158 SensorInfo AmbientTempSensor() {
    159   uint32_t flags = sc::kAmbientTempReportingMode |
    160       (sc::kAmbientTempIsWakeup ? SENSOR_FLAG_WAKE_UP : 0);
    161 
    162   return SensorInfo(sc::kAmbientTempName, sc::kVendor, sc::kVersion,
    163                     sc::kAmbientTempHandle, SENSOR_TYPE_AMBIENT_TEMPERATURE,
    164                     sc::kAmbientTempMaxRange, sc::kAmbientTempResolution,
    165                     sc::kAmbientTempPower, sc::kAmbientTempMinDelay,
    166                     sc::kFifoReservedEventCount, sc::kFifoMaxEventCount,
    167                     sc::kAmbientTempStringType, sc::kRequiredPermission,
    168                     sc::kMaxDelay, flags);
    169 }
    170 
    171 SensorInfo DeviceTempSensor() {
    172   uint32_t flags = sc::kDeviceTempReportingMode |
    173       (sc::kDeviceTempIsWakeup ? SENSOR_FLAG_WAKE_UP : 0);
    174 
    175   return SensorInfo(sc::kDeviceTempName, sc::kVendor, sc::kVersion,
    176                     sc::kDeviceTempHandle, SENSOR_TYPE_TEMPERATURE,
    177                     sc::kDeviceTempMaxRange, sc::kDeviceTempResolution,
    178                     sc::kDeviceTempPower, sc::kDeviceTempMinDelay,
    179                     sc::kFifoReservedEventCount, sc::kFifoMaxEventCount,
    180                     sc::kDeviceTempStringType, sc::kRequiredPermission,
    181                     sc::kMaxDelay, flags);
    182 }
    183 
    184 SensorInfo RelativeHumiditySensor() {
    185   uint32_t flags = sc::kRelativeHumidityReportingMode |
    186       (sc::kRelativeHumidityIsWakeup ? SENSOR_FLAG_WAKE_UP : 0);
    187 
    188   return SensorInfo(sc::kRelativeHumidityName, sc::kVendor, sc::kVersion,
    189                     sc::kRelativeHumidityHandle, SENSOR_TYPE_RELATIVE_HUMIDITY,
    190                     sc::kRelativeHumidityMaxRange,
    191                     sc::kRelativeHumidityResolution, sc::kRelativeHumidityPower,
    192                     sc::kRelativeHumidityMinDelay, sc::kFifoReservedEventCount,
    193                     sc::kFifoMaxEventCount, sc::kRelativeHumidityStringType,
    194                     sc::kRequiredPermission, sc::kMaxDelay,
    195                     flags);
    196 }
    197 
    198 }  // namespace cvd
    199