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)
159 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
162 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
166 const char *key = table ? lua_tostring(L, -2) : NULL;
168 switch (lua_type(L, -1))
171 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
178 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
183 case LUA_TLIGHTUSERDATA:
184 blobmsg_add_string(b, key, lua_tostring(L, -1));
188 if (ubus_lua_format_blob_is_array(L))
190 c = blobmsg_open_array(b, key);
191 rv = ubus_lua_format_blob_array(L, b, false);
192 blobmsg_close_array(b, c);
196 c = blobmsg_open_table(b, key);
197 rv = ubus_lua_format_blob_array(L, b, true);
198 blobmsg_close_table(b, c);
211 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
213 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
215 if (!ubus_lua_format_blob(L, b, table))
227 ubus_lua_connect(lua_State *L)
229 struct ubus_lua_connection *c;
230 const char *sockpath = luaL_optstring(L, 1, NULL);
231 int timeout = luaL_optint(L, 2, 30);
233 if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
234 (c->ctx = ubus_connect(sockpath)) != NULL)
236 ubus_add_uloop(c->ctx);
237 c->timeout = timeout;
238 memset(&c->buf, 0, sizeof(c->buf));
239 luaL_getmetatable(L, METANAME);
240 lua_setmetatable(L, -2);
244 /* NB: no errors from ubus_connect() yet */
246 lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
252 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
254 lua_State *L = (lua_State *)p;
256 lua_pushstring(L, o->path);
257 lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
261 ubus_lua_objects(lua_State *L)
264 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
267 rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
269 if (rv != UBUS_STATUS_OK)
273 lua_pushinteger(L, rv);
281 ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
282 struct ubus_request_data *req, const char *method,
283 struct blob_attr *msg)
285 struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
287 lua_getglobal(state, "__ubus_cb");
288 lua_rawgeti(state, -1, o->r);
289 lua_getfield(state, -1, method);
291 if (lua_isfunction(state, -1)) {
292 lua_pushlightuserdata(state, req);
296 ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
297 lua_call(state, 2, 0);
302 static int lua_gettablelen(lua_State *L, int index)
308 while (lua_next(L, index) != 0) {
316 static int ubus_lua_reply(lua_State *L)
318 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
319 struct ubus_request_data *req;
321 luaL_checktype(L, 3, LUA_TTABLE);
322 blob_buf_init(&c->buf, 0);
324 if (!ubus_lua_format_blob_array(L, &c->buf, true))
327 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
331 req = lua_touserdata(L, 2);
332 ubus_send_reply(c->ctx, req, c->buf.head);
337 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
339 struct blobmsg_policy *p;
343 /* get the function pointer */
344 lua_pushinteger(L, 1);
347 /* get the policy table */
348 lua_pushinteger(L, 2);
350 plen = lua_gettablelen(L, -1);
352 /* check if the method table is valid */
353 if ((lua_type(L, -2) != LUA_TFUNCTION) ||
354 (lua_type(L, -1) != LUA_TTABLE) ||
355 lua_objlen(L, -1) || !plen) {
360 /* store function pointer */
361 lua_pushvalue(L, -2);
362 lua_setfield(L, -6, lua_tostring(L, -5));
364 /* setup the policy pointers */
365 p = malloc(sizeof(struct blobmsg_policy) * plen);
366 memset(p, 0, sizeof(struct blobmsg_policy) * plen);
369 while (lua_next(L, -2) != 0) {
370 int val = lua_tointeger(L, -1);
372 /* check if the policy is valid */
373 if ((lua_type(L, -2) != LUA_TSTRING) ||
374 (lua_type(L, -1) != LUA_TNUMBER) ||
376 (val > BLOBMSG_TYPE_LAST)) {
380 p[pidx].name = lua_tostring(L, -2);
387 m->name = lua_tostring(L, -4);
388 m->handler = ubus_method_handler;
394 static struct ubus_object* ubus_lua_load_object(lua_State *L)
396 struct ubus_lua_object *obj = NULL;
397 int mlen = lua_gettablelen(L, -1);
398 struct ubus_method *m;
401 /* setup object pointers */
402 obj = malloc(sizeof(struct ubus_lua_object));
403 memset(obj, 0, sizeof(struct ubus_lua_object));
404 obj->o.name = lua_tostring(L, -2);
406 /* setup method pointers */
407 m = malloc(sizeof(struct ubus_method) * mlen);
408 memset(m, 0, sizeof(struct ubus_method) * mlen);
411 /* setup type pointers */
412 obj->o.type = malloc(sizeof(struct ubus_object_type));
413 memset(obj->o.type, 0, sizeof(struct ubus_object_type));
414 obj->o.type->name = lua_tostring(L, -2);
416 obj->o.type->methods = obj->o.methods;
418 /* create the he callback lookup table */
419 lua_createtable(L, 1, 0);
420 lua_getglobal(L, "__ubus_cb");
421 lua_pushvalue(L, -2);
422 obj->r = luaL_ref(L, -2);
425 /* scan each method */
427 while (lua_next(L, -3) != 0) {
428 /* check if it looks like a method */
429 if ((lua_type(L, -2) != LUA_TSTRING) ||
430 (lua_type(L, -1) != LUA_TTABLE) ||
431 !lua_objlen(L, -1)) {
436 if (!ubus_lua_load_methods(L, &m[midx]))
441 obj->o.type->n_methods = obj->o.n_methods = midx;
443 /* pop the callback table */
449 static int ubus_lua_add(lua_State *L)
451 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
453 /* verify top level object */
454 if (lua_istable(L, 1)) {
455 lua_pushstring(L, "you need to pass a table");
460 /* scan each object */
462 while (lua_next(L, -2) != 0) {
463 struct ubus_object *obj = NULL;
465 /* check if the object has a table of methods */
466 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
467 obj = ubus_lua_load_object(L);
470 ubus_add_object(c->ctx, obj);
479 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
481 lua_State *L = (lua_State *)p;
486 ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
490 ubus_lua_signatures(lua_State *L)
493 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
494 const char *path = luaL_checkstring(L, 2);
496 rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
498 if (rv != UBUS_STATUS_OK)
502 lua_pushinteger(L, rv);
511 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
513 lua_State *L = (lua_State *)req->priv;
518 ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
522 ubus_lua_call(lua_State *L)
526 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
527 const char *path = luaL_checkstring(L, 2);
528 const char *func = luaL_checkstring(L, 3);
530 luaL_checktype(L, 4, LUA_TTABLE);
531 blob_buf_init(&c->buf, 0);
533 if (!ubus_lua_format_blob_array(L, &c->buf, true))
536 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
540 rv = ubus_lookup_id(c->ctx, path, &id);
545 lua_pushinteger(L, rv);
550 rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
552 if (rv != UBUS_STATUS_OK)
556 lua_pushinteger(L, rv);
560 return lua_gettop(L) - top;
565 ubus_lua__gc(lua_State *L)
567 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
572 memset(c, 0, sizeof(*c));
578 static const luaL_Reg ubus[] = {
579 { "connect", ubus_lua_connect },
580 { "objects", ubus_lua_objects },
581 { "add", ubus_lua_add },
582 { "reply", ubus_lua_reply },
583 { "signatures", ubus_lua_signatures },
584 { "call", ubus_lua_call },
585 { "close", ubus_lua__gc },
586 { "__gc", ubus_lua__gc },
590 /* avoid missing prototype warning */
591 int luaopen_ubus(lua_State *L);
594 luaopen_ubus(lua_State *L)
596 /* create metatable */
597 luaL_newmetatable(L, METANAME);
599 /* metatable.__index = metatable */
600 lua_pushvalue(L, -1);
601 lua_setfield(L, -2, "__index");
604 luaL_register(L, NULL, ubus);
608 luaL_register(L, MODNAME, ubus);
610 /* set some enum defines */
611 lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
612 lua_setfield(L, -2, "ARRAY");
613 lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
614 lua_setfield(L, -2, "TABLE");
615 lua_pushinteger(L, BLOBMSG_TYPE_STRING);
616 lua_setfield(L, -2, "STRING");
617 lua_pushinteger(L, BLOBMSG_TYPE_INT64);
618 lua_setfield(L, -2, "INT64");
619 lua_pushinteger(L, BLOBMSG_TYPE_INT32);
620 lua_setfield(L, -2, "INT32");
621 lua_pushinteger(L, BLOBMSG_TYPE_INT16);
622 lua_setfield(L, -2, "INT16");
623 lua_pushinteger(L, BLOBMSG_TYPE_INT8);
624 lua_setfield(L, -2, "INT8");
625 lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
626 lua_setfield(L, -2, "BOOLEAN");
628 /* used in our callbacks */
631 /* create the callback table */
632 lua_createtable(L, 1, 0);
633 lua_setglobal(L, "__ubus_cb");