ubus.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
  3. * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
  4. * Copyright (C) 2016 Iain Fraser <iainf@netduma.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License version 2.1
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  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. #include <lua.h>
  21. #define MODNAME "ubus"
  22. #define METANAME MODNAME ".meta"
  23. static lua_State *state;
  24. struct ubus_lua_connection {
  25. int timeout;
  26. struct blob_buf buf;
  27. struct ubus_context *ctx;
  28. };
  29. struct ubus_lua_object {
  30. struct ubus_object o;
  31. int r;
  32. int rsubscriber;
  33. };
  34. struct ubus_lua_event {
  35. struct ubus_event_handler e;
  36. int r;
  37. };
  38. struct ubus_lua_subscriber {
  39. struct ubus_subscriber s;
  40. int rnotify;
  41. int rremove;
  42. };
  43. static int
  44. ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table);
  45. static int
  46. ubus_lua_parse_blob_array(lua_State *L, struct blob_attr *attr, int len, bool table)
  47. {
  48. int rv;
  49. int idx = 1;
  50. int rem = len;
  51. struct blob_attr *pos;
  52. lua_newtable(L);
  53. __blob_for_each_attr(pos, attr, rem)
  54. {
  55. rv = ubus_lua_parse_blob(L, pos, table);
  56. if (rv > 1)
  57. lua_rawset(L, -3);
  58. else if (rv > 0)
  59. lua_rawseti(L, -2, idx++);
  60. }
  61. return 1;
  62. }
  63. static int
  64. ubus_lua_parse_blob(lua_State *L, struct blob_attr *attr, bool table)
  65. {
  66. int len;
  67. int off = 0;
  68. void *data;
  69. if (!blobmsg_check_attr(attr, false))
  70. return 0;
  71. if (table && blobmsg_name(attr)[0])
  72. {
  73. lua_pushstring(L, blobmsg_name(attr));
  74. off++;
  75. }
  76. data = blobmsg_data(attr);
  77. len = blobmsg_data_len(attr);
  78. switch (blob_id(attr))
  79. {
  80. case BLOBMSG_TYPE_BOOL:
  81. lua_pushboolean(L, *(uint8_t *)data);
  82. break;
  83. case BLOBMSG_TYPE_INT16:
  84. lua_pushinteger(L, be16_to_cpu(*(uint16_t *)data));
  85. break;
  86. case BLOBMSG_TYPE_INT32:
  87. lua_pushinteger(L, be32_to_cpu(*(uint32_t *)data));
  88. break;
  89. case BLOBMSG_TYPE_INT64:
  90. lua_pushnumber(L, (double) be64_to_cpu(*(uint64_t *)data));
  91. break;
  92. case BLOBMSG_TYPE_STRING:
  93. lua_pushstring(L, data);
  94. break;
  95. case BLOBMSG_TYPE_ARRAY:
  96. ubus_lua_parse_blob_array(L, data, len, false);
  97. break;
  98. case BLOBMSG_TYPE_TABLE:
  99. ubus_lua_parse_blob_array(L, data, len, true);
  100. break;
  101. default:
  102. lua_pushnil(L);
  103. break;
  104. }
  105. return off + 1;
  106. }
  107. static bool
  108. ubus_lua_format_blob_is_array(lua_State *L)
  109. {
  110. lua_Integer prv = 0;
  111. lua_Integer cur = 0;
  112. /* Find out whether table is array-like */
  113. for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
  114. {
  115. #ifdef LUA_TINT
  116. if (lua_type(L, -2) != LUA_TNUMBER && lua_type(L, -2) != LUA_TINT)
  117. #else
  118. if (lua_type(L, -2) != LUA_TNUMBER)
  119. #endif
  120. {
  121. lua_pop(L, 2);
  122. return false;
  123. }
  124. cur = lua_tointeger(L, -2);
  125. if ((cur - 1) != prv)
  126. {
  127. lua_pop(L, 2);
  128. return false;
  129. }
  130. prv = cur;
  131. }
  132. return true;
  133. }
  134. static int
  135. ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table);
  136. static int
  137. ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
  138. {
  139. void *c;
  140. bool rv = true;
  141. const char *key = table ? lua_tostring(L, -2) : NULL;
  142. switch (lua_type(L, -1))
  143. {
  144. case LUA_TBOOLEAN:
  145. blobmsg_add_u8(b, key, (uint8_t)lua_toboolean(L, -1));
  146. break;
  147. #ifdef LUA_TINT
  148. case LUA_TINT:
  149. #endif
  150. case LUA_TNUMBER:
  151. blobmsg_add_u32(b, key, (uint32_t)lua_tointeger(L, -1));
  152. break;
  153. case LUA_TSTRING:
  154. case LUA_TUSERDATA:
  155. case LUA_TLIGHTUSERDATA:
  156. blobmsg_add_string(b, key, lua_tostring(L, -1));
  157. break;
  158. case LUA_TTABLE:
  159. if (ubus_lua_format_blob_is_array(L))
  160. {
  161. c = blobmsg_open_array(b, key);
  162. rv = ubus_lua_format_blob_array(L, b, false);
  163. blobmsg_close_array(b, c);
  164. }
  165. else
  166. {
  167. c = blobmsg_open_table(b, key);
  168. rv = ubus_lua_format_blob_array(L, b, true);
  169. blobmsg_close_table(b, c);
  170. }
  171. break;
  172. default:
  173. rv = false;
  174. break;
  175. }
  176. return rv;
  177. }
  178. static int
  179. ubus_lua_format_blob_array(lua_State *L, struct blob_buf *b, bool table)
  180. {
  181. for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
  182. {
  183. if (!ubus_lua_format_blob(L, b, table))
  184. {
  185. lua_pop(L, 1);
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191. static int
  192. ubus_lua_connect(lua_State *L)
  193. {
  194. struct ubus_lua_connection *c;
  195. const char *sockpath = luaL_optstring(L, 1, NULL);
  196. int timeout = luaL_optint(L, 2, 30);
  197. if ((c = lua_newuserdata(L, sizeof(*c))) != NULL &&
  198. (c->ctx = ubus_connect(sockpath)) != NULL)
  199. {
  200. ubus_add_uloop(c->ctx);
  201. c->timeout = timeout;
  202. memset(&c->buf, 0, sizeof(c->buf));
  203. luaL_getmetatable(L, METANAME);
  204. lua_setmetatable(L, -2);
  205. return 1;
  206. }
  207. /* NB: no errors from ubus_connect() yet */
  208. lua_pushnil(L);
  209. lua_pushinteger(L, UBUS_STATUS_UNKNOWN_ERROR);
  210. return 2;
  211. }
  212. static void
  213. ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
  214. {
  215. lua_State *L = (lua_State *)p;
  216. lua_pushstring(L, o->path);
  217. lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
  218. }
  219. static int
  220. ubus_lua_objects(lua_State *L)
  221. {
  222. int rv;
  223. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  224. lua_newtable(L);
  225. rv = ubus_lookup(c->ctx, NULL, ubus_lua_objects_cb, L);
  226. if (rv != UBUS_STATUS_OK)
  227. {
  228. lua_pop(L, 1);
  229. lua_pushnil(L);
  230. lua_pushinteger(L, rv);
  231. return 2;
  232. }
  233. return 1;
  234. }
  235. static int
  236. ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
  237. struct ubus_request_data *req, const char *method,
  238. struct blob_attr *msg)
  239. {
  240. struct ubus_lua_object *o = container_of(obj, struct ubus_lua_object, o);
  241. int rv = 0;
  242. lua_getglobal(state, "__ubus_cb");
  243. lua_rawgeti(state, -1, o->r);
  244. lua_getfield(state, -1, method);
  245. lua_remove(state, -2);
  246. lua_remove(state, -2);
  247. if (lua_isfunction(state, -1)) {
  248. lua_pushlightuserdata(state, req);
  249. if (!msg)
  250. lua_pushnil(state);
  251. else
  252. ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
  253. lua_call(state, 2, 1);
  254. if (lua_isnumber(state, -1))
  255. rv = lua_tonumber(state, -1);
  256. }
  257. lua_pop(state, 1);
  258. return rv;
  259. }
  260. static int lua_gettablelen(lua_State *L, int index)
  261. {
  262. int cnt = 0;
  263. lua_pushnil(L);
  264. index -= 1;
  265. while (lua_next(L, index) != 0) {
  266. cnt++;
  267. lua_pop(L, 1);
  268. }
  269. return cnt;
  270. }
  271. static int ubus_lua_reply(lua_State *L)
  272. {
  273. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  274. struct ubus_request_data *req;
  275. luaL_checktype(L, 3, LUA_TTABLE);
  276. blob_buf_init(&c->buf, 0);
  277. if (!ubus_lua_format_blob_array(L, &c->buf, true))
  278. {
  279. lua_pushnil(L);
  280. lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
  281. return 2;
  282. }
  283. req = lua_touserdata(L, 2);
  284. ubus_send_reply(c->ctx, req, c->buf.head);
  285. return 0;
  286. }
  287. static int ubus_lua_defer_request(lua_State *L)
  288. {
  289. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  290. struct ubus_request_data *req = lua_touserdata(L, 2);
  291. struct ubus_request_data *new_req = lua_newuserdata(L, sizeof(struct ubus_request_data));
  292. ubus_defer_request(c->ctx, req, new_req);
  293. return 1;
  294. }
  295. static int ubus_lua_complete_deferred_request(lua_State *L)
  296. {
  297. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  298. struct ubus_request_data *req = lua_touserdata(L, 2);
  299. int ret = luaL_checkinteger(L, 3);
  300. ubus_complete_deferred_request(c->ctx, req, ret);
  301. return 0;
  302. }
  303. static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
  304. {
  305. struct blobmsg_policy *p;
  306. int plen;
  307. int pidx = 0;
  308. /* get the function pointer */
  309. lua_pushinteger(L, 1);
  310. lua_gettable(L, -2);
  311. /* get the policy table */
  312. lua_pushinteger(L, 2);
  313. lua_gettable(L, -3);
  314. /* check if the method table is valid */
  315. if ((lua_type(L, -2) != LUA_TFUNCTION) ||
  316. (lua_type(L, -1) != LUA_TTABLE) ||
  317. lua_objlen(L, -1)) {
  318. lua_pop(L, 2);
  319. return 1;
  320. }
  321. /* store function pointer */
  322. lua_pushvalue(L, -2);
  323. lua_setfield(L, -6, lua_tostring(L, -5));
  324. m->name = lua_tostring(L, -4);
  325. m->handler = ubus_method_handler;
  326. plen = lua_gettablelen(L, -1);
  327. /* exit if policy table is empty */
  328. if (!plen) {
  329. lua_pop(L, 2);
  330. return 0;
  331. }
  332. /* setup the policy pointers */
  333. p = calloc(plen, sizeof(struct blobmsg_policy));
  334. if (!p)
  335. return 1;
  336. m->policy = p;
  337. lua_pushnil(L);
  338. while (lua_next(L, -2) != 0) {
  339. int val = lua_tointeger(L, -1);
  340. /* check if the policy is valid */
  341. if ((lua_type(L, -2) != LUA_TSTRING) ||
  342. (lua_type(L, -1) != LUA_TNUMBER) ||
  343. (val < 0) ||
  344. (val > BLOBMSG_TYPE_LAST)) {
  345. lua_pop(L, 1);
  346. continue;
  347. }
  348. p[pidx].name = lua_tostring(L, -2);
  349. p[pidx].type = val;
  350. lua_pop(L, 1);
  351. pidx++;
  352. }
  353. m->n_policy = pidx;
  354. lua_pop(L, 2);
  355. return 0;
  356. }
  357. static void
  358. ubus_new_sub_cb(struct ubus_context *ctx, struct ubus_object *obj)
  359. {
  360. struct ubus_lua_object *luobj;
  361. luobj = container_of(obj, struct ubus_lua_object, o);
  362. lua_getglobal(state, "__ubus_cb_publisher");
  363. lua_rawgeti(state, -1, luobj->rsubscriber);
  364. lua_remove(state, -2);
  365. if (lua_isfunction(state, -1)) {
  366. lua_pushnumber(state, luobj->o.has_subscribers );
  367. lua_call(state, 1, 0);
  368. } else {
  369. lua_pop(state, 1);
  370. }
  371. }
  372. static void
  373. ubus_lua_load_newsub_cb( lua_State *L, struct ubus_lua_object *obj )
  374. {
  375. /* keep ref to func */
  376. lua_getglobal(L, "__ubus_cb_publisher");
  377. lua_pushvalue(L, -2);
  378. obj->rsubscriber = luaL_ref(L, -2);
  379. lua_pop(L, 1);
  380. /* real callback */
  381. obj->o.subscribe_cb = ubus_new_sub_cb;
  382. return;
  383. }
  384. static struct ubus_object* ubus_lua_load_object(lua_State *L)
  385. {
  386. struct ubus_lua_object *obj = NULL;
  387. int mlen = lua_gettablelen(L, -1);
  388. struct ubus_method *m;
  389. int midx = 0;
  390. /* setup object pointers */
  391. obj = calloc(1, sizeof(struct ubus_lua_object));
  392. if (!obj)
  393. return NULL;
  394. obj->o.name = lua_tostring(L, -2);
  395. /* setup method pointers */
  396. m = calloc(mlen, sizeof(struct ubus_method));
  397. obj->o.methods = m;
  398. /* setup type pointers */
  399. obj->o.type = calloc(1, sizeof(struct ubus_object_type));
  400. if (!obj->o.type) {
  401. free(obj);
  402. return NULL;
  403. }
  404. obj->o.type->name = lua_tostring(L, -2);
  405. obj->o.type->id = 0;
  406. obj->o.type->methods = obj->o.methods;
  407. /* create the callback lookup table */
  408. lua_createtable(L, 1, 0);
  409. lua_getglobal(L, "__ubus_cb");
  410. lua_pushvalue(L, -2);
  411. obj->r = luaL_ref(L, -2);
  412. lua_pop(L, 1);
  413. /* scan each method */
  414. lua_pushnil(L);
  415. while (lua_next(L, -3) != 0) {
  416. /* check if its the subscriber notification callback */
  417. if( lua_type( L, -2 ) == LUA_TSTRING &&
  418. lua_type( L, -1 ) == LUA_TFUNCTION ){
  419. if( !strcmp( lua_tostring( L, -2 ), "__subscriber_cb" ) )
  420. ubus_lua_load_newsub_cb( L, obj );
  421. }
  422. /* check if it looks like a method */
  423. if ((lua_type(L, -2) != LUA_TSTRING) ||
  424. (lua_type(L, -1) != LUA_TTABLE) ||
  425. !lua_objlen(L, -1)) {
  426. lua_pop(L, 1);
  427. continue;
  428. }
  429. if (!ubus_lua_load_methods(L, &m[midx]))
  430. midx++;
  431. lua_pop(L, 1);
  432. }
  433. obj->o.type->n_methods = obj->o.n_methods = midx;
  434. /* pop the callback table */
  435. lua_pop(L, 1);
  436. return &obj->o;
  437. }
  438. static int ubus_lua_add(lua_State *L)
  439. {
  440. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  441. /* verify top level object */
  442. if (lua_istable(L, 1)) {
  443. lua_pushstring(L, "you need to pass a table");
  444. lua_error(L);
  445. return 0;
  446. }
  447. /* scan each object */
  448. lua_pushnil(L);
  449. while (lua_next(L, -2) != 0) {
  450. struct ubus_object *obj = NULL;
  451. /* check if the object has a table of methods */
  452. if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TTABLE)) {
  453. obj = ubus_lua_load_object(L);
  454. if (obj){
  455. ubus_add_object(c->ctx, obj);
  456. /* allow future reference of ubus obj */
  457. lua_pushstring(state,"__ubusobj");
  458. lua_pushlightuserdata(state, obj);
  459. lua_settable(state,-3);
  460. }
  461. }
  462. lua_pop(L, 1);
  463. }
  464. return 0;
  465. }
  466. static int
  467. ubus_lua_notify( lua_State *L )
  468. {
  469. struct ubus_lua_connection *c;
  470. struct ubus_object *obj;
  471. const char* method;
  472. c = luaL_checkudata(L, 1, METANAME);
  473. method = luaL_checkstring(L, 3);
  474. luaL_checktype(L, 4, LUA_TTABLE);
  475. if( !lua_islightuserdata( L, 2 ) ){
  476. lua_pushfstring( L, "Invald 2nd parameter, expected ubus obj ref" );
  477. lua_error( L );
  478. }
  479. obj = lua_touserdata( L, 2 );
  480. /* create parameters from table */
  481. blob_buf_init(&c->buf, 0);
  482. if( !ubus_lua_format_blob_array( L, &c->buf, true ) ){
  483. lua_pushfstring( L, "Invalid 4th parameter, expected table of arguments" );
  484. lua_error( L );
  485. }
  486. ubus_notify( c->ctx, obj, method, c->buf.head, -1 );
  487. return 0;
  488. }
  489. static void
  490. ubus_lua_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
  491. {
  492. lua_State *L = (lua_State *)p;
  493. if (!o->signature)
  494. return;
  495. ubus_lua_parse_blob_array(L, blob_data(o->signature), blob_len(o->signature), true);
  496. }
  497. static int
  498. ubus_lua_signatures(lua_State *L)
  499. {
  500. int rv;
  501. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  502. const char *path = luaL_checkstring(L, 2);
  503. rv = ubus_lookup(c->ctx, path, ubus_lua_signatures_cb, L);
  504. if (rv != UBUS_STATUS_OK)
  505. {
  506. lua_pop(L, 1);
  507. lua_pushnil(L);
  508. lua_pushinteger(L, rv);
  509. return 2;
  510. }
  511. return 1;
  512. }
  513. static void
  514. ubus_lua_call_cb(struct ubus_request *req, int type, struct blob_attr *msg)
  515. {
  516. lua_State *L = (lua_State *)req->priv;
  517. if (!msg && L)
  518. lua_pushnil(L);
  519. if (msg && L)
  520. ubus_lua_parse_blob_array(L, blob_data(msg), blob_len(msg), true);
  521. }
  522. static int
  523. ubus_lua_call(lua_State *L)
  524. {
  525. int rv, top;
  526. uint32_t id;
  527. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  528. const char *path = luaL_checkstring(L, 2);
  529. const char *func = luaL_checkstring(L, 3);
  530. luaL_checktype(L, 4, LUA_TTABLE);
  531. blob_buf_init(&c->buf, 0);
  532. if (!ubus_lua_format_blob_array(L, &c->buf, true))
  533. {
  534. lua_pushnil(L);
  535. lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
  536. return 2;
  537. }
  538. rv = ubus_lookup_id(c->ctx, path, &id);
  539. if (rv)
  540. {
  541. lua_pushnil(L);
  542. lua_pushinteger(L, rv);
  543. return 2;
  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. if (rv != UBUS_STATUS_OK)
  548. {
  549. lua_pop(L, 1);
  550. lua_pushnil(L);
  551. lua_pushinteger(L, rv);
  552. return 2;
  553. }
  554. return lua_gettop(L) - top;
  555. }
  556. static void
  557. ubus_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
  558. const char *type, struct blob_attr *msg)
  559. {
  560. struct ubus_lua_event *listener = container_of(ev, struct ubus_lua_event, e);
  561. lua_getglobal(state, "__ubus_cb_event");
  562. lua_rawgeti(state, -1, listener->r);
  563. lua_remove(state, -2);
  564. if (lua_isfunction(state, -1)) {
  565. ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
  566. lua_call(state, 1, 0);
  567. } else {
  568. lua_pop(state, 1);
  569. }
  570. }
  571. static struct ubus_event_handler*
  572. ubus_lua_load_event(lua_State *L)
  573. {
  574. struct ubus_lua_event* event = NULL;
  575. event = calloc(1, sizeof(struct ubus_lua_event));
  576. if (!event)
  577. return NULL;
  578. event->e.cb = ubus_event_handler;
  579. /* update the he callback lookup table */
  580. lua_getglobal(L, "__ubus_cb_event");
  581. lua_pushvalue(L, -2);
  582. event->r = luaL_ref(L, -2);
  583. lua_setfield(L, -1, lua_tostring(L, -3));
  584. return &event->e;
  585. }
  586. static int
  587. ubus_lua_listen(lua_State *L) {
  588. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  589. /* verify top level object */
  590. luaL_checktype(L, 2, LUA_TTABLE);
  591. /* scan each object */
  592. lua_pushnil(L);
  593. while (lua_next(L, -2) != 0) {
  594. struct ubus_event_handler *listener;
  595. /* check if the key is a string and the value is a method */
  596. if ((lua_type(L, -2) == LUA_TSTRING) && (lua_type(L, -1) == LUA_TFUNCTION)) {
  597. listener = ubus_lua_load_event(L);
  598. if(listener != NULL) {
  599. ubus_register_event_handler(c->ctx, listener, lua_tostring(L, -2));
  600. }
  601. }
  602. lua_pop(L, 1);
  603. }
  604. return 0;
  605. }
  606. static void
  607. ubus_sub_remove_handler(struct ubus_context *ctx, struct ubus_subscriber *s,
  608. uint32_t id)
  609. {
  610. struct ubus_lua_subscriber *sub;
  611. sub = container_of(s, struct ubus_lua_subscriber, s);
  612. lua_getglobal(state, "__ubus_cb_subscribe");
  613. lua_rawgeti(state, -1, sub->rremove);
  614. lua_remove(state, -2);
  615. if (lua_isfunction(state, -1)) {
  616. lua_call(state, 0, 0);
  617. } else {
  618. lua_pop(state, 1);
  619. }
  620. }
  621. static int
  622. ubus_sub_notify_handler(struct ubus_context *ctx, struct ubus_object *obj,
  623. struct ubus_request_data *req, const char *method,
  624. struct blob_attr *msg)
  625. {
  626. struct ubus_subscriber *s;
  627. struct ubus_lua_subscriber *sub;
  628. s = container_of(obj, struct ubus_subscriber, obj);
  629. sub = container_of(s, struct ubus_lua_subscriber, s);
  630. lua_getglobal(state, "__ubus_cb_subscribe");
  631. lua_rawgeti(state, -1, sub->rnotify);
  632. lua_remove(state, -2);
  633. if (lua_isfunction(state, -1)) {
  634. if( msg ){
  635. ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
  636. lua_call(state, 1, 0);
  637. } else {
  638. lua_call(state, 0, 0);
  639. }
  640. } else {
  641. lua_pop(state, 1);
  642. }
  643. return 0;
  644. }
  645. static void
  646. ubus_lua_do_subscribe( struct ubus_context *ctx, lua_State *L, const char* target,
  647. int idxnotify, int idxremove )
  648. {
  649. uint32_t id;
  650. int status;
  651. struct ubus_lua_subscriber *sub;
  652. if( ( status = ubus_lookup_id( ctx, target, &id ) ) ){
  653. lua_pushfstring( L, "Unable find target, status=%d", status );
  654. lua_error( L );
  655. }
  656. sub = calloc( 1, sizeof( struct ubus_lua_subscriber ) );
  657. if( !sub ){
  658. lua_pushstring( L, "Out of memory" );
  659. lua_error( L );
  660. }
  661. if( idxnotify ){
  662. lua_getglobal(L, "__ubus_cb_subscribe");
  663. lua_pushvalue(L, idxnotify);
  664. sub->rnotify = luaL_ref(L, -2);
  665. lua_pop(L, 1);
  666. sub->s.cb = ubus_sub_notify_handler;
  667. }
  668. if( idxremove ){
  669. lua_getglobal(L, "__ubus_cb_subscribe");
  670. lua_pushvalue(L, idxnotify);
  671. sub->rnotify = luaL_ref(L, -2);
  672. lua_pop(L, 1);
  673. sub->s.remove_cb = ubus_sub_remove_handler;
  674. }
  675. if( ( status = ubus_register_subscriber( ctx, &sub->s ) ) ){
  676. lua_pushfstring( L, "Failed to register subscriber, status=%d", status );
  677. lua_error( L );
  678. }
  679. if( ( status = ubus_subscribe( ctx, &sub->s, id) ) ){
  680. lua_pushfstring( L, "Failed to register subscriber, status=%d", status );
  681. lua_error( L );
  682. }
  683. }
  684. static int
  685. ubus_lua_subscribe(lua_State *L) {
  686. int idxnotify, idxremove, stackstart;
  687. struct ubus_lua_connection *c;
  688. const char* target;
  689. idxnotify = idxremove = 0;
  690. stackstart = lua_gettop( L );
  691. c = luaL_checkudata(L, 1, METANAME);
  692. target = luaL_checkstring(L, 2);
  693. luaL_checktype(L, 3, LUA_TTABLE);
  694. lua_pushstring( L, "notify");
  695. lua_gettable( L, 3 );
  696. if( lua_type( L, -1 ) == LUA_TFUNCTION ){
  697. idxnotify = lua_gettop( L );
  698. } else {
  699. lua_pop( L, 1 );
  700. }
  701. lua_pushstring( L, "remove");
  702. lua_gettable( L, 3 );
  703. if( lua_type( L, -1 ) == LUA_TFUNCTION ){
  704. idxremove = lua_gettop( L );
  705. } else {
  706. lua_pop( L, 1 );
  707. }
  708. if( idxnotify )
  709. ubus_lua_do_subscribe( c->ctx, L, target, idxnotify, idxremove );
  710. if( lua_gettop( L ) > stackstart )
  711. lua_pop( L, lua_gettop( L ) - stackstart );
  712. return 0;
  713. }
  714. static int
  715. ubus_lua_send(lua_State *L)
  716. {
  717. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  718. const char *event = luaL_checkstring(L, 2);
  719. if (*event == 0)
  720. return luaL_argerror(L, 2, "no event name");
  721. // Event content convert to ubus form
  722. luaL_checktype(L, 3, LUA_TTABLE);
  723. blob_buf_init(&c->buf, 0);
  724. if (!ubus_lua_format_blob_array(L, &c->buf, true)) {
  725. lua_pushnil(L);
  726. lua_pushinteger(L, UBUS_STATUS_INVALID_ARGUMENT);
  727. return 2;
  728. }
  729. // Send the event
  730. ubus_send_event(c->ctx, event, c->buf.head);
  731. return 0;
  732. }
  733. static int
  734. ubus_lua__gc(lua_State *L)
  735. {
  736. struct ubus_lua_connection *c = luaL_checkudata(L, 1, METANAME);
  737. blob_buf_free(&c->buf);
  738. if (c->ctx != NULL)
  739. {
  740. ubus_free(c->ctx);
  741. memset(c, 0, sizeof(*c));
  742. }
  743. return 0;
  744. }
  745. static const luaL_Reg ubus[] = {
  746. { "connect", ubus_lua_connect },
  747. { "objects", ubus_lua_objects },
  748. { "add", ubus_lua_add },
  749. { "notify", ubus_lua_notify },
  750. { "reply", ubus_lua_reply },
  751. { "defer_request", ubus_lua_defer_request },
  752. { "complete_deferred_request", ubus_lua_complete_deferred_request },
  753. { "signatures", ubus_lua_signatures },
  754. { "call", ubus_lua_call },
  755. { "close", ubus_lua__gc },
  756. { "listen", ubus_lua_listen },
  757. { "send", ubus_lua_send },
  758. { "subscribe", ubus_lua_subscribe },
  759. { "__gc", ubus_lua__gc },
  760. { NULL, NULL },
  761. };
  762. /* avoid missing prototype warning */
  763. int luaopen_ubus(lua_State *L);
  764. int
  765. luaopen_ubus(lua_State *L)
  766. {
  767. /* create metatable */
  768. luaL_newmetatable(L, METANAME);
  769. /* metatable.__index = metatable */
  770. lua_pushvalue(L, -1);
  771. lua_setfield(L, -2, "__index");
  772. /* fill metatable */
  773. luaL_register(L, NULL, ubus);
  774. lua_pop(L, 1);
  775. /* create module */
  776. luaL_register(L, MODNAME, ubus);
  777. /* set some enum defines */
  778. lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
  779. lua_setfield(L, -2, "ARRAY");
  780. lua_pushinteger(L, BLOBMSG_TYPE_TABLE);
  781. lua_setfield(L, -2, "TABLE");
  782. lua_pushinteger(L, BLOBMSG_TYPE_STRING);
  783. lua_setfield(L, -2, "STRING");
  784. lua_pushinteger(L, BLOBMSG_TYPE_INT64);
  785. lua_setfield(L, -2, "INT64");
  786. lua_pushinteger(L, BLOBMSG_TYPE_INT32);
  787. lua_setfield(L, -2, "INT32");
  788. lua_pushinteger(L, BLOBMSG_TYPE_INT16);
  789. lua_setfield(L, -2, "INT16");
  790. lua_pushinteger(L, BLOBMSG_TYPE_INT8);
  791. lua_setfield(L, -2, "INT8");
  792. lua_pushinteger(L, BLOBMSG_TYPE_BOOL);
  793. lua_setfield(L, -2, "BOOLEAN");
  794. /* used in our callbacks */
  795. state = L;
  796. /* create the callback table */
  797. lua_createtable(L, 1, 0);
  798. lua_setglobal(L, "__ubus_cb");
  799. /* create the event table */
  800. lua_createtable(L, 1, 0);
  801. lua_setglobal(L, "__ubus_cb_event");
  802. /* create the subscriber table */
  803. lua_createtable(L, 1, 0);
  804. lua_setglobal(L, "__ubus_cb_subscribe");
  805. /* create the publisher table - notifications of new subs */
  806. lua_createtable(L, 1, 0);
  807. lua_setglobal(L, "__ubus_cb_publisher");
  808. return 0;
  809. }