at_client.c 26 KB

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