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 NET_WEBSOCKET_TYPE_H
17 #define NET_WEBSOCKET_TYPE_H
18 
19 /**
20  * @addtogroup netstack
21  * @{
22  *
23  * @brief Provides C APIs for the WebSocket client module.
24  *
25  * @since 11
26  * @version 1.0
27  */
28 
29 /**
30  * @file net_websocket_type.h
31  * @brief Defines the data structure for the C APIs of the WebSocket client module.
32  *
33  * @library libnet_websocket.so
34  * @syscap SystemCapability.Communication.NetStack
35  * @since 11
36  * @version 1.0
37  */
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @brief Defines the parameters for connection closing by the server.
45  *
46  * @since 11
47  * @version 1.0
48  */
49 struct WebSocket_CloseResult {
50     /** Error code */
51     uint32_t code;
52     /** Error cause */
53     const char *reason;
54 };
55 
56 /**
57  * @brief Defines the parameters for proactive connection closing by the client.
58  *
59  * @since 11
60  * @version 1.0
61  */
62 struct WebSocket_CloseOption {
63     /** Error code */
64     uint32_t code;
65     /** Error cause */
66     const char *reason;
67 };
68 
69 /**
70  * @brief Defines the parameters for the connection error reported by the server.
71  *
72  * @since 11
73  * @version 1.0
74  */
75 struct WebSocket_ErrorResult {
76     /** Error code */
77     uint32_t errorCode;
78     /** Error message */
79     const char *errorMessage;
80 };
81 
82 /**
83  * @brief Defines the parameters for the connection success reported by the server.
84  *
85  * @since 11
86  * @version 1.0
87  */
88 struct WebSocket_OpenResult {
89     /** Connection success code */
90     uint32_t code;
91     /** Connection success reason */
92     const char *reason;
93 };
94 
95 /**
96  * @brief Defines the callback function invoked when an <b>open</b> message is received.
97  *
98  * @param client WebSocket client.
99  * @param openResult Content of the <b>open</b> message received by the WebSocket client.
100  * @since 11
101  * @version 1.0
102  */
103 typedef void (*WebSocket_OnOpenCallback)(struct WebSocket *client, WebSocket_OpenResult openResult);
104 
105 /**
106  * @brief Defines the callback function invoked when data is received.
107  *
108  * @param client WebSocket client.
109  * @param data Data received by the WebSocket client.
110  * @param length Length of the data received by the WebSocket client.
111  * @since 11
112  * @version 1.0
113  */
114 typedef void (*WebSocket_OnMessageCallback)(struct WebSocket *client, char *data, uint32_t length);
115 
116 /**
117  * @brief Defines the callback function invoked when an error message is received.
118  *
119  * @param client WebSocket client.
120  * @param errorResult Content of the connection error message received by the WebSocket client.
121  * @since 11
122  * @version 1.0
123  */
124 typedef void (*WebSocket_OnErrorCallback)(struct WebSocket *client, WebSocket_ErrorResult errorResult);
125 
126 /**
127  * @brief Defines the callback function invoked when a <b>close</b> message is received.
128  *
129  * @param client WebSocket client.
130  * @param closeResult Content of the <b>close</b> message received by the WebSocket client.
131  * @since 11
132  * @version 1.0
133  */
134 typedef void (*WebSocket_OnCloseCallback)(struct WebSocket *client, WebSocket_CloseResult closeResult);
135 
136 /**
137  * @brief Adds the header linked list to the WebSocket client.
138  *
139  * @since 11
140  * @version 1.0
141  */
142 struct WebSocket_Header {
143     /** Header field name */
144     const char *fieldName;
145     /** Header field content */
146     const char *fieldValue;
147     /** Next pointer of the header linked list */
148     struct WebSocket_Header *next;
149 };
150 
151 /**
152  * @brief Defines the parameters for the connection between the WebSocket client and server.
153  *
154  * @param headers Header information.
155  * @since 11
156  * @version 1.0
157  */
158 struct WebSocket_RequestOptions {
159     struct WebSocket_Header *headers;
160 };
161 
162 /**
163  * @brief Defines the WebSocket client structure.
164  *
165  * @since 11
166  * @version 1.0
167  */
168 struct WebSocket {
169     /** Pointer to the callback invoked when a connection message is received */
170     WebSocket_OnOpenCallback onOpen;
171     /** Pointer to the callback invoked when a message is received */
172     WebSocket_OnMessageCallback onMessage;
173     /** Pointer to the callback invoked when an error message is received */
174     WebSocket_OnErrorCallback onError;
175     /** Pointer to the callback invoked when a close message is received */
176     WebSocket_OnCloseCallback onClose;
177     /** Content of the request for establishing a connection on the client */
178     WebSocket_RequestOptions requestOptions;
179 };
180 
181 typedef enum WebSocket_ErrCode {
182     /**
183      * Operation success.
184      */
185     WEBSOCKET_OK = 0,
186 
187     /**
188      * @brief Error code base.
189      */
190     E_BASE = 1000,
191 
192     /**
193      * @brief The WebSocket client is null.
194      */
195     WEBSOCKET_CLIENT_NULL = (E_BASE + 1),
196 
197     /**
198      * @brief A WebSocket client is not created.
199      */
200     WEBSOCKET_CLIENT_NOT_CREATED = (E_BASE + 2),
201 
202     /**
203      * @brief An error occurs while setting up a WebSocket connection.
204      */
205     WEBSOCKET_CONNECTION_ERROR = (E_BASE + 3),
206 
207     /**
208      * @brief An error occurs while parsing WebSocket connection parameters.
209      */
210     WEBSOCKET_CONNECTION_PARSE_URL_ERROR = (E_BASE + 5),
211 
212     /**
213      * @brief The memory is insufficient for creating a context during WebSocket connection setup.
214      */
215     WEBSOCKET_CONNECTION_NO_MEMORY = (E_BASE + 6),
216 
217     /**
218      * @brief The WebSocket connection is closed by the peer.
219      */
220     WEBSOCKET_CONNECTION_CLOSED_BY_PEER = (E_BASE + 7),
221 
222     /**
223      * @brief The WebSocket connection is destroyed.
224      */
225     WEBSOCKET_DESTROYED = (E_BASE + 8),
226 
227     /**
228      * @brief An incorrect protocol is used for WebSocket connection.
229      */
230     WEBSOCKET_PROTOCOL_ERROR = (E_BASE + 9),
231 
232     /**
233      * @brief The memory for the WebSocket client to send data is insufficient.
234      */
235     WEBSOCKET_SEND_NO_MEMORY = (E_BASE + 10),
236 
237     /**
238      * @brief The data sent by the WebSocket client is null.
239      */
240     WEBSOCKET_SEND_DATA_NULL = (E_BASE + 11),
241 
242     /**
243      * @brief The length of the data sent by the WebSocket client exceeds the limit.
244      */
245     WEBSOCKET_DATA_LENGTH_EXCEEDED = (E_BASE + 12),
246 
247     /**
248      * @brief The queue length of the data sent by the WebSocket client exceeds the limit.
249      */
250     WEBSOCKET_QUEUE_LENGTH_EXCEEDED = (E_BASE + 13),
251 
252     /**
253      * @brief The context of the WebSocket client is null.
254      */
255     WEBSOCKET_NO_CLIENT_CONTEXT = (E_BASE + 14),
256 
257     /**
258      * @brief The header of the WebSocket client is null.
259      */
260     WEBSOCKET_NO_HEADER_CONTEXT = (E_BASE + 15),
261 
262     /**
263      * @brief The header of the WebSocket client exceeds the limit.
264      */
265     WEBSOCKET_HEADER_EXCEEDED = (E_BASE + 16),
266 
267     /**
268      * @brief The WebSocket client is not connected.
269      */
270     WEBSOCKET_NO_CONNECTION = (E_BASE + 17),
271 
272     /**
273      * @brief The WebSocket client does not have the connection context.
274      */
275     WEBSOCKET_NO_CONNECTION_CONTEXT = (E_BASE + 18),
276 } WebSocket_ErrCode;
277 
278 #ifdef __cplusplus
279 }
280 #endif
281 
282 #endif // NET_WEBSOCKET_TYPE_H