at_client.c 26 KB

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