bdf924a03e1b4891d500cced6a1c6b412f2d05aa
[project/ubus.git] / lua / ubus.c
1 /*
2  * Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
3  * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <unistd.h>
16 #include <libubus.h>
17 #include <libubox/blobmsg.h>
18 #include <libubox/blobmsg_json.h>
19 #include <lauxlib.h>
20
21
22 #define MODNAME        "ubus"
23 #define METANAME       MODNAME ".meta"
24
25 static lua_State *state;
26
27 struct ubus_lua_connection {
28         int timeout;
29         struct blob_buf buf;
30         struct ubus_context *ctx;
31 };
32
33 struct ubus_lua_object {
34         struct ubus_object o;
35         int r;
36 };
37
38 static int
39 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table);
40
41 static int
42 ubus_lua_parse_blob_array(lua_State *L, struct blob_attr *attr, int len, bool table)
43 {
44         int rv;
45         int idx = 1;
46         int rem = len;
47         struct blob_attr *pos;
48
49         lua_newtable(L);
50
51         __blob_for_each_attr(pos, attr, rem)
52         {
53                 rv = ubus_lua_parse_blob(L, pos, table);
54
55                 if (rv > 1)
56                         lua_rawset(L, -3);
57                 else if (rv > 0)
58                         lua_rawseti(L, -2, idx++);
59         }
60
61         return 1;
62 }
63
64 static int
65 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table)
66 {
67         int len;
68         int off = 0;
69         void *data;
70         char buf[32];
71
72         if (!blobmsg_check_attr(attr, false))
73                 return 0;
74
75         if (table && blobmsg_name(attr)[0])
76         {
77                 lua_pushstring(L, blobmsg_name(attr));
78                 off++;
79         }
80
81         data = blobmsg_data(attr);
82         len = blobmsg_data_len(attr);
83
84         switch (blob_id(attr))
85         {
86         case BLOBMSG_TYPE_BOOL:
87                 lua_pushboolean(L, *(uint8_t *)data);
88                 break;
89
90         case BLOBMSG_TYPE_INT16:
91                 lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
92                 break;
93
94         case BLOBMSG_TYPE_INT32:
95                 lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
96                 break;
97
98         case BLOBMSG_TYPE_INT64:
99                 /* NB: Lua cannot handle 64bit, format value as string and push that */
100                 sprintf(buf, "%lld", (long long int) be64_to_cpu(*(uint64_t *)data));
101                 lua_pushstring(L, buf);
102                 break;
103
104         case BLOBMSG_TYPE_STRING:
105                 lua_pushstring(L, data);
106                 break;
107
108         case BLOBMSG_TYPE_ARRAY:
109                 ubus_lua_parse_blob_array(L, data, len, false);
110                 break;
111
112         case BLOBMSG_TYPE_TABLE:
113                 ubus_lua_parse_blob_array(L, data, len, true);
114                 break;
115
116         default:
117                 lua_pushnil(L);
118                 break;
119         }
120
121         return off + 1;
122 }
123
124
125 static bool
126 ubus_lua_format_blob_is_array(lua_State *L)
127 {
128         lua_Integer prv = 0;
129         lua_Integer cur = 0;
130
131         /* Find out whether table is array-like */
132         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
133         {
134 #ifdef LUA_TINT
135                 if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
136 #else
137                 if (lua_type(L, -2) != LUA_TNUMBER)
138 #endif
139                 {
140                         lua_pop(L, 2);
141                         return false;
142                 }
143
144                 cur = lua_tointeger(L, -2);
145
146                 if ((cur - 1) != prv)
147                 {
148                         lua_pop(L, 2);
149                         return false;
150                 }
151
152                 prv = cur;
153         }
154
155         lua_pop(L, 1);
156         return true;
157 }
158
159 static int
160 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
161
162 static int
163 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
164 {
165         void *c;
166         bool rv = true;
167         const char *key = table ? lua_tostring(L, -2) : NULL;
168
169         switch (lua_type(L, -1))
170         {
171         case LUA_TBOOLEAN:
172                 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
173                 break;
174
175 #ifdef LUA_TINT
176         case LUA_TINT:
177 #endif
178         case LUA_TNUMBER:
179                 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
180                 break;
181
182         case LUA_TSTRING:
183         case LUA_TUSERDATA:
184         case LUA_TLIGHTUSERDATA:
185                 blobmsg_add_string(b, key, lua_tostring(L, -1));
186                 break;
187
188         case LUA_TTABLE:
189                 if (ubus_lua_format_blob_is_array(L))
190                 {
191                         c = blobmsg_open_array(b, key);
192                         rv = ubus_lua_format_blob_array(L, b, false);
193                         blobmsg_close_array(b, c);
194                 }
195                 else
196                 {
197                         c = blobmsg_open_table(b, key);
198                         rv = ubus_lua_format_blob_array(L, b, true);
199                         blobmsg_close_table(b, c);
200                 }
201                 break;
202
203         default:
204                 rv = false;
205                 break;
206         }
207
208         return rv;
209 }
210
211 static int
212 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
213 {
214         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
215         {
216                 if (!ubus_lua_format_blob(L, b, table))
217                 {
218                         lua_pop(L, 1);
219                         return false;
220                 }
221         }
222
223         return true;
224 }
225
226
227 static int
228 ubus_lua_connect(lua_State *L)
229 {
230         struct ubus_lua_connection *c;
231         const char *sockpath = luaL_optstring(L, 1, NULL);
232         int timeout = luaL_optint(L, 2, 30);
233
234         if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
235                 (c->ctx = ubus_connect(sockpath)) != NULL)
236         {
237                 ubus_add_uloop(c->ctx);
238                 c->timeout = timeout;
239                 memset(&c->buf, 0, sizeof(c->buf));
240                 luaL_getmetatable(L, METANAME);
241                 lua_setmetatable(L, -2);
242                 return 1;
243         }
244
245         /* NB: no errors from ubus_connect() yet */
246         lua_pushnil(L);
247         lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
248         return 2;
249 }
250
251
252 static void
253 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
254 {
255         lua_State *L = (lua_State *)p;
256
257         lua_pushstring(L, o->path);
258         lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
259 }
260
261 static int
262 ubus_lua_objects(lua_State *L)
263 {
264         int rv;
265         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
266
267         lua_newtable(L);
268         rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
269
270         if (rv != UBUS_STATUS_OK)
271         {
272                 lua_pop(L, 1);
273                 lua_pushnil(L);
274                 lua_pushinteger(L, rv);
275                 return 2;
276         }
277
278         return 1;
279 }
280
281 static int
282 ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
283                 struct ubus_request_data *req, const char *method,
284                 struct blob_attr *msg)
285 {
286         struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
287
288         lua_getglobal(state, "__ubus_cb");
289         lua_rawgeti(state, -1, o->r);
290         lua_getfield(state, -1, method);
291
292         if (lua_isfunction(state, -1)) {
293                 lua_pushlightuserdata(state, req);
294                 if (!msg)
295                         lua_pushnil(state);
296                 else
297                         ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
298                 lua_call(state, 2, 0);
299         }
300         return 0;
301 }
302
303 static int lua_gettablelen(lua_State *L, int index)
304 {
305         int cnt = 0;
306
307         lua_pushnil(L);
308         index -= 1;
309         while (lua_next(L, index) != 0) {
310                 cnt++;
311                 lua_pop(L, 1);
312         }
313
314         return cnt;
315 }
316
317 static int ubus_lua_reply(lua_State *L)
318 {
319         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
320         struct ubus_request_data *req;
321
322         luaL_checktype(L, 3, LUA_TTABLE);
323         blob_buf_init(&c->buf, 0);
324
325         if (!ubus_lua_format_blob_array(L, &c->buf, true))
326         {
327                 lua_pushnil(L);
328                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
329                 return 2;
330         }
331
332         req = lua_touserdata(L, 2);
333         ubus_send_reply(c->ctx, req, c->buf.head);
334
335         return 0;
336 }
337
338 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
339 {
340         struct blobmsg_policy *p;
341         int plen;
342         int pidx = 0;
343
344         /* get the function pointer */
345         lua_pushinteger(L, 1);
346         lua_gettable(L, -2);
347
348         /* get the policy table */
349         lua_pushinteger(L, 2);
350         lua_gettable(L, -3);
351         plen = lua_gettablelen(L, -1);
352
353         /* check if the method table is valid */
354         if ((lua_type(L, -2) != LUA_TFUNCTION) ||
355                         (lua_type(L, -1) != LUA_TTABLE) ||
356                         lua_objlen(L, -1) || !plen) {
357                 lua_pop(L, 2);
358                 return 1;
359         }
360
361         /* store function pointer */
362         lua_pushvalue(L, -2);
363         lua_setfield(L, -6, lua_tostring(L, -5));
364
365         /* setup the policy pointers */
366         p = malloc(sizeof(struct blobmsg_policy) * plen);
367         memset(p, 0, sizeof(struct blobmsg_policy) * plen);
368         m->policy = p;
369         lua_pushnil(L);
370         while (lua_next(L, -2) != 0) {
371                 int val = lua_tointeger(L, -1);
372
373                 /* check if the policy is valid */
374                 if ((lua_type(L, -2) != LUA_TSTRING) ||
375                                 (lua_type(L, -1) != LUA_TNUMBER) ||
376                                 (val < 0) ||
377                                 (val > BLOBMSG_TYPE_LAST)) {
378                         lua_pop(L, 1);
379                         continue;
380                 }
381                 p[pidx].name = lua_tostring(L, -2);
382                 p[pidx].type = val;
383                 lua_pop(L, 1);
384                 pidx++;
385         }
386
387         m->n_policy = pidx;
388         m->name = lua_tostring(L, -4);
389         m->handler = ubus_method_handler;
390         lua_pop(L, 2);
391
392         return 0;
393 }
394
395 static struct ubus_object* ubus_lua_load_object(lua_State *L)
396 {
397         struct ubus_lua_object *obj = NULL;
398         int mlen = lua_gettablelen(L, -1);
399         struct ubus_method *m;
400         int midx = 0;
401
402         /* setup object pointers */
403         obj = malloc(sizeof(struct ubus_lua_object));
404         memset(obj, 0, sizeof(struct ubus_lua_object));
405         obj->o.name = lua_tostring(L, -2);
406
407         /* setup method pointers */
408         m = malloc(sizeof(struct ubus_method) * mlen);
409         memset(m, 0, sizeof(struct ubus_method) * mlen);
410         obj->o.methods = m;
411
412         /* setup type pointers */
413         obj->o.type = malloc(sizeof(struct ubus_object_type));
414         memset(obj->o.type, 0, sizeof(struct ubus_object_type));
415         obj->o.type->name = lua_tostring(L, -2);
416         obj->o.type->id = 0;
417         obj->o.type->methods = obj->o.methods;
418
419         /* create the he callback lookup table */
420         lua_createtable(L, 1, 0);
421         lua_getglobal(L, "__ubus_cb");
422         lua_pushvalue(L, -2);
423         obj->r = luaL_ref(L, -2);
424         lua_pop(L, 1);
425
426         /* scan each method */
427         lua_pushnil(L);
428         while (lua_next(L, -3) != 0) {
429                 /* check if it looks like a method */
430                 if ((lua_type(L, -2) != LUA_TSTRING) ||
431                                 (lua_type(L, -1) != LUA_TTABLE) ||
432                                 !lua_objlen(L, -1)) {
433                         lua_pop(L, 1);
434                         continue;
435                 }
436
437                 if (!ubus_lua_load_methods(L, &m[midx]))
438                         midx++;
439                 lua_pop(L, 1);
440         }
441
442         obj->o.type->n_methods = obj->o.n_methods = midx;
443
444         /* pop the callback table */
445         lua_pop(L, 1);
446
447         return &obj->o;
448 }
449
450 static int ubus_lua_add(lua_State *L)
451 {
452         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
453
454         /* verify top level object */
455         if (lua_istable(L, 1)) {
456                 lua_pushstring(L, "you need to pass a table");
457                 lua_error(L);
458                 return 0;
459         }
460
461         /* scan each object */
462         lua_pushnil(L);
463         while (lua_next(L, -2) != 0) {
464                 struct ubus_object *obj = NULL;
465
466                 /* check if the object has a table of methods */
467                 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
468                         obj = ubus_lua_load_object(L);
469
470                         if (obj)
471                                 ubus_add_object(c->ctx, obj);
472                 }
473                 lua_pop(L, 1);
474         }
475
476         return 0;
477 }
478
479 static void
480 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
481 {
482         lua_State *L = (lua_State *)p;
483
484         if (!o->signature)
485                 return;
486
487         ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
488 }
489
490 static int
491 ubus_lua_signatures(lua_State *L)
492 {
493         int rv;
494         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
495         const char *path = luaL_checkstring(L, 2);
496
497         rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
498
499         if (rv != UBUS_STATUS_OK)
500         {
501                 lua_pop(L, 1);
502                 lua_pushnil(L);
503                 lua_pushinteger(L, rv);
504                 return 2;
505         }
506
507         return 1;
508 }
509
510
511 static void
512 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
513 {
514         lua_State *L = (lua_State *)req->priv;
515
516         if (!msg)
517                 lua_pushnil(L);
518
519         ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
520 }
521
522 static int
523 ubus_lua_call(lua_State *L)
524 {
525         int rv, top;
526         uint32_t id;
527         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
528         const char *path = luaL_checkstring(L, 2);
529         const char *func = luaL_checkstring(L, 3);
530
531         luaL_checktype(L, 4, LUA_TTABLE);
532         blob_buf_init(&c->buf, 0);
533
534         if (!ubus_lua_format_blob_array(L, &c->buf, true))
535         {
536                 lua_pushnil(L);
537                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
538                 return 2;
539         }
540
541         rv = ubus_lookup_id(c->ctx, path, &id);
542
543         if (rv)
544         {
545                 lua_pushnil(L);
546                 lua_pushinteger(L, rv);
547                 return 2;
548         }
549
550         top = lua_gettop(L);
551         rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
552
553         if (rv != UBUS_STATUS_OK)
554         {
555                 lua_pop(L, 1);
556                 lua_pushnil(L);
557                 lua_pushinteger(L, rv);
558                 return 2;
559         }
560
561         return lua_gettop(L) - top;
562 }
563
564
565 static int
566 ubus_lua__gc(lua_State *L)
567 {
568         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
569
570         if (c->ctx != NULL)
571         {
572                 ubus_free(c->ctx);
573                 memset(c, 0, sizeof(*c));
574         }
575
576         return 0;
577 }
578
579 static const luaL_Reg ubus[] = {
580         { "connect", ubus_lua_connect },
581         { "objects", ubus_lua_objects },
582         { "add", ubus_lua_add },
583         { "reply", ubus_lua_reply },
584         { "signatures", ubus_lua_signatures },
585         { "call", ubus_lua_call },
586         { "close", ubus_lua__gc },
587         { "__gc", ubus_lua__gc },
588         { NULL, NULL },
589 };
590
591 /* avoid missing prototype warning */
592 int luaopen_ubus(lua_State *L);
593
594 int
595 luaopen_ubus(lua_State *L)
596 {
597         /* create metatable */
598         luaL_newmetatable(L, METANAME);
599
600         /* metatable.__index = metatable */
601         lua_pushvalue(L, -1);
602         lua_setfield(L, -2, "__index");
603
604         /* fill metatable */
605         luaL_register(L, NULL, ubus);
606         lua_pop(L, 1);
607
608         /* create module */
609         luaL_register(L, MODNAME, ubus);
610
611         /* set some enum defines */
612         lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
613         lua_setfield(L, -2, "ARRAY");
614         lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
615         lua_setfield(L, -2, "TABLE");
616         lua_pushinteger(L, BLOBMSG_TYPE_STRING);
617         lua_setfield(L, -2, "STRING");
618         lua_pushinteger(L, BLOBMSG_TYPE_INT64);
619         lua_setfield(L, -2, "INT64");
620         lua_pushinteger(L, BLOBMSG_TYPE_INT32);
621         lua_setfield(L, -2, "INT32");
622         lua_pushinteger(L, BLOBMSG_TYPE_INT16);
623         lua_setfield(L, -2, "INT16");
624         lua_pushinteger(L, BLOBMSG_TYPE_INT8);
625         lua_setfield(L, -2, "INT8");
626         lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
627         lua_setfield(L, -2, "BOOLEAN");
628
629         /* used in our callbacks */
630         state = L;
631
632         /* create the callback table */
633         lua_createtable(L, 1, 0);
634         lua_setglobal(L, "__ubus_cb");
635
636         return 0;
637 }