iron out all extra compiler warnings
[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                 lua_error(L);
561                 return 0;
562         }
563
564         /* scan each object */
565         lua_pushnil(L);
566         while (lua_next(L, -2) != 0) {
567                 struct ubus_object *obj = NULL;
568
569                 /* check if the object has a table of methods */
570                 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
571                         obj = ubus_lua_load_object(L);
572
573                         if (obj){
574                                 ubus_add_object(c->ctx, obj);
575
576                                 /* allow future reference of ubus obj */
577                                 lua_pushstring(state,"__ubusobj");
578                                 lua_pushlightuserdata(state, obj);
579                                 lua_settable(state,-3);
580                         }
581                 }
582                 lua_pop(L, 1);
583         }
584
585         return 0;
586 }
587
588 static int
589 ubus_lua_notify( lua_State *L )
590 {
591         struct ubus_lua_connection *c;
592         struct ubus_object *obj;
593         const char* method;
594
595         c = luaL_checkudata(L, 1, METANAME);
596         method = luaL_checkstring(L, 3);
597         luaL_checktype(L, 4, LUA_TTABLE);
598
599         if( !lua_islightuserdata( L, 2 ) ){
600                 lua_pushfstring( L, "Invald 2nd parameter, expected ubus obj ref" );
601                 lua_error( L );
602         }
603         obj = lua_touserdata( L, 2 );
604
605         /* create parameters from table */
606         blob_buf_init(&c->buf, 0);
607         if( !ubus_lua_format_blob_array( L, &c->buf, true ) ){
608                 lua_pushfstring( L, "Invalid 4th parameter, expected table of arguments" );
609                 lua_error( L );
610         }
611
612         ubus_notify( c->ctx, obj, method, c->buf.head, -1 );
613         return 0;
614 }
615
616 static void
617 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
618 {
619         lua_State *L = (lua_State *)p;
620
621         if (!o->signature)
622                 return;
623
624         ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
625 }
626
627 static int
628 ubus_lua_signatures(lua_State *L)
629 {
630         int rv;
631         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
632         const char *path = luaL_checkstring(L, 2);
633
634         rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
635
636         if (rv != UBUS_STATUS_OK)
637         {
638                 lua_pop(L, 1);
639                 lua_pushnil(L);
640                 lua_pushinteger(L, rv);
641                 return 2;
642         }
643
644         return 1;
645 }
646
647
648 static void
649 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
650 {
651         lua_State *L = (lua_State *)req->priv;
652
653         if (!msg && L)
654                 lua_pushnil(L);
655
656         if (msg && L)
657                 ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
658 }
659
660 static int
661 ubus_lua_call(lua_State *L)
662 {
663         int rv, top;
664         uint32_t id;
665         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
666         const char *path = luaL_checkstring(L, 2);
667         const char *func = luaL_checkstring(L, 3);
668
669         luaL_checktype(L, 4, LUA_TTABLE);
670         blob_buf_init(&c->buf, 0);
671
672         if (!ubus_lua_format_blob_array(L, &c->buf, true))
673         {
674                 lua_pushnil(L);
675                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
676                 return 2;
677         }
678
679         rv = ubus_lookup_id(c->ctx, path, &id);
680
681         if (rv)
682         {
683                 lua_pushnil(L);
684                 lua_pushinteger(L, rv);
685                 return 2;
686         }
687
688         top = lua_gettop(L);
689         rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
690
691         if (rv != UBUS_STATUS_OK)
692         {
693                 lua_pop(L, 1);
694                 lua_pushnil(L);
695                 lua_pushinteger(L, rv);
696                 return 2;
697         }
698
699         return lua_gettop(L) - top;
700 }
701
702 static void
703 ubus_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
704                         const char *type, struct blob_attr *msg)
705 {
706         struct ubus_lua_event *listener = container_of(ev, struct ubus_lua_event, e);
707
708         lua_getglobal(state, "__ubus_cb_event");
709         lua_rawgeti(state, -1, listener->r);
710         lua_remove(state, -2);
711
712         if (lua_isfunction(state, -1)) {
713                 ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
714                 lua_call(state, 1, 0);
715         } else {
716                 lua_pop(state, 1);
717         }
718 }
719
720 static struct ubus_event_handler*
721 ubus_lua_load_event(lua_State *L)
722 {
723         struct ubus_lua_event* event = NULL;
724
725         event = calloc(1, sizeof(struct ubus_lua_event));
726         if (!event)
727                 return NULL;
728
729         event->e.cb = ubus_event_handler;
730
731         /* update the he callback lookup table */
732         lua_getglobal(L, "__ubus_cb_event");
733         lua_pushvalue(L, -2);
734         event->r = luaL_ref(L, -2);
735         lua_setfield(L, -1, lua_tostring(L, -3));
736
737         return &event->e;
738 }
739
740 static int
741 ubus_lua_listen(lua_State *L) {
742         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
743
744         /* verify top level object */
745         luaL_checktype(L, 2, LUA_TTABLE);
746
747         /* scan each object */
748         lua_pushnil(L);
749         while (lua_next(L, -2) != 0) {
750                 struct ubus_event_handler *listener;
751
752                 /* check if the key is a string and the value is a method */
753                 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TFUNCTION)) {
754                         listener = ubus_lua_load_event(L);
755                         if(listener != NULL) {
756                                 ubus_register_event_handler(c->ctx, listener, lua_tostring(L, -2));
757                         }
758                 }
759                 lua_pop(L, 1);
760         }
761         return 0;
762 }
763
764 static void
765 ubus_sub_remove_handler(struct ubus_context *ctx, struct ubus_subscriber *s,
766                             uint32_t id)
767 {
768         struct ubus_lua_subscriber *sub;
769
770         sub = container_of(s, struct ubus_lua_subscriber, s);
771
772         lua_getglobal(state, "__ubus_cb_subscribe");
773         lua_rawgeti(state, -1, sub->rremove);
774         lua_remove(state, -2);
775
776         if (lua_isfunction(state, -1)) {
777                 lua_call(state, 0, 0);
778         } else {
779                 lua_pop(state, 1);
780         }
781 }
782
783 static int
784 ubus_sub_notify_handler(struct ubus_context *ctx, struct ubus_object *obj,
785             struct ubus_request_data *req, const char *method,
786             struct blob_attr *msg)
787 {
788         struct ubus_subscriber *s;
789         struct ubus_lua_subscriber *sub;
790
791         s = container_of(obj, struct ubus_subscriber, obj);
792         sub = container_of(s, struct ubus_lua_subscriber, s);
793
794         lua_getglobal(state, "__ubus_cb_subscribe");
795         lua_rawgeti(state, -1, sub->rnotify);
796         lua_remove(state, -2);
797
798         if (lua_isfunction(state, -1)) {
799                 if( msg ){
800                         ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
801                 } else {
802                         lua_pushnil(state);
803                 }
804                 lua_pushstring(state, method);
805                 lua_call(state, 2, 0);
806         } else {
807                 lua_pop(state, 1);
808         }
809
810         return 0;
811 }
812
813
814
815 static void
816 ubus_lua_do_subscribe( struct ubus_context *ctx, lua_State *L, const char* target,
817                         int idxnotify, int idxremove )
818 {
819         uint32_t id;
820         int status;
821         struct ubus_lua_subscriber *sub;
822
823         if( ( status = ubus_lookup_id( ctx, target, &id ) ) ){
824                 lua_pushfstring( L, "Unable find target, status=%d", status );
825                 lua_error( L );
826         }
827
828         sub = calloc( 1, sizeof( struct ubus_lua_subscriber ) );
829         if( !sub ){
830                 lua_pushstring( L, "Out of memory" );
831                 lua_error( L );
832         }
833
834         if( idxnotify ){
835                 lua_getglobal(L, "__ubus_cb_subscribe");
836                 lua_pushvalue(L, idxnotify);
837                 sub->rnotify = luaL_ref(L, -2);
838                 lua_pop(L, 1);
839                 sub->s.cb = ubus_sub_notify_handler;
840         }
841
842         if( idxremove ){
843                 lua_getglobal(L, "__ubus_cb_subscribe");
844                 lua_pushvalue(L, idxnotify);
845                 sub->rnotify = luaL_ref(L, -2);
846                 lua_pop(L, 1);
847                 sub->s.remove_cb = ubus_sub_remove_handler;
848         }
849
850         if( ( status = ubus_register_subscriber( ctx, &sub->s ) ) ){
851                 lua_pushfstring( L, "Failed to register subscriber, status=%d", status );
852                 lua_error( L );
853         }
854
855         if( ( status = ubus_subscribe( ctx, &sub->s, id) ) ){
856                 lua_pushfstring( L, "Failed to register subscriber, status=%d", status );
857                 lua_error( L );
858         }
859 }
860
861 static int
862 ubus_lua_subscribe(lua_State *L) {
863         int idxnotify, idxremove, stackstart;
864         struct ubus_lua_connection *c;
865         const char* target;
866
867         idxnotify = idxremove = 0;
868         stackstart = lua_gettop( L );
869
870
871         c = luaL_checkudata(L, 1, METANAME);
872         target = luaL_checkstring(L, 2);
873         luaL_checktype(L, 3, LUA_TTABLE);
874
875
876         lua_pushstring( L, "notify");
877         lua_gettable( L, 3 );
878         if( lua_type( L, -1 ) == LUA_TFUNCTION ){
879                 idxnotify = lua_gettop( L );
880         } else {
881                 lua_pop( L, 1 );
882         }
883
884         lua_pushstring( L, "remove");
885         lua_gettable( L, 3 );
886         if( lua_type( L, -1 ) == LUA_TFUNCTION ){
887                 idxremove = lua_gettop( L );
888         } else {
889                 lua_pop( L, 1 );
890         }
891
892         if( idxnotify )
893                 ubus_lua_do_subscribe( c->ctx, L, target, idxnotify, idxremove );
894
895         if( lua_gettop( L ) > stackstart )
896                 lua_pop( L, lua_gettop( L ) - stackstart );
897
898         return 0;
899 }
900
901 static int
902 ubus_lua_send(lua_State *L)
903 {
904         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
905         const char *event = luaL_checkstring(L, 2);
906
907         if (*event == 0)
908                 return luaL_argerror(L, 2, "no event name");
909
910         // Event content convert to ubus form
911         luaL_checktype(L, 3, LUA_TTABLE);
912         blob_buf_init(&c->buf, 0);
913
914         if (!ubus_lua_format_blob_array(L, &c->buf, true)) {
915                 lua_pushnil(L);
916                 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
917                 return 2;
918         }
919
920         // Send the event
921         ubus_send_event(c->ctx, event, c->buf.head);
922
923         return 0;
924 }
925
926
927
928 static int
929 ubus_lua__gc(lua_State *L)
930 {
931         struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
932
933         blob_buf_free(&c->buf);
934         if (c->ctx != NULL)
935         {
936                 ubus_free(c->ctx);
937                 memset(c, 0, sizeof(*c));
938         }
939
940         return 0;
941 }
942
943 static const luaL_Reg ubus[] = {
944         { "connect", ubus_lua_connect },
945         { "objects", ubus_lua_objects },
946         { "add", ubus_lua_add },
947         { "notify", ubus_lua_notify },
948         { "reply", ubus_lua_reply },
949         { "defer_request", ubus_lua_defer_request },
950         { "complete_deferred_request", ubus_lua_complete_deferred_request },
951         { "signatures", ubus_lua_signatures },
952         { "call", ubus_lua_call },
953         { "close", ubus_lua__gc },
954         { "listen", ubus_lua_listen },
955         { "send", ubus_lua_send },
956         { "subscribe", ubus_lua_subscribe },
957         { "__gc", ubus_lua__gc },
958         { NULL, NULL },
959 };
960
961 /* avoid missing prototype warning */
962 int luaopen_ubus(lua_State *L);
963
964 int
965 luaopen_ubus(lua_State *L)
966 {
967         /* create metatable */
968         luaL_newmetatable(L, METANAME);
969
970         /* metatable.__index = metatable */
971         lua_pushvalue(L, -1);
972         lua_setfield(L, -2, "__index");
973
974         /* fill metatable */
975         luaL_register(L, NULL, ubus);
976         lua_pop(L, 1);
977
978         /* create module */
979         luaL_register(L, MODNAME, ubus);
980
981         /* set some enum defines */
982         lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
983         lua_setfield(L, -2, "ARRAY");
984         lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
985         lua_setfield(L, -2, "TABLE");
986         lua_pushinteger(L, BLOBMSG_TYPE_STRING);
987         lua_setfield(L, -2, "STRING");
988         lua_pushinteger(L, BLOBMSG_TYPE_INT64);
989         lua_setfield(L, -2, "INT64");
990         lua_pushinteger(L, BLOBMSG_TYPE_INT32);
991         lua_setfield(L, -2, "INT32");
992         lua_pushinteger(L, BLOBMSG_TYPE_INT16);
993         lua_setfield(L, -2, "INT16");
994         lua_pushinteger(L, BLOBMSG_TYPE_INT8);
995         lua_setfield(L, -2, "INT8");
996         lua_pushinteger(L, BLOBMSG_TYPE_DOUBLE);
997         lua_setfield(L, -2, "DOUBLE");
998         lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
999         lua_setfield(L, -2, "BOOLEAN");
1000
1001         /* used in our callbacks */
1002         state = L;
1003
1004         /* create the callback table */
1005         lua_createtable(L, 1, 0);
1006         lua_setglobal(L, "__ubus_cb");
1007
1008         /* create the event table */
1009         lua_createtable(L, 1, 0);
1010         lua_setglobal(L, "__ubus_cb_event");
1011
1012         /* create the subscriber table */
1013         lua_createtable(L, 1, 0);
1014         lua_setglobal(L, "__ubus_cb_subscribe");
1015
1016         /* create the publisher table - notifications of new subs */
1017         lua_createtable(L, 1, 0);
1018         lua_setglobal(L, "__ubus_cb_publisher");
1019         return 0;
1020 }