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 #pragma once
     17 
     18 #include "common/libs/time/monotonic_time.h"
     19 #include "guest/hals/sensors/sensors_hal.h"
     20 #include "guest/libs/platform_support/api_level_fixes.h"
     21 
     22 namespace cvd {
     23 
     24 // Stores static information about a sensor.
     25 // Must be completely compatible with sensor_t (i.e. no additional
     26 // information or virtual functions)
     27 // so we can cast a list of SensorInfo to a list of sensor_t.
     28 class SensorInfo : public sensor_t {
     29  public:
     30   // Dummy, empty set of sensor information (value-initialized).
     31   SensorInfo() : sensor_t() {}
     32 
     33  private:
     34   SensorInfo(const char* name, const char* vendor, int version, int handle,
     35              int type, float max_range, float resolution, float power,
     36              int32_t min_delay, uint32_t fifo_reserved_event_count,
     37              uint32_t fifo_max_event_count, const char* string_type,
     38              const char* required_permission, int32_t max_delay,
     39              uint32_t reporting_mode);
     40 
     41   friend SensorInfo AccelerometerSensor();
     42   friend SensorInfo GyroscopeSensor();
     43   friend SensorInfo LightSensor();
     44   friend SensorInfo MagneticFieldSensor();
     45   friend SensorInfo PressureSensor();
     46   friend SensorInfo ProximitySensor();
     47   friend SensorInfo AmbientTempSensor();
     48   friend SensorInfo DeviceTempSensor();
     49   friend SensorInfo RelativeHumiditySensor();
     50 };
     51 
     52 SensorInfo AccelerometerSensor();
     53 SensorInfo GyroscopeSensor();
     54 SensorInfo LightSensor();
     55 SensorInfo MagneticFieldSensor();
     56 SensorInfo PressureSensor();
     57 SensorInfo ProximitySensor();
     58 SensorInfo AmbientTempSensor();
     59 SensorInfo DeviceTempSensor();
     60 SensorInfo RelativeHumiditySensor();
     61 
     62 // Stores the current state of a sensor.
     63 class SensorState {
     64  public:
     65   SensorState(SensorInfo info);
     66   virtual ~SensorState() {}
     67 
     68   // What this sensor is activated or not.
     69   bool enabled_;
     70   // Buffer of incoming events.
     71   sensors_event_t event_;
     72   // The deadline at which we should report the next sensor event
     73   // to the framework in order to meet our frequency constraints.
     74   // For disabled sensors, should be 'infinity'.
     75   cvd::time::MonotonicTimePoint deadline_;
     76   // Delay time between consecutive sensor samples, in ns.
     77   cvd::time::Nanoseconds sampling_period_;
     78 
     79   // Time 'infinity'.
     80   static const cvd::time::MonotonicTimePoint kInfinity;
     81 };
     82 
     83 namespace sensors_constants {
     84 // TODO: Verify these numbers.
     85 // Vendor of the hardware part.
     86 const char kVendor[] = "Google";
     87 // Version of the hardware part + driver. The value of this field
     88 // must increase when the driver is updated in a way that
     89 // changes the output of the sensor.
     90 const int kVersion = VSOC_SENSOR_DEVICE_VERSION;
     91 // Number of events reserved for this sensor in batch mode FIFO.
     92 // If it has its own FIFO, the size of that FIFO.
     93 const uint32_t kFifoReservedEventCount = 15;
     94 // Maximum events that can be batched. In a shared FIFO,
     95 // the size of that FIFO.
     96 const uint32_t kFifoMaxEventCount = 15;
     97 // Permission required to use this sensor, or empty string
     98 // if none required.
     99 const char kRequiredPermission[] = "";
    100 // Defined only for continuous mode and on-change sensors.
    101 // Delay corresponding with lowest frequency supported.
    102 const int32_t kMaxDelay = 5000000;
    103 
    104 // Name of this sensor. Must be unique.
    105 const char kAccelerometerName[] = "acceleration";
    106 const char kGyroscopeName[] = "gyroscope";
    107 const char kLightName[] = "light";
    108 const char kMagneticFieldName[] = "magnetic_field";
    109 const char kPressureName[] = "pressure";
    110 const char kProximityName[] = "proximity";
    111 const char kAmbientTempName[] = "ambient_temp";
    112 const char kDeviceTempName[] = "device_temp";
    113 const char kRelativeHumidityName[] = "relative_humidity";
    114 
    115 // Handle that identifies the sensor. This is used as an array index,
    116 // so must be unique in the range [0, # sensors)
    117 
    118 const int kAccelerometerHandle = 0;
    119 const int kGyroscopeHandle = 1;
    120 const int kLightHandle = 2;
    121 const int kMagneticFieldHandle = 3;
    122 const int kPressureHandle = 4;
    123 const int kProximityHandle = 5;
    124 const int kAmbientTempHandle = 6;
    125 const int kDeviceTempHandle = 7;
    126 const int kRelativeHumidityHandle = 8;
    127 
    128 // For continuous sensors, minimum sample period (in microseconds).
    129 // On-Change (0), One-shot (-1), and special (0).
    130 const int32_t kAccelerometerMinDelay = 4444;
    131 const int32_t kGyroscopeMinDelay = 4444;
    132 const int32_t kLightMinDelay = 0;
    133 const int32_t kMagneticFieldMinDelay = 14285;
    134 const int32_t kPressureMinDelay = 28571;
    135 const int32_t kProximityMinDelay = 0;
    136 const int32_t kAmbientTempMinDelay = 4444;
    137 const int32_t kDeviceTempMinDelay = 4444;
    138 const int32_t kRelativeHumidityMinDelay = 4444;
    139 
    140 // Maximum range of this sensor's value in SI units.
    141 const float kAccelerometerMaxRange = 39.226593f;
    142 const float kGyroscopeMaxRange = 8.726639f;
    143 const float kLightMaxRange = 10000.0f;
    144 const float kMagneticFieldMaxRange = 4911.9995f;
    145 const float kPressureMaxRange = 1100.0f;
    146 const float kProximityMaxRange = 5.0f;
    147 const float kAmbientTempMaxRange = 80.0f;
    148 const float kDeviceTempMaxRange = 80.0f;
    149 const float kRelativeHumidityMaxRange = 100;
    150 
    151 // Smallest difference between two values reported by this sensor.
    152 const float kAccelerometerResolution = 0.45f;
    153 const float kGyroscopeResolution = 10.0f;
    154 const float kLightResolution = 10.0f;
    155 const float kMagneticFieldResolution = 1.0f;
    156 const float kPressureResolution = 1.0f;
    157 const float kProximityResolution = 1.0f;
    158 const float kAmbientTempResolution = 1.0f;
    159 const float kDeviceTempResolution = 1.0f;
    160 const float kRelativeHumidityResolution = 1.0f;
    161 
    162 // Rough estimate of this sensor's power consumption in mA.
    163 const float kAccelerometerPower = 0.45f;
    164 const float kGyroscopePower = 3.6f;
    165 const float kLightPower = 0.175f;
    166 const float kMagneticFieldPower = 5.0f;
    167 const float kPressurePower = 0.004f;
    168 const float kProximityPower = 12.675f;
    169 const float kAmbientTempPower = 1.0f;
    170 const float kDeviceTempPower = 1.0f;
    171 const float kRelativeHumidityPower = 1.0f;
    172 
    173 // Type of this sensor, represented as a string.
    174 
    175 #if VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_2)
    176 const char kAccelerometerStringType[] = SENSOR_STRING_TYPE_ACCELEROMETER;
    177 const char kGyroscopeStringType[] = SENSOR_STRING_TYPE_GYROSCOPE;
    178 const char kLightStringType[] = SENSOR_STRING_TYPE_LIGHT;
    179 const char kMagneticFieldStringType[] = SENSOR_STRING_TYPE_MAGNETIC_FIELD;
    180 const char kPressureStringType[] = SENSOR_STRING_TYPE_PRESSURE;
    181 const char kProximityStringType[] = SENSOR_STRING_TYPE_PROXIMITY;
    182 const char kAmbientTempStringType[] = SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
    183 const char kDeviceTempStringType[] = SENSOR_STRING_TYPE_TEMPERATURE;
    184 const char kRelativeHumidityStringType[] = SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
    185 #else
    186 const char kAccelerometerStringType[] = "";
    187 const char kGyroscopeStringType[] = "";
    188 const char kLightStringType[] = "";
    189 const char kMagneticFieldStringType[] = "";
    190 const char kPressureStringType[] = "";
    191 const char kProximityStringType[] = "";
    192 const char kAmbientTempStringType[] = "";
    193 const char kDeviceTempStringType[] = "";
    194 const char kRelativeHumidityStringType[] = "";
    195 #endif
    196 
    197 #if VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_3)
    198 const uint32_t kAccelerometerReportingMode = SENSOR_FLAG_CONTINUOUS_MODE;
    199 const uint32_t kGyroscopeReportingMode = SENSOR_FLAG_CONTINUOUS_MODE;
    200 const uint32_t kLightReportingMode = SENSOR_FLAG_ON_CHANGE_MODE;
    201 const uint32_t kMagneticFieldReportingMode = SENSOR_FLAG_CONTINUOUS_MODE;
    202 const uint32_t kPressureReportingMode = SENSOR_FLAG_CONTINUOUS_MODE;
    203 const uint32_t kProximityReportingMode = SENSOR_FLAG_ON_CHANGE_MODE;
    204 const uint32_t kAmbientTempReportingMode = SENSOR_FLAG_ON_CHANGE_MODE;
    205 const uint32_t kDeviceTempReportingMode = SENSOR_FLAG_ON_CHANGE_MODE;
    206 const uint32_t kRelativeHumidityReportingMode = SENSOR_FLAG_ON_CHANGE_MODE;
    207 #else
    208 const uint32_t kAccelerometerReportingMode = 0;
    209 const uint32_t kGyroscopeReportingMode = 0;
    210 const uint32_t kLightReportingMode = 0;
    211 const uint32_t kMagneticFieldReportingMode = 0;
    212 const uint32_t kPressureReportingMode = 0;
    213 const uint32_t kProximityReportingMode = 0;
    214 const uint32_t kAmbientTempReportingMode = 0;
    215 const uint32_t kDeviceTempReportingMode = 0;
    216 const uint32_t kRelativeHumidityReportingMode = 0;
    217 #endif
    218 
    219 const bool kAccelerometerIsWakeup = false;
    220 const bool kGyroscopeIsWakeup = false;
    221 const bool kLightIsWakeup = false;
    222 const bool kMagneticFieldIsWakeup = false;
    223 const bool kPressureIsWakeup = false;
    224 const bool kProximityIsWakeup = true;
    225 const bool kAmbientTempIsWakeup = false;
    226 const bool kDeviceTempIsWakeup = false;
    227 const bool kRelativeHumidityIsWakeup = false;
    228 
    229 }  // namespace sensors_constants
    230 }  // namespace cvd
    231 
    232