1 /*
2 * Copyright (c) 2016 Intel Corporation
3 * Copyright (c) 2023 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/logging/log.h>
9 LOG_MODULE_DECLARE(net_shell);
10
11 #if defined(CONFIG_NET_L2_VIRTUAL)
12 #include <zephyr/net/virtual.h>
13 #endif
14
15 #include "net_shell_private.h"
16
17 #include "websocket/websocket_internal.h"
18
19 #include <zephyr/sys/fdtable.h>
20
21 #if defined(CONFIG_WEBSOCKET_CLIENT)
websocket_context_cb(struct websocket_context * context,void * user_data)22 static void websocket_context_cb(struct websocket_context *context,
23 void *user_data)
24 {
25 struct net_shell_user_data *data = user_data;
26 const struct shell *sh = data->sh;
27 struct net_context *net_ctx;
28 int *count = data->user_data;
29 /* +7 for []:port */
30 char addr_local[ADDR_LEN + 7];
31 char addr_remote[ADDR_LEN + 7] = "";
32
33 net_ctx = zvfs_get_fd_obj(context->real_sock, NULL, 0);
34 if (net_ctx == NULL) {
35 PR_ERROR("Invalid fd %d", context->real_sock);
36 return;
37 }
38
39 if ((*count) == 0) {
40 PR(" websocket/net_ctx \tIface\t"
41 "%-16s\t%-16s\n", "Local", "Remote");
42 }
43
44 get_addresses(net_ctx, addr_local, sizeof(addr_local),
45 addr_remote, sizeof(addr_remote));
46
47 PR("[%2d] %p/%p\t%d\t%-16s\t%-16s\n",
48 (*count) + 1, context, net_ctx,
49 net_if_get_by_iface(net_context_get_iface(net_ctx)),
50 addr_local, addr_remote);
51
52 (*count)++;
53 }
54 #endif /* CONFIG_WEBSOCKET_CLIENT */
55
cmd_net_websocket(const struct shell * sh,size_t argc,char * argv[])56 static int cmd_net_websocket(const struct shell *sh, size_t argc, char *argv[])
57 {
58 #if defined(CONFIG_WEBSOCKET_CLIENT)
59 struct net_shell_user_data user_data;
60 int count = 0;
61
62 ARG_UNUSED(argc);
63 ARG_UNUSED(argv);
64
65 user_data.sh = sh;
66 user_data.user_data = &count;
67
68 websocket_context_foreach(websocket_context_cb, &user_data);
69
70 if (count == 0) {
71 PR("No connections\n");
72 }
73 #else
74 PR_INFO("Set %s to enable %s support.\n", "CONFIG_WEBSOCKET_CLIENT",
75 "Websocket");
76 #endif /* CONFIG_WEBSOCKET_CLIENT */
77
78 return 0;
79 }
80
81 SHELL_SUBCMD_ADD((net), websocket, NULL,
82 "Print information about WebSocket connections.",
83 cmd_net_websocket, 1, 0);
84