• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 CONNECTION_H
17 #define CONNECTION_H
18 
19 #include "softbus_common.h"
20 #include <stdbool.h>
21 #include <stdint.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef enum {
28     CONNECTION_STATE_CONNECTED_SUCCESS = 0,
29     CONNECTION_STATE_CONNECTED_FAILED,
30     CONNECTION_STATE_DISCONNECTED,
31 } GeneralConnectionState;
32 
33 /**
34  * @brief Defines connection callbacks.
35  *
36  * When accepts a connection or the connection state is changed or data is received or the softbus_server process died,
37  * the related callback is invoked.
38  *
39  * @since 2.0
40  * @version 2.0
41  */
42 typedef struct {
43     /**
44      * @brief Called when server side accept a connection request.
45      *
46      * This callback is invoked when the server side accept a connection request.
47      * The server side refers to the side that called {@GeneralCreateServer} function.
48      *
49      * @param name Indicates the session name of the connection.
50      * @param handle Indicates the handle of the connection.
51      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
52      * @since 2.0
53      * @version 2.0
54      */
55     int32_t (*OnAcceptConnect)(const char *name, uint32_t handle);
56 
57     /**
58      * @brief Called when a connection state is changed.
59      *
60      * @param handle Indicates the handle of the connection.
61      * @param state Indicates the new state of the connection.
62      * @param reason Indicates the reason of the connection state change.
63 
64      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
65      * @since 2.0
66      * @version 2.0
67      */
68     int32_t (*OnConnectionStateChange)(uint32_t handle, int32_t state, int32_t reason);
69 
70     /**
71      * @brief Called when data is received.
72      *
73      * This callback is invoked when data is received.
74      *
75      * @param handle Indicates the handle of the connection.
76      * @param data Indicates the data received.
77      * @param len Indicates the length of the data received.
78      * @since 2.0
79      * @version 2.0
80      */
81     void (*OnDataReceived)(uint32_t handle, const uint8_t *data, uint32_t len);
82 
83     /**
84      * @brief Called when server side softbus_server process died.
85      *
86      * This callback is invoked when the server side softbus_server process died.
87      * @since 2.0
88      * @version 2.0
89      */
90     void (*OnServiceDied)(void);
91 } IGeneralListener;
92 
93 /**
94  * @brief Registers a connection listener.
95  *
96  * @param listener Indicates the pointer to the connection callback.
97  *
98  * @return Returns <b>SOFTBUS_OK</b> if the listen creation is successful;
99  * returns an error code less than zero otherwise.
100  * @since 2.0
101  * @version 2.0
102  */
103 int32_t GeneralRegisterListener(IGeneralListener *listener);
104 
105 /**
106  * @brief Unregisters a connection listener.
107  *
108  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful;
109  * returns an error code less than zero otherwise.
110  * @since 2.0
111  * @version 2.0
112  */
113 int32_t GeneralUnregisterListener();
114 
115 /**
116  * @brief Creates a server.
117  *
118  * @param pkgName Indicates the package name of the server.
119  * @param name Indicates the session name of the server.
120  *
121  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful;
122  * returns a non-zero value otherwise.
123  * @since 2.0
124  * @version 2.0
125  */
126 int32_t GeneralCreateServer(const char *pkgName, const char *name);
127 
128 /**
129  * @brief Removes a server.
130  *
131  * @param pkgName Indicates the package name of the server.
132  * @param name Indicates the session name of the server.
133  *
134  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful;
135  * returns a non-zero value otherwise.
136  * @since 2.0
137  * @version 2.0
138  */
139 int32_t GeneralRemoveServer(const char *pkgName, const char *name);
140 
141 /**
142  * @brief Defines the address of a remote device.
143  *
144  * @since 2.0
145  * @version 2.0
146  */
147 typedef struct {
148     ConnectionAddrType addrType; /**< Address type */
149     union {
150         struct BleAddress {
151             char mac[BT_MAC_LEN]; /**< MAC address */
152         } ble;
153     } addr;
154 } Address;
155 
156 /**
157  * @brief Connects to a remote device.
158  *
159  * @param pkgName Indicates the package name of the client.
160  * @param name Indicates the session name of the client.
161  * @param address Indicates the address of the remote device.
162  *
163  * @return Returns handle of the connection if the operation is successful;
164  * returns a non-zero value otherwise.
165  * @since 2.0
166  * @version 2.0
167  */
168 int32_t GeneralConnect(const char *pkgName, const char *name, const Address *address);
169 
170 /**
171  * @brief Disconnects a connection.
172  *
173  * @param handle Indicates the handle of the connection.
174  *
175  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful;
176  * returns a non-zero value otherwise.
177  * @since 2.0
178  * @version 2.0
179  */
180 int32_t GeneralDisconnect(uint32_t handle);
181 
182 /**
183  * @brief Sends data to a remote device.
184  *
185  * @param handle Indicates the handle of the connection.
186  * @param data Indicates the data to be sent.
187  * @param len Indicates the length of the data to be sent.
188  *
189  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful;
190  * returns a non-zero value otherwise.
191  * @since 2.0
192  * @version 2.0
193  */
194 int32_t GeneralSend(uint32_t handle, const uint8_t *data, uint32_t len);
195 
196 /**
197  * @brief Gets the peer device id of a connection.
198  *
199  * @param handle Indicates the handle of the connection.
200  * @param deviceId Indicates the pointer to the buffer to store the peer device id.
201  * @param len Indicates the length of the buffer to store the peer device id.
202  *
203  * @return Returns <b>SOFTBUS_OK</b> if the operation is successful;
204  * returns a non-zero value otherwise.
205  * @since 2.0
206  * @version 2.0
207  */
208 int32_t GeneralGetPeerDeviceId(uint32_t handle, char *deviceId, uint32_t len);
209 #ifdef __cplusplus
210 }
211 #endif
212 #endif // CONNECTION_H
213