6974280b74ff5a740f893f7d2b43809d1190137f
[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( wl_probe(ifname) )
27                 lua_pushstring(L, "wl");
28
29         else if( madwifi_probe(ifname) )
30                 lua_pushstring(L, "madwifi");
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_pushinteger(L, x);
96                         lua_newtable(L);
97
98                         lua_pushnumber(L, e->mw);
99                         lua_setfield(L, -2, "mw");
100                         
101                         lua_pushnumber(L, e->dbm);
102                         lua_setfield(L, -2, "dbm");
103
104                         lua_settable(L, -3);
105                 }
106         }
107
108         return 1;
109 }
110
111 /* Broadcom */
112 LUA_WRAP_INT(wl,channel)
113 LUA_WRAP_INT(wl,frequency)
114 LUA_WRAP_INT(wl,bitrate)
115 LUA_WRAP_INT(wl,signal)
116 LUA_WRAP_INT(wl,noise)
117 LUA_WRAP_INT(wl,quality)
118 LUA_WRAP_INT(wl,quality_max)
119 LUA_WRAP_INT(wl,mbssid_support)
120 LUA_WRAP_STRING(wl,mode)
121 LUA_WRAP_STRING(wl,ssid)
122 LUA_WRAP_STRING(wl,bssid)
123 LUA_WRAP_STRING(wl,enctype)
124 LUA_WRAP_ASSOCLIST(wl)
125 LUA_WRAP_TXPWRLIST(wl)
126
127 /* Madwifi */
128 LUA_WRAP_INT(madwifi,channel)
129 LUA_WRAP_INT(madwifi,frequency)
130 LUA_WRAP_INT(madwifi,bitrate)
131 LUA_WRAP_INT(madwifi,signal)
132 LUA_WRAP_INT(madwifi,noise)
133 LUA_WRAP_INT(madwifi,quality)
134 LUA_WRAP_INT(madwifi,quality_max)
135 LUA_WRAP_INT(madwifi,mbssid_support)
136 LUA_WRAP_STRING(madwifi,mode)
137 LUA_WRAP_STRING(madwifi,ssid)
138 LUA_WRAP_STRING(madwifi,bssid)
139 LUA_WRAP_STRING(madwifi,enctype)
140 LUA_WRAP_ASSOCLIST(madwifi)
141 LUA_WRAP_TXPWRLIST(madwifi)
142
143 /* Wext */
144 LUA_WRAP_INT(wext,channel)
145 LUA_WRAP_INT(wext,frequency)
146 LUA_WRAP_INT(wext,bitrate)
147 LUA_WRAP_INT(wext,signal)
148 LUA_WRAP_INT(wext,noise)
149 LUA_WRAP_INT(wext,quality)
150 LUA_WRAP_INT(wext,quality_max)
151 LUA_WRAP_INT(wext,mbssid_support)
152 LUA_WRAP_STRING(wext,mode)
153 LUA_WRAP_STRING(wext,ssid)
154 LUA_WRAP_STRING(wext,bssid)
155 LUA_WRAP_STRING(wext,enctype)
156 LUA_WRAP_ASSOCLIST(wext)
157 LUA_WRAP_TXPWRLIST(wext)
158
159 /* Broadcom table */
160 static const luaL_reg R_wl[] = {
161         LUA_REG(wl,channel),
162         LUA_REG(wl,frequency),
163         LUA_REG(wl,bitrate),
164         LUA_REG(wl,signal),
165         LUA_REG(wl,noise),
166         LUA_REG(wl,quality),
167         LUA_REG(wl,quality_max),
168         LUA_REG(wl,mode),
169         LUA_REG(wl,ssid),
170         LUA_REG(wl,bssid),
171         LUA_REG(wl,enctype),
172         LUA_REG(wl,assoclist),
173         LUA_REG(wl,txpwrlist),
174         LUA_REG(wl,mbssid_support),
175         { NULL, NULL }
176 };
177
178 /* Madwifi table */
179 static const luaL_reg R_madwifi[] = {
180         LUA_REG(madwifi,channel),
181         LUA_REG(madwifi,frequency),
182         LUA_REG(madwifi,bitrate),
183         LUA_REG(madwifi,signal),
184         LUA_REG(madwifi,noise),
185         LUA_REG(madwifi,quality),
186         LUA_REG(madwifi,quality_max),
187         LUA_REG(madwifi,mode),
188         LUA_REG(madwifi,ssid),
189         LUA_REG(madwifi,bssid),
190         LUA_REG(madwifi,enctype),
191         LUA_REG(madwifi,assoclist),
192         LUA_REG(madwifi,txpwrlist),
193         LUA_REG(madwifi,mbssid_support),
194         { NULL, NULL }
195 };
196
197 /* Wext table */
198 static const luaL_reg R_wext[] = {
199         LUA_REG(wext,channel),
200         LUA_REG(wext,frequency),
201         LUA_REG(wext,bitrate),
202         LUA_REG(wext,signal),
203         LUA_REG(wext,noise),
204         LUA_REG(wext,quality),
205         LUA_REG(wext,quality_max),
206         LUA_REG(wext,mode),
207         LUA_REG(wext,ssid),
208         LUA_REG(wext,bssid),
209         LUA_REG(wext,enctype),
210         LUA_REG(wext,assoclist),
211         LUA_REG(wext,txpwrlist),
212         LUA_REG(wext,mbssid_support),
213         { NULL, NULL }
214 };
215
216 /* Common */
217 static const luaL_reg R_common[] = {
218         { "type", iwinfo_L_type },
219         { NULL, NULL }
220 };
221
222
223 LUALIB_API int luaopen_iwinfo(lua_State *L) {
224         luaL_register(L, IWINFO_META, R_common);
225
226         luaL_newmetatable(L, IWINFO_WL_META);
227         luaL_register(L, NULL, R_wl);
228         lua_pushvalue(L, -1);
229         lua_setfield(L, -2, "__index");
230         lua_setfield(L, -2, "wl");
231
232         luaL_newmetatable(L, IWINFO_MADWIFI_META);
233         luaL_register(L, NULL, R_madwifi);
234         lua_pushvalue(L, -1);
235         lua_setfield(L, -2, "__index");
236         lua_setfield(L, -2, "madwifi");
237
238         luaL_newmetatable(L, IWINFO_WEXT_META);
239         luaL_register(L, NULL, R_wext);
240         lua_pushvalue(L, -1);
241         lua_setfield(L, -2, "__index");
242         lua_setfield(L, -2, "wext");
243
244         return 1;
245 }
246