luat_lib_airlink.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include "luat_base.h"
  2. #include "luat_gpio.h"
  3. #include "luat_mem.h"
  4. #include "luat_mcu.h"
  5. #include "luat_msgbus.h"
  6. #include "luat_timer.h"
  7. #include "luat_rtos.h"
  8. #include "luat_mcu.h"
  9. #include <math.h>
  10. #include "luat_airlink.h"
  11. #include "luat_airlink_fota.h"
  12. #include "luat_zbuff.h"
  13. #define LUAT_LOG_TAG "airlink"
  14. #include "luat_log.h"
  15. extern airlink_statistic_t g_airlink_statistic;
  16. extern uint32_t g_airlink_spi_task_mode;
  17. extern uint32_t g_airlink_pause;
  18. static int l_airlink_init(lua_State *L) {
  19. LLOGD("初始化AirLink");
  20. luat_airlink_init();
  21. return 0;
  22. }
  23. static int l_airlink_start(lua_State *L) {
  24. int id = luaL_checkinteger(L, 1);
  25. if (id == 0) {
  26. // 临时入口,先写死
  27. LLOGD("启动AirLink从机模式");
  28. }
  29. else {
  30. // 临时入口,先写死
  31. LLOGD("启动AirLink主机模式");
  32. }
  33. luat_airlink_task_start();
  34. luat_airlink_start(id);
  35. return 0;
  36. }
  37. static int l_airlink_stop(lua_State *L) {
  38. int id = luaL_checkinteger(L, 1);
  39. if (id == 0) {
  40. // 临时入口,先写死
  41. LLOGD("停止AirLink从机模式");
  42. }
  43. else {
  44. // 临时入口,先写死
  45. LLOGD("停止AirLink主机模式");
  46. }
  47. luat_airlink_stop(id);
  48. return 0;
  49. }
  50. static int l_airlink_test(lua_State *L) {
  51. int count = luaL_checkinteger(L, 1);
  52. luat_airlink_cmd_t* cmd = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, sizeof(luat_airlink_cmd_t) + 128);
  53. cmd->cmd = 0x21;
  54. cmd->len = 128;
  55. if (count > 0) {
  56. // 发送次数
  57. LLOGD("测试AirLink发送%d次", count);
  58. for (size_t i = 0; i < count; i++)
  59. {
  60. luat_airlink_send2slave(cmd);
  61. }
  62. }
  63. luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
  64. return 0;
  65. }
  66. static void print_stat(const char* tag, airlink_statistic_part_t* part, int is_full) {
  67. if (is_full) {
  68. LLOGD("统计信息 %s %lld %lld %lld %lld", tag, part->total, part->ok, part->err, part->drop);
  69. }
  70. else {
  71. LLOGD("统计信息 %s %lld", tag, part->total);
  72. }
  73. }
  74. static int l_airlink_statistics(lua_State *L) {
  75. airlink_statistic_t tmp;
  76. memcpy(&tmp, &g_airlink_statistic, sizeof(airlink_statistic_t));
  77. print_stat("收发总包", &tmp.tx_pkg, 1);
  78. print_stat("发送IP包", &tmp.tx_ip, 1);
  79. print_stat("发送IP字节", &tmp.tx_bytes, 1);
  80. print_stat("接收IP包", &tmp.rx_ip, 1);
  81. print_stat("接收IP字节", &tmp.rx_bytes, 1);
  82. if (g_airlink_spi_task_mode == 0) {
  83. }
  84. else {
  85. print_stat("等待从机", &tmp.wait_rdy, 0);
  86. print_stat("Task超时事件", &tmp.event_timeout, 0);
  87. print_stat("Task新数据事件", &tmp.event_new_data, 0);
  88. // print_stat("Task从机通知事件", &tmp.event_rdy_irq, 0);
  89. }
  90. return 0;
  91. }
  92. static int l_airlink_slave_reboot(lua_State *L) {
  93. LLOGD("重启从机");
  94. luat_airlink_cmd_t* cmd = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, sizeof(luat_airlink_cmd_t) + 4);
  95. cmd->cmd = 0x03;
  96. cmd->len = 4;
  97. luat_airlink_send2slave(cmd);
  98. luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
  99. return 0;
  100. }
  101. static int l_airlink_sdata(lua_State *L) {
  102. size_t len = 0;
  103. const char* data = NULL;
  104. if (lua_type(L, 1) == LUA_TSTRING) {
  105. data = luaL_checklstring(L, 1, &len);
  106. }
  107. else if (lua_isuserdata(L, 1)) {
  108. // zbuff
  109. luat_zbuff_t* buff = tozbuff(L);
  110. data = (const char*)buff->addr;
  111. len = buff->used;
  112. }
  113. else {
  114. LLOGE("无效的参数,只能是字符串或者zbuff");
  115. return 0;
  116. }
  117. if (len > 1500) {
  118. LLOGE("无效的数据长度,最大1500字节");
  119. return 0;
  120. }
  121. luat_airlink_cmd_t* cmd = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, sizeof(luat_airlink_cmd_t) + len);
  122. cmd->cmd = 0x20;
  123. cmd->len = len;
  124. memcpy(cmd->data, data, len);
  125. luat_airlink_send2slave(cmd);
  126. luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
  127. lua_pushboolean(L, 1);
  128. return 1;
  129. }
  130. static int l_airlink_ready(lua_State *L) {
  131. lua_pushboolean(L, luat_airlink_ready());
  132. return 1;
  133. }
  134. static int l_airlink_cmd(lua_State *L) {
  135. size_t len = 0;
  136. const char* data = NULL;
  137. uint32_t cmd_id = luaL_checkinteger(L, 1);
  138. if (lua_type(L, 2) == LUA_TSTRING) {
  139. data = luaL_checklstring(L, 1, &len);
  140. }
  141. else if (lua_isuserdata(L, 1)) {
  142. // zbuff
  143. luat_zbuff_t* buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  144. data = (const char*)buff->addr;
  145. len = buff->used;
  146. }
  147. else {
  148. LLOGE("无效的参数,只能是字符串或者zbuff");
  149. return 0;
  150. }
  151. if (len > 1500) {
  152. LLOGE("无效的数据长度,最大1500字节");
  153. return 0;
  154. }
  155. luat_airlink_cmd_t* cmd = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, sizeof(luat_airlink_cmd_t) + len);
  156. cmd->cmd = cmd_id;
  157. cmd->len = len;
  158. memcpy(cmd->data, data, len);
  159. luat_airlink_send2slave(cmd);
  160. luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
  161. lua_pushboolean(L, 1);
  162. return 1;
  163. }
  164. static int sample_cmd_nodata(lua_State *L, uint32_t cmd_id) {
  165. luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(cmd_id, 8);
  166. uint64_t pkgid = luat_airlink_get_next_cmd_id();
  167. memcpy(cmd->data, &pkgid, 8);
  168. luat_airlink_send2slave(cmd);
  169. luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
  170. lua_pushboolean(L, 1);
  171. return 1;
  172. }
  173. static int l_airlink_sfota_init(lua_State *L) {
  174. LLOGD("执行sfota_init");
  175. return sample_cmd_nodata(L, 0x04);
  176. }
  177. static int l_airlink_sfota_done(lua_State *L) {
  178. LLOGD("执行sfota_done");
  179. return sample_cmd_nodata(L, 0x06);
  180. }
  181. static int l_airlink_sfota_end(lua_State *L) {
  182. LLOGD("执行sfota_end");
  183. return sample_cmd_nodata(L, 0x07);
  184. }
  185. static int l_airlink_sfota_write(lua_State *L) {
  186. LLOGD("执行sfota_write");
  187. size_t len = 0;
  188. const char* data = NULL;
  189. if (lua_type(L, 1) == LUA_TSTRING) {
  190. data = luaL_checklstring(L, 1, &len);
  191. }
  192. else if (lua_isuserdata(L, 1)) {
  193. // zbuff
  194. luat_zbuff_t* buff = ((luat_zbuff_t *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE));
  195. data = (const char*)buff->addr;
  196. len = buff->used;
  197. }
  198. else {
  199. LLOGE("无效的参数,只能是字符串或者zbuff");
  200. return 0;
  201. }
  202. if (len > 1500) {
  203. LLOGE("无效的数据长度,最大1500字节");
  204. return 0;
  205. }
  206. luat_airlink_cmd_t* cmd = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, sizeof(luat_airlink_cmd_t) + len);
  207. cmd->cmd = 0x05;
  208. cmd->len = len;
  209. memcpy(cmd->data, data, len);
  210. luat_airlink_send2slave(cmd);
  211. luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
  212. lua_pushboolean(L, 1);
  213. return 1;
  214. }
  215. /*
  216. 配置AirLink的参数
  217. @api airlink.config(key, value)
  218. @int key 配置项, 参考airlink的常数项
  219. @int value 配置值
  220. @return bool 成功返回true, 失败返回nil
  221. @usage
  222. --配置AirLink的SPI ID为1, CS引脚为10, RDY引脚为11, IRQ引脚为12
  223. airlink.config(airlink.CONF_SPI_ID, 1)
  224. airlink.config(airlink.CONF_SPI_CS, 10)
  225. airlink.config(airlink.CONF_SPI_RDY, 11)
  226. airlink.config(airlink.CONF_SPI_IRQ, 12)
  227. */
  228. static int l_airlink_config(lua_State *L) {
  229. int key = luaL_checkinteger(L, 1);
  230. int value = luaL_checkinteger(L, 2);
  231. switch (key)
  232. {
  233. case LUAT_AIRLINK_CONF_SPI_ID:
  234. g_airlink_spi_conf.spi_id = value;
  235. break;
  236. case LUAT_AIRLINK_CONF_SPI_CS:
  237. g_airlink_spi_conf.cs_pin = value;
  238. break;
  239. case LUAT_AIRLINK_CONF_SPI_RDY:
  240. g_airlink_spi_conf.rdy_pin = value;
  241. break;
  242. case LUAT_AIRLINK_CONF_SPI_IRQ:
  243. g_airlink_spi_conf.irq_pin = value;
  244. break;
  245. default:
  246. return 0;
  247. }
  248. lua_pushboolean(L, 1);
  249. return 1;
  250. }
  251. static int l_airlink_sfota(lua_State *L) {
  252. // 直接发指令是不行了, 需要干预airlink task的执行流程
  253. // bk72xx的flash擦除很慢, 导致spi master需要等很久才能发下一个包
  254. const char* path = luaL_checkstring(L, 1);
  255. luat_airlink_fota_t ctx = {0};
  256. memcpy(ctx.path, path, strlen(path) + 1);
  257. int ret = luat_airlink_fota_init(&ctx);
  258. if (ret) {
  259. LLOGE("sfota 启动失败!!! %s %d", path, ret);
  260. }
  261. lua_pushboolean(L, ret == 0);
  262. return 1;
  263. }
  264. static int l_airlink_debug(lua_State *L) {
  265. g_airlink_debug = luaL_checkinteger(L, 1);
  266. return 0;
  267. }
  268. static int l_airlink_pause(lua_State *L) {
  269. g_airlink_pause = luaL_checkinteger(L, 1);
  270. return 0;
  271. }
  272. #include "rotable2.h"
  273. static const rotable_Reg_t reg_airlink[] =
  274. {
  275. { "init" , ROREG_FUNC(l_airlink_init )},
  276. { "config", ROREG_FUNC(l_airlink_config)},
  277. { "start" , ROREG_FUNC(l_airlink_start)},
  278. { "stop" , ROREG_FUNC(l_airlink_stop )},
  279. { "ready", ROREG_FUNC(l_airlink_ready)},
  280. { "test", ROREG_FUNC(l_airlink_test )},
  281. { "sdata", ROREG_FUNC(l_airlink_sdata)},
  282. { "cmd", ROREG_FUNC(l_airlink_cmd)},
  283. { "statistics", ROREG_FUNC(l_airlink_statistics )},
  284. { "slave_reboot", ROREG_FUNC(l_airlink_slave_reboot )},
  285. { "pause", ROREG_FUNC(l_airlink_pause)},
  286. // 测试用的fota指令
  287. { "sfota", ROREG_FUNC(l_airlink_sfota )},
  288. { "sfota_init", ROREG_FUNC(l_airlink_sfota_init )},
  289. { "sfota_done", ROREG_FUNC(l_airlink_sfota_done )},
  290. { "sfota_end", ROREG_FUNC(l_airlink_sfota_end )},
  291. { "sfota_write", ROREG_FUNC(l_airlink_sfota_write )},
  292. { "debug", ROREG_FUNC(l_airlink_debug )},
  293. { "MODE_SPI_SLAVE", ROREG_INT(0) },
  294. { "MODE_SPI_MASTER", ROREG_INT(1) },
  295. { "CONF_SPI_ID", ROREG_INT(LUAT_AIRLINK_CONF_SPI_ID)},
  296. { "CONF_SPI_CS", ROREG_INT(LUAT_AIRLINK_CONF_SPI_CS)},
  297. { "CONF_SPI_RDY", ROREG_INT(LUAT_AIRLINK_CONF_SPI_RDY)},
  298. { "CONF_SPI_IRQ", ROREG_INT(LUAT_AIRLINK_CONF_SPI_IRQ)},
  299. { NULL, ROREG_INT(0) }
  300. };
  301. LUAMOD_API int luaopen_airlink( lua_State *L ) {
  302. luat_newlib2(L, reg_airlink);
  303. return 1;
  304. }