Home | History | Annotate | Download | only in core
      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 #ifndef CHRE_CORE_SENSOR_H_
     18 #define CHRE_CORE_SENSOR_H_
     19 
     20 #include "chre/platform/platform_sensor.h"
     21 #include "chre/util/non_copyable.h"
     22 #include "chre/util/optional.h"
     23 
     24 namespace chre {
     25 
     26 /**
     27  * Represents a sensor in the system that is exposed to nanoapps in CHRE.
     28  *
     29  * Note that like chre::Nanoapp, this class uses inheritance to separate the
     30  * common code (Sensor) from common interface with platform-specific
     31  * implementation (PlatformSensor) from the fully platform-specific part
     32  * (PlatformSensorBase). However, this inheritance relationship does *not* imply
     33  * polymorphism, and this object must only be referred to via the most-derived
     34  * type, i.e. chre::Sensor.
     35  */
     36 class Sensor : public PlatformSensor {
     37  public:
     38   /**
     39    * Constructs a sensor in an unspecified state. Should not be called directly
     40    * by common code, as platform-specific initialization of the Sensor object is
     41    * required for it to be usable.
     42    *
     43    * @see PlatformSensor::getSensors
     44    */
     45   Sensor() = default;
     46 
     47   Sensor(Sensor&& other) = default;
     48   Sensor& operator=(Sensor&& other) = default;
     49 
     50   /**
     51    * Obtains a reference to the latest request that has been accepted by the
     52    * platform.
     53    *
     54    * @return A const reference to the SensorRequest.
     55    */
     56   const SensorRequest& getRequest() const {
     57     return mSensorRequest;
     58   }
     59 
     60   /**
     61    * Sets the current request of this sensor. If this request is a change from
     62    * the previous request, it is sent to the underlying platform. If isValid()
     63    * returns false this function will also return false and do nothing.
     64    *
     65    * @param request The new request for this sensor.
     66    * @return true if there was no change required or the platform has set the
     67    *         request successfully.
     68    */
     69   bool setRequest(const SensorRequest& request);
     70 
     71  private:
     72   //! The most recent sensor request accepted by the platform.
     73   SensorRequest mSensorRequest;
     74 };
     75 
     76 }  // namespace chre
     77 
     78 #endif  // CHRE_CORE_SENSOR_H_
     79