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>
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
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.
18 #include <libubox/blobmsg.h>
19 #include <libubox/blobmsg_json.h>
23 #define MODNAME "ubus"
24 #define METANAME MODNAME ".meta"
26 static lua_State *state;
28 struct ubus_lua_connection {
31 struct ubus_context *ctx;
34 struct ubus_lua_object {
40 struct ubus_lua_event {
41 struct ubus_event_handler e;
45 struct ubus_lua_subscriber {
46 struct ubus_subscriber s;
52 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table);
55 ubus_lua_parse_blob_array(lua_State *L, struct blob_attr *attr, size_t len, bool table)
60 struct blob_attr *pos;
64 __blob_for_each_attr(pos, attr, rem)
66 rv = ubus_lua_parse_blob(L, pos, table);
71 lua_rawseti(L, -2, idx++);
78 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table)
84 if (!blobmsg_check_attr(attr, false))
87 if (table && blobmsg_name(attr)[0])
89 lua_pushstring(L, blobmsg_name(attr));
93 data = blobmsg_data(attr);
94 len = blobmsg_data_len(attr);
96 switch (blob_id(attr))
98 case BLOBMSG_TYPE_BOOL:
99 lua_pushboolean(L, *(uint8_t *)data);
102 case BLOBMSG_TYPE_INT16:
103 lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
106 case BLOBMSG_TYPE_INT32:
107 lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
110 case BLOBMSG_TYPE_INT64:
111 lua_pushnumber(L, (double) be64_to_cpu(*(uint64_t *)data));
114 case BLOBMSG_TYPE_DOUBLE:
120 v.u64 = be64_to_cpu(*(uint64_t *)data);
121 lua_pushnumber(L, v.d);
125 case BLOBMSG_TYPE_STRING:
126 lua_pushstring(L, data);
129 case BLOBMSG_TYPE_ARRAY:
130 ubus_lua_parse_blob_array(L, data, len, false);
133 case BLOBMSG_TYPE_TABLE:
134 ubus_lua_parse_blob_array(L, data, len, true);
147 ubus_lua_format_blob_is_array(lua_State *L)
152 /* Find out whether table is array-like */
153 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
156 if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
158 if (lua_type(L, -2) != LUA_TNUMBER)
165 cur = lua_tointeger(L, -2);
167 if ((cur - 1) != prv)
180 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
183 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
187 const char *key = table ? lua_tostring(L, -2) : NULL;
189 switch (lua_type(L, -1))
192 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
199 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
204 case LUA_TLIGHTUSERDATA:
205 blobmsg_add_string(b, key, lua_tostring(L, -1));
209 if (ubus_lua_format_blob_is_array(L))
211 c = blobmsg_open_array(b, key);
212 rv = ubus_lua_format_blob_array(L, b, false);
213 blobmsg_close_array(b, c);
217 c = blobmsg_open_table(b, key);
218 rv = ubus_lua_format_blob_array(L, b, true);
219 blobmsg_close_table(b, c);
232 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
234 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
236 if (!ubus_lua_format_blob(L, b, table))
248 ubus_lua_connect(lua_State *L)
250 struct ubus_lua_connection *c;
251 const char *sockpath = luaL_optstring(L, 1, NULL);
252 int timeout = luaL_optint(L, 2, 30);
254 if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
255 (c->ctx = ubus_connect(sockpath)) != NULL)
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);
265 /* NB: no errors from ubus_connect() yet */
267 lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
273 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
275 lua_State *L = (lua_State *)p;
277 lua_pushstring(L, o->path);
278 lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
282 ubus_lua_objects(lua_State *L)
285 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
288 rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
290 if (rv != UBUS_STATUS_OK)
294 lua_pushinteger(L, rv);
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)
306 struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
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);
315 if (lua_isfunction(state, -1)) {
316 lua_pushlightuserdata(state, req);
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);
331 static int lua_gettablelen(lua_State *L, int index)
337 while (lua_next(L, index) != 0) {
345 static int ubus_lua_reply(lua_State *L)
347 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
348 struct ubus_request_data *req;
350 luaL_checktype(L, 3, LUA_TTABLE);
351 blob_buf_init(&c->buf, 0);
353 if (!ubus_lua_format_blob_array(L, &c->buf, true))
356 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
360 req = lua_touserdata(L, 2);
361 ubus_send_reply(c->ctx, req, c->buf.head);
366 static int ubus_lua_defer_request(lua_State *L)
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);
376 static int ubus_lua_complete_deferred_request(lua_State *L)
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);
386 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
388 struct blobmsg_policy *p;
392 /* get the function pointer */
393 lua_pushinteger(L, 1);
396 /* get the policy table */
397 lua_pushinteger(L, 2);
400 /* check if the method table is valid */
401 if ((lua_type(L, -2) != LUA_TFUNCTION) ||
402 (lua_type(L, -1) != LUA_TTABLE) ||
408 /* store function pointer */
409 lua_pushvalue(L, -2);
410 lua_setfield(L, -6, lua_tostring(L, -5));
412 m->name = lua_tostring(L, -4);
413 m->handler = ubus_method_handler;
415 plen = lua_gettablelen(L, -1);
417 /* exit if policy table is empty */
423 /* setup the policy pointers */
424 p = calloc(plen, sizeof(struct blobmsg_policy));
430 while (lua_next(L, -2) != 0) {
431 int val = lua_tointeger(L, -1);
433 /* check if the policy is valid */
434 if ((lua_type(L, -2) != LUA_TSTRING) ||
435 (lua_type(L, -1) != LUA_TNUMBER) ||
437 (val > BLOBMSG_TYPE_LAST)) {
441 p[pidx].name = lua_tostring(L, -2);
454 ubus_new_sub_cb(struct ubus_context *ctx, struct ubus_object *obj)
456 struct ubus_lua_object *luobj;
458 luobj = container_of(obj, struct ubus_lua_object, o);
460 lua_getglobal(state, "__ubus_cb_publisher");
461 lua_rawgeti(state, -1, luobj->rsubscriber);
462 lua_remove(state, -2);
464 if (lua_isfunction(state, -1)) {
465 lua_pushnumber(state, luobj->o.has_subscribers );
466 lua_call(state, 1, 0);
473 ubus_lua_load_newsub_cb( lua_State *L, struct ubus_lua_object *obj )
475 /* keep ref to func */
476 lua_getglobal(L, "__ubus_cb_publisher");
477 lua_pushvalue(L, -2);
478 obj->rsubscriber = luaL_ref(L, -2);
482 obj->o.subscribe_cb = ubus_new_sub_cb;
486 static struct ubus_object* ubus_lua_load_object(lua_State *L)
488 struct ubus_lua_object *obj = NULL;
489 int mlen = lua_gettablelen(L, -1);
490 struct ubus_method *m;
493 /* setup object pointers */
494 obj = calloc(1, sizeof(struct ubus_lua_object));
498 obj->o.name = lua_tostring(L, -2);
500 /* setup method pointers */
501 m = calloc(mlen, sizeof(struct ubus_method));
504 /* setup type pointers */
505 obj->o.type = calloc(1, sizeof(struct ubus_object_type));
511 obj->o.type->name = lua_tostring(L, -2);
513 obj->o.type->methods = obj->o.methods;
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);
522 /* scan each method */
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 );
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)) {
540 if (!ubus_lua_load_methods(L, &m[midx]))
545 obj->o.type->n_methods = obj->o.n_methods = midx;
547 /* pop the callback table */
553 static int ubus_lua_add(lua_State *L)
555 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
557 /* verify top level object */
558 if (lua_istable(L, 1)) {
559 lua_pushstring(L, "you need to pass a table");
564 /* scan each object */
566 while (lua_next(L, -2) != 0) {
567 struct ubus_object *obj = NULL;
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);
574 ubus_add_object(c->ctx, obj);
576 /* allow future reference of ubus obj */
577 lua_pushstring(state,"__ubusobj");
578 lua_pushlightuserdata(state, obj);
579 lua_settable(state,-3);
589 ubus_lua_notify( lua_State *L )
591 struct ubus_lua_connection *c;
592 struct ubus_object *obj;
595 c = luaL_checkudata(L, 1, METANAME);
596 method = luaL_checkstring(L, 3);
597 luaL_checktype(L, 4, LUA_TTABLE);
599 if( !lua_islightuserdata( L, 2 ) ){
600 lua_pushfstring( L, "Invald 2nd parameter, expected ubus obj ref" );
603 obj = lua_touserdata( L, 2 );
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" );
612 ubus_notify( c->ctx, obj, method, c->buf.head, -1 );
617 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
619 lua_State *L = (lua_State *)p;
624 ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
628 ubus_lua_signatures(lua_State *L)
631 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
632 const char *path = luaL_checkstring(L, 2);
634 rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
636 if (rv != UBUS_STATUS_OK)
640 lua_pushinteger(L, rv);
649 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
651 lua_State *L = (lua_State *)req->priv;
657 ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
661 ubus_lua_call(lua_State *L)
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);
669 luaL_checktype(L, 4, LUA_TTABLE);
670 blob_buf_init(&c->buf, 0);
672 if (!ubus_lua_format_blob_array(L, &c->buf, true))
675 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
679 rv = ubus_lookup_id(c->ctx, path, &id);
684 lua_pushinteger(L, rv);
689 rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
691 if (rv != UBUS_STATUS_OK)
695 lua_pushinteger(L, rv);
699 return lua_gettop(L) - top;
703 ubus_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
704 const char *type, struct blob_attr *msg)
706 struct ubus_lua_event *listener = container_of(ev, struct ubus_lua_event, e);
708 lua_getglobal(state, "__ubus_cb_event");
709 lua_rawgeti(state, -1, listener->r);
710 lua_remove(state, -2);
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);
720 static struct ubus_event_handler*
721 ubus_lua_load_event(lua_State *L)
723 struct ubus_lua_event* event = NULL;
725 event = calloc(1, sizeof(struct ubus_lua_event));
729 event->e.cb = ubus_event_handler;
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));
741 ubus_lua_listen(lua_State *L) {
742 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
744 /* verify top level object */
745 luaL_checktype(L, 2, LUA_TTABLE);
747 /* scan each object */
749 while (lua_next(L, -2) != 0) {
750 struct ubus_event_handler *listener;
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));
765 ubus_sub_remove_handler(struct ubus_context *ctx, struct ubus_subscriber *s,
768 struct ubus_lua_subscriber *sub;
770 sub = container_of(s, struct ubus_lua_subscriber, s);
772 lua_getglobal(state, "__ubus_cb_subscribe");
773 lua_rawgeti(state, -1, sub->rremove);
774 lua_remove(state, -2);
776 if (lua_isfunction(state, -1)) {
777 lua_call(state, 0, 0);
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)
788 struct ubus_subscriber *s;
789 struct ubus_lua_subscriber *sub;
791 s = container_of(obj, struct ubus_subscriber, obj);
792 sub = container_of(s, struct ubus_lua_subscriber, s);
794 lua_getglobal(state, "__ubus_cb_subscribe");
795 lua_rawgeti(state, -1, sub->rnotify);
796 lua_remove(state, -2);
798 if (lua_isfunction(state, -1)) {
800 ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
804 lua_pushstring(state, method);
805 lua_call(state, 2, 0);
816 ubus_lua_do_subscribe( struct ubus_context *ctx, lua_State *L, const char* target,
817 int idxnotify, int idxremove )
821 struct ubus_lua_subscriber *sub;
823 if( ( status = ubus_lookup_id( ctx, target, &id ) ) ){
824 lua_pushfstring( L, "Unable find target, status=%d", status );
828 sub = calloc( 1, sizeof( struct ubus_lua_subscriber ) );
830 lua_pushstring( L, "Out of memory" );
835 lua_getglobal(L, "__ubus_cb_subscribe");
836 lua_pushvalue(L, idxnotify);
837 sub->rnotify = luaL_ref(L, -2);
839 sub->s.cb = ubus_sub_notify_handler;
843 lua_getglobal(L, "__ubus_cb_subscribe");
844 lua_pushvalue(L, idxnotify);
845 sub->rnotify = luaL_ref(L, -2);
847 sub->s.remove_cb = ubus_sub_remove_handler;
850 if( ( status = ubus_register_subscriber( ctx, &sub->s ) ) ){
851 lua_pushfstring( L, "Failed to register subscriber, status=%d", status );
855 if( ( status = ubus_subscribe( ctx, &sub->s, id) ) ){
856 lua_pushfstring( L, "Failed to register subscriber, status=%d", status );
862 ubus_lua_subscribe(lua_State *L) {
863 int idxnotify, idxremove, stackstart;
864 struct ubus_lua_connection *c;
867 idxnotify = idxremove = 0;
868 stackstart = lua_gettop( L );
871 c = luaL_checkudata(L, 1, METANAME);
872 target = luaL_checkstring(L, 2);
873 luaL_checktype(L, 3, LUA_TTABLE);
876 lua_pushstring( L, "notify");
877 lua_gettable( L, 3 );
878 if( lua_type( L, -1 ) == LUA_TFUNCTION ){
879 idxnotify = lua_gettop( L );
884 lua_pushstring( L, "remove");
885 lua_gettable( L, 3 );
886 if( lua_type( L, -1 ) == LUA_TFUNCTION ){
887 idxremove = lua_gettop( L );
893 ubus_lua_do_subscribe( c->ctx, L, target, idxnotify, idxremove );
895 if( lua_gettop( L ) > stackstart )
896 lua_pop( L, lua_gettop( L ) - stackstart );
902 ubus_lua_send(lua_State *L)
904 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
905 const char *event = luaL_checkstring(L, 2);
908 return luaL_argerror(L, 2, "no event name");
910 // Event content convert to ubus form
911 luaL_checktype(L, 3, LUA_TTABLE);
912 blob_buf_init(&c->buf, 0);
914 if (!ubus_lua_format_blob_array(L, &c->buf, true)) {
916 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
921 ubus_send_event(c->ctx, event, c->buf.head);
929 ubus_lua__gc(lua_State *L)
931 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
933 blob_buf_free(&c->buf);
937 memset(c, 0, sizeof(*c));
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 },
961 /* avoid missing prototype warning */
962 int luaopen_ubus(lua_State *L);
965 luaopen_ubus(lua_State *L)
967 /* create metatable */
968 luaL_newmetatable(L, METANAME);
970 /* metatable.__index = metatable */
971 lua_pushvalue(L, -1);
972 lua_setfield(L, -2, "__index");
975 luaL_register(L, NULL, ubus);
979 luaL_register(L, MODNAME, ubus);
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");
1001 /* used in our callbacks */
1004 /* create the callback table */
1005 lua_createtable(L, 1, 0);
1006 lua_setglobal(L, "__ubus_cb");
1008 /* create the event table */
1009 lua_createtable(L, 1, 0);
1010 lua_setglobal(L, "__ubus_cb_event");
1012 /* create the subscriber table */
1013 lua_createtable(L, 1, 0);
1014 lua_setglobal(L, "__ubus_cb_subscribe");
1016 /* create the publisher table - notifications of new subs */
1017 lua_createtable(L, 1, 0);
1018 lua_setglobal(L, "__ubus_cb_publisher");