Skip to content

Commit

Permalink
net: shell: Add websocket command
Browse files Browse the repository at this point in the history
Move "websocket" command to a separate file.

Signed-off-by: Jukka Rissanen <[email protected]>
  • Loading branch information
jukkar authored and carlescufi committed Oct 23, 2023
1 parent d84c257 commit aab3713
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 63 deletions.
1 change: 1 addition & 0 deletions subsys/net/lib/shell/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ zephyr_library_sources(tcp.c)
zephyr_library_sources(udp.c)
zephyr_library_sources(virtual.c)
zephyr_library_sources(vlan.c)
zephyr_library_sources(websocket.c)

zephyr_library_include_directories(
${ZEPHYR_BASE}/kernel/include
Expand Down
63 changes: 0 additions & 63 deletions subsys/net/lib/shell/net_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG);
#include "net_shell.h"

#include <zephyr/sys/fdtable.h>
#include "websocket/websocket_internal.h"

int get_iface_idx(const struct shell *sh, char *index_str)
{
Expand Down Expand Up @@ -243,65 +242,6 @@ const char *iface2str(struct net_if *iface, const char **extra)
return "<unknown type>";
}

#if defined(CONFIG_WEBSOCKET_CLIENT)
static void websocket_context_cb(struct websocket_context *context,
void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *sh = data->sh;
struct net_context *net_ctx;
int *count = data->user_data;
/* +7 for []:port */
char addr_local[ADDR_LEN + 7];
char addr_remote[ADDR_LEN + 7] = "";

net_ctx = z_get_fd_obj(context->real_sock, NULL, 0);
if (net_ctx == NULL) {
PR_ERROR("Invalid fd %d", context->real_sock);
return;
}

get_addresses(net_ctx, addr_local, sizeof(addr_local),
addr_remote, sizeof(addr_remote));

PR("[%2d] %p/%p\t%p %16s\t%16s\n",
(*count) + 1, context, net_ctx,
net_context_get_iface(net_ctx),
addr_local, addr_remote);

(*count)++;
}
#endif /* CONFIG_WEBSOCKET_CLIENT */

static int cmd_net_websocket(const struct shell *sh, size_t argc,
char *argv[])
{
#if defined(CONFIG_WEBSOCKET_CLIENT)
struct net_shell_user_data user_data;
int count = 0;

ARG_UNUSED(argc);
ARG_UNUSED(argv);

PR(" websocket/net_ctx\tIface "
"Local \tRemote\n");

user_data.sh = sh;
user_data.user_data = &count;

websocket_context_foreach(websocket_context_cb, &user_data);

if (count == 0) {
PR("No connections\n");
}
#else
PR_INFO("Set %s to enable %s support.\n", "CONFIG_WEBSOCKET_CLIENT",
"Websocket");
#endif /* CONFIG_WEBSOCKET_CLIENT */

return 0;
}

#if defined(CONFIG_NET_SHELL_DYN_CMD_COMPLETION)

SHELL_DYNAMIC_CMD_CREATE(iface_index, iface_index_get);
Expand All @@ -311,9 +251,6 @@ SHELL_DYNAMIC_CMD_CREATE(iface_index, iface_index_get);


SHELL_STATIC_SUBCMD_SET_CREATE(net_commands,
SHELL_CMD(websocket, NULL, "Print information about WebSocket "
"connections.",
cmd_net_websocket),
SHELL_SUBCMD_SET_END
);

Expand Down
83 changes: 83 additions & 0 deletions subsys/net/lib/shell/websocket.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2016 Intel Corporation
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(net_shell);

#if defined(CONFIG_NET_L2_VIRTUAL)
#include <zephyr/net/virtual.h>
#endif

#include "common.h"

#include "websocket/websocket_internal.h"

#include <zephyr/sys/fdtable.h>

#if defined(CONFIG_WEBSOCKET_CLIENT)
static void websocket_context_cb(struct websocket_context *context,
void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *sh = data->sh;
struct net_context *net_ctx;
int *count = data->user_data;
/* +7 for []:port */
char addr_local[ADDR_LEN + 7];
char addr_remote[ADDR_LEN + 7] = "";

net_ctx = z_get_fd_obj(context->real_sock, NULL, 0);
if (net_ctx == NULL) {
PR_ERROR("Invalid fd %d", context->real_sock);
return;
}

if ((*count) == 0) {
PR(" websocket/net_ctx\tIface "
"Local \tRemote\n");
}

get_addresses(net_ctx, addr_local, sizeof(addr_local),
addr_remote, sizeof(addr_remote));

PR("[%2d] %p/%p\t%p %16s\t%16s\n",
(*count) + 1, context, net_ctx,
net_context_get_iface(net_ctx),
addr_local, addr_remote);

(*count)++;
}
#endif /* CONFIG_WEBSOCKET_CLIENT */

static int cmd_net_websocket(const struct shell *sh, size_t argc, char *argv[])
{
#if defined(CONFIG_WEBSOCKET_CLIENT)
struct net_shell_user_data user_data;
int count = 0;

ARG_UNUSED(argc);
ARG_UNUSED(argv);

user_data.sh = sh;
user_data.user_data = &count;

websocket_context_foreach(websocket_context_cb, &user_data);

if (count == 0) {
PR("No connections\n");
}
#else
PR_INFO("Set %s to enable %s support.\n", "CONFIG_WEBSOCKET_CLIENT",
"Websocket");
#endif /* CONFIG_WEBSOCKET_CLIENT */

return 0;
}

SHELL_SUBCMD_ADD((net), websocket, NULL,
"Print information about WebSocket connections.",
cmd_net_websocket, 1, 0);

0 comments on commit aab3713

Please sign in to comment.