luat_airlink_cmd_exec_basic.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "luat_base.h"
  2. #include "luat_spi.h"
  3. #include "luat_airlink.h"
  4. #include "luat_rtos.h"
  5. #include "luat_debug.h"
  6. #include "luat_spi.h"
  7. #include "luat_pm.h"
  8. #include "luat_gpio.h"
  9. #include "luat_airlink.h"
  10. #include "luat_fota.h"
  11. #include "luat_mem.h"
  12. #include "luat_msgbus.h"
  13. #define LUAT_LOG_TAG "airlink"
  14. #include "luat_log.h"
  15. __AIRLINK_CODE_IN_RAM__ int luat_airlink_cmd_exec_nop(luat_airlink_cmd_t *cmd, void *userdata)
  16. {
  17. return 0;
  18. }
  19. int luat_airlink_cmd_exec_ping(luat_airlink_cmd_t *cmd, void *userdata)
  20. {
  21. LLOGD("收到ping指令,返回pong");
  22. // TODO 返回PONG
  23. return 0;
  24. }
  25. int luat_airlink_cmd_exec_pong(luat_airlink_cmd_t *cmd, void *userdata)
  26. {
  27. LLOGD("收到pong指令,检查数据是否匹配!!");
  28. return 0;
  29. }
  30. int luat_airlink_cmd_exec_reset(luat_airlink_cmd_t *cmd, void *userdata)
  31. {
  32. LLOGD("收到reset指令!!!");
  33. luat_pm_reset();
  34. return 0;
  35. }
  36. int luat_airlink_cmd_exec_fota_init(luat_airlink_cmd_t *cmd, void *userdata)
  37. {
  38. LLOGD("收到FOTA初始化指令!!!");
  39. int ret = luat_fota_init(0, 0, NULL, NULL, 0);
  40. LLOGD("fota_init ret %d", ret);
  41. return 0;
  42. }
  43. int luat_airlink_cmd_exec_fota_write(luat_airlink_cmd_t *cmd, void *userdata)
  44. {
  45. // LLOGD("收到FOTA数据, len=%ld %02X%02X%02X%02X", cmd->len, cmd->data[0], cmd->data[1], cmd->data[2], cmd->data[3]);
  46. int ret = luat_fota_write(cmd->data, cmd->len);
  47. if (ret < 0) {
  48. LLOGD("fota_write ret %d", ret);
  49. }
  50. return 0;
  51. }
  52. int luat_airlink_cmd_exec_fota_done(luat_airlink_cmd_t *cmd, void *userdata)
  53. {
  54. LLOGD("收到FOTA传输完毕指令!!!");
  55. int ret = luat_fota_done();
  56. LLOGD("fota_done ret %d", ret);
  57. return 0;
  58. }
  59. int luat_airlink_cmd_exec_fota_end(luat_airlink_cmd_t *cmd, void *userdata)
  60. {
  61. LLOGD("收到FOTA传输完毕指令!!!");
  62. int ret = luat_fota_end(1);
  63. LLOGD("fota_end ret %d", ret);
  64. return 0;
  65. }
  66. #ifdef __LUATOS__
  67. #include "luat_msgbus.h"
  68. static int sdata_cb(lua_State *L, void *ptr)
  69. {
  70. rtos_msg_t *msg = (rtos_msg_t *)lua_topointer(L, -1);
  71. lua_getglobal(L, "sys_pub");
  72. lua_pushstring(L, "AIRLINK_SDATA");
  73. lua_pushlstring(L, msg->ptr, msg->arg1);
  74. luat_heap_opt_free(AIRLINK_MEM_TYPE, msg->ptr);
  75. lua_call(L, 2, 0);
  76. return 0;
  77. }
  78. int luat_airlink_cmd_exec_sdata(luat_airlink_cmd_t *cmd, void *userdata)
  79. {
  80. // LLOGD("收到sdata指令!!! %d", cmd->len);
  81. rtos_msg_t msg = {0};
  82. msg.handler = sdata_cb;
  83. msg.ptr = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, cmd->len);
  84. if (msg.ptr == NULL)
  85. {
  86. LLOGE("sdata_cb malloc fail!!! %d", cmd->len);
  87. return 0;
  88. }
  89. memcpy(msg.ptr, cmd->data, cmd->len);
  90. msg.arg1 = cmd->len;
  91. luat_msgbus_put(&msg, 0);
  92. return 0;
  93. }
  94. #endif
  95. int luat_airlink_cmd_exec_notify_log(luat_airlink_cmd_t *cmd, void *userdata) {
  96. if (cmd->len < 9) {
  97. return 0;
  98. }
  99. uint8_t level = cmd->data[0];
  100. uint8_t tag_len = cmd->data[1];
  101. char tag[16] = {0};
  102. memcpy(tag, cmd->data + 2, tag_len);
  103. luat_log_log(level, tag, "%.*s", cmd->len - 2 - tag_len, cmd->data + 2 + tag_len);
  104. return 0;
  105. }
  106. static int push_args(lua_State *L, uint8_t* ptr, uint32_t* limit) {
  107. uint32_t tmp = *limit;
  108. if (tmp < 2) {
  109. return 0;
  110. }
  111. uint8_t type = ptr[0];
  112. uint8_t len = ptr[1];
  113. uint32_t i = 0;
  114. uint8_t b = 0;
  115. float f = 0;
  116. if (len + 2 > tmp) {
  117. return -1;
  118. }
  119. switch (type)
  120. {
  121. case LUA_TSTRING:
  122. // LLOGD("添加字符串 %.*s", len, ptr + 2);
  123. lua_pushlstring(L, (const char*)ptr + 2, len);
  124. break;
  125. case LUA_TNUMBER:
  126. memcpy(&f, ptr + 2, 4);
  127. // LLOGD("添加浮点数 %f", f);
  128. lua_pushnumber(L, f);
  129. break;
  130. case LUA_TINTEGER:
  131. memcpy(&i, ptr + 2, 4);
  132. // LLOGD("添加整数 %d", i);
  133. lua_pushinteger(L, i);
  134. break;
  135. case LUA_TBOOLEAN:
  136. memcpy(&b, ptr + 2, 1);
  137. // LLOGD("添加bool %d", b);
  138. lua_pushboolean(L, b);
  139. break;
  140. case LUA_TNIL:
  141. // LLOGD("添加nil");
  142. lua_pushnil(L);
  143. break;
  144. default:
  145. break;
  146. }
  147. return len + 2;
  148. }
  149. static int l_airlink_sys_pub(lua_State *L, void* ptr) {
  150. int ret = 0;
  151. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  152. if (lua_getglobal(L, "sys_pub") != LUA_TFUNCTION) {
  153. LLOGE("获取 sys_pub 失败!!!");
  154. return 0;
  155. };
  156. uint32_t limit = msg->arg1;
  157. uint8_t* tmp = msg->ptr;
  158. int c = 0;
  159. while (limit > 0) {
  160. ret = push_args(L, tmp, &limit);
  161. if (ret <= 0) {
  162. break;
  163. }
  164. tmp += ret;
  165. limit -= ret;
  166. c ++;
  167. }
  168. ret = lua_pcall(L, c, 0, 0);
  169. // LLOGD("pcall args %d ret %d", c, ret);
  170. return 0;
  171. }
  172. int luat_airlink_cmd_exec_notify_sys_pub(luat_airlink_cmd_t *cmd, void *userdata) {
  173. // LLOGD("收到sys_pub指令!!! %d", cmd->len);
  174. if (cmd->len < 12) {
  175. LLOGD("非法的sys_pub指令,长度不足 %d", cmd->len);
  176. return 0;
  177. }
  178. rtos_msg_t msg = {0};
  179. msg.handler = l_airlink_sys_pub;
  180. msg.ptr = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, cmd->len - 8);
  181. if (msg.ptr == NULL) {
  182. LLOGE("l_airlink_sys_pub malloc fail!!! %d", cmd->len - 8);
  183. return 0;
  184. }
  185. memcpy(msg.ptr, cmd->data + 8, cmd->len - 8);
  186. msg.arg1 = cmd->len - 8;
  187. luat_msgbus_put(&msg, 0);
  188. return 0;
  189. }