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 #include <zephyr/pm/device.h>
10 LOG_MODULE_DECLARE(net_shell);
11 
12 #include "net_shell_private.h"
13 
cmd_net_suspend(const struct shell * sh,size_t argc,char * argv[])14 static int cmd_net_suspend(const struct shell *sh, size_t argc, char *argv[])
15 {
16 #if defined(CONFIG_NET_POWER_MANAGEMENT)
17 	if (argv[1]) {
18 		struct net_if *iface = NULL;
19 		const struct device *dev;
20 		int idx;
21 		int ret;
22 
23 		idx = get_iface_idx(sh, argv[1]);
24 		if (idx < 0) {
25 			return -ENOEXEC;
26 		}
27 
28 		iface = net_if_get_by_index(idx);
29 		if (!iface) {
30 			PR_WARNING("No such interface in index %d\n", idx);
31 			return -ENOEXEC;
32 		}
33 
34 		dev = net_if_get_device(iface);
35 
36 		ret = pm_device_action_run(dev, PM_DEVICE_ACTION_SUSPEND);
37 		if (ret != 0) {
38 			PR_INFO("Iface could not be suspended: ");
39 
40 			if (ret == -EBUSY) {
41 				PR_INFO("device is busy\n");
42 			} else if (ret == -EALREADY) {
43 				PR_INFO("dehive is already suspended\n");
44 			}
45 		}
46 	} else {
47 		PR("Usage:\n");
48 		PR("\tsuspend <iface index>\n");
49 	}
50 #else
51 	PR_INFO("You need a network driver supporting Power Management.\n");
52 #endif /* CONFIG_NET_POWER_MANAGEMENT */
53 
54 	return 0;
55 }
56 
57 SHELL_SUBCMD_ADD((net), suspend, NULL,
58 		 "Suspend a network interface",
59 		 cmd_net_suspend, 2, 0);
60