at_client.c 31 KB

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