• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #ifndef OHOS_BT_SOCKET_H
17 #define OHOS_BT_SOCKET_H
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include "ohos_bt_def.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define BT_SOCKET_READ_SOCKET_CLOSED (0)
28 #define BT_SOCKET_READ_FAILED (-1)
29 #define BT_SOCKET_WRITE_FAILED (-1)
30 #define BT_SOCKET_INVALID_ID (-1)
31 #define BT_SOCKET_INVALID_PSM (-1)
32 #define BT_SOCKET_INVALID_SCN (-1)
33 #define BT_SOCKET_INVALID_TYPE (-2)
34 #define BT_SOCKET_LIMITED_RESOURCES (-3)
35 #define BT_SOCEKET_CONNECT_FAILED (-4)
36 
37 typedef enum {
38     OHOS_SOCKET_SPP_RFCOMM = 0x0,
39     OHOS_SOCKET_L2CAP_LE = 0x02,
40 } BluetoothSocketType;
41 
42 typedef struct {
43     /** uuid This parameter is required when type is {@link OHOS_SOCKET_SPP_RFCOMM} */
44     BtUuid uuid;
45     /** type type of socket */
46     BluetoothSocketType socketType;
47     /** encrypt require the connection to be encrypted */
48     bool isEncrypt;
49 } BluetoothCreateSocketPara;
50 
51 typedef struct {
52     int minInterval;
53     int maxInterval;
54     int peripheralLatency;
55     int supervisionTimeout;
56     int minConnEventLen;
57     int maxConnEventLen;
58 } BluetoothCocUpdateSocketParam;
59 
60 /**
61  * @brief callback invoked when the socket connection state changed.
62  * @param bdAddr Indicates the ID of the GATT client.
63  * @param uuid This parameter is required when type is {@link OHOS_SOCKET_SPP_RFCOMM}.
64  * @param status Indicates the connection status {@link ConnStatus}.
65  * @param result Indicates the operation result.
66  */
67 typedef void (*SocketConnectionStateChangedCallback)(const BdAddr *bdAddr, BtUuid uuid, int status, int result);
68 typedef void (*SocketBleConnectionStateChangedCallback)(const BdAddr *bdAddr, int psm, int status, int result);
69 typedef struct {
70     SocketConnectionStateChangedCallback connStateCb;
71     SocketBleConnectionStateChangedCallback bleConnStateCb;
72 } BtSocketConnectionCallback;
73 
74 /**
75  * @brief Creates an server listening socket based on the service record.
76  *
77  * @param socketPara The parameters to create a server socket.
78  * @param name The service's name.
79  * @return Returns a server ID, if create fail return {@link BT_SOCKET_INVALID_ID}.
80  */
81 int SocketServerCreate(const BluetoothCreateSocketPara *socketPara, const char *name);
82 
83 /**
84  * @brief Waits for a remote device to connect to this server socket.
85  *
86  * This method return a client ID indicates a client socket
87  * can be used to read data from and write data to remote device.
88  * This method will block until a connection is established.
89  *
90  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
91  * {@link SocketServerCreate}.
92  * @return Returns a client ID, if accept fail return {@link BT_SOCKET_INVALID_ID}.
93  */
94 int SocketServerAccept(int serverId);
95 
96 /**
97  * @brief Disables an socket server socket and releases related resources.
98  *
99  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
100  * {@link SocketServerCreate}.
101  * @return Returns the operation result status {@link BtStatus}.
102  */
103 int SocketServerClose(int serverId);
104 
105 /**
106  * @brief Set fast connection flag
107  *
108  * @param bdAddr The remote device address to connect.
109  * @return Returns the operation result status {@link BtStatus}.
110  */
111 int SocketSetFastConnection(const BdAddr *bdAddr);
112 
113 /**
114  * @brief Connects to a remote device over the socket.
115  * This method will block until a connection is made or the connection fails.
116  *
117  * @param socketPara The param to create a client socket and connect to a remote device.
118  * @param bdAddr The remote device address to connect.
119  * @param psm BluetoothSocketType is {@link OHOS_SOCKET_L2CAP_LE} use dynamic PSM value from remote device.
120  * BluetoothSocketType is {@link OHOS_SOCKET_SPP_RFCOMM} use -1.
121  * @return Returns a client ID, if connect fail return {@link BT_SOCKET_INVALID_ID}.
122  */
123 int SocketConnect(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdAddr, int psm);
124 
125 /**
126  * @brief Connects to a remote device over the socket.
127  * This method will block until a connection is made or the connection fails.
128  *
129  * @param socketPara The param to create a client socket and connect to a remote device.
130  * @param bdAddr The remote device address to connect.
131  * @param psm BluetoothSocketType is {@link OHOS_SOCKET_L2CAP_LE} use dynamic PSM value from remote device.
132  * BluetoothSocketType is {@link OHOS_SOCKET_SPP_RFCOMM} use -1.
133  * @param callback Reference to the socket state observer.
134  * @return Returns a client ID, if connect fail return {@link BT_SOCKET_INVALID_ID}.
135  */
136 int SocketConnectEx(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdAddr, int psm,
137     BtSocketConnectionCallback *callback);
138 
139 /**
140  * @brief Disables a connection and releases related resources.
141  *
142  * @param clientId The relative ID used to identify the current client socket.
143  * @return Returns the operation result status {@link BtStatus}.
144  */
145 int SocketDisconnect(int clientId);
146 
147 /**
148  * @brief Get the connection status of this socket.
149  *
150  * @param clientId The relative ID used to identify the current client socket.
151  * @return Returns true is connected or false is not connected.
152  */
153 bool IsSocketConnected(int clientId);
154 
155 /**
156  * @brief Socket get remote device's address.
157  *
158  * @param clientId The relative ID used to identify the current client socket.
159  * @param remoteAddr Remote device's address, memory allocated by caller.
160  * @return Returns the operation result status {@link BtStatus}.
161  */
162 int SocketGetRemoteAddr(int clientId, BdAddr *remoteAddr);
163 
164 /**
165  * @brief Read data from socket.
166  * This method blocks until input data is available.
167  *
168  * @param clientId The relative ID used to identify the current client socket.
169  * @param buf Indicate the buffer which read in, memory allocated by caller.
170  * @param bufLen Indicate the buffer length.
171  * @return Returns the length greater than 0 as read the actual length.
172  * Returns {@link BT_SOCKET_READ_SOCKET_CLOSED} if the socket is closed.
173  * Returns {@link BT_SOCKET_READ_FAILED} if the operation failed.
174  */
175 int SocketRead(int clientId, uint8_t *buf, uint32_t bufLen);
176 
177 /**
178  * @brief Client write data to socket.
179  *
180  * @param clientId The relative ID used to identify the current client socket.
181  * @param data Indicate the data to be written.
182  * @param len Indicates the length of the data to be written.
183  * @return Returns the actual write length.
184  * Returns {@link BT_SOCKET_WRITE_FAILED} if the operation failed.
185  */
186 int SocketWrite(int clientId, const uint8_t *data, uint32_t len);
187 
188 /**
189  * @brief Get dynamic PSM value for OHOS_SOCKET_L2CAP.
190  *
191  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
192  * {@link SocketServerCreate}.
193  * @return Returns the PSM value.
194  * Returns {@link BT_SOCKET_INVALID_PSM} if the operation failed.
195  */
196 int SocketGetPsm(int serverId);
197 
198 /**
199  * @brief Get server scm number for OHOS_SOCKET_RFCOMM.
200  *
201  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
202  * {@link SocketServerCreate}.
203  * @return Returns the scn number.
204  * Returns {@link BT_SOCKET_INVALID_SCN} if the operation failed.
205  */
206 int SocketGetScn(int serverId);
207 
208 /**
209  * @brief Adjust the socket send and recv buffer size, limit range is 4KB to 50KB
210  *
211  * @param clientId The relative ID used to identify the current client socket.
212  * @param bufferSize The buffer size want to set, unit is byte.
213  * @return  Returns the operation result status {@link BtStatus}.
214  */
215 int SetSocketBufferSize(int clientId, uint32_t bufferSize);
216 
217 /**
218  * @brief Update the coc connection params.
219  *
220  * @param param CocUpdateSocketParam instance for carry params.
221  * @return Returns the operation result status {@link BtStatus}.
222  */
223 int SocketUpdateCocConnectionParams(BluetoothCocUpdateSocketParam* param, const BdAddr *bdAddr);
224 
225 /**
226  * @brief Obtain the device random address based on the device real address and tokenId.
227  *
228  * @param realAddr The remote device real address.
229  * @param randomAddr The remote device random address.
230  * @param tokenId The relative ID used to identify the current client socket.
231  * @return Returns the operation result status {@link BtStatus}.
232  */
233 int GetRandomAddress(const BdAddr *realAddr, BdAddr *randomAddr, uint64_t tokenId);
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 #endif