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