2 * Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
3 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #include <libubox/blobmsg.h>
18 #include <libubox/blobmsg_json.h>
22 #define MODNAME "ubus"
23 #define METANAME MODNAME ".meta"
25 static lua_State
*state
;
27 struct ubus_lua_connection
{
30 struct ubus_context
*ctx
;
33 struct ubus_lua_object
{
38 struct ubus_lua_event
{
39 struct ubus_event_handler e
;
44 ubus_lua_parse_blob(lua_State
*L
, struct blob_attr
*attr
, bool table
);
47 ubus_lua_parse_blob_array(lua_State
*L
, struct blob_attr
*attr
, int len
, bool table
)
52 struct blob_attr
*pos
;
56 __blob_for_each_attr(pos
, attr
, rem
)
58 rv
= ubus_lua_parse_blob(L
, pos
, table
);
63 lua_rawseti(L
, -2, idx
++);
70 ubus_lua_parse_blob(lua_State
*L
, struct blob_attr
*attr
, bool table
)
76 if (!blobmsg_check_attr(attr
, false))
79 if (table
&& blobmsg_name(attr
)[0])
81 lua_pushstring(L
, blobmsg_name(attr
));
85 data
= blobmsg_data(attr
);
86 len
= blobmsg_data_len(attr
);
88 switch (blob_id(attr
))
90 case BLOBMSG_TYPE_BOOL
:
91 lua_pushboolean(L
, *(uint8_t *)data
);
94 case BLOBMSG_TYPE_INT16
:
95 lua_pushinteger(L
, be16_to_cpu(*(uint16_t *)data
));
98 case BLOBMSG_TYPE_INT32
:
99 lua_pushinteger(L
, be32_to_cpu(*(uint32_t *)data
));
102 case BLOBMSG_TYPE_INT64
:
103 lua_pushnumber(L
, (double) be64_to_cpu(*(uint64_t *)data
));
106 case BLOBMSG_TYPE_STRING
:
107 lua_pushstring(L
, data
);
110 case BLOBMSG_TYPE_ARRAY
:
111 ubus_lua_parse_blob_array(L
, data
, len
, false);
114 case BLOBMSG_TYPE_TABLE
:
115 ubus_lua_parse_blob_array(L
, data
, len
, true);
128 ubus_lua_format_blob_is_array(lua_State
*L
)
133 /* Find out whether table is array-like */
134 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1))
137 if (lua_type(L
, -2) != LUA_TNUMBER
&& lua_type(L
, -2) != LUA_TINT
)
139 if (lua_type(L
, -2) != LUA_TNUMBER
)
146 cur
= lua_tointeger(L
, -2);
148 if ((cur
- 1) != prv
)
161 ubus_lua_format_blob_array(lua_State
*L
, struct blob_buf
*b
, bool table
);
164 ubus_lua_format_blob(lua_State
*L
, struct blob_buf
*b
, bool table
)
168 const char *key
= table
? lua_tostring(L
, -2) : NULL
;
170 switch (lua_type(L
, -1))
173 blobmsg_add_u8(b
, key
, (uint8_t)lua_toboolean(L
, -1));
180 blobmsg_add_u32(b
, key
, (uint32_t)lua_tointeger(L
, -1));
185 case LUA_TLIGHTUSERDATA
:
186 blobmsg_add_string(b
, key
, lua_tostring(L
, -1));
190 if (ubus_lua_format_blob_is_array(L
))
192 c
= blobmsg_open_array(b
, key
);
193 rv
= ubus_lua_format_blob_array(L
, b
, false);
194 blobmsg_close_array(b
, c
);
198 c
= blobmsg_open_table(b
, key
);
199 rv
= ubus_lua_format_blob_array(L
, b
, true);
200 blobmsg_close_table(b
, c
);
213 ubus_lua_format_blob_array(lua_State
*L
, struct blob_buf
*b
, bool table
)
215 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1))
217 if (!ubus_lua_format_blob(L
, b
, table
))
229 ubus_lua_connect(lua_State
*L
)
231 struct ubus_lua_connection
*c
;
232 const char *sockpath
= luaL_optstring(L
, 1, NULL
);
233 int timeout
= luaL_optint(L
, 2, 30);
235 if ((c
= lua_newuserdata(L
, sizeof(*c
))) != NULL
&&
236 (c
->ctx
= ubus_connect(sockpath
)) != NULL
)
238 ubus_add_uloop(c
->ctx
);
239 c
->timeout
= timeout
;
240 memset(&c
->buf
, 0, sizeof(c
->buf
));
241 luaL_getmetatable(L
, METANAME
);
242 lua_setmetatable(L
, -2);
246 /* NB: no errors from ubus_connect() yet */
248 lua_pushinteger(L
, UBUS_STATUS_UNKNOWN_ERROR
);
254 ubus_lua_objects_cb(struct ubus_context
*c
, struct ubus_object_data
*o
, void *p
)
256 lua_State
*L
= (lua_State
*)p
;
258 lua_pushstring(L
, o
->path
);
259 lua_rawseti(L
, -2, lua_objlen(L
, -2) + 1);
263 ubus_lua_objects(lua_State
*L
)
266 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
269 rv
= ubus_lookup(c
->ctx
, NULL
, ubus_lua_objects_cb
, L
);
271 if (rv
!= UBUS_STATUS_OK
)
275 lua_pushinteger(L
, rv
);
283 ubus_method_handler(struct ubus_context
*ctx
, struct ubus_object
*obj
,
284 struct ubus_request_data
*req
, const char *method
,
285 struct blob_attr
*msg
)
287 struct ubus_lua_object
*o
= container_of(obj
, struct ubus_lua_object
, o
);
290 lua_getglobal(state
, "__ubus_cb");
291 lua_rawgeti(state
, -1, o
->r
);
292 lua_getfield(state
, -1, method
);
293 lua_remove(state
, -2);
294 lua_remove(state
, -2);
296 if (lua_isfunction(state
, -1)) {
297 lua_pushlightuserdata(state
, req
);
301 ubus_lua_parse_blob_array(state
, blob_data(msg
), blob_len(msg
), true);
302 lua_call(state
, 2, 1);
303 if (lua_isnumber(state
, -1))
304 rv
= lua_tonumber(state
, -1);
311 static int lua_gettablelen(lua_State
*L
, int index
)
317 while (lua_next(L
, index
) != 0) {
325 static int ubus_lua_reply(lua_State
*L
)
327 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
328 struct ubus_request_data
*req
;
330 luaL_checktype(L
, 3, LUA_TTABLE
);
331 blob_buf_init(&c
->buf
, 0);
333 if (!ubus_lua_format_blob_array(L
, &c
->buf
, true))
336 lua_pushinteger(L
, UBUS_STATUS_INVALID_ARGUMENT
);
340 req
= lua_touserdata(L
, 2);
341 ubus_send_reply(c
->ctx
, req
, c
->buf
.head
);
346 static int ubus_lua_load_methods(lua_State
*L
, struct ubus_method
*m
)
348 struct blobmsg_policy
*p
;
352 /* get the function pointer */
353 lua_pushinteger(L
, 1);
356 /* get the policy table */
357 lua_pushinteger(L
, 2);
360 /* check if the method table is valid */
361 if ((lua_type(L
, -2) != LUA_TFUNCTION
) ||
362 (lua_type(L
, -1) != LUA_TTABLE
) ||
368 /* store function pointer */
369 lua_pushvalue(L
, -2);
370 lua_setfield(L
, -6, lua_tostring(L
, -5));
372 m
->name
= lua_tostring(L
, -4);
373 m
->handler
= ubus_method_handler
;
375 plen
= lua_gettablelen(L
, -1);
377 /* exit if policy table is empty */
383 /* setup the policy pointers */
384 p
= malloc(sizeof(struct blobmsg_policy
) * plen
);
385 memset(p
, 0, sizeof(struct blobmsg_policy
) * plen
);
388 while (lua_next(L
, -2) != 0) {
389 int val
= lua_tointeger(L
, -1);
391 /* check if the policy is valid */
392 if ((lua_type(L
, -2) != LUA_TSTRING
) ||
393 (lua_type(L
, -1) != LUA_TNUMBER
) ||
395 (val
> BLOBMSG_TYPE_LAST
)) {
399 p
[pidx
].name
= lua_tostring(L
, -2);
411 static struct ubus_object
* ubus_lua_load_object(lua_State
*L
)
413 struct ubus_lua_object
*obj
= NULL
;
414 int mlen
= lua_gettablelen(L
, -1);
415 struct ubus_method
*m
;
418 /* setup object pointers */
419 obj
= malloc(sizeof(struct ubus_lua_object
));
420 memset(obj
, 0, sizeof(struct ubus_lua_object
));
421 obj
->o
.name
= lua_tostring(L
, -2);
423 /* setup method pointers */
424 m
= malloc(sizeof(struct ubus_method
) * mlen
);
425 memset(m
, 0, sizeof(struct ubus_method
) * mlen
);
428 /* setup type pointers */
429 obj
->o
.type
= malloc(sizeof(struct ubus_object_type
));
430 memset(obj
->o
.type
, 0, sizeof(struct ubus_object_type
));
431 obj
->o
.type
->name
= lua_tostring(L
, -2);
433 obj
->o
.type
->methods
= obj
->o
.methods
;
435 /* create the callback lookup table */
436 lua_createtable(L
, 1, 0);
437 lua_getglobal(L
, "__ubus_cb");
438 lua_pushvalue(L
, -2);
439 obj
->r
= luaL_ref(L
, -2);
442 /* scan each method */
444 while (lua_next(L
, -3) != 0) {
445 /* check if it looks like a method */
446 if ((lua_type(L
, -2) != LUA_TSTRING
) ||
447 (lua_type(L
, -1) != LUA_TTABLE
) ||
448 !lua_objlen(L
, -1)) {
453 if (!ubus_lua_load_methods(L
, &m
[midx
]))
458 obj
->o
.type
->n_methods
= obj
->o
.n_methods
= midx
;
460 /* pop the callback table */
466 static int ubus_lua_add(lua_State
*L
)
468 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
470 /* verify top level object */
471 if (lua_istable(L
, 1)) {
472 lua_pushstring(L
, "you need to pass a table");
477 /* scan each object */
479 while (lua_next(L
, -2) != 0) {
480 struct ubus_object
*obj
= NULL
;
482 /* check if the object has a table of methods */
483 if ((lua_type(L
, -2) == LUA_TSTRING
) && (lua_type(L
, -1) == LUA_TTABLE
)) {
484 obj
= ubus_lua_load_object(L
);
487 ubus_add_object(c
->ctx
, obj
);
496 ubus_lua_signatures_cb(struct ubus_context
*c
, struct ubus_object_data
*o
, void *p
)
498 lua_State
*L
= (lua_State
*)p
;
503 ubus_lua_parse_blob_array(L
, blob_data(o
->signature
), blob_len(o
->signature
), true);
507 ubus_lua_signatures(lua_State
*L
)
510 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
511 const char *path
= luaL_checkstring(L
, 2);
513 rv
= ubus_lookup(c
->ctx
, path
, ubus_lua_signatures_cb
, L
);
515 if (rv
!= UBUS_STATUS_OK
)
519 lua_pushinteger(L
, rv
);
528 ubus_lua_call_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
530 lua_State
*L
= (lua_State
*)req
->priv
;
535 ubus_lua_parse_blob_array(L
, blob_data(msg
), blob_len(msg
), true);
539 ubus_lua_call(lua_State
*L
)
543 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
544 const char *path
= luaL_checkstring(L
, 2);
545 const char *func
= luaL_checkstring(L
, 3);
547 luaL_checktype(L
, 4, LUA_TTABLE
);
548 blob_buf_init(&c
->buf
, 0);
550 if (!ubus_lua_format_blob_array(L
, &c
->buf
, true))
553 lua_pushinteger(L
, UBUS_STATUS_INVALID_ARGUMENT
);
557 rv
= ubus_lookup_id(c
->ctx
, path
, &id
);
562 lua_pushinteger(L
, rv
);
567 rv
= ubus_invoke(c
->ctx
, id
, func
, c
->buf
.head
, ubus_lua_call_cb
, L
, c
->timeout
* 1000);
569 if (rv
!= UBUS_STATUS_OK
)
573 lua_pushinteger(L
, rv
);
577 return lua_gettop(L
) - top
;
581 ubus_event_handler(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
582 const char *type
, struct blob_attr
*msg
)
584 struct ubus_lua_event
*listener
= container_of(ev
, struct ubus_lua_event
, e
);
586 lua_getglobal(state
, "__ubus_cb_event");
587 lua_rawgeti(state
, -1, listener
->r
);
589 if (lua_isfunction(state
, -1)) {
590 ubus_lua_parse_blob_array(state
, blob_data(msg
), blob_len(msg
), true);
591 lua_call(state
, 1, 0);
595 static struct ubus_event_handler
*
596 ubus_lua_load_event(lua_State
*L
)
598 struct ubus_lua_event
* event
= NULL
;
600 event
= malloc(sizeof(struct ubus_lua_event
));
601 memset(event
, 0, sizeof(struct ubus_lua_event
));
602 event
->e
.cb
= ubus_event_handler
;
604 /* update the he callback lookup table */
605 lua_getglobal(L
, "__ubus_cb_event");
606 lua_pushvalue(L
, -2);
607 event
->r
= luaL_ref(L
, -2);
608 lua_setfield(L
, -1, lua_tostring(L
, -3));
614 ubus_lua_listen(lua_State
*L
) {
615 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
617 /* verify top level object */
618 luaL_checktype(L
, 2, LUA_TTABLE
);
620 /* scan each object */
622 while (lua_next(L
, -2) != 0) {
623 struct ubus_event_handler
*listener
;
625 /* check if the key is a string and the value is a method */
626 if ((lua_type(L
, -2) == LUA_TSTRING
) && (lua_type(L
, -1) == LUA_TFUNCTION
)) {
627 listener
= ubus_lua_load_event(L
);
628 if(listener
!= NULL
) {
629 ubus_register_event_handler(c
->ctx
, listener
, lua_tostring(L
, -2));
638 ubus_lua_send(lua_State
*L
)
640 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
641 const char *event
= luaL_checkstring(L
, 2);
644 return luaL_argerror(L
, 2, "no event name");
646 // Event content convert to ubus form
647 luaL_checktype(L
, 3, LUA_TTABLE
);
648 blob_buf_init(&c
->buf
, 0);
650 if (!ubus_lua_format_blob_array(L
, &c
->buf
, true)) {
652 lua_pushinteger(L
, UBUS_STATUS_INVALID_ARGUMENT
);
657 ubus_send_event(c
->ctx
, event
, c
->buf
.head
);
665 ubus_lua__gc(lua_State
*L
)
667 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
672 memset(c
, 0, sizeof(*c
));
678 static const luaL_Reg ubus
[] = {
679 { "connect", ubus_lua_connect
},
680 { "objects", ubus_lua_objects
},
681 { "add", ubus_lua_add
},
682 { "reply", ubus_lua_reply
},
683 { "signatures", ubus_lua_signatures
},
684 { "call", ubus_lua_call
},
685 { "close", ubus_lua__gc
},
686 { "listen", ubus_lua_listen
},
687 { "send", ubus_lua_send
},
688 { "__gc", ubus_lua__gc
},
692 /* avoid missing prototype warning */
693 int luaopen_ubus(lua_State
*L
);
696 luaopen_ubus(lua_State
*L
)
698 /* create metatable */
699 luaL_newmetatable(L
, METANAME
);
701 /* metatable.__index = metatable */
702 lua_pushvalue(L
, -1);
703 lua_setfield(L
, -2, "__index");
706 luaL_register(L
, NULL
, ubus
);
710 luaL_register(L
, MODNAME
, ubus
);
712 /* set some enum defines */
713 lua_pushinteger(L
, BLOBMSG_TYPE_ARRAY
);
714 lua_setfield(L
, -2, "ARRAY");
715 lua_pushinteger(L
, BLOBMSG_TYPE_TABLE
);
716 lua_setfield(L
, -2, "TABLE");
717 lua_pushinteger(L
, BLOBMSG_TYPE_STRING
);
718 lua_setfield(L
, -2, "STRING");
719 lua_pushinteger(L
, BLOBMSG_TYPE_INT64
);
720 lua_setfield(L
, -2, "INT64");
721 lua_pushinteger(L
, BLOBMSG_TYPE_INT32
);
722 lua_setfield(L
, -2, "INT32");
723 lua_pushinteger(L
, BLOBMSG_TYPE_INT16
);
724 lua_setfield(L
, -2, "INT16");
725 lua_pushinteger(L
, BLOBMSG_TYPE_INT8
);
726 lua_setfield(L
, -2, "INT8");
727 lua_pushinteger(L
, BLOBMSG_TYPE_BOOL
);
728 lua_setfield(L
, -2, "BOOLEAN");
730 /* used in our callbacks */
733 /* create the callback table */
734 lua_createtable(L
, 1, 0);
735 lua_setglobal(L
, "__ubus_cb");
737 /* create the event table */
738 lua_createtable(L
, 1, 0);
739 lua_setglobal(L
, "__ubus_cb_event");