Skip to content

Commit b3bea64

Browse files
committed
net: websocket: client: Simple API for Websocket client
Implement simple API to do Websocket client requests. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent c5d686a commit b3bea64

File tree

8 files changed

+1234
-0
lines changed

8 files changed

+1234
-0
lines changed

include/net/websocket.h

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/** @file
2+
* @brief Websocket API
3+
*
4+
* An API for applications to setup websocket connections
5+
*/
6+
7+
/*
8+
* Copyright (c) 2019 Intel Corporation
9+
*
10+
* SPDX-License-Identifier: Apache-2.0
11+
*/
12+
13+
#ifndef ZEPHYR_INCLUDE_NET_WEBSOCKET_H_
14+
#define ZEPHYR_INCLUDE_NET_WEBSOCKET_H_
15+
16+
#include <kernel.h>
17+
18+
#include <net/net_ip.h>
19+
#include <net/http_parser.h>
20+
#include <net/http_client.h>
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
/**
27+
* @brief Websocket API
28+
* @defgroup websocket Websocket API
29+
* @ingroup networking
30+
* @{
31+
*/
32+
33+
/** Values for flag variable in HTTP receive callback */
34+
#define WEBSOCKET_FLAG_FINAL 0x00000001 /**< Final frame */
35+
#define WEBSOCKET_FLAG_TEXT 0x00000002 /**< Textual data */
36+
#define WEBSOCKET_FLAG_BINARY 0x00000004 /**< Binary data */
37+
#define WEBSOCKET_FLAG_CLOSE 0x00000008 /**< Closing connection */
38+
#define WEBSOCKET_FLAG_PING 0x00000010 /**< Ping message */
39+
#define WEBSOCKET_FLAG_PONG 0x00000011 /**< Pong message */
40+
41+
enum websocket_opcode {
42+
WEBSOCKET_OPCODE_CONTINUE = 0x00,
43+
WEBSOCKET_OPCODE_DATA_TEXT = 0x01,
44+
WEBSOCKET_OPCODE_DATA_BINARY = 0x02,
45+
WEBSOCKET_OPCODE_CLOSE = 0x08,
46+
WEBSOCKET_OPCODE_PING = 0x09,
47+
WEBSOCKET_OPCODE_PONG = 0x0A,
48+
};
49+
50+
/**
51+
* @typedef websocket_connect_cb_t
52+
* @brief Callback called after Websocket connection is established.
53+
*
54+
* @param sock Websocket id
55+
* @param req HTTP handshake request
56+
* @param user_data A valid pointer on some user data or NULL
57+
*
58+
* @return 0 if ok, <0 if there is an error and connection should be aborted
59+
*/
60+
typedef int (*websocket_connect_cb_t)(int ws_sock, struct http_request *req,
61+
void *user_data);
62+
63+
/**
64+
* @brief Connect to a server that provides Websocket service. The callback is
65+
* called after connection is established. The returned value is a new socket
66+
* value that can be used to send / receive data.
67+
*
68+
* @param http_sock Socket id to the server. Note that this socket is used to do
69+
* HTTP handshakes etc. The actual Websocket connectivity is done via the
70+
* returned websocket id.
71+
* @param host Host of the Websocket server when doing HTTP handshakes.
72+
* @param url URL of the Websocket.
73+
* @param optional_headers A NULL terminated list of Any optional headers that
74+
* should be added to the HTTP request.
75+
* @param cb User supplied callback function to call when connection is
76+
* established.
77+
* @param http_cb User supplied list of callback functions if the
78+
* calling application wants to know the parsing status or the HTTP
79+
* fields during the handshake. This is optional parameter and normally
80+
* not needed but is useful if the caller wants to know something about
81+
* the fields that the server is sending.
82+
* @param tmp_buf User supplied buffer where HTTP connection data is stored
83+
* @param tmp_buf_len Length of the user supplied temp buffer
84+
* @param timeout Max timeout to wait for the connection. The timeout value
85+
* cannot be 0 as there would be no time to receive the data.
86+
* @param user_data User specified data that is passed to the callback.
87+
*
88+
* @return Websocket id to be used when sending/receiving Websocket data.
89+
*/
90+
int websocket_connect(int http_sock, const char *host, const char *url,
91+
const char **optional_headers,
92+
websocket_connect_cb_t cb,
93+
const struct http_parser_settings *http_cb,
94+
u8_t *tmp_buf, size_t tmp_buf_len,
95+
s32_t timeout, void *user_data);
96+
97+
/**
98+
* @brief Send websocket msg to peer.
99+
*
100+
* @details The function will automatically add websocket header to the
101+
* message.
102+
*
103+
* @param ws_sock websocket id returned by websocket_connect(). Note that this
104+
* socket is used to do HTTP handshakes etc. The actual Websocket
105+
* connectivity is done via the returned websocket id.
106+
* @param payload Websocket data to send.
107+
* @param payload_len Length of the data to be sent.
108+
* @param opcode Operation code (text, binary, ping, pong, close)
109+
* @param mask Mask the data, see RFC 6455 for details
110+
* @param final Is this final message for this message send. If final == false,
111+
* then the first message must have valid opcode and subsequent messages
112+
* must have opcode WEBSOCKET_OPCODE_CONTINUE. If final == true and this
113+
* is the only message, then opcode should have proper opcode (text or
114+
* binary) set.
115+
* @param timeout How long to try to send the message.
116+
*
117+
* @return <0 if error, >=0 amount of bytes sent
118+
*/
119+
int websocket_send_msg(int ws_sock, const u8_t *payload, size_t payload_len,
120+
enum websocket_opcode opcode, bool mask, bool final,
121+
s32_t timeout);
122+
123+
/**
124+
* @brief Receive websocket msg from peer.
125+
*
126+
* @details The function will automatically remove websocket header from the
127+
* message.
128+
*
129+
* @param ws_sock websocket id returned by websocket_connect().
130+
* @param buf Buffer where websocket data is read.
131+
* @param buf_len Length of the data buffer.
132+
* @param message_type Type of the message.
133+
* @param remaining How much there is data left in the message after this read.
134+
* @param timeout How long to try to send the message.
135+
*
136+
* @return <0 if error, >=0 amount of bytes sent
137+
*/
138+
int websocket_recv_msg(int ws_sock, u8_t *buf, size_t buf_len,
139+
u32_t *message_type, u64_t *remaining, s32_t timeout);
140+
141+
/**
142+
* @brief Close websocket.
143+
*
144+
* @details One must call websocket_connect() after this call to re-establish
145+
* the connection.
146+
*
147+
* @param ws_sock websocket id returned by websocket_connect().
148+
*/
149+
int websocket_disconnect(int ws_sock);
150+
151+
#if defined(CONFIG_WEBSOCKET_CLIENT)
152+
void websocket_init(void);
153+
#else
154+
static inline void websocket_init(void)
155+
{
156+
}
157+
#endif
158+
159+
#ifdef __cplusplus
160+
}
161+
#endif
162+
163+
/**
164+
* @}
165+
*/
166+
167+
#endif /* ZEPHYR_INCLUDE_NET_WEBSOCKET_H_ */

