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)
71 if (!blobmsg_check_attr(attr, false))
74 if (table && blobmsg_name(attr)[0])
76 lua_pushstring(L, blobmsg_name(attr));
80 data = blobmsg_data(attr);
81 len = blobmsg_data_len(attr);
83 switch (blob_id(attr))
85 case BLOBMSG_TYPE_BOOL:
86 lua_pushboolean(L, *(uint8_t *)data);
89 case BLOBMSG_TYPE_INT16:
90 lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
93 case BLOBMSG_TYPE_INT32:
94 lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
97 case BLOBMSG_TYPE_INT64:
98 lua_pushnumber(L, (double) be64_to_cpu(*(uint64_t *)data));
101 case BLOBMSG_TYPE_STRING:
102 lua_pushstring(L, data);
105 case BLOBMSG_TYPE_ARRAY:
106 ubus_lua_parse_blob_array(L, data, len, false);
109 case BLOBMSG_TYPE_TABLE:
110 ubus_lua_parse_blob_array(L, data, len, true);
123 ubus_lua_format_blob_is_array(lua_State *L)
128 /* Find out whether table is array-like */
129 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
132 if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
134 if (lua_type(L, -2) != LUA_TNUMBER)
141 cur = lua_tointeger(L, -2);
143 if ((cur - 1) != prv)
156 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
159 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
163 const char *key = table ? lua_tostring(L, -2) : NULL;
165 switch (lua_type(L, -1))
168 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
175 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
180 case LUA_TLIGHTUSERDATA:
181 blobmsg_add_string(b, key, lua_tostring(L, -1));
185 if (ubus_lua_format_blob_is_array(L))
187 c = blobmsg_open_array(b, key);
188 rv = ubus_lua_format_blob_array(L, b, false);
189 blobmsg_close_array(b, c);
193 c = blobmsg_open_table(b, key);
194 rv = ubus_lua_format_blob_array(L, b, true);
195 blobmsg_close_table(b, c);
208 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
210 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
212 if (!ubus_lua_format_blob(L, b, table))
224 ubus_lua_connect(lua_State *L)
226 struct ubus_lua_connection *c;
227 const char *sockpath = luaL_optstring(L, 1, NULL);
228 int timeout = luaL_optint(L, 2, 30);
230 if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
231 (c->ctx = ubus_connect(sockpath)) != NULL)
233 ubus_add_uloop(c->ctx);
234 c->timeout = timeout;
235 memset(&c->buf, 0, sizeof(c->buf));
236 luaL_getmetatable(L, METANAME);
237 lua_setmetatable(L, -2);
241 /* NB: no errors from ubus_connect() yet */
243 lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
249 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
251 lua_State *L = (lua_State *)p;
253 lua_pushstring(L, o->path);
254 lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
258 ubus_lua_objects(lua_State *L)
261 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
264 rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
266 if (rv != UBUS_STATUS_OK)
270 lua_pushinteger(L, rv);
278 ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
279 struct ubus_request_data *req, const char *method,
280 struct blob_attr *msg)
282 struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
284 lua_getglobal(state, "__ubus_cb");
285 lua_rawgeti(state, -1, o->r);
286 lua_getfield(state, -1, method);
288 if (lua_isfunction(state, -1)) {
289 lua_pushlightuserdata(state, req);
293 ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
294 lua_call(state, 2, 0);
299 static int lua_gettablelen(lua_State *L, int index)
305 while (lua_next(L, index) != 0) {
313 static int ubus_lua_reply(lua_State *L)
315 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
316 struct ubus_request_data *req;
318 luaL_checktype(L, 3, LUA_TTABLE);
319 blob_buf_init(&c->buf, 0);
321 if (!ubus_lua_format_blob_array(L, &c->buf, true))
324 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
328 req = lua_touserdata(L, 2);
329 ubus_send_reply(c->ctx, req, c->buf.head);
334 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
336 struct blobmsg_policy *p;
340 /* get the function pointer */
341 lua_pushinteger(L, 1);
344 /* get the policy table */
345 lua_pushinteger(L, 2);
347 plen = lua_gettablelen(L, -1);
349 /* check if the method table is valid */
350 if ((lua_type(L, -2) != LUA_TFUNCTION) ||
351 (lua_type(L, -1) != LUA_TTABLE) ||
352 lua_objlen(L, -1) || !plen) {
357 /* store function pointer */
358 lua_pushvalue(L, -2);
359 lua_setfield(L, -6, lua_tostring(L, -5));
361 /* setup the policy pointers */
362 p = malloc(sizeof(struct blobmsg_policy) * plen);
363 memset(p, 0, sizeof(struct blobmsg_policy) * plen);
366 while (lua_next(L, -2) != 0) {
367 int val = lua_tointeger(L, -1);
369 /* check if the policy is valid */
370 if ((lua_type(L, -2) != LUA_TSTRING) ||
371 (lua_type(L, -1) != LUA_TNUMBER) ||
373 (val > BLOBMSG_TYPE_LAST)) {
377 p[pidx].name = lua_tostring(L, -2);
384 m->name = lua_tostring(L, -4);
385 m->handler = ubus_method_handler;
391 static struct ubus_object* ubus_lua_load_object(lua_State *L)
393 struct ubus_lua_object *obj = NULL;
394 int mlen = lua_gettablelen(L, -1);
395 struct ubus_method *m;
398 /* setup object pointers */
399 obj = malloc(sizeof(struct ubus_lua_object));
400 memset(obj, 0, sizeof(struct ubus_lua_object));
401 obj->o.name = lua_tostring(L, -2);
403 /* setup method pointers */
404 m = malloc(sizeof(struct ubus_method) * mlen);
405 memset(m, 0, sizeof(struct ubus_method) * mlen);
408 /* setup type pointers */
409 obj->o.type = malloc(sizeof(struct ubus_object_type));
410 memset(obj->o.type, 0, sizeof(struct ubus_object_type));
411 obj->o.type->name = lua_tostring(L, -2);
413 obj->o.type->methods = obj->o.methods;
415 /* create the he callback lookup table */
416 lua_createtable(L, 1, 0);
417 lua_getglobal(L, "__ubus_cb");
418 lua_pushvalue(L, -2);
419 obj->r = luaL_ref(L, -2);
422 /* scan each method */
424 while (lua_next(L, -3) != 0) {
425 /* check if it looks like a method */
426 if ((lua_type(L, -2) != LUA_TSTRING) ||
427 (lua_type(L, -1) != LUA_TTABLE) ||
428 !lua_objlen(L, -1)) {
433 if (!ubus_lua_load_methods(L, &m[midx]))
438 obj->o.type->n_methods = obj->o.n_methods = midx;
440 /* pop the callback table */
446 static int ubus_lua_add(lua_State *L)
448 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
450 /* verify top level object */
451 if (lua_istable(L, 1)) {
452 lua_pushstring(L, "you need to pass a table");
457 /* scan each object */
459 while (lua_next(L, -2) != 0) {
460 struct ubus_object *obj = NULL;
462 /* check if the object has a table of methods */
463 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
464 obj = ubus_lua_load_object(L);
467 ubus_add_object(c->ctx, obj);
476 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
478 lua_State *L = (lua_State *)p;
483 ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
487 ubus_lua_signatures(lua_State *L)
490 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
491 const char *path = luaL_checkstring(L, 2);
493 rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
495 if (rv != UBUS_STATUS_OK)
499 lua_pushinteger(L, rv);
508 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
510 lua_State *L = (lua_State *)req->priv;
515 ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
519 ubus_lua_call(lua_State *L)
523 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
524 const char *path = luaL_checkstring(L, 2);
525 const char *func = luaL_checkstring(L, 3);
527 luaL_checktype(L, 4, LUA_TTABLE);
528 blob_buf_init(&c->buf, 0);
530 if (!ubus_lua_format_blob_array(L, &c->buf, true))
533 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
537 rv = ubus_lookup_id(c->ctx, path, &id);
542 lua_pushinteger(L, rv);
547 rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
549 if (rv != UBUS_STATUS_OK)
553 lua_pushinteger(L, rv);
557 return lua_gettop(L) - top;
562 ubus_lua__gc(lua_State *L)
564 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
569 memset(c, 0, sizeof(*c));
575 static const luaL_Reg ubus[] = {
576 { "connect", ubus_lua_connect },
577 { "objects", ubus_lua_objects },
578 { "add", ubus_lua_add },
579 { "reply", ubus_lua_reply },
580 { "signatures", ubus_lua_signatures },
581 { "call", ubus_lua_call },
582 { "close", ubus_lua__gc },
583 { "__gc", ubus_lua__gc },
587 /* avoid missing prototype warning */
588 int luaopen_ubus(lua_State *L);
591 luaopen_ubus(lua_State *L)
593 /* create metatable */
594 luaL_newmetatable(L, METANAME);
596 /* metatable.__index = metatable */
597 lua_pushvalue(L, -1);
598 lua_setfield(L, -2, "__index");
601 luaL_register(L, NULL, ubus);
605 luaL_register(L, MODNAME, ubus);
607 /* set some enum defines */
608 lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
609 lua_setfield(L, -2, "ARRAY");
610 lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
611 lua_setfield(L, -2, "TABLE");
612 lua_pushinteger(L, BLOBMSG_TYPE_STRING);
613 lua_setfield(L, -2, "STRING");
614 lua_pushinteger(L, BLOBMSG_TYPE_INT64);
615 lua_setfield(L, -2, "INT64");
616 lua_pushinteger(L, BLOBMSG_TYPE_INT32);
617 lua_setfield(L, -2, "INT32");
618 lua_pushinteger(L, BLOBMSG_TYPE_INT16);
619 lua_setfield(L, -2, "INT16");
620 lua_pushinteger(L, BLOBMSG_TYPE_INT8);
621 lua_setfield(L, -2, "INT8");
622 lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
623 lua_setfield(L, -2, "BOOLEAN");
625 /* used in our callbacks */
628 /* create the callback table */
629 lua_createtable(L, 1, 0);
630 lua_setglobal(L, "__ubus_cb");