• 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 /**
66  * @brief Class for client socket functions.
67  *
68  * @since 6
69  */
70 class BLUETOOTH_API ClientSocket {
71 public:
72     /**
73      * @brief A constructor used to create an ClientSocket instance.
74      *
75      * @param bda Remote device object.
76      * @param uuid Uuid.
77      * @param type Socket type.
78      * @param auth Connection state.
79      * @since 6
80      */
81     ClientSocket(const BluetoothRemoteDevice &bda, UUID uuid, BtSocketType type, bool auth);
82 
83     /**
84      * @brief A constructor used to create an ClientSocket instance. This constructor to construct the
85      * ClientSocket object when the Accept function is called.
86      *
87      * @param fd Socket fd.
88      * @param address Remote bluetooth address.
89      * @param type Socket type.
90      * @since 6
91      */
92     ClientSocket(int fd, std::string address, BtSocketType type);
93 
94     /**
95      * @brief Destroy the ClientSocket object.
96      *
97      * @since 6
98      */
99     virtual ~ClientSocket();
100 
101     /**
102      * @brief The function is used to connect to a remote device.
103      *
104      * @param psm dynamic PSM value from remote device.
105      * @return Returns <b>0</b> if the operation is successful.
106      *         Returns <b>-1</b> if the operation fails.
107      * @since 6
108      */
109     int Connect(int psm);
110 
111     /**
112      * @brief Client disconnected.
113      *
114      * @since 6
115      */
116     void Close();
117 
118     /**
119      * @brief Get the input stream with this socket.
120      *
121      * @return Returns the object of the InputStream class.
122      * @since 6
123      */
124     InputStream &GetInputStream();
125 
126     /**
127      * @brief Get the output stream with this socket.
128      *
129      * @return Returns the object of the OutputStream class.
130      * @since 6
131      */
132     OutputStream &GetOutputStream();
133 
134     /**
135      * @brief Get the remote device with this socket.
136      *
137      * @return Remote device.
138      * @since 6
139      */
140     BluetoothRemoteDevice &GetRemoteDevice();
141 
142     /**
143      * @brief Get the connection status of this socket.
144      *
145      * @return Returns <b>true</b> is connected.
146      *         Returns <b>false</b> is not connected.
147      * @since 6
148      */
149     bool IsConnected() const;
150 
151     /**
152      * @brief Set socket send & recv buffer size, The size limit ranges from 4KB to 50KB.
153      *
154      * @return the operation status
155      * @since 6
156      */
157     int SetBufferSize(int bufferSize);
158 
159 private:
160     ClientSocket() = delete;
161     BLUETOOTH_DECLARE_IMPL();
162 };
163 
164 /**
165  * @brief Class for server socket functions.
166  *
167  * @since 6
168  */
169 class BLUETOOTH_API ServerSocket {
170 public:
171     /**
172      * @brief A constructor used to create an ServerSocket instance.
173      *
174      * @param name Server name.
175      * @param uuid Uuid.
176      * @param type Socket type.
177      * @param encrypt Remote device auth and encrypt connection.
178      * @since 6
179      */
180     ServerSocket(const std::string &name, UUID uuid, BtSocketType type, bool encrypt);
181 
182     /**
183      * @brief Destroy the ServerSocket object.
184      *
185      * @since 6
186      */
187     ~ServerSocket();
188 
189     /**
190      * @brief Listen the client connect event.
191      *
192      * @return listen error code.
193      * @since 6
194      */
195     int Listen();
196 
197     /**
198      * @brief Accept a client connection and return an acceptClientSocket to interact with the client.
199      *
200      * @param timeout Timeout for the accept.
201      * @return A ClientSocket.
202      * @since 6
203      */
204     std::shared_ptr<ClientSocket> Accept(int timeout);
205 
206     /**
207      * @brief Server disconnected.
208      *
209      * @since 6
210      */
211     void Close();
212 
213     /**
214      * @brief Get the server socket type and server name.
215      *
216      * @return A string.
217      * @since 6
218      */
219     const std::string &GetStringTag();
220 
221     /**
222      * @brief Get dynamic PSM value.
223      *
224      * @return int psm.
225      * @since 6
226      */
227     int GetPsm();
228 private:
229     BLUETOOTH_DECLARE_IMPL();
230 };
231 
232 class BLUETOOTH_API SocketFactory {
233 public:
234     /**
235      * @brief Create a server record to listen to the insecure rfcomm.
236      *
237      * @param name Server name.
238      * @param uuid Uuid.
239      * @return A ServerSocket.
240      * @since 6
241      */
242     static std::shared_ptr<ServerSocket> DataListenInsecureRfcommByServiceRecord(
243         const std::string &name, const UUID &uuid);
244 
245     /**
246      * @brief Create a server record to listen to the rfcomm.
247      *
248      * @param name Server name.
249      * @param uuid Uuid.
250      * @return A ServerSocket.
251      * @since 6
252      */
253     static std::shared_ptr<ServerSocket> DataListenRfcommByServiceRecord(const std::string &name, const UUID &uuid);
254 
255     /**
256      * @brief Build insecure rfcomm data socket by service record.
257      *
258      * @param device Remote device object.
259      * @param uuid Uuid.
260      * @return A ClientSocket.
261      * @since 6
262      */
263     static std::shared_ptr<ClientSocket> BuildInsecureRfcommDataSocketByServiceRecord(
264         const BluetoothRemoteDevice &device, const UUID &uuid);
265 
266     /**
267      * @brief Build rfcomm data socket by service record.
268      *
269      * @param device Remote device object.
270      * @param uuid Uuid.
271      * @return A ClientSocket.
272      * @since 6
273      */
274     static std::shared_ptr<ClientSocket> BuildRfcommDataSocketByServiceRecord(
275         const BluetoothRemoteDevice &device, const UUID &uuid);
276 };
277 } // namespace Bluetooth
278 } // namespace OHOS
279 #endif  // BLUETOOTH_SOCKET_H