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