• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 &param);
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 A constructor used to create an ServerSocket instance.
292      *
293      * @param name Server name.
294      * @param uuid Uuid.
295      * @param type Socket type.
296      * @param encrypt Remote device auth and encrypt connection.
297      * @param psm L2CAP socket psm.
298      * @since 20
299      */
300     ServerSocket(const std::string &name, UUID uuid, BtSocketType type, bool encrypt, int psm);
301 
302     /**
303      * @brief Destroy the ServerSocket object.
304      *
305      * @since 6
306      */
307     ~ServerSocket();
308 
309     /**
310      * @brief Listen the client connect event.
311      *
312      * @return listen error code.
313      * @since 6
314      */
315     int Listen();
316 
317     /**
318      * @brief Accept a client connection and return an acceptClientSocket to interact with the client.
319      *
320      * @param timeout Timeout for the accept.
321      * @return A ClientSocket.
322      * @since 6
323      */
324     std::shared_ptr<ClientSocket> Accept(int timeout);
325 
326     /**
327      * @brief Server disconnected.
328      *
329      * @since 6
330      */
331     void Close();
332 
333     /**
334      * @brief Get the server socket type and server name.
335      *
336      * @return A string.
337      * @since 6
338      */
339     const std::string &GetStringTag();
340 
341     /**
342      * @brief Get dynamic PSM value for TYPE_L2CAP.
343      *
344      * @return int psm.
345      * @since 6
346      */
347     int GetL2capPsm();
348 
349     /**
350      * @brief Get server channel number for TYPE_RFCOMM.
351      *
352      * @return int scn.
353      * @since 6
354      */
355 
356     int GetRfcommScn();
357 
358     /**
359      * @brief Get the maximum supported transmit packet size for the underlying transport
360      *
361      * @return int the maximum supported transmit packet size
362      * @since 6
363      */
364     uint32_t GetMaxTransmitPacketSize();
365 
366     /**
367      * @brief Get the maximum supported receive packet size for the underlying transport
368      *
369      * @return int the maximum supported receive packet size
370      * @since 6
371      */
372     uint32_t GetMaxReceivePacketSize();
373 
374     /**
375      * @brief Get server socket fd
376      *
377      * @return int fd.
378      * @since 6
379      */
380     int GetSocketFd();
381 
382 private:
383     BLUETOOTH_DECLARE_IMPL();
384 };
385 
386 class BLUETOOTH_API SocketFactory {
387 public:
388     /**
389      * @brief Create a server record to listen to the insecure rfcomm.
390      *
391      * @param name Server name.
392      * @param uuid Uuid.
393      * @return A ServerSocket.
394      * @since 6
395      */
396     static std::shared_ptr<ServerSocket> DataListenInsecureRfcommByServiceRecord(
397         const std::string &name, const UUID &uuid);
398 
399     /**
400      * @brief Create a server record to listen to the rfcomm.
401      *
402      * @param name Server name.
403      * @param uuid Uuid.
404      * @return A ServerSocket.
405      * @since 6
406      */
407     static std::shared_ptr<ServerSocket> DataListenRfcommByServiceRecord(const std::string &name, const UUID &uuid);
408 
409     /**
410      * @brief Build insecure rfcomm data socket by service record.
411      *
412      * @param device Remote device object.
413      * @param uuid Uuid.
414      * @return A ClientSocket.
415      * @since 6
416      */
417     static std::shared_ptr<ClientSocket> BuildInsecureRfcommDataSocketByServiceRecord(
418         const BluetoothRemoteDevice &device, const UUID &uuid);
419 
420     /**
421      * @brief Build rfcomm data socket by service record.
422      *
423      * @param device Remote device object.
424      * @param uuid Uuid.
425      * @return A ClientSocket.
426      * @since 6
427      */
428     static std::shared_ptr<ClientSocket> BuildRfcommDataSocketByServiceRecord(
429         const BluetoothRemoteDevice &device, const UUID &uuid);
430 };
431 } // namespace Bluetooth
432 } // namespace OHOS
433 #endif  // BLUETOOTH_SOCKET_H