at_cli.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. static char endmark_back[AT_END_MARK_LEN];
  96. /* backup server device and getchar function */
  97. {
  98. level = rt_hw_interrupt_disable();
  99. device_bak = server->device;
  100. getchar_bak = server->get_char;
  101. rt_memset(endmark_back, 0x00, AT_END_MARK_LEN);
  102. rt_memcpy(endmark_back, server->end_mark, strlen(server->end_mark));
  103. /* setup server device as console device */
  104. server->device = rt_console_get_device();
  105. server->get_char = at_server_console_getchar;
  106. rt_memset(server->end_mark, 0x00, AT_END_MARK_LEN);
  107. server->end_mark[0] = '\r';
  108. rt_hw_interrupt_enable(level);
  109. }
  110. if (server)
  111. {
  112. rt_kprintf("======== Welcome to using RT-Thread AT command server cli ========\n");
  113. rt_kprintf("Input your at command for test server. Press 'ESC' to exit.\n");
  114. server->parser_entry(server);
  115. }
  116. else
  117. {
  118. rt_kprintf("AT client not initialized\n");
  119. }
  120. /* restore server device and getchar function */
  121. {
  122. level = rt_hw_interrupt_disable();
  123. server->device = device_bak;
  124. server->get_char = getchar_bak;
  125. rt_memset(server->end_mark, 0x00, AT_END_MARK_LEN);
  126. rt_memcpy(server->end_mark, endmark_back, strlen(endmark_back));
  127. rt_hw_interrupt_enable(level);
  128. }
  129. }
  130. #endif /* AT_USING_SERVER */
  131. #ifdef AT_USING_CLIENT
  132. static char client_getchar(void)
  133. {
  134. char ch;
  135. rt_sem_take(&client_rx_notice, RT_WAITING_FOREVER);
  136. rt_ringbuffer_getchar(client_rx_fifo, (rt_uint8_t *)&ch);
  137. return ch;
  138. }
  139. static void at_client_entry(void *param)
  140. {
  141. char ch;
  142. while(1)
  143. {
  144. ch = client_getchar();
  145. rt_kprintf("%c", ch);
  146. }
  147. }
  148. static rt_err_t client_getchar_rx_ind(rt_device_t dev, rt_size_t size)
  149. {
  150. uint8_t ch;
  151. rt_size_t i;
  152. for (i = 0; i < size; i++)
  153. {
  154. /* read a char */
  155. if (rt_device_read(dev, 0, &ch, 1))
  156. {
  157. rt_ringbuffer_put_force(client_rx_fifo, &ch, 1);
  158. rt_sem_release(&client_rx_notice);
  159. }
  160. }
  161. return RT_EOK;
  162. }
  163. static void client_cli_parser(at_client_t client)
  164. {
  165. #define ESC_KEY 0x1B
  166. #define BACKSPACE_KEY 0x08
  167. #define DELECT_KEY 0x7F
  168. char ch;
  169. char cur_line[FINSH_CMD_SIZE] = { 0 };
  170. rt_size_t cur_line_len = 0;
  171. static rt_err_t (*client_odev_rx_ind)(rt_device_t dev, rt_size_t size) = RT_NULL;
  172. rt_base_t level;
  173. rt_thread_t at_client;
  174. at_status_t client_odev_status;
  175. if (client)
  176. {
  177. /* backup client status */
  178. {
  179. client_odev_status = client->status;
  180. client->status = AT_STATUS_CLI;
  181. }
  182. /* backup client device RX indicate */
  183. {
  184. level = rt_hw_interrupt_disable();
  185. client_odev_rx_ind = client->device->rx_indicate;
  186. rt_device_set_rx_indicate(client->device, client_getchar_rx_ind);
  187. rt_hw_interrupt_enable(level);
  188. }
  189. rt_sem_init(&client_rx_notice, "cli_r", 0, RT_IPC_FLAG_FIFO);
  190. client_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);
  191. at_client = rt_thread_create("at_cli", at_client_entry, RT_NULL, 512, 8, 8);
  192. if (client_rx_fifo && at_client)
  193. {
  194. rt_kprintf("======== Welcome to using RT-Thread AT command client cli ========\n");
  195. rt_kprintf("Cli will forward your command to server port(%s). Press 'ESC' to exit.\n", client->device->parent.name);
  196. rt_thread_startup(at_client);
  197. /* process user input */
  198. while (ESC_KEY != (ch = console_getchar()))
  199. {
  200. if (ch == BACKSPACE_KEY || ch == DELECT_KEY)
  201. {
  202. if (cur_line_len)
  203. {
  204. cur_line[--cur_line_len] = 0;
  205. rt_kprintf("\b \b");
  206. }
  207. continue;
  208. }
  209. else if (ch == '\r' || ch == '\n')
  210. {
  211. /* execute a AT request */
  212. if (cur_line_len)
  213. {
  214. rt_kprintf("\n");
  215. at_obj_exec_cmd(client, RT_NULL, "%.*s", cur_line_len, cur_line);
  216. }
  217. cur_line_len = 0;
  218. }
  219. else
  220. {
  221. if(cur_line_len >= FINSH_CMD_SIZE)
  222. {
  223. continue;
  224. }
  225. rt_kprintf("%c", ch);
  226. cur_line[cur_line_len++] = ch;
  227. }
  228. }
  229. /* restore client status */
  230. client->status = client_odev_status;
  231. /* restore client device RX indicate */
  232. {
  233. level = rt_hw_interrupt_disable();
  234. rt_device_set_rx_indicate(client->device, client_odev_rx_ind);
  235. rt_hw_interrupt_enable(level);
  236. }
  237. rt_thread_delete(at_client);
  238. rt_sem_detach(&client_rx_notice);
  239. rt_ringbuffer_destroy(client_rx_fifo);
  240. }
  241. else
  242. {
  243. rt_kprintf("No mem for AT cli client\n");
  244. }
  245. }
  246. else
  247. {
  248. rt_kprintf("AT client not initialized\n");
  249. }
  250. }
  251. #endif /* AT_USING_CLIENT */
  252. static void at(int argc, char **argv)
  253. {
  254. if (argc != 2 && argc != 3)
  255. {
  256. rt_kprintf("Please input '<server|client [dev_name]>' \n");
  257. return;
  258. }
  259. at_cli_init();
  260. if (!strcmp(argv[1], "server"))
  261. {
  262. #ifdef AT_USING_SERVER
  263. server_cli_parser();
  264. #else
  265. rt_kprintf("Not support AT server, please check your configure!\n");
  266. #endif /* AT_USING_SERVER */
  267. }
  268. else if (!strcmp(argv[1], "client"))
  269. {
  270. #ifdef AT_USING_CLIENT
  271. at_client_t client = RT_NULL;
  272. if (argc == 2)
  273. {
  274. client_cli_parser(at_client_get_first());
  275. }
  276. else if (argc == 3)
  277. {
  278. client = at_client_get(argv[2]);
  279. if (client == RT_NULL)
  280. {
  281. rt_kprintf("input AT client device name(%s) error.\n", argv[2]);
  282. }
  283. else
  284. {
  285. client_cli_parser(client);
  286. }
  287. }
  288. #else
  289. rt_kprintf("Not support AT client, please check your configure!\n");
  290. #endif /* AT_USING_CLIENT */
  291. }
  292. else
  293. {
  294. rt_kprintf("Please input '<server|client [dev_name]>' \n");
  295. }
  296. at_cli_deinit();
  297. }
  298. MSH_CMD_EXPORT(at, RT-Thread AT component cli: at <server|client [dev_name]>);
  299. #endif /* AT_USING_CLI */