Skip to content

Commit

Permalink
net: shell: dhcpv6: Add cmd to start/stop DHCPv6 client
Browse files Browse the repository at this point in the history
Allow user to use the net-shell to start or stop DHCPv6 client.

Signed-off-by: Jukka Rissanen <[email protected]>
  • Loading branch information
jukkar authored and fabiobaltieri committed Sep 23, 2024
1 parent bc003db commit e459191
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 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 @@ -11,6 +11,7 @@ zephyr_library_sources(arp.c)
zephyr_library_sources(capture.c)
zephyr_library_sources(conn.c)
zephyr_library_sources(dhcpv4.c)
zephyr_library_sources(dhcpv6.c)
zephyr_library_sources(dns.c)
zephyr_library_sources(events.c)
zephyr_library_sources(gptp.c)
Expand Down
95 changes: 95 additions & 0 deletions subsys/net/lib/shell/dhcpv6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

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

#include <stdint.h>
#include <zephyr/net/socket.h>

#include "net_shell_private.h"

static int cmd_net_dhcpv6_client_start(const struct shell *sh, size_t argc, char *argv[])
{
#if defined(CONFIG_NET_DHCPV6)
struct net_if *iface = NULL;
int idx;

if (argc < 1) {
PR_ERROR("Correct usage: net dhcpv6 client %s <index>\n", "start");
return -EINVAL;
}

idx = get_iface_idx(sh, argv[1]);
if (idx < 0) {
return -ENOEXEC;
}

iface = net_if_get_by_index(idx);
if (!iface) {
PR_WARNING("No such interface in index %d\n", idx);
return -ENOEXEC;
}

net_dhcpv6_restart(iface);

#else /* CONFIG_NET_DHCPV6 */
PR_INFO("Set %s to enable %s support.\n", "CONFIG_NET_DHCPV6", "Dhcpv6");
#endif /* CONFIG_NET_DHCPV6 */
return 0;
}

static int cmd_net_dhcpv6_client_stop(const struct shell *sh, size_t argc, char *argv[])
{
#if defined(CONFIG_NET_DHCPV6)
struct net_if *iface = NULL;
int idx;

if (argc < 1) {
PR_ERROR("Correct usage: net dhcpv6 client %s <index>\n", "stop");
return -EINVAL;
}

idx = get_iface_idx(sh, argv[1]);
if (idx < 0) {
return -ENOEXEC;
}

iface = net_if_get_by_index(idx);
if (!iface) {
PR_WARNING("No such interface in index %d\n", idx);
return -ENOEXEC;
}

net_dhcpv6_stop(iface);

#else /* CONFIG_NET_DHCPV6 */
PR_INFO("Set %s to enable %s support.\n", "CONFIG_NET_DHCPV6", "Dhcpv6");
#endif /* CONFIG_NET_DHCPV6 */
return 0;
}

SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dhcpv6_client,
SHELL_CMD_ARG(start, NULL, "Start the Dhcpv6 client operation on the interface.\n"
"'net dhcpv6 client start <index>'\n"
"<index> is the network interface index.",
cmd_net_dhcpv6_client_start, 2, 0),
SHELL_CMD_ARG(stop, NULL, "Stop the Dhcpv6 client operation on the interface.\n"
"'net dhcpv6 client stop <index>'\n"
"<index> is the network interface index.",
cmd_net_dhcpv6_client_stop, 2, 0),
SHELL_SUBCMD_SET_END
);

SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dhcpv6,
SHELL_CMD(client, &net_cmd_dhcpv6_client,
"Dhcpv6 client management.",
NULL),
SHELL_SUBCMD_SET_END
);

SHELL_SUBCMD_ADD((net), dhcpv6, &net_cmd_dhcpv6, "Manage DHPCv6 services.",
NULL, 1, 0);

0 comments on commit e459191

Please sign in to comment.