at_client.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. */
  231. int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr, ...)
  232. {
  233. va_list args;
  234. rt_size_t cmd_size = 0;
  235. rt_err_t result = RT_EOK;
  236. const char *cmd = RT_NULL;
  237. RT_ASSERT(cmd_expr);
  238. if (client == RT_NULL)
  239. {
  240. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  241. return -RT_ERROR;
  242. }
  243. rt_mutex_take(client->lock, RT_WAITING_FOREVER);
  244. client->resp_status = AT_RESP_OK;
  245. client->resp = resp;
  246. va_start(args, cmd_expr);
  247. at_vprintfln(client->device, cmd_expr, args);
  248. va_end(args);
  249. if (resp != RT_NULL)
  250. {
  251. resp->line_counts = 0;
  252. if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK)
  253. {
  254. cmd = at_get_last_cmd(&cmd_size);
  255. LOG_E("execute command (%.*s) timeout (%d ticks)!", cmd_size, cmd, resp->timeout);
  256. client->resp_status = AT_RESP_TIMEOUT;
  257. result = -RT_ETIMEOUT;
  258. goto __exit;
  259. }
  260. if (client->resp_status != AT_RESP_OK)
  261. {
  262. cmd = at_get_last_cmd(&cmd_size);
  263. LOG_E("execute command (%.*s) failed!", cmd_size, cmd);
  264. result = -RT_ERROR;
  265. goto __exit;
  266. }
  267. }
  268. __exit:
  269. client->resp = RT_NULL;
  270. rt_mutex_release(client->lock);
  271. return result;
  272. }
  273. /**
  274. * Waiting for connection to external devices.
  275. *
  276. * @param client current AT client object
  277. * @param timeout millisecond for timeout
  278. *
  279. * @return 0 : success
  280. * -2 : timeout
  281. * -5 : no memory
  282. */
  283. int at_client_obj_wait_connect(at_client_t client, rt_uint32_t timeout)
  284. {
  285. rt_err_t result = RT_EOK;
  286. at_response_t resp = RT_NULL;
  287. rt_tick_t start_time = 0;
  288. if (client == RT_NULL)
  289. {
  290. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  291. return -RT_ERROR;
  292. }
  293. resp = at_create_resp(16, 0, rt_tick_from_millisecond(500));
  294. if (resp == RT_NULL)
  295. {
  296. LOG_E("No memory for response object!");
  297. return -RT_ENOMEM;
  298. }
  299. rt_mutex_take(client->lock, RT_WAITING_FOREVER);
  300. client->resp = resp;
  301. start_time = rt_tick_get();
  302. while (1)
  303. {
  304. /* Check whether it is timeout */
  305. if (rt_tick_get() - start_time > rt_tick_from_millisecond(timeout))
  306. {
  307. LOG_E("wait connect timeout (%d millisecond)!", timeout);
  308. result = -RT_ETIMEOUT;
  309. break;
  310. }
  311. /* Check whether it is already connected */
  312. resp->line_counts = 0;
  313. rt_device_write(client->device, 0, "AT\r\n", 4);
  314. if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK)
  315. continue;
  316. else
  317. break;
  318. }
  319. at_delete_resp(resp);
  320. client->resp = RT_NULL;
  321. rt_mutex_release(client->lock);
  322. return result;
  323. }
  324. /**
  325. * Send data to AT server, send data don't have end sign(eg: \r\n).
  326. *
  327. * @param client current AT client object
  328. * @param buf send data buffer
  329. * @param size send fixed data size
  330. *
  331. * @return >0: send data size
  332. * =0: send failed
  333. */
  334. rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size)
  335. {
  336. RT_ASSERT(buf);
  337. if (client == RT_NULL)
  338. {
  339. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  340. return 0;
  341. }
  342. #ifdef AT_PRINT_RAW_CMD
  343. at_print_raw_cmd("send", buf, size);
  344. #endif
  345. return rt_device_write(client->device, 0, buf, size);
  346. }
  347. static rt_err_t at_client_getchar(at_client_t client, char *ch, rt_int32_t timeout)
  348. {
  349. rt_err_t result = RT_EOK;
  350. while (rt_device_read(client->device, 0, ch, 1) == 0)
  351. {
  352. rt_sem_control(client->rx_notice, RT_IPC_CMD_RESET, RT_NULL);
  353. result = rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout));
  354. if (result != RT_EOK)
  355. {
  356. return result;
  357. }
  358. }
  359. return RT_EOK;
  360. }
  361. /**
  362. * AT client receive fixed-length data.
  363. *
  364. * @param client current AT client object
  365. * @param buf receive data buffer
  366. * @param size receive fixed data size
  367. * @param timeout receive data timeout (ms)
  368. *
  369. * @note this function can only be used in execution function of URC data
  370. *
  371. * @return >0: receive data size
  372. * =0: receive failed
  373. */
  374. rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size, rt_int32_t timeout)
  375. {
  376. rt_size_t read_idx = 0;
  377. rt_err_t result = RT_EOK;
  378. char ch;
  379. RT_ASSERT(buf);
  380. if (client == RT_NULL)
  381. {
  382. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  383. return 0;
  384. }
  385. while (1)
  386. {
  387. if (read_idx < size)
  388. {
  389. result = at_client_getchar(client, &ch, timeout);
  390. if (result != RT_EOK)
  391. {
  392. LOG_E("AT Client receive failed, uart device get data error(%d)", result);
  393. return 0;
  394. }
  395. buf[read_idx++] = ch;
  396. }
  397. else
  398. {
  399. break;
  400. }
  401. }
  402. #ifdef AT_PRINT_RAW_CMD
  403. at_print_raw_cmd("urc_recv", buf, size);
  404. #endif
  405. return read_idx;
  406. }
  407. /**
  408. * AT client set end sign.
  409. *
  410. * @param client current AT client object
  411. * @param ch the end sign, can not be used when it is '\0'
  412. */
  413. void at_obj_set_end_sign(at_client_t client, char ch)
  414. {
  415. if (client == RT_NULL)
  416. {
  417. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  418. return;
  419. }
  420. client->end_sign = ch;
  421. }
  422. /**
  423. * set URC(Unsolicited Result Code) table
  424. *
  425. * @param client current AT client object
  426. * @param table URC table
  427. * @param size table size
  428. */
  429. void at_obj_set_urc_table(at_client_t client, const struct at_urc *urc_table, rt_size_t table_sz)
  430. {
  431. rt_size_t idx;
  432. if (client == RT_NULL)
  433. {
  434. LOG_E("input AT Client object is NULL, please create or get AT Client object!");
  435. return;
  436. }
  437. for (idx = 0; idx < table_sz; idx++)
  438. {
  439. RT_ASSERT(urc_table[idx].cmd_prefix);
  440. RT_ASSERT(urc_table[idx].cmd_suffix);
  441. }
  442. client->urc_table = urc_table;
  443. client->urc_table_size = table_sz;
  444. }
  445. /**
  446. * get AT client object by AT device name.
  447. *
  448. * @dev_name AT client device name
  449. *
  450. * @return AT client object
  451. */
  452. at_client_t at_client_get(const char *dev_name)
  453. {
  454. int idx = 0;
  455. RT_ASSERT(dev_name);
  456. for (idx = 0; idx < AT_CLIENT_NUM_MAX; idx++)
  457. {
  458. if (rt_strcmp(at_client_table[idx].device->parent.name, dev_name) == 0)
  459. {
  460. return &at_client_table[idx];
  461. }
  462. }
  463. return RT_NULL;
  464. }
  465. /**
  466. * get first AT client object in the table.
  467. *
  468. * @return AT client object
  469. */
  470. at_client_t at_client_get_first(void)
  471. {
  472. if (at_client_table[0].device == RT_NULL)
  473. {
  474. return RT_NULL;
  475. }
  476. return &at_client_table[0];
  477. }
  478. static const struct at_urc *get_urc_obj(at_client_t client)
  479. {
  480. rt_size_t i, prefix_len, suffix_len;
  481. rt_size_t buf_sz;
  482. char *buffer = RT_NULL;
  483. if (client->urc_table == RT_NULL)
  484. {
  485. return RT_NULL;
  486. }
  487. buffer = client->recv_buffer;
  488. buf_sz = client->cur_recv_len;
  489. for (i = 0; i < client->urc_table_size; i++)
  490. {
  491. prefix_len = strlen(client->urc_table[i].cmd_prefix);
  492. suffix_len = strlen(client->urc_table[i].cmd_suffix);
  493. if (buf_sz < prefix_len + suffix_len)
  494. {
  495. continue;
  496. }
  497. if ((prefix_len ? !strncmp(buffer, client->urc_table[i].cmd_prefix, prefix_len) : 1)
  498. && (suffix_len ? !strncmp(buffer + buf_sz - suffix_len, client->urc_table[i].cmd_suffix, suffix_len) : 1))
  499. {
  500. return &client->urc_table[i];
  501. }
  502. }
  503. return RT_NULL;
  504. }
  505. static int at_recv_readline(at_client_t client)
  506. {
  507. rt_size_t read_len = 0;
  508. char ch = 0, last_ch = 0;
  509. rt_bool_t is_full = RT_FALSE;
  510. memset(client->recv_buffer, 0x00, client->recv_bufsz);
  511. client->cur_recv_len = 0;
  512. while (1)
  513. {
  514. at_client_getchar(client, &ch, RT_WAITING_FOREVER);
  515. if (read_len < client->recv_bufsz)
  516. {
  517. client->recv_buffer[read_len++] = ch;
  518. client->cur_recv_len = read_len;
  519. }
  520. else
  521. {
  522. is_full = RT_TRUE;
  523. }
  524. /* is newline or URC data */
  525. if ((ch == '\n' && last_ch == '\r') || (client->end_sign != 0 && ch == client->end_sign)
  526. || get_urc_obj(client))
  527. {
  528. if (is_full)
  529. {
  530. LOG_E("read line failed. The line data length is out of buffer size(%d)!", client->recv_bufsz);
  531. memset(client->recv_buffer, 0x00, client->recv_bufsz);
  532. client->cur_recv_len = 0;
  533. return -RT_EFULL;
  534. }
  535. break;
  536. }
  537. last_ch = ch;
  538. }
  539. #ifdef AT_PRINT_RAW_CMD
  540. at_print_raw_cmd("recvline", client->recv_buffer, read_len);
  541. #endif
  542. return read_len;
  543. }
  544. static void client_parser(at_client_t client)
  545. {
  546. int resp_buf_len = 0;
  547. const struct at_urc *urc;
  548. rt_size_t line_counts = 0;
  549. while(1)
  550. {
  551. if (at_recv_readline(client) > 0)
  552. {
  553. if ((urc = get_urc_obj(client)) != RT_NULL)
  554. {
  555. /* current receive is request, try to execute related operations */
  556. if (urc->func != RT_NULL)
  557. {
  558. urc->func(client->recv_buffer, client->cur_recv_len);
  559. }
  560. }
  561. else if (client->resp != RT_NULL)
  562. {
  563. /* current receive is response */
  564. client->recv_buffer[client->cur_recv_len - 1] = '\0';
  565. if (resp_buf_len + client->cur_recv_len < client->resp->buf_size)
  566. {
  567. /* copy response lines, separated by '\0' */
  568. memcpy(client->resp->buf + resp_buf_len, client->recv_buffer, client->cur_recv_len);
  569. resp_buf_len += client->cur_recv_len;
  570. line_counts++;
  571. }
  572. else
  573. {
  574. client->resp_status = AT_RESP_BUFF_FULL;
  575. LOG_E("Read response buffer failed. The Response buffer size is out of buffer size(%d)!", client->resp->buf_size);
  576. }
  577. /* check response result */
  578. if (memcmp(client->recv_buffer, AT_RESP_END_OK, strlen(AT_RESP_END_OK)) == 0
  579. && client->resp->line_num == 0)
  580. {
  581. /* get the end data by response result, return response state END_OK. */
  582. client->resp_status = AT_RESP_OK;
  583. }
  584. else if (strstr(client->recv_buffer, AT_RESP_END_ERROR)
  585. || (memcmp(client->recv_buffer, AT_RESP_END_FAIL, strlen(AT_RESP_END_FAIL)) == 0))
  586. {
  587. client->resp_status = AT_RESP_ERROR;
  588. }
  589. else if (line_counts == client->resp->line_num && client->resp->line_num)
  590. {
  591. /* get the end data by response line, return response state END_OK.*/
  592. client->resp_status = AT_RESP_OK;
  593. }
  594. else
  595. {
  596. continue;
  597. }
  598. client->resp->line_counts = line_counts;
  599. client->resp = RT_NULL;
  600. rt_sem_release(client->resp_notice);
  601. resp_buf_len = 0, line_counts = 0;
  602. }
  603. else
  604. {
  605. // log_d("unrecognized line: %.*s", client->cur_recv_len, client->recv_buffer);
  606. }
  607. }
  608. }
  609. }
  610. static rt_err_t at_client_rx_ind(rt_device_t dev, rt_size_t size)
  611. {
  612. int idx = 0;
  613. for (idx = 0; idx < AT_CLIENT_NUM_MAX; idx++)
  614. {
  615. if (at_client_table[idx].device == dev && size > 0)
  616. {
  617. rt_sem_release(at_client_table[idx].rx_notice);
  618. }
  619. }
  620. return RT_EOK;
  621. }
  622. /* initialize the client object parameters */
  623. static int at_client_para_init(at_client_t client)
  624. {
  625. #define AT_CLIENT_LOCK_NAME "at_c"
  626. #define AT_CLIENT_SEM_NAME "at_cs"
  627. #define AT_CLIENT_RESP_NAME "at_cr"
  628. #define AT_CLIENT_THREAD_NAME "at_clnt"
  629. int result = RT_EOK;
  630. static int at_client_num = 0;
  631. char name[RT_NAME_MAX];
  632. client->status = AT_STATUS_UNINITIALIZED;
  633. client->cur_recv_len = 0;
  634. client->recv_buffer = (char *) rt_calloc(1, client->recv_bufsz);
  635. if (client->recv_buffer == RT_NULL)
  636. {
  637. LOG_E("AT client initialize failed! No memory for receive buffer.");
  638. result = -RT_ENOMEM;
  639. goto __exit;
  640. }
  641. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_LOCK_NAME, at_client_num);
  642. client->lock = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  643. if (client->lock == RT_NULL)
  644. {
  645. LOG_E("AT client initialize failed! at_client_recv_lock create failed!");
  646. result = -RT_ENOMEM;
  647. goto __exit;
  648. }
  649. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_SEM_NAME, at_client_num);
  650. client->rx_notice = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  651. if (client->rx_notice == RT_NULL)
  652. {
  653. LOG_E("AT client initialize failed! at_client_notice semaphore create failed!");
  654. result = -RT_ENOMEM;
  655. goto __exit;
  656. }
  657. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_RESP_NAME, at_client_num);
  658. client->resp_notice = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  659. if (client->resp_notice == RT_NULL)
  660. {
  661. LOG_E("AT client initialize failed! at_client_resp semaphore create failed!");
  662. result = -RT_ENOMEM;
  663. goto __exit;
  664. }
  665. client->urc_table = RT_NULL;
  666. client->urc_table_size = 0;
  667. rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_THREAD_NAME, at_client_num);
  668. client->parser = rt_thread_create(name,
  669. (void (*)(void *parameter))client_parser,
  670. client,
  671. 1024 + 512,
  672. RT_THREAD_PRIORITY_MAX / 3 - 1,
  673. 5);
  674. if (client->parser == RT_NULL)
  675. {
  676. result = -RT_ENOMEM;
  677. goto __exit;
  678. }
  679. __exit:
  680. if (result != RT_EOK)
  681. {
  682. if (client->lock)
  683. {
  684. rt_mutex_delete(client->lock);
  685. }
  686. if (client->rx_notice)
  687. {
  688. rt_sem_delete(client->rx_notice);
  689. }
  690. if (client->resp_notice)
  691. {
  692. rt_sem_delete(client->resp_notice);
  693. }
  694. if (client->device)
  695. {
  696. rt_device_close(client->device);
  697. }
  698. if (client->recv_buffer)
  699. {
  700. rt_free(client->recv_buffer);
  701. }
  702. rt_memset(client, 0x00, sizeof(struct at_client));
  703. }
  704. else
  705. {
  706. at_client_num++;
  707. }
  708. return result;
  709. }
  710. /**
  711. * AT client initialize.
  712. *
  713. * @param dev_name AT client device name
  714. * @param recv_bufsz the maximum number of receive buffer length
  715. *
  716. * @return 0 : initialize success
  717. * -1 : initialize failed
  718. * -5 : no memory
  719. */
  720. int at_client_init(const char *dev_name, rt_size_t recv_bufsz)
  721. {
  722. int idx = 0;
  723. int result = RT_EOK;
  724. rt_err_t open_result = RT_EOK;
  725. at_client_t client = RT_NULL;
  726. RT_ASSERT(dev_name);
  727. RT_ASSERT(recv_bufsz > 0);
  728. for (idx = 0; idx < AT_CLIENT_NUM_MAX && at_client_table[idx].device; idx++);
  729. if (idx >= AT_CLIENT_NUM_MAX)
  730. {
  731. LOG_E("AT client initialize failed! Check the maximum number(%d) of AT client.", AT_CLIENT_NUM_MAX);
  732. result = -RT_EFULL;
  733. goto __exit;
  734. }
  735. client = &at_client_table[idx];
  736. client->recv_bufsz = recv_bufsz;
  737. /* find and open command device */
  738. client->device = rt_device_find(dev_name);
  739. if (client->device)
  740. {
  741. RT_ASSERT(client->device->type == RT_Device_Class_Char);
  742. /* using DMA mode first */
  743. open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX);
  744. /* using interrupt mode when DMA mode not supported */
  745. if (open_result == -RT_EIO)
  746. {
  747. open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  748. }
  749. RT_ASSERT(open_result == RT_EOK);
  750. rt_device_set_rx_indicate(client->device, at_client_rx_ind);
  751. }
  752. else
  753. {
  754. LOG_E("AT client initialize failed! Not find the device(%s).", dev_name);
  755. result = -RT_ERROR;
  756. goto __exit;
  757. }
  758. result = at_client_para_init(client);
  759. if (result != RT_EOK)
  760. {
  761. goto __exit;
  762. }
  763. __exit:
  764. if (result == RT_EOK)
  765. {
  766. client->status = AT_STATUS_INITIALIZED;
  767. rt_thread_startup(client->parser);
  768. LOG_I("AT client(V%s) on device %s initialize success.", AT_SW_VERSION, dev_name);
  769. }
  770. else
  771. {
  772. LOG_E("AT client(V%s) on device %s initialize failed(%d).", AT_SW_VERSION, dev_name, result);
  773. }
  774. return result;
  775. }
  776. #endif /* AT_USING_CLIENT */