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