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 {
39 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table);
42 ubus_lua_parse_blob_array(lua_State *L, struct blob_attr *attr, int len, bool table)
47 struct blob_attr *pos;
51 __blob_for_each_attr(pos, attr, rem)
53 rv = ubus_lua_parse_blob(L, pos, table);
58 lua_rawseti(L, -2, idx++);
65 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table)
72 if (!blobmsg_check_attr(attr, false))
75 if (table && blobmsg_name(attr)[0])
77 lua_pushstring(L, blobmsg_name(attr));
81 data = blobmsg_data(attr);
82 len = blobmsg_data_len(attr);
84 switch (blob_id(attr))
86 case BLOBMSG_TYPE_BOOL:
87 lua_pushboolean(L, *(uint8_t *)data);
90 case BLOBMSG_TYPE_INT16:
91 lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
94 case BLOBMSG_TYPE_INT32:
95 lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
98 case BLOBMSG_TYPE_INT64:
99 /* NB: Lua cannot handle 64bit, format value as string and push that */
100 sprintf(buf, "%lld", (long long int) be64_to_cpu(*(uint64_t *)data));
101 lua_pushstring(L, buf);
104 case BLOBMSG_TYPE_STRING:
105 lua_pushstring(L, data);
108 case BLOBMSG_TYPE_ARRAY:
109 ubus_lua_parse_blob_array(L, data, len, false);
112 case BLOBMSG_TYPE_TABLE:
113 ubus_lua_parse_blob_array(L, data, len, true);
126 ubus_lua_format_blob_is_array(lua_State *L)
131 /* Find out whether table is array-like */
132 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
135 if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
137 if (lua_type(L, -2) != LUA_TNUMBER)
144 cur = lua_tointeger(L, -2);
146 if ((cur - 1) != prv)
160 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
163 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
167 const char *key = table ? lua_tostring(L, -2) : NULL;
169 switch (lua_type(L, -1))
172 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
179 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
184 case LUA_TLIGHTUSERDATA:
185 blobmsg_add_string(b, key, lua_tostring(L, -1));
189 if (ubus_lua_format_blob_is_array(L))
191 c = blobmsg_open_array(b, key);
192 rv = ubus_lua_format_blob_array(L, b, false);
193 blobmsg_close_array(b, c);
197 c = blobmsg_open_table(b, key);
198 rv = ubus_lua_format_blob_array(L, b, true);
199 blobmsg_close_table(b, c);
212 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
214 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
216 if (!ubus_lua_format_blob(L, b, table))
228 ubus_lua_connect(lua_State *L)
230 struct ubus_lua_connection *c;
231 const char *sockpath = luaL_optstring(L, 1, NULL);
232 int timeout = luaL_optint(L, 2, 30);
234 if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
235 (c->ctx = ubus_connect(sockpath)) != NULL)
237 ubus_add_uloop(c->ctx);
238 c->timeout = timeout;
239 memset(&c->buf, 0, sizeof(c->buf));
240 luaL_getmetatable(L, METANAME);
241 lua_setmetatable(L, -2);
245 /* NB: no errors from ubus_connect() yet */
247 lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
253 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
255 lua_State *L = (lua_State *)p;
257 lua_pushstring(L, o->path);
258 lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
262 ubus_lua_objects(lua_State *L)
265 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
268 rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
270 if (rv != UBUS_STATUS_OK)
274 lua_pushinteger(L, rv);
282 ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
283 struct ubus_request_data *req, const char *method,
284 struct blob_attr *msg)
286 struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
288 lua_getglobal(state, "__ubus_cb");
289 lua_rawgeti(state, -1, o->r);
290 lua_getfield(state, -1, method);
292 if (lua_isfunction(state, -1)) {
293 lua_pushlightuserdata(state, req);
297 ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
298 lua_call(state, 2, 0);
303 static int lua_gettablelen(lua_State *L, int index)
309 while (lua_next(L, index) != 0) {
317 static int ubus_lua_reply(lua_State *L)
319 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
320 struct ubus_request_data *req;
322 luaL_checktype(L, 3, LUA_TTABLE);
323 blob_buf_init(&c->buf, 0);
325 if (!ubus_lua_format_blob_array(L, &c->buf, true))
328 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
332 req = lua_touserdata(L, 2);
333 ubus_send_reply(c->ctx, req, c->buf.head);
338 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
340 struct blobmsg_policy *p;
344 /* get the function pointer */
345 lua_pushinteger(L, 1);
348 /* get the policy table */
349 lua_pushinteger(L, 2);
351 plen = lua_gettablelen(L, -1);
353 /* check if the method table is valid */
354 if ((lua_type(L, -2) != LUA_TFUNCTION) ||
355 (lua_type(L, -1) != LUA_TTABLE) ||
356 lua_objlen(L, -1) || !plen) {
361 /* store function pointer */
362 lua_pushvalue(L, -2);
363 lua_setfield(L, -6, lua_tostring(L, -5));
365 /* setup the policy pointers */
366 p = malloc(sizeof(struct blobmsg_policy) * plen);
367 memset(p, 0, sizeof(struct blobmsg_policy) * plen);
370 while (lua_next(L, -2) != 0) {
371 int val = lua_tointeger(L, -1);
373 /* check if the policy is valid */
374 if ((lua_type(L, -2) != LUA_TSTRING) ||
375 (lua_type(L, -1) != LUA_TNUMBER) ||
377 (val > BLOBMSG_TYPE_LAST)) {
381 p[pidx].name = lua_tostring(L, -2);
388 m->name = lua_tostring(L, -4);
389 m->handler = ubus_method_handler;
395 static struct ubus_object* ubus_lua_load_object(lua_State *L)
397 struct ubus_lua_object *obj = NULL;
398 int mlen = lua_gettablelen(L, -1);
399 struct ubus_method *m;
402 /* setup object pointers */
403 obj = malloc(sizeof(struct ubus_lua_object));
404 memset(obj, 0, sizeof(struct ubus_lua_object));
405 obj->o.name = lua_tostring(L, -2);
407 /* setup method pointers */
408 m = malloc(sizeof(struct ubus_method) * mlen);
409 memset(m, 0, sizeof(struct ubus_method) * mlen);
412 /* setup type pointers */
413 obj->o.type = malloc(sizeof(struct ubus_object_type));
414 memset(obj->o.type, 0, sizeof(struct ubus_object_type));
415 obj->o.type->name = lua_tostring(L, -2);
417 obj->o.type->methods = obj->o.methods;
419 /* create the he callback lookup table */
420 lua_createtable(L, 1, 0);
421 lua_getglobal(L, "__ubus_cb");
422 lua_pushvalue(L, -2);
423 obj->r = luaL_ref(L, -2);
426 /* scan each method */
428 while (lua_next(L, -3) != 0) {
429 /* check if it looks like a method */
430 if ((lua_type(L, -2) != LUA_TSTRING) ||
431 (lua_type(L, -1) != LUA_TTABLE) ||
432 !lua_objlen(L, -1)) {
437 if (!ubus_lua_load_methods(L, &m[midx]))
442 obj->o.type->n_methods = obj->o.n_methods = midx;
444 /* pop the callback table */
450 static int ubus_lua_add(lua_State *L)
452 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
454 /* verify top level object */
455 if (lua_istable(L, 1)) {
456 lua_pushstring(L, "you need to pass a table");
461 /* scan each object */
463 while (lua_next(L, -2) != 0) {
464 struct ubus_object *obj = NULL;
466 /* check if the object has a table of methods */
467 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
468 obj = ubus_lua_load_object(L);
471 ubus_add_object(c->ctx, obj);
480 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
482 lua_State *L = (lua_State *)p;
487 ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
491 ubus_lua_signatures(lua_State *L)
494 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
495 const char *path = luaL_checkstring(L, 2);
497 rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
499 if (rv != UBUS_STATUS_OK)
503 lua_pushinteger(L, rv);
512 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
514 lua_State *L = (lua_State *)req->priv;
519 ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
523 ubus_lua_call(lua_State *L)
527 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
528 const char *path = luaL_checkstring(L, 2);
529 const char *func = luaL_checkstring(L, 3);
531 luaL_checktype(L, 4, LUA_TTABLE);
532 blob_buf_init(&c->buf, 0);
534 if (!ubus_lua_format_blob_array(L, &c->buf, true))
537 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
541 rv = ubus_lookup_id(c->ctx, path, &id);
546 lua_pushinteger(L, rv);
551 rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
553 if (rv != UBUS_STATUS_OK)
557 lua_pushinteger(L, rv);
561 return lua_gettop(L) - top;
566 ubus_lua__gc(lua_State *L)
568 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
573 memset(c, 0, sizeof(*c));
579 static const luaL_Reg ubus[] = {
580 { "connect", ubus_lua_connect },
581 { "objects", ubus_lua_objects },
582 { "add", ubus_lua_add },
583 { "reply", ubus_lua_reply },
584 { "signatures", ubus_lua_signatures },
585 { "call", ubus_lua_call },
586 { "close", ubus_lua__gc },
587 { "__gc", ubus_lua__gc },
591 /* avoid missing prototype warning */
592 int luaopen_ubus(lua_State *L);
595 luaopen_ubus(lua_State *L)
597 /* create metatable */
598 luaL_newmetatable(L, METANAME);
600 /* metatable.__index = metatable */
601 lua_pushvalue(L, -1);
602 lua_setfield(L, -2, "__index");
605 luaL_register(L, NULL, ubus);
609 luaL_register(L, MODNAME, ubus);
611 /* set some enum defines */
612 lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
613 lua_setfield(L, -2, "ARRAY");
614 lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
615 lua_setfield(L, -2, "TABLE");
616 lua_pushinteger(L, BLOBMSG_TYPE_STRING);
617 lua_setfield(L, -2, "STRING");
618 lua_pushinteger(L, BLOBMSG_TYPE_INT64);
619 lua_setfield(L, -2, "INT64");
620 lua_pushinteger(L, BLOBMSG_TYPE_INT32);
621 lua_setfield(L, -2, "INT32");
622 lua_pushinteger(L, BLOBMSG_TYPE_INT16);
623 lua_setfield(L, -2, "INT16");
624 lua_pushinteger(L, BLOBMSG_TYPE_INT8);
625 lua_setfield(L, -2, "INT8");
626 lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
627 lua_setfield(L, -2, "BOOLEAN");
629 /* used in our callbacks */
632 /* create the callback table */
633 lua_createtable(L, 1, 0);
634 lua_setglobal(L, "__ubus_cb");