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