at_server.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-03-30 chenyong first version
  9. * 2018-04-14 chenyong modify parse arguments
  10. */
  11. #include <at.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <rthw.h>
  16. #define LOG_TAG "at.svr"
  17. #include <at_log.h>
  18. #ifdef AT_USING_SERVER
  19. #define AT_CMD_CHAR_0 '0'
  20. #define AT_CMD_CHAR_9 '9'
  21. #define AT_CMD_QUESTION_MARK '?'
  22. #define AT_CMD_EQUAL_MARK '='
  23. #define AT_CMD_L_SQ_BRACKET '['
  24. #define AT_CMD_R_SQ_BRACKET ']'
  25. #define AT_CMD_L_ANGLE_BRACKET '<'
  26. #define AT_CMD_R_ANGLE_BRACKET '>'
  27. #define AT_CMD_COMMA_MARK ','
  28. #define AT_CMD_SEMICOLON ';'
  29. #define AT_CMD_CR '\r'
  30. #define AT_CMD_LF '\n'
  31. static at_server_t at_server_local = RT_NULL;
  32. static at_cmd_t cmd_table = RT_NULL;
  33. static rt_size_t cmd_num;
  34. extern void at_vprintf(rt_device_t device, const char *format, va_list args);
  35. extern void at_vprintfln(rt_device_t device, const char *format, va_list args);
  36. /**
  37. * AT server send data to AT device
  38. *
  39. * @param format the input format
  40. */
  41. void at_server_printf(const char *format, ...)
  42. {
  43. va_list args;
  44. va_start(args, format);
  45. at_vprintf(at_server_local->device, format, args);
  46. va_end(args);
  47. }
  48. /**
  49. * AT server send data and newline to AT device
  50. *
  51. * @param format the input format
  52. */
  53. void at_server_printfln(const char *format, ...)
  54. {
  55. va_list args;
  56. va_start(args, format);
  57. at_vprintfln(at_server_local->device, format, args);
  58. va_end(args);
  59. }
  60. /**
  61. * AT server request arguments parse arguments
  62. *
  63. * @param req_args request arguments
  64. * @param req_expr request expression
  65. *
  66. * @return -1 : parse arguments failed
  67. * 0 : parse without match
  68. * >0 : The number of arguments successfully parsed
  69. */
  70. int at_req_parse_args(const char *req_args, const char *req_expr, ...)
  71. {
  72. va_list args;
  73. int req_args_num = 0;
  74. RT_ASSERT(req_args);
  75. RT_ASSERT(req_expr);
  76. va_start(args, req_expr);
  77. req_args_num = vsscanf(req_args, req_expr, args);
  78. va_end(args);
  79. return req_args_num;
  80. }
  81. /**
  82. * AT server send command execute result to AT device
  83. *
  84. * @param result AT command execute result
  85. */
  86. void at_server_print_result(at_result_t result)
  87. {
  88. switch (result)
  89. {
  90. case AT_RESULT_OK:
  91. at_server_printfln("");
  92. at_server_printfln("OK");
  93. break;
  94. case AT_RESULT_FAILE:
  95. at_server_printfln("");
  96. at_server_printfln("ERROR");
  97. break;
  98. case AT_RESULT_NULL:
  99. break;
  100. case AT_RESULT_CMD_ERR:
  101. at_server_printfln("ERR CMD MATCH FAILED!");
  102. at_server_print_result(AT_RESULT_FAILE);
  103. break;
  104. case AT_RESULT_CHECK_FAILE:
  105. at_server_printfln("ERR CHECK ARGS FORMAT FAILED!");
  106. at_server_print_result(AT_RESULT_FAILE);
  107. break;
  108. case AT_RESULT_PARSE_FAILE:
  109. at_server_printfln("ERR PARSE ARGS FAILED!");
  110. at_server_print_result(AT_RESULT_FAILE);
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. /**
  117. * AT server print all commands to AT device
  118. */
  119. void rt_at_server_print_all_cmd(void)
  120. {
  121. rt_size_t i = 0;
  122. at_server_printfln("Commands list : ");
  123. for (i = 0; i < cmd_num; i++)
  124. {
  125. at_server_printf("%s", cmd_table[i].name);
  126. if (cmd_table[i].args_expr)
  127. {
  128. at_server_printfln("%s", cmd_table[i].args_expr);
  129. }
  130. else
  131. {
  132. at_server_printf("%c%c", AT_CMD_CR, AT_CMD_LF);
  133. }
  134. }
  135. }
  136. at_server_t at_get_server(void)
  137. {
  138. RT_ASSERT(at_server_local);
  139. RT_ASSERT(at_server_local->status != AT_STATUS_UNINITIALIZED);
  140. return at_server_local;
  141. }
  142. static rt_err_t at_check_args(const char *args, const char *args_format)
  143. {
  144. rt_size_t left_sq_bracket_num = 0, right_sq_bracket_num = 0;
  145. rt_size_t left_angle_bracket_num = 0, right_angle_bracket_num = 0;
  146. rt_size_t comma_mark_num = 0;
  147. rt_size_t i = 0;
  148. RT_ASSERT(args);
  149. RT_ASSERT(args_format);
  150. for (i = 0; i < strlen(args_format); i++)
  151. {
  152. switch (args_format[i])
  153. {
  154. case AT_CMD_L_SQ_BRACKET:
  155. left_sq_bracket_num++;
  156. break;
  157. case AT_CMD_R_SQ_BRACKET:
  158. right_sq_bracket_num++;
  159. break;
  160. case AT_CMD_L_ANGLE_BRACKET:
  161. left_angle_bracket_num++;
  162. break;
  163. case AT_CMD_R_ANGLE_BRACKET:
  164. right_angle_bracket_num++;
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. if (left_sq_bracket_num != right_sq_bracket_num || left_angle_bracket_num != right_angle_bracket_num
  171. || left_sq_bracket_num > left_angle_bracket_num)
  172. {
  173. return -RT_ERROR;
  174. }
  175. for (i = 0; i < strlen(args); i++)
  176. {
  177. if (args[i] == AT_CMD_COMMA_MARK)
  178. {
  179. comma_mark_num++;
  180. }
  181. }
  182. if ((comma_mark_num + 1 < left_angle_bracket_num - left_sq_bracket_num)
  183. || comma_mark_num + 1 > left_angle_bracket_num)
  184. {
  185. return -RT_ERROR;
  186. }
  187. return RT_EOK;
  188. }
  189. static rt_err_t at_cmd_process(at_cmd_t cmd, const char *cmd_args)
  190. {
  191. at_result_t result = AT_RESULT_OK;
  192. RT_ASSERT(cmd);
  193. RT_ASSERT(cmd_args);
  194. if (cmd_args[0] == AT_CMD_EQUAL_MARK && cmd_args[1] == AT_CMD_QUESTION_MARK && cmd_args[2] == AT_CMD_CR)
  195. {
  196. if (cmd->test == RT_NULL)
  197. {
  198. at_server_print_result(AT_RESULT_CMD_ERR);
  199. return -RT_ERROR;
  200. }
  201. result = cmd->test();
  202. at_server_print_result(result);
  203. }
  204. else if (cmd_args[0] == AT_CMD_QUESTION_MARK && cmd_args[1] == AT_CMD_CR)
  205. {
  206. if (cmd->query == RT_NULL)
  207. {
  208. at_server_print_result(AT_RESULT_CMD_ERR);
  209. return -RT_ERROR;
  210. }
  211. result = cmd->query();
  212. at_server_print_result(result);
  213. }
  214. else if (cmd_args[0] == AT_CMD_EQUAL_MARK
  215. || (cmd_args[0] >= AT_CMD_CHAR_0 && cmd_args[0] <= AT_CMD_CHAR_9 && cmd_args[1] == AT_CMD_CR))
  216. {
  217. if (cmd->setup == RT_NULL)
  218. {
  219. at_server_print_result(AT_RESULT_CMD_ERR);
  220. return -RT_ERROR;
  221. }
  222. if(at_check_args(cmd_args, cmd->args_expr) < 0)
  223. {
  224. at_server_print_result(AT_RESULT_CHECK_FAILE);
  225. return -RT_ERROR;
  226. }
  227. result = cmd->setup(cmd_args);
  228. at_server_print_result(result);
  229. }
  230. else if (cmd_args[0] == AT_CMD_CR)
  231. {
  232. if (cmd->exec == RT_NULL)
  233. {
  234. at_server_print_result(AT_RESULT_CMD_ERR);
  235. return -RT_ERROR;
  236. }
  237. result = cmd->exec();
  238. at_server_print_result(result);
  239. }
  240. else
  241. {
  242. return -RT_ERROR;
  243. }
  244. return RT_EOK;
  245. }
  246. static at_cmd_t at_find_cmd(const char *cmd)
  247. {
  248. rt_size_t i = 0;
  249. RT_ASSERT(cmd_table);
  250. for (i = 0; i < cmd_num; i++)
  251. {
  252. if (!strcasecmp(cmd, cmd_table[i].name))
  253. {
  254. return &cmd_table[i];
  255. }
  256. }
  257. return RT_NULL;
  258. }
  259. static rt_err_t at_cmd_get_name(const char *cmd_buffer, char *cmd_name)
  260. {
  261. rt_size_t cmd_name_len = 0, i = 0;
  262. RT_ASSERT(cmd_name);
  263. RT_ASSERT(cmd_buffer);
  264. for (i = 0; i < strlen(cmd_buffer) + 1; i++)
  265. {
  266. if (*(cmd_buffer + i) == AT_CMD_QUESTION_MARK || *(cmd_buffer + i) == AT_CMD_EQUAL_MARK
  267. || *(cmd_buffer + i) == AT_CMD_CR
  268. || (*(cmd_buffer + i) >= AT_CMD_CHAR_0 && *(cmd_buffer + i) <= AT_CMD_CHAR_9))
  269. {
  270. cmd_name_len = i;
  271. memcpy(cmd_name, cmd_buffer, cmd_name_len);
  272. *(cmd_name + cmd_name_len) = '\0';
  273. return RT_EOK;
  274. }
  275. }
  276. return -RT_ERROR;
  277. }
  278. static char at_server_gerchar(void)
  279. {
  280. char ch;
  281. while (rt_device_read(at_server_local->device, 0, &ch, 1) == 0)
  282. {
  283. rt_sem_control(at_server_local->rx_notice, RT_IPC_CMD_RESET, RT_NULL);
  284. rt_sem_take(at_server_local->rx_notice, RT_WAITING_FOREVER);
  285. }
  286. return ch;
  287. }
  288. static void server_parser(at_server_t server)
  289. {
  290. #define ESC_KEY 0x1B
  291. #define BACKSPACE_KEY 0x08
  292. #define DELECT_KEY 0x7F
  293. char cur_cmd_name[AT_CMD_NAME_LEN] = { 0 };
  294. at_cmd_t cur_cmd = RT_NULL;
  295. char *cur_cmd_args = RT_NULL, ch, last_ch;
  296. RT_ASSERT(server);
  297. RT_ASSERT(server->status != AT_STATUS_UNINITIALIZED);
  298. while (ESC_KEY != (ch = server->get_char()))
  299. {
  300. if (server->echo_mode)
  301. {
  302. if (ch == AT_CMD_CR || (ch == AT_CMD_LF && last_ch != AT_CMD_CR))
  303. {
  304. at_server_printf("%c%c", AT_CMD_CR, AT_CMD_LF);
  305. }
  306. else if (ch == BACKSPACE_KEY || ch == DELECT_KEY)
  307. {
  308. if (server->cur_recv_len)
  309. {
  310. server->recv_buffer[--server->cur_recv_len] = 0;
  311. at_server_printf("\b \b");
  312. }
  313. continue;
  314. }
  315. else
  316. {
  317. at_server_printf("%c", ch);
  318. }
  319. }
  320. server->recv_buffer[server->cur_recv_len++] = ch;
  321. last_ch = ch;
  322. if(!strstr(server->recv_buffer, server->end_mark))
  323. {
  324. continue;
  325. }
  326. if (at_cmd_get_name(server->recv_buffer, cur_cmd_name) < 0)
  327. {
  328. at_server_print_result(AT_RESULT_CMD_ERR);
  329. goto __retry;
  330. }
  331. cur_cmd = at_find_cmd(cur_cmd_name);
  332. if (!cur_cmd)
  333. {
  334. at_server_print_result(AT_RESULT_CMD_ERR);
  335. goto __retry;
  336. }
  337. cur_cmd_args = server->recv_buffer + strlen(cur_cmd_name);
  338. if (at_cmd_process(cur_cmd, cur_cmd_args) < 0)
  339. {
  340. goto __retry;
  341. }
  342. __retry:
  343. memset(server->recv_buffer, 0x00, AT_SERVER_RECV_BUFF_LEN);
  344. server->cur_recv_len = 0;
  345. }
  346. }
  347. static rt_err_t at_rx_ind(rt_device_t dev, rt_size_t size)
  348. {
  349. if (size > 0)
  350. {
  351. rt_sem_release(at_server_local->rx_notice);
  352. }
  353. return RT_EOK;
  354. }
  355. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  356. #pragma section="RtAtCmdTab"
  357. #endif
  358. int at_server_init(void)
  359. {
  360. rt_err_t result = RT_EOK;
  361. rt_err_t open_result = RT_EOK;
  362. if (at_server_local)
  363. {
  364. return result;
  365. }
  366. /* initialize the AT commands table.*/
  367. #if defined(__CC_ARM) /* ARM C Compiler */
  368. extern const int RtAtCmdTab$$Base;
  369. extern const int RtAtCmdTab$$Limit;
  370. cmd_table = (at_cmd_t)&RtAtCmdTab$$Base;
  371. cmd_num = (at_cmd_t)&RtAtCmdTab$$Limit - cmd_table;
  372. #elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
  373. cmd_table = (at_cmd_t)__section_begin("RtAtCmdTab");
  374. cmd_num = (at_cmd_t)__section_end("RtAtCmdTab") - cmd_table;
  375. #elif defined (__GNUC__) /* for GCC Compiler */
  376. extern const int __rtatcmdtab_start;
  377. extern const int __rtatcmdtab_end;
  378. cmd_table = (at_cmd_t)&__rtatcmdtab_start;
  379. cmd_num = (at_cmd_t) &__rtatcmdtab_end - cmd_table;
  380. #endif /* defined(__CC_ARM) */
  381. at_server_local = (at_server_t) rt_calloc(1, sizeof(struct at_server));
  382. if (!at_server_local)
  383. {
  384. result = -RT_ENOMEM;
  385. LOG_E("AT server session initialize failed! No memory for at_server structure !");
  386. goto __exit;
  387. }
  388. at_server_local->echo_mode = 1;
  389. at_server_local->status = AT_STATUS_UNINITIALIZED;
  390. memset(at_server_local->recv_buffer, 0x00, AT_SERVER_RECV_BUFF_LEN);
  391. at_server_local->cur_recv_len = 0;
  392. at_server_local->rx_notice = rt_sem_create("at_svr", 0, RT_IPC_FLAG_FIFO);
  393. if (!at_server_local->rx_notice)
  394. {
  395. LOG_E("AT server session initialize failed! at_rx_notice semaphore create failed!");
  396. result = -RT_ENOMEM;
  397. goto __exit;
  398. }
  399. /* Find and open command device */
  400. at_server_local->device = rt_device_find(AT_SERVER_DEVICE);
  401. if (at_server_local->device)
  402. {
  403. RT_ASSERT(at_server_local->device->type == RT_Device_Class_Char);
  404. /* using DMA mode first */
  405. open_result = rt_device_open(at_server_local->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX);
  406. /* using interrupt mode when DMA mode not supported */
  407. if (open_result == -RT_EIO)
  408. {
  409. open_result = rt_device_open(at_server_local->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  410. }
  411. RT_ASSERT(open_result == RT_EOK);
  412. rt_device_set_rx_indicate(at_server_local->device, at_rx_ind);
  413. }
  414. else
  415. {
  416. LOG_E("AT device initialize failed! Not find the device : %s.", AT_SERVER_DEVICE);
  417. result = -RT_ERROR;
  418. goto __exit;
  419. }
  420. at_server_local->get_char = at_server_gerchar;
  421. memcpy(at_server_local->end_mark, AT_CMD_END_MARK, sizeof(AT_CMD_END_MARK));
  422. at_server_local->parser_entry = server_parser;
  423. at_server_local->parser = rt_thread_create("at_svr",
  424. (void (*)(void *parameter))server_parser,
  425. at_server_local,
  426. 2 * 1024,
  427. RT_THREAD_PRIORITY_MAX / 3 - 1,
  428. 5);
  429. if (at_server_local->parser == RT_NULL)
  430. {
  431. result = -RT_ENOMEM;
  432. goto __exit;
  433. }
  434. __exit:
  435. if (!result)
  436. {
  437. at_server_local->status = AT_STATUS_INITIALIZED;
  438. rt_thread_startup(at_server_local->parser);
  439. LOG_I("RT-Thread AT server (V%s) initialize success.", AT_SW_VERSION);
  440. }
  441. else
  442. {
  443. if (at_server_local)
  444. {
  445. rt_free(at_server_local);
  446. }
  447. LOG_E("RT-Thread AT server (V%s) initialize failed(%d).", AT_SW_VERSION, result);
  448. }
  449. return result;
  450. }
  451. INIT_COMPONENT_EXPORT(at_server_init);
  452. RT_WEAK void at_port_reset(void)
  453. {
  454. LOG_E("The reset for AT server is not implement.");
  455. }
  456. RT_WEAK void at_port_factory_reset(void)
  457. {
  458. LOG_E("The factory reset for AT server is not implement.");
  459. }
  460. #endif /* AT_USING_SERVER */