luat_shell.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /**
  2. LuatOS Shell -- LuatOS 控制台
  3. */
  4. #include "lua.h"
  5. #include "luat_base.h"
  6. #include "luat_msgbus.h"
  7. #include "luat_mem.h"
  8. #define LUAT_LOG_TAG "shell"
  9. #include "luat_log.h"
  10. #ifdef LUAT_USE_MCU
  11. #include "luat_mcu.h"
  12. #endif
  13. #include "luat_shell.h"
  14. #include "luat_str.h"
  15. #include "luat_cmux.h"
  16. #ifdef LUAT_USE_YMODEM
  17. #include "luat_ymodem.h"
  18. #endif
  19. luat_shell_t shell_ctx = {
  20. .echo_enable = 1
  21. };
  22. extern luat_cmux_t cmux_ctx;
  23. static int luat_shell_loadstr(lua_State *L, void* ptr);
  24. // 查询BSP
  25. static void cmd_ati(char* uart_buff, size_t len);
  26. // 复位重启
  27. static void cmd_reset(char* uart_buff, size_t len);
  28. // AT回显OK
  29. static void cmd_at(char* uart_buff, size_t len);
  30. // AT回显设置
  31. static void cmd_ate(char* uart_buff, size_t len);
  32. // 查询内存状态
  33. static void cmd_free(char* uart_buff, size_t len);
  34. // 查询芯片唯一id
  35. static void cmd_cgsn(char* uart_buff, size_t len);
  36. // cmux初始化
  37. static void cmd_cmux_cmd_init(char* uart_buff, size_t len);
  38. // rpel 操作
  39. static void cmd_loadstr(char* uart_buff, size_t len);
  40. static void cmd_i2c_tools(char* uart_buff, size_t len);
  41. static void cmd_ping(char* uart_buff, size_t len);
  42. static void cmd_sntp(char* uart_buff, size_t len);
  43. // 文件操作
  44. static void cmd_ry(char* uart_buff, size_t len);
  45. static void cmd_lsdir(char* uart_buff, size_t len);
  46. static void cmd_fread(char* uart_buff, size_t len);
  47. static void cmd_fwrite(char* uart_buff, size_t len);
  48. const luat_shell_cmd_reg_t cmd_regs[] = {
  49. {"ATI", cmd_ati},
  50. {"ati", cmd_ati},
  51. {"AT+RESET", cmd_reset},
  52. {"at+ecrst", cmd_reset},
  53. {"AT+ECRST", cmd_reset},
  54. {"AT\r", cmd_at},
  55. {"AT\n", cmd_at},
  56. {"ATE", cmd_ate},
  57. {"free", cmd_free},
  58. #ifdef LUAT_USE_MCU
  59. {"AT+CGSN", cmd_cgsn},
  60. #endif
  61. {LUAT_CMUX_CMD_INIT, cmd_cmux_cmd_init},
  62. #ifdef LUAT_USE_I2CTOOLS
  63. {"i2c tools", cmd_i2c_tools},
  64. #endif
  65. #ifdef LUAT_USE_LOADSTR
  66. {"loadstr ", cmd_loadstr},
  67. #endif
  68. #ifdef LUAT_USE_YMODEM
  69. {"ry\r", cmd_ry},
  70. #endif
  71. #ifdef LUAT_USE_SNTP
  72. {"sntp", cmd_sntp},
  73. #endif
  74. #ifdef LUAT_USE_PING
  75. {"ping", cmd_ping},
  76. #endif
  77. {"lsdir ", cmd_lsdir},
  78. {"fread ", cmd_fread},
  79. {"fwrite ", cmd_fwrite},
  80. {NULL, NULL}
  81. };
  82. #ifdef LUAT_USE_SHELL
  83. void luat_shell_push(char* uart_buff, size_t rcount) {
  84. if (!rcount)
  85. return;
  86. if (cmux_ctx.state == 1) {
  87. luat_cmux_read((unsigned char *)uart_buff,rcount);
  88. return;
  89. }
  90. luat_shell_exec(uart_buff, rcount);
  91. }
  92. #endif
  93. void luat_shell_exec(char* uart_buff, size_t rcount) {
  94. int cmd_index = 0;
  95. if (shell_ctx.echo_enable){
  96. luat_shell_write(uart_buff, rcount);
  97. luat_shell_write("\r\n", 2);
  98. }
  99. while (cmd_regs[cmd_index].prefix != NULL)
  100. {
  101. if (!memcmp(cmd_regs[cmd_index].prefix, uart_buff, strlen(cmd_regs[cmd_index].prefix))) {
  102. cmd_regs[cmd_index].cmd(uart_buff, rcount);
  103. return;
  104. }
  105. cmd_index++;
  106. }
  107. luat_shell_write("ERR\r\n", 5);
  108. return;
  109. }
  110. // ---- 各种子命令
  111. #ifdef LUAT_USE_LOADSTR
  112. static int luat_shell_loadstr(lua_State *L, void* ptr) {
  113. lua_settop(L, 0);
  114. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  115. int ret = luaL_loadbuffer(L, (char*)ptr, msg->arg1, 0);
  116. if (ret == LUA_OK) {
  117. lua_pcall(L, 0, 0, 0);
  118. }
  119. else {
  120. LLOGW("loadstr %s", lua_tostring(L, -1));
  121. }
  122. return 0;
  123. }
  124. #endif
  125. static void cmd_ati(char* uart_buff, size_t len) {
  126. char buff[128] = {0};
  127. #ifdef LUAT_BSP_VERSION
  128. len = sprintf(buff, "LuatOS-SoC_%s_%s\r\n", LUAT_BSP_VERSION, luat_os_bsp());
  129. #else
  130. len = sprintf(buff, "LuatOS-SoC_%s_%s\r\n", luat_version_str(), luat_os_bsp());
  131. #endif
  132. luat_shell_write(buff, len);
  133. }
  134. static void cmd_reset(char* uart_buff, size_t len) {
  135. luat_shell_write("OK\r\n", 4);
  136. luat_os_reboot(0);
  137. }
  138. static void cmd_at(char* uart_buff, size_t len) {
  139. luat_shell_write("OK\r\n", 4);
  140. }
  141. static void cmd_ate(char* uart_buff, size_t len) {
  142. if (len > 4) {
  143. if (uart_buff[4] == '1')
  144. shell_ctx.echo_enable = 1;
  145. else
  146. shell_ctx.echo_enable = 0;
  147. luat_shell_write("OK\r\n", 4);
  148. }
  149. }
  150. static void cmd_free(char* uart_buff, size_t len) {
  151. char buff[256] = {0};
  152. size_t total, used, max_used = 0;
  153. luat_meminfo_luavm(&total, &used, &max_used);
  154. len = sprintf(buff, "lua total=%d used=%d max_used=%d\r\n", total, used, max_used);
  155. luat_shell_write(buff, len);
  156. luat_meminfo_sys(&total, &used, &max_used);
  157. len = sprintf(buff, "sys total=%d used=%d max_used=%d\r\n", total, used, max_used);
  158. luat_shell_write(buff, len);
  159. }
  160. #ifdef LUAT_USE_MCU
  161. static void cmd_cgsn(char* uart_buff, size_t len) {
  162. char buff[128] = {0};
  163. memcpy(buff, "+CGSN=", 6);
  164. char* _id = (char*)luat_mcu_unique_id(&len);
  165. luat_str_tohex(_id, len, buff+6);
  166. buff[6+len*2] = '\n';
  167. luat_shell_write(buff, 6+len*2+1);
  168. }
  169. #endif
  170. static void cmd_cmux_cmd_init(char* uart_buff, size_t len) {
  171. cmux_ctx.state = 1;
  172. #ifdef __AIR105_BSP__
  173. extern uint8_t gMainWDTEnable;
  174. gMainWDTEnable = 1;
  175. #endif
  176. luat_shell_write("OK\r\n", 4);
  177. }
  178. #ifdef LUAT_USE_I2CTOOLS
  179. #include "i2c_utils.h"
  180. extern void i2c_tools(const char * data,size_t len);
  181. static void cmd_i2c_tools(char* uart_buff, size_t len) {
  182. i2c_tools(uart_buff, len);
  183. }
  184. #endif
  185. #ifdef LUAT_USE_SNTP
  186. #include "luat_sntp.h"
  187. static void cmd_sntp(char* uart_buff, size_t len){
  188. ntp_get(-1);
  189. }
  190. #endif
  191. #ifdef LUAT_USE_PING
  192. #include "luat_ping.h"
  193. static void cmd_ping(char* uart_buff, size_t len){
  194. char* buff = (char*)memchr(uart_buff, ' ', len);
  195. l_ping(buff+1, len-(buff-uart_buff)-1);
  196. }
  197. #endif
  198. #ifdef LUAT_USE_LOADSTR
  199. static void cmd_loadstr(char* uart_buff, size_t len) {
  200. size_t slen = len - strlen("loadstr ") + 1;
  201. char* tmp = luat_heap_malloc(slen);
  202. if (tmp == NULL) {
  203. LLOGW("loadstr out of memory");
  204. }
  205. else {
  206. memset(tmp, 0, slen);
  207. memcpy(tmp, uart_buff, slen - 1);
  208. rtos_msg_t msg = {
  209. .handler = luat_shell_loadstr,
  210. .ptr = tmp,
  211. .arg1 = slen
  212. };
  213. luat_msgbus_put(&msg, 0);
  214. }
  215. }
  216. #endif
  217. #ifdef LUAT_USE_YMODEM
  218. static void* yhandler;
  219. static void cmd_ry(char* uart_buff, size_t len) {
  220. int result;
  221. uint8_t ack, flag, file_ok, all_done;
  222. if (shell_ctx.ymodem_enable == 0) {
  223. shell_ctx.ymodem_enable = 1;
  224. yhandler = luat_ymodem_create_handler("/", NULL);
  225. return;
  226. }
  227. result = luat_ymodem_receive(yhandler, (uint8_t*)uart_buff, len, &ack, &flag, &file_ok, &all_done);
  228. if (result) {
  229. luat_ymodem_reset(yhandler);
  230. return;
  231. }
  232. if (all_done) {
  233. luat_ymodem_release(yhandler);
  234. shell_ctx.ymodem_enable = 0;
  235. // luat_heap_free(yhandler);
  236. yhandler = NULL;
  237. }
  238. }
  239. #endif
  240. #include <string.h>
  241. #include "luat_fs.h"
  242. // 文件相关的命令
  243. static void cmd_lsdir(char* uart_buff, size_t len) {
  244. if (uart_buff[len - 1] == '\r') {
  245. uart_buff[len - 1] = 0;
  246. }
  247. if (uart_buff[len - 2] == '\r') {
  248. uart_buff[len - 2] = 0;
  249. }
  250. strtok(uart_buff, " ");
  251. char* path = strtok(NULL, " ");
  252. luat_fs_dirent_t* ents = luat_heap_malloc(sizeof(luat_fs_dirent_t) * 10);
  253. if (ents == NULL) {
  254. LLOGE("out of memory when malloc luat_fs_dirent_t");
  255. return;
  256. }
  257. int i = 0;
  258. int ret = 0;
  259. while (1)
  260. {
  261. //LLOGD("lsdir for %s %d %d", path, i* 10, 10);
  262. ret = luat_fs_lsdir(path, ents, i* 10, 10);
  263. //LLOGD("luat_fs_lsdir %d", ret);
  264. if (ret < 1)
  265. break;
  266. for (size_t i = 0; i < ret; i++)
  267. {
  268. LLOGD(">> %s %s", ents[i].d_type == 0 ? "FILE" : "DIR ", ents[i].d_name);
  269. }
  270. if (ret != 10)
  271. break;
  272. }
  273. luat_heap_free(ents);
  274. }
  275. static void cmd_fread(char* uart_buff, size_t len) {
  276. if (uart_buff[len - 1] == '\r') {
  277. uart_buff[len - 1] = 0;
  278. }
  279. if (uart_buff[len - 2] == '\r') {
  280. uart_buff[len - 2] = 0;
  281. }
  282. strtok(uart_buff, " ");
  283. char* path = strtok(NULL, " ");
  284. size_t flen = luat_fs_fsize(path);
  285. if (flen < 1) {
  286. LLOGD("ERR FILE NOT FOUND");
  287. return;
  288. }
  289. FILE* fd = luat_fs_fopen(path, "rb");
  290. if (fd == NULL) {
  291. LLOGD("ERR FILE NOT FOUND");
  292. return;
  293. }
  294. LLOGD("OK %d", flen);
  295. char* buff = luat_heap_malloc(4096);
  296. if (buff == NULL) {
  297. luat_fs_fclose(fd);
  298. LLOGE("out of memory when read file");
  299. return;
  300. }
  301. int count = 0;
  302. while (count < flen) {
  303. int rlen = luat_fs_fread(buff, 4096, 1, fd);
  304. if (rlen < 1)
  305. break;
  306. luat_shell_write(buff, rlen);
  307. }
  308. luat_fs_fclose(fd);
  309. }
  310. static void cmd_fwrite(char* uart_buff, size_t len) {
  311. LLOGD("not support yet");
  312. return;
  313. }
  314. /// 已废弃的函数
  315. //===============================================================================
  316. // 这个函数已废弃
  317. static int luat_shell_msg_handler(lua_State *L, void* ptr) {
  318. char buff[128] = {0};
  319. lua_settop(L, 0); //重置虚拟堆栈
  320. size_t rcount = 0;
  321. char *uart_buff = luat_shell_read(&rcount);
  322. int ret = 0;
  323. int len = 0;
  324. if (rcount) {
  325. if (cmux_ctx.state == 1){
  326. //luat_cmux_read((unsigned char *)uart_buff,rcount);
  327. }else{
  328. // 是不是ATI命令呢?
  329. if (shell_ctx.echo_enable){
  330. luat_shell_write(uart_buff, rcount);
  331. luat_shell_write("\r\n", 2);
  332. }
  333. // 查询版本号
  334. if (strncmp("ATI", uart_buff, 3) == 0 || strncmp("ati", uart_buff, 3) == 0) {
  335. char buff[128] = {0};
  336. #ifdef LUAT_BSP_VERSION
  337. len = sprintf(buff, "LuatOS-SoC_%s_%s\r\n", LUAT_BSP_VERSION, luat_os_bsp());
  338. #else
  339. len = sprintf(buff, "LuatOS-SoC_%s_%s\r\n", luat_version_str(), luat_os_bsp());
  340. #endif
  341. luat_shell_write(buff, len);
  342. }
  343. // 重启
  344. else if (strncmp("AT+RESET", uart_buff, 8) == 0
  345. || strncmp("at+ecrst", uart_buff, 8) == 0
  346. || strncmp("AT+ECRST", uart_buff, 8) == 0) {
  347. luat_shell_write("OK\r\n", 4);
  348. luat_os_reboot(0);
  349. }
  350. // AT测试
  351. else if (strncmp("AT\r", uart_buff, 3) == 0 || strncmp("AT\r\n", uart_buff, 4) == 0) {
  352. luat_shell_write("OK\r\n", 4);
  353. }
  354. // 回显关闭
  355. else if (strncmp("ATE0\r", uart_buff, 4) == 0 || strncmp("ATE0\r\n", uart_buff, 5) == 0) {
  356. shell_ctx.echo_enable = 0;
  357. luat_shell_write("OK\r\n", 4);
  358. }
  359. // 回显开启
  360. else if (strncmp("ATE1\r", uart_buff, 4) == 0 || strncmp("ATE1\r\n", uart_buff, 5) == 0) {
  361. shell_ctx.echo_enable = 1;
  362. luat_shell_write("OK\r\n", 4);
  363. }
  364. // 查询内存状态
  365. else if (strncmp("free", uart_buff, 4) == 0) {
  366. size_t total, used, max_used = 0;
  367. luat_meminfo_luavm(&total, &used, &max_used);
  368. len = sprintf(buff, "lua total=%d used=%d max_used=%d\r\n", total, used, max_used);
  369. luat_shell_write(buff, len);
  370. luat_meminfo_sys(&total, &used, &max_used);
  371. len = sprintf(buff, "sys total=%d used=%d max_used=%d\r\n", total, used, max_used);
  372. luat_shell_write(buff, len);
  373. }
  374. #ifdef LUAT_USE_MCU
  375. else if (strncmp("AT+CGSN", uart_buff, 7) == 0) {
  376. memcpy(buff, "+CGSN=", 6);
  377. char* _id = (char*)luat_mcu_unique_id((size_t *)&len);
  378. luat_str_tohex(_id, len, buff+6);
  379. buff[6+len*2] = '\n';
  380. luat_shell_write(buff, 6+len*2+1);
  381. }
  382. #endif
  383. // else if (!strncmp(LUAT_CMUX_CMD_INIT, uart_buff, strlen(LUAT_CMUX_CMD_INIT))) {
  384. // luat_shell_write("OK\n", 3);
  385. // cmux_state = 1;
  386. // }
  387. // 执行脚本
  388. else if (strncmp("loadstr ", uart_buff, strlen("loadstr ")) == 0) {
  389. char * tmp = (char*)uart_buff + strlen("loadstr ");
  390. ret = luaL_loadbuffer(L, tmp, strlen(uart_buff) - strlen("loadstr "), 0);
  391. if (ret == LUA_OK) {
  392. lua_pcall(L, 0, 0, 0);
  393. }
  394. else {
  395. LLOGW("loadstr %s", lua_tostring(L, -1));
  396. }
  397. }
  398. else {
  399. luat_shell_write("ERR\r\n", 5);
  400. }
  401. }
  402. }
  403. luat_shell_notify_recv();
  404. return 0;
  405. }
  406. // 函数已废弃
  407. void luat_shell_notify_read() {
  408. rtos_msg_t msg;
  409. msg.handler = luat_shell_msg_handler;
  410. msg.ptr = NULL;
  411. msg.arg1 = 0;
  412. msg.arg2 = 0;
  413. luat_msgbus_put(&msg, 0);
  414. }