at_cli.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-04-02 armink first version
  9. */
  10. #include <at.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include <rthw.h>
  16. #ifdef AT_USING_CLI
  17. #define AT_CLI_FIFO_SIZE 256
  18. static struct rt_semaphore console_rx_notice;
  19. static struct rt_ringbuffer *console_rx_fifo = RT_NULL;
  20. static rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size) = RT_NULL;
  21. #ifdef AT_USING_CLIENT
  22. static struct rt_semaphore client_rx_notice;
  23. static struct rt_ringbuffer *client_rx_fifo = RT_NULL;
  24. #endif
  25. static char console_getchar(void)
  26. {
  27. char ch;
  28. rt_sem_take(&console_rx_notice, RT_WAITING_FOREVER);
  29. rt_ringbuffer_getchar(console_rx_fifo, (rt_uint8_t *)&ch);
  30. return ch;
  31. }
  32. static rt_err_t console_getchar_rx_ind(rt_device_t dev, rt_size_t size)
  33. {
  34. uint8_t ch;
  35. rt_size_t i;
  36. for (i = 0; i < size; i++)
  37. {
  38. /* read a char */
  39. if (rt_device_read(dev, 0, &ch, 1))
  40. {
  41. rt_ringbuffer_put_force(console_rx_fifo, &ch, 1);
  42. rt_sem_release(&console_rx_notice);
  43. }
  44. }
  45. return RT_EOK;
  46. }
  47. void at_cli_init(void)
  48. {
  49. rt_base_t int_lvl;
  50. rt_device_t console;
  51. rt_sem_init(&console_rx_notice, "cli_c", 0, RT_IPC_FLAG_FIFO);
  52. /* create RX FIFO */
  53. console_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);
  54. /* created must success */
  55. RT_ASSERT(console_rx_fifo);
  56. int_lvl = rt_hw_interrupt_disable();
  57. console = rt_console_get_device();
  58. if (console)
  59. {
  60. /* backup RX indicate */
  61. odev_rx_ind = console->rx_indicate;
  62. rt_device_set_rx_indicate(console, console_getchar_rx_ind);
  63. }
  64. rt_hw_interrupt_enable(int_lvl);
  65. }
  66. void at_cli_deinit(void)
  67. {
  68. rt_base_t int_lvl;
  69. rt_device_t console;
  70. int_lvl = rt_hw_interrupt_disable();
  71. console = rt_console_get_device();
  72. if (console && odev_rx_ind)
  73. {
  74. /* restore RX indicate */
  75. rt_device_set_rx_indicate(console, odev_rx_ind);
  76. }
  77. rt_hw_interrupt_enable(int_lvl);
  78. rt_sem_detach(&console_rx_notice);
  79. rt_ringbuffer_destroy(console_rx_fifo);
  80. }
  81. #ifdef AT_USING_SERVER
  82. static rt_err_t at_server_console_getchar(struct at_server *server, char *ch, rt_int32_t timeout)
  83. {
  84. *ch = console_getchar();
  85. return RT_EOK;
  86. }
  87. static void server_cli_parser(void)
  88. {
  89. extern at_server_t at_get_server(void);
  90. at_server_t server = at_get_server();
  91. rt_base_t int_lvl;
  92. static rt_device_t device_bak;
  93. static rt_err_t (*getchar_bak)(struct at_server *server, char *ch, rt_int32_t timeout);
  94. static char endmark_back[AT_END_MARK_LEN];
  95. /* backup server device and getchar function */
  96. {
  97. int_lvl = rt_hw_interrupt_disable();
  98. device_bak = server->device;
  99. getchar_bak = server->get_char;
  100. memset(endmark_back, 0x00, AT_END_MARK_LEN);
  101. memcpy(endmark_back, server->end_mark, strlen(server->end_mark));
  102. /* setup server device as console device */
  103. server->device = rt_console_get_device();
  104. server->get_char = at_server_console_getchar;
  105. memset(server->end_mark, 0x00, AT_END_MARK_LEN);
  106. server->end_mark[0] = '\r';
  107. rt_hw_interrupt_enable(int_lvl);
  108. }
  109. if (server)
  110. {
  111. rt_kprintf("======== Welcome to using RT-Thread AT command server cli ========\n");
  112. rt_kprintf("Input your at command for test server. Press 'ESC' to exit.\n");
  113. server->parser_entry(server);
  114. }
  115. else
  116. {
  117. rt_kprintf("AT client not initialized\n");
  118. }
  119. /* restore server device and getchar function */
  120. {
  121. int_lvl = rt_hw_interrupt_disable();
  122. server->device = device_bak;
  123. server->get_char = getchar_bak;
  124. memset(server->end_mark, 0x00, AT_END_MARK_LEN);
  125. memcpy(server->end_mark, endmark_back, strlen(endmark_back));
  126. rt_hw_interrupt_enable(int_lvl);
  127. }
  128. }
  129. #endif /* AT_USING_SERVER */
  130. #ifdef AT_USING_CLIENT
  131. static char client_getchar(void)
  132. {
  133. char ch;
  134. rt_sem_take(&client_rx_notice, RT_WAITING_FOREVER);
  135. rt_ringbuffer_getchar(client_rx_fifo, (rt_uint8_t *)&ch);
  136. return ch;
  137. }
  138. static void at_client_entry(void *param)
  139. {
  140. char ch;
  141. while(1)
  142. {
  143. ch = client_getchar();
  144. rt_kprintf("%c", ch);
  145. }
  146. }
  147. static rt_err_t client_getchar_rx_ind(rt_device_t dev, rt_size_t size)
  148. {
  149. uint8_t ch;
  150. rt_size_t i;
  151. for (i = 0; i < size; i++)
  152. {
  153. /* read a char */
  154. if (rt_device_read(dev, 0, &ch, 1))
  155. {
  156. rt_ringbuffer_put_force(client_rx_fifo, &ch, 1);
  157. rt_sem_release(&client_rx_notice);
  158. }
  159. }
  160. return RT_EOK;
  161. }
  162. static void client_cli_parser(at_client_t client)
  163. {
  164. #define ESC_KEY 0x1B
  165. #define BACKSPACE_KEY 0x08
  166. #define DELECT_KEY 0x7F
  167. char ch;
  168. char cur_line[FINSH_CMD_SIZE] = { 0 };
  169. rt_size_t cur_line_len = 0;
  170. static rt_err_t (*client_odev_rx_ind)(rt_device_t dev, rt_size_t size) = RT_NULL;
  171. rt_base_t int_lvl;
  172. rt_thread_t at_client;
  173. at_status_t client_odev_status;
  174. if (client)
  175. {
  176. /* backup client status */
  177. {
  178. client_odev_status = client->status;
  179. client->status = AT_STATUS_CLI;
  180. }
  181. /* backup client device RX indicate */
  182. {
  183. int_lvl = rt_hw_interrupt_disable();
  184. client_odev_rx_ind = client->device->rx_indicate;
  185. rt_device_set_rx_indicate(client->device, client_getchar_rx_ind);
  186. rt_hw_interrupt_enable(int_lvl);
  187. }
  188. rt_sem_init(&client_rx_notice, "cli_r", 0, RT_IPC_FLAG_FIFO);
  189. client_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);
  190. at_client = rt_thread_create("at_cli", at_client_entry, RT_NULL, 512, 8, 8);
  191. if (client_rx_fifo && at_client)
  192. {
  193. rt_kprintf("======== Welcome to using RT-Thread AT command client cli ========\n");
  194. rt_kprintf("Cli will forward your command to server port(%s). Press 'ESC' to exit.\n", client->device->parent.name);
  195. rt_thread_startup(at_client);
  196. /* process user input */
  197. while (ESC_KEY != (ch = console_getchar()))
  198. {
  199. if (ch == BACKSPACE_KEY || ch == DELECT_KEY)
  200. {
  201. if (cur_line_len)
  202. {
  203. cur_line[--cur_line_len] = 0;
  204. rt_kprintf("\b \b");
  205. }
  206. continue;
  207. }
  208. else if (ch == '\r' || ch == '\n')
  209. {
  210. /* execute a AT request */
  211. if (cur_line_len)
  212. {
  213. rt_kprintf("\n");
  214. at_obj_exec_cmd(client, RT_NULL, "%.*s", cur_line_len, cur_line);
  215. }
  216. cur_line_len = 0;
  217. }
  218. else
  219. {
  220. rt_kprintf("%c", ch);
  221. cur_line[cur_line_len++] = ch;
  222. }
  223. }
  224. /* restore client status */
  225. client->status = client_odev_status;
  226. /* restore client device RX indicate */
  227. {
  228. int_lvl = rt_hw_interrupt_disable();
  229. rt_device_set_rx_indicate(client->device, client_odev_rx_ind);
  230. rt_hw_interrupt_enable(int_lvl);
  231. }
  232. rt_thread_delete(at_client);
  233. rt_sem_detach(&client_rx_notice);
  234. rt_ringbuffer_destroy(client_rx_fifo);
  235. }
  236. else
  237. {
  238. rt_kprintf("No mem for AT cli client\n");
  239. }
  240. }
  241. else
  242. {
  243. rt_kprintf("AT client not initialized\n");
  244. }
  245. }
  246. #endif /* AT_USING_CLIENT */
  247. static void at(int argc, char **argv)
  248. {
  249. if (argc != 2 && argc != 3)
  250. {
  251. rt_kprintf("Please input '<server|client [dev_name]>' \n");
  252. return;
  253. }
  254. at_cli_init();
  255. if (!strcmp(argv[1], "server"))
  256. {
  257. #ifdef AT_USING_SERVER
  258. server_cli_parser();
  259. #else
  260. rt_kprintf("Not support AT server, please check your configure!\n");
  261. #endif /* AT_USING_SERVER */
  262. }
  263. else if (!strcmp(argv[1], "client"))
  264. {
  265. #ifdef AT_USING_CLIENT
  266. at_client_t client = RT_NULL;
  267. if (argc == 2)
  268. {
  269. client_cli_parser(at_client_get_first());
  270. }
  271. else if (argc == 3)
  272. {
  273. client = at_client_get(argv[2]);
  274. if (client == RT_NULL)
  275. {
  276. rt_kprintf("input AT client device name(%s) error.\n", argv[2]);
  277. }
  278. else
  279. {
  280. client_cli_parser(client);
  281. }
  282. }
  283. #else
  284. rt_kprintf("Not support AT client, please check your configure!\n");
  285. #endif /* AT_USING_CLIENT */
  286. }
  287. else
  288. {
  289. rt_kprintf("Please input '<server|client [dev_name]>' \n");
  290. }
  291. at_cli_deinit();
  292. }
  293. MSH_CMD_EXPORT(at, RT-Thread AT component cli: at <server|client [dev_name]>);
  294. #endif /* AT_USING_CLI */