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::shared_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 /** 258 * @brief Check if socket connect is allowed 259 * 260 * @param socketType socketType. 261 * @return Returns <b>true</b> allowed. 262 * Returns <b>false</b> not allowed. 263 * @since 6 264 */ 265 bool IsAllowSocketConnect(int socketType); 266 267 private: 268 ClientSocket() = delete; 269 BLUETOOTH_DECLARE_IMPL(); 270 }; 271 272 /** 273 * @brief Class for server socket functions. 274 * 275 * @since 6 276 */ 277 class BLUETOOTH_API ServerSocket { 278 public: 279 /** 280 * @brief A constructor used to create an ServerSocket instance. 281 * 282 * @param name Server name. 283 * @param uuid Uuid. 284 * @param type Socket type. 285 * @param encrypt Remote device auth and encrypt connection. 286 * @since 6 287 */ 288 ServerSocket(const std::string &name, UUID uuid, BtSocketType type, bool encrypt); 289 290 /** 291 * @brief Destroy the ServerSocket object. 292 * 293 * @since 6 294 */ 295 ~ServerSocket(); 296 297 /** 298 * @brief Listen the client connect event. 299 * 300 * @return listen error code. 301 * @since 6 302 */ 303 int Listen(); 304 305 /** 306 * @brief Accept a client connection and return an acceptClientSocket to interact with the client. 307 * 308 * @param timeout Timeout for the accept. 309 * @return A ClientSocket. 310 * @since 6 311 */ 312 std::shared_ptr<ClientSocket> Accept(int timeout); 313 314 /** 315 * @brief Server disconnected. 316 * 317 * @since 6 318 */ 319 void Close(); 320 321 /** 322 * @brief Get the server socket type and server name. 323 * 324 * @return A string. 325 * @since 6 326 */ 327 const std::string &GetStringTag(); 328 329 /** 330 * @brief Get dynamic PSM value for TYPE_L2CAP. 331 * 332 * @return int psm. 333 * @since 6 334 */ 335 int GetL2capPsm(); 336 337 /** 338 * @brief Get server channel number for TYPE_RFCOMM. 339 * 340 * @return int scn. 341 * @since 6 342 */ 343 344 int GetRfcommScn(); 345 346 /** 347 * @brief Get the maximum supported transmit packet size for the underlying transport 348 * 349 * @return int the maximum supported transmit packet size 350 * @since 6 351 */ 352 uint32_t GetMaxTransmitPacketSize(); 353 354 /** 355 * @brief Get the maximum supported receive packet size for the underlying transport 356 * 357 * @return int the maximum supported receive packet size 358 * @since 6 359 */ 360 uint32_t GetMaxReceivePacketSize(); 361 362 /** 363 * @brief Get server socket fd 364 * 365 * @return int fd. 366 * @since 6 367 */ 368 int GetSocketFd(); 369 370 private: 371 BLUETOOTH_DECLARE_IMPL(); 372 }; 373 374 class BLUETOOTH_API SocketFactory { 375 public: 376 /** 377 * @brief Create a server record to listen to the insecure rfcomm. 378 * 379 * @param name Server name. 380 * @param uuid Uuid. 381 * @return A ServerSocket. 382 * @since 6 383 */ 384 static std::shared_ptr<ServerSocket> DataListenInsecureRfcommByServiceRecord( 385 const std::string &name, const UUID &uuid); 386 387 /** 388 * @brief Create a server record to listen to the rfcomm. 389 * 390 * @param name Server name. 391 * @param uuid Uuid. 392 * @return A ServerSocket. 393 * @since 6 394 */ 395 static std::shared_ptr<ServerSocket> DataListenRfcommByServiceRecord(const std::string &name, const UUID &uuid); 396 397 /** 398 * @brief Build insecure rfcomm data socket by service record. 399 * 400 * @param device Remote device object. 401 * @param uuid Uuid. 402 * @return A ClientSocket. 403 * @since 6 404 */ 405 static std::shared_ptr<ClientSocket> BuildInsecureRfcommDataSocketByServiceRecord( 406 const BluetoothRemoteDevice &device, const UUID &uuid); 407 408 /** 409 * @brief Build rfcomm data socket by service record. 410 * 411 * @param device Remote device object. 412 * @param uuid Uuid. 413 * @return A ClientSocket. 414 * @since 6 415 */ 416 static std::shared_ptr<ClientSocket> BuildRfcommDataSocketByServiceRecord( 417 const BluetoothRemoteDevice &device, const UUID &uuid); 418 }; 419 } // namespace Bluetooth 420 } // namespace OHOS 421 #endif // BLUETOOTH_SOCKET_H