subsys/net/ip/net_core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ LOG_MODULE_REGISTER(net_core, CONFIG_NET_CORE_LOG_LEVEL);
2727
#include <net/net_core.h>
2828
#include <net/dns_resolve.h>
2929
#include <net/gptp.h>
30+
#include <net/websocket.h>
3031

3132
#if defined(CONFIG_NET_LLDP)
3233
#include <net/lldp.h>
@@ -450,6 +451,7 @@ static inline int services_init(void)
450451
}
451452

452453
dns_init_resolver();
454+
websocket_init();
453455

454456
net_shell_init();
455457

subsys/net/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ if(CONFIG_HTTP_PARSER_URL OR CONFIG_HTTP_PARSER OR CONFIG_HTTP_CLIENT)
2121
add_subdirectory(http)
2222
endif()
2323

24+
add_subdirectory_ifdef(CONFIG_WEBSOCKET_CLIENT websocket)
2425
add_subdirectory_ifdef(CONFIG_OPENTHREAD_PLAT openthread)

subsys/net/lib/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ source "subsys/net/lib/mqtt/Kconfig"
1414

1515
source "subsys/net/lib/http/Kconfig"
1616

17+
source "subsys/net/lib/websocket/Kconfig"
18+
1719
source "subsys/net/lib/lwm2m/Kconfig"
1820

1921
source "subsys/net/lib/socks/Kconfig"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library_named(websocket_client)
4+
zephyr_include_directories(${ZEPHYR_BASE}/subsys/net/lib/sockets)
5+
6+
zephyr_library_sources(
7+
websocket.c
8+
)
9+
10+
zephyr_library_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)

subsys/net/lib/websocket/Kconfig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) 2019 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
menuconfig WEBSOCKET_CLIENT
7+
bool "Websocket client support [EXPERIMENTAL]"
8+
select NET_SOCKETS
9+
select HTTP_PARSER
10+
select HTTP_CLIENT
11+
select MBEDTLS
12+
select BASE64
13+
help
14+
Enable Websocket client library.
15+
16+
if WEBSOCKET_CLIENT
17+
18+
config WEBSOCKET_MAX_CONTEXTS
19+
int "Max number of websockets to allocate"
20+
default 1
21+
help
22+
How many Websockets can be created in the system.
23+
24+
module = NET_WEBSOCKET
25+
module-dep = NET_LOG
26+
module-str = Log level for Websocket
27+
module-help = Enable debug message of Websocket client library.
28+
source "subsys/net/Kconfig.template.log_config.net"
29+
30+
endif

0 commit comments

Comments
 (0)