86e34b7c0739f6c568925bfb5feeb4d34ca0d570
[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 #include <lua.h>
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 struct ubus_lua_event {
39         struct ubus_event_handler e;
40         int r;
41 };
42
43 static int
44 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table);
45
46 static int
47 ubus_lua_parse_blob_array(lua_State *L, struct blob_attr *attr, int len, bool table)
48 {
49         int rv;
50         int idx = 1;
51         int rem = len;
52         struct blob_attr *pos;
53
54         lua_newtable(L);
55
56         __blob_for_each_attr(pos, attr, rem)
57         {
58                 rv = ubus_lua_parse_blob(L, pos, table);
59
60                 if (rv > 1)
61                         lua_rawset(L, -3);
62                 else if (rv > 0)
63                         lua_rawseti(L, -2, idx++);
64         }
65
66         return 1;
67 }
68
69 static int
70 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table)
71 {
72         int len;
73         int off = 0;
74         void *data;
75
76         if (!blobmsg_check_attr(attr, false))
77                 return 0;
78
79         if (table && blobmsg_name(attr)[0])
80         {
81                 lua_pushstring(L, blobmsg_name(attr));
82                 off++;
83         }
84
85         data = blobmsg_data(attr);
86         len = blobmsg_data_len(attr);
87
88         switch (blob_id(attr))
89         {
90         case BLOBMSG_TYPE_BOOL:
91                 lua_pushboolean(L, *(uint8_t *)data);
92                 break;
93
94         case BLOBMSG_TYPE_INT16:
95                 lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
96                 break;
97
98         case BLOBMSG_TYPE_INT32:
99                 lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
100                 break;
101
102         case BLOBMSG_TYPE_INT64:
103                 lua_pushnumber(L, (double) be64_to_cpu(*(uint64_t *)data));
104                 break;
105
106         case BLOBMSG_TYPE_STRING:
107                 lua_pushstring(L, data);
108                 break;
109
110         case BLOBMSG_TYPE_ARRAY:
111                 ubus_lua_parse_blob_array(L, data, len, false);
112                 break;
113
114         case BLOBMSG_TYPE_TABLE:
115                 ubus_lua_parse_blob_array(L, data, len, true);
116                 break;
117
118         default:
119                 lua_pushnil(L);
120                 break;
121         }
122
123         return off + 1;
124 }
125
126
127 static bool
128 ubus_lua_format_blob_is_array(lua_State *L)
129 {
130         lua_Integer prv = 0;
131         lua_Integer cur = 0;
132
133         /* Find out whether table is array-like */
134         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
135         {
136 #ifdef LUA_TINT
137                 if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
138 #else
139                 if (lua_type(L, -2) != LUA_TNUMBER)
140 #endif
141                 {
142                         lua_pop(L, 2);
143                         return false;
144                 }
145
146                 cur = lua_tointeger(L, -2);
147
148                 if ((cur - 1) != prv)
149                 {
150                         lua_pop(L, 2);
151                         return false;
152                 }
153
154                 prv = cur;
155         }
156
157         return true;
158 }
159
160 static int
161 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
162
163 static int
164 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
165 {
166         void *c;
167         bool rv = true;
168         const char *key = table ? lua_tostring(L, -2) : NULL;
169
170         switch (lua_type(L, -1))
171         {
172         case LUA_TBOOLEAN:
173                 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
174                 break;
175
176 #ifdef LUA_TINT
177         case LUA_TINT:
178 #endif
179         case LUA_TNUMBER:
180                 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
181                 break;
182
183         case LUA_TSTRING:
184         case LUA_TUSERDATA:
185         case LUA_TLIGHTUSERDATA:
186                 blobmsg_add_string(b, key, lua_tostring(L, -1));
187                 break;
188
189         case LUA_TTABLE:
190                 if (ubus_lua_format_blob_is_array(L))
191                 {
192                         c = blobmsg_open_array(b, key);
193                         rv = ubus_lua_format_blob_array(L, b, false);
194                         blobmsg_close_array(b, c);
195                 }
196                 else
197                 {
198                         c = blobmsg_open_table(b, key);
199                         rv = ubus_lua_format_blob_array(L, b, true);
200                         blobmsg_close_table(b, c);
201                 }
202                 break;
203
204         default:
205                 rv = false;
206                 break;
207         }
208
209         return rv;
210 }
211
212 static int
213 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
214 {
215         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
216         {
217                 if (!ubus_lua_format_blob(L, b, table))
218                 {
219                         lua_pop(L, 1);
220                         return false;
221                 }
222         }
223
224         return true;
225 }
226
227
228 static int
229 ubus_lua_connect(lua_State *L)
230 {
231         struct ubus_lua_connection *c;
232         const char *sockpath = luaL_optstring(L, 1, NULL);
233         int timeout = luaL_optint(L, 2, 30);
234
235         if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
236                 (c->ctx = ubus_connect(sockpath)) != NULL)
237         {
238                 ubus_add_uloop(c->ctx);
239                 c->timeout = timeout;
240                 memset(&c->buf, 0, sizeof(c->buf));
241                 luaL_getmetatable(L, METANAME);
242                 lua_setmetatable(L, -2);
243                 return 1;
244         }
245
246         /* NB: no errors from ubus_connect() yet */
247         lua_pushnil(L);
248         lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
249         return 2;
250 }
251
252
253 static void
254 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
255 {
256         lua_State *L = (lua_State *)p;
257
258         lua_pushstring(L, o->path);
259         lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
260 }
261
262 static int
263 ubus_lua_objects(lua_State *L)
264 {
265         int rv;
266         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
267
268         lua_newtable(L);
269         rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
270
271         if (rv != UBUS_STATUS_OK)
272         {
273                 lua_pop(L, 1);
274                 lua_pushnil(L);
275                 lua_pushinteger(L, rv);
276                 return 2;
277         }
278
279         return 1;
280 }
281
282 static int
283 ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
284                 struct ubus_request_data *req, const char *method,
285                 struct blob_attr *msg)
286 {
287         struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
288         int rv = 0;
289
290         lua_getglobal(state, "__ubus_cb");
291         lua_rawgeti(state, -1, o->r);
292         lua_getfield(state, -1, method);
293         lua_remove(state, -2);
294         lua_remove(state, -2);
295
296         if (lua_isfunction(state, -1)) {
297                 lua_pushlightuserdata(state, req);
298                 if (!msg)
299                         lua_pushnil(state);
300                 else
301                         ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
302                 lua_call(state, 2, 1);
303                 if (lua_isnumber(state, -1))
304                         rv = lua_tonumber(state, -1);
305         }
306
307         lua_pop(state, 1);
308
309         return rv;
310 }
311
312 static int lua_gettablelen(lua_State *L, int index)
313 {
314         int cnt = 0;
315
316         lua_pushnil(L);
317         index -= 1;
318         while (lua_next(L, index) != 0) {
319                 cnt++;
320                 lua_pop(L, 1);
321         }
322
323         return cnt;
324 }
325
326 static int ubus_lua_reply(lua_State *L)
327 {
328         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
329         struct ubus_request_data *req;
330
331         luaL_checktype(L, 3, LUA_TTABLE);
332         blob_buf_init(&c->buf, 0);
333
334         if (!ubus_lua_format_blob_array(L, &c->buf, true))
335         {
336                 lua_pushnil(L);
337                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
338                 return 2;
339         }
340
341         req = lua_touserdata(L, 2);
342         ubus_send_reply(c->ctx, req, c->buf.head);
343
344         return 0;
345 }
346
347 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
348 {
349         struct blobmsg_policy *p;
350         int plen;
351         int pidx = 0;
352
353         /* get the function pointer */
354         lua_pushinteger(L, 1);
355         lua_gettable(L, -2);
356
357         /* get the policy table */
358         lua_pushinteger(L, 2);
359         lua_gettable(L, -3);
360
361         /* check if the method table is valid */
362         if ((lua_type(L, -2) != LUA_TFUNCTION) ||
363                         (lua_type(L, -1) != LUA_TTABLE) ||
364                         lua_objlen(L, -1)) {
365                 lua_pop(L, 2);
366                 return 1;
367         }
368
369         /* store function pointer */
370         lua_pushvalue(L, -2);
371         lua_setfield(L, -6, lua_tostring(L, -5));
372
373         m->name = lua_tostring(L, -4);
374         m->handler = ubus_method_handler;
375
376         plen = lua_gettablelen(L, -1);
377
378         /* exit if policy table is empty */
379         if (!plen) {
380                 lua_pop(L, 2);
381                 return 0;
382         }
383
384         /* setup the policy pointers */
385         p = malloc(sizeof(struct blobmsg_policy) * plen);
386         if (!p)
387                 return 1;
388
389         memset(p, 0, sizeof(struct blobmsg_policy) * plen);
390         m->policy = p;
391         lua_pushnil(L);
392         while (lua_next(L, -2) != 0) {
393                 int val = lua_tointeger(L, -1);
394
395                 /* check if the policy is valid */
396                 if ((lua_type(L, -2) != LUA_TSTRING) ||
397                                 (lua_type(L, -1) != LUA_TNUMBER) ||
398                                 (val < 0) ||
399                                 (val > BLOBMSG_TYPE_LAST)) {
400                         lua_pop(L, 1);
401                         continue;
402                 }
403                 p[pidx].name = lua_tostring(L, -2);
404                 p[pidx].type = val;
405                 lua_pop(L, 1);
406                 pidx++;
407         }
408
409         m->n_policy = pidx;
410         lua_pop(L, 2);
411
412         return 0;
413 }
414
415 static struct ubus_object* ubus_lua_load_object(lua_State *L)
416 {
417         struct ubus_lua_object *obj = NULL;
418         int mlen = lua_gettablelen(L, -1);
419         struct ubus_method *m;
420         int midx = 0;
421
422         /* setup object pointers */
423         obj = malloc(sizeof(struct ubus_lua_object));
424         if (!obj)
425                 return NULL;
426
427         memset(obj, 0, sizeof(struct ubus_lua_object));
428         obj->o.name = lua_tostring(L, -2);
429
430         /* setup method pointers */
431         m = malloc(sizeof(struct ubus_method) * mlen);
432         memset(m, 0, sizeof(struct ubus_method) * mlen);
433         obj->o.methods = m;
434
435         /* setup type pointers */
436         obj->o.type = malloc(sizeof(struct ubus_object_type));
437         if (!obj->o.type) {
438                 free(obj);
439                 return NULL;
440         }
441
442         memset(obj->o.type, 0, sizeof(struct ubus_object_type));
443         obj->o.type->name = lua_tostring(L, -2);
444         obj->o.type->id = 0;
445         obj->o.type->methods = obj->o.methods;
446
447         /* create the callback lookup table */
448         lua_createtable(L, 1, 0);
449         lua_getglobal(L, "__ubus_cb");
450         lua_pushvalue(L, -2);
451         obj->r = luaL_ref(L, -2);
452         lua_pop(L, 1);
453
454         /* scan each method */
455         lua_pushnil(L);
456         while (lua_next(L, -3) != 0) {
457                 /* check if it looks like a method */
458                 if ((lua_type(L, -2) != LUA_TSTRING) ||
459                                 (lua_type(L, -1) != LUA_TTABLE) ||
460                                 !lua_objlen(L, -1)) {
461                         lua_pop(L, 1);
462                         continue;
463                 }
464
465                 if (!ubus_lua_load_methods(L, &m[midx]))
466                         midx++;
467                 lua_pop(L, 1);
468         }
469
470         obj->o.type->n_methods = obj->o.n_methods = midx;
471
472         /* pop the callback table */
473         lua_pop(L, 1);
474
475         return &obj->o;
476 }
477
478 static int ubus_lua_add(lua_State *L)
479 {
480         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
481
482         /* verify top level object */
483         if (lua_istable(L, 1)) {
484                 lua_pushstring(L, "you need to pass a table");
485                 lua_error(L);
486                 return 0;
487         }
488
489         /* scan each object */
490         lua_pushnil(L);
491         while (lua_next(L, -2) != 0) {
492                 struct ubus_object *obj = NULL;
493
494                 /* check if the object has a table of methods */
495                 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
496                         obj = ubus_lua_load_object(L);
497
498                         if (obj)
499                                 ubus_add_object(c->ctx, obj);
500                 }
501                 lua_pop(L, 1);
502         }
503
504         return 0;
505 }
506
507 static void
508 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
509 {
510         lua_State *L = (lua_State *)p;
511
512         if (!o->signature)
513                 return;
514
515         ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
516 }
517
518 static int
519 ubus_lua_signatures(lua_State *L)
520 {
521         int rv;
522         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
523         const char *path = luaL_checkstring(L, 2);
524
525         rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
526
527         if (rv != UBUS_STATUS_OK)
528         {
529                 lua_pop(L, 1);
530                 lua_pushnil(L);
531                 lua_pushinteger(L, rv);
532                 return 2;
533         }
534
535         return 1;
536 }
537
538
539 static void
540 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
541 {
542         lua_State *L = (lua_State *)req->priv;
543
544         if (!msg && L)
545                 lua_pushnil(L);
546
547         if (msg && L)
548                 ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
549 }
550
551 static int
552 ubus_lua_call(lua_State *L)
553 {
554         int rv, top;
555         uint32_t id;
556         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
557         const char *path = luaL_checkstring(L, 2);
558         const char *func = luaL_checkstring(L, 3);
559
560         luaL_checktype(L, 4, LUA_TTABLE);
561         blob_buf_init(&c->buf, 0);
562
563         if (!ubus_lua_format_blob_array(L, &c->buf, true))
564         {
565                 lua_pushnil(L);
566                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
567                 return 2;
568         }
569
570         rv = ubus_lookup_id(c->ctx, path, &id);
571
572         if (rv)
573         {
574                 lua_pushnil(L);
575                 lua_pushinteger(L, rv);
576                 return 2;
577         }
578
579         top = lua_gettop(L);
580         rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
581
582         if (rv != UBUS_STATUS_OK)
583         {
584                 lua_pop(L, 1);
585                 lua_pushnil(L);
586                 lua_pushinteger(L, rv);
587                 return 2;
588         }
589
590         return lua_gettop(L) - top;
591 }
592
593 static void
594 ubus_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
595                         const char *type, struct blob_attr *msg)
596 {
597         struct ubus_lua_event *listener = container_of(ev, struct ubus_lua_event, e);
598
599         lua_getglobal(state, "__ubus_cb_event");
600         lua_rawgeti(state, -1, listener->r);
601         lua_remove(state, -2);
602
603         if (lua_isfunction(state, -1)) {
604                 ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
605                 lua_call(state, 1, 0);
606         } else {
607                 lua_pop(state, 1);
608         }
609 }
610
611 static struct ubus_event_handler*
612 ubus_lua_load_event(lua_State *L)
613 {
614         struct ubus_lua_event* event = NULL;
615
616         event = malloc(sizeof(struct ubus_lua_event));
617         if (!event)
618                 return NULL;
619
620         memset(event, 0, sizeof(struct ubus_lua_event));
621         event->e.cb = ubus_event_handler;
622
623         /* update the he callback lookup table */
624         lua_getglobal(L, "__ubus_cb_event");
625         lua_pushvalue(L, -2);
626         event->r = luaL_ref(L, -2);
627         lua_setfield(L, -1, lua_tostring(L, -3));
628
629         return &event->e;
630 }
631
632 static int
633 ubus_lua_listen(lua_State *L) {
634         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
635
636         /* verify top level object */
637         luaL_checktype(L, 2, LUA_TTABLE);
638
639         /* scan each object */
640         lua_pushnil(L);
641         while (lua_next(L, -2) != 0) {
642                 struct ubus_event_handler *listener;
643
644                 /* check if the key is a string and the value is a method */
645                 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TFUNCTION)) {
646                         listener = ubus_lua_load_event(L);
647                         if(listener != NULL) {
648                                 ubus_register_event_handler(c->ctx, listener, lua_tostring(L, -2));
649                         }
650                 }
651                 lua_pop(L, 1);
652         }
653         return 0;
654 }
655
656 static int
657 ubus_lua_send(lua_State *L)
658 {
659         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
660         const char *event = luaL_checkstring(L, 2);
661
662         if (*event == 0)
663                 return luaL_argerror(L, 2, "no event name");
664
665         // Event content convert to ubus form
666         luaL_checktype(L, 3, LUA_TTABLE);
667         blob_buf_init(&c->buf, 0);
668
669         if (!ubus_lua_format_blob_array(L, &c->buf, true)) {
670                 lua_pushnil(L);
671                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
672                 return 2;
673         }
674
675         // Send the event
676         ubus_send_event(c->ctx, event, c->buf.head);
677
678         return 0;
679 }
680
681
682
683 static int
684 ubus_lua__gc(lua_State *L)
685 {
686         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
687
688         blob_buf_free(&c->buf);
689         if (c->ctx != NULL)
690         {
691                 ubus_free(c->ctx);
692                 memset(c, 0, sizeof(*c));
693         }
694
695         return 0;
696 }
697
698 static const luaL_Reg ubus[] = {
699         { "connect", ubus_lua_connect },
700         { "objects", ubus_lua_objects },
701         { "add", ubus_lua_add },
702         { "reply", ubus_lua_reply },
703         { "signatures", ubus_lua_signatures },
704         { "call", ubus_lua_call },
705         { "close", ubus_lua__gc },
706         { "listen", ubus_lua_listen },
707         { "send", ubus_lua_send },
708         { "__gc", ubus_lua__gc },
709         { NULL, NULL },
710 };
711
712 /* avoid missing prototype warning */
713 int luaopen_ubus(lua_State *L);
714
715 int
716 luaopen_ubus(lua_State *L)
717 {
718         /* create metatable */
719         luaL_newmetatable(L, METANAME);
720
721         /* metatable.__index = metatable */
722         lua_pushvalue(L, -1);
723         lua_setfield(L, -2, "__index");
724
725         /* fill metatable */
726         luaL_register(L, NULL, ubus);
727         lua_pop(L, 1);
728
729         /* create module */
730         luaL_register(L, MODNAME, ubus);
731
732         /* set some enum defines */
733         lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
734         lua_setfield(L, -2, "ARRAY");
735         lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
736         lua_setfield(L, -2, "TABLE");
737         lua_pushinteger(L, BLOBMSG_TYPE_STRING);
738         lua_setfield(L, -2, "STRING");
739         lua_pushinteger(L, BLOBMSG_TYPE_INT64);
740         lua_setfield(L, -2, "INT64");
741         lua_pushinteger(L, BLOBMSG_TYPE_INT32);
742         lua_setfield(L, -2, "INT32");
743         lua_pushinteger(L, BLOBMSG_TYPE_INT16);
744         lua_setfield(L, -2, "INT16");
745         lua_pushinteger(L, BLOBMSG_TYPE_INT8);
746         lua_setfield(L, -2, "INT8");
747         lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
748         lua_setfield(L, -2, "BOOLEAN");
749
750         /* used in our callbacks */
751         state = L;
752
753         /* create the callback table */
754         lua_createtable(L, 1, 0);
755         lua_setglobal(L, "__ubus_cb");
756
757         /* create the event table */
758         lua_createtable(L, 1, 0);
759         lua_setglobal(L, "__ubus_cb_event");
760
761         return 0;
762 }