2 * Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
16 #include <libubox/blobmsg.h>
20 #define MODNAME "ubus"
21 #define METANAME MODNAME ".meta"
24 struct ubus_lua_connection
{
27 struct ubus_context
*ctx
;
32 ubus_lua_parse_blob(lua_State
*L
, struct blob_attr
*attr
, bool table
);
35 ubus_lua_parse_blob_array(lua_State
*L
, struct blob_attr
*attr
, int len
, bool table
)
40 struct blob_attr
*pos
;
44 __blob_for_each_attr(pos
, attr
, rem
)
46 rv
= ubus_lua_parse_blob(L
, pos
, table
);
51 lua_rawseti(L
, -2, idx
++);
58 ubus_lua_parse_blob(lua_State
*L
, struct blob_attr
*attr
, bool table
)
65 if (!blobmsg_check_attr(attr
, false))
68 if (table
&& blobmsg_name(attr
)[0])
70 lua_pushstring(L
, blobmsg_name(attr
));
74 data
= blobmsg_data(attr
);
75 len
= blobmsg_data_len(attr
);
77 switch (blob_id(attr
))
79 case BLOBMSG_TYPE_BOOL
:
80 lua_pushboolean(L
, *(uint8_t *)data
);
83 case BLOBMSG_TYPE_INT16
:
84 lua_pushinteger(L
, be16_to_cpu(*(uint16_t *)data
));
87 case BLOBMSG_TYPE_INT32
:
88 lua_pushinteger(L
, be32_to_cpu(*(uint32_t *)data
));
91 case BLOBMSG_TYPE_INT64
:
92 /* NB: Lua cannot handle 64bit, format value as string and push that */
93 sprintf(buf
, "%lld", (long long int) be64_to_cpu(*(uint64_t *)data
));
94 lua_pushstring(L
, buf
);
97 case BLOBMSG_TYPE_STRING
:
98 lua_pushstring(L
, data
);
101 case BLOBMSG_TYPE_ARRAY
:
102 ubus_lua_parse_blob_array(L
, data
, len
, false);
105 case BLOBMSG_TYPE_TABLE
:
106 ubus_lua_parse_blob_array(L
, data
, len
, true);
119 ubus_lua_format_blob_is_array(lua_State
*L
)
124 /* Find out whether table is array-like */
125 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1))
127 if (lua_type(L
, -2) != LUA_TNUMBER
&& lua_type(L
, -2) != LUA_TINT
)
133 cur
= lua_tointeger(L
, -2);
135 if ((cur
- 1) != prv
)
148 ubus_lua_format_blob_array(lua_State
*L
, struct blob_buf
*b
, bool table
);
151 ubus_lua_format_blob(lua_State
*L
, struct blob_buf
*b
, bool table
)
155 const char *key
= table
? lua_tostring(L
, -2) : NULL
;
157 switch (lua_type(L
, -1))
160 blobmsg_add_u8(b
, key
, (uint8_t)lua_toboolean(L
, -1));
165 blobmsg_add_u32(b
, key
, (uint32_t)lua_tointeger(L
, -1));
170 case LUA_TLIGHTUSERDATA
:
171 blobmsg_add_string(b
, key
, lua_tostring(L
, -1));
175 if (ubus_lua_format_blob_is_array(L
))
177 c
= blobmsg_open_array(b
, key
);
178 rv
= ubus_lua_format_blob_array(L
, b
, false);
179 blobmsg_close_array(b
, c
);
183 c
= blobmsg_open_table(b
, key
);
184 rv
= ubus_lua_format_blob_array(L
, b
, true);
185 blobmsg_close_table(b
, c
);
198 ubus_lua_format_blob_array(lua_State
*L
, struct blob_buf
*b
, bool table
)
200 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1))
202 if (!ubus_lua_format_blob(L
, b
, table
))
214 ubus_lua_connect(lua_State
*L
)
216 struct ubus_lua_connection
*c
;
217 const char *sockpath
= luaL_optstring(L
, 1, NULL
);
218 int timeout
= luaL_optint(L
, 2, 30);
220 if ((c
= lua_newuserdata(L
, sizeof(*c
))) != NULL
&&
221 (c
->ctx
= ubus_connect(sockpath
)) != NULL
)
223 c
->timeout
= timeout
;
224 memset(&c
->buf
, 0, sizeof(c
->buf
));
225 luaL_getmetatable(L
, METANAME
);
226 lua_setmetatable(L
, -2);
230 /* NB: no errors from ubus_connect() yet */
232 lua_pushinteger(L
, UBUS_STATUS_UNKNOWN_ERROR
);
238 ubus_lua_objects_cb(struct ubus_context
*c
, struct ubus_object_data
*o
, void *p
)
240 lua_State
*L
= (lua_State
*)p
;
242 lua_pushstring(L
, o
->path
);
243 lua_rawseti(L
, -2, lua_objlen(L
, -2) + 1);
247 ubus_lua_objects(lua_State
*L
)
250 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
253 rv
= ubus_lookup(c
->ctx
, NULL
, ubus_lua_objects_cb
, L
);
255 if (rv
!= UBUS_STATUS_OK
)
259 lua_pushinteger(L
, rv
);
268 ubus_lua_signatures_cb(struct ubus_context
*c
, struct ubus_object_data
*o
, void *p
)
270 lua_State
*L
= (lua_State
*)p
;
275 ubus_lua_parse_blob_array(L
, blob_data(o
->signature
), blob_len(o
->signature
), true);
279 ubus_lua_signatures(lua_State
*L
)
282 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
283 const char *path
= luaL_checkstring(L
, 2);
285 rv
= ubus_lookup(c
->ctx
, path
, ubus_lua_signatures_cb
, L
);
287 if (rv
!= UBUS_STATUS_OK
)
291 lua_pushinteger(L
, rv
);
300 ubus_lua_call_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
302 lua_State
*L
= (lua_State
*)req
->priv
;
307 ubus_lua_parse_blob_array(L
, blob_data(msg
), blob_len(msg
), true);
311 ubus_lua_call(lua_State
*L
)
315 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
316 const char *path
= luaL_checkstring(L
, 2);
317 const char *func
= luaL_checkstring(L
, 3);
319 luaL_checktype(L
, 4, LUA_TTABLE
);
320 blob_buf_init(&c
->buf
, 0);
322 if (!ubus_lua_format_blob_array(L
, &c
->buf
, true))
325 lua_pushinteger(L
, UBUS_STATUS_INVALID_ARGUMENT
);
329 rv
= ubus_lookup_id(c
->ctx
, path
, &id
);
334 lua_pushinteger(L
, rv
);
338 rv
= ubus_invoke(c
->ctx
, id
, func
, c
->buf
.head
, ubus_lua_call_cb
, L
, c
->timeout
* 1000);
340 if (rv
!= UBUS_STATUS_OK
)
344 lua_pushinteger(L
, rv
);
353 ubus_lua__gc(lua_State
*L
)
355 struct ubus_lua_connection
*c
= luaL_checkudata(L
, 1, METANAME
);
360 memset(c
, 0, sizeof(*c
));
366 static const luaL_Reg ubus
[] = {
367 { "connect", ubus_lua_connect
},
368 { "objects", ubus_lua_objects
},
369 { "signatures", ubus_lua_signatures
},
370 { "call", ubus_lua_call
},
371 { "close", ubus_lua__gc
},
372 { "__gc", ubus_lua__gc
},
376 /* avoid missing prototype warning */
377 int luaopen_ubus(lua_State
*L
);
380 luaopen_ubus(lua_State
*L
)
382 /* create metatable */
383 luaL_newmetatable(L
, METANAME
);
385 /* metatable.__index = metatable */
386 lua_pushvalue(L
, -1);
387 lua_setfield(L
, -2, "__index");
390 luaL_register(L
, NULL
, ubus
);
394 luaL_register(L
, MODNAME
, ubus
);