1 /* 2 * Copyright (C) 2021-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 HFP_AG_RFCOMM_CONNECTION_H 17 #define HFP_AG_RFCOMM_CONNECTION_H 18 19 #include <cstdint> 20 #include <map> 21 #include <mutex> 22 #include <string> 23 24 #include "base_def.h" 25 #include "btstack.h" 26 #include "hfp_ag_defines.h" 27 #include "hfp_ag_gap_client.h" 28 #include "packet.h" 29 #include "raw_address.h" 30 #include "rfcomm.h" 31 32 namespace OHOS { 33 namespace bluetooth { 34 /** 35 * @brief Class for rfcomm connection. 36 */ 37 class HfpAgRfcommConnection { 38 public: 39 /** 40 * @brief Init rfcomm connection. 41 */ 42 static int Init(); 43 44 /** 45 * @brief Clean up rfcomm connection. 46 */ 47 static void CleanUp(); 48 49 /** 50 * @brief Add device to device map. 51 * 52 * @param handle Device handle. 53 * @param addr Device address. 54 */ 55 static void AddConnectionDevice(uint16_t handle, std::string addr); 56 57 /** 58 * @brief Remove device from device map. 59 * 60 * @param handle Device handle. 61 */ 62 static void RemoveConnectionDevice(uint16_t handle); 63 64 /** 65 * @brief Get the Remote Address By Handle object. 66 * 67 * @param handle Rfcomm handle. 68 * @return Returns the remote device address. 69 */ 70 static std::string GetRemoteAddressByHandle(uint16_t handle); 71 72 /** 73 * @brief Construct a new HfpAgRfcommConnection object. 74 * 75 * @param fn Rfcomm callback. 76 */ HfpAgRfcommConnection(RFCOMM_EventCallback fn)77 explicit HfpAgRfcommConnection(RFCOMM_EventCallback fn) : fn_(fn) 78 {} 79 80 /** 81 * @brief Destroy the HfpAgRfcommConnection object. 82 */ ~HfpAgRfcommConnection()83 ~HfpAgRfcommConnection(){}; 84 85 /** 86 * @brief This function used to connect peer device rfcomm channel after SDP discovery OK (Initiator). 87 * 88 * @return Returns the result of connect. 89 */ 90 int Connect(); 91 92 /** 93 * @brief This function used to close rfcomm connection. 94 * 95 * @return Returns the result of disconnect. 96 */ 97 int Disconnect() const; 98 99 /** 100 * @brief This function used to write data to peer device. 101 * 102 * @param pkt The address of data packet pointer. 103 * @return Returns the result of read data. 104 */ 105 int ReadData(Packet **pkt) const; 106 107 /** 108 * @brief This function used to write data to peer device. 109 * 110 * @param pkt Data packet pointer. 111 * @return Returns the result of write data. 112 */ 113 int WriteData(Packet &pkt) const; 114 115 /** 116 * @brief This function set rfcomm connection handle. 117 * 118 * @param handle Rfcomm handle. 119 */ 120 void SetConnectionHandle(uint16_t handle); 121 122 /** 123 * @brief This function get connection by handle. 124 * 125 * @return Returns rfcomm connection handle. 126 */ 127 uint16_t GetConnectionHandle() const; 128 129 /** 130 * @brief This function set remote address. 131 * 132 * @param addr Remote device address. 133 */ 134 void SetRemoteAddr(const std::string &addr); 135 136 /** 137 * @brief This function set remote server channel number. 138 * 139 * @param scn Remote server channel number 140 */ 141 void SetRemoteScn(uint8_t scn); 142 143 /** 144 * @brief This function get remote server channel number. 145 * 146 * @return Remote server channel number 147 */ 148 uint8_t GetRemoteScn() const; 149 150 private: 151 /** 152 * @brief This function create channel to connect rfcomm. 153 * 154 * @return Returns the result of create channel. 155 */ 156 int Create(); 157 158 static std::map<uint16_t, std::string> g_devMap; 159 BtAddr remoteAddr_ {}; 160 uint8_t remoteScn_ {0}; 161 uint16_t connHandle_ {0}; 162 RFCOMM_EventCallback fn_; 163 std::unique_ptr<HfpAgGapClient> gap_ {std::make_unique<HfpAgGapClient>()}; 164 inline static constexpr uint32_t HFP_RFCOMM_CONNECTION_EVENTS = 165 RFCOMM_CHANNEL_EV_CONNECT_SUCCESS | RFCOMM_CHANNEL_EV_CONNECT_FAIL | RFCOMM_CHANNEL_EV_DISCONNECTED | 166 RFCOMM_CHANNEL_EV_DISCONNECT_SUCCESS | RFCOMM_CHANNEL_EV_DISCONNECT_FAIL | RFCOMM_CHANNEL_EV_REV_DATA; 167 168 BT_DISALLOW_COPY_AND_ASSIGN(HfpAgRfcommConnection); 169 }; 170 } // namespace bluetooth 171 } // namespace OHOS 172 #endif // HFP_AG_RFCOMM_CONNECTION_H