1 /* 2 * Copyright (C) 2022 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_API_TEST_MANAGER_H_ 18 #define CHRE_API_TEST_MANAGER_H_ 19 20 #include <cinttypes> 21 #include <cstdint> 22 23 #include "chre/re.h" 24 #include "chre/util/pigweed/rpc_server.h" 25 #include "chre/util/singleton.h" 26 #include "chre_api/chre.h" 27 #include "chre_api_test.rpc.pb.h" 28 29 using ::chre::Optional; 30 31 /** 32 * Contains signature-generated RPC functions for the ChreApiTest service. 33 */ 34 class ChreApiTestService final 35 : public chre::rpc::pw_rpc::nanopb::ChreApiTestService::Service< 36 ChreApiTestService> { 37 public: 38 /** 39 * Returns the BLE capabilities. 40 */ 41 pw::Status ChreBleGetCapabilities(const google_protobuf_Empty &request, 42 chre_rpc_Capabilities &response); 43 44 /** 45 * Returns the BLE filter capabilities. 46 */ 47 pw::Status ChreBleGetFilterCapabilities(const google_protobuf_Empty &request, 48 chre_rpc_Capabilities &response); 49 50 /** 51 * Finds the default sensor and returns the handle in the output. 52 */ 53 pw::Status ChreSensorFindDefault( 54 const chre_rpc_ChreSensorFindDefaultInput &request, 55 chre_rpc_ChreSensorFindDefaultOutput &response); 56 57 /** 58 * Gets the sensor information. 59 */ 60 pw::Status ChreGetSensorInfo(const chre_rpc_ChreHandleInput &request, 61 chre_rpc_ChreGetSensorInfoOutput &response); 62 63 /** 64 * Gets the sensor sampling status for a given sensor. 65 */ 66 pw::Status ChreGetSensorSamplingStatus( 67 const chre_rpc_ChreHandleInput &request, 68 chre_rpc_ChreGetSensorSamplingStatusOutput &response); 69 70 /** 71 * Configures a given sensor. 72 */ 73 pw::Status ChreSensorConfigure( 74 const chre_rpc_ChreSensorConfigureInput &request, 75 chre_rpc_Status &response); 76 77 /** 78 * Configures the mode for a sensor. 79 */ 80 pw::Status ChreSensorConfigureModeOnly( 81 const chre_rpc_ChreSensorConfigureModeOnlyInput &request, 82 chre_rpc_Status &response); 83 84 /** 85 * Gets the audio source information. 86 */ 87 pw::Status ChreAudioGetSource(const chre_rpc_ChreHandleInput &request, 88 chre_rpc_ChreAudioGetSourceOutput &response); 89 90 /** 91 * Configures delivery of audio data to the current nanoapp. 92 */ 93 pw::Status ChreAudioConfigureSource( 94 const chre_rpc_ChreAudioConfigureSourceInput &request, 95 chre_rpc_Status &response); 96 97 /** 98 * Gets the current chreAudioSourceStatus struct for a given audio handle. 99 */ 100 pw::Status ChreAudioGetStatus(const chre_rpc_ChreHandleInput &request, 101 chre_rpc_ChreAudioGetStatusOutput &response); 102 103 /** 104 * Configures host endpoint notification. 105 */ 106 pw::Status ChreConfigureHostEndpointNotifications( 107 const chre_rpc_ChreConfigureHostEndpointNotificationsInput &request, 108 chre_rpc_Status &response); 109 110 /** 111 * Gets the host endpoint info for a given host endpoint id. 112 */ 113 pw::Status ChreGetHostEndpointInfo( 114 const chre_rpc_ChreGetHostEndpointInfoInput &request, 115 chre_rpc_ChreGetHostEndpointInfoOutput &response); 116 117 /** 118 * Starts a BLE scan synchronously. Waits for the CHRE_EVENT_BLE_ASYNC_RESULT 119 * event. 120 */ 121 void ChreBleStartScanSync(const chre_rpc_ChreBleStartScanAsyncInput &request, 122 ServerWriter<chre_rpc_GeneralSyncMessage> &writer); 123 124 /** 125 * Stops a BLE scan synchronously. Waits for the CHRE_EVENT_BLE_ASYNC_RESULT 126 * event. 127 */ 128 void ChreBleStopScanSync(const google_protobuf_Empty &request, 129 ServerWriter<chre_rpc_GeneralSyncMessage> &writer); 130 131 /** 132 * Gathers events that match the input filter before the timeout in ns or 133 * the max event count. 134 */ 135 void GatherEvents(const chre_rpc_GatherEventsInput &request, 136 ServerWriter<chre_rpc_GeneralEventsMessage> &writer); 137 138 /** 139 * Handles a BLE event from CHRE. 140 * 141 * @param result the event result. 142 */ 143 void handleBleAsyncResult(const chreAsyncResult *result); 144 145 /** 146 * Gathers the event if there is an existing event writer. 147 * 148 * @param eventType the event type. 149 * @param eventData the event data. 150 */ 151 void handleGatheringEvent(uint16_t eventType, const void *eventData); 152 153 /** 154 * Handles a timer event from CHRE. 155 * 156 * @param cookie the cookie from the event. 157 */ 158 void handleTimerEvent(const void *cookie); 159 160 /** 161 * Handles host endpoint notification event from CHRE. 162 * 163 * @param data the data from event. 164 */ 165 void handleHostEndpointNotificationEvent( 166 const chreHostEndpointNotification *data); 167 168 private: 169 /** 170 * Sets the synchronous timeout timer for the active sync message. 171 * 172 * @return if the operation was successful. 173 */ 174 bool startSyncTimer(); 175 176 /** 177 * The following functions validate the RPC input: request, calls the 178 * underlying function, and sets the return value in response. 179 * 180 * @param request the request. 181 * @param response the response. 182 * @return true if the input was validated correctly; 183 * false otherwise. 184 */ 185 bool validateInputAndCallChreBleGetCapabilities( 186 const google_protobuf_Empty &request, chre_rpc_Capabilities &response); 187 188 bool validateInputAndCallChreBleGetFilterCapabilities( 189 const google_protobuf_Empty &request, chre_rpc_Capabilities &response); 190 191 bool validateInputAndCallChreBleStartScanAsync( 192 const chre_rpc_ChreBleStartScanAsyncInput &request, 193 chre_rpc_Status &response); 194 195 bool validateInputAndCallChreBleStopScanAsync( 196 const google_protobuf_Empty &request, chre_rpc_Status &response); 197 198 bool validateInputAndCallChreSensorFindDefault( 199 const chre_rpc_ChreSensorFindDefaultInput &request, 200 chre_rpc_ChreSensorFindDefaultOutput &response); 201 202 bool validateInputAndCallChreGetSensorInfo( 203 const chre_rpc_ChreHandleInput &request, 204 chre_rpc_ChreGetSensorInfoOutput &response); 205 206 bool validateInputAndCallChreGetSensorSamplingStatus( 207 const chre_rpc_ChreHandleInput &request, 208 chre_rpc_ChreGetSensorSamplingStatusOutput &response); 209 210 bool validateInputAndCallChreSensorConfigure( 211 const chre_rpc_ChreSensorConfigureInput &request, 212 chre_rpc_Status &response); 213 214 bool validateInputAndCallChreSensorConfigureModeOnly( 215 const chre_rpc_ChreSensorConfigureModeOnlyInput &request, 216 chre_rpc_Status &response); 217 218 bool validateInputAndCallChreAudioGetSource( 219 const chre_rpc_ChreHandleInput &request, 220 chre_rpc_ChreAudioGetSourceOutput &response); 221 222 bool validateInputAndCallChreAudioConfigureSource( 223 const chre_rpc_ChreAudioConfigureSourceInput &request, 224 chre_rpc_Status &response); 225 226 bool validateInputAndCallChreAudioGetStatus( 227 const chre_rpc_ChreHandleInput &request, 228 chre_rpc_ChreAudioGetStatusOutput &response); 229 230 bool validateInputAndCallChreConfigureHostEndpointNotifications( 231 const chre_rpc_ChreConfigureHostEndpointNotificationsInput &request, 232 chre_rpc_Status &response); 233 234 bool validateInputAndCallChreGetHostEndpointInfo( 235 const chre_rpc_ChreGetHostEndpointInfoInput &request, 236 chre_rpc_ChreGetHostEndpointInfoOutput &response); 237 238 /** 239 * Handle assigning the data to the GeneralEventsMessage proto received from 240 * a CHRE_AUDIO_DATA_EVENT event. 241 * 242 * @param message The message proto to fill in. 243 * @param data The data received in the event. 244 */ 245 bool handleChreAudioDataEvent(const chreAudioDataEvent *data); 246 247 /** 248 * Handle sending a single message to host. Asserts success on event write. 249 * 250 * @param message The message proto to send. 251 * @return False if we have written the number of expected events. 252 */ 253 bool sendGeneralEventToHost(const chre_rpc_GeneralEventsMessage &message); 254 255 /** 256 * Handle sending part of a single message to host. 257 * Used for sending events larger than CHRE_MESSAGE_TO_HOST_MAX_SIZE. 258 * Asserts success on event write. 259 * 260 * @param message The message proto to send. 261 */ 262 void sendPartialGeneralEventToHost( 263 const chre_rpc_GeneralEventsMessage &message); 264 265 /** 266 * Handles checking if we need to finish sending events. 267 * Must be used after calls to sendPartialGeneralEventToHost. 268 * 269 * @param message The message proto to send. 270 * @return False if we have written the number of expected events. 271 */ 272 bool closePartialGeneralEventToHost(); 273 274 /** 275 * Validates the BLE scan filters and creates a generic filter in the 276 * outputScanFilters array. scanFilters and outputScanFilters must be of size 277 * scanFilterCount or greater. 278 * 279 * @param scanFilters the input scan filters. 280 * @param outputScanFilters the output scan filters. 281 * @param scanFilterCount the number of scan filters. 282 * @return whether the validation was successful. 283 */ 284 bool validateBleScanFilters(const chre_rpc_ChreBleGenericFilter *scanFilters, 285 chreBleGenericFilter *outputScanFilters, 286 uint32_t scanFilterCount); 287 288 constexpr static uint32_t kMaxNumEventTypes = 289 10; // declared in chre_api_test.options 290 291 /** 292 * Variables to control synchronization for sync API calls. 293 * Only one sync API call may be made at a time. 294 */ 295 Optional<ServerWriter<chre_rpc_GeneralSyncMessage>> mWriter; 296 uint32_t mSyncTimerHandle = CHRE_TIMER_INVALID; 297 uint8_t mRequestType; 298 299 /* 300 * Variables to control synchronization for sync events calls. 301 * Only one sync event call may be made at a time. 302 */ 303 Optional<ServerWriter<chre_rpc_GeneralEventsMessage>> mEventWriter; 304 uint32_t mEventTimerHandle = CHRE_TIMER_INVALID; 305 uint16_t mEventTypes[kMaxNumEventTypes]; 306 uint32_t mEventTypeCount; 307 uint32_t mEventExpectedCount; 308 uint32_t mEventSentCount; 309 }; 310 311 /** 312 * Handles RPC requests for the CHRE API Test nanoapp. 313 */ 314 class ChreApiTestManager { 315 public: 316 /** 317 * Allows the manager to do any init necessary as part of nanoappStart. 318 */ 319 bool start(); 320 321 /** 322 * Allows the manager to do any cleanup necessary as part of nanoappEnd. 323 */ 324 void end(); 325 326 /** 327 * Handle a CHRE event. 328 * 329 * @param senderInstanceId the instand ID that sent the event. 330 * @param eventType the type of the event. 331 * @param eventData the data for the event. 332 */ 333 void handleEvent(uint32_t senderInstanceId, uint16_t eventType, 334 const void *eventData); 335 336 /** 337 * Sets the permission for the next server message. 338 * 339 * @params permission Bitmasked CHRE_MESSAGE_PERMISSION_. 340 */ 341 void setPermissionForNextMessage(uint32_t permission); 342 343 private: 344 // RPC server. 345 chre::RpcServer mServer; 346 347 // pw_rpc service used to process the RPCs. 348 ChreApiTestService mChreApiTestService; 349 }; 350 351 typedef chre::Singleton<ChreApiTestManager> ChreApiTestManagerSingleton; 352 353 #endif // CHRE_API_TEST_MANAGER_H_ 354