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_H
17 #define NET_WEBSOCKET_H
18 
19 #include <signal.h>
20 #include <stdint.h>
21 #include <string.h>
22 
23 /**
24  * @addtogroup netstack
25  * @{
26  *
27  * @brief Provides C APIs for the WebSocket client module.
28 
29  * @since 11
30  * @version 1.0
31  */
32 
33 /**
34  * @file net_websocket.h
35  *
36  * @brief Defines the APIs for the WebSocket client module.
37  *
38  * @library libnet_websocket.so
39  * @syscap SystemCapability.Communication.NetStack
40  * @since 11
41  * @version 1.0
42  */
43 
44 #include "net_websocket_type.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /**
51  * @brief Constructor of websocket.
52  *
53  * @param onMessage Callback function invoked when a message is received.
54  * @param onClose Callback function invoked when a connection closing message is closed.
55  * @param onError Callback function invoked when a connection error message is received.
56  * @param onOpen Callback function invoked when a connection setup message is received.
57  * @return Pointer to the WebSocket client if success; NULL otherwise.
58  * @syscap SystemCapability.Communication.NetStack
59  * @since 11
60  * @version 1.0
61  */
62 struct WebSocket *OH_WebSocketClient_Constructor(WebSocket_OnOpenCallback onOpen, WebSocket_OnMessageCallback onMessage,
63                                                  WebSocket_OnErrorCallback onError, WebSocket_OnCloseCallback onclose);
64 
65 /**
66  * @brief Adds the header information to the client request.
67  *
68  * @param client Pointer to the WebSocket client.
69  * @param header Header information
70  * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
71  * @syscap SystemCapability.Communication.NetStack
72  * @since 11
73  * @version 1.0
74  */
75 int OH_WebSocketClient_AddHeader(struct WebSocket *client, struct WebSocket_Header header);
76 
77 /**
78  * @brief Connects the client to the server.
79  *
80  * @param client Pointer to the WebSocket client.
81  * @param url URL for the client to connect to the server.
82  * @param options Optional parameters.
83  * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
84  * @permission ohos.permission.INTERNET
85  * @syscap SystemCapability.Communication.NetStack
86  * @since 11
87  * @version 1.0
88  */
89 int OH_WebSocketClient_Connect(struct WebSocket *client, const char *url, struct WebSocket_RequestOptions options);
90 
91 /**
92  * @brief Sends data from the client to the server.
93  *
94  * @param client Pointer to the WebSocket client.
95  * @param data Data sent by the client.
96  * @param length Length of the data sent by the client.
97  * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
98  * @permission ohos.permission.INTERNET
99  * @syscap SystemCapability.Communication.NetStack
100  * @since 11
101  * @version 1.0
102  */
103 int OH_WebSocketClient_Send(struct WebSocket *client, char *data, size_t length);
104 
105 /**
106  * @brief Closes a WebSocket connection.
107  *
108  * @param client Pointer to the WebSocket client.
109  * @param url URL for the client to connect to the server.
110  * @param options Optional parameters.
111  * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
112  * @permission ohos.permission.INTERNET
113  * @syscap SystemCapability.Communication.NetStack
114  * @since 11
115  * @version 1.0
116  */
117 int OH_WebSocketClient_Close(struct WebSocket *client, struct WebSocket_CloseOption options);
118 
119 /**
120  * @brief Releases the context and resources of the WebSocket connection.
121  *
122  * @param client Pointer to the WebSocket client.
123  * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
124  * @permission ohos.permission.INTERNET
125  * @syscap SystemCapability.Communication.NetStack
126  * @since 11
127  * @version 1.0
128  */
129 int OH_WebSocketClient_Destroy(struct WebSocket *client);
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif // NET_WEBSOCKET_H