1 #include <errno.h>
2 #include <string.h>
3 
4 #include <netlink/genl/genl.h>
5 #include <netlink/msg.h>
6 #include <netlink/attr.h>
7 
8 #include "nl80211.h"
9 #include "iw.h"
10 
set_power_save(struct nl80211_state * state,struct nl_cb * cb,struct nl_msg * msg,int argc,char ** argv,enum id_input id)11 static int set_power_save(struct nl80211_state *state,
12 			  struct nl_cb *cb,
13 			  struct nl_msg *msg,
14 			  int argc, char **argv,
15 			  enum id_input id)
16 {
17 	enum nl80211_ps_state ps_state;
18 
19 	if (argc != 1) {
20 		printf("Invalid parameters!\n");
21 		return 2;
22 	}
23 
24 	if (strcmp(argv[0], "on") == 0)
25 		ps_state = NL80211_PS_ENABLED;
26 	else if (strcmp(argv[0], "off") == 0)
27 		ps_state = NL80211_PS_DISABLED;
28 	else {
29 		printf("Invalid parameter: %s\n", argv[0]);
30 		return 2;
31 	}
32 
33 	NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
34 
35 	return 0;
36 
37  nla_put_failure:
38 	return -ENOBUFS;
39 }
40 
41 COMMAND(set, power_save, "<on|off>",
42 	NL80211_CMD_SET_POWER_SAVE, 0, CIB_NETDEV, set_power_save,
43 	"Set power save state to on or off.");
44 
print_power_save_handler(struct nl_msg * msg,void * arg)45 static int print_power_save_handler(struct nl_msg *msg, void *arg)
46 {
47 	struct nlattr *attrs[NL80211_ATTR_MAX + 1];
48 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
49 	const char *s;
50 
51 	nla_parse(attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
52 		  genlmsg_attrlen(gnlh, 0), NULL);
53 
54 	if (!attrs[NL80211_ATTR_PS_STATE])
55 		return NL_SKIP;
56 
57 	switch (nla_get_u32(attrs[NL80211_ATTR_PS_STATE])) {
58 	case NL80211_PS_ENABLED:
59 		s = "on";
60 		break;
61 	case NL80211_PS_DISABLED:
62 	default:
63 		s = "off";
64 		break;
65 	}
66 
67 	printf("Power save: %s\n", s);
68 
69 	return NL_SKIP;
70 }
71 
get_power_save(struct nl80211_state * state,struct nl_cb * cb,struct nl_msg * msg,int argc,char ** argv,enum id_input id)72 static int get_power_save(struct nl80211_state *state,
73 				   struct nl_cb *cb,
74 				   struct nl_msg *msg,
75 				   int argc, char **argv,
76 				   enum id_input id)
77 {
78 	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
79 		  print_power_save_handler, NULL);
80 	return 0;
81 }
82 
83 COMMAND(get, power_save, "<param>",
84 	NL80211_CMD_GET_POWER_SAVE, 0, CIB_NETDEV, get_power_save,
85 	"Retrieve power save state.");
86