luat_lib_airlink.c 9.5 KB

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