Home | History | Annotate | Download | only in default
      1 /*
      2  * Copyright (C) 2018 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.h"
     18 
     19 #include <utils/SystemClock.h>
     20 
     21 #include <cmath>
     22 
     23 namespace android {
     24 namespace hardware {
     25 namespace sensors {
     26 namespace V2_0 {
     27 namespace implementation {
     28 
     29 using ::android::hardware::sensors::V1_0::MetaDataEventType;
     30 using ::android::hardware::sensors::V1_0::SensorFlagBits;
     31 using ::android::hardware::sensors::V1_0::SensorStatus;
     32 
     33 static constexpr float kDefaultMaxDelayUs = 10 * 1000 * 1000;
     34 
     35 Sensor::Sensor(ISensorsEventCallback* callback)
     36     : mIsEnabled(false),
     37       mSamplingPeriodNs(0),
     38       mLastSampleTimeNs(0),
     39       mCallback(callback),
     40       mMode(OperationMode::NORMAL) {
     41     mRunThread = std::thread(startThread, this);
     42 }
     43 
     44 Sensor::~Sensor() {
     45     std::unique_lock<std::mutex> lock(mRunMutex);
     46     mStopThread = true;
     47     mIsEnabled = false;
     48     mWaitCV.notify_all();
     49     lock.release();
     50     mRunThread.join();
     51 }
     52 
     53 const SensorInfo& Sensor::getSensorInfo() const {
     54     return mSensorInfo;
     55 }
     56 
     57 void Sensor::batch(int32_t samplingPeriodNs) {
     58     if (samplingPeriodNs < mSensorInfo.minDelay * 1000) {
     59         samplingPeriodNs = mSensorInfo.minDelay * 1000;
     60     } else if (samplingPeriodNs > mSensorInfo.maxDelay * 1000) {
     61         samplingPeriodNs = mSensorInfo.maxDelay * 1000;
     62     }
     63 
     64     if (mSamplingPeriodNs != samplingPeriodNs) {
     65         mSamplingPeriodNs = samplingPeriodNs;
     66         // Wake up the 'run' thread to check if a new event should be generated now
     67         mWaitCV.notify_all();
     68     }
     69 }
     70 
     71 void Sensor::activate(bool enable) {
     72     if (mIsEnabled != enable) {
     73         std::unique_lock<std::mutex> lock(mRunMutex);
     74         mIsEnabled = enable;
     75         mWaitCV.notify_all();
     76     }
     77 }
     78 
     79 Result Sensor::flush() {
     80     // Only generate a flush complete event if the sensor is enabled and if the sensor is not a
     81     // one-shot sensor.
     82     if (!mIsEnabled || (mSensorInfo.flags & static_cast<uint32_t>(SensorFlagBits::ONE_SHOT_MODE))) {
     83         return Result::BAD_VALUE;
     84     }
     85 
     86     // Note: If a sensor supports batching, write all of the currently batched events for the sensor
     87     // to the Event FMQ prior to writing the flush complete event.
     88     Event ev;
     89     ev.sensorHandle = mSensorInfo.sensorHandle;
     90     ev.sensorType = SensorType::META_DATA;
     91     ev.u.meta.what = MetaDataEventType::META_DATA_FLUSH_COMPLETE;
     92     std::vector<Event> evs{ev};
     93     mCallback->postEvents(evs, isWakeUpSensor());
     94 
     95     return Result::OK;
     96 }
     97 
     98 void Sensor::startThread(Sensor* sensor) {
     99     sensor->run();
    100 }
    101 
    102 void Sensor::run() {
    103     std::unique_lock<std::mutex> runLock(mRunMutex);
    104     constexpr int64_t kNanosecondsInSeconds = 1000 * 1000 * 1000;
    105 
    106     while (!mStopThread) {
    107         if (!mIsEnabled || mMode == OperationMode::DATA_INJECTION) {
    108             mWaitCV.wait(runLock, [&] {
    109                 return ((mIsEnabled && mMode == OperationMode::NORMAL) || mStopThread);
    110             });
    111         } else {
    112             timespec curTime;
    113             clock_gettime(CLOCK_REALTIME, &curTime);
    114             int64_t now = (curTime.tv_sec * kNanosecondsInSeconds) + curTime.tv_nsec;
    115             int64_t nextSampleTime = mLastSampleTimeNs + mSamplingPeriodNs;
    116 
    117             if (now >= nextSampleTime) {
    118                 mLastSampleTimeNs = now;
    119                 nextSampleTime = mLastSampleTimeNs + mSamplingPeriodNs;
    120                 mCallback->postEvents(readEvents(), isWakeUpSensor());
    121             }
    122 
    123             mWaitCV.wait_for(runLock, std::chrono::nanoseconds(nextSampleTime - now));
    124         }
    125     }
    126 }
    127 
    128 bool Sensor::isWakeUpSensor() {
    129     return mSensorInfo.flags & static_cast<uint32_t>(SensorFlagBits::WAKE_UP);
    130 }
    131 
    132 std::vector<Event> Sensor::readEvents() {
    133     std::vector<Event> events;
    134     Event event;
    135     event.sensorHandle = mSensorInfo.sensorHandle;
    136     event.sensorType = mSensorInfo.type;
    137     event.timestamp = ::android::elapsedRealtimeNano();
    138     event.u.vec3.x = 0;
    139     event.u.vec3.y = 0;
    140     event.u.vec3.z = 0;
    141     event.u.vec3.status = SensorStatus::ACCURACY_HIGH;
    142     events.push_back(event);
    143     return events;
    144 }
    145 
    146 void Sensor::setOperationMode(OperationMode mode) {
    147     if (mMode != mode) {
    148         std::unique_lock<std::mutex> lock(mRunMutex);
    149         mMode = mode;
    150         mWaitCV.notify_all();
    151     }
    152 }
    153 
    154 bool Sensor::supportsDataInjection() const {
    155     return mSensorInfo.flags & static_cast<uint32_t>(SensorFlagBits::DATA_INJECTION);
    156 }
    157 
    158 Result Sensor::injectEvent(const Event& event) {
    159     Result result = Result::OK;
    160     if (event.sensorType == SensorType::ADDITIONAL_INFO) {
    161         // When in OperationMode::NORMAL, SensorType::ADDITIONAL_INFO is used to push operation
    162         // environment data into the device.
    163     } else if (!supportsDataInjection()) {
    164         result = Result::INVALID_OPERATION;
    165     } else if (mMode == OperationMode::DATA_INJECTION) {
    166         mCallback->postEvents(std::vector<Event>{event}, isWakeUpSensor());
    167     } else {
    168         result = Result::BAD_VALUE;
    169     }
    170     return result;
    171 }
    172 
    173 OnChangeSensor::OnChangeSensor(ISensorsEventCallback* callback)
    174     : Sensor(callback), mPreviousEventSet(false) {}
    175 
    176 void OnChangeSensor::activate(bool enable) {
    177     Sensor::activate(enable);
    178     if (!enable) {
    179         mPreviousEventSet = false;
    180     }
    181 }
    182 
    183 std::vector<Event> OnChangeSensor::readEvents() {
    184     std::vector<Event> events = Sensor::readEvents();
    185     std::vector<Event> outputEvents;
    186 
    187     for (auto iter = events.begin(); iter != events.end(); ++iter) {
    188         Event ev = *iter;
    189         if (ev.u.vec3 != mPreviousEvent.u.vec3 || !mPreviousEventSet) {
    190             outputEvents.push_back(ev);
    191             mPreviousEvent = ev;
    192             mPreviousEventSet = true;
    193         }
    194     }
    195     return outputEvents;
    196 }
    197 
    198 AccelSensor::AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback) : Sensor(callback) {
    199     mSensorInfo.sensorHandle = sensorHandle;
    200     mSensorInfo.name = "Accel Sensor";
    201     mSensorInfo.vendor = "Vendor String";
    202     mSensorInfo.version = 1;
    203     mSensorInfo.type = SensorType::ACCELEROMETER;
    204     mSensorInfo.typeAsString = "";
    205     mSensorInfo.maxRange = 78.4f;  // +/- 8g
    206     mSensorInfo.resolution = 1.52e-5;
    207     mSensorInfo.power = 0.001f;          // mA
    208     mSensorInfo.minDelay = 20 * 1000;    // microseconds
    209     mSensorInfo.maxDelay = kDefaultMaxDelayUs;
    210     mSensorInfo.fifoReservedEventCount = 0;
    211     mSensorInfo.fifoMaxEventCount = 0;
    212     mSensorInfo.requiredPermission = "";
    213     mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::DATA_INJECTION);
    214 };
    215 
    216 PressureSensor::PressureSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
    217     : Sensor(callback) {
    218     mSensorInfo.sensorHandle = sensorHandle;
    219     mSensorInfo.name = "Pressure Sensor";
    220     mSensorInfo.vendor = "Vendor String";
    221     mSensorInfo.version = 1;
    222     mSensorInfo.type = SensorType::PRESSURE;
    223     mSensorInfo.typeAsString = "";
    224     mSensorInfo.maxRange = 1100.0f;   // hPa
    225     mSensorInfo.resolution = 0.005f;  // hPa
    226     mSensorInfo.power = 0.001f;       // mA
    227     mSensorInfo.minDelay = 100 * 1000;  // microseconds
    228     mSensorInfo.maxDelay = kDefaultMaxDelayUs;
    229     mSensorInfo.fifoReservedEventCount = 0;
    230     mSensorInfo.fifoMaxEventCount = 0;
    231     mSensorInfo.requiredPermission = "";
    232     mSensorInfo.flags = 0;
    233 };
    234 
    235 MagnetometerSensor::MagnetometerSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
    236     : Sensor(callback) {
    237     mSensorInfo.sensorHandle = sensorHandle;
    238     mSensorInfo.name = "Magnetic Field Sensor";
    239     mSensorInfo.vendor = "Vendor String";
    240     mSensorInfo.version = 1;
    241     mSensorInfo.type = SensorType::MAGNETIC_FIELD;
    242     mSensorInfo.typeAsString = "";
    243     mSensorInfo.maxRange = 1300.0f;
    244     mSensorInfo.resolution = 0.01f;
    245     mSensorInfo.power = 0.001f;       // mA
    246     mSensorInfo.minDelay = 20 * 1000;  // microseconds
    247     mSensorInfo.maxDelay = kDefaultMaxDelayUs;
    248     mSensorInfo.fifoReservedEventCount = 0;
    249     mSensorInfo.fifoMaxEventCount = 0;
    250     mSensorInfo.requiredPermission = "";
    251     mSensorInfo.flags = 0;
    252 };
    253 
    254 LightSensor::LightSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
    255     : OnChangeSensor(callback) {
    256     mSensorInfo.sensorHandle = sensorHandle;
    257     mSensorInfo.name = "Light Sensor";
    258     mSensorInfo.vendor = "Vendor String";
    259     mSensorInfo.version = 1;
    260     mSensorInfo.type = SensorType::LIGHT;
    261     mSensorInfo.typeAsString = "";
    262     mSensorInfo.maxRange = 43000.0f;
    263     mSensorInfo.resolution = 10.0f;
    264     mSensorInfo.power = 0.001f;           // mA
    265     mSensorInfo.minDelay = 200 * 1000;    // microseconds
    266     mSensorInfo.maxDelay = kDefaultMaxDelayUs;
    267     mSensorInfo.fifoReservedEventCount = 0;
    268     mSensorInfo.fifoMaxEventCount = 0;
    269     mSensorInfo.requiredPermission = "";
    270     mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE);
    271 };
    272 
    273 ProximitySensor::ProximitySensor(int32_t sensorHandle, ISensorsEventCallback* callback)
    274     : OnChangeSensor(callback) {
    275     mSensorInfo.sensorHandle = sensorHandle;
    276     mSensorInfo.name = "Proximity Sensor";
    277     mSensorInfo.vendor = "Vendor String";
    278     mSensorInfo.version = 1;
    279     mSensorInfo.type = SensorType::PROXIMITY;
    280     mSensorInfo.typeAsString = "";
    281     mSensorInfo.maxRange = 5.0f;
    282     mSensorInfo.resolution = 1.0f;
    283     mSensorInfo.power = 0.012f;  // mA
    284     mSensorInfo.minDelay = 200 * 1000;  // microseconds
    285     mSensorInfo.maxDelay = kDefaultMaxDelayUs;
    286     mSensorInfo.fifoReservedEventCount = 0;
    287     mSensorInfo.fifoMaxEventCount = 0;
    288     mSensorInfo.requiredPermission = "";
    289     mSensorInfo.flags =
    290             static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE | SensorFlagBits::WAKE_UP);
    291 };
    292 
    293 GyroSensor::GyroSensor(int32_t sensorHandle, ISensorsEventCallback* callback) : Sensor(callback) {
    294     mSensorInfo.sensorHandle = sensorHandle;
    295     mSensorInfo.name = "Gyro Sensor";
    296     mSensorInfo.vendor = "Vendor String";
    297     mSensorInfo.version = 1;
    298     mSensorInfo.type = SensorType::GYROSCOPE;
    299     mSensorInfo.typeAsString = "";
    300     mSensorInfo.maxRange = 1000.0f * M_PI / 180.0f;
    301     mSensorInfo.resolution = 1000.0f * M_PI / (180.0f * 32768.0f);
    302     mSensorInfo.power = 0.001f;
    303     mSensorInfo.minDelay = 2.5f * 1000;  // microseconds
    304     mSensorInfo.maxDelay = kDefaultMaxDelayUs;
    305     mSensorInfo.fifoReservedEventCount = 0;
    306     mSensorInfo.fifoMaxEventCount = 0;
    307     mSensorInfo.requiredPermission = "";
    308     mSensorInfo.flags = 0;
    309 };
    310 
    311 AmbientTempSensor::AmbientTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
    312     : OnChangeSensor(callback) {
    313     mSensorInfo.sensorHandle = sensorHandle;
    314     mSensorInfo.name = "Ambient Temp Sensor";
    315     mSensorInfo.vendor = "Vendor String";
    316     mSensorInfo.version = 1;
    317     mSensorInfo.type = SensorType::AMBIENT_TEMPERATURE;
    318     mSensorInfo.typeAsString = "";
    319     mSensorInfo.maxRange = 80.0f;
    320     mSensorInfo.resolution = 0.01f;
    321     mSensorInfo.power = 0.001f;
    322     mSensorInfo.minDelay = 40 * 1000;  // microseconds
    323     mSensorInfo.maxDelay = kDefaultMaxDelayUs;
    324     mSensorInfo.fifoReservedEventCount = 0;
    325     mSensorInfo.fifoMaxEventCount = 0;
    326     mSensorInfo.requiredPermission = "";
    327     mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE);
    328 };
    329 
    330 DeviceTempSensor::DeviceTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
    331     : OnChangeSensor(callback) {
    332     mSensorInfo.sensorHandle = sensorHandle;
    333     mSensorInfo.name = "Device Temp Sensor";
    334     mSensorInfo.vendor = "Vendor String";
    335     mSensorInfo.version = 1;
    336     mSensorInfo.type = SensorType::TEMPERATURE;
    337     mSensorInfo.typeAsString = "";
    338     mSensorInfo.maxRange = 80.0f;
    339     mSensorInfo.resolution = 0.01f;
    340     mSensorInfo.power = 0.001f;
    341     mSensorInfo.minDelay = 40 * 1000;  // microseconds
    342     mSensorInfo.maxDelay = kDefaultMaxDelayUs;
    343     mSensorInfo.fifoReservedEventCount = 0;
    344     mSensorInfo.fifoMaxEventCount = 0;
    345     mSensorInfo.requiredPermission = "";
    346     mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE);
    347 }
    348 
    349 RelativeHumiditySensor::RelativeHumiditySensor(int32_t sensorHandle,
    350                                                ISensorsEventCallback* callback)
    351     : OnChangeSensor(callback) {
    352     mSensorInfo.sensorHandle = sensorHandle;
    353     mSensorInfo.name = "Relative Humidity Sensor";
    354     mSensorInfo.vendor = "Vendor String";
    355     mSensorInfo.version = 1;
    356     mSensorInfo.type = SensorType::RELATIVE_HUMIDITY;
    357     mSensorInfo.typeAsString = "";
    358     mSensorInfo.maxRange = 100.0f;
    359     mSensorInfo.resolution = 0.1f;
    360     mSensorInfo.power = 0.001f;
    361     mSensorInfo.minDelay = 40 * 1000;  // microseconds
    362     mSensorInfo.maxDelay = kDefaultMaxDelayUs;
    363     mSensorInfo.fifoReservedEventCount = 0;
    364     mSensorInfo.fifoMaxEventCount = 0;
    365     mSensorInfo.requiredPermission = "";
    366     mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::ON_CHANGE_MODE);
    367 }
    368 
    369 }  // namespace implementation
    370 }  // namespace V2_0
    371 }  // namespace sensors
    372 }  // namespace hardware
    373 }  // namespace android
    374