]> git.ipfire.org Git - thirdparty/iw.git/blob - phy.c
show channels and allow setting frequency by channel
[thirdparty/iw.git] / phy.c
1 #include <stdbool.h>
2 #include <errno.h>
3 #include <net/if.h>
4
5 #include <netlink/genl/genl.h>
6 #include <netlink/genl/family.h>
7 #include <netlink/genl/ctrl.h>
8 #include <netlink/msg.h>
9 #include <netlink/attr.h>
10
11 #include "nl80211.h"
12 #include "iw.h"
13
14 static int handle_name(struct nl_cb *cb,
15 struct nl_msg *msg,
16 int argc, char **argv)
17 {
18 if (argc != 1)
19 return 1;
20
21 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, *argv);
22
23 return 0;
24 nla_put_failure:
25 return -ENOBUFS;
26 }
27 COMMAND(set, name, "<new name>", NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_name);
28
29 static int handle_freqchan(struct nl_msg *msg, bool chan,
30 int argc, char **argv)
31 {
32 static const struct {
33 const char *name;
34 unsigned int val;
35 } htmap[] = {
36 { .name = "HT20", .val = NL80211_SEC_CHAN_DISABLED, },
37 { .name = "HT40+", .val = NL80211_SEC_CHAN_ABOVE, },
38 { .name = "HT40-", .val = NL80211_SEC_CHAN_BELOW, },
39 };
40 unsigned int htval = NL80211_SEC_CHAN_NO_HT;
41 unsigned int freq;
42 int i;
43
44 if (!argc || argc > 2)
45 return 1;
46
47 if (argc == 2) {
48 for (i = 0; i < sizeof(htmap)/sizeof(htmap[0]); i++) {
49 if (strcasecmp(htmap[i].name, argv[1]) == 0) {
50 htval = htmap[i].val;
51 break;
52 }
53 }
54 if (htval == NL80211_SEC_CHAN_NO_HT)
55 return 1;
56 }
57
58 freq = strtoul(argv[0], NULL, 10);
59 if (chan)
60 freq = ieee80211_channel_to_frequency(freq);
61
62 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
63 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_SEC_CHAN_OFFSET, htval);
64
65 return 0;
66 nla_put_failure:
67 return -ENOBUFS;
68 }
69
70 static int handle_freq(struct nl_cb *cb, struct nl_msg *msg,
71 int argc, char **argv)
72 {
73 return handle_freqchan(msg, false, argc, argv);
74 }
75 COMMAND(set, freq, "<freq> [HT20|HT40+|HT40-]",
76 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_freq);
77 COMMAND(set, freq, "<freq> [HT20|HT40+|HT40-]",
78 NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_freq);
79
80 static int handle_chan(struct nl_cb *cb, struct nl_msg *msg,
81 int argc, char **argv)
82 {
83 return handle_freqchan(msg, true, argc, argv);
84 }
85 COMMAND(set, channel, "<channel> [HT20|HT40+|HT40-]",
86 NL80211_CMD_SET_WIPHY, 0, CIB_PHY, handle_chan);
87 COMMAND(set, channel, "<channel> [HT20|HT40+|HT40-]",
88 NL80211_CMD_SET_WIPHY, 0, CIB_NETDEV, handle_chan);