luat_lib_wlanraw.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. @module wlanraw
  3. @summary WLAN数据RAW传输
  4. @version 1.0
  5. @date 2024.4.11
  6. @tag LUAT_USE_WLAN_RAW
  7. @usage
  8. -- 请查阅 https://github.com/wendal/xt804-spinet
  9. */
  10. #include "luat_base.h"
  11. #include "rotable2.h"
  12. #include "luat_zbuff.h"
  13. #include "luat_msgbus.h"
  14. #include "luat_timer.h"
  15. #include "luat_wlan_raw.h"
  16. #include "luat_zbuff.h"
  17. #include "luat_mem.h"
  18. #define LUAT_LOG_TAG "wlan.raw"
  19. #include "luat_log.h"
  20. // 存放回调函数
  21. static int lua_cb_ref;
  22. #define RAW_BUFF_COUNT 8
  23. #define RAW_BUFF_SIZE 1600
  24. static luat_wlan_raw_data_t datas[RAW_BUFF_COUNT];
  25. /*
  26. 初始化WLAN的RAW层
  27. @api wlanraw.setup(opts, cb)
  28. @table opts 配置参数
  29. @function 回调函数,形式function(buff, size)
  30. @return boolean true表示成功,其他失败
  31. @usage
  32. -- 当前仅XT804系列支持, 例如 Air101/Air103/Air601/Air690
  33. wlanraw.setup({
  34. buffsize = 1600, -- 缓冲区大小, 默认1600字节
  35. buffcount = 10, -- 缓冲区数量, 默认8
  36. }, cb)
  37. */
  38. static int l_wlan_raw_setup(lua_State *L) {
  39. if (datas[0].buff) {
  40. return 0;
  41. }
  42. size_t len = RAW_BUFF_SIZE;
  43. size_t count = RAW_BUFF_COUNT;
  44. lua_newtable(L);
  45. for (size_t i = 0; i < count; i++)
  46. {
  47. luat_zbuff_t *buff = (luat_zbuff_t *)lua_newuserdata(L, sizeof(luat_zbuff_t));
  48. luaL_setmetatable(L, LUAT_ZBUFF_TYPE);
  49. lua_pushvalue(L, -1);
  50. memset(buff, 0, sizeof(luat_zbuff_t));
  51. buff->type = LUAT_HEAP_PSRAM;
  52. buff->addr = (uint8_t *)luat_heap_opt_malloc(buff->type, len);
  53. buff->len = len;
  54. datas[i].zbuff_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  55. datas[i].zbuff_used = &buff->used;
  56. datas[i].buff = buff->addr;
  57. lua_seti(L, -2, i + 1);
  58. // LLOGD("zbuff_ref 数组索引%d lua索引%d", i, datas[i].zbuff_ref);
  59. }
  60. lua_pushvalue(L, 2);
  61. lua_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  62. luat_wlan_raw_conf_t conf = {0};
  63. luat_wlan_raw_setup(&conf);
  64. lua_pushboolean(L, 1);
  65. lua_pushvalue(L, 3);
  66. return 1;
  67. }
  68. static int l_wlan_raw_write(lua_State *L) {
  69. int id = luaL_checkinteger(L, 1);
  70. luat_zbuff_t * buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  71. int offset = luaL_optinteger(L, 3, 0);
  72. int len = luaL_optinteger(L, 4, buff->used - offset);
  73. int ret = luat_wlan_raw_write(id, buff->addr + offset, len);
  74. lua_pushboolean(L, ret == 0);
  75. lua_pushinteger(L, ret);
  76. return 2;
  77. }
  78. static const rotable_Reg_t reg_wlan_raw[] = {
  79. { "setup", ROREG_FUNC(l_wlan_raw_setup)},
  80. { "write", ROREG_FUNC(l_wlan_raw_write)},
  81. { NULL, ROREG_INT(0)}
  82. };
  83. LUAMOD_API int luaopen_wlan_raw(lua_State *L)
  84. {
  85. luat_newlib2(L, reg_wlan_raw);
  86. return 1;
  87. }
  88. static int l_wlan_raw_event_handler(lua_State *L, void* ptr) {
  89. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  90. if (!lua_cb_ref) {
  91. return 0; // 没设置回调函数, 直接退出
  92. }
  93. lua_rawgeti(L, LUA_REGISTRYINDEX, lua_cb_ref);
  94. if (!lua_isfunction(L, -1)) {
  95. return 0; // 没设置回调函数, 直接退出
  96. }
  97. lua_pushinteger(L, msg->arg1);
  98. // LLOGD("取出zbuff lua索引%d", msg->arg2);
  99. lua_rawgeti(L, LUA_REGISTRYINDEX, msg->arg2);
  100. lua_call(L, 2, 0);
  101. return 0;
  102. }
  103. int l_wlan_raw_event(int tp, void* buff, size_t len) {
  104. rtos_msg_t msg = {
  105. .handler = l_wlan_raw_event_handler,
  106. .arg1 = tp,
  107. .arg2 = 0,
  108. .ptr = NULL,
  109. };
  110. if (len > RAW_BUFF_SIZE) {
  111. LLOGE("类型 %d 数据长度%d 超过缓冲区大小%d", tp, len, RAW_BUFF_SIZE);
  112. return -2;
  113. }
  114. if (!lua_cb_ref) {
  115. LLOGW("未初始化, 忽略数据 %d %p %d", tp, buff, len);
  116. return -3;
  117. }
  118. for (size_t i = 0; i < RAW_BUFF_COUNT; i++)
  119. {
  120. if ((*datas[i].zbuff_used) == 0) {
  121. // LLOGD("使用zbuff传输 数组索引%d 写入长度%d", i, len);
  122. memcpy(datas[i].buff, buff, len);
  123. // datas[i].used = 1;
  124. *(datas[i].zbuff_used) = len;
  125. msg.arg2 = datas[i].zbuff_ref;
  126. luat_msgbus_put(&msg, 0);
  127. return 0;
  128. }
  129. }
  130. LLOGE("没有可用的zbuff");
  131. return -1;
  132. }