at_client.c 25 KB

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