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