at_cli.c 8.8 KB

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