at_client.c 26 KB

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