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