• 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 
33 typedef enum {
34     OHOS_SOCKET_SPP_RFCOMM = 0x0,
35     OHOS_SOCKET_L2CAP_LE = 0x02,
36 } BluetoothSocketType;
37 
38 typedef struct {
39     /** uuid This parameter is required when type is {@link OHOS_SOCKET_SPP_RFCOMM} */
40     BtUuid uuid;
41     /** type type of socket */
42     BluetoothSocketType socketType;
43     /** encrypt require the connection to be encrypted */
44     bool isEncrypt;
45 } BluetoothCreateSocketPara;
46 
47 /**
48  * @brief Creates an server listening socket based on the service record.
49  *
50  * @param socketPara The parameters to create a server socket.
51  * @param name The service's name.
52  * @return Returns a server ID, if create fail return {@link BT_SOCKET_INVALID_ID}.
53  */
54 int SocketServerCreate(const BluetoothCreateSocketPara *socketPara, const char *name);
55 
56 /**
57  * @brief Waits for a remote device to connect to this server socket.
58  *
59  * This method return a client ID indicates a client socket
60  * can be used to read data from and write data to remote device.
61  * This method will block until a connection is established.
62  *
63  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
64  * {@link SocketServerCreate}.
65  * @return Returns a client ID, if accept fail return {@link BT_SOCKET_INVALID_ID}.
66  */
67 int SocketServerAccept(int serverId);
68 
69 /**
70  * @brief Disables an socket server socket and releases related resources.
71  *
72  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
73  * {@link SocketServerCreate}.
74  * @return Returns the operation result status {@link BtStatus}.
75  */
76 int SocketServerClose(int serverId);
77 
78 /**
79  * @brief Connects to a remote device over the socket.
80  * This method will block until a connection is made or the connection fails.
81  *
82  * @param socketPara The param to create a client socket and connect to a remote device.
83  * @param bdAddr The remote device address to connect.
84  * @param psm BluetoothSocketType is {@link OHOS_SOCKET_L2CAP_LE} use dynamic PSM value from remote device.
85  * BluetoothSocketType is {@link OHOS_SOCKET_SPP_RFCOMM} use -1.
86  * @return Returns a client ID, if connect fail return {@link BT_SOCKET_INVALID_ID}.
87  */
88 int SocketConnect(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdAddr, int psm);
89 
90 /**
91  * @brief Disables a connection and releases related resources.
92  *
93  * @param clientId The relative ID used to identify the current client socket.
94  * @return Returns the operation result status {@link BtStatus}.
95  */
96 int SocketDisconnect(int clientId);
97 
98 /**
99  * @brief Get the connection status of this socket.
100  *
101  * @param clientId The relative ID used to identify the current client socket.
102  * @return Returns true is connected or false is not connected.
103  */
104 bool IsSocketConnected(int clientId);
105 
106 /**
107  * @brief Socket get remote device's address.
108  *
109  * @param clientId The relative ID used to identify the current client socket.
110  * @param remoteAddr Remote device's address, memory allocated by caller.
111  * @return Returns the operation result status {@link BtStatus}.
112  */
113 int SocketGetRemoteAddr(int clientId, BdAddr *remoteAddr);
114 
115 /**
116  * @brief Read data from socket.
117  * This method blocks until input data is available.
118  *
119  * @param clientId The relative ID used to identify the current client socket.
120  * @param buf Indicate the buffer which read in, memory allocated by caller.
121  * @param bufLen Indicate the buffer length.
122  * @return Returns the length greater than 0 as read the actual length.
123  * Returns {@link BT_SOCKET_READ_SOCKET_CLOSED} if the socket is closed.
124  * Returns {@link BT_SOCKET_READ_FAILED} if the operation failed.
125  */
126 int SocketRead(int clientId, uint8_t *buf, uint32_t bufLen);
127 
128 /**
129  * @brief Client write data to socket.
130  *
131  * @param clientId The relative ID used to identify the current client socket.
132  * @param data Indicate the data to be written.
133  * @param len Indicates the length of the data to be written.
134  * @return Returns the actual write length.
135  * Returns {@link BT_SOCKET_WRITE_FAILED} if the operation failed.
136  */
137 int SocketWrite(int clientId, const uint8_t *data, uint32_t len);
138 
139 /**
140  * @brief Get dynamic PSM value.
141  *
142  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
143  * {@link SocketServerCreate}.
144  * @return Returns the PSM value.
145  * Returns {@link BT_SOCKET_INVALID_PSM} if the operation failed.
146  */
147 int SocketGetPsm(int serverId);
148 
149 /**
150  * @brief Adjust the socket send and recv buffer size, limit range is 4KB to 50KB
151  *
152  * @param clientId The relative ID used to identify the current client socket.
153  * @param bufferSize The buffer size want to set, unit is byte.
154  * @return  Returns the operation result status {@link BtStatus}.
155  */
156 int SetSocketBufferSize(int clientId, uint32_t bufferSize);
157 
158 #ifdef __cplusplus
159 }
160 #endif
161 #endif