86dcc5007b3a2776a611bf6c0a9255b7c5d0b946
[oweals/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  * Copyright (C) 2016 Iain Fraser <iainf@netduma.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License version 2.1
8  * as published by the Free Software Foundation
9  *
10  * This program 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.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <unistd.h>
17 #include <libubus.h>
18 #include <libubox/blobmsg.h>
19 #include <libubox/blobmsg_json.h>
20 #include <lauxlib.h>
21 #include <lua.h>
22
23 #define MODNAME         "ubus"
24 #define METANAME        MODNAME ".meta"
25
26 static lua_State *state;
27
28 struct ubus_lua_connection {
29         int timeout;
30         struct blob_buf buf;
31         struct ubus_context *ctx;
32 };
33
34 struct ubus_lua_object {
35         struct ubus_object o;
36         int r;
37         int rsubscriber;
38 };
39
40 struct ubus_lua_event {
41         struct ubus_event_handler e;
42         int r;
43 };
44
45 struct ubus_lua_subscriber {
46         struct ubus_subscriber s;
47         int rnotify;
48         int rremove;
49 };
50
51 static int
52 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table);
53
54 static int
55 ubus_lua_parse_blob_array(lua_State *L, struct blob_attr *attr, size_t len, bool table)
56 {
57         int rv;
58         int idx = 1;
59         size_t rem = len;
60         struct blob_attr *pos;
61
62         lua_newtable(L);
63
64         __blob_for_each_attr(pos, attr, rem)
65         {
66                 rv = ubus_lua_parse_blob(L, pos, table);
67
68                 if (rv > 1)
69                         lua_rawset(L, -3);
70                 else if (rv > 0)
71                         lua_rawseti(L, -2, idx++);
72         }
73
74         return 1;
75 }
76
77 static int
78 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table)
79 {
80         int len;
81         int off = 0;
82         void *data;
83
84         if (!blobmsg_check_attr(attr, false))
85                 return 0;
86
87         if (table && blobmsg_name(attr)[0])
88         {
89                 lua_pushstring(L, blobmsg_name(attr));
90                 off++;
91         }
92
93         data = blobmsg_data(attr);
94         len = blobmsg_data_len(attr);
95
96         switch (blob_id(attr))
97         {
98         case BLOBMSG_TYPE_BOOL:
99                 lua_pushboolean(L, *(uint8_t *)data);
100                 break;
101
102         case BLOBMSG_TYPE_INT16:
103                 lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
104                 break;
105
106         case BLOBMSG_TYPE_INT32:
107                 lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
108                 break;
109
110         case BLOBMSG_TYPE_INT64:
111                 lua_pushnumber(L, (double) be64_to_cpu(*(uint64_t *)data));
112                 break;
113
114         case BLOBMSG_TYPE_DOUBLE:
115                 {
116                         union {
117                                 double d;
118                                 uint64_t u64;
119                         } v;
120                         v.u64 = be64_to_cpu(*(uint64_t *)data);
121                         lua_pushnumber(L, v.d);
122                 }
123                 break;
124
125         case BLOBMSG_TYPE_STRING:
126                 lua_pushstring(L, data);
127                 break;
128
129         case BLOBMSG_TYPE_ARRAY:
130                 ubus_lua_parse_blob_array(L, data, len, false);
131                 break;
132
133         case BLOBMSG_TYPE_TABLE:
134                 ubus_lua_parse_blob_array(L, data, len, true);
135                 break;
136
137         default:
138                 lua_pushnil(L);
139                 break;
140         }
141
142         return off + 1;
143 }
144
145
146 static bool
147 ubus_lua_format_blob_is_array(lua_State *L)
148 {
149         lua_Integer prv = 0;
150         lua_Integer cur = 0;
151
152         /* Find out whether table is array-like */
153         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
154         {
155 #ifdef LUA_TINT
156                 if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
157 #else
158                 if (lua_type(L, -2) != LUA_TNUMBER)
159 #endif
160                 {
161                         lua_pop(L, 2);
162                         return false;
163                 }
164
165                 cur = lua_tointeger(L, -2);
166
167                 if ((cur - 1) != prv)
168                 {
169                         lua_pop(L, 2);
170                         return false;
171                 }
172
173                 prv = cur;
174         }
175
176         return true;
177 }
178
179 static int
180 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
181
182 static int
183 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
184 {
185         void *c;
186         bool rv = true;
187         const char *key = table ? lua_tostring(L, -2) : NULL;
188
189         switch (lua_type(L, -1))
190         {
191         case LUA_TBOOLEAN:
192                 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
193                 break;
194
195 #ifdef LUA_TINT
196         case LUA_TINT:
197 #endif
198         case LUA_TNUMBER:
199                 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
200                 break;
201
202         case LUA_TSTRING:
203         case LUA_TUSERDATA:
204         case LUA_TLIGHTUSERDATA:
205                 blobmsg_add_string(b, key, lua_tostring(L, -1));
206                 break;
207
208         case LUA_TTABLE:
209                 if (ubus_lua_format_blob_is_array(L))
210                 {
211                         c = blobmsg_open_array(b, key);
212                         rv = ubus_lua_format_blob_array(L, b, false);
213                         blobmsg_close_array(b, c);
214                 }
215                 else
216                 {
217                         c = blobmsg_open_table(b, key);
218                         rv = ubus_lua_format_blob_array(L, b, true);
219                         blobmsg_close_table(b, c);
220                 }
221                 break;
222
223         default:
224                 rv = false;
225                 break;
226         }
227
228         return rv;
229 }
230
231 static int
232 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
233 {
234         for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
235         {
236                 if (!ubus_lua_format_blob(L, b, table))
237                 {
238                         lua_pop(L, 1);
239                         return false;
240                 }
241         }
242
243         return true;
244 }
245
246
247 static int
248 ubus_lua_connect(lua_State *L)
249 {
250         struct ubus_lua_connection *c;
251         const char *sockpath = luaL_optstring(L, 1, NULL);
252         int timeout = luaL_optint(L, 2, 30);
253
254         if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
255                 (c->ctx = ubus_connect(sockpath)) != NULL)
256         {
257                 ubus_add_uloop(c->ctx);
258                 c->timeout = timeout;
259                 memset(&c->buf, 0, sizeof(c->buf));
260                 luaL_getmetatable(L, METANAME);
261                 lua_setmetatable(L, -2);
262                 return 1;
263         }
264
265         /* NB: no errors from ubus_connect() yet */
266         lua_pushnil(L);
267         lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
268         return 2;
269 }
270
271
272 static void
273 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
274 {
275         lua_State *L = (lua_State *)p;
276
277         lua_pushstring(L, o->path);
278         lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
279 }
280
281 static int
282 ubus_lua_objects(lua_State *L)
283 {
284         int rv;
285         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
286
287         lua_newtable(L);
288         rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
289
290         if (rv != UBUS_STATUS_OK)
291         {
292                 lua_pop(L, 1);
293                 lua_pushnil(L);
294                 lua_pushinteger(L, rv);
295                 return 2;
296         }
297
298         return 1;
299 }
300
301 static int
302 ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
303                 struct ubus_request_data *req, const char *method,
304                 struct blob_attr *msg)
305 {
306         struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
307         int rv = 0;
308
309         lua_getglobal(state, "__ubus_cb");
310         lua_rawgeti(state, -1, o->r);
311         lua_getfield(state, -1, method);
312         lua_remove(state, -2);
313         lua_remove(state, -2);
314
315         if (lua_isfunction(state, -1)) {
316                 lua_pushlightuserdata(state, req);
317                 if (!msg)
318                         lua_pushnil(state);
319                 else
320                         ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
321                 lua_call(state, 2, 1);
322                 if (lua_isnumber(state, -1))
323                         rv = lua_tonumber(state, -1);
324         }
325
326         lua_pop(state, 1);
327
328         return rv;
329 }
330
331 static int lua_gettablelen(lua_State *L, int index)
332 {
333         int cnt = 0;
334
335         lua_pushnil(L);
336         index -= 1;
337         while (lua_next(L, index) != 0) {
338                 cnt++;
339                 lua_pop(L, 1);
340         }
341
342         return cnt;
343 }
344
345 static int ubus_lua_reply(lua_State *L)
346 {
347         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
348         struct ubus_request_data *req;
349
350         luaL_checktype(L, 3, LUA_TTABLE);
351         blob_buf_init(&c->buf, 0);
352
353         if (!ubus_lua_format_blob_array(L, &c->buf, true))
354         {
355                 lua_pushnil(L);
356                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
357                 return 2;
358         }
359
360         req = lua_touserdata(L, 2);
361         ubus_send_reply(c->ctx, req, c->buf.head);
362
363         return 0;
364 }
365
366 static int ubus_lua_defer_request(lua_State *L)
367 {
368         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
369         struct ubus_request_data *req = lua_touserdata(L, 2);
370         struct ubus_request_data *new_req = lua_newuserdata(L, sizeof(struct ubus_request_data));
371         ubus_defer_request(c->ctx, req, new_req);
372
373         return 1;
374 }
375
376 static int ubus_lua_complete_deferred_request(lua_State *L)
377 {
378         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
379         struct ubus_request_data *req = lua_touserdata(L, 2);
380         int ret = luaL_checkinteger(L, 3);
381         ubus_complete_deferred_request(c->ctx, req, ret);
382
383         return 0;
384 }
385
386 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
387 {
388         struct blobmsg_policy *p;
389         int plen;
390         int pidx = 0;
391
392         /* get the function pointer */
393         lua_pushinteger(L, 1);
394         lua_gettable(L, -2);
395
396         /* get the policy table */
397         lua_pushinteger(L, 2);
398         lua_gettable(L, -3);
399
400         /* check if the method table is valid */
401         if ((lua_type(L, -2) != LUA_TFUNCTION) ||
402                         (lua_type(L, -1) != LUA_TTABLE) ||
403                         lua_objlen(L, -1)) {
404                 lua_pop(L, 2);
405                 return 1;
406         }
407
408         /* store function pointer */
409         lua_pushvalue(L, -2);
410         lua_setfield(L, -6, lua_tostring(L, -5));
411
412         m->name = lua_tostring(L, -4);
413         m->handler = ubus_method_handler;
414
415         plen = lua_gettablelen(L, -1);
416
417         /* exit if policy table is empty */
418         if (!plen) {
419                 lua_pop(L, 2);
420                 return 0;
421         }
422
423         /* setup the policy pointers */
424         p = calloc(plen, sizeof(struct blobmsg_policy));
425         if (!p)
426                 return 1;
427
428         m->policy = p;
429         lua_pushnil(L);
430         while (lua_next(L, -2) != 0) {
431                 int val = lua_tointeger(L, -1);
432
433                 /* check if the policy is valid */
434                 if ((lua_type(L, -2) != LUA_TSTRING) ||
435                                 (lua_type(L, -1) != LUA_TNUMBER) ||
436                                 (val < 0) ||
437                                 (val > BLOBMSG_TYPE_LAST)) {
438                         lua_pop(L, 1);
439                         continue;
440                 }
441                 p[pidx].name = lua_tostring(L, -2);
442                 p[pidx].type = val;
443                 lua_pop(L, 1);
444                 pidx++;
445         }
446
447         m->n_policy = pidx;
448         lua_pop(L, 2);
449
450         return 0;
451 }
452
453 static void
454 ubus_new_sub_cb(struct ubus_context *ctx, struct ubus_object *obj)
455 {
456         struct ubus_lua_object *luobj;
457
458         luobj = container_of(obj, struct ubus_lua_object, o);
459
460         lua_getglobal(state, "__ubus_cb_publisher");
461         lua_rawgeti(state, -1, luobj->rsubscriber);
462         lua_remove(state, -2);
463
464         if (lua_isfunction(state, -1)) {
465                 lua_pushnumber(state, luobj->o.has_subscribers );
466                 lua_call(state, 1, 0);
467         } else {
468                 lua_pop(state, 1);
469         }
470 }
471
472 static void
473 ubus_lua_load_newsub_cb( lua_State *L, struct ubus_lua_object *obj )
474 {
475         /* keep ref to func */
476         lua_getglobal(L, "__ubus_cb_publisher");
477         lua_pushvalue(L, -2);
478         obj->rsubscriber = luaL_ref(L, -2);
479         lua_pop(L, 1);
480
481         /* real callback */
482         obj->o.subscribe_cb = ubus_new_sub_cb;
483         return;
484 }
485
486 static struct ubus_object* ubus_lua_load_object(lua_State *L)
487 {
488         struct ubus_lua_object *obj = NULL;
489         int mlen = lua_gettablelen(L, -1);
490         struct ubus_method *m;
491         int midx = 0;
492
493         /* setup object pointers */
494         obj = calloc(1, sizeof(struct ubus_lua_object));
495         if (!obj)
496                 return NULL;
497
498         obj->o.name = lua_tostring(L, -2);
499
500         /* setup method pointers */
501         m = calloc(mlen, sizeof(struct ubus_method));
502         obj->o.methods = m;
503
504         /* setup type pointers */
505         obj->o.type = calloc(1, sizeof(struct ubus_object_type));
506         if (!obj->o.type) {
507                 free(obj);
508                 return NULL;
509         }
510
511         obj->o.type->name = lua_tostring(L, -2);
512         obj->o.type->id = 0;
513         obj->o.type->methods = obj->o.methods;
514
515         /* create the callback lookup table */
516         lua_createtable(L, 1, 0);
517         lua_getglobal(L, "__ubus_cb");
518         lua_pushvalue(L, -2);
519         obj->r = luaL_ref(L, -2);
520         lua_pop(L, 1);
521
522         /* scan each method */
523         lua_pushnil(L);
524         while (lua_next(L, -3) != 0) {
525                 /* check if its the subscriber notification callback */
526                 if( lua_type( L, -2 ) == LUA_TSTRING &&
527                                 lua_type( L, -1 ) == LUA_TFUNCTION ){
528                   if( !strcmp( lua_tostring( L, -2 ), "__subscriber_cb" ) )
529                           ubus_lua_load_newsub_cb( L, obj );
530                 }
531
532                 /* check if it looks like a method */
533                 if ((lua_type(L, -2) != LUA_TSTRING) ||
534                                 (lua_type(L, -1) != LUA_TTABLE) ||
535                                 !lua_objlen(L, -1)) {
536                         lua_pop(L, 1);
537                         continue;
538                 }
539
540                 if (!ubus_lua_load_methods(L, &m[midx]))
541                         midx++;
542                 lua_pop(L, 1);
543         }
544
545         obj->o.type->n_methods = obj->o.n_methods = midx;
546
547         /* pop the callback table */
548         lua_pop(L, 1);
549
550         return &obj->o;
551 }
552
553 static int ubus_lua_add(lua_State *L)
554 {
555         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
556
557         /* verify top level object */
558         if (lua_istable(L, 1)) {
559                 lua_pushstring(L, "you need to pass a table");
560                 return lua_error(L);
561         }
562
563         /* scan each object */
564         lua_pushnil(L);
565         while (lua_next(L, -2) != 0) {
566                 struct ubus_object *obj = NULL;
567
568                 /* check if the object has a table of methods */
569                 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
570                         obj = ubus_lua_load_object(L);
571
572                         if (obj){
573                                 ubus_add_object(c->ctx, obj);
574
575                                 /* allow future reference of ubus obj */
576                                 lua_pushstring(state,"__ubusobj");
577                                 lua_pushlightuserdata(state, obj);
578                                 lua_settable(state,-3);
579                         }
580                 }
581                 lua_pop(L, 1);
582         }
583
584         return 0;
585 }
586
587 static int
588 ubus_lua_notify( lua_State *L )
589 {
590         struct ubus_lua_connection *c;
591         struct ubus_object *obj;
592         const char* method;
593
594         c = luaL_checkudata(L, 1, METANAME);
595         method = luaL_checkstring(L, 3);
596         luaL_checktype(L, 4, LUA_TTABLE);
597
598         if( !lua_islightuserdata( L, 2 ) ){
599                 lua_pushfstring( L, "Invald 2nd parameter, expected ubus obj ref" );
600                 return lua_error( L );
601         }
602         obj = lua_touserdata( L, 2 );
603
604         /* create parameters from table */
605         blob_buf_init(&c->buf, 0);
606         if( !ubus_lua_format_blob_array( L, &c->buf, true ) ){
607                 lua_pushfstring( L, "Invalid 4th parameter, expected table of arguments" );
608                 return lua_error( L );
609         }
610
611         ubus_notify( c->ctx, obj, method, c->buf.head, -1 );
612         return 0;
613 }
614
615 static void
616 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
617 {
618         lua_State *L = (lua_State *)p;
619
620         if (!o->signature)
621                 return;
622
623         ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
624 }
625
626 static int
627 ubus_lua_signatures(lua_State *L)
628 {
629         int rv;
630         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
631         const char *path = luaL_checkstring(L, 2);
632
633         rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
634
635         if (rv != UBUS_STATUS_OK)
636         {
637                 lua_pop(L, 1);
638                 lua_pushnil(L);
639                 lua_pushinteger(L, rv);
640                 return 2;
641         }
642
643         return 1;
644 }
645
646
647 static void
648 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
649 {
650         lua_State *L = (lua_State *)req->priv;
651
652         if (!msg && L)
653                 lua_pushnil(L);
654
655         if (msg && L)
656                 ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
657 }
658
659 static int
660 ubus_lua_call(lua_State *L)
661 {
662         int rv, top;
663         uint32_t id;
664         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
665         const char *path = luaL_checkstring(L, 2);
666         const char *func = luaL_checkstring(L, 3);
667
668         luaL_checktype(L, 4, LUA_TTABLE);
669         blob_buf_init(&c->buf, 0);
670
671         if (!ubus_lua_format_blob_array(L, &c->buf, true))
672         {
673                 lua_pushnil(L);
674                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
675                 return 2;
676         }
677
678         rv = ubus_lookup_id(c->ctx, path, &id);
679
680         if (rv)
681         {
682                 lua_pushnil(L);
683                 lua_pushinteger(L, rv);
684                 return 2;
685         }
686
687         top = lua_gettop(L);
688         rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
689
690         if (rv != UBUS_STATUS_OK)
691         {
692                 lua_pop(L, 1);
693                 lua_pushnil(L);
694                 lua_pushinteger(L, rv);
695                 return 2;
696         }
697
698         return lua_gettop(L) - top;
699 }
700
701 static void
702 ubus_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
703                         const char *type, struct blob_attr *msg)
704 {
705         struct ubus_lua_event *listener = container_of(ev, struct ubus_lua_event, e);
706
707         lua_getglobal(state, "__ubus_cb_event");
708         lua_rawgeti(state, -1, listener->r);
709         lua_remove(state, -2);
710
711         if (lua_isfunction(state, -1)) {
712                 ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
713                 lua_call(state, 1, 0);
714         } else {
715                 lua_pop(state, 1);
716         }
717 }
718
719 static struct ubus_event_handler*
720 ubus_lua_load_event(lua_State *L)
721 {
722         struct ubus_lua_event* event = NULL;
723
724         event = calloc(1, sizeof(struct ubus_lua_event));
725         if (!event)
726                 return NULL;
727
728         event->e.cb = ubus_event_handler;
729
730         /* update the he callback lookup table */
731         lua_getglobal(L, "__ubus_cb_event");
732         lua_pushvalue(L, -2);
733         event->r = luaL_ref(L, -2);
734         lua_setfield(L, -1, lua_tostring(L, -3));
735
736         return &event->e;
737 }
738
739 static int
740 ubus_lua_listen(lua_State *L) {
741         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
742
743         /* verify top level object */
744         luaL_checktype(L, 2, LUA_TTABLE);
745
746         /* scan each object */
747         lua_pushnil(L);
748         while (lua_next(L, -2) != 0) {
749                 struct ubus_event_handler *listener;
750
751                 /* check if the key is a string and the value is a method */
752                 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TFUNCTION)) {
753                         listener = ubus_lua_load_event(L);
754                         if(listener != NULL) {
755                                 ubus_register_event_handler(c->ctx, listener, lua_tostring(L, -2));
756                         }
757                 }
758                 lua_pop(L, 1);
759         }
760         return 0;
761 }
762
763 static void
764 ubus_sub_remove_handler(struct ubus_context *ctx, struct ubus_subscriber *s,
765                             uint32_t id)
766 {
767         struct ubus_lua_subscriber *sub;
768
769         sub = container_of(s, struct ubus_lua_subscriber, s);
770
771         lua_getglobal(state, "__ubus_cb_subscribe");
772         lua_rawgeti(state, -1, sub->rremove);
773         lua_remove(state, -2);
774
775         if (lua_isfunction(state, -1)) {
776                 lua_call(state, 0, 0);
777         } else {
778                 lua_pop(state, 1);
779         }
780 }
781
782 static int
783 ubus_sub_notify_handler(struct ubus_context *ctx, struct ubus_object *obj,
784             struct ubus_request_data *req, const char *method,
785             struct blob_attr *msg)
786 {
787         struct ubus_subscriber *s;
788         struct ubus_lua_subscriber *sub;
789
790         s = container_of(obj, struct ubus_subscriber, obj);
791         sub = container_of(s, struct ubus_lua_subscriber, s);
792
793         lua_getglobal(state, "__ubus_cb_subscribe");
794         lua_rawgeti(state, -1, sub->rnotify);
795         lua_remove(state, -2);
796
797         if (lua_isfunction(state, -1)) {
798                 if( msg ){
799                         ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
800                 } else {
801                         lua_pushnil(state);
802                 }
803                 lua_pushstring(state, method);
804                 lua_call(state, 2, 0);
805         } else {
806                 lua_pop(state, 1);
807         }
808
809         return 0;
810 }
811
812
813
814 static int
815 ubus_lua_do_subscribe( struct ubus_context *ctx, lua_State *L, const char* target,
816                         int idxnotify, int idxremove )
817 {
818         uint32_t id;
819         int status;
820         struct ubus_lua_subscriber *sub;
821
822         if( ( status = ubus_lookup_id( ctx, target, &id ) ) ){
823                 lua_pushfstring( L, "Unable find target, status=%d", status );
824                 return lua_error( L );
825         }
826
827         sub = calloc( 1, sizeof( struct ubus_lua_subscriber ) );
828         if( !sub ){
829                 lua_pushstring( L, "Out of memory" );
830                 return lua_error( L );
831         }
832
833         if( idxnotify ){
834                 lua_getglobal(L, "__ubus_cb_subscribe");
835                 lua_pushvalue(L, idxnotify);
836                 sub->rnotify = luaL_ref(L, -2);
837                 lua_pop(L, 1);
838                 sub->s.cb = ubus_sub_notify_handler;
839         }
840
841         if( idxremove ){
842                 lua_getglobal(L, "__ubus_cb_subscribe");
843                 lua_pushvalue(L, idxremove);
844                 sub->rremove = luaL_ref(L, -2);
845                 lua_pop(L, 1);
846                 sub->s.remove_cb = ubus_sub_remove_handler;
847         }
848
849         if( ( status = ubus_register_subscriber( ctx, &sub->s ) ) ){
850                 lua_pushfstring( L, "Failed to register subscriber, status=%d", status );
851                 return lua_error( L );
852         }
853
854         if( ( status = ubus_subscribe( ctx, &sub->s, id) ) ){
855                 lua_pushfstring( L, "Failed to register subscriber, status=%d", status );
856                 return lua_error( L );
857         }
858
859         return 0;
860 }
861
862 static int
863 ubus_lua_subscribe(lua_State *L) {
864         int idxnotify, idxremove, stackstart;
865         struct ubus_lua_connection *c;
866         const char* target;
867
868         idxnotify = idxremove = 0;
869         stackstart = lua_gettop( L );
870
871
872         c = luaL_checkudata(L, 1, METANAME);
873         target = luaL_checkstring(L, 2);
874         luaL_checktype(L, 3, LUA_TTABLE);
875
876
877         lua_pushstring( L, "notify");
878         lua_gettable( L, 3 );
879         if( lua_type( L, -1 ) == LUA_TFUNCTION ){
880                 idxnotify = lua_gettop( L );
881         } else {
882                 lua_pop( L, 1 );
883         }
884
885         lua_pushstring( L, "remove");
886         lua_gettable( L, 3 );
887         if( lua_type( L, -1 ) == LUA_TFUNCTION ){
888                 idxremove = lua_gettop( L );
889         } else {
890                 lua_pop( L, 1 );
891         }
892
893         if( idxnotify )
894                 ubus_lua_do_subscribe( c->ctx, L, target, idxnotify, idxremove );
895
896         if( lua_gettop( L ) > stackstart )
897                 lua_pop( L, lua_gettop( L ) - stackstart );
898
899         return 0;
900 }
901
902 static int
903 ubus_lua_send(lua_State *L)
904 {
905         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
906         const char *event = luaL_checkstring(L, 2);
907
908         if (*event == 0)
909                 return luaL_argerror(L, 2, "no event name");
910
911         // Event content convert to ubus form
912         luaL_checktype(L, 3, LUA_TTABLE);
913         blob_buf_init(&c->buf, 0);
914
915         if (!ubus_lua_format_blob_array(L, &c->buf, true)) {
916                 lua_pushnil(L);
917                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
918                 return 2;
919         }
920
921         // Send the event
922         ubus_send_event(c->ctx, event, c->buf.head);
923
924         return 0;
925 }
926
927
928
929 static int
930 ubus_lua__gc(lua_State *L)
931 {
932         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
933
934         blob_buf_free(&c->buf);
935         if (c->ctx != NULL)
936         {
937                 ubus_free(c->ctx);
938                 memset(c, 0, sizeof(*c));
939         }
940
941         return 0;
942 }
943
944 static const luaL_Reg ubus[] = {
945         { "connect", ubus_lua_connect },
946         { "objects", ubus_lua_objects },
947         { "add", ubus_lua_add },
948         { "notify", ubus_lua_notify },
949         { "reply", ubus_lua_reply },
950         { "defer_request", ubus_lua_defer_request },
951         { "complete_deferred_request", ubus_lua_complete_deferred_request },
952         { "signatures", ubus_lua_signatures },
953         { "call", ubus_lua_call },
954         { "close", ubus_lua__gc },
955         { "listen", ubus_lua_listen },
956         { "send", ubus_lua_send },
957         { "subscribe", ubus_lua_subscribe },
958         { "__gc", ubus_lua__gc },
959         { NULL, NULL },
960 };
961
962 /* avoid missing prototype warning */
963 int luaopen_ubus(lua_State *L);
964
965 int
966 luaopen_ubus(lua_State *L)
967 {
968         /* create metatable */
969         luaL_newmetatable(L, METANAME);
970
971         /* metatable.__index = metatable */
972         lua_pushvalue(L, -1);
973         lua_setfield(L, -2, "__index");
974
975         /* fill metatable */
976         luaL_register(L, NULL, ubus);
977         lua_pop(L, 1);
978
979         /* create module */
980         luaL_register(L, MODNAME, ubus);
981
982         /* set some enum defines */
983         lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
984         lua_setfield(L, -2, "ARRAY");
985         lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
986         lua_setfield(L, -2, "TABLE");
987         lua_pushinteger(L, BLOBMSG_TYPE_STRING);
988         lua_setfield(L, -2, "STRING");
989         lua_pushinteger(L, BLOBMSG_TYPE_INT64);
990         lua_setfield(L, -2, "INT64");
991         lua_pushinteger(L, BLOBMSG_TYPE_INT32);
992         lua_setfield(L, -2, "INT32");
993         lua_pushinteger(L, BLOBMSG_TYPE_INT16);
994         lua_setfield(L, -2, "INT16");
995         lua_pushinteger(L, BLOBMSG_TYPE_INT8);
996         lua_setfield(L, -2, "INT8");
997         lua_pushinteger(L, BLOBMSG_TYPE_DOUBLE);
998         lua_setfield(L, -2, "DOUBLE");
999         lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
1000         lua_setfield(L, -2, "BOOLEAN");
1001
1002         /* used in our callbacks */
1003         state = L;
1004
1005         /* create the callback table */
1006         lua_createtable(L, 1, 0);
1007         lua_setglobal(L, "__ubus_cb");
1008
1009         /* create the event table */
1010         lua_createtable(L, 1, 0);
1011         lua_setglobal(L, "__ubus_cb_event");
1012
1013         /* create the subscriber table */
1014         lua_createtable(L, 1, 0);
1015         lua_setglobal(L, "__ubus_cb_subscribe");
1016
1017         /* create the publisher table - notifications of new subs */
1018         lua_createtable(L, 1, 0);
1019         lua_setglobal(L, "__ubus_cb_publisher");
1020         return 0;
1021 }