libs/iwinfo: implement *_get_txpwrlist() to obtain valid tx power levels
[project/luci.git] / libs / iwinfo / src / iwinfo_wext.c
1 /*
2  * iwinfo - Wireless Information Library - Linux Wireless Extension Backend
3  *
4  *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  * The iwinfo library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * The iwinfo library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
17  *
18  * Parts of this code are derived from the Linux wireless tools, iwlib.c,
19  * iwlist.c and iwconfig.c in particular.
20  */
21
22 #include "iwinfo.h"
23 #include "iwinfo_wext.h"
24
25 #define LOG10_MAGIC     1.25892541179
26
27 static int ioctl_socket = -1;
28
29 static double wext_freq2float(const struct iw_freq *in)
30 {
31         int             i;
32         double  res = (double) in->m;
33         for(i = 0; i < in->e; i++) res *= 10;
34         return res;
35 }
36
37 static int wext_freq2mhz(const struct iw_freq *in)
38 {
39         int i, mhz;
40
41         if( in->e == 6 )
42         {
43                 return in->m;
44         }
45         else
46         {
47                 mhz = in->m;
48                 for(i = 0; i < in->e; i++)
49                         mhz *= 10;
50
51                 return (int)(mhz / 100000);
52         }
53 }
54
55 static int wext_dbm2mw(int in)
56 {
57         double res = 1.0;
58         int ip = in / 10;
59         int fp = in % 10;
60         int k;
61
62         for(k = 0; k < ip; k++) res *= 10;
63         for(k = 0; k < fp; k++) res *= LOG10_MAGIC;
64
65         return (int)res;
66 }
67
68 static int wext_mw2dbm(int in)
69 {
70         double fin = (double) in;
71         int res = 0;
72
73         while(fin > 10.0)
74         {
75                 res += 10;
76                 fin /= 10.0;
77         }
78
79         while(fin > 1.000001)
80         {
81                 res += 1;
82                 fin /= LOG10_MAGIC;
83         }
84
85         return res;
86 }
87
88 static int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
89 {
90         /* prepare socket */
91         if( ioctl_socket == -1 )
92                 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
93
94         strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
95         return ioctl(ioctl_socket, cmd, wrq);
96 }
97
98
99 int wext_probe(const char *ifname)
100 {
101         struct iwreq wrq;
102
103         if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
104                 return 1;
105
106         return 0;
107 }
108
109 int wext_get_mode(const char *ifname, char *buf)
110 {
111         struct iwreq wrq;
112
113         if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
114         {
115                 switch(wrq.u.mode)
116                 {
117                         case 0:
118                                 sprintf(buf, "Auto");
119                                 break;
120
121                         case 1:
122                                 sprintf(buf, "Ad-Hoc");
123                                 break;
124
125                         case 2:
126                                 sprintf(buf, "Client");
127                                 break;
128
129                         case 3:
130                                 sprintf(buf, "Master");
131                                 break;
132
133                         case 4:
134                                 sprintf(buf, "Repeater");
135                                 break;
136
137                         case 5:
138                                 sprintf(buf, "Secondary");
139                                 break;
140
141                         case 6:
142                                 sprintf(buf, "Monitor");
143                                 break;
144
145                         default:
146                                 sprintf(buf, "Unknown");
147                 }
148
149                 return 0;
150         }
151
152         return -1;
153 }
154
155 int wext_get_ssid(const char *ifname, char *buf)
156 {
157         struct iwreq wrq;
158
159         wrq.u.essid.pointer = (caddr_t) buf;
160         wrq.u.essid.length  = IW_ESSID_MAX_SIZE + 1;
161         wrq.u.essid.flags   = 0;
162
163         if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
164                 return 0;
165
166         return -1;
167 }
168
169 int wext_get_bssid(const char *ifname, char *buf)
170 {
171         struct iwreq wrq;
172
173         if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
174         {
175                 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
176                         (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
177                         (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
178                         (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
179
180                 return 0;
181         }
182
183         return -1;      
184 }
185
186 int wext_get_bitrate(const char *ifname, int *buf)
187 {
188         struct iwreq wrq;
189
190         if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
191         {
192                 *buf = wrq.u.bitrate.value;
193                 return 0;
194         }
195
196         return -1;      
197 }
198
199 int wext_get_channel(const char *ifname, int *buf)
200 {
201         struct iwreq wrq;
202
203         if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
204         {
205                 /* FIXME: iwlib has some strange workarounds here, maybe we need them as well... */
206                 *buf = (int) wext_freq2float(&wrq.u.freq);
207                 return 0;
208         }
209
210         return -1;      
211 }
212
213 int wext_get_frequency(const char *ifname, int *buf)
214 {
215         struct iwreq wrq;
216         struct iw_range range;
217         int i, channel;
218
219         if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
220         {
221                 /* We got a channel number instead ... */
222                 if( wrq.u.freq.m < 1000 )
223                 {
224                         channel = wrq.u.freq.m;
225                         wrq.u.data.pointer = (caddr_t) &range;
226                         wrq.u.data.length  = sizeof(struct iw_range);
227                         wrq.u.data.flags   = 0;
228
229                         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
230                         {
231                                 for(i = 0; i < range.num_frequency; i++)
232                                 {
233                                         if( range.freq[i].i == channel )
234                                         {
235                                                 *buf = wext_freq2mhz(&range.freq[i]);
236                                                 return 0;
237                                         }
238                                 }
239                         }
240                 }
241                 else
242                 {
243                         *buf = wext_freq2mhz(&wrq.u.freq);
244                         return 0;
245                 }
246         }
247
248         return -1;      
249 }
250
251 int wext_get_signal(const char *ifname, int *buf)
252 {
253         struct iwreq wrq;
254         struct iw_statistics stats;
255
256         wrq.u.data.pointer = (caddr_t) &stats;
257         wrq.u.data.length  = sizeof(struct iw_statistics);
258         wrq.u.data.flags   = 1;
259
260         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
261         {
262                 *buf = (stats.qual.level - 0x100);
263                 return 0;
264         }
265
266         return -1;
267 }
268
269 int wext_get_noise(const char *ifname, int *buf)
270 {
271         struct iwreq wrq;
272         struct iw_statistics stats;
273
274         wrq.u.data.pointer = (caddr_t) &stats;
275         wrq.u.data.length  = sizeof(struct iw_statistics);
276         wrq.u.data.flags   = 1;
277
278         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
279         {
280                 *buf = (stats.qual.noise - 0x100);
281                 return 0;
282         }
283
284         return -1;
285 }
286
287 int wext_get_quality(const char *ifname, int *buf)
288 {
289         struct iwreq wrq;
290         struct iw_statistics stats;
291
292         wrq.u.data.pointer = (caddr_t) &stats;
293         wrq.u.data.length  = sizeof(struct iw_statistics);
294         wrq.u.data.flags   = 1;
295
296         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
297         {
298                 *buf = stats.qual.qual;
299                 return 0;
300         }
301
302         return -1;
303 }
304
305 int wext_get_quality_max(const char *ifname, int *buf)
306 {
307         struct iwreq wrq;
308         struct iw_range range;
309
310         wrq.u.data.pointer = (caddr_t) &range;
311         wrq.u.data.length  = sizeof(struct iw_range);
312         wrq.u.data.flags   = 0;
313
314         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
315         {
316                 *buf = range.max_qual.qual;
317                 return 0;
318         }
319
320         return -1;
321 }
322
323 int wext_get_enctype(const char *ifname, char *buf)
324 {
325         /* Stub */
326         return -1;
327 }
328
329 int wext_get_assoclist(const char *ifname, char *buf, int *len)
330 {
331         /* Stub */
332         return -1;
333 }
334
335 int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
336 {
337         struct iwreq wrq;
338         struct iw_range range;
339         struct iwinfo_txpwrlist_entry entry;
340         int i;
341
342         wrq.u.data.pointer = (caddr_t) &range;
343         wrq.u.data.length  = sizeof(struct iw_range);
344         wrq.u.data.flags   = 0;
345
346         if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
347             (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
348             !(range.txpower_capa & IW_TXPOW_RELATIVE)
349         ) {
350                 for( i = 0; i < range.num_txpower; i++ )
351                 {
352                         if( range.txpower_capa & IW_TXPOW_MWATT )
353                         {
354                                 entry.dbm = wext_mw2dbm(range.txpower[i]);
355                                 entry.mw  = range.txpower[i];
356                         }
357                         else
358                         {
359                                 entry.dbm = range.txpower[i];
360                                 entry.mw  = wext_dbm2mw(range.txpower[i]);
361                         }
362
363                         memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
364                 }
365
366                 *len = i * sizeof(entry);
367                 return 0;
368         }
369
370         return -1;      
371 }
372
373 int wext_get_mbssid_support(const char *ifname, int *buf)
374 {
375         /* No multi bssid support atm */
376         return -1;
377 }
378