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