at_cli.c 8.7 KB

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