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_api/chre/sensor.h" 23 #include "chre/core/nanoapp.h" 24 #include "chre/core/sensor_type.h" 25 #include "chre/util/time.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 * Assigns the current request to the maximal superset of the mode, rate 81 * and latency of the other request. 82 * 83 * @param request The other request to compare the attributes of. 84 * @return true if any of the attributes of this request changed. 85 */ 86 bool mergeWith(const SensorRequest& request); 87 88 /** 89 * @return Returns the interval of samples for this request. 90 */ getInterval()91 Nanoseconds getInterval() const { 92 return mInterval; 93 } 94 95 /** 96 * @return Returns the maximum amount of time samples can be batched prior to 97 * dispatching to the client. 98 */ getLatency()99 Nanoseconds getLatency() const { 100 return mLatency; 101 } 102 103 /** 104 * @return The mode of this request. 105 */ getMode()106 SensorMode getMode() const { 107 return mMode; 108 } 109 110 /** 111 * @return The instance ID of the nanoapp that owns this request. 112 */ getInstanceId()113 uint32_t getInstanceId() const { 114 return mInstanceId; 115 } 116 117 private: 118 //! The nanoapp that made this request or zero when unset. This will be 119 //! kInvalidInstanceId when returned by the generateIntersectionOf method. 120 uint32_t mInstanceId = kInvalidInstanceId; 121 122 //! The interval between samples for this request. 123 Nanoseconds mInterval; 124 125 //! The maximum amount of time samples can be batched prior to dispatching to 126 //! the client 127 Nanoseconds mLatency; 128 129 //! The mode of this request. 130 SensorMode mMode; 131 }; 132 133 } // namespace chre 134 135 #endif // CHRE_CORE_SENSOR_REQUEST_H_ 136