• 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 SppSocketType {
48     TYPE_RFCOMM,
49     TYPE_L2CAP,
50 };
51 
52 enum SppSocketState {
53     SOCKET_INIT,
54     SOCKET_CONNECTED,
55     SOCKET_LISTENING,
56     SOCKET_CLOSED,
57 };
58 
59 const int FLAG_ENCRYPT = 1;
60 const int FLAG_AUTH = 1 << 1;
61 
62 /**
63  * @brief Class for spp client socket functions.
64  *
65  * @since 6
66  */
67 class BLUETOOTH_API SppClientSocket {
68 public:
69     /**
70      * @brief A constructor used to create an SppClientSocket instance.
71      *
72      * @param bda Remote device object.
73      * @param uuid Uuid.
74      * @param type Socket type.
75      * @param auth Connection state.
76      * @since 6
77      */
78     SppClientSocket(const BluetoothRemoteDevice &bda, UUID uuid, SppSocketType type, bool auth);
79 
80     /**
81      * @brief A constructor used to create an SppClientSocket instance. This constructor to construct the
82      * SppClientSocket object when the Accept function is called.
83      *
84      * @param fd Socket fd.
85      * @param address Remote bluetooth address.
86      * @since 6
87      */
88     SppClientSocket(int fd, std::string address);
89 
90     /**
91      * @brief Destroy the SppClientSocket object.
92      *
93      * @since 6
94      */
95     virtual ~SppClientSocket();
96 
97     /**
98      * @brief The function is used to connect to a remote device.
99      *
100      * @return Returns <b>0</b> if the operation is successful.
101      *         Returns <b>-1</b> if the operation fails.
102      * @since 6
103      */
104     int Connect();
105 
106     /**
107      * @brief Client disconnected.
108      *
109      * @since 6
110      */
111     void Close();
112 
113     /**
114      * @brief Get the input stream with this socket.
115      *
116      * @return Returns the object of the InputStream class.
117      * @since 6
118      */
119     InputStream &GetInputStream();
120 
121     /**
122      * @brief Get the output stream with this socket.
123      *
124      * @return Returns the object of the OutputStream class.
125      * @since 6
126      */
127     OutputStream &GetOutputStream();
128 
129     /**
130      * @brief Get the remote device with this socket.
131      *
132      * @return Remote device.
133      * @since 6
134      */
135     BluetoothRemoteDevice &GetRemoteDevice();
136 
137     /**
138      * @brief Get the connection status of this socket.
139      *
140      * @return Returns <b>true</b> is connected.
141      *         Returns <b>false</b> is not connected.
142      * @since 6
143      */
144     bool IsConnected() const;
145 
146 private:
147     SppClientSocket() = delete;
148     BLUETOOTH_DECLARE_IMPL();
149 };
150 
151 /**
152  * @brief Class for spp server socket functions.
153  *
154  * @since 6
155  */
156 class BLUETOOTH_API SppServerSocket {
157 public:
158     /**
159      * @brief A constructor used to create an SppServerSocket instance.
160      *
161      * @param name Server name.
162      * @param uuid Uuid.
163      * @param type Socket type.
164      * @param encrypt Remote device auth and encrypt connection.
165      * @since 6
166      */
167     SppServerSocket(const std::string &name, UUID uuid, SppSocketType type, bool encrypt);
168 
169     /**
170      * @brief Destroy the SppServerSocket object.
171      *
172      * @since 6
173      */
174     ~SppServerSocket();
175 
176     /**
177      * @brief Accept a client connection and return an acceptClientSocket to interact with the client.
178      *
179      * @param timeout Timeout for the accept.
180      * @return A SppClientSocket.
181      * @since 6
182      */
183     std::unique_ptr<SppClientSocket> Accept(int timeout);
184 
185     /**
186      * @brief Server disconnected.
187      *
188      * @since 6
189      */
190     void Close();
191 
192     /**
193      * @brief Get the server socket type and server name.
194      *
195      * @return A string.
196      * @since 6
197      */
198     const std::string &GetStringTag();
199 
200 private:
201     BLUETOOTH_DECLARE_IMPL();
202 };
203 
204 class BLUETOOTH_API SocketFactory {
205 public:
206     /**
207      * @brief Create a server record to listen to the insecure rfcomm.
208      *
209      * @param name Server name.
210      * @param uuid Uuid.
211      * @return A SppServerSocket.
212      * @since 6
213      */
214     static SppServerSocket *DataListenInsecureRfcommByServiceRecord(const std::string &name, const UUID &uuid);
215 
216     /**
217      * @brief Create a server record to listen to the rfcomm.
218      *
219      * @param name Server name.
220      * @param uuid Uuid.
221      * @return A SppServerSocket.
222      * @since 6
223      */
224     static SppServerSocket *DataListenRfcommByServiceRecord(const std::string &name, const UUID &uuid);
225 
226     /**
227      * @brief Build insecure rfcomm data socket by service record.
228      *
229      * @param device Remote device object.
230      * @param uuid Uuid.
231      * @return A SppClientSocket.
232      * @since 6
233      */
234     static SppClientSocket *BuildInsecureRfcommDataSocketByServiceRecord(
235         const BluetoothRemoteDevice &device, const UUID &uuid);
236 
237     /**
238      * @brief Build rfcomm data socket by service record.
239      *
240      * @param device Remote device object.
241      * @param uuid Uuid.
242      * @return A SppClientSocket.
243      * @since 6
244      */
245     static SppClientSocket *BuildRfcommDataSocketByServiceRecord(const BluetoothRemoteDevice &device, const UUID &uuid);
246 };
247 } // namespace Bluetooth
248 } // namespace OHOS
249 #endif  // BLUETOOTH_SOCKET_H
250