at_client.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. while (rt_device_read(client->device, 0, ch, 1) == 0)
  363. {
  364. rt_sem_control(client->rx_notice, RT_IPC_CMD_RESET, RT_NULL);
  365. result = rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout));
  366. if (result != RT_EOK)
  367. {
  368. return result;
  369. }
  370. }
  371. return RT_EOK;
  372. }
  373. /**
  374. * AT client receive fixed-length data.
  375. *
  376. * @param client current AT client object
  377. * @param buf receive data buffer
  378. * @param size receive fixed data size
  379. * @param timeout receive data timeout (ms)
  380. *
  381. * @note this function can only be used in execution function of URC data
  382. *
  383. * @return >0: receive data size
  384. * =0: receive failed
  385. */
  386. rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size, rt_int32_t timeout)
  387. {
  388. rt_size_t read_idx = 0;
  389. rt_err_t result = RT_EOK;
  390. char ch;
  391. RT_ASSERT(buf);
  392. if (client == RT_NULL)
  393. {
  394. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  395. return 0;
  396. }
  397. while (1)
  398. {
  399. if (read_idx < size)
  400. {
  401. result = at_client_getchar(client, &ch, timeout);
  402. if (result != RT_EOK)
  403. {
  404. LOG_E("AT Client receive failed, uart device get data error(%d)", result);
  405. return 0;
  406. }
  407. buf[read_idx++] = ch;
  408. }
  409. else
  410. {
  411. break;
  412. }
  413. }
  414. #ifdef AT_PRINT_RAW_CMD
  415. at_print_raw_cmd("urc_recv", buf, size);
  416. #endif
  417. return read_idx;
  418. }
  419. /**
  420. * AT client set end sign.
  421. *
  422. * @param client current AT client object
  423. * @param ch the end sign, can not be used when it is '\0'
  424. */
  425. void at_obj_set_end_sign(at_client_t client, char ch)
  426. {
  427. if (client == RT_NULL)
  428. {
  429. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  430. return;
  431. }
  432. client->end_sign = ch;
  433. }
  434. /**
  435. * set URC(Unsolicited Result Code) table
  436. *
  437. * @param client current AT client object
  438. * @param table URC table
  439. * @param size table size
  440. */
  441. int at_obj_set_urc_table(at_client_t client, const struct at_urc *urc_table, rt_size_t table_sz)
  442. {
  443. rt_size_t idx;
  444. if (client == RT_NULL)
  445. {
  446. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  447. return -RT_ERROR;
  448. }
  449. for (idx = 0; idx < table_sz; idx++)
  450. {
  451. RT_ASSERT(urc_table[idx].cmd_prefix);
  452. RT_ASSERT(urc_table[idx].cmd_suffix);
  453. }
  454. if (client->urc_table_size == 0)
  455. {
  456. client->urc_table = (struct at_urc_table *) rt_calloc(1, sizeof(struct at_urc_table));
  457. if (client->urc_table == RT_NULL)
  458. {
  459. return -RT_ENOMEM;
  460. }
  461. client->urc_table[0].urc = urc_table;
  462. client->urc_table[0].urc_size = table_sz;
  463. client->urc_table_size++;
  464. }
  465. else
  466. {
  467. struct at_urc_table *old_urc_table = RT_NULL;
  468. size_t old_table_size = client->urc_table_size * sizeof(struct at_urc_table);
  469. old_urc_table = (struct at_urc_table *) rt_malloc(old_table_size);
  470. if (old_urc_table == RT_NULL)
  471. {
  472. return -RT_ENOMEM;
  473. }
  474. rt_memcpy(old_urc_table, client->urc_table, old_table_size);
  475. /* realloc urc table space */
  476. client->urc_table = (struct at_urc_table *) rt_realloc(client->urc_table,
  477. old_table_size + sizeof(struct at_urc_table));
  478. if (client->urc_table == RT_NULL)
  479. {
  480. rt_free(old_urc_table);
  481. return -RT_ENOMEM;
  482. }
  483. rt_memcpy(client->urc_table, old_urc_table, old_table_size);
  484. client->urc_table[client->urc_table_size].urc = urc_table;
  485. client->urc_table[client->urc_table_size].urc_size = table_sz;
  486. client->urc_table_size++;
  487. rt_free(old_urc_table);
  488. }
  489. return RT_EOK;
  490. }
  491. /**
  492. * get AT client object by AT device name.
  493. *
  494. * @dev_name AT client device name
  495. *
  496. * @return AT client object
  497. */
  498. at_client_t at_client_get(const char *dev_name)
  499. {
  500. int idx = 0;
  501. RT_ASSERT(dev_name);
  502. for (idx = 0; idx < AT_CLIENT_NUM_MAX; idx++)
  503. {
  504. if (rt_strcmp(at_client_table[idx].device->parent.name, dev_name) == 0)
  505. {
  506. return &at_client_table[idx];
  507. }
  508. }
  509. return RT_NULL;
  510. }
  511. /**
  512. * get first AT client object in the table.
  513. *
  514. * @return AT client object
  515. */
  516. at_client_t at_client_get_first(void)
  517. {
  518. if (at_client_table[0].device == RT_NULL)
  519. {
  520. return RT_NULL;
  521. }
  522. return &at_client_table[0];
  523. }
  524. static const struct at_urc *get_urc_obj(at_client_t client)
  525. {
  526. rt_size_t i, j, prefix_len, suffix_len;
  527. rt_size_t bufsz;
  528. char *buffer = RT_NULL;
  529. const struct at_urc *urc = RT_NULL;
  530. struct at_urc_table *urc_table = RT_NULL;
  531. if (client->urc_table == RT_NULL)
  532. {
  533. return RT_NULL;
  534. }
  535. buffer = client->recv_line_buf;
  536. bufsz = client->recv_line_len;
  537. for (i = 0; i < client->urc_table_size; i++)
  538. {
  539. for (j = 0; j < client->urc_table[i].urc_size; j++)
  540. {
  541. urc_table = client->urc_table + i;
  542. urc = urc_table->urc + j;
  543. prefix_len = rt_strlen(urc->cmd_prefix);
  544. suffix_len = rt_strlen(urc->cmd_suffix);
  545. if (bufsz < prefix_len + suffix_len)
  546. {
  547. continue;
  548. }
  549. if ((prefix_len ? !rt_strncmp(buffer, urc->cmd_prefix, prefix_len) : 1)
  550. && (suffix_len ? !rt_strncmp(buffer + bufsz - suffix_len, urc->cmd_suffix, suffix_len) : 1))
  551. {
  552. return urc;
  553. }
  554. }
  555. }
  556. return RT_NULL;
  557. }
  558. static int at_recv_readline(at_client_t client)
  559. {
  560. rt_size_t read_len = 0;
  561. char ch = 0, last_ch = 0;
  562. rt_bool_t is_full = RT_FALSE;
  563. rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
  564. client->recv_line_len = 0;
  565. while (1)
  566. {
  567. at_client_getchar(client, &ch, RT_WAITING_FOREVER);
  568. if (read_len < client->recv_bufsz)
  569. {
  570. client->recv_line_buf[read_len++] = ch;
  571. client->recv_line_len = read_len;
  572. }
  573. else
  574. {
  575. is_full = RT_TRUE;
  576. }
  577. /* is newline or URC data */
  578. if ((ch == '\n' && last_ch == '\r') || (client->end_sign != 0 && ch == client->end_sign)
  579. || get_urc_obj(client))
  580. {
  581. if (is_full)
  582. {
  583. LOG_E("read line failed. The line data length is out of buffer size(%d)!", client->recv_bufsz);
  584. rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
  585. client->recv_line_len = 0;
  586. return -RT_EFULL;
  587. }
  588. break;
  589. }
  590. last_ch = ch;
  591. }
  592. #ifdef AT_PRINT_RAW_CMD
  593. at_print_raw_cmd("recvline", client->recv_line_buf, read_len);
  594. #endif
  595. return read_len;
  596. }
  597. static void client_parser(at_client_t client)
  598. {
  599. const struct at_urc *urc;
  600. while(1)
  601. {
  602. if (at_recv_readline(client) > 0)
  603. {
  604. if ((urc = get_urc_obj(client)) != RT_NULL)
  605. {
  606. /* current receive is request, try to execute related operations */
  607. if (urc->func != RT_NULL)
  608. {
  609. urc->func(client, client->recv_line_buf, client->recv_line_len);
  610. }
  611. }
  612. else if (client->resp != RT_NULL)
  613. {
  614. at_response_t resp = client->resp;
  615. /* current receive is response */
  616. client->recv_line_buf[client->recv_line_len - 1] = '\0';
  617. if (resp->buf_len + client->recv_line_len < resp->buf_size)
  618. {
  619. /* copy response lines, separated by '\0' */
  620. rt_memcpy(resp->buf + resp->buf_len, client->recv_line_buf, client->recv_line_len);
  621. /* update the current response information */
  622. resp->buf_len += client->recv_line_len;
  623. resp->line_counts++;
  624. }
  625. else
  626. {
  627. client->resp_status = AT_RESP_BUFF_FULL;
  628. LOG_E("Read response buffer failed. The Response buffer size is out of buffer size(%d)!", resp->buf_size);
  629. }
  630. /* check response result */
  631. if (rt_memcmp(client->recv_line_buf, AT_RESP_END_OK, rt_strlen(AT_RESP_END_OK)) == 0
  632. && resp->line_num == 0)
  633. {
  634. /* get the end data by response result, return response state END_OK. */
  635. client->resp_status = AT_RESP_OK;
  636. }
  637. else if (rt_strstr(client->recv_line_buf, AT_RESP_END_ERROR)
  638. || (rt_memcmp(client->recv_line_buf, AT_RESP_END_FAIL, rt_strlen(AT_RESP_END_FAIL)) == 0))
  639. {
  640. client->resp_status = AT_RESP_ERROR;
  641. }
  642. else if (resp->line_counts == resp->line_num && resp->line_num)
  643. {
  644. /* get the end data by response line, return response state END_OK.*/
  645. client->resp_status = AT_RESP_OK;
  646. }
  647. else
  648. {
  649. continue;
  650. }
  651. client->resp = RT_NULL;
  652. rt_sem_release(client->resp_notice);
  653. }
  654. else
  655. {
  656. // log_d("unrecognized line: %.*s", client->recv_line_len, client->recv_line_buf);
  657. }
  658. }
  659. }
  660. }
  661. static rt_err_t at_client_rx_ind(rt_device_t dev, rt_size_t size)
  662. {
  663. int idx = 0;
  664. for (idx = 0; idx < AT_CLIENT_NUM_MAX; idx++)
  665. {
  666. if (at_client_table[idx].device == dev && size > 0)
  667. {
  668. rt_sem_release(at_client_table[idx].rx_notice);
  669. }
  670. }
  671. return RT_EOK;
  672. }
  673. /* initialize the client object parameters */
  674. static int at_client_para_init(at_client_t client)
  675. {
  676. #define AT_CLIENT_LOCK_NAME "at_c"
  677. #define AT_CLIENT_SEM_NAME "at_cs"
  678. #define AT_CLIENT_RESP_NAME "at_cr"
  679. #define AT_CLIENT_THREAD_NAME "at_clnt"
  680. int result = RT_EOK;
  681. static int at_client_num = 0;
  682. char name[RT_NAME_MAX];
  683. client->status = AT_STATUS_UNINITIALIZED;
  684. client->recv_line_len = 0;
  685. client->recv_line_buf = (char *) rt_calloc(1, client->recv_bufsz);
  686. if (client->recv_line_buf == RT_NULL)
  687. {
  688. LOG_E("AT client initialize failed! No memory for receive buffer.");
  689. result = -RT_ENOMEM;
  690. goto __exit;
  691. }
  692. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_LOCK_NAME, at_client_num);
  693. client->lock = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  694. if (client->lock == RT_NULL)
  695. {
  696. LOG_E("AT client initialize failed! at_client_recv_lock create failed!");
  697. result = -RT_ENOMEM;
  698. goto __exit;
  699. }
  700. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_SEM_NAME, at_client_num);
  701. client->rx_notice = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  702. if (client->rx_notice == RT_NULL)
  703. {
  704. LOG_E("AT client initialize failed! at_client_notice semaphore create failed!");
  705. result = -RT_ENOMEM;
  706. goto __exit;
  707. }
  708. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_RESP_NAME, at_client_num);
  709. client->resp_notice = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  710. if (client->resp_notice == RT_NULL)
  711. {
  712. LOG_E("AT client initialize failed! at_client_resp semaphore create failed!");
  713. result = -RT_ENOMEM;
  714. goto __exit;
  715. }
  716. client->urc_table = RT_NULL;
  717. client->urc_table_size = 0;
  718. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_THREAD_NAME, at_client_num);
  719. client->parser = rt_thread_create(name,
  720. (void (*)(void *parameter))client_parser,
  721. client,
  722. 1024 + 512,
  723. RT_THREAD_PRIORITY_MAX / 3 - 1,
  724. 5);
  725. if (client->parser == RT_NULL)
  726. {
  727. result = -RT_ENOMEM;
  728. goto __exit;
  729. }
  730. __exit:
  731. if (result != RT_EOK)
  732. {
  733. if (client->lock)
  734. {
  735. rt_mutex_delete(client->lock);
  736. }
  737. if (client->rx_notice)
  738. {
  739. rt_sem_delete(client->rx_notice);
  740. }
  741. if (client->resp_notice)
  742. {
  743. rt_sem_delete(client->resp_notice);
  744. }
  745. if (client->device)
  746. {
  747. rt_device_close(client->device);
  748. }
  749. if (client->recv_line_buf)
  750. {
  751. rt_free(client->recv_line_buf);
  752. }
  753. rt_memset(client, 0x00, sizeof(struct at_client));
  754. }
  755. else
  756. {
  757. at_client_num++;
  758. }
  759. return result;
  760. }
  761. /**
  762. * AT client initialize.
  763. *
  764. * @param dev_name AT client device name
  765. * @param recv_bufsz the maximum number of receive buffer length
  766. *
  767. * @return 0 : initialize success
  768. * -1 : initialize failed
  769. * -5 : no memory
  770. */
  771. int at_client_init(const char *dev_name, rt_size_t recv_bufsz)
  772. {
  773. int idx = 0;
  774. int result = RT_EOK;
  775. rt_err_t open_result = RT_EOK;
  776. at_client_t client = RT_NULL;
  777. RT_ASSERT(dev_name);
  778. RT_ASSERT(recv_bufsz > 0);
  779. if (at_client_get(dev_name) != RT_NULL)
  780. {
  781. return result;
  782. }
  783. for (idx = 0; idx < AT_CLIENT_NUM_MAX && at_client_table[idx].device; idx++);
  784. if (idx >= AT_CLIENT_NUM_MAX)
  785. {
  786. LOG_E("AT client initialize failed! Check the maximum number(%d) of AT client.", AT_CLIENT_NUM_MAX);
  787. result = -RT_EFULL;
  788. goto __exit;
  789. }
  790. client = &at_client_table[idx];
  791. client->recv_bufsz = recv_bufsz;
  792. result = at_client_para_init(client);
  793. if (result != RT_EOK)
  794. {
  795. goto __exit;
  796. }
  797. /* find and open command device */
  798. client->device = rt_device_find(dev_name);
  799. if (client->device)
  800. {
  801. RT_ASSERT(client->device->type == RT_Device_Class_Char);
  802. /* using DMA mode first */
  803. open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX);
  804. /* using interrupt mode when DMA mode not supported */
  805. if (open_result == -RT_EIO)
  806. {
  807. open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  808. }
  809. RT_ASSERT(open_result == RT_EOK);
  810. rt_device_set_rx_indicate(client->device, at_client_rx_ind);
  811. }
  812. else
  813. {
  814. LOG_E("AT client initialize failed! Not find the device(%s).", dev_name);
  815. result = -RT_ERROR;
  816. goto __exit;
  817. }
  818. __exit:
  819. if (result == RT_EOK)
  820. {
  821. client->status = AT_STATUS_INITIALIZED;
  822. rt_thread_startup(client->parser);
  823. LOG_I("AT client(V%s) on device %s initialize success.", AT_SW_VERSION, dev_name);
  824. }
  825. else
  826. {
  827. LOG_E("AT client(V%s) on device %s initialize failed(%d).", AT_SW_VERSION, dev_name, result);
  828. }
  829. return result;
  830. }
  831. #endif /* AT_USING_CLIENT */