]> git.ipfire.org Git - thirdparty/iw.git/blob - util.c
Merge branch 'master' into scan-test
[thirdparty/iw.git] / util.c
1 #include "nl80211.h"
2 #include "iw.h"
3
4 int mac_addr_n2a(char *mac_addr, unsigned char *arg)
5 {
6 int i, l;
7
8 l = 0;
9 for (i = 0; i < ETH_ALEN ; i++) {
10 if (i == 0) {
11 sprintf(mac_addr+l, "%02x", arg[i]);
12 l += 2;
13 } else {
14 sprintf(mac_addr+l, ":%02x", arg[i]);
15 l += 3;
16 }
17 }
18 return 0;
19 }
20
21 int mac_addr_a2n(unsigned char *mac_addr, char *arg)
22 {
23 int i;
24
25 for (i = 0; i < ETH_ALEN ; i++) {
26 int temp;
27 char *cp = strchr(arg, ':');
28 if (cp) {
29 *cp = 0;
30 cp++;
31 }
32 if (sscanf(arg, "%x", &temp) != 1)
33 return -1;
34 if (temp < 0 || temp > 255)
35 return -1;
36
37 mac_addr[i] = temp;
38 if (!cp)
39 break;
40 arg = cp;
41 }
42 if (i < ETH_ALEN - 1)
43 return -1;
44
45 return 0;
46 }
47
48 static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
49 "unspecified",
50 "IBSS",
51 "Station",
52 "AP",
53 "AP(VLAN)",
54 "WDS",
55 "Monitor",
56 "mesh point"
57 };
58
59 static char modebuf[100];
60
61 const char *iftype_name(enum nl80211_iftype iftype)
62 {
63 if (iftype <= NL80211_IFTYPE_MAX)
64 return ifmodes[iftype];
65 sprintf(modebuf, "Unknown mode (%d)", iftype);
66 return modebuf;
67 }
68
69 int ieee80211_channel_to_frequency(int chan)
70 {
71 if (chan < 14)
72 return 2407 + chan * 5;
73
74 if (chan == 14)
75 return 2484;
76
77 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
78 return (chan + 1000) * 5;
79 }
80
81 int ieee80211_frequency_to_channel(int freq)
82 {
83 if (freq == 2484)
84 return 14;
85
86 if (freq < 2484)
87 return (freq - 2407) / 5;
88
89 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
90 return freq/5 - 1000;
91 }