ubus.c 22 KB

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