1 /* 2 * Copyright (C) 2020 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_CROSS_VALIDATOR_MANAGER_H_ 18 #define CHRE_CROSS_VALIDATOR_MANAGER_H_ 19 20 #include <pb_encode.h> 21 22 #include "chre_api/chre.h" 23 #include "chre_cross_validation_sensor.nanopb.h" 24 25 #include "chre/util/optional.h" 26 #include "chre/util/singleton.h" 27 28 namespace chre::cross_validator_sensor { 29 30 // TODO(b/154271551): Break up the Manager class into more fine-grained classes 31 // to avoid it becoming to complex. 32 33 //! The maximum size of a sensor name. 34 constexpr size_t kMaxSensorNameSize = 128; 35 36 /** 37 * Class to manage a CHRE cross validator nanoapp. 38 */ 39 class Manager { 40 public: 41 /** 42 * Calls the cleanup helper method. This dtor is called on a singleton deinit 43 * call. 44 */ 45 ~Manager(); 46 47 /** 48 * Handle a CHRE event. 49 * 50 * @param senderInstanceId The instand ID that sent the event. 51 * @param eventType The type of the event. 52 * @param eventData The data for the event. 53 */ 54 void handleEvent(uint32_t senderInstanceId, uint16_t eventType, 55 const void *eventData); 56 57 private: 58 /** 59 * The enum that describes the type of cross validator in use. 60 */ 61 enum class CrossValidatorType { SENSOR }; 62 63 /** 64 * Struct to hold the state of the cross validator nanoapp. 65 */ 66 struct CrossValidatorState { 67 // Set upon received start message and read when nanoapp ends to handle 68 // cleanup 69 CrossValidatorType crossValidatorType; 70 // Set when start message received and checked against when a sensor data 71 // event from CHRE comes in 72 uint8_t sensorType; 73 // Set when start message is received and default sensor is found for 74 // requested sensor type and read when the sensor configuration is being 75 // cleaned up. Unused in non-sensor type validations 76 uint32_t sensorHandle; 77 // The host endpoint which is read from the start message and used when 78 // sending data back to AP. 79 uint64_t timeStart; 80 // The host endpoint which is read from the start message and used when 81 // sending data back to AP. 82 uint16_t hostEndpoint = CHRE_HOST_ENDPOINT_BROADCAST; 83 // The sensor for this validation uses continuous reporting mode. 84 bool isContinuous; 85 CrossValidatorStateCrossValidatorState86 CrossValidatorState(CrossValidatorType crossValidatorTypeIn, 87 uint8_t sensorTypeIn, uint32_t sensorHandleIn, 88 uint64_t timeStartIn, uint16_t hostEndpointIn, 89 bool isContinuousIn) 90 : crossValidatorType(crossValidatorTypeIn), 91 sensorType(sensorTypeIn), 92 sensorHandle(sensorHandleIn), 93 timeStart(timeStartIn), 94 hostEndpoint(hostEndpointIn), 95 isContinuous(isContinuousIn) {} 96 }; 97 98 //! The current state of the nanoapp. 99 //! Unset if start message was not received or error while processing start 100 //! message. 101 chre::Optional<CrossValidatorState> mCrossValidatorState; 102 103 //! A temporary global buffer where the sensor name is stored. 104 char mSensorNameArray[kMaxSensorNameSize]; 105 106 /** 107 * Make a SensorDatapoint proto message. 108 * 109 * @param sampleDataFromChre The sample data from CHRE. 110 * @param currentTimestamp The current CHRE timestamp. 111 * 112 * @return The SensorDatapoint proto message. 113 */ 114 static chre_cross_validation_sensor_SensorDatapoint makeDatapoint( 115 bool (*encodeFunc)(pb_ostream_t *, const pb_field_t *, void *const *), 116 const void *sampleDataFromChre, uint64_t currentTimestamp); 117 118 /** 119 * Encodes the values array of a sensor datapoint proto message. Used as the 120 * encode field of the SenorDatapoint message. 121 * 122 * @param stream The stream to write to. 123 * @param field The field to write to (unused). 124 * @param arg The data passed in order to write to the stream. 125 * @return true if successful. 126 */ 127 static bool encodeThreeAxisSensorDatapointValues(pb_ostream_t *stream, 128 const pb_field_t * /*field*/, 129 void *const *arg); 130 131 /** 132 * Encodes the datapoints into a SensorData message. 133 * 134 * @param stream The stream to write to. 135 * @param field The field to write to (unused). 136 * @param arg The data passed in order to write to the stream. 137 * @return true if successful. 138 */ 139 static bool encodeThreeAxisSensorDatapoints(pb_ostream_t *stream, 140 const pb_field_t * /*field*/, 141 void *const *arg); 142 143 /** 144 * Encodes the datapoints into a SensorData message. 145 * 146 * @param stream The stream to write to. 147 * @param field The field to write to (unused). 148 * @param arg The data passed in order to write to the stream. 149 * @return true if successful. 150 */ 151 static bool encodeFloatSensorDatapoints(pb_ostream_t *stream, 152 const pb_field_t * /*field*/, 153 void *const *arg); 154 155 /** 156 * Encodes the datapoints into a SensorData message. 157 * 158 * @param stream The stream to write to. 159 * @param field The field to write to (unused). 160 * @param arg The data passed in order to write to the stream. 161 * @return true if successful. 162 */ 163 static bool encodeProximitySensorDatapoints(pb_ostream_t *stream, 164 const pb_field_t * /*field*/, 165 void *const *arg); 166 167 /** 168 * Encodes the datapoints into a SensorData message. 169 * 170 * @param stream The stream to write to. 171 * @param field The field to write to. 172 * @param arg The data passed in order to write to the stream. 173 * @return true if successful. 174 */ 175 static bool encodeStepCounterSensorDatapoints(pb_ostream_t *stream, 176 const pb_field_t *field, 177 void *const *arg); 178 179 /** 180 * Encodes a single float value into values list of SensorDatapoint object. 181 * 182 * @param stream The stream to write to. 183 * @param field The field to write to (unused). 184 * @param arg The data passed in order to write to the stream. 185 * @return true if successful. 186 */ 187 static bool encodeFloatSensorDatapointValue(pb_ostream_t *stream, 188 const pb_field_t * /*field*/, 189 void *const *arg); 190 191 /** 192 * Encodes a single promximity value into values list of SensorDatapoint 193 * object, converting the isNear property into 5.0 (false) or 0.0 (true) in 194 * the process. 195 * 196 * @param stream The stream to write to. 197 * @param field The field to write to (unused). 198 * @param arg The data passed in order to write to the stream. 199 * @return true if successful. 200 */ 201 static bool encodeProximitySensorDatapointValue(pb_ostream_t *stream, 202 const pb_field_t * /*field*/, 203 void *const *arg); 204 205 /** 206 * Encodes a single step counter value into the values list of SensorDatapoint 207 * object, converting the uint64 value property into a float in the process. 208 * 209 * @param stream The stream to write to. 210 * @param field The field to write to (unused). 211 * @param arg The data passed in order to write to the stream. 212 * @return true if successful. 213 */ 214 static bool encodeStepCounterSensorDatapointValue(pb_ostream_t *stream, 215 const pb_field_t *field, 216 void *const *arg); 217 218 /** 219 * Handle a start sensor message. 220 * 221 * @param startSensorCommand The StartSensorCommand proto message received. 222 * 223 * @return true if successful. 224 */ 225 bool handleStartSensorMessage( 226 const chre_cross_validation_sensor_StartSensorCommand 227 &startSensorCommand); 228 229 /** 230 * @param header The sensor data header from CHRE sensor data event. 231 * 232 * @return true if header is valid. 233 */ 234 bool isValidHeader(const chreSensorDataHeader &header); 235 236 /** 237 * Handle a start message from CHRE with the given data from host. 238 * 239 * @param hostEndpoint The host endpoint the data was sent from. 240 * @param hostData The data from host that has a start message. 241 */ 242 void handleStartMessage(uint16_t hostEndpoint, 243 const chreMessageFromHostData *hostData); 244 245 /** 246 * Handle an info message from CHRE with the given data from host. 247 * 248 * @param hostEndpoint The host endpoint the data was sent from. 249 * @param hostData The data from host that has a start message. 250 */ 251 void handleInfoMessage(uint16_t hostEndpoint, 252 const chreMessageFromHostData *hostData); 253 254 /** 255 * Handle a message from the host. 256 * 257 * @param senderInstanceId The instance ID of the sender of this message. 258 * @param hostData The data from the host message. 259 */ 260 void handleMessageFromHost(uint32_t senderInstanceId, 261 const chreMessageFromHostData *hostData); 262 263 /** 264 * @param threeAxisDataFromChre Three axis sensor data from CHRE. 265 * @param sensorType The sensor type that sent the three axis data. 266 * 267 * @return The Data proto message that is ready to be sent to host with three 268 * axis data. 269 */ 270 static chre_cross_validation_sensor_Data makeSensorThreeAxisData( 271 const chreSensorThreeAxisData *threeAxisDataFromChre, uint8_t sensorType); 272 273 /** 274 * @param floatDataFromChre Float sensor data from CHRE. 275 * @param sensorType The sensor type that sent the three axis data. 276 * 277 * @return The Data proto message that is ready to be sent to host with float 278 * data. 279 */ 280 static chre_cross_validation_sensor_Data makeSensorFloatData( 281 const chreSensorFloatData *floatDataFromChre, uint8_t sensorType); 282 283 /** 284 * @param proximtyDataFromChre Proximity sensor data from CHRE. 285 * @param sensorType The sensor type that sent the three axis data. 286 * 287 * @return The Data proto message that is ready to be sent to host with float 288 * data. 289 */ 290 static chre_cross_validation_sensor_Data makeSensorProximityData( 291 const chreSensorByteData *proximityDataFromChre); 292 293 /** 294 * @param stepCounterDataFromChre Proximity sensor data from CHRE. 295 * @param sensorType The sensor type that sent the uint64 data. 296 * 297 * @return The Data proto message that is ready to be sent to host with float 298 * data. 299 */ 300 static chre_cross_validation_sensor_Data makeSensorStepCounterData( 301 const chreSensorUint64Data *stepCounterDataFromChre); 302 303 /** 304 * Handle sensor three axis data from CHRE. 305 * 306 * @param threeAxisDataFromChre The data from CHRE to parse. 307 * @param sensorType The sensor type that sent the three axis data. 308 */ 309 void handleSensorThreeAxisData( 310 const chreSensorThreeAxisData *threeAxisDataFromChre, uint8_t sensorType); 311 312 /** 313 * Handle sensor float data from CHRE. 314 * 315 * @param floatDataFromChre The data from CHRE to parse. 316 * @param sensorType The sensor type that sent the float data. 317 */ 318 void handleSensorFloatData(const chreSensorFloatData *floatDataFromChre, 319 uint8_t sensorType); 320 321 /** 322 * Handle proximity sensor data from CHRE. 323 * 324 * @param proximityDataFromChre The data to parse. 325 */ 326 void handleProximityData(const chreSensorByteData *proximityDataFromChre); 327 328 /** 329 * Send data to be validated to the host. 330 * Handle step counter sensor data from CHRE. 331 * 332 * @param stepCounterDataFromChre The data to parse. 333 */ 334 void handleStepCounterData( 335 const chreSensorUint64Data *stepCounterDataFromChre); 336 337 /** 338 * Encode and send data to be validated to host. 339 * 340 * @param data The data to send. 341 */ 342 void sendDataToHost(const chre_cross_validation_sensor_Data &data); 343 344 /** 345 * Encode and send the info response to the host. 346 * 347 * @param hostEndpoint The endpoint to send the response to. 348 * @param infoResponse The info response to be encoded and sent. 349 */ 350 static void sendInfoResponse( 351 uint16_t hostEndpoint, 352 const chre_cross_validation_sensor_SensorInfoResponse &infoResponse); 353 354 /** 355 * Determine if nanoapp is ready to process new sensor data. 356 * 357 * @param header The sensor data header that was received with data. 358 * @param sensorType The sensor type of received data. 359 * 360 * @return true if state is inline with receiving this new sensor data. 361 */ 362 bool processSensorData(const chreSensorDataHeader &header, 363 uint8_t sensorType); 364 365 /** 366 * @param sensorType The sensor type received from a CHRE sensor data event. 367 * 368 * @return true if sensorType matches the sensorType of current cross 369 * validator state. 370 */ 371 bool sensorTypeIsValid(uint8_t sensorType); 372 373 /** 374 * Cleanup the manager by tearing down any CHRE API resources that were used 375 * during validation. 376 */ 377 void cleanup(); 378 379 /** 380 * @param sensorType The CHRE sensor type. 381 * @param sensorIndex The CHRE sensor index as defined in chreSensorFind. 382 * @param handle A non-null pointer where the sensor handle is stored, if 383 * found. 384 * 385 * @return true if the sensor corresponding to the input is available. 386 */ 387 static bool getSensor(uint32_t sensorType, uint32_t sensorIndex, 388 uint32_t *handle); 389 }; 390 391 // The chre cross validator manager singleton. 392 typedef chre::Singleton<Manager> ManagerSingleton; 393 394 } // namespace chre::cross_validator_sensor 395 396 #endif // CHRE_CROSS_VALIDATOR_MANAGER_H_ 397