luat_lib_ymodem.c 4.3 KB

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