libs: remove cbi folder
[project/luci.git] / libs / iwinfo / src / iwinfo_lualib.c
1 /*
2  * iwinfo - Wireless Information Library - Lua Bindings
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
19 #include "iwinfo_lualib.h"
20
21 /* Determine type */
22 static int iwinfo_L_type(lua_State *L)
23 {
24         const char *ifname = luaL_checkstring(L, 1);
25
26 #ifdef USE_MADWIFI
27         if( madwifi_probe(ifname) )
28                 lua_pushstring(L, "madwifi");
29         else
30 #endif
31
32 #ifdef USE_WL
33         if( wl_probe(ifname) )
34                 lua_pushstring(L, "wl");
35         else
36 #endif
37
38         if( wext_probe(ifname) )
39                 lua_pushstring(L, "wext");
40
41         else
42                 lua_pushnil(L);
43
44         return 1;
45 }
46
47 /* Wrapper for assoclist */
48 static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *))
49 {
50         int i, len;
51         char rv[IWINFO_BUFSIZE];
52         char macstr[18];
53         const char *ifname = luaL_checkstring(L, 1);
54         struct iwinfo_assoclist_entry *e;
55
56         lua_newtable(L);
57         memset(rv, 0, sizeof(rv));
58
59         if( !(*func)(ifname, rv, &len) )
60         {
61                 for( i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry) )
62                 {
63                         e = (struct iwinfo_assoclist_entry *) &rv[i];
64
65                         sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
66                                 e->mac[0], e->mac[1], e->mac[2],
67                                 e->mac[3], e->mac[4], e->mac[5]);
68
69                         lua_newtable(L);
70
71                         lua_pushnumber(L, e->signal);
72                         lua_setfield(L, -2, "signal");
73                         
74                         lua_pushnumber(L, e->noise);
75                         lua_setfield(L, -2, "noise");
76
77                         lua_setfield(L, -2, macstr);
78                 }
79         }
80
81         return 1;
82 }
83
84 /* Wrapper for tx power list */
85 static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *))
86 {
87         int i, x, len;
88         char rv[IWINFO_BUFSIZE];
89         const char *ifname = luaL_checkstring(L, 1);
90         struct iwinfo_txpwrlist_entry *e;
91
92         lua_newtable(L);
93         memset(rv, 0, sizeof(rv));
94
95         if( !(*func)(ifname, rv, &len) )
96         {
97                 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++ )
98                 {
99                         e = (struct iwinfo_txpwrlist_entry *) &rv[i];
100
101                         lua_newtable(L);
102
103                         lua_pushnumber(L, e->mw);
104                         lua_setfield(L, -2, "mw");
105                         
106                         lua_pushnumber(L, e->dbm);
107                         lua_setfield(L, -2, "dbm");
108
109                         lua_rawseti(L, -2, x);
110                 }
111         }
112
113         return 1;
114 }
115
116 /* Wrapper for scan list */
117 static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *))
118 {
119         int i, j, x, y, len;
120         char rv[IWINFO_BUFSIZE];
121         char macstr[18];
122         const char *ifname = luaL_checkstring(L, 1);
123         struct iwinfo_scanlist_entry *e;
124
125         lua_newtable(L);
126         memset(rv, 0, sizeof(rv));
127
128         if( !(*func)(ifname, rv, &len) )
129         {
130                 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++ )
131                 {
132                         e = (struct iwinfo_scanlist_entry *) &rv[i];
133
134                         lua_newtable(L);
135
136                         /* BSSID */
137                         sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
138                                 e->mac[0], e->mac[1], e->mac[2],
139                                 e->mac[3], e->mac[4], e->mac[5]);
140
141                         lua_pushstring(L, macstr);
142                         lua_setfield(L, -2, "bssid");
143
144                         /* ESSID */
145                         if( e->ssid[0] )
146                         {
147                                 lua_pushstring(L, (char *) e->ssid);
148                                 lua_setfield(L, -2, "ssid");
149                         }
150
151                         /* Channel */
152                         lua_pushinteger(L, e->channel);
153                         lua_setfield(L, -2, "channel");
154
155                         /* Mode */
156                         lua_pushstring(L, (char *) e->mode);
157                         lua_setfield(L, -2, "mode");
158
159                         /* Quality, Signal */
160                         lua_pushinteger(L, e->quality);
161                         lua_setfield(L, -2, "quality");
162
163                         lua_pushinteger(L, e->quality_max);
164                         lua_setfield(L, -2, "quality_max");
165
166                         lua_pushnumber(L, (e->signal - 0x100));
167                         lua_setfield(L, -2, "signal");
168
169                         /* Crypto */
170                         lua_pushboolean(L, (!e->crypto.wpa_version && e->crypto.enabled));
171                         lua_setfield(L, -2, "wep");
172
173                         if( e->crypto.wpa_version )
174                         {
175                                 lua_pushinteger(L, e->crypto.wpa_version);
176                                 lua_setfield(L, -2, "wpa");
177
178                                 lua_newtable(L);
179                                 for( j = 0, y = 1; j < IW_IE_CYPHER_NUM; j++ )
180                                 {
181                                         if( e->crypto.group_ciphers & (1<<j) )
182                                         {
183                                                 lua_pushstring(L, iw_ie_cypher_name[j]);
184                                                 lua_rawseti(L, -2, y++);
185                                         }
186                                 }
187                                 lua_setfield(L, -2, "group_ciphers");
188
189                                 lua_newtable(L);
190                                 for( j = 0, y = 1; j < IW_IE_CYPHER_NUM; j++ )
191                                 {
192                                         if( e->crypto.pair_ciphers & (1<<j) )
193                                         {
194                                                 lua_pushstring(L, iw_ie_cypher_name[j]);
195                                                 lua_rawseti(L, -2, y++);
196                                         }
197                                 }
198                                 lua_setfield(L, -2, "pair_ciphers");
199
200                                 lua_newtable(L);
201                                 for( j = 0, y = 1; j < IW_IE_KEY_MGMT_NUM; j++ )
202                                 {
203                                         if( e->crypto.auth_suites & (1<<j) )
204                                         {
205                                                 lua_pushstring(L, iw_ie_key_mgmt_name[j]);
206                                                 lua_rawseti(L, -2, y++);
207                                         }
208                                 }
209                                 lua_setfield(L, -2, "auth_suites");
210                         }
211
212                         lua_rawseti(L, -2, x);
213                 }
214         }
215
216         return 1;
217 }
218
219 /* Wrapper for frequency list */
220 static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *))
221 {
222         int i, x, len;
223         char rv[IWINFO_BUFSIZE];
224         const char *ifname = luaL_checkstring(L, 1);
225         struct iwinfo_freqlist_entry *e;
226
227         lua_newtable(L);
228         memset(rv, 0, sizeof(rv));
229
230         if( !(*func)(ifname, rv, &len) )
231         {
232                 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_freqlist_entry), x++ )
233                 {
234                         e = (struct iwinfo_freqlist_entry *) &rv[i];
235
236                         lua_newtable(L);
237
238                         /* MHz */
239                         lua_pushinteger(L, e->mhz);
240                         lua_setfield(L, -2, "mhz");
241
242                         /* Channel */
243                         lua_pushinteger(L, e->channel);
244                         lua_setfield(L, -2, "channel");
245
246                         lua_rawseti(L, -2, x);
247                 }
248         }
249
250         return 1;
251 }
252
253 #ifdef USE_WL
254 /* Broadcom */
255 LUA_WRAP_INT(wl,channel)
256 LUA_WRAP_INT(wl,frequency)
257 LUA_WRAP_INT(wl,bitrate)
258 LUA_WRAP_INT(wl,signal)
259 LUA_WRAP_INT(wl,noise)
260 LUA_WRAP_INT(wl,quality)
261 LUA_WRAP_INT(wl,quality_max)
262 LUA_WRAP_INT(wl,mbssid_support)
263 LUA_WRAP_STRING(wl,mode)
264 LUA_WRAP_STRING(wl,ssid)
265 LUA_WRAP_STRING(wl,bssid)
266 LUA_WRAP_STRING(wl,enctype)
267 LUA_WRAP_LIST(wl,assoclist)
268 LUA_WRAP_LIST(wl,txpwrlist)
269 LUA_WRAP_LIST(wl,scanlist)
270 LUA_WRAP_LIST(wl,freqlist)
271 #endif
272
273 #ifdef USE_MADWIFI
274 /* Madwifi */
275 LUA_WRAP_INT(madwifi,channel)
276 LUA_WRAP_INT(madwifi,frequency)
277 LUA_WRAP_INT(madwifi,bitrate)
278 LUA_WRAP_INT(madwifi,signal)
279 LUA_WRAP_INT(madwifi,noise)
280 LUA_WRAP_INT(madwifi,quality)
281 LUA_WRAP_INT(madwifi,quality_max)
282 LUA_WRAP_INT(madwifi,mbssid_support)
283 LUA_WRAP_STRING(madwifi,mode)
284 LUA_WRAP_STRING(madwifi,ssid)
285 LUA_WRAP_STRING(madwifi,bssid)
286 LUA_WRAP_STRING(madwifi,enctype)
287 LUA_WRAP_LIST(madwifi,assoclist)
288 LUA_WRAP_LIST(madwifi,txpwrlist)
289 LUA_WRAP_LIST(madwifi,scanlist)
290 LUA_WRAP_LIST(madwifi,freqlist)
291 #endif
292
293 /* Wext */
294 LUA_WRAP_INT(wext,channel)
295 LUA_WRAP_INT(wext,frequency)
296 LUA_WRAP_INT(wext,bitrate)
297 LUA_WRAP_INT(wext,signal)
298 LUA_WRAP_INT(wext,noise)
299 LUA_WRAP_INT(wext,quality)
300 LUA_WRAP_INT(wext,quality_max)
301 LUA_WRAP_INT(wext,mbssid_support)
302 LUA_WRAP_STRING(wext,mode)
303 LUA_WRAP_STRING(wext,ssid)
304 LUA_WRAP_STRING(wext,bssid)
305 LUA_WRAP_STRING(wext,enctype)
306 LUA_WRAP_LIST(wext,assoclist)
307 LUA_WRAP_LIST(wext,txpwrlist)
308 LUA_WRAP_LIST(wext,scanlist)
309 LUA_WRAP_LIST(wext,freqlist)
310
311 #ifdef USE_WL
312 /* Broadcom table */
313 static const luaL_reg R_wl[] = {
314         LUA_REG(wl,channel),
315         LUA_REG(wl,frequency),
316         LUA_REG(wl,bitrate),
317         LUA_REG(wl,signal),
318         LUA_REG(wl,noise),
319         LUA_REG(wl,quality),
320         LUA_REG(wl,quality_max),
321         LUA_REG(wl,mode),
322         LUA_REG(wl,ssid),
323         LUA_REG(wl,bssid),
324         LUA_REG(wl,enctype),
325         LUA_REG(wl,assoclist),
326         LUA_REG(wl,txpwrlist),
327         LUA_REG(wl,scanlist),
328         LUA_REG(wl,freqlist),
329         LUA_REG(wl,mbssid_support),
330         { NULL, NULL }
331 };
332 #endif
333
334 #ifdef USE_MADWIFI
335 /* Madwifi table */
336 static const luaL_reg R_madwifi[] = {
337         LUA_REG(madwifi,channel),
338         LUA_REG(madwifi,frequency),
339         LUA_REG(madwifi,bitrate),
340         LUA_REG(madwifi,signal),
341         LUA_REG(madwifi,noise),
342         LUA_REG(madwifi,quality),
343         LUA_REG(madwifi,quality_max),
344         LUA_REG(madwifi,mode),
345         LUA_REG(madwifi,ssid),
346         LUA_REG(madwifi,bssid),
347         LUA_REG(madwifi,enctype),
348         LUA_REG(madwifi,assoclist),
349         LUA_REG(madwifi,txpwrlist),
350         LUA_REG(madwifi,scanlist),
351         LUA_REG(madwifi,freqlist),
352         LUA_REG(madwifi,mbssid_support),
353         { NULL, NULL }
354 };
355 #endif
356
357 /* Wext table */
358 static const luaL_reg R_wext[] = {
359         LUA_REG(wext,channel),
360         LUA_REG(wext,frequency),
361         LUA_REG(wext,bitrate),
362         LUA_REG(wext,signal),
363         LUA_REG(wext,noise),
364         LUA_REG(wext,quality),
365         LUA_REG(wext,quality_max),
366         LUA_REG(wext,mode),
367         LUA_REG(wext,ssid),
368         LUA_REG(wext,bssid),
369         LUA_REG(wext,enctype),
370         LUA_REG(wext,assoclist),
371         LUA_REG(wext,txpwrlist),
372         LUA_REG(wext,scanlist),
373         LUA_REG(wext,freqlist),
374         LUA_REG(wext,mbssid_support),
375         { NULL, NULL }
376 };
377
378 /* Common */
379 static const luaL_reg R_common[] = {
380         { "type", iwinfo_L_type },
381         { NULL, NULL }
382 };
383
384
385 LUALIB_API int luaopen_iwinfo(lua_State *L) {
386         luaL_register(L, IWINFO_META, R_common);
387
388 #ifdef USE_WL
389         luaL_newmetatable(L, IWINFO_WL_META);
390         luaL_register(L, NULL, R_wl);
391         lua_pushvalue(L, -1);
392         lua_setfield(L, -2, "__index");
393         lua_setfield(L, -2, "wl");
394 #endif
395
396 #ifdef USE_MADWIFI
397         luaL_newmetatable(L, IWINFO_MADWIFI_META);
398         luaL_register(L, NULL, R_madwifi);
399         lua_pushvalue(L, -1);
400         lua_setfield(L, -2, "__index");
401         lua_setfield(L, -2, "madwifi");
402 #endif
403
404         luaL_newmetatable(L, IWINFO_WEXT_META);
405         luaL_register(L, NULL, R_wext);
406         lua_pushvalue(L, -1);
407         lua_setfield(L, -2, "__index");
408         lua_setfield(L, -2, "wext");
409
410         return 1;
411 }
412