lua: allow building the lua binding with vanilla lua
[project/ubus.git] / lua / ubus.c
1 /*
2  * Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <unistd.h>
15 #include <libubus.h>
16 #include <libubox/blobmsg.h>
17 #include <lauxlib.h>
18
19
20 #define MODNAME        "ubus"
21 #define METANAME       MODNAME ".meta"
22
23
24 struct ubus_lua_connection {
25         int timeout;
26         struct blob_buf buf;
27         struct ubus_context *ctx;
28 };
29
30
31 static int
32 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table);
33
34 static int
35 ubus_lua_parse_blob_array(lua_State *L, struct blob_attr *attr, int len, bool table)
36 {
37         int rv;
38         int idx = 1;
39         int rem = len;
40         struct blob_attr *pos;
41
42         lua_newtable(L);
43
44         __blob_for_each_attr(pos, attr, rem)
45         {
46                 rv = ubus_lua_parse_blob(L, pos, table);
47
48                 if (rv > 1)
49                         lua_rawset(L, -3);
50                 else if (rv > 0)
51                         lua_rawseti(L, -2, idx++);
52         }
53
54         return 1;
55 }
56
57 static int
58 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table)
59 {
60         int len;
61         int off = 0;
62         void *data;
63         char buf[32];
64
65         if (!blobmsg_check_attr(attr, false))
66                 return 0;
67
68         if (table && blobmsg_name(attr)[0])
69         {
70                 lua_pushstring(L, blobmsg_name(attr));
71                 off++;
72         }
73
74         data = blobmsg_data(attr);
75         len = blobmsg_data_len(attr);
76
77         switch (blob_id(attr))
78         {
79         case BLOBMSG_TYPE_BOOL:
80                 lua_pushboolean(L, *(uint8_t *)data);
81                 break;
82
83         case BLOBMSG_TYPE_INT16:
84                 lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
85                 break;
86
87         case BLOBMSG_TYPE_INT32:
88                 lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
89                 break;
90
91         case BLOBMSG_TYPE_INT64:
92                 /* NB: Lua cannot handle 64bit, format value as string and push that */
93                 sprintf(buf, "%lld", (long long int) be64_to_cpu(*(uint64_t *)data));
94                 lua_pushstring(L, buf);
95                 break;
96
97         case BLOBMSG_TYPE_STRING:
98                 lua_pushstring(L, data);
99                 break;
100
101         case BLOBMSG_TYPE_ARRAY:
102                 ubus_lua_parse_blob_array(L, data, len, false);
103                 break;
104
105         case BLOBMSG_TYPE_TABLE:
106                 ubus_lua_parse_blob_array(L, data, len, true);
107                 break;
108
109         default:
110                 lua_pushnil(L);
111                 break;
112         }
113
114         return off + 1;
115 }
116
117
118 static bool
119 ubus_lua_format_blob_is_array(lua_State *L)
120 {
121         lua_Integer prv = 0;
122         lua_Integer cur = 0;
123
124         /* Find out whether table is array-like */
125         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
126         {
127 #ifdef LUA_TINT
128                 if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
129 #else
130                 if (lua_type(L, -2) != LUA_TNUMBER)
131 #endif
132                 {
133                         lua_pop(L, 1);
134                         return false;
135                 }
136
137                 cur = lua_tointeger(L, -2);
138
139                 if ((cur - 1) != prv)
140                 {
141                         lua_pop(L, 1);
142                         return false;
143                 }
144
145                 prv = cur;
146         }
147
148         return true;
149 }
150
151 static int
152 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
153
154 static int
155 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
156 {
157         void *c;
158         bool rv = true;
159         const char *key = table ? lua_tostring(L, -2) : NULL;
160
161         switch (lua_type(L, -1))
162         {
163         case LUA_TBOOLEAN:
164                 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
165                 break;
166
167 #ifdef LUA_TINT
168         case LUA_TINT:
169 #endif
170         case LUA_TNUMBER:
171                 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
172                 break;
173
174         case LUA_TSTRING:
175         case LUA_TUSERDATA:
176         case LUA_TLIGHTUSERDATA:
177                 blobmsg_add_string(b, key, lua_tostring(L, -1));
178                 break;
179
180         case LUA_TTABLE:
181                 if (ubus_lua_format_blob_is_array(L))
182                 {
183                         c = blobmsg_open_array(b, key);
184                         rv = ubus_lua_format_blob_array(L, b, false);
185                         blobmsg_close_array(b, c);
186                 }
187                 else
188                 {
189                         c = blobmsg_open_table(b, key);
190                         rv = ubus_lua_format_blob_array(L, b, true);
191                         blobmsg_close_table(b, c);
192                 }
193                 break;
194
195         default:
196                 rv = false;
197                 break;
198         }
199
200         return rv;
201 }
202
203 static int
204 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
205 {
206         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
207         {
208                 if (!ubus_lua_format_blob(L, b, table))
209                 {
210                         lua_pop(L, 1);
211                         return false;
212                 }
213         }
214
215         return true;
216 }
217
218
219 static int
220 ubus_lua_connect(lua_State *L)
221 {
222         struct ubus_lua_connection *c;
223         const char *sockpath = luaL_optstring(L, 1, NULL);
224         int timeout = luaL_optint(L, 2, 30);
225
226         if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
227                 (c->ctx = ubus_connect(sockpath)) != NULL)
228         {
229                 c->timeout = timeout;
230                 memset(&c->buf, 0, sizeof(c->buf));
231                 luaL_getmetatable(L, METANAME);
232                 lua_setmetatable(L, -2);
233                 return 1;
234         }
235
236         /* NB: no errors from ubus_connect() yet */
237         lua_pushnil(L);
238         lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
239         return 2;
240 }
241
242
243 static void
244 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
245 {
246         lua_State *L = (lua_State *)p;
247
248         lua_pushstring(L, o->path);
249         lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
250 }
251
252 static int
253 ubus_lua_objects(lua_State *L)
254 {
255         int rv;
256         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
257
258         lua_newtable(L);
259         rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
260
261         if (rv != UBUS_STATUS_OK)
262         {
263                 lua_pop(L, 1);
264                 lua_pushnil(L);
265                 lua_pushinteger(L, rv);
266                 return 2;
267         }
268
269         return 1;
270 }
271
272
273 static void
274 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
275 {
276         lua_State *L = (lua_State *)p;
277
278         if (!o->signature)
279                 return;
280
281         ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
282 }
283
284 static int
285 ubus_lua_signatures(lua_State *L)
286 {
287         int rv;
288         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
289         const char *path = luaL_checkstring(L, 2);
290
291         rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
292
293         if (rv != UBUS_STATUS_OK)
294         {
295                 lua_pop(L, 1);
296                 lua_pushnil(L);
297                 lua_pushinteger(L, rv);
298                 return 2;
299         }
300
301         return 1;
302 }
303
304
305 static void
306 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
307 {
308         lua_State *L = (lua_State *)req->priv;
309
310         if (!msg)
311                 lua_pushnil(L);
312
313         ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
314 }
315
316 static int
317 ubus_lua_call(lua_State *L)
318 {
319         int rv;
320         uint32_t id;
321         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
322         const char *path = luaL_checkstring(L, 2);
323         const char *func = luaL_checkstring(L, 3);
324
325         luaL_checktype(L, 4, LUA_TTABLE);
326         blob_buf_init(&c->buf, 0);
327
328         if (!ubus_lua_format_blob_array(L, &c->buf, true))
329         {
330                 lua_pushnil(L);
331                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
332                 return 2;
333         }
334
335         rv = ubus_lookup_id(c->ctx, path, &id);
336
337         if (rv)
338         {
339                 lua_pushnil(L);
340                 lua_pushinteger(L, rv);
341                 return 2;
342         }
343
344         rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
345
346         if (rv != UBUS_STATUS_OK)
347         {
348                 lua_pop(L, 1);
349                 lua_pushnil(L);
350                 lua_pushinteger(L, rv);
351                 return 2;
352         }
353
354         return 1;
355 }
356
357
358 static int
359 ubus_lua__gc(lua_State *L)
360 {
361         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
362
363         if (c->ctx != NULL)
364         {
365                 ubus_free(c->ctx);
366                 memset(c, 0, sizeof(*c));
367         }
368
369         return 0;
370 }
371
372 static const luaL_Reg ubus[] = {
373         { "connect", ubus_lua_connect },
374         { "objects", ubus_lua_objects },
375         { "signatures", ubus_lua_signatures },
376         { "call", ubus_lua_call },
377         { "close", ubus_lua__gc },
378         { "__gc", ubus_lua__gc },
379         { NULL, NULL },
380 };
381
382 /* avoid missing prototype warning */
383 int luaopen_ubus(lua_State *L);
384
385 int
386 luaopen_ubus(lua_State *L)
387 {
388         /* create metatable */
389         luaL_newmetatable(L, METANAME);
390
391         /* metatable.__index = metatable */
392         lua_pushvalue(L, -1);
393         lua_setfield(L, -2, "__index");
394
395         /* fill metatable */
396         luaL_register(L, NULL, ubus);
397         lua_pop(L, 1);
398
399         /* create module */
400         luaL_register(L, MODNAME, ubus);
401
402         return 0;
403 }