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_REQUEST_H_ 18 #define CHRE_CORE_SENSOR_REQUEST_H_ 19 20 #include <cstdint> 21 22 #include "chre/core/nanoapp.h" 23 #include "chre/core/sensor_type.h" 24 #include "chre/util/time.h" 25 #include "chre_api/chre/sensor.h" 26 27 namespace chre { 28 29 //! Maximum of non-default interval and latency values in nanoseconds to ensure 30 //! no overflow in CHRE operations. 31 constexpr uint64_t kMaxIntervalLatencyNs = (UINT64_MAX - 1) / 2; 32 33 /** 34 * Models a request for sensor data. This class implements the API set forth by 35 * the RequestMultiplexer container. 36 */ 37 class SensorRequest { 38 public: 39 /** 40 * Default constructs a sensor request to the minimal possible configuration. 41 * The sensor is disabled and the interval and latency are both set to zero. 42 */ 43 SensorRequest(); 44 45 /** 46 * Constructs a sensor request given a mode, interval and latency. Non-default 47 * interval or latency higher than kMaxIntervalLatencyNs will be capped. 48 * 49 * @param mode The mode of the sensor request. 50 * @param interval The interval between samples. 51 * @param latency The maximum amount of time to batch samples before 52 * delivering to a client. 53 */ 54 SensorRequest(SensorMode mode, Nanoseconds interval, Nanoseconds latency); 55 56 /** 57 * Constructs a sensor request given an owning nanoapp, mode, interval and 58 * latency. Non-default interval or latency higher than kMaxIntervalLatencyNs 59 * will be capped. 60 * 61 * @param instanceId The instance ID of the nanoapp that made this request. 62 * @param mode The mode of the sensor request. 63 * @param interval The interval between samples. 64 * @param latency The maximum amount of time to batch samples before 65 * delivering to a client. 66 */ 67 SensorRequest(uint32_t instanceId, SensorMode mode, Nanoseconds interval, 68 Nanoseconds latency); 69 70 /** 71 * Performs an equivalency comparison of two sensor requests. This determines 72 * if the effective request for sensor data is the same as another. 73 * 74 * @param request The request to compare against. 75 * @return Returns true if this request is equivalent to another. 76 */ 77 bool isEquivalentTo(const SensorRequest &request) const; 78 79 /** 80 * Indicates that the only difference between the two requests is that the 81 * bias request state has changed. 82 * 83 * @param request The request to compare against. 84 * @return true if only the bias request value is different. 85 */ 86 bool onlyBiasRequestUpdated(const SensorRequest &request) const; 87 88 /** 89 * Assigns the current request to the maximal superset of the mode, rate 90 * and latency of the other request. 91 * 92 * @param request The other request to compare the attributes of. 93 * @return true if any of the attributes of this request changed. 94 */ 95 bool mergeWith(const SensorRequest &request); 96 97 /** 98 * @return Returns the interval of samples for this request. 99 */ getInterval()100 Nanoseconds getInterval() const { 101 return mInterval; 102 } 103 104 /** 105 * Overrides the latency specified in the request to the provided latency 106 * value. 107 */ setLatency(Nanoseconds latency)108 void setLatency(Nanoseconds latency) { 109 mLatency = latency; 110 } 111 112 /** 113 * @return Returns the maximum amount of time samples can be batched prior to 114 * dispatching to the client. 115 */ getLatency()116 Nanoseconds getLatency() const { 117 return mLatency; 118 } 119 120 /** 121 * @return The mode of this request. 122 */ getMode()123 SensorMode getMode() const { 124 return mMode; 125 } 126 127 /** 128 * @return The instance ID of the nanoapp that owns this request. 129 */ getInstanceId()130 uint32_t getInstanceId() const { 131 return mInstanceId; 132 } 133 134 /** 135 * Sets whether the request also wants bias updates. 136 * 137 * @param requested Whether bias updates should be requested. 138 */ setBiasUpdatesRequested(bool requested)139 void setBiasUpdatesRequested(bool requested) { 140 mBiasUpdatesRequested = requested; 141 } 142 143 /** 144 * @return Whether bias updates are requested 145 */ getBiasUpdatesRequested()146 bool getBiasUpdatesRequested() const { 147 return mBiasUpdatesRequested; 148 } 149 150 private: 151 //! The interval between samples for this request. 152 Nanoseconds mInterval; 153 154 //! The maximum amount of time samples can be batched prior to dispatching to 155 //! the client 156 Nanoseconds mLatency; 157 158 //! The nanoapp that made this request or zero when unset. This will be 159 //! kInvalidInstanceId when returned by the generateIntersectionOf method. 160 uint32_t mInstanceId = kInvalidInstanceId; 161 162 //! The mode of this request. 163 SensorMode mMode; 164 165 //! Whether the nanoapp is requesting bias updates. 166 bool mBiasUpdatesRequested = false; 167 }; 168 169 } // namespace chre 170 171 #endif // CHRE_CORE_SENSOR_REQUEST_H_ 172