1 /* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HID_HOST_L2CAP_CONNECTION_H 17 #define HID_HOST_L2CAP_CONNECTION_H 18 19 #include "base_def.h" 20 #include "l2cap_if.h" 21 #include "gap_if.h" 22 #include "hid_host_defines.h" 23 #include "hid_host_message.h" 24 #include "raw_address.h" 25 26 namespace OHOS { 27 namespace bluetooth { 28 /** 29 * @brief Class for l2cap connection. 30 */ 31 class HidHostL2capConnection { 32 public: 33 /** 34 * @brief start l2cap connection. 35 */ 36 static int Startup(); 37 38 /** 39 * @brief close l2cap connection. 40 */ 41 static void Shutdown(); 42 43 /** 44 * @brief Construct a new HidHostL2capConnection object. 45 */ 46 explicit HidHostL2capConnection(std::string address); 47 48 /** 49 * @brief Destroy the HidHostL2capConnection object. 50 */ 51 ~HidHostL2capConnection() = default; 52 53 /** 54 * @brief This function used to connect peer device L2CAP channel after SDP discovery OK (Initiator). 55 * 56 * @return Returns the result of connect. 57 */ 58 int Connect(); 59 60 /** 61 * @brief This function used to close L2CAP connection. 62 * 63 * @return Returns the result of disconnect. 64 */ 65 int Disconnect(); 66 67 /** 68 * @brief This function used to send data to remote device. 69 * @param sendData The data information. 70 * @param length The data length. 71 * @param pkt The send data. 72 * 73 * @return Returns the result. 74 */ 75 int SendData(SendHidData sendData, int length, uint8_t* pkt); 76 77 /** 78 * @brief This function used to get the control channel's local cid. 79 * 80 * @return Returns the cid. 81 */ 82 uint16_t GetControlLcid(); 83 84 /** 85 * @brief This function used to get the interrupt channel's local cid. 86 * 87 * @return Returns the cid. 88 */ 89 uint16_t GetInterruptLcid(); 90 91 /** 92 * @brief Process l2cap event. 93 * 94 * @param event The event. 95 */ 96 97 void ProcessEvent(const HidHostMessage &event); 98 99 /** 100 * @brief Get the l2cap event name. 101 * 102 * @param what event number. 103 * @return Returns event string name. 104 */ 105 static std::string GetEventName(int what); 106 107 private: 108 /** 109 * @brief Outgoing connect gap security callback. 110 * 111 * @param result Indicates the result. 112 * @param security Indicates the struct GapServiceSecurityInfo. 113 * @param context Indicates the pointer to context. 114 */ 115 static void HidHostSecurityCheckCallback(uint16_t result, GapServiceSecurityInfo security, void *context); 116 void HidHostSecurityCheckTask(uint16_t result); 117 /** 118 * @brief Incoming connect gap security callback. 119 * 120 * @param result Indicates the result. 121 * @param security Indicates the struct GapServiceSecurityInfo. 122 * @param context Indicates the pointer to context. 123 */ 124 static void HidHostIndSecurityCheckDeviceConnectCallback(uint16_t result, 125 GapServiceSecurityInfo security, void *context); 126 void HidHostSecurityCheckDeviceConnectTask(uint16_t result); 127 128 /** 129 * @brief callback of control channel connect request. 130 * 131 * @param addr Indicates the pointer to BtAddr. 132 * @param lcid Indicates the lcid. 133 * @param result Indicates the result. 134 * @param context Indicates the pointer to context. 135 */ 136 static void HidHostL2cConnectControlReqCallback(const BtAddr *addr, uint16_t lcid, int result, void *context); 137 138 /** 139 * @brief callback of interrupt channel connect request. 140 * 141 * @param addr Indicates the pointer to BtAddr. 142 * @param lcid Indicates the lcid. 143 * @param result Indicates the result. 144 * @param context Indicates the pointer to context. 145 */ 146 static void HidHostL2cConnectInterruptReqCallback(const BtAddr *addr, uint16_t lcid, int result, void *context); 147 148 /** 149 * @brief received bredr connect request. 150 * 151 * @param lcid Indicates the lcid. 152 * @param id Indicates the id. 153 * @param info Indicates the pointer to L2capConnectionInfo. 154 * @param lpsm Indicates the lpsm. 155 * @param ctx Indicates the pointer to context. 156 */ 157 static void HidHostRecvConnectionReqCallback( 158 uint16_t lcid, uint8_t id, const L2capConnectionInfo *info, uint16_t lpsm, void *ctx); 159 160 /** 161 * @brief received bredr connect response. 162 * 163 * @param lcid Indicates the lcid. 164 * @param info Indicates the pointer to L2capConnectionInfo. 165 * @param result Indicates the result. 166 * @param status Indicates the status. 167 * @param ctx Indicates the pointer to context. 168 */ 169 static void HidHostRecvConnectionRspCallback( 170 uint16_t lcid, const L2capConnectionInfo *info, uint16_t result, uint16_t status, void *ctx); 171 172 /** 173 * @brief receive config request. 174 * 175 * @param lcid Indicates the lcid. 176 * @param id Indicates the id. 177 * @param cfg Indicates the pointer to const L2capConfigInfo. 178 * @param ctx Indicates the pointer to context. 179 */ 180 static void HidHostRecvConfigReqCallback(uint16_t lcid, uint8_t id, const L2capConfigInfo *cfg, void *ctx); 181 182 /** 183 * @brief receive config response. 184 * 185 * @param lcid Indicates the lcid. 186 * @param cfg Indicates the pointer to const L2capConfigInfo. 187 * @param result Indicates the result. 188 * @param ctx Indicates the pointer to context. 189 */ 190 static void HidHostRecvConfigRspCallback(uint16_t lcid, const L2capConfigInfo *cfg, uint16_t result, void *ctx); 191 192 /** 193 * @brief receive disconnect request. 194 * 195 * @param lcid local channel id. 196 * @param lcid Indicates the lcid. 197 * @param id Indicates the id. 198 * @param ctx Indicates the pointer to context. 199 */ 200 static void HidHostRecvDisconnectionReqCallback(uint16_t lcid, uint8_t id, void *ctx); 201 202 /** 203 * @brief receive disconnect response. 204 * 205 * @param lcid Indicates the lcid. 206 * @param ctx Indicates the pointer to context. 207 */ 208 static void HidHostRecvDisconnectionRspCallback(uint16_t lcid, void *ctx); 209 210 /** 211 * @brief receive disconnect abnormal. 212 * 213 * @param lcid Indicates the lcid. 214 * @param reason Indicates the reason. 215 * @param ctx Indicates the pointer to context. 216 217 */ 218 static void HidHostDisconnectAbnormalCallback(uint16_t lcid, uint8_t reason, void *ctx); 219 220 /** 221 * @brief receive message from peer. 222 * 223 * @param lcid Indicates the lcid. 224 * @param pkt message data 225 * @param ctx Indicates the pointer to context. 226 */ 227 static void HidHostRecvDataCallback(uint16_t lcid, Packet *pkt, void *ctx); 228 229 /** 230 * @brief remote peer is busy in Enhanced mode. 231 * 232 * @param lcid Indicates the lcid. 233 * @param isBusy busy/unbusy 234 * @param ctx Indicates the pointer to context. 235 */ 236 static void HidHostRemoteBusyCallback(uint16_t lcid, uint8_t isBusy, void *ctx); 237 238 // Receive l2cap callback,do the task. 239 void HidHostRecvConnectionReqCallbackTask(uint16_t lcid, uint8_t id, uint16_t lpsm); 240 void HidHostRecvConnectionRspCallbackTask(uint16_t lcid, int result); 241 void HidHostRecvConfigReqCallbackTask(uint16_t lcid, uint8_t id, L2capConfigInfo cfg); 242 void HidHostRecvConfigRspCallbackTask(uint16_t lcid, const L2capConfigInfo cfg, uint16_t result); 243 void HidHostRecvDisconnectionReqCallbackTask(uint16_t lcid, uint8_t id); 244 void HidHostRecvDisconnectionRspCallbackTask(uint16_t lcid); 245 void HidHostDisconnectAbnormalCallbackTask(uint16_t lcid, uint8_t reason); 246 void HidHostRemoteBusyCallbackTask(uint16_t lcid, uint8_t isBusy); 247 static uint8_t HidHostGetType(uint8_t type); 248 static uint8_t HidHostGetParam(uint8_t type); 249 int SendGapRequestSecurity(bool isIncoming, uint16_t lcid, uint8_t id); 250 void SendGetReport(SendHidData sendData); 251 void SendSetReport(SendHidData sendData, int length, uint8_t* pkt); 252 253 // Regist l2cap callback 254 static constexpr L2capService L2CAP_HID_Host_CALLBACK = { 255 HidHostRecvConnectionReqCallback, 256 HidHostRecvConnectionRspCallback, 257 HidHostRecvConfigReqCallback, 258 HidHostRecvConfigRspCallback, 259 HidHostRecvDisconnectionReqCallback, 260 HidHostRecvDisconnectionRspCallback, 261 HidHostDisconnectAbnormalCallback, 262 HidHostRecvDataCallback, 263 HidHostRemoteBusyCallback 264 }; 265 266 std::string address_; 267 uint8_t state_; /* Device state */ 268 uint16_t ctrlLcid_; 269 uint8_t ctrlId_; 270 uint16_t intrLcid_; 271 uint8_t connFlags_; 272 273 BT_DISALLOW_COPY_AND_ASSIGN(HidHostL2capConnection); 274 }; 275 } // namespace bluetooth 276 } // namespace OHOS 277 #endif // HID_HOST_L2CAP_CONNECTION_H