1 /* 2 * Copyright (C) 2025 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 #pragma once 18 19 #include <string> 20 #include <vector> 21 22 #include <aidl/android/hardware/contexthub/BnContextHub.h> 23 #include <aidl/android/hardware/contexthub/BnEndpointCommunication.h> 24 25 #include "chre/platform/shared/host_protocol_common.h" 26 #include "chre_host/generated/host_messages_generated.h" 27 #include "flatbuffers/flatbuffers.h" 28 29 namespace android::hardware::contexthub::common::implementation { 30 31 using AidlEndpointId = ::aidl::android::hardware::contexthub::EndpointId; 32 using AidlEndpointInfo = ::aidl::android::hardware::contexthub::EndpointInfo; 33 using AidlHubInfo = ::aidl::android::hardware::contexthub::HubInfo; 34 using AidlMessage = ::aidl::android::hardware::contexthub::Message; 35 using AidlMessageDeliveryStatus = 36 ::aidl::android::hardware::contexthub::MessageDeliveryStatus; 37 using AidlReason = ::aidl::android::hardware::contexthub::Reason; 38 using AidlService = ::aidl::android::hardware::contexthub::Service; 39 40 /** Helpers for converting ContextHub V4+ AIDL <-> CHRE flatbuffer messages */ 41 class HostProtocolHostV4 : public ::chre::HostProtocolCommon { 42 public: 43 /** 44 * Encodes a GetMessageHubsAndEndpointsRequest message 45 * 46 * @param builder The builder used to encode the message 47 */ 48 static void encodeGetMessageHubsAndEndpointsRequest( 49 flatbuffers::FlatBufferBuilder &builder); 50 51 /** 52 * Encodes a GetMessageHubsAndEndpointsResponse message 53 * 54 * @param builder The builder used to encode the message 55 * @param hubs The list of known host message hubs 56 * @param endpoints The list of known host endpoints 57 */ 58 static void encodeGetMessageHubsAndEndpointsResponse( 59 flatbuffers::FlatBufferBuilder &builder, 60 const std::vector<AidlHubInfo> &hubs, 61 const std::vector<AidlEndpointInfo> &endpoints); 62 63 /** 64 * Encodes a RegisterMessageHub message 65 * 66 * @param builder The builder used to encode the message 67 * @param hub The host message hub being registered 68 */ 69 static void encodeRegisterMessageHub(flatbuffers::FlatBufferBuilder &builder, 70 const AidlHubInfo &info); 71 72 /** 73 * Encodes a UnregisterMessageHub message 74 * 75 * @param builder The builder used to encode the message 76 * @param id The id of the host message hub being unregistered 77 */ 78 static void encodeUnregisterMessageHub( 79 flatbuffers::FlatBufferBuilder &builder, int64_t id); 80 81 /** 82 * Encodes a RegisterEndpoint message 83 * 84 * @param builder The builder used to encode the message 85 * @param info The host endpoint being registered 86 */ 87 static void encodeRegisterEndpoint(flatbuffers::FlatBufferBuilder &builder, 88 const AidlEndpointInfo &info); 89 90 /** 91 * Encodes a UnregisterEndpoint message 92 * 93 * @param builder The builder used to encode the message 94 * @param id The id of the host endpoint being unregistered 95 */ 96 static void encodeUnregisterEndpoint(flatbuffers::FlatBufferBuilder &builder, 97 const AidlEndpointId &id); 98 99 /** 100 * Encodes a OpenEndpointSessionRequest message 101 * 102 * @param builder The builder used to encode the message 103 * @param hostHubId The id of the host hub originating the request 104 * @param sessionId The id of the session to be created 105 * @param initiator The id of the host endpoint 106 * @param destination The id of the embedded endpoint 107 * @param serviceDescriptor The descriptor of the service for the session 108 */ 109 static void encodeOpenEndpointSessionRequest( 110 flatbuffers::FlatBufferBuilder &builder, int64_t hostHubId, 111 uint16_t sessionId, const AidlEndpointId &initiator, 112 const AidlEndpointId &destination, 113 const std::optional<std::string> &serviceDescriptor); 114 115 /** 116 * Encodes a EndpointSessionOpened message 117 * 118 * @param builder The builder used to encode the message 119 * @param hostHubId The id of the host hub originating the notification 120 * @param sessionId The id of the session that was accepted 121 */ 122 static void encodeEndpointSessionOpened( 123 flatbuffers::FlatBufferBuilder &builder, int64_t hostHubId, 124 uint16_t sessionId); 125 126 /** 127 * Encodes a EndpointSessionClosed message 128 * 129 * @param builder The builder used to encode the message 130 * @param hostHubId The id of the host hub originating the notification 131 * @param sessionId The id of the session that was closed 132 * @param reason The reason for the closure 133 */ 134 static void encodeEndpointSessionClosed( 135 flatbuffers::FlatBufferBuilder &builder, int64_t hostHubId, 136 uint16_t sessionId, AidlReason reason); 137 138 /** 139 * Encodes a EndpointSessionMessage message 140 * 141 * @param builder The builder used to encode the message 142 * @param hostHubId The id of the host hub originating the message 143 * @param sessionId The id of the session over which the message was sent 144 * @param message The message being sent 145 */ 146 static void encodeEndpointSessionMessage( 147 flatbuffers::FlatBufferBuilder &builder, int64_t hostHubId, 148 uint16_t sessionId, const AidlMessage &message); 149 150 /** 151 * Encodes a EndpointSessionMessageDeliveryStatus message 152 * 153 * @param builder The builder used to encode the message 154 * @param hostHubId The id of the host hub originating the notification 155 * @param sessionId The id of the session over which the message was sent 156 * @param status The status of the message delivery 157 */ 158 static void encodeEndpointSessionMessageDeliveryStatus( 159 flatbuffers::FlatBufferBuilder &builder, int64_t hostHubId, 160 uint16_t sessionId, const AidlMessageDeliveryStatus &status); 161 162 /** 163 * Decodes a GetMessageHubsAndEndpointsResponse message 164 * 165 * @param msg The message 166 * @param hubs (out) The list of embedded message hubs 167 * @param endpoints (out) The list of embedded endpoints 168 */ 169 static void decodeGetMessageHubsAndEndpointsResponse( 170 const ::chre::fbs::GetMessageHubsAndEndpointsResponseT &msg, 171 std::vector<AidlHubInfo> &hubs, std::vector<AidlEndpointInfo> &endpoints); 172 173 /** 174 * Decodes a RegisterMessageHub message 175 * 176 * @param msg The message 177 * @param info (out) The details of the new embedded message hub 178 */ 179 static void decodeRegisterMessageHub( 180 const ::chre::fbs::RegisterMessageHubT &msg, AidlHubInfo &info); 181 182 /** 183 * Decodes a UnregisterMessageHub message 184 * 185 * @param msg The message 186 * @param id (out) The id of the unregistered embedded hub 187 */ 188 static void decodeUnregisterMessageHub( 189 const ::chre::fbs::UnregisterMessageHubT &msg, int64_t &id); 190 191 /** 192 * Decodes a RegisterEndpoint message 193 * 194 * @param msg The message 195 * @param info (out) The details of the new embedded endpoint 196 */ 197 static void decodeRegisterEndpoint(const ::chre::fbs::RegisterEndpointT &msg, 198 AidlEndpointInfo &info); 199 200 /** 201 * Decodes a AddServiceToEndpoint message 202 * 203 * @param msg The message 204 * @param info (out) The id of the new embedded endpoint 205 * @param service (out) The service being added 206 */ 207 static void decodeAddServiceToEndpoint( 208 const ::chre::fbs::AddServiceToEndpointT &msg, AidlEndpointId &id, 209 AidlService &service); 210 211 /** 212 * Decodes a EndpointReady message 213 * 214 * @param msg The message 215 * @param info (out) The id of the newly ready embedded endpoint 216 */ 217 static void decodeEndpointReady(const ::chre::fbs::EndpointReadyT &msg, 218 AidlEndpointId &id); 219 220 /** 221 * Decodes a UnregisterEndpoint message 222 * 223 * @param msg The message 224 * @param info (out) The id of the unregistered embedded endpoint 225 */ 226 static void decodeUnregisterEndpoint( 227 const ::chre::fbs::UnregisterEndpointT &msg, AidlEndpointId &id); 228 229 /** 230 * Decodes a OpenEndpointSessionRequest message 231 * 232 * @param msg The message 233 * @param hubId (out) The destination host message hub id 234 * @param sessionId (out) The id of the session being requested 235 * @param hostEndpoint (out) The destination host endpoint id 236 * @param embeddedEndpoint (out) The initating embedded endpoint id 237 * @param serviceDescriptor (out) The service requested for the session 238 */ 239 static void decodeOpenEndpointSessionRequest( 240 const ::chre::fbs::OpenEndpointSessionRequestT &msg, int64_t &hubId, 241 uint16_t &sessionId, AidlEndpointId &hostEndpoint, 242 AidlEndpointId &embeddedEndpoint, 243 std::optional<std::string> &serviceDescriptor); 244 245 /** 246 * Decodes a EndpointSessionOpened message 247 * 248 * @param msg The message 249 * @param hubId (out) The id of the destination host message hub 250 * @param sessionId (out) The id of the accepted session 251 */ 252 static void decodeEndpointSessionOpened( 253 const ::chre::fbs::EndpointSessionOpenedT &msg, int64_t &hubId, 254 uint16_t &sessionId); 255 256 /** 257 * Decodes a EndpointSessionClosed message 258 * 259 * @param msg The message 260 * @param hubId (out) The id of the destination host message hub 261 * @param sessionId (out) The id of the closed session 262 */ 263 static void decodeEndpointSessionClosed( 264 const ::chre::fbs::EndpointSessionClosedT &msg, int64_t &hubId, 265 uint16_t &sessionId, AidlReason &reason); 266 267 /** 268 * Decodes a EndpointSessionMessage message 269 * 270 * @param msg The wrapper message 271 * @param hubId (out) The id of the destination host message hub 272 * @param sessionId (out) The id of the session hosting the message 273 * @param message (out) The containined message 274 */ 275 static void decodeEndpointSessionMessage( 276 const ::chre::fbs::EndpointSessionMessageT &msg, int64_t &hubId, 277 uint16_t &sessionId, AidlMessage &message); 278 279 /** 280 * Decodes a EndpointSessionMessageDeliveryStatus message 281 * 282 * @param msg The wrapper message 283 * @param hubId (out) The id of the destination host message hub 284 * @param sessionId (out) The id of the session hosting the message 285 * @param status (out) The result of a message sent over sessionId 286 */ 287 static void decodeEndpointSessionMessageDeliveryStatus( 288 const ::chre::fbs::EndpointSessionMessageDeliveryStatusT &msg, 289 int64_t &hubId, uint16_t &sessionId, AidlMessageDeliveryStatus &status); 290 291 private: 292 static flatbuffers::Offset<::chre::fbs::MessageHub> aidlToFbsMessageHub( 293 flatbuffers::FlatBufferBuilder &builder, const AidlHubInfo &info); 294 295 static AidlHubInfo fbsMessageHubToAidl(const ::chre::fbs::MessageHubT &hub); 296 297 static flatbuffers::Offset<::chre::fbs::EndpointInfo> aidlToFbsEndpointInfo( 298 flatbuffers::FlatBufferBuilder &builder, const AidlEndpointInfo &info); 299 300 static AidlEndpointInfo fbsEndpointInfoToAidl( 301 const ::chre::fbs::EndpointInfoT &endpoint); 302 303 static flatbuffers::Offset<::chre::fbs::EndpointId> aidlToFbsEndpointId( 304 flatbuffers::FlatBufferBuilder &builder, const AidlEndpointId &id); 305 306 static AidlEndpointId fbsEndpointIdToAidl( 307 const ::chre::fbs::EndpointIdT &endpoint); 308 }; 309 310 } // namespace android::hardware::contexthub::common::implementation 311