luat_lib_ctiot.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. @module ctiot
  3. @summary 中国电信CTIOT集成
  4. @version 1.0
  5. @date 2020.08.30
  6. @demo ctiot
  7. */
  8. #include "luat_base.h"
  9. #include "luat_timer.h"
  10. #include "luat_malloc.h"
  11. #include "luat_msgbus.h"
  12. #include "luat_ctiot.h"
  13. #define LUAT_LOG_TAG "ctiot"
  14. #include "luat_log.h"
  15. //---------------------------
  16. #ifdef AIR302
  17. #define DBG(X,Y...) LLOGD("%s %d:"X, __FUNCTION__, __LINE__, ##Y)
  18. const char luat_pub_tx_tag[] = "CTIOT_TX";
  19. const char luat_pub_rx_tag[] = "CTIOT_RX";
  20. const char luat_pub_reg_tag[] = "CTIOT_REG";
  21. const char luat_pub_update_tag[] = "CTIOT_UPDATE";
  22. const char luat_pub_dereg_tag[] = "CTIOT_DEREG";
  23. const char luat_pub_wakeup_tag[] = "CTIOT_WAKEUP";
  24. const char luat_pub_other_tag[] = "CTIOT_OTHER";
  25. const char luat_pub_fota_tag[] = "CTIOT_FOTA";
  26. static int luat_ctiot_msg_handler(lua_State *L, void* ptr)
  27. {
  28. uint8_t type;
  29. uint8_t code;
  30. uint32_t len;
  31. uint8_t *buff = (uint8_t *)ptr;
  32. char *tag;
  33. LUA_INTEGER error,error_code,param;
  34. type = buff[0];
  35. code = buff[1];
  36. memcpy(&len, buff+2, 4);
  37. switch(type)
  38. {
  39. case CTIOT_EVENT_TX:
  40. tag = (char *)luat_pub_tx_tag;
  41. param = 0;
  42. switch (code)
  43. {
  44. case CTIOT_TX_ACK:
  45. case CTIOT_TX_DONE:
  46. goto LUAT_CTIOT_MSG_HANDLER_DONE;
  47. //break;
  48. default:
  49. error = 1;
  50. error_code = code;
  51. break;
  52. }
  53. break;
  54. case CTIOT_EVENT_UPDATE:
  55. tag = (char *)luat_pub_update_tag;
  56. param = 0;
  57. switch (code)
  58. {
  59. case CTIOT_UPDATE_OK:
  60. error = 0;
  61. error_code = 0;
  62. param = 0;
  63. break;
  64. default:
  65. error = 1;
  66. error_code = code;
  67. param = 0;
  68. break;
  69. }
  70. break;
  71. case CTIOT_EVENT_AIR:
  72. tag = (char *)luat_pub_tx_tag;
  73. param = 0;
  74. switch (code)
  75. {
  76. case CTIOT_AIR_NON_SEND_START:
  77. goto LUAT_CTIOT_MSG_HANDLER_DONE;
  78. //break;
  79. default:
  80. error = 0;
  81. error_code = 0;
  82. param = buff[6];
  83. break;
  84. }
  85. break;
  86. case CTIOT_EVENT_RX:
  87. tag = (char *)luat_pub_rx_tag;
  88. break;
  89. case CTIOT_EVENT_STATE:
  90. switch (code)
  91. {
  92. case CTIOT_STATE_TAU_WAKEUP:
  93. case CTIOT_STATE_TAU_NOTIFY:
  94. tag = (char *)luat_pub_wakeup_tag;
  95. error = 0;
  96. error_code = 0;
  97. param = code;
  98. break;
  99. default:
  100. tag = (char *)luat_pub_other_tag;
  101. error = 0;
  102. error_code = type;
  103. param = code;
  104. break;
  105. }
  106. break;
  107. case CTIOT_EVENT_REG:
  108. tag = (char *)luat_pub_reg_tag;
  109. switch (code)
  110. {
  111. case CTIOT_REG_OK:
  112. error = 0;
  113. error_code = 0;
  114. param = 0;
  115. break;
  116. default:
  117. error = 1;
  118. error_code = code;
  119. param = 0;
  120. break;
  121. }
  122. break;
  123. case CTIOT_EVENT_OB19:
  124. tag = (char *)luat_pub_reg_tag;
  125. switch (code)
  126. {
  127. case CTIOT_OB19_ON:
  128. error = 0;
  129. error_code = 0;
  130. param = 1;
  131. break;
  132. default:
  133. error = 1;
  134. error_code = code;
  135. param = 0;
  136. break;
  137. }
  138. break;
  139. case CTIOT_EVENT_DEREG:
  140. tag = (char *)luat_pub_dereg_tag;
  141. error = 0;
  142. error_code = 0;
  143. param = 0;
  144. break;
  145. case CTIOT_EVENT_FOTA:
  146. tag = (char *)luat_pub_fota_tag;
  147. error = 0;
  148. error_code = 0;
  149. param = code;
  150. break;
  151. default:
  152. tag = (char *)luat_pub_other_tag;
  153. error = 0;
  154. error_code = type;
  155. param = code;
  156. break;
  157. }
  158. lua_getglobal(L, "sys_pub");
  159. lua_pushstring(L, tag);
  160. /*
  161. @sys_pub ctiot
  162. CTIOT 接收回调消息
  163. CTIOT_RX
  164. @string data, CTIOT 接收数据
  165. @usage
  166. sys.subscribe("CTIOT_RX", function(data)
  167. log.info("CTIOT_RX", data:toHex())
  168. end)
  169. */
  170. if (CTIOT_EVENT_RX == type)
  171. {
  172. lua_pushlstring(L, buff + 6, len);
  173. lua_call(L, 2, 0);
  174. }
  175. else
  176. {
  177. /*
  178. @sys_pub ctiot
  179. CTIOT 发送回调消息
  180. CTIOT_TX
  181. @bool error, 是否成功
  182. @number error_code, 错误代码
  183. @number param, 数据
  184. @usage
  185. sys.subscribe("CTIOT_TX", function (error, error_code, param)
  186. log.info("CTIOT_TX", error, error_code, param)
  187. end)
  188. */
  189. /*
  190. @sys_pub ctiot
  191. CTIOT REG回调消息
  192. CTIOT_REG
  193. @bool error, 是否成功
  194. @number error_code, 错误代码
  195. @number param, 数据
  196. @usage
  197. sys.subscribe("CTIOT_REG", function (error, error_code, param)
  198. log.info("CTIOT_REG", error, error_code, param)
  199. end)
  200. */
  201. /*
  202. @sys_pub ctiot
  203. CTIOT DEREG回调消息
  204. CTIOT_DEREG
  205. @bool error, 是否成功
  206. @number error_code, 错误代码
  207. @number param, 数据
  208. @usage
  209. sys.subscribe("CTIOT_DEREG", function (error, error_code, param)
  210. log.info("CTIOT_DEREG", error, error_code, param)
  211. end)
  212. */
  213. /*
  214. @sys_pub ctiot
  215. CTIOT 唤醒回调消息
  216. CTIOT_WAKEUP
  217. @bool error, 是否成功
  218. @number error_code, 错误代码
  219. @number param, 数据
  220. @usage
  221. sys.subscribe("CTIOT_WAKEUP", function (error, error_code, param)
  222. log.info("CTIOT_WAKEUP", error, error_code, param)
  223. end)
  224. */
  225. /*
  226. @sys_pub ctiot
  227. CTIOT 其他回调消息
  228. CTIOT_OTHER
  229. @bool error, 是否成功
  230. @number error_code, 错误代码
  231. @number param, 数据
  232. @usage
  233. sys.subscribe("CTIOT_OTHER", function (error, error_code, param)
  234. log.info("CTIOT_OTHER", error, error_code, param)
  235. end)
  236. */
  237. /*
  238. @sys_pub ctiot
  239. CTIOT FOTA回调消息
  240. CTIOT_FOTA
  241. @bool error, 是否成功
  242. @number error_code, 错误代码
  243. @number param, 数据
  244. @usage
  245. sys.subscribe("CTIOT_FOTA", function (error, error_code, param)
  246. log.info("CTIOT_FOTA", error, error_code, param)
  247. end)
  248. */
  249. lua_pushboolean(L, error);
  250. lua_pushinteger(L, error_code);
  251. lua_pushinteger(L, param);
  252. lua_call(L, 4, 0);
  253. }
  254. LUAT_CTIOT_MSG_HANDLER_DONE:
  255. luat_heap_free(ptr);
  256. return 0;
  257. }
  258. void luat_ctiot_callback(uint8_t type, uint8_t code, void *buf, uint32_t len)
  259. {
  260. rtos_msg_t msg;
  261. uint8_t *buff;
  262. DBG("%d,%d,0x%08x,%u",type, code, buf, len);
  263. if (type >= CTIOT_EVENT_DBG)
  264. {
  265. return;
  266. }
  267. msg.handler = luat_ctiot_msg_handler;
  268. if (buf)
  269. {
  270. msg.ptr = luat_heap_malloc(6 + len);
  271. }
  272. else
  273. {
  274. msg.ptr = luat_heap_malloc(6);
  275. }
  276. buff = msg.ptr;
  277. if (buff)
  278. {
  279. buff[0] = type;
  280. buff[1] = code;
  281. memcpy(buff + 2, &len, 4);
  282. if (buf)
  283. {
  284. memcpy(buff + 6, buf, len);
  285. }
  286. }
  287. luat_msgbus_put(&msg, 0);
  288. }
  289. /**
  290. 初始化ctiot,在复位开机后使用一次
  291. @api ctiot.init()
  292. @return nil 无返回值
  293. */
  294. static int l_ctiot_init(lua_State *L)
  295. {
  296. luat_ctiot_init();
  297. return 0;
  298. }
  299. /**
  300. 设置和读取ctiot相关参数,有参数输入则设置,无论是否有参数输入,均输出当前参数
  301. @api ctiot.param(ip, port, lifetime)
  302. @string 服务器ip
  303. @int 服务器端口
  304. @int 生命周期,单位秒
  305. @return string 服务器ip
  306. @return int 服务器端口
  307. @return int 生命周期,单位秒
  308. */
  309. static int l_ctiot_param(lua_State *L)
  310. {
  311. char server_ip[20];
  312. const char *new_ip;
  313. uint16_t port;
  314. uint16_t result;
  315. uint32_t lifetime;
  316. size_t len;
  317. uint8_t new_set = 0;
  318. result = luat_ctiot_get_para(server_ip, &port, &lifetime, NULL);
  319. new_ip = lua_tolstring(L, 1, &len);
  320. if (len < 20 && len)
  321. {
  322. memset(server_ip, 0, 20);
  323. memcpy(server_ip, new_ip, len);
  324. new_set = 1;
  325. }
  326. if (lua_isinteger(L, 2))
  327. {
  328. port = lua_tointeger(L, 2);
  329. new_set = 1;
  330. }
  331. if (lua_isinteger(L, 3))
  332. {
  333. lifetime = lua_tointeger(L, 3);
  334. new_set = 1;
  335. }
  336. if (new_set)
  337. {
  338. result = luat_ctiot_set_para(server_ip, port, lifetime, NULL);
  339. }
  340. lua_pushstring(L, server_ip);
  341. lua_pushinteger(L, port);
  342. lua_pushinteger(L, lifetime);
  343. return 3;
  344. }
  345. /**
  346. 设置和读取自定义EP
  347. @api ctiot.ep(val)
  348. @string 自定义EP的值,默认是imei,读取的话不要填这个参数
  349. @return string 当前EP值
  350. */
  351. static int l_ctiot_ep(lua_State *L)
  352. {
  353. char userEp[40];
  354. const char *new_ep;
  355. size_t len;
  356. uint8_t new_set = 0;
  357. luat_ctiot_get_ep(userEp);
  358. new_ep = lua_tolstring(L, 1, &len);
  359. if (len < 40 && len)
  360. {
  361. memset(userEp, 0, 40);
  362. memcpy(userEp, new_ep, len);
  363. new_set = 1;
  364. }
  365. if (new_set)
  366. {
  367. luat_ctiot_set_ep(userEp);
  368. luat_ctiot_get_ep(userEp);
  369. }
  370. lua_pushstring(L, userEp);
  371. return 1;
  372. }
  373. // /**
  374. //设置和读取ctiot相关模式,有模式输入则设置,无论是否有模式输入,均输出当前模式
  375. //@api ctiot.mode()
  376. //@return nil 当前无返回值
  377. // */
  378. static int l_ctiot_mode(lua_State *L)
  379. {
  380. return 0;
  381. }
  382. /**
  383. 连接CTIOT,必须在设置完参数和模式后再使用
  384. @api ctiot.connect()
  385. @return boolean 成功返回true,否则返回false
  386. */
  387. static int l_ctiot_connect(lua_State *L)
  388. {
  389. if (luat_ctiot_reg())
  390. {
  391. lua_pushboolean(L, 0);
  392. }
  393. else
  394. {
  395. lua_pushboolean(L, 1);
  396. }
  397. return 1;
  398. }
  399. /**
  400. 断开ctiot
  401. @api ctiot.disconnect()
  402. @return nil 无返回值
  403. */
  404. static int l_ctiot_disconnect(lua_State *L)
  405. {
  406. luat_ctiot_dereg();
  407. return 0;
  408. }
  409. /**
  410. 发送数据给ctiot
  411. @api ctiot.write(data, mode, seq)
  412. @string 需要发送的数据
  413. @int 模式, ctiot.CON/NON/NON_REL/CON_REL
  414. @int 序号
  415. @return boolean 成功返回true,否则返回false
  416. @return string 成功为nil,失败返回错误描述
  417. */
  418. static int l_ctiot_write(lua_State *L)
  419. {
  420. const char *data;
  421. uint8_t mode = 0;
  422. uint8_t seq = 0;
  423. size_t len = 0;
  424. data = lua_tolstring(L, 1, &len);
  425. if (!len)
  426. {
  427. lua_pushboolean(L, 0);
  428. lua_pushstring(L, "no data");
  429. return 2;
  430. }
  431. if (lua_isinteger(L, 2))
  432. {
  433. mode = lua_tointeger(L, 2);
  434. }
  435. if (lua_isinteger(L, 3))
  436. {
  437. seq = lua_tointeger(L, 3);
  438. }
  439. if (mode >= 4)
  440. {
  441. lua_pushboolean(L, 0);
  442. lua_pushstring(L, "mode error");
  443. return 2;
  444. }
  445. uint16_t result = luat_ctiot_send(data, len, mode, seq);
  446. if (result)
  447. {
  448. lua_pushboolean(L, 0);
  449. lua_pushfstring(L, "error code %d", result);
  450. }
  451. else
  452. {
  453. lua_pushboolean(L, 1);
  454. lua_pushnil(L);
  455. }
  456. return 2;
  457. }
  458. // /**
  459. //读取已经接收到的数据
  460. //@api ctiot.read()
  461. //@return nil 暂无返回值
  462. // */
  463. static int l_ctiot_read(lua_State *L)
  464. {
  465. return 0;
  466. }
  467. /**
  468. 是否已经就绪
  469. @api ctiot.ready()
  470. @return int 已经就绪返回0,否则返回错误代码
  471. */
  472. static int l_ctiot_ready(lua_State *L)
  473. {
  474. uint16_t result = luat_ctiot_check_ready();
  475. lua_pushinteger(L, result);
  476. return 1;
  477. }
  478. /**
  479. 发送更新注册信息给ctiot
  480. @api ctio.update()
  481. @return boolean 发送成功等待结果返回true,否则返回false
  482. */
  483. static int l_ctiot_update(lua_State *L)
  484. {
  485. uint16_t msgId;
  486. if (luat_ctiot_update_reg(&msgId, NULL))
  487. {
  488. lua_pushboolean(L, 0);
  489. }
  490. else
  491. {
  492. lua_pushboolean(L, 1);
  493. }
  494. return 1;
  495. }
  496. #endif
  497. #include "rotable2.h"
  498. static const rotable_Reg_t reg_ctiot[] =
  499. {
  500. #ifdef AIR302
  501. { "init", ROREG_FUNC(l_ctiot_init)},
  502. { "param", ROREG_FUNC(l_ctiot_param)},
  503. { "ep", ROREG_FUNC(l_ctiot_ep)},
  504. { "isReady", ROREG_FUNC(l_ctiot_ready)},
  505. // { "mode", ROREG_FUNC(l_ctiot_mode)},
  506. { "connect", ROREG_FUNC(l_ctiot_connect)},
  507. { "disconnect", ROREG_FUNC(l_ctiot_disconnect)},
  508. { "write", ROREG_FUNC(l_ctiot_write)},
  509. // { "read", ROREG_FUNC(l_ctiot_read)},
  510. { "update", ROREG_FUNC(l_ctiot_update)},
  511. // ----- 类型常量
  512. { "CON", ROREG_INT(0)},
  513. { "NON", ROREG_INT(1)},
  514. { "NON_REL", ROREG_INT(2)},
  515. { "CON_REL", ROREG_INT(3)},
  516. #endif
  517. { NULL, ROREG_INT(0) }
  518. };
  519. LUAMOD_API int luaopen_ctiot( lua_State *L ) {
  520. luat_newlib2(L, reg_ctiot);
  521. return 1;
  522. }