ubus: add notification for subscribers present/gone
[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         return true;
156 }
157
158 static int
159 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
160
161 static int
162 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
163 {
164         void *c;
165         bool rv = true;
166         const char *key = table ? lua_tostring(L, -2) : NULL;
167
168         switch (lua_type(L, -1))
169         {
170         case LUA_TBOOLEAN:
171                 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
172                 break;
173
174 #ifdef LUA_TINT
175         case LUA_TINT:
176 #endif
177         case LUA_TNUMBER:
178                 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
179                 break;
180
181         case LUA_TSTRING:
182         case LUA_TUSERDATA:
183         case LUA_TLIGHTUSERDATA:
184                 blobmsg_add_string(b, key, lua_tostring(L, -1));
185                 break;
186
187         case LUA_TTABLE:
188                 if (ubus_lua_format_blob_is_array(L))
189                 {
190                         c = blobmsg_open_array(b, key);
191                         rv = ubus_lua_format_blob_array(L, b, false);
192                         blobmsg_close_array(b, c);
193                 }
194                 else
195                 {
196                         c = blobmsg_open_table(b, key);
197                         rv = ubus_lua_format_blob_array(L, b, true);
198                         blobmsg_close_table(b, c);
199                 }
200                 break;
201
202         default:
203                 rv = false;
204                 break;
205         }
206
207         return rv;
208 }
209
210 static int
211 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
212 {
213         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
214         {
215                 if (!ubus_lua_format_blob(L, b, table))
216                 {
217                         lua_pop(L, 1);
218                         return false;
219                 }
220         }
221
222         return true;
223 }
224
225
226 static int
227 ubus_lua_connect(lua_State *L)
228 {
229         struct ubus_lua_connection *c;
230         const char *sockpath = luaL_optstring(L, 1, NULL);
231         int timeout = luaL_optint(L, 2, 30);
232
233         if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
234                 (c->ctx = ubus_connect(sockpath)) != NULL)
235         {
236                 ubus_add_uloop(c->ctx);
237                 c->timeout = timeout;
238                 memset(&c->buf, 0, sizeof(c->buf));
239                 luaL_getmetatable(L, METANAME);
240                 lua_setmetatable(L, -2);
241                 return 1;
242         }
243
244         /* NB: no errors from ubus_connect() yet */
245         lua_pushnil(L);
246         lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
247         return 2;
248 }
249
250
251 static void
252 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
253 {
254         lua_State *L = (lua_State *)p;
255
256         lua_pushstring(L, o->path);
257         lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
258 }
259
260 static int
261 ubus_lua_objects(lua_State *L)
262 {
263         int rv;
264         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
265
266         lua_newtable(L);
267         rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
268
269         if (rv != UBUS_STATUS_OK)
270         {
271                 lua_pop(L, 1);
272                 lua_pushnil(L);
273                 lua_pushinteger(L, rv);
274                 return 2;
275         }
276
277         return 1;
278 }
279
280 static int
281 ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
282                 struct ubus_request_data *req, const char *method,
283                 struct blob_attr *msg)
284 {
285         struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
286
287         lua_getglobal(state, "__ubus_cb");
288         lua_rawgeti(state, -1, o->r);
289         lua_getfield(state, -1, method);
290
291         if (lua_isfunction(state, -1)) {
292                 lua_pushlightuserdata(state, req);
293                 if (!msg)
294                         lua_pushnil(state);
295                 else
296                         ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
297                 lua_call(state, 2, 0);
298         }
299         return 0;
300 }
301
302 static int lua_gettablelen(lua_State *L, int index)
303 {
304         int cnt = 0;
305
306         lua_pushnil(L);
307         index -= 1;
308         while (lua_next(L, index) != 0) {
309                 cnt++;
310                 lua_pop(L, 1);
311         }
312
313         return cnt;
314 }
315
316 static int ubus_lua_reply(lua_State *L)
317 {
318         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
319         struct ubus_request_data *req;
320
321         luaL_checktype(L, 3, LUA_TTABLE);
322         blob_buf_init(&c->buf, 0);
323
324         if (!ubus_lua_format_blob_array(L, &c->buf, true))
325         {
326                 lua_pushnil(L);
327                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
328                 return 2;
329         }
330
331         req = lua_touserdata(L, 2);
332         ubus_send_reply(c->ctx, req, c->buf.head);
333
334         return 0;
335 }
336
337 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
338 {
339         struct blobmsg_policy *p;
340         int plen;
341         int pidx = 0;
342
343         /* get the function pointer */
344         lua_pushinteger(L, 1);
345         lua_gettable(L, -2);
346
347         /* get the policy table */
348         lua_pushinteger(L, 2);
349         lua_gettable(L, -3);
350         plen = lua_gettablelen(L, -1);
351
352         /* check if the method table is valid */
353         if ((lua_type(L, -2) != LUA_TFUNCTION) ||
354                         (lua_type(L, -1) != LUA_TTABLE) ||
355                         lua_objlen(L, -1) || !plen) {
356                 lua_pop(L, 2);
357                 return 1;
358         }
359
360         /* store function pointer */
361         lua_pushvalue(L, -2);
362         lua_setfield(L, -6, lua_tostring(L, -5));
363
364         /* setup the policy pointers */
365         p = malloc(sizeof(struct blobmsg_policy) * plen);
366         memset(p, 0, sizeof(struct blobmsg_policy) * plen);
367         m->policy = p;
368         lua_pushnil(L);
369         while (lua_next(L, -2) != 0) {
370                 int val = lua_tointeger(L, -1);
371
372                 /* check if the policy is valid */
373                 if ((lua_type(L, -2) != LUA_TSTRING) ||
374                                 (lua_type(L, -1) != LUA_TNUMBER) ||
375                                 (val < 0) ||
376                                 (val > BLOBMSG_TYPE_LAST)) {
377                         lua_pop(L, 1);
378                         continue;
379                 }
380                 p[pidx].name = lua_tostring(L, -2);
381                 p[pidx].type = val;
382                 lua_pop(L, 1);
383                 pidx++;
384         }
385
386         m->n_policy = pidx;
387         m->name = lua_tostring(L, -4);
388         m->handler = ubus_method_handler;
389         lua_pop(L, 2);
390
391         return 0;
392 }
393
394 static struct ubus_object* ubus_lua_load_object(lua_State *L)
395 {
396         struct ubus_lua_object *obj = NULL;
397         int mlen = lua_gettablelen(L, -1);
398         struct ubus_method *m;
399         int midx = 0;
400
401         /* setup object pointers */
402         obj = malloc(sizeof(struct ubus_lua_object));
403         memset(obj, 0, sizeof(struct ubus_lua_object));
404         obj->o.name = lua_tostring(L, -2);
405
406         /* setup method pointers */
407         m = malloc(sizeof(struct ubus_method) * mlen);
408         memset(m, 0, sizeof(struct ubus_method) * mlen);
409         obj->o.methods = m;
410
411         /* setup type pointers */
412         obj->o.type = malloc(sizeof(struct ubus_object_type));
413         memset(obj->o.type, 0, sizeof(struct ubus_object_type));
414         obj->o.type->name = lua_tostring(L, -2);
415         obj->o.type->id = 0;
416         obj->o.type->methods = obj->o.methods;
417
418         /* create the he callback lookup table */
419         lua_createtable(L, 1, 0);
420         lua_getglobal(L, "__ubus_cb");
421         lua_pushvalue(L, -2);
422         obj->r = luaL_ref(L, -2);
423         lua_pop(L, 1);
424
425         /* scan each method */
426         lua_pushnil(L);
427         while (lua_next(L, -3) != 0) {
428                 /* check if it looks like a method */
429                 if ((lua_type(L, -2) != LUA_TSTRING) ||
430                                 (lua_type(L, -1) != LUA_TTABLE) ||
431                                 !lua_objlen(L, -1)) {
432                         lua_pop(L, 1);
433                         continue;
434                 }
435
436                 if (!ubus_lua_load_methods(L, &m[midx]))
437                         midx++;
438                 lua_pop(L, 1);
439         }
440
441         obj->o.type->n_methods = obj->o.n_methods = midx;
442
443         /* pop the callback table */
444         lua_pop(L, 1);
445
446         return &obj->o;
447 }
448
449 static int ubus_lua_add(lua_State *L)
450 {
451         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
452
453         /* verify top level object */
454         if (lua_istable(L, 1)) {
455                 lua_pushstring(L, "you need to pass a table");
456                 lua_error(L);
457                 return 0;
458         }
459
460         /* scan each object */
461         lua_pushnil(L);
462         while (lua_next(L, -2) != 0) {
463                 struct ubus_object *obj = NULL;
464
465                 /* check if the object has a table of methods */
466                 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
467                         obj = ubus_lua_load_object(L);
468
469                         if (obj)
470                                 ubus_add_object(c->ctx, obj);
471                 }
472                 lua_pop(L, 1);
473         }
474
475         return 0;
476 }
477
478 static void
479 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
480 {
481         lua_State *L = (lua_State *)p;
482
483         if (!o->signature)
484                 return;
485
486         ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
487 }
488
489 static int
490 ubus_lua_signatures(lua_State *L)
491 {
492         int rv;
493         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
494         const char *path = luaL_checkstring(L, 2);
495
496         rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
497
498         if (rv != UBUS_STATUS_OK)
499         {
500                 lua_pop(L, 1);
501                 lua_pushnil(L);
502                 lua_pushinteger(L, rv);
503                 return 2;
504         }
505
506         return 1;
507 }
508
509
510 static void
511 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
512 {
513         lua_State *L = (lua_State *)req->priv;
514
515         if (!msg)
516                 lua_pushnil(L);
517
518         ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
519 }
520
521 static int
522 ubus_lua_call(lua_State *L)
523 {
524         int rv, top;
525         uint32_t id;
526         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
527         const char *path = luaL_checkstring(L, 2);
528         const char *func = luaL_checkstring(L, 3);
529
530         luaL_checktype(L, 4, LUA_TTABLE);
531         blob_buf_init(&c->buf, 0);
532
533         if (!ubus_lua_format_blob_array(L, &c->buf, true))
534         {
535                 lua_pushnil(L);
536                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
537                 return 2;
538         }
539
540         rv = ubus_lookup_id(c->ctx, path, &id);
541
542         if (rv)
543         {
544                 lua_pushnil(L);
545                 lua_pushinteger(L, rv);
546                 return 2;
547         }
548
549         top = lua_gettop(L);
550         rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
551
552         if (rv != UBUS_STATUS_OK)
553         {
554                 lua_pop(L, 1);
555                 lua_pushnil(L);
556                 lua_pushinteger(L, rv);
557                 return 2;
558         }
559
560         return lua_gettop(L) - top;
561 }
562
563
564 static int
565 ubus_lua__gc(lua_State *L)
566 {
567         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
568
569         if (c->ctx != NULL)
570         {
571                 ubus_free(c->ctx);
572                 memset(c, 0, sizeof(*c));
573         }
574
575         return 0;
576 }
577
578 static const luaL_Reg ubus[] = {
579         { "connect", ubus_lua_connect },
580         { "objects", ubus_lua_objects },
581         { "add", ubus_lua_add },
582         { "reply", ubus_lua_reply },
583         { "signatures", ubus_lua_signatures },
584         { "call", ubus_lua_call },
585         { "close", ubus_lua__gc },
586         { "__gc", ubus_lua__gc },
587         { NULL, NULL },
588 };
589
590 /* avoid missing prototype warning */
591 int luaopen_ubus(lua_State *L);
592
593 int
594 luaopen_ubus(lua_State *L)
595 {
596         /* create metatable */
597         luaL_newmetatable(L, METANAME);
598
599         /* metatable.__index = metatable */
600         lua_pushvalue(L, -1);
601         lua_setfield(L, -2, "__index");
602
603         /* fill metatable */
604         luaL_register(L, NULL, ubus);
605         lua_pop(L, 1);
606
607         /* create module */
608         luaL_register(L, MODNAME, ubus);
609
610         /* set some enum defines */
611         lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
612         lua_setfield(L, -2, "ARRAY");
613         lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
614         lua_setfield(L, -2, "TABLE");
615         lua_pushinteger(L, BLOBMSG_TYPE_STRING);
616         lua_setfield(L, -2, "STRING");
617         lua_pushinteger(L, BLOBMSG_TYPE_INT64);
618         lua_setfield(L, -2, "INT64");
619         lua_pushinteger(L, BLOBMSG_TYPE_INT32);
620         lua_setfield(L, -2, "INT32");
621         lua_pushinteger(L, BLOBMSG_TYPE_INT16);
622         lua_setfield(L, -2, "INT16");
623         lua_pushinteger(L, BLOBMSG_TYPE_INT8);
624         lua_setfield(L, -2, "INT8");
625         lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
626         lua_setfield(L, -2, "BOOLEAN");
627
628         /* used in our callbacks */
629         state = L;
630
631         /* create the callback table */
632         lua_createtable(L, 1, 0);
633         lua_setglobal(L, "__ubus_cb");
634
635         return 0;
636 }