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_PLATFORM_PLATFORM_SENSOR_H_ 18 #define CHRE_PLATFORM_PLATFORM_SENSOR_H_ 19 20 #include "chre/core/sensor_request.h" 21 #include "chre/target_platform/platform_sensor_base.h" 22 #include "chre/util/dynamic_vector.h" 23 #include "chre/util/non_copyable.h" 24 25 namespace chre { 26 27 class Sensor; 28 29 /** 30 * Defines the common interface to sensor functionality that is implemented in a 31 * platform-specific way, and must be supported on every platform. 32 * 33 * @see Sensor 34 */ 35 class PlatformSensor : public PlatformSensorBase, 36 public NonCopyable { 37 public: 38 /** 39 * Initializes the sensors subsystem. This must be called as part of the 40 * initialization of the runtime. 41 */ 42 static void init(); 43 44 /** 45 * Deinitializes the sensors subsystem, including releasing any outstanding 46 * sensor requests. This must be called as part of the deinitialization of the 47 * runtime. 48 */ 49 static void deinit(); 50 51 /** 52 * Constructs Sensor objects for every CHRE-supported sensor in the system, 53 * and puts them in the supplied DynamicVector, which should be empty when 54 * passed in. If this method returns false the vector may be partially filled. 55 * 56 * @param sensors A non-null pointer to a DynamicVector to populate with the 57 * list of sensors. 58 * @return true if the query was successful. 59 */ 60 static bool getSensors(DynamicVector<Sensor> *sensors); 61 62 /** 63 * Obtains the SensorType of this platform sensor. The implementation of this 64 * method is supplied by the platform as the mechanism for determining the 65 * type may vary across platforms. 66 * 67 * @return The type of this sensor. 68 */ 69 SensorType getSensorType() const; 70 71 /** 72 * @return This sensor's minimum supported sampling interval, in nanoseconds. 73 */ 74 uint64_t getMinInterval() const; 75 76 /** 77 * Returns a descriptive name (e.g. type and model) for this sensor. 78 * 79 * @return A pointer to a string with storage duration at least as long as the 80 * lifetime of this object. 81 */ 82 const char *getSensorName() const; 83 84 /** 85 * @return Pointer to this sensor's last data event. It returns a nullptr if 86 * the the platform doesn't provide it. 87 */ 88 ChreSensorData *getLastEvent() const; 89 90 /** 91 * Gets the current status of this sensor in the CHRE API format. 92 * 93 * @param status A non-null pointer to chreSensorSamplingStatus to populate 94 * @return true if the sampling status has been successfully obtained. 95 */ 96 bool getSamplingStatus(struct chreSensorSamplingStatus *status) const; 97 98 protected: 99 /** 100 * Default constructor that puts this instance in an unspecified state. 101 * Additional platform-specific initialization will likely be necessary to put 102 * this object in a usable state. Do not construct PlatformSensor directly; 103 * instead construct via Sensor. 104 */ 105 PlatformSensor() = default; 106 107 PlatformSensor(PlatformSensor&& other); 108 PlatformSensor& operator=(PlatformSensor&& other); 109 110 /** 111 * Perform any necessary cleanup of resources acquired in PlatformSensorBase. 112 */ 113 ~PlatformSensor(); 114 115 /** 116 * Sends the sensor request to the platform sensor. The implementation 117 * of this method is supplied by the platform. If the request is 118 * invalid/unsupported by this sensor, for example because it requests an 119 * interval that is too short, then this function must return false. If 120 * setting this new request fails due to a transient failure (example: 121 * inability to communicate with the sensor) false must also be returned. 122 * 123 * If the request's latency is lower than its interval, the platform sensor 124 * must deliver the first sample to clients as soon as it becomes available. 125 * 126 * @param request The new request to set this sensor to. 127 * @return true if the platform sensor was successfully configured with the 128 * supplied request. 129 */ 130 bool applyRequest(const SensorRequest& request); 131 }; 132 133 } // namespace chre 134 135 #endif // CHRE_PLATFORM_PLATFORM_SENSOR_H_ 136