Skip to content

Commit e459191

Browse files
jukkarfabiobaltieri
authored andcommitted
net: shell: dhcpv6: Add cmd to start/stop DHCPv6 client
Allow user to use the net-shell to start or stop DHCPv6 client. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent bc003db commit e459191

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

subsys/net/lib/shell/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ zephyr_library_sources(arp.c)
1111
zephyr_library_sources(capture.c)
1212
zephyr_library_sources(conn.c)
1313
zephyr_library_sources(dhcpv4.c)
14+
zephyr_library_sources(dhcpv6.c)
1415
zephyr_library_sources(dns.c)
1516
zephyr_library_sources(events.c)
1617
zephyr_library_sources(gptp.c)

subsys/net/lib/shell/dhcpv6.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/logging/log.h>
8+
LOG_MODULE_DECLARE(net_shell);
9+
10+
#include <stdint.h>
11+
#include <zephyr/net/socket.h>
12+
13+
#include "net_shell_private.h"
14+
15+
static int cmd_net_dhcpv6_client_start(const struct shell *sh, size_t argc, char *argv[])
16+
{
17+
#if defined(CONFIG_NET_DHCPV6)
18+
struct net_if *iface = NULL;
19+
int idx;
20+
21+
if (argc < 1) {
22+
PR_ERROR("Correct usage: net dhcpv6 client %s <index>\n", "start");
23+
return -EINVAL;
24+
}
25+
26+
idx = get_iface_idx(sh, argv[1]);
27+
if (idx < 0) {
28+
return -ENOEXEC;
29+
}
30+
31+
iface = net_if_get_by_index(idx);
32+
if (!iface) {
33+
PR_WARNING("No such interface in index %d\n", idx);
34+
return -ENOEXEC;
35+
}
36+
37+
net_dhcpv6_restart(iface);
38+
39+
#else /* CONFIG_NET_DHCPV6 */
40+
PR_INFO("Set %s to enable %s support.\n", "CONFIG_NET_DHCPV6", "Dhcpv6");
41+
#endif /* CONFIG_NET_DHCPV6 */
42+
return 0;
43+
}
44+
45+
static int cmd_net_dhcpv6_client_stop(const struct shell *sh, size_t argc, char *argv[])
46+
{
47+
#if defined(CONFIG_NET_DHCPV6)
48+
struct net_if *iface = NULL;
49+
int idx;
50+
51+
if (argc < 1) {
52+
PR_ERROR("Correct usage: net dhcpv6 client %s <index>\n", "stop");
53+
return -EINVAL;
54+
}
55+
56+
idx = get_iface_idx(sh, argv[1]);
57+
if (idx < 0) {
58+
return -ENOEXEC;
59+
}
60+
61+
iface = net_if_get_by_index(idx);
62+
if (!iface) {
63+
PR_WARNING("No such interface in index %d\n", idx);
64+
return -ENOEXEC;
65+
}
66+
67+
net_dhcpv6_stop(iface);
68+
69+
#else /* CONFIG_NET_DHCPV6 */
70+
PR_INFO("Set %s to enable %s support.\n", "CONFIG_NET_DHCPV6", "Dhcpv6");
71+
#endif /* CONFIG_NET_DHCPV6 */
72+
return 0;
73+
}
74+
75+
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dhcpv6_client,
76+
SHELL_CMD_ARG(start, NULL, "Start the Dhcpv6 client operation on the interface.\n"
77+
"'net dhcpv6 client start <index>'\n"
78+
"<index> is the network interface index.",
79+
cmd_net_dhcpv6_client_start, 2, 0),
80+
SHELL_CMD_ARG(stop, NULL, "Stop the Dhcpv6 client operation on the interface.\n"
81+
"'net dhcpv6 client stop <index>'\n"
82+
"<index> is the network interface index.",
83+
cmd_net_dhcpv6_client_stop, 2, 0),
84+
SHELL_SUBCMD_SET_END
85+
);
86+
87+
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dhcpv6,
88+
SHELL_CMD(client, &net_cmd_dhcpv6_client,
89+
"Dhcpv6 client management.",
90+
NULL),
91+
SHELL_SUBCMD_SET_END
92+
);
93+
94+
SHELL_SUBCMD_ADD((net), dhcpv6, &net_cmd_dhcpv6, "Manage DHPCv6 services.",
95+
NULL, 1, 0);

0 commit comments

Comments
 (0)