developer | e53f087 | 2021-07-23 11:13:49 +0800 | [diff] [blame] | 1 | /* Copyright (C) 2021 Mediatek Inc. */ |
| 2 | #define _GNU_SOURCE |
| 3 | |
| 4 | #include "mt76-vendor.h" |
| 5 | |
developer | 6f8cffd | 2021-09-29 19:48:25 +0800 | [diff] [blame] | 6 | static struct nla_policy |
developer | e53f087 | 2021-07-23 11:13:49 +0800 | [diff] [blame] | 7 | amnt_ctrl_policy[NUM_MTK_VENDOR_ATTRS_AMNT_CTRL] = { |
| 8 | [MTK_VENDOR_ATTR_AMNT_CTRL_SET] = {.type = NLA_NESTED }, |
| 9 | [MTK_VENDOR_ATTR_AMNT_CTRL_DUMP] = { .type = NLA_NESTED }, |
| 10 | }; |
| 11 | |
developer | 6f8cffd | 2021-09-29 19:48:25 +0800 | [diff] [blame] | 12 | static struct nla_policy |
developer | e53f087 | 2021-07-23 11:13:49 +0800 | [diff] [blame] | 13 | amnt_dump_policy[NUM_MTK_VENDOR_ATTRS_AMNT_DUMP] = { |
| 14 | [MTK_VENDOR_ATTR_AMNT_DUMP_INDEX] = {.type = NLA_U8 }, |
| 15 | [MTK_VENDOR_ATTR_AMNT_DUMP_LEN] = { .type = NLA_U8 }, |
| 16 | [MTK_VENDOR_ATTR_AMNT_DUMP_RESULT] = { .type = NLA_NESTED }, |
| 17 | }; |
| 18 | |
| 19 | static int mt76_amnt_set_attr(struct nl_msg *msg, int argc, char **argv) |
| 20 | { |
| 21 | void *tb1, *tb2; |
| 22 | u8 a[ETH_ALEN], idx; |
| 23 | int i = 0, matches; |
| 24 | |
| 25 | idx = strtoul(argv[0], NULL, 0); |
| 26 | matches = sscanf(argv[1], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 27 | a, a+1, a+2, a+3, a+4, a+5); |
| 28 | |
| 29 | if (matches != ETH_ALEN) |
| 30 | return -EINVAL; |
| 31 | |
| 32 | tb1 = nla_nest_start(msg, MTK_VENDOR_ATTR_AMNT_CTRL_SET | NLA_F_NESTED); |
developer | 6f8cffd | 2021-09-29 19:48:25 +0800 | [diff] [blame] | 33 | if (!tb1) |
| 34 | return -ENOMEM; |
| 35 | |
developer | e53f087 | 2021-07-23 11:13:49 +0800 | [diff] [blame] | 36 | nla_put_u8(msg, MTK_VENDOR_ATTR_AMNT_SET_INDEX, idx); |
| 37 | |
| 38 | tb2 = nla_nest_start(msg, MTK_VENDOR_ATTR_AMNT_SET_MACADDR | NLA_F_NESTED); |
developer | 6f8cffd | 2021-09-29 19:48:25 +0800 | [diff] [blame] | 39 | if (!tb2) { |
| 40 | nla_nest_end(msg, tb1); |
| 41 | return -ENOMEM; |
| 42 | } |
| 43 | |
developer | e53f087 | 2021-07-23 11:13:49 +0800 | [diff] [blame] | 44 | for (i = 0; i < ETH_ALEN; i++) |
| 45 | nla_put_u8(msg, i, a[i]); |
| 46 | |
| 47 | nla_nest_end(msg, tb2); |
| 48 | nla_nest_end(msg, tb1); |
developer | 6f8cffd | 2021-09-29 19:48:25 +0800 | [diff] [blame] | 49 | |
developer | e53f087 | 2021-07-23 11:13:49 +0800 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | int mt76_amnt_set(int idx, int argc, char **argv) |
| 54 | { |
| 55 | struct nl_msg *msg; |
| 56 | void *data; |
| 57 | int ret; |
| 58 | |
| 59 | if (argc < 1) |
| 60 | return 1; |
| 61 | |
| 62 | if (unl_genl_init(&unl, "nl80211") < 0) { |
| 63 | fprintf(stderr, "Failed to connect to nl80211\n"); |
| 64 | return 2; |
| 65 | } |
| 66 | |
| 67 | msg = unl_genl_msg(&unl, NL80211_CMD_VENDOR, false); |
| 68 | |
| 69 | if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, idx) || |
| 70 | nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, MTK_NL80211_VENDOR_ID) || |
| 71 | nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, MTK_NL80211_VENDOR_SUBCMD_AMNT_CTRL)) |
| 72 | return false; |
| 73 | |
| 74 | data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA | NLA_F_NESTED); |
| 75 | |
| 76 | mt76_amnt_set_attr(msg, argc, argv); |
| 77 | |
| 78 | nla_nest_end(msg, data); |
| 79 | |
| 80 | ret = unl_genl_request(&unl, msg, NULL, NULL); |
| 81 | if (ret) |
| 82 | fprintf(stderr, "nl80211 call failed: %s\n", strerror(-ret)); |
| 83 | |
| 84 | unl_free(&unl); |
| 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | static int mt76_amnt_dump_cb(struct nl_msg *msg, void *arg) |
| 90 | { |
| 91 | struct nlattr *tb1[NUM_MTK_VENDOR_ATTRS_AMNT_CTRL]; |
| 92 | struct nlattr *tb2[NUM_MTK_VENDOR_ATTRS_AMNT_DUMP]; |
| 93 | struct nlattr *attr; |
| 94 | struct nlattr *data; |
| 95 | struct nlattr *cur; |
| 96 | struct amnt_data *res; |
| 97 | int len = 0, rem; |
| 98 | |
| 99 | attr = unl_find_attr(&unl, msg, NL80211_ATTR_VENDOR_DATA); |
| 100 | if (!attr) { |
| 101 | fprintf(stderr, "Testdata attribute not found\n"); |
| 102 | return NL_SKIP; |
| 103 | } |
| 104 | |
| 105 | nla_parse_nested(tb1, MTK_VENDOR_ATTR_AMNT_CTRL_MAX, |
| 106 | attr, amnt_ctrl_policy); |
| 107 | |
| 108 | if (!tb1[MTK_VENDOR_ATTR_AMNT_CTRL_DUMP]) |
| 109 | return NL_SKIP; |
| 110 | |
| 111 | nla_parse_nested(tb2, NUM_MTK_VENDOR_ATTRS_AMNT_DUMP, |
| 112 | tb1[MTK_VENDOR_ATTR_AMNT_CTRL_DUMP], amnt_dump_policy); |
| 113 | |
| 114 | if (!tb2[MTK_VENDOR_ATTR_AMNT_DUMP_LEN]) |
| 115 | return NL_SKIP; |
| 116 | |
| 117 | len = nla_get_u8(tb2[MTK_VENDOR_ATTR_AMNT_DUMP_LEN]); |
| 118 | if (!len) |
| 119 | return 0; |
| 120 | |
| 121 | if (!tb2[MTK_VENDOR_ATTR_AMNT_DUMP_RESULT]) |
| 122 | return NL_SKIP; |
| 123 | |
| 124 | data = tb2[MTK_VENDOR_ATTR_AMNT_DUMP_RESULT]; |
| 125 | nla_for_each_nested(cur,data, rem) { |
| 126 | res = (struct amnt_data *) nla_data(cur); |
| 127 | printf("[vendor] amnt_idx: %d, addr=%x:%x:%x:%x:%x:%x, rssi=%d/%d/%d/%d, last_seen=%u\n", |
| 128 | res->idx, |
| 129 | res->addr[0], res->addr[1], res->addr[2], |
| 130 | res->addr[3], res->addr[4], res->addr[5], |
| 131 | res->rssi[0], res->rssi[1], res->rssi[2], |
| 132 | res->rssi[3], res->last_seen); |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int mt76_amnt_dump(int idx, int argc, char **argv) |
| 138 | { |
| 139 | struct nl_msg *msg, *tb1; |
| 140 | void *data; |
| 141 | int ret; |
| 142 | u8 amnt_idx = 0; |
| 143 | |
| 144 | if (argc < 1) |
| 145 | return 1; |
| 146 | |
| 147 | if (unl_genl_init(&unl, "nl80211") < 0) { |
| 148 | fprintf(stderr, "Failed to connect to nl80211\n"); |
| 149 | return 2; |
| 150 | } |
| 151 | |
| 152 | msg = unl_genl_msg(&unl, NL80211_CMD_VENDOR, true); |
| 153 | |
| 154 | if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, idx) || |
| 155 | nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, MTK_NL80211_VENDOR_ID) || |
| 156 | nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, MTK_NL80211_VENDOR_SUBCMD_AMNT_CTRL)) |
| 157 | return false; |
| 158 | |
| 159 | data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA | NLA_F_NESTED); |
| 160 | |
| 161 | tb1 = nla_nest_start(msg, MTK_VENDOR_ATTR_AMNT_CTRL_DUMP | NLA_F_NESTED); |
| 162 | |
| 163 | amnt_idx = strtoul(argv[0], NULL, 0); |
| 164 | nla_put_u8(msg, MTK_VENDOR_ATTR_AMNT_DUMP_INDEX, amnt_idx); |
| 165 | |
| 166 | nla_nest_end(msg, tb1); |
| 167 | |
| 168 | nla_nest_end(msg, data); |
| 169 | |
| 170 | ret = unl_genl_request(&unl, msg, mt76_amnt_dump_cb, NULL); |
| 171 | if (ret) |
| 172 | fprintf(stderr, "nl80211 call failed: %s\n", strerror(-ret)); |
| 173 | |
| 174 | unl_free(&unl); |
| 175 | |
| 176 | return ret; |
developer | 6f8cffd | 2021-09-29 19:48:25 +0800 | [diff] [blame] | 177 | } |