at_cli.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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, "cli_c", 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. int_lvl = rt_hw_interrupt_disable();
  85. console = rt_console_get_device();
  86. if (console && odev_rx_ind)
  87. {
  88. /* restore RX indicate */
  89. rt_device_set_rx_indicate(console, odev_rx_ind);
  90. }
  91. rt_hw_interrupt_enable(int_lvl);
  92. rt_sem_detach(&console_rx_notice);
  93. rt_ringbuffer_destroy(console_rx_fifo);
  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(at_client_t client)
  172. {
  173. #define ESC_KEY 0x1B
  174. #define BACKSPACE_KEY 0x08
  175. #define DELECT_KEY 0x7F
  176. char ch;
  177. char cur_line[FINSH_CMD_SIZE] = { 0 };
  178. rt_size_t cur_line_len = 0;
  179. static rt_err_t (*client_odev_rx_ind)(rt_device_t dev, rt_size_t size) = RT_NULL;
  180. rt_base_t int_lvl;
  181. rt_thread_t at_client;
  182. if (client)
  183. {
  184. /* backup client device RX indicate */
  185. {
  186. int_lvl = rt_hw_interrupt_disable();
  187. client_odev_rx_ind = client->device->rx_indicate;
  188. rt_device_set_rx_indicate(client->device, client_getchar_rx_ind);
  189. rt_hw_interrupt_enable(int_lvl);
  190. }
  191. rt_sem_init(&client_rx_notice, "cli_r", 0, RT_IPC_FLAG_FIFO);
  192. client_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);
  193. at_client = rt_thread_create("at_cli", at_client_entry, RT_NULL, 512, 8, 8);
  194. if (client_rx_fifo && at_client)
  195. {
  196. rt_kprintf("======== Welcome to using RT-Thread AT command client cli ========\n");
  197. rt_kprintf("Cli will forward your command to server port(%s). Press 'ESC' to exit.\n", client->device->parent.name);
  198. rt_thread_startup(at_client);
  199. /* process user input */
  200. while (ESC_KEY != (ch = console_getchar()))
  201. {
  202. if (ch == BACKSPACE_KEY || ch == DELECT_KEY)
  203. {
  204. if (cur_line_len)
  205. {
  206. cur_line[--cur_line_len] = 0;
  207. rt_kprintf("\b \b");
  208. }
  209. continue;
  210. }
  211. else if (ch == '\r' || ch == '\n')
  212. {
  213. /* execute a AT request */
  214. if (cur_line_len)
  215. {
  216. rt_kprintf("\n");
  217. at_obj_exec_cmd(client, RT_NULL, "%.*s", cur_line_len, cur_line);
  218. }
  219. cur_line_len = 0;
  220. }
  221. else
  222. {
  223. rt_kprintf("%c", ch);
  224. cur_line[cur_line_len++] = ch;
  225. }
  226. }
  227. /* restore client device RX indicate */
  228. {
  229. int_lvl = rt_hw_interrupt_disable();
  230. rt_device_set_rx_indicate(client->device, client_odev_rx_ind);
  231. rt_hw_interrupt_enable(int_lvl);
  232. }
  233. rt_thread_delete(at_client);
  234. rt_sem_detach(&client_rx_notice);
  235. rt_ringbuffer_destroy(client_rx_fifo);
  236. }
  237. else
  238. {
  239. rt_kprintf("No mem for AT cli client\n");
  240. }
  241. }
  242. else
  243. {
  244. rt_kprintf("AT client not initialized\n");
  245. }
  246. }
  247. #endif /* AT_USING_CLIENT */
  248. static void at(int argc, char **argv)
  249. {
  250. if (argc != 2 && argc != 3)
  251. {
  252. rt_kprintf("Please input '<server|client [dev_name]>' \n");
  253. return;
  254. }
  255. at_cli_init();
  256. if (!strcmp(argv[1], "server"))
  257. {
  258. #ifdef AT_USING_SERVER
  259. server_cli_parser();
  260. #else
  261. rt_kprintf("Not support AT server, please check your configure!\n");
  262. #endif /* AT_USING_SERVER */
  263. }
  264. else if (!strcmp(argv[1], "client"))
  265. {
  266. #ifdef AT_USING_CLIENT
  267. at_client_t client = RT_NULL;
  268. if (argc == 2)
  269. {
  270. client_cli_parser(at_client_get_first());
  271. }
  272. else if (argc == 3)
  273. {
  274. client = at_client_get(argv[2]);
  275. if (client == RT_NULL)
  276. {
  277. rt_kprintf("input AT client device name(%s) error.\n", argv[2]);
  278. }
  279. else
  280. {
  281. client_cli_parser(client);
  282. }
  283. }
  284. #else
  285. rt_kprintf("Not support AT client, please check your configure!\n");
  286. #endif /* AT_USING_CLIENT */
  287. }
  288. else
  289. {
  290. rt_kprintf("Please input '<server|client [dev_name]>' \n");
  291. }
  292. at_cli_deinit();
  293. }
  294. MSH_CMD_EXPORT(at, RT-Thread AT component cli: at <server|client [dev_name]>);
  295. #endif /* AT_USING_CLI */