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 PAN_BNEP_H 17 #define PAN_BNEP_H 18 19 #include <queue> 20 21 #include "base_def.h" 22 #include "l2cap_if.h" 23 #include "gap_if.h" 24 #include "pan_defines.h" 25 #include "pan_message.h" 26 #include "raw_address.h" 27 28 namespace OHOS { 29 namespace bluetooth { 30 /** 31 * @brief Class for l2cap connection. 32 */ 33 class PanBnep { 34 public: 35 /** 36 * @brief start l2cap connection. 37 */ 38 static int Startup(); 39 40 /** 41 * @brief close l2cap connection. 42 */ 43 static void Shutdown(); 44 45 /** 46 * @brief Construct a new PanBnep object. 47 */ 48 explicit PanBnep(std::string address); 49 50 /** 51 * @brief Destroy the PanBnep object. 52 */ 53 ~PanBnep(); 54 55 /** 56 * @brief This function used to connect peer device L2CAP channel. 57 * 58 * @return Returns the result of connect. 59 */ 60 int Connect(); 61 62 /** 63 * @brief This function used to close L2CAP connection. 64 * 65 * @return Returns the result of disconnect. 66 */ 67 int Disconnect(); 68 69 /** 70 * @brief This function used to send data to remote device. 71 * @param ethernetHeader The ethernet information. 72 * @param length The data length. 73 * @param pkt The send data. 74 * 75 * @return Returns the result. 76 */ 77 int SendData(EthernetHeader ethernetHeader, uint8_t* pkt, int length); 78 79 /** 80 * @brief This function used to get the local cid. 81 * 82 * @return Returns the cid. 83 */ 84 uint16_t GetLcid(); 85 86 /** 87 * @brief Process l2cap event. 88 * 89 * @param event The event. 90 */ 91 92 void ProcessEvent(const PanMessage &event); 93 94 /** 95 * @brief Get the l2cap event name. 96 * 97 * @param what event number. 98 * @return Returns event string name. 99 */ 100 static std::string GetEventName(int what); 101 102 private: 103 /** 104 * @brief Outgoing connect gap security callback. 105 * 106 * @param result Indicates the result. 107 * @param security Indicates the struct GapServiceSecurityInfo. 108 * @param context Indicates the pointer to context. 109 */ 110 static void BnepSecurityCheckCallback(uint16_t result, GapServiceSecurityInfo security, void *context); 111 void BnepSecurityCheckTask(uint16_t result); 112 /** 113 * @brief Incoming connect gap security callback. 114 * 115 * @param result Indicates the result. 116 * @param security Indicates the struct GapServiceSecurityInfo. 117 * @param context Indicates the pointer to context. 118 */ 119 static void BnepIndSecurityCheckDeviceConnectCallback(uint16_t result, 120 GapServiceSecurityInfo security, void *context); 121 void BnepSecurityCheckDeviceConnectTask(uint16_t result); 122 123 /** 124 * @brief callback of control channel connect request. 125 * 126 * @param addr Indicates the pointer to BtAddr. 127 * @param lcid Indicates the lcid. 128 * @param result Indicates the result. 129 * @param context Indicates the pointer to context. 130 */ 131 static void BnepL2cConnectReqCallback(const BtAddr *addr, uint16_t lcid, int result, void *context); 132 133 /** 134 * @brief received bredr connect request. 135 * 136 * @param lcid Indicates the lcid. 137 * @param id Indicates the id. 138 * @param info Indicates the pointer to L2capConnectionInfo. 139 * @param lpsm Indicates the lpsm. 140 * @param ctx Indicates the pointer to context. 141 */ 142 static void BnepRecvConnectionReqCallback( 143 uint16_t lcid, uint8_t id, const L2capConnectionInfo *info, uint16_t lpsm, void *ctx); 144 145 /** 146 * @brief received bredr connect response. 147 * 148 * @param lcid Indicates the lcid. 149 * @param info Indicates the pointer to L2capConnectionInfo. 150 * @param result Indicates the result. 151 * @param status Indicates the status. 152 * @param ctx Indicates the pointer to context. 153 */ 154 static void BnepRecvConnectionRspCallback( 155 uint16_t lcid, const L2capConnectionInfo *info, uint16_t result, uint16_t status, void *ctx); 156 157 /** 158 * @brief receive config request. 159 * 160 * @param lcid Indicates the lcid. 161 * @param id Indicates the id. 162 * @param cfg Indicates the pointer to const L2capConfigInfo. 163 * @param ctx Indicates the pointer to context. 164 */ 165 static void BnepRecvConfigReqCallback(uint16_t lcid, uint8_t id, const L2capConfigInfo *cfg, void *ctx); 166 167 /** 168 * @brief receive config response. 169 * 170 * @param lcid Indicates the lcid. 171 * @param cfg Indicates the pointer to const L2capConfigInfo. 172 * @param result Indicates the result. 173 * @param ctx Indicates the pointer to context. 174 */ 175 static void BnepRecvConfigRspCallback(uint16_t lcid, const L2capConfigInfo *cfg, uint16_t result, void *ctx); 176 177 /** 178 * @brief receive disconnect request. 179 * 180 * @param lcid local channel id. 181 * @param lcid Indicates the lcid. 182 * @param id Indicates the id. 183 * @param ctx Indicates the pointer to context. 184 */ 185 static void BnepRecvDisconnectionReqCallback(uint16_t lcid, uint8_t id, void *ctx); 186 187 /** 188 * @brief receive disconnect response. 189 * 190 * @param lcid Indicates the lcid. 191 * @param ctx Indicates the pointer to context. 192 */ 193 static void BnepRecvDisconnectionRspCallback(uint16_t lcid, void *ctx); 194 195 /** 196 * @brief receive disconnect abnormal. 197 * 198 * @param lcid Indicates the lcid. 199 * @param reason Indicates the reason. 200 * @param ctx Indicates the pointer to context. 201 202 */ 203 static void BnepDisconnectAbnormalCallback(uint16_t lcid, uint8_t reason, void *ctx); 204 205 /** 206 * @brief receive message from peer. 207 * 208 * @param lcid Indicates the lcid. 209 * @param pkt message data 210 * @param ctx Indicates the pointer to context. 211 */ 212 static void BnepRecvDataCallback(uint16_t lcid, Packet *pkt, void *ctx); 213 214 /** 215 * @brief remote peer is busy in Enhanced mode. 216 * 217 * @param lcid Indicates the lcid. 218 * @param isBusy busy/unbusy 219 * @param ctx Indicates the pointer to context. 220 */ 221 static void BnepRemoteBusyCallback(uint16_t lcid, uint8_t isBusy, void *ctx); 222 223 // Receive l2cap callback,do the task. 224 void BnepRecvConnectionReqCallbackTask(uint16_t lcid, uint8_t id, uint16_t lpsm); 225 void BnepRecvConnectionRspCallbackTask(uint16_t lcid, int result); 226 void BnepRecvConfigReqCallbackTask(uint16_t lcid, uint8_t id, L2capConfigInfo cfg); 227 void BnepRecvConfigRspCallbackTask(uint16_t lcid, const L2capConfigInfo cfg, uint16_t result); 228 void BnepRecvDisconnectionReqCallbackTask(uint16_t lcid, uint8_t id); 229 void BnepRecvDisconnectionRspCallbackTask(uint16_t lcid); 230 void BnepDisconnectAbnormalCallbackTask(uint16_t lcid, uint8_t reason); 231 void BnepRecvDataCallbackTask(uint16_t lcid, uint8_t *data, int dataLength); 232 void BnepRemoteBusyCallbackTask(uint16_t lcid, uint8_t isBusy); 233 int SendGapRequestSecurity(bool isIncoming, uint16_t lcid, uint8_t id); 234 235 int ProcessBnepControlData(uint8_t *data, int dataLength, bool isExtension); 236 int ProcessBnepExtensionData(uint8_t *data, int dataLength); 237 int ProcessBnepControlCommandNotUnderstood(uint8_t *data, int dataLength); 238 int ProcessBnepSetupConnectionRequest(uint8_t *data, int dataLength); 239 int ProcessBnepSetupConnectionResponse(uint8_t *data, int dataLength); 240 int ProcessBnepFilterNetTypeSet(uint8_t *data, int dataLength); 241 int ProcessBnepFilterNetTypeResponse(uint8_t *data, int dataLength); 242 int ProcessBnepFilterMultiAddrSet(uint8_t *data, int dataLength); 243 int ProcessBnepFilterMultiAddrResponse(uint8_t *data, int dataLength); 244 int ProcessBnepEthernetPacketHeader(uint8_t type, EthernetHeader ðernetHeader, 245 uint8_t *data, int dataLength); 246 void CheckRoleUuid(Uuid localUuid, Uuid remoteUuid); 247 void BnepSendControlCommandNotUnderstood(uint8_t controlCommand); 248 void BnepSendSetupConnectionResponse(uint16_t responseMessage); 249 void BnepSendFilterNetTypeResponse(uint16_t responseMessage); 250 void BnepSendFilterMultiAddrResponse(uint16_t responseMessage); 251 int BnepBuildEthernetPacketHeader(uint8_t type, EthernetHeader ethernetHeader, uint8_t *buf); 252 bool CheckBnepEthernetDataFilter(EthernetHeader ethernetHeader, uint8_t* pkt, int length); 253 void AddPacketToWaitingSendDataList(Packet *addPacket); 254 void SendBnepDataEvent(EthernetHeader ethernetHeader, int dataLength, int offset, uint8_t *data); 255 256 // Regist l2cap callback 257 static constexpr L2capService BNEP_CALLBACK = { 258 BnepRecvConnectionReqCallback, 259 BnepRecvConnectionRspCallback, 260 BnepRecvConfigReqCallback, 261 BnepRecvConfigRspCallback, 262 BnepRecvDisconnectionReqCallback, 263 BnepRecvDisconnectionRspCallback, 264 BnepDisconnectAbnormalCallback, 265 BnepRecvDataCallback, 266 BnepRemoteBusyCallback 267 }; 268 269 std::string address_; 270 uint8_t state_; /* Device state */ 271 uint16_t lcid_; 272 uint16_t id_; 273 uint8_t connFlags_; 274 bool isBusy_; 275 std::queue<Packet *> waitingSendDataList_; 276 std::vector<BnepProtocolFilterRange> protocolFilterList_; 277 std::vector<BnepMultycastFilterRange> multycastFilterList_; 278 279 BT_DISALLOW_COPY_AND_ASSIGN(PanBnep); 280 }; 281 } // namespace bluetooth 282 } // namespace OHOS 283 #endif // PAN_BNEP_H