at_client.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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-03-30 chenyong first version
  9. * 2018-04-12 chenyong add client implement
  10. * 2018-08-17 chenyong multiple client support
  11. * 2021-03-17 Meco Man fix a buf of leaking memory
  12. */
  13. #include <at.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #define LOG_TAG "at.clnt"
  18. #include <at_log.h>
  19. #ifdef AT_USING_CLIENT
  20. #define AT_RESP_END_OK "OK"
  21. #define AT_RESP_END_ERROR "ERROR"
  22. #define AT_RESP_END_FAIL "FAIL"
  23. #define AT_END_CR_LF "\r\n"
  24. static struct at_client at_client_table[AT_CLIENT_NUM_MAX] = { 0 };
  25. extern rt_size_t at_vprintfln(rt_device_t device, const char *format, va_list args);
  26. extern void at_print_raw_cmd(const char *type, const char *cmd, rt_size_t size);
  27. extern const char *at_get_last_cmd(rt_size_t *cmd_size);
  28. /**
  29. * Create response object.
  30. *
  31. * @param buf_size the maximum response buffer size
  32. * @param line_num the number of setting response lines
  33. * = 0: the response data will auto return when received 'OK' or 'ERROR'
  34. * != 0: the response data will return when received setting lines number data
  35. * @param timeout the maximum response time
  36. *
  37. * @return != RT_NULL: response object
  38. * = RT_NULL: no memory
  39. */
  40. at_response_t at_create_resp(rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout)
  41. {
  42. at_response_t resp = RT_NULL;
  43. resp = (at_response_t) rt_calloc(1, sizeof(struct at_response));
  44. if (resp == RT_NULL)
  45. {
  46. LOG_E("AT create response object failed! No memory for response object!");
  47. return RT_NULL;
  48. }
  49. resp->buf = (char *) rt_calloc(1, buf_size);
  50. if (resp->buf == RT_NULL)
  51. {
  52. LOG_E("AT create response object failed! No memory for response buffer!");
  53. rt_free(resp);
  54. return RT_NULL;
  55. }
  56. resp->buf_size = buf_size;
  57. resp->line_num = line_num;
  58. resp->line_counts = 0;
  59. resp->timeout = timeout;
  60. return resp;
  61. }
  62. /**
  63. * Delete and free response object.
  64. *
  65. * @param resp response object
  66. */
  67. void at_delete_resp(at_response_t resp)
  68. {
  69. if (resp && resp->buf)
  70. {
  71. rt_free(resp->buf);
  72. }
  73. if (resp)
  74. {
  75. rt_free(resp);
  76. resp = RT_NULL;
  77. }
  78. }
  79. /**
  80. * Set response object information
  81. *
  82. * @param resp response object
  83. * @param buf_size the maximum response buffer size
  84. * @param line_num the number of setting response lines
  85. * = 0: the response data will auto return when received 'OK' or 'ERROR'
  86. * != 0: the response data will return when received setting lines number data
  87. * @param timeout the maximum response time
  88. *
  89. * @return != RT_NULL: response object
  90. * = RT_NULL: no memory
  91. */
  92. at_response_t at_resp_set_info(at_response_t resp, rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout)
  93. {
  94. char *p_temp;
  95. RT_ASSERT(resp);
  96. if (resp->buf_size != buf_size)
  97. {
  98. resp->buf_size = buf_size;
  99. p_temp = (char *) rt_realloc(resp->buf, buf_size);
  100. if (p_temp == RT_NULL)
  101. {
  102. LOG_D("No memory for realloc response buffer size(%d).", buf_size);
  103. return RT_NULL;
  104. }
  105. else
  106. {
  107. resp->buf = p_temp;
  108. }
  109. }
  110. resp->line_num = line_num;
  111. resp->timeout = timeout;
  112. return resp;
  113. }
  114. /**
  115. * Get one line AT response buffer by line number.
  116. *
  117. * @param resp response object
  118. * @param resp_line line number, start from '1'
  119. *
  120. * @return != RT_NULL: response line buffer
  121. * = RT_NULL: input response line error
  122. */
  123. const char *at_resp_get_line(at_response_t resp, rt_size_t resp_line)
  124. {
  125. char *resp_buf = resp->buf;
  126. char *resp_line_buf = RT_NULL;
  127. rt_size_t line_num = 1;
  128. RT_ASSERT(resp);
  129. if (resp_line > resp->line_counts || resp_line <= 0)
  130. {
  131. LOG_E("AT response get line failed! Input response line(%d) error!", resp_line);
  132. return RT_NULL;
  133. }
  134. for (line_num = 1; line_num <= resp->line_counts; line_num++)
  135. {
  136. if (resp_line == line_num)
  137. {
  138. resp_line_buf = resp_buf;
  139. return resp_line_buf;
  140. }
  141. resp_buf += strlen(resp_buf) + 1;
  142. }
  143. return RT_NULL;
  144. }
  145. /**
  146. * Get one line AT response buffer by keyword
  147. *
  148. * @param resp response object
  149. * @param keyword query keyword
  150. *
  151. * @return != RT_NULL: response line buffer
  152. * = RT_NULL: no matching data
  153. */
  154. const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword)
  155. {
  156. char *resp_buf = resp->buf;
  157. char *resp_line_buf = RT_NULL;
  158. rt_size_t line_num = 1;
  159. RT_ASSERT(resp);
  160. RT_ASSERT(keyword);
  161. for (line_num = 1; line_num <= resp->line_counts; line_num++)
  162. {
  163. if (strstr(resp_buf, keyword))
  164. {
  165. resp_line_buf = resp_buf;
  166. return resp_line_buf;
  167. }
  168. resp_buf += strlen(resp_buf) + 1;
  169. }
  170. return RT_NULL;
  171. }
  172. /**
  173. * Get and parse AT response buffer arguments by line number.
  174. *
  175. * @param resp response object
  176. * @param resp_line line number, start from '1'
  177. * @param resp_expr response buffer expression
  178. *
  179. * @return -1 : input response line number error or get line buffer error
  180. * 0 : parsed without match
  181. * >0 : the number of arguments successfully parsed
  182. */
  183. int at_resp_parse_line_args(at_response_t resp, rt_size_t resp_line, const char *resp_expr, ...)
  184. {
  185. va_list args;
  186. int resp_args_num = 0;
  187. const char *resp_line_buf = RT_NULL;
  188. RT_ASSERT(resp);
  189. RT_ASSERT(resp_expr);
  190. if ((resp_line_buf = at_resp_get_line(resp, resp_line)) == RT_NULL)
  191. {
  192. return -1;
  193. }
  194. va_start(args, resp_expr);
  195. resp_args_num = vsscanf(resp_line_buf, resp_expr, args);
  196. va_end(args);
  197. return resp_args_num;
  198. }
  199. /**
  200. * Get and parse AT response buffer arguments by keyword.
  201. *
  202. * @param resp response object
  203. * @param keyword query keyword
  204. * @param resp_expr response buffer expression
  205. *
  206. * @return -1 : input keyword error or get line buffer error
  207. * 0 : parsed without match
  208. * >0 : the number of arguments successfully parsed
  209. */
  210. int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const char *resp_expr, ...)
  211. {
  212. va_list args;
  213. int resp_args_num = 0;
  214. const char *resp_line_buf = RT_NULL;
  215. RT_ASSERT(resp);
  216. RT_ASSERT(resp_expr);
  217. if ((resp_line_buf = at_resp_get_line_by_kw(resp, keyword)) == RT_NULL)
  218. {
  219. return -1;
  220. }
  221. va_start(args, resp_expr);
  222. resp_args_num = vsscanf(resp_line_buf, resp_expr, args);
  223. va_end(args);
  224. return resp_args_num;
  225. }
  226. /**
  227. * Send commands to AT server and wait response.
  228. *
  229. * @param client current AT client object
  230. * @param resp AT response object, using RT_NULL when you don't care response
  231. * @param cmd_expr AT commands expression
  232. *
  233. * @return 0 : success
  234. * -1 : response status error
  235. * -2 : wait timeout
  236. * -7 : enter AT CLI mode
  237. */
  238. int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr, ...)
  239. {
  240. va_list args;
  241. rt_size_t cmd_size = 0;
  242. rt_err_t result = RT_EOK;
  243. const char *cmd = RT_NULL;
  244. RT_ASSERT(cmd_expr);
  245. if (client == RT_NULL)
  246. {
  247. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  248. return -RT_ERROR;
  249. }
  250. /* check AT CLI mode */
  251. if (client->status == AT_STATUS_CLI && resp)
  252. {
  253. return -RT_EBUSY;
  254. }
  255. rt_mutex_take(client->lock, RT_WAITING_FOREVER);
  256. client->resp_status = AT_RESP_OK;
  257. client->resp = resp;
  258. if (resp != RT_NULL)
  259. {
  260. resp->buf_len = 0;
  261. resp->line_counts = 0;
  262. }
  263. va_start(args, cmd_expr);
  264. at_vprintfln(client->device, cmd_expr, args);
  265. va_end(args);
  266. if (resp != RT_NULL)
  267. {
  268. if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK)
  269. {
  270. cmd = at_get_last_cmd(&cmd_size);
  271. LOG_D("execute command (%.*s) timeout (%d ticks)!", cmd_size, cmd, resp->timeout);
  272. client->resp_status = AT_RESP_TIMEOUT;
  273. result = -RT_ETIMEOUT;
  274. goto __exit;
  275. }
  276. if (client->resp_status != AT_RESP_OK)
  277. {
  278. cmd = at_get_last_cmd(&cmd_size);
  279. LOG_E("execute command (%.*s) failed!", cmd_size, cmd);
  280. result = -RT_ERROR;
  281. goto __exit;
  282. }
  283. }
  284. __exit:
  285. client->resp = RT_NULL;
  286. rt_mutex_release(client->lock);
  287. return result;
  288. }
  289. /**
  290. * Waiting for connection to external devices.
  291. *
  292. * @param client current AT client object
  293. * @param timeout millisecond for timeout
  294. *
  295. * @return 0 : success
  296. * -2 : timeout
  297. * -5 : no memory
  298. */
  299. int at_client_obj_wait_connect(at_client_t client, rt_uint32_t timeout)
  300. {
  301. rt_err_t result = RT_EOK;
  302. at_response_t resp = RT_NULL;
  303. rt_tick_t start_time = 0;
  304. char *client_name = client->device->parent.name;
  305. if (client == RT_NULL)
  306. {
  307. LOG_E("input AT client object is NULL, please create or get AT Client object!");
  308. return -RT_ERROR;
  309. }
  310. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  311. if (resp == RT_NULL)
  312. {
  313. LOG_E("no memory for AT client(%s) response object.", client_name);
  314. return -RT_ENOMEM;
  315. }
  316. rt_mutex_take(client->lock, RT_WAITING_FOREVER);
  317. client->resp = resp;
  318. start_time = rt_tick_get();
  319. while (1)
  320. {
  321. /* Check whether it is timeout */
  322. if (rt_tick_get() - start_time > rt_tick_from_millisecond(timeout))
  323. {
  324. LOG_E("wait AT client(%s) connect timeout(%d tick).", client_name, timeout);
  325. result = -RT_ETIMEOUT;
  326. break;
  327. }
  328. /* Check whether it is already connected */
  329. resp->buf_len = 0;
  330. resp->line_counts = 0;
  331. rt_device_write(client->device, 0, "AT\r\n", 4);
  332. if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK)
  333. continue;
  334. else
  335. break;
  336. }
  337. at_delete_resp(resp);
  338. client->resp = RT_NULL;
  339. rt_mutex_release(client->lock);
  340. return result;
  341. }
  342. /**
  343. * Send data to AT server, send data don't have end sign(eg: \r\n).
  344. *
  345. * @param client current AT client object
  346. * @param buf send data buffer
  347. * @param size send fixed data size
  348. *
  349. * @return >0: send data size
  350. * =0: send failed
  351. */
  352. rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size)
  353. {
  354. RT_ASSERT(buf);
  355. if (client == RT_NULL)
  356. {
  357. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  358. return 0;
  359. }
  360. #ifdef AT_PRINT_RAW_CMD
  361. at_print_raw_cmd("sendline", buf, size);
  362. #endif
  363. return rt_device_write(client->device, 0, buf, size);
  364. }
  365. static rt_err_t at_client_getchar(at_client_t client, char *ch, rt_int32_t timeout)
  366. {
  367. rt_err_t result = RT_EOK;
  368. while (rt_device_read(client->device, 0, ch, 1) == 0)
  369. {
  370. result = rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout));
  371. if (result != RT_EOK)
  372. {
  373. return result;
  374. }
  375. rt_sem_control(client->rx_notice, RT_IPC_CMD_RESET, RT_NULL);
  376. }
  377. return RT_EOK;
  378. }
  379. /**
  380. * AT client receive fixed-length data.
  381. *
  382. * @param client current AT client object
  383. * @param buf receive data buffer
  384. * @param size receive fixed data size
  385. * @param timeout receive data timeout (ms)
  386. *
  387. * @note this function can only be used in execution function of URC data
  388. *
  389. * @return >0: receive data size
  390. * =0: receive failed
  391. */
  392. rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size, rt_int32_t timeout)
  393. {
  394. rt_size_t read_idx = 0;
  395. rt_err_t result = RT_EOK;
  396. char ch;
  397. RT_ASSERT(buf);
  398. if (client == RT_NULL)
  399. {
  400. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  401. return 0;
  402. }
  403. while (1)
  404. {
  405. if (read_idx < size)
  406. {
  407. result = at_client_getchar(client, &ch, timeout);
  408. if (result != RT_EOK)
  409. {
  410. LOG_E("AT Client receive failed, uart device get data error(%d)", result);
  411. return 0;
  412. }
  413. buf[read_idx++] = ch;
  414. }
  415. else
  416. {
  417. break;
  418. }
  419. }
  420. #ifdef AT_PRINT_RAW_CMD
  421. at_print_raw_cmd("urc_recv", buf, size);
  422. #endif
  423. return read_idx;
  424. }
  425. /**
  426. * AT client set end sign.
  427. *
  428. * @param client current AT client object
  429. * @param ch the end sign, can not be used when it is '\0'
  430. */
  431. void at_obj_set_end_sign(at_client_t client, char ch)
  432. {
  433. if (client == RT_NULL)
  434. {
  435. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  436. return;
  437. }
  438. client->end_sign = ch;
  439. }
  440. /**
  441. * set URC(Unsolicited Result Code) table
  442. *
  443. * @param client current AT client object
  444. * @param table URC table
  445. * @param size table size
  446. */
  447. int at_obj_set_urc_table(at_client_t client, const struct at_urc *urc_table, rt_size_t table_sz)
  448. {
  449. rt_size_t idx;
  450. if (client == RT_NULL)
  451. {
  452. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  453. return -RT_ERROR;
  454. }
  455. for (idx = 0; idx < table_sz; idx++)
  456. {
  457. RT_ASSERT(urc_table[idx].cmd_prefix);
  458. RT_ASSERT(urc_table[idx].cmd_suffix);
  459. }
  460. if (client->urc_table_size == 0)
  461. {
  462. client->urc_table = (struct at_urc_table *) rt_calloc(1, sizeof(struct at_urc_table));
  463. if (client->urc_table == RT_NULL)
  464. {
  465. return -RT_ENOMEM;
  466. }
  467. client->urc_table[0].urc = urc_table;
  468. client->urc_table[0].urc_size = table_sz;
  469. client->urc_table_size++;
  470. }
  471. else
  472. {
  473. struct at_urc_table *old_urc_table = RT_NULL;
  474. size_t old_table_size = client->urc_table_size * sizeof(struct at_urc_table);
  475. old_urc_table = (struct at_urc_table *) rt_malloc(old_table_size);
  476. if (old_urc_table == RT_NULL)
  477. {
  478. return -RT_ENOMEM;
  479. }
  480. rt_memcpy(old_urc_table, client->urc_table, old_table_size);
  481. /* realloc urc table space */
  482. client->urc_table = (struct at_urc_table *) rt_realloc(client->urc_table,
  483. old_table_size + sizeof(struct at_urc_table));
  484. if (client->urc_table == RT_NULL)
  485. {
  486. rt_free(old_urc_table);
  487. return -RT_ENOMEM;
  488. }
  489. rt_memcpy(client->urc_table, old_urc_table, old_table_size);
  490. client->urc_table[client->urc_table_size].urc = urc_table;
  491. client->urc_table[client->urc_table_size].urc_size = table_sz;
  492. client->urc_table_size++;
  493. rt_free(old_urc_table);
  494. }
  495. return RT_EOK;
  496. }
  497. /**
  498. * get AT client object by AT device name.
  499. *
  500. * @dev_name AT client device name
  501. *
  502. * @return AT client object
  503. */
  504. at_client_t at_client_get(const char *dev_name)
  505. {
  506. int idx = 0;
  507. RT_ASSERT(dev_name);
  508. for (idx = 0; idx < AT_CLIENT_NUM_MAX; idx++)
  509. {
  510. if (rt_strcmp(at_client_table[idx].device->parent.name, dev_name) == 0)
  511. {
  512. return &at_client_table[idx];
  513. }
  514. }
  515. return RT_NULL;
  516. }
  517. /**
  518. * get first AT client object in the table.
  519. *
  520. * @return AT client object
  521. */
  522. at_client_t at_client_get_first(void)
  523. {
  524. if (at_client_table[0].device == RT_NULL)
  525. {
  526. return RT_NULL;
  527. }
  528. return &at_client_table[0];
  529. }
  530. static const struct at_urc *get_urc_obj(at_client_t client)
  531. {
  532. rt_size_t i, j, prefix_len, suffix_len;
  533. rt_size_t bufsz;
  534. char *buffer = RT_NULL;
  535. const struct at_urc *urc = RT_NULL;
  536. struct at_urc_table *urc_table = RT_NULL;
  537. if (client->urc_table == RT_NULL)
  538. {
  539. return RT_NULL;
  540. }
  541. buffer = client->recv_line_buf;
  542. bufsz = client->recv_line_len;
  543. for (i = 0; i < client->urc_table_size; i++)
  544. {
  545. for (j = 0; j < client->urc_table[i].urc_size; j++)
  546. {
  547. urc_table = client->urc_table + i;
  548. urc = urc_table->urc + j;
  549. prefix_len = rt_strlen(urc->cmd_prefix);
  550. suffix_len = rt_strlen(urc->cmd_suffix);
  551. if (bufsz < prefix_len + suffix_len)
  552. {
  553. continue;
  554. }
  555. if ((prefix_len ? !rt_strncmp(buffer, urc->cmd_prefix, prefix_len) : 1)
  556. && (suffix_len ? !rt_strncmp(buffer + bufsz - suffix_len, urc->cmd_suffix, suffix_len) : 1))
  557. {
  558. return urc;
  559. }
  560. }
  561. }
  562. return RT_NULL;
  563. }
  564. static int at_recv_readline(at_client_t client)
  565. {
  566. rt_size_t read_len = 0;
  567. char ch = 0, last_ch = 0;
  568. rt_bool_t is_full = RT_FALSE;
  569. rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
  570. client->recv_line_len = 0;
  571. while (1)
  572. {
  573. at_client_getchar(client, &ch, RT_WAITING_FOREVER);
  574. if (read_len < client->recv_bufsz)
  575. {
  576. client->recv_line_buf[read_len++] = ch;
  577. client->recv_line_len = read_len;
  578. }
  579. else
  580. {
  581. is_full = RT_TRUE;
  582. }
  583. /* is newline or URC data */
  584. if ((ch == '\n' && last_ch == '\r') || (client->end_sign != 0 && ch == client->end_sign)
  585. || get_urc_obj(client))
  586. {
  587. if (is_full)
  588. {
  589. LOG_E("read line failed. The line data length is out of buffer size(%d)!", client->recv_bufsz);
  590. rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
  591. client->recv_line_len = 0;
  592. return -RT_EFULL;
  593. }
  594. break;
  595. }
  596. last_ch = ch;
  597. }
  598. #ifdef AT_PRINT_RAW_CMD
  599. at_print_raw_cmd("recvline", client->recv_line_buf, read_len);
  600. #endif
  601. return read_len;
  602. }
  603. static void client_parser(at_client_t client)
  604. {
  605. const struct at_urc *urc;
  606. while(1)
  607. {
  608. if (at_recv_readline(client) > 0)
  609. {
  610. if ((urc = get_urc_obj(client)) != RT_NULL)
  611. {
  612. /* current receive is request, try to execute related operations */
  613. if (urc->func != RT_NULL)
  614. {
  615. urc->func(client, client->recv_line_buf, client->recv_line_len);
  616. }
  617. }
  618. else if (client->resp != RT_NULL)
  619. {
  620. at_response_t resp = client->resp;
  621. /* current receive is response */
  622. client->recv_line_buf[client->recv_line_len - 1] = '\0';
  623. if (resp->buf_len + client->recv_line_len < resp->buf_size)
  624. {
  625. /* copy response lines, separated by '\0' */
  626. rt_memcpy(resp->buf + resp->buf_len, client->recv_line_buf, client->recv_line_len);
  627. /* update the current response information */
  628. resp->buf_len += client->recv_line_len;
  629. resp->line_counts++;
  630. }
  631. else
  632. {
  633. client->resp_status = AT_RESP_BUFF_FULL;
  634. LOG_E("Read response buffer failed. The Response buffer size is out of buffer size(%d)!", resp->buf_size);
  635. }
  636. /* check response result */
  637. if (rt_memcmp(client->recv_line_buf, AT_RESP_END_OK, rt_strlen(AT_RESP_END_OK)) == 0
  638. && resp->line_num == 0)
  639. {
  640. /* get the end data by response result, return response state END_OK. */
  641. client->resp_status = AT_RESP_OK;
  642. }
  643. else if (rt_strstr(client->recv_line_buf, AT_RESP_END_ERROR)
  644. || (rt_memcmp(client->recv_line_buf, AT_RESP_END_FAIL, rt_strlen(AT_RESP_END_FAIL)) == 0))
  645. {
  646. client->resp_status = AT_RESP_ERROR;
  647. }
  648. else if (resp->line_counts == resp->line_num && resp->line_num)
  649. {
  650. /* get the end data by response line, return response state END_OK.*/
  651. client->resp_status = AT_RESP_OK;
  652. }
  653. else
  654. {
  655. continue;
  656. }
  657. client->resp = RT_NULL;
  658. rt_sem_release(client->resp_notice);
  659. }
  660. else
  661. {
  662. // log_d("unrecognized line: %.*s", client->recv_line_len, client->recv_line_buf);
  663. }
  664. }
  665. }
  666. }
  667. static rt_err_t at_client_rx_ind(rt_device_t dev, rt_size_t size)
  668. {
  669. int idx = 0;
  670. for (idx = 0; idx < AT_CLIENT_NUM_MAX; idx++)
  671. {
  672. if (at_client_table[idx].device == dev && size > 0)
  673. {
  674. rt_sem_release(at_client_table[idx].rx_notice);
  675. }
  676. }
  677. return RT_EOK;
  678. }
  679. /* initialize the client object parameters */
  680. static int at_client_para_init(at_client_t client)
  681. {
  682. #define AT_CLIENT_LOCK_NAME "at_c"
  683. #define AT_CLIENT_SEM_NAME "at_cs"
  684. #define AT_CLIENT_RESP_NAME "at_cr"
  685. #define AT_CLIENT_THREAD_NAME "at_clnt"
  686. int result = RT_EOK;
  687. static int at_client_num = 0;
  688. char name[RT_NAME_MAX];
  689. client->status = AT_STATUS_UNINITIALIZED;
  690. client->recv_line_len = 0;
  691. client->recv_line_buf = (char *) rt_calloc(1, client->recv_bufsz);
  692. if (client->recv_line_buf == RT_NULL)
  693. {
  694. LOG_E("AT client initialize failed! No memory for receive buffer.");
  695. result = -RT_ENOMEM;
  696. goto __exit;
  697. }
  698. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_LOCK_NAME, at_client_num);
  699. client->lock = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  700. if (client->lock == RT_NULL)
  701. {
  702. LOG_E("AT client initialize failed! at_client_recv_lock create failed!");
  703. result = -RT_ENOMEM;
  704. goto __exit;
  705. }
  706. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_SEM_NAME, at_client_num);
  707. client->rx_notice = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  708. if (client->rx_notice == RT_NULL)
  709. {
  710. LOG_E("AT client initialize failed! at_client_notice semaphore create failed!");
  711. result = -RT_ENOMEM;
  712. goto __exit;
  713. }
  714. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_RESP_NAME, at_client_num);
  715. client->resp_notice = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  716. if (client->resp_notice == RT_NULL)
  717. {
  718. LOG_E("AT client initialize failed! at_client_resp semaphore create failed!");
  719. result = -RT_ENOMEM;
  720. goto __exit;
  721. }
  722. client->urc_table = RT_NULL;
  723. client->urc_table_size = 0;
  724. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_THREAD_NAME, at_client_num);
  725. client->parser = rt_thread_create(name,
  726. (void (*)(void *parameter))client_parser,
  727. client,
  728. 1024 + 512,
  729. RT_THREAD_PRIORITY_MAX / 3 - 1,
  730. 5);
  731. if (client->parser == RT_NULL)
  732. {
  733. result = -RT_ENOMEM;
  734. goto __exit;
  735. }
  736. __exit:
  737. if (result != RT_EOK)
  738. {
  739. if (client->lock)
  740. {
  741. rt_mutex_delete(client->lock);
  742. }
  743. if (client->rx_notice)
  744. {
  745. rt_sem_delete(client->rx_notice);
  746. }
  747. if (client->resp_notice)
  748. {
  749. rt_sem_delete(client->resp_notice);
  750. }
  751. if (client->device)
  752. {
  753. rt_device_close(client->device);
  754. }
  755. if (client->recv_line_buf)
  756. {
  757. rt_free(client->recv_line_buf);
  758. }
  759. rt_memset(client, 0x00, sizeof(struct at_client));
  760. }
  761. else
  762. {
  763. at_client_num++;
  764. }
  765. return result;
  766. }
  767. /**
  768. * AT client initialize.
  769. *
  770. * @param dev_name AT client device name
  771. * @param recv_bufsz the maximum number of receive buffer length
  772. *
  773. * @return 0 : initialize success
  774. * -1 : initialize failed
  775. * -5 : no memory
  776. */
  777. int at_client_init(const char *dev_name, rt_size_t recv_bufsz)
  778. {
  779. int idx = 0;
  780. int result = RT_EOK;
  781. rt_err_t open_result = RT_EOK;
  782. at_client_t client = RT_NULL;
  783. RT_ASSERT(dev_name);
  784. RT_ASSERT(recv_bufsz > 0);
  785. if (at_client_get(dev_name) != RT_NULL)
  786. {
  787. return result;
  788. }
  789. for (idx = 0; idx < AT_CLIENT_NUM_MAX && at_client_table[idx].device; idx++);
  790. if (idx >= AT_CLIENT_NUM_MAX)
  791. {
  792. LOG_E("AT client initialize failed! Check the maximum number(%d) of AT client.", AT_CLIENT_NUM_MAX);
  793. result = -RT_EFULL;
  794. goto __exit;
  795. }
  796. client = &at_client_table[idx];
  797. client->recv_bufsz = recv_bufsz;
  798. result = at_client_para_init(client);
  799. if (result != RT_EOK)
  800. {
  801. goto __exit;
  802. }
  803. /* find and open command device */
  804. client->device = rt_device_find(dev_name);
  805. if (client->device)
  806. {
  807. RT_ASSERT(client->device->type == RT_Device_Class_Char);
  808. /* using DMA mode first */
  809. open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX);
  810. /* using interrupt mode when DMA mode not supported */
  811. if (open_result == -RT_EIO)
  812. {
  813. open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  814. }
  815. RT_ASSERT(open_result == RT_EOK);
  816. rt_device_set_rx_indicate(client->device, at_client_rx_ind);
  817. }
  818. else
  819. {
  820. LOG_E("AT client initialize failed! Not find the device(%s).", dev_name);
  821. result = -RT_ERROR;
  822. goto __exit;
  823. }
  824. __exit:
  825. if (result == RT_EOK)
  826. {
  827. client->status = AT_STATUS_INITIALIZED;
  828. rt_thread_startup(client->parser);
  829. LOG_I("AT client(V%s) on device %s initialize success.", AT_SW_VERSION, dev_name);
  830. }
  831. else
  832. {
  833. LOG_E("AT client(V%s) on device %s initialize failed(%d).", AT_SW_VERSION, dev_name, result);
  834. }
  835. return result;
  836. }
  837. #endif /* AT_USING_CLIENT */