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);
293 lua_call(state, 1, 0);
298 static int lua_gettablelen(lua_State *L, int index)
304 while (lua_next(L, index) != 0) {
312 static int ubus_lua_reply(lua_State *L)
314 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
315 struct ubus_request_data *req;
317 luaL_checktype(L, 3, LUA_TTABLE);
318 blob_buf_init(&c->buf, 0);
320 if (!ubus_lua_format_blob_array(L, &c->buf, true))
323 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
327 req = lua_touserdata(L, 2);
328 ubus_send_reply(c->ctx, req, c->buf.head);
333 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
335 struct blobmsg_policy *p;
339 /* get the function pointer */
340 lua_pushinteger(L, 1);
343 /* get the policy table */
344 lua_pushinteger(L, 2);
346 plen = lua_gettablelen(L, -1);
348 /* check if the method table is valid */
349 if ((lua_type(L, -2) != LUA_TFUNCTION) ||
350 (lua_type(L, -1) != LUA_TTABLE) ||
351 lua_objlen(L, -1) || !plen) {
356 /* store function pointer */
357 lua_pushvalue(L, -2);
358 lua_setfield(L, -6, lua_tostring(L, -5));
360 /* setup the policy pointers */
361 p = malloc(sizeof(struct blobmsg_policy) * plen);
362 memset(p, 0, sizeof(struct blobmsg_policy) * plen);
365 while (lua_next(L, -2) != 0) {
366 int val = lua_tointeger(L, -1);
368 /* check if the policy is valid */
369 if ((lua_type(L, -2) != LUA_TSTRING) ||
370 (lua_type(L, -1) != LUA_TNUMBER) ||
372 (val > BLOBMSG_TYPE_LAST)) {
376 p[pidx].name = lua_tostring(L, -2);
383 m->name = lua_tostring(L, -4);
384 m->handler = ubus_method_handler;
390 static struct ubus_object* ubus_lua_load_object(lua_State *L)
392 struct ubus_lua_object *obj = NULL;
393 int mlen = lua_gettablelen(L, -1);
394 struct ubus_method *m;
397 /* setup object pointers */
398 obj = malloc(sizeof(struct ubus_lua_object));
399 memset(obj, 0, sizeof(struct ubus_lua_object));
400 obj->o.name = lua_tostring(L, -2);
402 /* setup method pointers */
403 m = malloc(sizeof(struct ubus_method) * mlen);
404 memset(m, 0, sizeof(struct ubus_method) * mlen);
407 /* setup type pointers */
408 obj->o.type = malloc(sizeof(struct ubus_object_type));
409 memset(obj->o.type, 0, sizeof(struct ubus_object_type));
410 obj->o.type->name = lua_tostring(L, -2);
412 obj->o.type->methods = obj->o.methods;
414 /* create the he callback lookup table */
415 lua_createtable(L, 1, 0);
416 lua_getglobal(L, "__ubus_cb");
417 lua_pushvalue(L, -2);
418 obj->r = luaL_ref(L, -2);
421 /* scan each method */
423 while (lua_next(L, -3) != 0) {
424 /* check if it looks like a method */
425 if ((lua_type(L, -2) != LUA_TSTRING) ||
426 (lua_type(L, -1) != LUA_TTABLE) ||
427 !lua_objlen(L, -1)) {
432 if (!ubus_lua_load_methods(L, &m[midx]))
437 obj->o.type->n_methods = obj->o.n_methods = midx;
439 /* pop the callback table */
445 static int ubus_lua_add(lua_State *L)
447 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
449 /* verify top level object */
450 if (lua_istable(L, 1)) {
451 lua_pushstring(L, "you need to pass a table");
456 /* scan each object */
458 while (lua_next(L, -2) != 0) {
459 struct ubus_object *obj = NULL;
461 /* check if the object has a table of methods */
462 if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
463 obj = ubus_lua_load_object(L);
466 ubus_add_object(c->ctx, obj);
475 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
477 lua_State *L = (lua_State *)p;
482 ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
486 ubus_lua_signatures(lua_State *L)
489 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
490 const char *path = luaL_checkstring(L, 2);
492 rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
494 if (rv != UBUS_STATUS_OK)
498 lua_pushinteger(L, rv);
507 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
509 lua_State *L = (lua_State *)req->priv;
514 ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
518 ubus_lua_call(lua_State *L)
522 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
523 const char *path = luaL_checkstring(L, 2);
524 const char *func = luaL_checkstring(L, 3);
526 luaL_checktype(L, 4, LUA_TTABLE);
527 blob_buf_init(&c->buf, 0);
529 if (!ubus_lua_format_blob_array(L, &c->buf, true))
532 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
536 rv = ubus_lookup_id(c->ctx, path, &id);
541 lua_pushinteger(L, rv);
546 rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
548 if (rv != UBUS_STATUS_OK)
552 lua_pushinteger(L, rv);
556 return lua_gettop(L) - top;
561 ubus_lua__gc(lua_State *L)
563 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
568 memset(c, 0, sizeof(*c));
574 static const luaL_Reg ubus[] = {
575 { "connect", ubus_lua_connect },
576 { "objects", ubus_lua_objects },
577 { "add", ubus_lua_add },
578 { "reply", ubus_lua_reply },
579 { "signatures", ubus_lua_signatures },
580 { "call", ubus_lua_call },
581 { "close", ubus_lua__gc },
582 { "__gc", ubus_lua__gc },
586 /* avoid missing prototype warning */
587 int luaopen_ubus(lua_State *L);
590 luaopen_ubus(lua_State *L)
592 /* create metatable */
593 luaL_newmetatable(L, METANAME);
595 /* metatable.__index = metatable */
596 lua_pushvalue(L, -1);
597 lua_setfield(L, -2, "__index");
600 luaL_register(L, NULL, ubus);
604 luaL_register(L, MODNAME, ubus);
606 /* set some enum defines */
607 lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
608 lua_setfield(L, -2, "ARRAY");
609 lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
610 lua_setfield(L, -2, "TABLE");
611 lua_pushinteger(L, BLOBMSG_TYPE_STRING);
612 lua_setfield(L, -2, "STRING");
613 lua_pushinteger(L, BLOBMSG_TYPE_INT64);
614 lua_setfield(L, -2, "INT64");
615 lua_pushinteger(L, BLOBMSG_TYPE_INT32);
616 lua_setfield(L, -2, "INT32");
617 lua_pushinteger(L, BLOBMSG_TYPE_INT16);
618 lua_setfield(L, -2, "INT16");
619 lua_pushinteger(L, BLOBMSG_TYPE_INT8);
620 lua_setfield(L, -2, "INT8");
621 lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
622 lua_setfield(L, -2, "BOOLEAN");
624 /* used in our callbacks */
627 /* create the callback table */
628 lua_createtable(L, 1, 0);
629 lua_setglobal(L, "__ubus_cb");