Skip to content

Commit bc003db

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

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

subsys/net/lib/shell/dhcpv4.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,66 @@ static int cmd_net_dhcpv4_server_status(const struct shell *sh, size_t argc, cha
187187
return 0;
188188
}
189189

190+
static int cmd_net_dhcpv4_client_start(const struct shell *sh, size_t argc, char *argv[])
191+
{
192+
#if defined(CONFIG_NET_DHCPV4)
193+
struct net_if *iface = NULL;
194+
int idx;
195+
196+
if (argc < 1) {
197+
PR_ERROR("Correct usage: net dhcpv4 client %s <index>\n", "start");
198+
return -EINVAL;
199+
}
200+
201+
idx = get_iface_idx(sh, argv[1]);
202+
if (idx < 0) {
203+
return -ENOEXEC;
204+
}
205+
206+
iface = net_if_get_by_index(idx);
207+
if (!iface) {
208+
PR_WARNING("No such interface in index %d\n", idx);
209+
return -ENOEXEC;
210+
}
211+
212+
net_dhcpv4_restart(iface);
213+
214+
#else /* CONFIG_NET_DHCPV4 */
215+
PR_INFO("Set %s to enable %s support.\n", "CONFIG_NET_DHCPV4", "DHCPv4");
216+
#endif /* CONFIG_NET_DHCPV4 */
217+
return 0;
218+
}
219+
220+
static int cmd_net_dhcpv4_client_stop(const struct shell *sh, size_t argc, char *argv[])
221+
{
222+
#if defined(CONFIG_NET_DHCPV4)
223+
struct net_if *iface = NULL;
224+
int idx;
225+
226+
if (argc < 1) {
227+
PR_ERROR("Correct usage: net dhcpv4 client %s <index>\n", "stop");
228+
return -EINVAL;
229+
}
230+
231+
idx = get_iface_idx(sh, argv[1]);
232+
if (idx < 0) {
233+
return -ENOEXEC;
234+
}
235+
236+
iface = net_if_get_by_index(idx);
237+
if (!iface) {
238+
PR_WARNING("No such interface in index %d\n", idx);
239+
return -ENOEXEC;
240+
}
241+
242+
net_dhcpv4_stop(iface);
243+
244+
#else /* CONFIG_NET_DHCPV4 */
245+
PR_INFO("Set %s to enable %s support.\n", "CONFIG_NET_DHCPV4", "DHCPv4");
246+
#endif /* CONFIG_NET_DHCPV4 */
247+
return 0;
248+
}
249+
190250
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dhcpv4_server,
191251
SHELL_CMD_ARG(start, NULL, "Start the DHCPv4 server operation on the interface.\n"
192252
"'net dhcpv4 server start <index> <base address>'\n"
@@ -204,10 +264,25 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dhcpv4_server,
204264
SHELL_SUBCMD_SET_END
205265
);
206266

267+
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dhcpv4_client,
268+
SHELL_CMD_ARG(start, NULL, "Start the DHCPv4 client operation on the interface.\n"
269+
"'net dhcpv4 client start <index>'\n"
270+
"<index> is the network interface index.",
271+
cmd_net_dhcpv4_client_start, 2, 0),
272+
SHELL_CMD_ARG(stop, NULL, "Stop the DHCPv4 client operation on the interface.\n"
273+
"'net dhcpv4 client stop <index>'\n"
274+
"<index> is the network interface index.",
275+
cmd_net_dhcpv4_client_stop, 2, 0),
276+
SHELL_SUBCMD_SET_END
277+
);
278+
207279
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dhcpv4,
208280
SHELL_CMD(server, &net_cmd_dhcpv4_server,
209281
"DHCPv4 server service management.",
210282
NULL),
283+
SHELL_CMD(client, &net_cmd_dhcpv4_client,
284+
"DHCPv4 client management.",
285+
NULL),
211286
SHELL_SUBCMD_SET_END
212287
);
213288

0 commit comments

Comments
 (0)