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