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