add lua binding test scripts
[project/ubus.git] / lua / ubus.c
1 /*
2 * Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
3 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 #include <unistd.h>
16 #include <libubus.h>
17 #include <libubox/blobmsg.h>
18 #include <libubox/blobmsg_json.h>
19 #include <lauxlib.h>
20
21
22 #define MODNAME "ubus"
23 #define METANAME MODNAME ".meta"
24
25 static lua_State *state;
26
27 struct ubus_lua_connection {
28 int timeout;
29 struct blob_buf buf;
30 struct ubus_context *ctx;
31 };
32
33 struct ubus_lua_object {
34 struct ubus_object o;
35 int r;
36 };
37
38 static int
39 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table);
40
41 static int
42 ubus_lua_parse_blob_array(lua_State *L, struct blob_attr *attr, int len, bool table)
43 {
44 int rv;
45 int idx = 1;
46 int rem = len;
47 struct blob_attr *pos;
48
49 lua_newtable(L);
50
51 __blob_for_each_attr(pos, attr, rem)
52 {
53 rv = ubus_lua_parse_blob(L, pos, table);
54
55 if (rv > 1)
56 lua_rawset(L, -3);
57 else if (rv > 0)
58 lua_rawseti(L, -2, idx++);
59 }
60
61 return 1;
62 }
63
64 static int
65 ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table)
66 {
67 int len;
68 int off = 0;
69 void *data;
70 char buf[32];
71
72 if (!blobmsg_check_attr(attr, false))
73 return 0;
74
75 if (table && blobmsg_name(attr)[0])
76 {
77 lua_pushstring(L, blobmsg_name(attr));
78 off++;
79 }
80
81 data = blobmsg_data(attr);
82 len = blobmsg_data_len(attr);
83
84 switch (blob_id(attr))
85 {
86 case BLOBMSG_TYPE_BOOL:
87 lua_pushboolean(L, *(uint8_t *)data);
88 break;
89
90 case BLOBMSG_TYPE_INT16:
91 lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
92 break;
93
94 case BLOBMSG_TYPE_INT32:
95 lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
96 break;
97
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);
102 break;
103
104 case BLOBMSG_TYPE_STRING:
105 lua_pushstring(L, data);
106 break;
107
108 case BLOBMSG_TYPE_ARRAY:
109 ubus_lua_parse_blob_array(L, data, len, false);
110 break;
111
112 case BLOBMSG_TYPE_TABLE:
113 ubus_lua_parse_blob_array(L, data, len, true);
114 break;
115
116 default:
117 lua_pushnil(L);
118 break;
119 }
120
121 return off + 1;
122 }
123
124
125 static bool
126 ubus_lua_format_blob_is_array(lua_State *L)
127 {
128 lua_Integer prv = 0;
129 lua_Integer cur = 0;
130
131 /* Find out whether table is array-like */
132 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
133 {
134 #ifdef LUA_TINT
135 if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
136 #else
137 if (lua_type(L, -2) != LUA_TNUMBER)
138 #endif
139 {
140 lua_pop(L, 1);
141 return false;
142 }
143
144 cur = lua_tointeger(L, -2);
145
146 if ((cur - 1) != prv)
147 {
148 lua_pop(L, 1);
149 return false;
150 }
151
152 prv = cur;
153 }
154
155 return true;
156 }
157
158 static int
159 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
160
161 static int
162 ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
163 {
164 void *c;
165 bool rv = true;
166 const char *key = table ? lua_tostring(L, -2) : NULL;
167
168 switch (lua_type(L, -1))
169 {
170 case LUA_TBOOLEAN:
171 blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
172 break;
173
174 #ifdef LUA_TINT
175 case LUA_TINT:
176 #endif
177 case LUA_TNUMBER:
178 blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
179 break;
180
181 case LUA_TSTRING:
182 case LUA_TUSERDATA:
183 case LUA_TLIGHTUSERDATA:
184 blobmsg_add_string(b, key, lua_tostring(L, -1));
185 break;
186
187 case LUA_TTABLE:
188 if (ubus_lua_format_blob_is_array(L))
189 {
190 c = blobmsg_open_array(b, key);
191 rv = ubus_lua_format_blob_array(L, b, false);
192 blobmsg_close_array(b, c);
193 }
194 else
195 {
196 c = blobmsg_open_table(b, key);
197 rv = ubus_lua_format_blob_array(L, b, true);
198 blobmsg_close_table(b, c);
199 }
200 break;
201
202 default:
203 rv = false;
204 break;
205 }
206
207 return rv;
208 }
209
210 static int
211 ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
212 {
213 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
214 {
215 if (!ubus_lua_format_blob(L, b, table))
216 {
217 lua_pop(L, 1);
218 return false;
219 }
220 }
221
222 return true;
223 }
224
225
226 static int
227 ubus_lua_connect(lua_State *L)
228 {
229 struct ubus_lua_connection *c;
230 const char *sockpath = luaL_optstring(L, 1, NULL);
231 int timeout = luaL_optint(L, 2, 30);
232
233 if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
234 (c->ctx = ubus_connect(sockpath)) != NULL)
235 {
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);
241 return 1;
242 }
243
244 /* NB: no errors from ubus_connect() yet */
245 lua_pushnil(L);
246 lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
247 return 2;
248 }
249
250
251 static void
252 ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
253 {
254 lua_State *L = (lua_State *)p;
255
256 lua_pushstring(L, o->path);
257 lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
258 }
259
260 static int
261 ubus_lua_objects(lua_State *L)
262 {
263 int rv;
264 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
265
266 lua_newtable(L);
267 rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
268
269 if (rv != UBUS_STATUS_OK)
270 {
271 lua_pop(L, 1);
272 lua_pushnil(L);
273 lua_pushinteger(L, rv);
274 return 2;
275 }
276
277 return 1;
278 }
279
280 static int
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)
284 {
285 struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
286
287 lua_getglobal(state, "__ubus_cb");
288 lua_rawgeti(state, -1, o->r);
289 lua_getfield(state, -1, method);
290
291 if (lua_isfunction(state, -1)) {
292 lua_pushlightuserdata(state, req);
293 lua_call(state, 1, 0);
294 }
295 return 0;
296 }
297
298 static int lua_gettablelen(lua_State *L, int index)
299 {
300 int cnt = 0;
301
302 lua_pushnil(L);
303 index -= 1;
304 while (lua_next(L, index) != 0) {
305 cnt++;
306 lua_pop(L, 1);
307 }
308
309 return cnt;
310 }
311
312 static int ubus_lua_reply(lua_State *L)
313 {
314 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
315 struct ubus_request_data *req;
316
317 luaL_checktype(L, 3, LUA_TTABLE);
318 blob_buf_init(&c->buf, 0);
319
320 if (!ubus_lua_format_blob_array(L, &c->buf, true))
321 {
322 lua_pushnil(L);
323 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
324 return 2;
325 }
326
327 req = lua_touserdata(L, 2);
328 ubus_send_reply(c->ctx, req, c->buf.head);
329
330 return 0;
331 }
332
333 static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
334 {
335 struct blobmsg_policy *p;
336 int plen;
337 int pidx = 0;
338
339 /* get the function pointer */
340 lua_pushinteger(L, 1);
341 lua_gettable(L, -2);
342
343 /* get the policy table */
344 lua_pushinteger(L, 2);
345 lua_gettable(L, -3);
346 plen = lua_gettablelen(L, -1);
347
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) {
352 lua_pop(L, 2);
353 return 1;
354 }
355
356 /* store function pointer */
357 lua_pushvalue(L, -2);
358 lua_setfield(L, -6, lua_tostring(L, -5));
359
360 /* setup the policy pointers */
361 p = malloc(sizeof(struct blobmsg_policy) * plen);
362 memset(p, 0, sizeof(struct blobmsg_policy) * plen);
363 m->policy = p;
364 lua_pushnil(L);
365 while (lua_next(L, -2) != 0) {
366 int val = lua_tointeger(L, -1);
367
368 /* check if the policy is valid */
369 if ((lua_type(L, -2) != LUA_TSTRING) ||
370 (lua_type(L, -1) != LUA_TNUMBER) ||
371 (val < 0) ||
372 (val > BLOBMSG_TYPE_LAST)) {
373 lua_pop(L, 1);
374 continue;
375 }
376 p[pidx].name = lua_tostring(L, -2);
377 p[pidx].type = val;
378 lua_pop(L, 1);
379 pidx++;
380 }
381
382 m->n_policy = pidx;
383 m->name = lua_tostring(L, -4);
384 m->handler = ubus_method_handler;
385 lua_pop(L, 2);
386
387 return 0;
388 }
389
390 static struct ubus_object* ubus_lua_load_object(lua_State *L)
391 {
392 struct ubus_lua_object *obj = NULL;
393 int mlen = lua_gettablelen(L, -1);
394 struct ubus_method *m;
395 int midx = 0;
396
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);
401
402 /* setup method pointers */
403 m = malloc(sizeof(struct ubus_method) * mlen);
404 memset(m, 0, sizeof(struct ubus_method) * mlen);
405 obj->o.methods = m;
406
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);
411 obj->o.type->id = 0;
412 obj->o.type->methods = obj->o.methods;
413
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);
419 lua_pop(L, 1);
420
421 /* scan each method */
422 lua_pushnil(L);
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)) {
428 lua_pop(L, 1);
429 continue;
430 }
431
432 if (!ubus_lua_load_methods(L, &m[midx]))
433 midx++;
434 lua_pop(L, 1);
435 }
436
437 obj->o.type->n_methods = obj->o.n_methods = midx;
438
439 /* pop the callback table */
440 lua_pop(L, 1);
441
442 return &obj->o;
443 }
444
445 static int ubus_lua_add(lua_State *L)
446 {
447 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
448
449 /* verify top level object */
450 if (lua_istable(L, 1)) {
451 lua_pushstring(L, "you need to pass a table");
452 lua_error(L);
453 return 0;
454 }
455
456 /* scan each object */
457 lua_pushnil(L);
458 while (lua_next(L, -2) != 0) {
459 struct ubus_object *obj = NULL;
460
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);
464
465 if (obj)
466 ubus_add_object(c->ctx, obj);
467 }
468 lua_pop(L, 1);
469 }
470
471 return 0;
472 }
473
474 static void
475 ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
476 {
477 lua_State *L = (lua_State *)p;
478
479 if (!o->signature)
480 return;
481
482 ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
483 }
484
485 static int
486 ubus_lua_signatures(lua_State *L)
487 {
488 int rv;
489 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
490 const char *path = luaL_checkstring(L, 2);
491
492 rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
493
494 if (rv != UBUS_STATUS_OK)
495 {
496 lua_pop(L, 1);
497 lua_pushnil(L);
498 lua_pushinteger(L, rv);
499 return 2;
500 }
501
502 return 1;
503 }
504
505
506 static void
507 ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
508 {
509 lua_State *L = (lua_State *)req->priv;
510
511 if (!msg)
512 lua_pushnil(L);
513
514 ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
515 }
516
517 static int
518 ubus_lua_call(lua_State *L)
519 {
520 int rv, top;
521 uint32_t id;
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);
525
526 luaL_checktype(L, 4, LUA_TTABLE);
527 blob_buf_init(&c->buf, 0);
528
529 if (!ubus_lua_format_blob_array(L, &c->buf, true))
530 {
531 lua_pushnil(L);
532 lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
533 return 2;
534 }
535
536 rv = ubus_lookup_id(c->ctx, path, &id);
537
538 if (rv)
539 {
540 lua_pushnil(L);
541 lua_pushinteger(L, rv);
542 return 2;
543 }
544
545 top = lua_gettop(L);
546 rv = ubus_invoke(c->ctx, id, func, c->buf.head, ubus_lua_call_cb, L, c->timeout * 1000);
547
548 if (rv != UBUS_STATUS_OK)
549 {
550 lua_pop(L, 1);
551 lua_pushnil(L);
552 lua_pushinteger(L, rv);
553 return 2;
554 }
555
556 return lua_gettop(L) - top;
557 }
558
559
560 static int
561 ubus_lua__gc(lua_State *L)
562 {
563 struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
564
565 if (c->ctx != NULL)
566 {
567 ubus_free(c->ctx);
568 memset(c, 0, sizeof(*c));
569 }
570
571 return 0;
572 }
573
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 },
583 { NULL, NULL },
584 };
585
586 /* avoid missing prototype warning */
587 int luaopen_ubus(lua_State *L);
588
589 int
590 luaopen_ubus(lua_State *L)
591 {
592 /* create metatable */
593 luaL_newmetatable(L, METANAME);
594
595 /* metatable.__index = metatable */
596 lua_pushvalue(L, -1);
597 lua_setfield(L, -2, "__index");
598
599 /* fill metatable */
600 luaL_register(L, NULL, ubus);
601 lua_pop(L, 1);
602
603 /* create module */
604 luaL_register(L, MODNAME, ubus);
605
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");
623
624 /* used in our callbacks */
625 state = L;
626
627 /* create the callback table */
628 lua_createtable(L, 1, 0);
629 lua_setglobal(L, "__ubus_cb");
630
631 return 0;
632 }