1 /* 2 * Copyright (C) 2021 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 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief Defines a bluetooth system that provides basic blurtooth connection and profile functions, 21 * including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc. 22 * 23 * @since 6 24 */ 25 26 /** 27 * @file bluetooth_socket.h 28 * 29 * @brief Declares spp socket framework functions, including basic functions. 30 * 31 * @since 6 32 */ 33 34 #ifndef BLUETOOTH_SOCKET_H 35 #define BLUETOOTH_SOCKET_H 36 37 #include <string> 38 #include <vector> 39 #include <memory> 40 41 #include "bluetooth_remote_device.h" 42 #include "bluetooth_socket_inputstream.h" 43 #include "bluetooth_socket_outputstream.h" 44 45 namespace OHOS { 46 namespace Bluetooth { 47 enum BtSocketType { 48 TYPE_RFCOMM = 0x0, 49 TYPE_L2CAP = 0x01, 50 TYPE_L2CAP_LE = 0x02, 51 }; 52 53 enum SocketState { 54 SOCKET_INIT, 55 SOCKET_CONNECTED, 56 SOCKET_LISTENING, 57 SOCKET_CLOSED, 58 }; 59 60 const int FLAG_ENCRYPT = 1; 61 const int FLAG_AUTH = 1 << 1; 62 63 const int SPP_SOCKET_PSM_VALUE = -1; 64 65 /** 66 * @brief Represents bluetooth connection callbcak. 67 */ 68 class BluetoothConnectionObserver { 69 public: 70 /** 71 * @brief delete the BluetoothConnectionObserver instance. 72 */ 73 virtual ~BluetoothConnectionObserver() = default; 74 75 /** 76 * @brief notify connection status and result. 77 */ 78 virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &dev, UUID uuid, int status, int result) = 0; 79 }; 80 81 /** 82 * @brief Class for client socket functions. 83 * 84 * @since 6 85 */ 86 class BLUETOOTH_API ClientSocket : public std::enable_shared_from_this<ClientSocket> { 87 public: 88 /** 89 * @brief init socketClient. 90 * 91 * @return init api init result. 92 * @since 6 93 * 94 */ 95 bool Init(); 96 97 /** 98 * @brief A constructor used to create an ClientSocket instance. 99 * 100 * @param bda Remote device object. 101 * @param uuid Uuid. 102 * @param type Socket type. 103 * @param auth Connection state. 104 * @since 6 105 */ 106 ClientSocket(const BluetoothRemoteDevice &bda, UUID uuid, BtSocketType type, bool auth); 107 108 /** 109 * @brief A constructor used to create an ClientSocket instance. This constructor to construct the 110 * ClientSocket object when the Accept function is called. 111 * 112 * @param fd Socket fd. 113 * @param address Remote bluetooth address. 114 * @param type Socket type. 115 * @since 6 116 */ 117 ClientSocket(int fd, std::string address, BtSocketType type); 118 119 /** 120 * @brief A constructor used to create an ClientSocket instance. 121 * 122 * @param bda Remote device object. 123 * @param uuid Uuid. 124 * @param type Socket type. 125 * @param auth Connection state. 126 * @param observer Connection callback. 127 * @since 6 128 */ 129 ClientSocket(const BluetoothRemoteDevice &bda, UUID uuid, BtSocketType type, bool auth, 130 std::weak_ptr<BluetoothConnectionObserver> observer); 131 132 /** 133 * @brief Destroy the ClientSocket object. 134 * 135 * @since 6 136 */ 137 virtual ~ClientSocket(); 138 139 /** 140 * @brief The function is used to connect to a remote device. 141 * 142 * @param psm dynamic PSM value from remote device. 143 * @return Returns <b>0</b> if the operation is successful. 144 * Returns <b>-1</b> if the operation fails. 145 * @since 6 146 */ 147 int Connect(int psm); 148 149 /** 150 * @brief Client disconnected. 151 * 152 * @since 6 153 */ 154 void Close(); 155 156 /** 157 * @brief Get the input stream with this socket. 158 * 159 * @return Returns the object of the InputStream class. 160 * @since 6 161 */ 162 InputStream &GetInputStream(); 163 164 /** 165 * @brief Get the output stream with this socket. 166 * 167 * @return Returns the object of the OutputStream class. 168 * @since 6 169 */ 170 OutputStream &GetOutputStream(); 171 172 /** 173 * @brief Get the remote device with this socket. 174 * 175 * @return Remote device. 176 * @since 6 177 */ 178 BluetoothRemoteDevice &GetRemoteDevice(); 179 180 /** 181 * @brief Get the connection status of this socket. 182 * 183 * @return Returns <b>true</b> is connected. 184 * Returns <b>false</b> is not connected. 185 * @since 6 186 */ 187 bool IsConnected() const; 188 189 /** 190 * @brief Set socket send & recv buffer size, The size limit ranges from 4KB to 50KB. 191 * 192 * @return the operation status 193 * @since 6 194 */ 195 int SetBufferSize(int bufferSize); 196 197 /** 198 * @brief update coc connection params 199 * 200 * @param CocUpdateSocketParam coc socket params. 201 * @return Returns <b>0</b> if the operation is successful. 202 * Returns <b>-1</b> if the operation fails. 203 * @since 6 204 */ 205 int UpdateCocConnectionParams(CocUpdateSocketParam ¶m); 206 207 /** 208 * @brief Get client socket fd 209 * 210 * @return int fd 211 * @since 6 212 */ 213 int GetSocketFd(); 214 215 /** 216 * @brief Get dynamic PSM value for TYPE_L2CAP_LE. 217 * 218 * @return int psm. 219 * @since 6 220 */ 221 int GetL2capPsm(); 222 223 /** 224 * @brief Get client channel number for TYPE_RFCOMM. 225 * 226 * @return int scn. 227 * @since 6 228 */ 229 230 int GetRfcommScn(); 231 232 /** 233 * @brief Get the maximum supported transmit packet size for the underlying transport 234 * 235 * @return int the maximum supported transmit packet size 236 * @since 6 237 */ 238 uint32_t GetMaxTransmitPacketSize(); 239 240 /** 241 * @brief Get the maximum supported receive packet size for the underlying transport 242 * 243 * @return int the maximum supported receive packet size 244 * @since 6 245 */ 246 uint32_t GetMaxReceivePacketSize(); 247 248 private: 249 ClientSocket() = delete; 250 BLUETOOTH_DECLARE_IMPL(); 251 }; 252 253 /** 254 * @brief Class for server socket functions. 255 * 256 * @since 6 257 */ 258 class BLUETOOTH_API ServerSocket { 259 public: 260 /** 261 * @brief A constructor used to create an ServerSocket instance. 262 * 263 * @param name Server name. 264 * @param uuid Uuid. 265 * @param type Socket type. 266 * @param encrypt Remote device auth and encrypt connection. 267 * @since 6 268 */ 269 ServerSocket(const std::string &name, UUID uuid, BtSocketType type, bool encrypt); 270 271 /** 272 * @brief Destroy the ServerSocket object. 273 * 274 * @since 6 275 */ 276 ~ServerSocket(); 277 278 /** 279 * @brief Listen the client connect event. 280 * 281 * @return listen error code. 282 * @since 6 283 */ 284 int Listen(); 285 286 /** 287 * @brief Accept a client connection and return an acceptClientSocket to interact with the client. 288 * 289 * @param timeout Timeout for the accept. 290 * @return A ClientSocket. 291 * @since 6 292 */ 293 std::shared_ptr<ClientSocket> Accept(int timeout); 294 295 /** 296 * @brief Server disconnected. 297 * 298 * @since 6 299 */ 300 void Close(); 301 302 /** 303 * @brief Get the server socket type and server name. 304 * 305 * @return A string. 306 * @since 6 307 */ 308 const std::string &GetStringTag(); 309 310 /** 311 * @brief Get dynamic PSM value for TYPE_L2CAP. 312 * 313 * @return int psm. 314 * @since 6 315 */ 316 int GetL2capPsm(); 317 318 /** 319 * @brief Get server channel number for TYPE_RFCOMM. 320 * 321 * @return int scn. 322 * @since 6 323 */ 324 325 int GetRfcommScn(); 326 327 /** 328 * @brief Get the maximum supported transmit packet size for the underlying transport 329 * 330 * @return int the maximum supported transmit packet size 331 * @since 6 332 */ 333 uint32_t GetMaxTransmitPacketSize(); 334 335 /** 336 * @brief Get the maximum supported receive packet size for the underlying transport 337 * 338 * @return int the maximum supported receive packet size 339 * @since 6 340 */ 341 uint32_t GetMaxReceivePacketSize(); 342 343 /** 344 * @brief Get server socket fd 345 * 346 * @return int fd. 347 * @since 6 348 */ 349 int GetSocketFd(); 350 351 private: 352 BLUETOOTH_DECLARE_IMPL(); 353 }; 354 355 class BLUETOOTH_API SocketFactory { 356 public: 357 /** 358 * @brief Create a server record to listen to the insecure rfcomm. 359 * 360 * @param name Server name. 361 * @param uuid Uuid. 362 * @return A ServerSocket. 363 * @since 6 364 */ 365 static std::shared_ptr<ServerSocket> DataListenInsecureRfcommByServiceRecord( 366 const std::string &name, const UUID &uuid); 367 368 /** 369 * @brief Create a server record to listen to the rfcomm. 370 * 371 * @param name Server name. 372 * @param uuid Uuid. 373 * @return A ServerSocket. 374 * @since 6 375 */ 376 static std::shared_ptr<ServerSocket> DataListenRfcommByServiceRecord(const std::string &name, const UUID &uuid); 377 378 /** 379 * @brief Build insecure rfcomm data socket by service record. 380 * 381 * @param device Remote device object. 382 * @param uuid Uuid. 383 * @return A ClientSocket. 384 * @since 6 385 */ 386 static std::shared_ptr<ClientSocket> BuildInsecureRfcommDataSocketByServiceRecord( 387 const BluetoothRemoteDevice &device, const UUID &uuid); 388 389 /** 390 * @brief Build rfcomm data socket by service record. 391 * 392 * @param device Remote device object. 393 * @param uuid Uuid. 394 * @return A ClientSocket. 395 * @since 6 396 */ 397 static std::shared_ptr<ClientSocket> BuildRfcommDataSocketByServiceRecord( 398 const BluetoothRemoteDevice &device, const UUID &uuid); 399 }; 400 } // namespace Bluetooth 401 } // namespace OHOS 402 #endif // BLUETOOTH_SOCKET_H