1 /* 2 * Copyright (C) 2017 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 #pragma once 17 18 #include <vector> 19 20 #include "common/libs/threads/cuttlefish_thread.h" 21 #include "common/libs/fs/shared_fd.h" 22 #include "guest/hals/sensors/sensors.h" 23 #include "guest/hals/sensors/sensors_hal.h" 24 25 namespace cvd { 26 27 // Used for sending control messages to the receiver thread. 28 // The sensor_handle field may be left unused if it is not needed. 29 enum ControlMessageType { 30 THREAD_STOP, 31 SENSOR_STATE_UPDATE 32 }; 33 typedef struct { 34 ControlMessageType message_type; 35 uint8_t sensor_handle; 36 } SensorControlMessage; 37 38 #if VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_0) 39 // Last updated to HAL 1.4 40 // Version history: 41 // Before jb, jb-mr1 SENSORS_DEVICE_API_VERSION_0_1 (no version in sensors.h) 42 // jb-mr2: SENSORS_DEVICE_API_VERSION_1_0 43 // k: SENSORS_DEVICE_API_VERSION_1_1 44 // l, l-mr1: SENSORS_DEVICE_API_VERSION_1_3 45 // m, n, n-mr1: SENSORS_DEVICE_API_VERSION_1_4 46 #else 47 // Pre-1.0 sensors do not define the sensors_poll_device_1 type. 48 typedef sensors_poll_device_t sensors_poll_device_1; 49 #endif 50 51 class GceSensors : public sensors_poll_device_1 { 52 public: 53 GceSensors(); 54 ~GceSensors(); 55 56 /** 57 ** SENSOR HAL API FUNCTIONS FOR MODULE 58 **/ 59 60 // Gets a list of all supported sensors and stores in list. 61 // Returns the number of supported sensors. 62 static int GetSensorsList(struct sensors_module_t* module, 63 struct sensor_t const** list); 64 65 // Place the module in a specific mode. The following modes are defined 66 // 67 // 0 - Normal operation. Default state of the module. 68 // 1 - Loopback mode. Data is injected for the supported 69 // sensors by the sensor service in this mode. 70 // @return 0 on success 71 // -EINVAL if requested mode is not supported 72 // -EPERM if operation is not allowed 73 static int SetOperationMode(unsigned int mode); 74 75 76 /** 77 ** SENSOR HAL API FUNCTIONS FOR DEVICE 78 **/ 79 // Opens the device. 80 static int Open(const struct hw_module_t* module, const char* name, 81 struct hw_device_t** device); 82 83 // Closes the device, closing all sensors. 84 int Close(); 85 86 // Activate (or deactivate) the sensor with the given handle. 87 // 88 // One-shot sensors deactivate themselves automatically upon receiving an 89 // event, and they must still accept to be deactivated through a call to 90 // activate(..., enabled=0). 91 // Non-wake-up sensors never prevent the SoC from going into suspend mode; 92 // that is, the HAL shall not hold a partial wake-lock on behalf of 93 // applications. 94 // 95 // If enabled is 1 and the sensor is already activated, this function is a 96 // no-op and succeeds. 97 // 98 // If enabled is 0 and the sensor is already deactivated, this function is a 99 // no-op and succeeds. 100 // 101 // This function returns 0 on success and a negative error number otherwise. 102 int Activate(int handle, int enabled); 103 104 // Sets the delay (in ns) for the sensor with the given handle. 105 // Deprecated as of HAL 1.1 106 // Called after activate() 107 int SetDelay(int handle, int64_t sampling_period_ns); 108 109 // Returns an array of sensor data by filling the data argument. 110 // This function must block until events are available. It will return 111 // the number of events read on success, or a negative number in case of 112 // an error. 113 int Poll(sensors_event_t* data, int count); 114 115 #if VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_0) 116 // Sets a sensor’s parameters, including sampling frequency and maximum 117 // report latency. This function can be called while the sensor is 118 // activated, in which case it must not cause any sensor measurements to 119 // be lost: transitioning from one sampling rate to the other cannot cause 120 // lost events, nor can transitioning from a high maximum report latency to 121 // a low maximum report latency. 122 // 123 // Before SENSORS_DEVICE_API_VERSION_1_3, flags included: 124 // SENSORS_BATCH_DRY_RUN 125 // SENSORS_BATCH_WAKE_UPON_FIFO_FULL 126 // 127 // After SENSORS_DEVICE_API_VERSION_1_3 see WAKE_UPON_FIFO_FULL 128 // in sensor_t.flags 129 int Batch(int sensor_handle, int flags, int64_t sampling_period_ns, 130 int64_t max_report_latency_ns) { 131 // TODO: Add support for maximum report latency with max_report_latency_ns. 132 return SetDelay(sensor_handle, sampling_period_ns); 133 } 134 #endif 135 136 #if VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_1) 137 // Adds a META_DATA_FLUSH_COMPLETE event (sensors_event_meta_data_t) 138 // to the end of the "batch mode" FIFO for the specified sensor and flushes 139 // the FIFO. 140 // 141 // If the FIFO is empty or if the sensor doesn't support batching (FIFO 142 // size zero), it should return SUCCESS along with a trivial 143 // META_DATA_FLUSH_COMPLETE event added to the event stream. This applies to 144 // all sensors other than one-shot sensors. 145 // 146 // If the sensor is a one-shot sensor, flush must return -EINVAL and not 147 // generate any flush complete metadata. 148 // 149 // If the sensor is not active at the time flush() is called, flush() should 150 // return -EINVAL. 151 int Flush(int sensor_handle) { 152 return -EINVAL; 153 } 154 #endif 155 156 #if VSOC_SENSORS_DEVICE_API_VERSION_ATLEAST(1_4) 157 // Inject a single sensor sample to be to this device. 158 // data points to the sensor event to be injected 159 // @return 0 on success 160 // -EPERM if operation is not allowed 161 // -EINVAL if sensor event cannot be injected 162 int InjectSensorData(const sensors_event_t *data) { 163 return -EINVAL; 164 } 165 #endif 166 167 private: 168 typedef std::vector<SensorState*> SensorStateVector; 169 typedef std::vector<sensors_event_t> FifoType; 170 // Total number of sensors supported by this HAL. 171 static int total_sensor_count_; 172 // Vector of static sensor information for sensors supported by this HAL. 173 // Indexed by the handle. Length must always be equal to total_sensor_count_. 174 static SensorInfo* sensor_infos_; 175 // Vector of sensor state information, indexed by the handle. 176 // Assumption here is that the sensor handles will start at 0 and be 177 // contiguous up to the number of supported sensors. 178 SensorStateVector sensor_states_; 179 // Keep track of the time when the thread in Poll() is scheduled to wake. 180 cvd::time::MonotonicTimePoint current_deadline_; 181 182 // Ordered set of sensor values. 183 // TODO(ghartman): Simulate FIFO overflow. 184 FifoType fifo_; 185 // Thread to handle new connections. 186 pthread_t receiver_thread_; 187 // Socket to receive sensor events on. 188 cvd::SharedFD sensor_listener_socket_; 189 // Socket for listener thread to receive control messages. 190 cvd::SharedFD control_receiver_socket_; 191 // Socket to send control messages to listener thread. 192 cvd::SharedFD control_sender_socket_; 193 194 // Lock to protect shared state, including 195 // sensor_states_ and next_deadline_. 196 // Associated with deadline_change_ condition variable. 197 cvd::Mutex sensor_state_lock_; 198 // Condition variable to signal changes in the deadline. 199 cvd::ConditionVariable deadline_change_; 200 201 // When events are arriving from a client, we report only 202 // when they arrive, rather than at a fixed cycle. After not 203 // receiving a real event for both a given number of periods 204 // and a given time period, we will give up and resume 205 // sending mock events. 206 const static int kInjectedEventWaitPeriods; 207 const static cvd::time::Nanoseconds kInjectedEventWaitTime; 208 209 /** 210 ** UTILITY FUNCTIONS 211 **/ 212 213 // Receive data from remoter. 214 void* Receiver(); 215 216 // Notifies the remoter that the HAL is awake and ready. 217 inline bool NotifyRemoter(); 218 219 // Looks through all active sensor deadlines, and finds the one that 220 // is coming up next. If this is not next_deadline_, then the deadline 221 // has changed. Update it and signal the Poll thread. 222 // This should be called anytime the next deadline may have changed. 223 // Can only be called while holding sensor_state_lock_. 224 // Returns true if the deadline has changed. 225 cvd::time::MonotonicTimePoint UpdateDeadline(); 226 227 // Sends an update for the sensor with the given handle to the remoter. 228 // Update will be enqueued for receiver, not send immediately. 229 inline bool UpdateRemoterState(int handle); 230 231 // Sends a control event to the listener. 232 inline bool SendControlMessage(SensorControlMessage msg); 233 234 // Populates the list of static sensor info. Returns the number 235 // of sensors supported. Should only be called once. 236 static inline int RegisterSensors(); 237 238 }; 239 240 } //namespace cvd 241 242