2 * iwinfo - Wireless Information Library - Lua Bindings
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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/.
19 #include "iwinfo_lualib.h"
22 static int iwinfo_L_type(lua_State *L)
24 const char *ifname = luaL_checkstring(L, 1);
26 if( wl_probe(ifname) )
27 lua_pushstring(L, "wl");
29 else if( madwifi_probe(ifname) )
30 lua_pushstring(L, "madwifi");
32 else if( wext_probe(ifname) )
33 lua_pushstring(L, "wext");
41 /* Wrapper for assoclist */
42 static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *))
45 char rv[IWINFO_BUFSIZE];
47 const char *ifname = luaL_checkstring(L, 1);
48 struct iwinfo_assoclist_entry *e;
51 memset(rv, 0, sizeof(rv));
53 if( !(*func)(ifname, rv, &len) )
55 for( i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry) )
57 e = (struct iwinfo_assoclist_entry *) &rv[i];
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]);
65 lua_pushnumber(L, e->signal);
66 lua_setfield(L, -2, "signal");
68 lua_pushnumber(L, e->noise);
69 lua_setfield(L, -2, "noise");
71 lua_setfield(L, -2, macstr);
78 /* Wrapper for tx power list */
79 static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *))
82 char rv[IWINFO_BUFSIZE];
83 const char *ifname = luaL_checkstring(L, 1);
84 struct iwinfo_txpwrlist_entry *e;
87 memset(rv, 0, sizeof(rv));
89 if( !(*func)(ifname, rv, &len) )
91 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++ )
93 e = (struct iwinfo_txpwrlist_entry *) &rv[i];
97 lua_pushnumber(L, e->mw);
98 lua_setfield(L, -2, "mw");
100 lua_pushnumber(L, e->dbm);
101 lua_setfield(L, -2, "dbm");
103 lua_rawseti(L, -2, x);
111 LUA_WRAP_INT(wl,channel)
112 LUA_WRAP_INT(wl,frequency)
113 LUA_WRAP_INT(wl,bitrate)
114 LUA_WRAP_INT(wl,signal)
115 LUA_WRAP_INT(wl,noise)
116 LUA_WRAP_INT(wl,quality)
117 LUA_WRAP_INT(wl,quality_max)
118 LUA_WRAP_INT(wl,mbssid_support)
119 LUA_WRAP_STRING(wl,mode)
120 LUA_WRAP_STRING(wl,ssid)
121 LUA_WRAP_STRING(wl,bssid)
122 LUA_WRAP_STRING(wl,enctype)
123 LUA_WRAP_LIST(wl,assoclist)
124 LUA_WRAP_LIST(wl,txpwrlist)
127 LUA_WRAP_INT(madwifi,channel)
128 LUA_WRAP_INT(madwifi,frequency)
129 LUA_WRAP_INT(madwifi,bitrate)
130 LUA_WRAP_INT(madwifi,signal)
131 LUA_WRAP_INT(madwifi,noise)
132 LUA_WRAP_INT(madwifi,quality)
133 LUA_WRAP_INT(madwifi,quality_max)
134 LUA_WRAP_INT(madwifi,mbssid_support)
135 LUA_WRAP_STRING(madwifi,mode)
136 LUA_WRAP_STRING(madwifi,ssid)
137 LUA_WRAP_STRING(madwifi,bssid)
138 LUA_WRAP_STRING(madwifi,enctype)
139 LUA_WRAP_LIST(madwifi,assoclist)
140 LUA_WRAP_LIST(madwifi,txpwrlist)
143 LUA_WRAP_INT(wext,channel)
144 LUA_WRAP_INT(wext,frequency)
145 LUA_WRAP_INT(wext,bitrate)
146 LUA_WRAP_INT(wext,signal)
147 LUA_WRAP_INT(wext,noise)
148 LUA_WRAP_INT(wext,quality)
149 LUA_WRAP_INT(wext,quality_max)
150 LUA_WRAP_INT(wext,mbssid_support)
151 LUA_WRAP_STRING(wext,mode)
152 LUA_WRAP_STRING(wext,ssid)
153 LUA_WRAP_STRING(wext,bssid)
154 LUA_WRAP_STRING(wext,enctype)
155 LUA_WRAP_LIST(wext,assoclist)
156 LUA_WRAP_LIST(wext,txpwrlist)
159 static const luaL_reg R_wl[] = {
161 LUA_REG(wl,frequency),
166 LUA_REG(wl,quality_max),
171 LUA_REG(wl,assoclist),
172 LUA_REG(wl,txpwrlist),
173 LUA_REG(wl,mbssid_support),
178 static const luaL_reg R_madwifi[] = {
179 LUA_REG(madwifi,channel),
180 LUA_REG(madwifi,frequency),
181 LUA_REG(madwifi,bitrate),
182 LUA_REG(madwifi,signal),
183 LUA_REG(madwifi,noise),
184 LUA_REG(madwifi,quality),
185 LUA_REG(madwifi,quality_max),
186 LUA_REG(madwifi,mode),
187 LUA_REG(madwifi,ssid),
188 LUA_REG(madwifi,bssid),
189 LUA_REG(madwifi,enctype),
190 LUA_REG(madwifi,assoclist),
191 LUA_REG(madwifi,txpwrlist),
192 LUA_REG(madwifi,mbssid_support),
197 static const luaL_reg R_wext[] = {
198 LUA_REG(wext,channel),
199 LUA_REG(wext,frequency),
200 LUA_REG(wext,bitrate),
201 LUA_REG(wext,signal),
203 LUA_REG(wext,quality),
204 LUA_REG(wext,quality_max),
208 LUA_REG(wext,enctype),
209 LUA_REG(wext,assoclist),
210 LUA_REG(wext,txpwrlist),
211 LUA_REG(wext,mbssid_support),
216 static const luaL_reg R_common[] = {
217 { "type", iwinfo_L_type },
222 LUALIB_API int luaopen_iwinfo(lua_State *L) {
223 luaL_register(L, IWINFO_META, R_common);
225 luaL_newmetatable(L, IWINFO_WL_META);
226 luaL_register(L, NULL, R_wl);
227 lua_pushvalue(L, -1);
228 lua_setfield(L, -2, "__index");
229 lua_setfield(L, -2, "wl");
231 luaL_newmetatable(L, IWINFO_MADWIFI_META);
232 luaL_register(L, NULL, R_madwifi);
233 lua_pushvalue(L, -1);
234 lua_setfield(L, -2, "__index");
235 lua_setfield(L, -2, "madwifi");
237 luaL_newmetatable(L, IWINFO_WEXT_META);
238 luaL_register(L, NULL, R_wext);
239 lua_pushvalue(L, -1);
240 lua_setfield(L, -2, "__index");
241 lua_setfield(L, -2, "wext");