1 /*
2  * Copyright (C) 2016 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 "chre/core/sensor.h"
18 
19 namespace chre {
20 namespace {
21 
22 static const char *kInvalidSensorName = "Invalid Sensor";
23 
24 }  // namespace
25 
Sensor()26 Sensor::Sensor() {}
27 
Sensor(PlatformSensor && platformSensor)28 Sensor::Sensor(PlatformSensor&& platformSensor)
29     : mPlatformSensor(std::move(platformSensor)) {}
30 
getSensorType() const31 SensorType Sensor::getSensorType() const {
32   return isValid() ? mPlatformSensor->getSensorType() : SensorType::Unknown;
33 }
34 
isValid() const35 bool Sensor::isValid() const {
36   return mPlatformSensor.has_value();
37 }
38 
setRequest(const SensorRequest & request)39 bool Sensor::setRequest(const SensorRequest& request) {
40   bool requestWasSet = false;
41   if (isValid() && !request.isEquivalentTo(mSensorRequest)
42       && mPlatformSensor->setRequest(request)) {
43     mSensorRequest = request;
44     requestWasSet = true;
45 
46     // Mark last event as invalid when sensor is disabled.
47     if (request.getMode() == SensorMode::Off) {
48       mLastEventValid = false;
49     }
50   }
51 
52   return requestWasSet;
53 }
54 
operator =(Sensor && other)55 Sensor& Sensor::operator=(Sensor&& other) {
56   mSensorRequest = other.mSensorRequest;
57   mPlatformSensor = std::move(other.mPlatformSensor);
58   return *this;
59 }
60 
getMinInterval() const61 uint64_t Sensor::getMinInterval() const {
62   return isValid() ? mPlatformSensor->getMinInterval() :
63       CHRE_SENSOR_INTERVAL_DEFAULT;
64 }
65 
getSensorName() const66 const char *Sensor::getSensorName() const {
67   return isValid() ? mPlatformSensor->getSensorName() : kInvalidSensorName;
68 }
69 
getLastEvent() const70 ChreSensorData *Sensor::getLastEvent() const {
71   return (isValid() && mLastEventValid) ? mPlatformSensor->getLastEvent() :
72       nullptr;
73 }
74 
setLastEvent(const ChreSensorData * event)75 void Sensor::setLastEvent(const ChreSensorData *event) {
76   if (isValid()) {
77     mPlatformSensor->setLastEvent(event);
78 
79     // Mark last event as valid only if the sensor is enabled. Event data may
80     // arrive after sensor is disabled.
81     if (mSensorRequest.getMode() != SensorMode::Off) {
82       mLastEventValid = true;
83     }
84   }
85 }
86 
87 }  // namespace chre
88