luat_lib_ymodem.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. @module ymodem
  3. @summary ymodem协议
  4. @version 1.0
  5. @date 2022.09.30
  6. @author Dozingfiretruck
  7. @tag LUAT_USE_YMODEM
  8. @usage
  9. -- 本库的用途是接收数据, 若需要发送文件, 建议用xmodem库
  10. local handler = ymodem.create("/")
  11. uart.setup(1, 115200)
  12. uart.on(1, "receive", function(id, len)
  13. while 1 do
  14. local data = uart.read(id, 512)
  15. if not data or #data == 0 then
  16. break
  17. end
  18. ymodem.receive(handler, data)
  19. end
  20. end)
  21. */
  22. #include "luat_base.h"
  23. #include "luat_mem.h"
  24. #include "luat_ymodem.h"
  25. #include "luat_zbuff.h"
  26. #define LUAT_LOG_TAG "ymodem"
  27. #include "luat_log.h"
  28. typedef struct
  29. {
  30. void *ctrl;
  31. }ymodem_handler;
  32. /*
  33. 创建一个ymodem处理句柄
  34. @api ymodem.create(dir_path,file_path)
  35. @string 保存的文件夹路径,默认是"/"
  36. @string 强制保存的绝对文件路径,默认是空,如果设置了,就会直接保存在该文件中
  37. @return boolean 成功true, 失败false
  38. @usage
  39. local handler = ymodem.create("/")
  40. */
  41. static int l_ymodem_create(lua_State *L){
  42. ymodem_handler *handler = (ymodem_handler *)lua_newuserdata(L, sizeof(ymodem_handler));
  43. size_t len;
  44. const char *dir_path,*file_path;
  45. if (lua_isstring(L, 1))
  46. {
  47. dir_path = lua_tolstring(L, 1, &len);
  48. }
  49. else
  50. {
  51. dir_path = NULL;
  52. }
  53. if (lua_isstring(L, 2))
  54. {
  55. file_path = lua_tolstring(L, 2, &len);
  56. }
  57. else
  58. {
  59. file_path = NULL;
  60. }
  61. handler->ctrl = luat_ymodem_create_handler(dir_path?dir_path:"/", file_path);
  62. lua_pushlightuserdata(L, handler);
  63. return 1;
  64. }
  65. /*
  66. ymodem接收文件数据并保存
  67. @api ymodem.receive(handler, data)
  68. @userdata ymodem处理句柄
  69. @zbuff/string 输入的数据
  70. @return boolean 成功true,失败false
  71. @return int ack值,需要通过串口/网络等途径返回发送方
  72. @return int flag值,需要通过串口/网络等途径返回发送方,如果有ack值则不发送flag
  73. @return boolean, 一个文件接收完成true,传输中false
  74. @return boolean, 整个传输完成true 否则false
  75. @usage
  76. -- 注意, 数据来源不限, 通常是uart.read得到data
  77. no_error,ack,flag,file_done,all_done = ymodem.receive(handler, data)
  78. */
  79. static int l_ymodem_receive(lua_State *L){
  80. ymodem_handler *handler = (ymodem_handler *)lua_touserdata(L, 1);
  81. int result;
  82. size_t len;
  83. uint8_t ack, flag, file_ok, all_done;
  84. const char *data;
  85. if (handler && handler->ctrl)
  86. {
  87. if (lua_isstring(L, 2))
  88. {
  89. data = lua_tolstring(L, 1, &len);
  90. }
  91. else if(lua_isuserdata(L, 2))
  92. {
  93. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  94. len = buff->used;
  95. data = (const char *)(buff->addr);
  96. }
  97. else
  98. {
  99. data = NULL;
  100. len = 0;
  101. }
  102. result = luat_ymodem_receive(handler->ctrl, (uint8_t*)data, len, &ack, &flag, &file_ok, &all_done);
  103. lua_pushboolean(L, !result);
  104. lua_pushinteger(L, ack);
  105. if (flag)
  106. {
  107. lua_pushinteger(L, flag);
  108. }
  109. else
  110. {
  111. lua_pushnil(L);
  112. }
  113. lua_pushboolean(L, file_ok);
  114. lua_pushboolean(L, all_done);
  115. }
  116. else
  117. {
  118. LLOGE("%x,%x", handler, handler->ctrl);
  119. lua_pushboolean(L, 0);
  120. lua_pushnil(L);
  121. lua_pushnil(L);
  122. lua_pushboolean(L, 0);
  123. lua_pushboolean(L, 0);
  124. }
  125. return 5;
  126. }
  127. /*
  128. 重置ymodem处理过程
  129. @api ymodem.reset(handler)
  130. @userdata ymodem处理句柄
  131. @usage
  132. -- 恢复到初始状态,一般用于接收出错后重置,从而进行下一次接收
  133. ymodem.reset(handler)
  134. */
  135. static int l_ymodem_reset(lua_State *L){
  136. ymodem_handler *handler = (ymodem_handler *)lua_touserdata(L, 1);
  137. if (handler && handler->ctrl) luat_ymodem_reset(handler->ctrl);
  138. return 0;
  139. }
  140. /*
  141. 释放ymodem处理句柄
  142. @api ymodem.release(handler)
  143. @userdata handler
  144. @usage
  145. ymodem.release(handler)
  146. */
  147. static int l_ymodem_release(lua_State *L){
  148. ymodem_handler *handler = (ymodem_handler *)lua_touserdata(L, 1);
  149. if (handler && handler->ctrl) {
  150. luat_ymodem_release(handler->ctrl);
  151. handler->ctrl = NULL;
  152. }
  153. return 0;
  154. }
  155. #include "rotable2.h"
  156. static const rotable_Reg_t reg_ymodem[] =
  157. {
  158. { "create", ROREG_FUNC(l_ymodem_create)},
  159. { "receive", ROREG_FUNC(l_ymodem_receive)},
  160. { "reset", ROREG_FUNC(l_ymodem_reset)},
  161. { "release", ROREG_FUNC(l_ymodem_release)},
  162. { NULL, ROREG_INT(0)}
  163. };
  164. LUAMOD_API int luaopen_ymodem( lua_State *L ) {
  165. luat_newlib2(L, reg_ymodem);
  166. return 1;
  167. }