Skip to content

Commit ec321f4

Browse files
committed
net: shell: Print info about websocket
Add "net websocket" command that displays websocket information. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent a75d68c commit ec321f4

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

subsys/net/ip/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ endif()
4545
if(CONFIG_NET_SHELL)
4646
zephyr_library_sources(net_shell.c)
4747
zephyr_library_include_directories(. ${ZEPHYR_BASE}/subsys/net/l2)
48+
zephyr_library_include_directories(. ${ZEPHYR_BASE}/subsys/net/lib)
4849
zephyr_library_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)
4950
endif()

subsys/net/ip/net_shell.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG);
6666
#include "net_shell.h"
6767
#include "net_stats.h"
6868

69+
#include <sys/fdtable.h>
70+
#include "websocket/websocket_internal.h"
71+
6972
#define PR(fmt, ...) \
7073
shell_fprintf(shell, SHELL_NORMAL, fmt, ##__VA_ARGS__)
7174

@@ -3934,6 +3937,72 @@ static int cmd_net_vlan_del(const struct shell *shell, size_t argc,
39343937
return 0;
39353938
}
39363939

3940+
#if defined(CONFIG_WEBSOCKET_CLIENT)
3941+
static void websocket_context_cb(struct websocket_context *context,
3942+
void *user_data)
3943+
{
3944+
#if defined(CONFIG_NET_IPV6) && !defined(CONFIG_NET_IPV4)
3945+
#define ADDR_LEN NET_IPV6_ADDR_LEN
3946+
#elif defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_IPV6)
3947+
#define ADDR_LEN NET_IPV4_ADDR_LEN
3948+
#else
3949+
#define ADDR_LEN NET_IPV6_ADDR_LEN
3950+
#endif
3951+
struct net_shell_user_data *data = user_data;
3952+
const struct shell *shell = data->shell;
3953+
struct net_context *net_ctx;
3954+
int *count = data->user_data;
3955+
/* +7 for []:port */
3956+
char addr_local[ADDR_LEN + 7];
3957+
char addr_remote[ADDR_LEN + 7] = "";
3958+
3959+
net_ctx = z_get_fd_obj(context->real_sock, NULL, 0);
3960+
if (net_ctx == NULL) {
3961+
PR_ERROR("Invalid fd %d");
3962+
return;
3963+
}
3964+
3965+
get_addresses(net_ctx, addr_local, sizeof(addr_local),
3966+
addr_remote, sizeof(addr_remote));
3967+
3968+
PR("[%2d] %p/%p\t%p %16s\t%16s\n",
3969+
(*count) + 1, context, net_ctx,
3970+
net_context_get_iface(net_ctx),
3971+
addr_local, addr_remote);
3972+
3973+
(*count)++;
3974+
}
3975+
#endif /* CONFIG_WEBSOCKET_CLIENT */
3976+
3977+
static int cmd_net_websocket(const struct shell *shell, size_t argc,
3978+
char *argv[])
3979+
{
3980+
#if defined(CONFIG_WEBSOCKET_CLIENT)
3981+
struct net_shell_user_data user_data;
3982+
int count = 0;
3983+
3984+
ARG_UNUSED(argc);
3985+
ARG_UNUSED(argv);
3986+
3987+
PR(" websocket/net_ctx\tIface "
3988+
"Local \tRemote\n");
3989+
3990+
user_data.shell = shell;
3991+
user_data.user_data = &count;
3992+
3993+
websocket_context_foreach(websocket_context_cb, &user_data);
3994+
3995+
if (count == 0) {
3996+
PR("No connections\n");
3997+
}
3998+
#else
3999+
PR_INFO("Set CONFIG_WEBSOCKET_CLIENT to enable Websocket client "
4000+
"support.\n");
4001+
#endif /* CONFIG_WEBSOCKET_CLIENT */
4002+
4003+
return 0;
4004+
}
4005+
39374006
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_arp,
39384007
SHELL_CMD(flush, NULL, "Remove all entries from ARP cache.",
39394008
cmd_net_arp_flush),
@@ -4249,6 +4318,9 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_commands,
42494318
SHELL_CMD(tcp, &net_cmd_tcp, "Connect/send/close TCP connection.",
42504319
cmd_net_tcp),
42514320
SHELL_CMD(vlan, &net_cmd_vlan, "Show VLAN information.", cmd_net_vlan),
4321+
SHELL_CMD(websocket, NULL, "Print information about WebSocket "
4322+
"connections.",
4323+
cmd_net_websocket),
42524324
SHELL_SUBCMD_SET_END
42534325
);
42544326

0 commit comments

Comments
 (0)