at_socket_sim76xx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-22 thomasonegd first version
  9. * 2019-03-06 thomasonegd fix udp connection.
  10. * 2019-03-08 thomasonegd add power_on & power_off api
  11. * 2019-05-14 chenyong multi AT socket client support
  12. * 2019-08-24 chenyong add netdev support
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <at_device_sim76xx.h>
  17. #define LOG_TAG "at.skt.sim76"
  18. #include <at_log.h>
  19. #ifdef AT_DEVICE_USING_SIM76XX
  20. #define SIM76XX_MODULE_SEND_MAX_SIZE 1500
  21. #define SIM76XX_MAX_CONNECTIONS 10
  22. #define SIM76XX_IPADDR_LEN 16
  23. /* set real event by current socket and current state */
  24. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  25. /* AT socket event type */
  26. #define SIM76XX_EVENT_CONN_OK (1L << 0)
  27. #define SIM76XX_EVENT_SEND_OK (1L << 1)
  28. #define SIM76XX_EVENT_RECV_OK (1L << 2)
  29. #define SIM76XX_EVNET_CLOSE_OK (1L << 3)
  30. #define SIM76XX_EVENT_CONN_FAIL (1L << 4)
  31. #define SIM76XX_EVENT_SEND_FAIL (1L << 5)
  32. static at_evt_cb_t at_evt_cb_set[] =
  33. {
  34. [AT_SOCKET_EVT_RECV] = NULL,
  35. [AT_SOCKET_EVT_CLOSED] = NULL,
  36. };
  37. static char udp_ipstr[SIM76XX_MAX_CONNECTIONS][SIM76XX_IPADDR_LEN] = {0};
  38. static int udp_port[SIM76XX_MAX_CONNECTIONS] = {0};
  39. /* unsolicited TCP/IP command<err> codes */
  40. static void at_tcp_ip_errcode_parse(int result)
  41. {
  42. switch(result)
  43. {
  44. case 0 : LOG_D("%d : operation succeeded ", result); break;
  45. case 1 : LOG_E("%d : UNetwork failure", result); break;
  46. case 2 : LOG_E("%d : Network not opened", result); break;
  47. case 3 : LOG_E("%d : Wrong parameter", result); break;
  48. case 4 : LOG_D("%d : Operation not supported", result); break;
  49. case 5 : LOG_E("%d : Failed to create socket", result); break;
  50. case 6 : LOG_E("%d : Failed to bind socket", result); break;
  51. case 7 : LOG_E("%d : TCP server is already listening", result); break;
  52. case 8 : LOG_E("%d : Busy", result); break;
  53. case 9 : LOG_E("%d : Sockets opened", result); break;
  54. case 10 : LOG_E("%d : Timeout ", result); break;
  55. case 11 : LOG_E("%d : DNS parse failed for AT+CIPOPEN", result); break;
  56. case 255 : LOG_E("%d : Unknown error", result); break;
  57. }
  58. }
  59. static int sim76xx_socket_event_send(struct at_device *device, uint32_t event)
  60. {
  61. return (int)rt_event_send(device->socket_event, event);
  62. }
  63. static int sim76xx_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  64. {
  65. int result = RT_EOK;
  66. rt_uint32_t recved;
  67. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  68. if (result != RT_EOK)
  69. {
  70. return -RT_ETIMEOUT;
  71. }
  72. return recved;
  73. }
  74. /**
  75. * close socket by AT commands.
  76. *
  77. * @param current socket
  78. *
  79. * @return 0: close socket success
  80. * -1: send AT commands error
  81. * -2: wait socket event timeout
  82. * -5: no memory
  83. */
  84. static int sim76xx_socket_close(struct at_socket *socket)
  85. {
  86. int result = RT_EOK;
  87. int lnk_stat[10] = {0};
  88. at_response_t resp = RT_NULL;
  89. int device_socket = (int) socket->user_data;
  90. struct at_device *device = (struct at_device *) socket->device;
  91. resp = at_create_resp(64, 0, RT_TICK_PER_SECOND);
  92. if (resp == RT_NULL)
  93. {
  94. LOG_E("no memory for resp create.");
  95. return -RT_ENOMEM;
  96. }
  97. rt_thread_mdelay(100);
  98. /* check socket link_state */
  99. if (at_obj_exec_cmd(device->client, resp, "AT+CIPCLOSE?") < 0)
  100. {
  101. result = -RT_ERROR;
  102. goto __exit;
  103. }
  104. if (at_resp_parse_line_args_by_kw(resp, "+CIPCLOSE:", "+CIPCLOSE: %d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
  105. &lnk_stat[0], &lnk_stat[1], &lnk_stat[2], &lnk_stat[3], &lnk_stat[4],
  106. &lnk_stat[5], &lnk_stat[6], &lnk_stat[7], &lnk_stat[8], &lnk_stat[9]) < 0)
  107. {
  108. result = -RT_ERROR;
  109. goto __exit;
  110. }
  111. if (lnk_stat[device_socket])
  112. {
  113. #define CLOSE_COUNTS 5
  114. int i = 0;
  115. /* close tcp or udp socket if connected */
  116. if (at_obj_exec_cmd(device->client, resp, "AT+CIPCLOSE=%d", device_socket) < 0)
  117. {
  118. result = -RT_ERROR;
  119. goto __exit;
  120. }
  121. /* wait sim76xx device sockt closed */
  122. for (i = 0; i < CLOSE_COUNTS; i++)
  123. {
  124. if (at_obj_exec_cmd(device->client, resp, "AT+CIPCLOSE?") < 0)
  125. {
  126. result = -RT_ERROR;
  127. goto __exit;
  128. }
  129. at_resp_parse_line_args_by_kw(resp, "+CIPCLOSE:", "+CIPCLOSE: %d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
  130. &lnk_stat[0], &lnk_stat[1], &lnk_stat[2], &lnk_stat[3], &lnk_stat[4],
  131. &lnk_stat[5], &lnk_stat[6], &lnk_stat[7], &lnk_stat[8], &lnk_stat[9]);
  132. if (lnk_stat[device_socket] == 0)
  133. {
  134. break;
  135. }
  136. rt_thread_delay(1000);
  137. }
  138. }
  139. __exit:
  140. if (resp)
  141. {
  142. at_delete_resp(resp);
  143. }
  144. return result;
  145. }
  146. /**
  147. * create TCP/UDP client or server connect by AT commands.
  148. *
  149. * @param socket current socket
  150. * @param ip server or client IP address
  151. * @param port server or client port
  152. * @param type connect socket type(tcp, udp)
  153. * @param is_client connection is client
  154. *
  155. * @return 0: connect success
  156. * -1: connect failed, send commands error or type error
  157. * -2: wait socket event timeout
  158. * -5: no memory
  159. */
  160. static int sim76xx_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  161. {
  162. int result = RT_EOK, event_result = 0;
  163. rt_bool_t retryed = RT_FALSE;
  164. at_response_t resp = RT_NULL;
  165. int device_socket = (int) socket->user_data;
  166. struct at_device *device = (struct at_device *) socket->device;
  167. rt_mutex_t lock = at_device_get_client_lock(device);
  168. RT_ASSERT(ip);
  169. RT_ASSERT(port >= 0);
  170. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  171. if (resp == RT_NULL)
  172. {
  173. LOG_E("no memory for resp create.");
  174. return -RT_ENOMEM;
  175. }
  176. rt_mutex_take(lock, RT_WAITING_FOREVER);
  177. /* check and close current socket */
  178. sim76xx_socket_close(socket);
  179. __retry:
  180. if (is_client)
  181. {
  182. switch (type)
  183. {
  184. case AT_SOCKET_TCP:
  185. /* send AT commands to connect TCP server */
  186. if (at_obj_exec_cmd(device->client, resp, "AT+CIPOPEN=%d,\"TCP\",\"%s\",%d", device_socket, ip, port) < 0)
  187. {
  188. result = -RT_ERROR;
  189. }
  190. break;
  191. case AT_SOCKET_UDP:
  192. if (at_obj_exec_cmd(device->client, resp, "AT+CIPOPEN=%d,\"UDP\",,,%d", device_socket, port) < 0)
  193. {
  194. result = -RT_ERROR;
  195. }
  196. rt_strncpy(udp_ipstr[device_socket], ip, SIM76XX_IPADDR_LEN);
  197. udp_port[device_socket] = port;
  198. break;
  199. default:
  200. LOG_E("not supported connect type : %d.", type);
  201. result = -RT_ERROR;
  202. goto __exit;
  203. }
  204. }
  205. /* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use.*/
  206. if (sim76xx_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  207. {
  208. LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
  209. result = -RT_ETIMEOUT;
  210. goto __exit;
  211. }
  212. /* waiting OK or failed result */
  213. event_result = sim76xx_socket_event_recv(device, SIM76XX_EVENT_CONN_OK | SIM76XX_EVENT_CONN_FAIL,
  214. 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  215. if (event_result < 0)
  216. {
  217. LOG_E("%s device socket(%d) wait connect OK|FAIL timeout.", device->name, device_socket);
  218. result = -RT_ETIMEOUT;
  219. goto __exit;
  220. }
  221. /* check result */
  222. if (event_result & SIM76XX_EVENT_CONN_FAIL)
  223. {
  224. if (retryed == RT_FALSE)
  225. {
  226. LOG_D("socket(%d) connect failed, the socket was not be closed and now will connect retry.", device_socket);
  227. if (sim76xx_socket_close(socket) < 0)
  228. {
  229. result = -RT_ERROR;
  230. goto __exit;
  231. }
  232. retryed = RT_TRUE;
  233. goto __retry;
  234. }
  235. LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
  236. result = -RT_ERROR;
  237. goto __exit;
  238. }
  239. __exit:
  240. rt_mutex_release(lock);
  241. if (resp)
  242. {
  243. at_delete_resp(resp);
  244. }
  245. return result;
  246. }
  247. /**
  248. * send data to server or client by AT commands.
  249. *
  250. * @param socket current socket
  251. * @param buff send buffer
  252. * @param bfsz send buffer size
  253. * @param type connect socket type(tcp, udp)
  254. *
  255. * @return >=0: the size of send success
  256. * -1: send AT commands error or send data error
  257. * -2: waited socket event timeout
  258. * -5: no memory
  259. */
  260. static int sim76xx_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  261. {
  262. int result = RT_EOK;
  263. int event_result = 0;
  264. size_t cur_pkt_size = 0, sent_size = 0;
  265. at_response_t resp = RT_NULL;
  266. int device_socket = (int) socket->user_data;
  267. struct at_device *device = (struct at_device *) socket->device;
  268. struct at_device_sim76xx *sim76xx = (struct at_device_sim76xx *) device->user_data;
  269. rt_mutex_t lock = at_device_get_client_lock(device);
  270. RT_ASSERT(buff);
  271. RT_ASSERT(bfsz > 0);
  272. resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
  273. if (resp == RT_NULL)
  274. {
  275. LOG_E("no memory for resp create.");
  276. return -RT_ENOMEM;
  277. }
  278. rt_mutex_take(lock, RT_WAITING_FOREVER);
  279. /* set current socket for send URC event */
  280. sim76xx->user_data = (void *) device_socket;
  281. /* set AT client end sign to deal with '>' sign.*/
  282. at_obj_set_end_sign(device->client, '>');
  283. while (sent_size < bfsz)
  284. {
  285. if (bfsz - sent_size < SIM76XX_MODULE_SEND_MAX_SIZE)
  286. {
  287. cur_pkt_size = bfsz - sent_size;
  288. }
  289. else
  290. {
  291. cur_pkt_size = SIM76XX_MODULE_SEND_MAX_SIZE;
  292. }
  293. switch (socket->type)
  294. {
  295. case AT_SOCKET_TCP:
  296. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
  297. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
  298. {
  299. result = -RT_ERROR;
  300. goto __exit;
  301. }
  302. break;
  303. case AT_SOCKET_UDP:
  304. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
  305. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSEND=%d,%d,\"%s\",%d",
  306. device_socket, cur_pkt_size, udp_ipstr[device_socket], udp_port[device_socket]) < 0)
  307. {
  308. result = -RT_ERROR;
  309. goto __exit;
  310. }
  311. break;
  312. default:
  313. LOG_E("input socket type(%d) error.", socket->type);
  314. break;
  315. }
  316. /* send the real data to server or client */
  317. result = (int)at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
  318. if (result == 0)
  319. {
  320. result = -RT_ERROR;
  321. goto __exit;
  322. }
  323. /* waiting result event from AT URC */
  324. if (sim76xx_socket_event_recv(device, SET_EVENT(device_socket, 0), 5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  325. {
  326. LOG_E("%s device socket (%d) wait send result timeout.", device->name, device_socket);
  327. result = -RT_ETIMEOUT;
  328. goto __exit;
  329. }
  330. /* waiting OK or failed result */
  331. event_result = sim76xx_socket_event_recv(device, SIM76XX_EVENT_SEND_OK | SIM76XX_EVENT_SEND_FAIL,
  332. 5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  333. if (event_result < 0)
  334. {
  335. LOG_E("%s device socket(%d) wait send OK|FAIL timeout.", device->name, device_socket);
  336. result = -RT_ETIMEOUT;
  337. goto __exit;
  338. }
  339. /* check result */
  340. if (event_result & SIM76XX_EVENT_SEND_FAIL)
  341. {
  342. LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
  343. result = -RT_ERROR;
  344. goto __exit;
  345. }
  346. sent_size += cur_pkt_size;
  347. }
  348. __exit:
  349. /* reset the end sign for data */
  350. at_obj_set_end_sign(device->client, 0);
  351. rt_mutex_release(lock);
  352. if (resp)
  353. {
  354. at_delete_resp(resp);
  355. }
  356. return result > 0 ? sent_size : result;
  357. }
  358. /**
  359. * domain resolve by AT commands.
  360. *
  361. * @param name domain name
  362. * @param ip parsed IP address, it's length must be 16
  363. *
  364. * @return 0: domain resolve success
  365. * -2: wait socket event timeout
  366. * -5: no memory
  367. */
  368. static int sim76xx_domain_resolve(const char *name, char ip[16])
  369. {
  370. #define RESOLVE_RETRY 5
  371. int i, result = RT_EOK;
  372. char domain[32] = {0};
  373. char domain_ip[16] = {0};
  374. at_response_t resp = RT_NULL;
  375. struct at_device *device = RT_NULL;
  376. RT_ASSERT(name);
  377. RT_ASSERT(ip);
  378. device = at_device_get_first_initialized();
  379. if (device == RT_NULL)
  380. {
  381. LOG_E("get first init device failed.");
  382. return -RT_ERROR;
  383. }
  384. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  385. if (resp == RT_NULL)
  386. {
  387. LOG_E("no memory for resp create.");
  388. return -RT_ENOMEM;
  389. }
  390. for (i = 0; i < RESOLVE_RETRY; i++)
  391. {
  392. if (at_obj_exec_cmd(device->client, resp, "AT+CDNSGIP=\"%s\"", name) < 0)
  393. {
  394. rt_thread_mdelay(200);
  395. /* resolve failed, maybe receive an URC CRLF */
  396. continue;
  397. }
  398. /* parse the third line of response data, get the IP address */
  399. /* +CDNSGIP: 1,"www.baidu.com","14.215.177.39" */
  400. if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP:", "+CDNSGIP: 1,%[^,],\"%[^\"]", domain, domain_ip) < 0)
  401. {
  402. rt_thread_mdelay(200);
  403. /* resolve failed, maybe receive an URC CRLF */
  404. continue;
  405. }
  406. if (rt_strlen(domain_ip) < 8)
  407. {
  408. rt_thread_mdelay(200);
  409. /* resolve failed, maybe receive an URC CRLF */
  410. continue;
  411. }
  412. else
  413. {
  414. rt_strncpy(ip, domain_ip, 15);
  415. ip[15] = '\0';
  416. break;
  417. }
  418. }
  419. if (i == RESOLVE_RETRY)
  420. {
  421. result = -RT_ERROR;
  422. }
  423. if (resp)
  424. {
  425. at_delete_resp(resp);
  426. }
  427. return result;
  428. }
  429. /**
  430. * set AT socket event notice callback
  431. *
  432. * @param event notice event
  433. * @param cb notice callback
  434. */
  435. static void sim76xx_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  436. {
  437. if (event < (sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1])))
  438. {
  439. at_evt_cb_set[event] = cb;
  440. }
  441. }
  442. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  443. {
  444. int device_socket = 0, rqst_size, cnf_size;
  445. struct at_device *device = RT_NULL;
  446. char *client_name = client->device->parent.name;
  447. RT_ASSERT(data && size);
  448. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  449. if (device == RT_NULL)
  450. {
  451. LOG_E("get device(%s) failed.");
  452. return;
  453. }
  454. rt_sscanf(data, "+CIPSEND: %d,%d,%d", &device_socket, &rqst_size, &cnf_size);
  455. sim76xx_socket_event_send(device, SET_EVENT(device_socket, SIM76XX_EVENT_SEND_OK));
  456. }
  457. static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
  458. {
  459. int device_socket = 0, result = 0;
  460. struct at_device *device = RT_NULL;
  461. char *client_name = client->device->parent.name;
  462. RT_ASSERT(data && size);
  463. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  464. if (device == RT_NULL)
  465. {
  466. LOG_E("get device(%s) failed.", client_name);
  467. return;
  468. }
  469. rt_sscanf(data, "+CIPOPEN: %d,%d", &device_socket, &result);
  470. if (result == 0)
  471. {
  472. sim76xx_socket_event_send(device, SET_EVENT(device_socket, SIM76XX_EVENT_CONN_OK));
  473. }
  474. else
  475. {
  476. at_tcp_ip_errcode_parse(result);
  477. sim76xx_socket_event_send(device, SET_EVENT(device_socket, SIM76XX_EVENT_CONN_FAIL));
  478. }
  479. }
  480. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  481. {
  482. int device_socket = 0, reason = 0;
  483. struct at_socket *socket = RT_NULL;
  484. struct at_device *device = RT_NULL;
  485. char *client_name = client->device->parent.name;
  486. RT_ASSERT(data && size);
  487. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  488. if (device == RT_NULL)
  489. {
  490. LOG_E("get device(%s) failed.", device->name);
  491. return;
  492. }
  493. rt_sscanf(data, "+IPCLOSE %d,%d", &device_socket, &reason);
  494. /* get AT socket object by device socket descriptor */
  495. socket = &(device->sockets[device_socket]);
  496. /* notice the socket is disconnect by remote */
  497. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  498. {
  499. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  500. }
  501. }
  502. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  503. {
  504. rt_size_t bfsz = 0, temp_size = 0;
  505. rt_int32_t timeout;
  506. char *recv_buf = RT_NULL, temp[8] = {0};
  507. int device_socket = 0;
  508. struct at_socket *socket = RT_NULL;
  509. struct at_device *device = RT_NULL;
  510. struct at_device_sim76xx *sim76xx = RT_NULL;
  511. char *client_name = client->device->parent.name;
  512. RT_ASSERT(data && size);
  513. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  514. if (device == RT_NULL)
  515. {
  516. LOG_E("get device(%s) failed.", client_name);
  517. return;
  518. }
  519. sim76xx = (struct at_device_sim76xx *) device->user_data;
  520. device_socket = (int) sim76xx->user_data;
  521. /* get the current socket and receive buffer size by receive data */
  522. rt_sscanf(data, "+IPD%d:", (int *)&bfsz);
  523. /* get receive timeout by receive buffer length */
  524. timeout = bfsz * 10;
  525. if (bfsz == 0)
  526. return;
  527. recv_buf = (char *) rt_calloc(1, bfsz);
  528. if (recv_buf == RT_NULL)
  529. {
  530. LOG_E("no memory for receive buffer(%d).", bfsz);
  531. /* read and clean the coming data */
  532. while (temp_size < bfsz)
  533. {
  534. if (bfsz - temp_size > sizeof(temp))
  535. {
  536. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  537. }
  538. else
  539. {
  540. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  541. }
  542. temp_size += sizeof(temp);
  543. }
  544. return;
  545. }
  546. /* sync receive data */
  547. if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
  548. {
  549. LOG_E("%s device receive size(%d) data failed.", device->name, bfsz);
  550. rt_free(recv_buf);
  551. return;
  552. }
  553. /* get AT socket object by device socket descriptor */
  554. socket = &(device->sockets[device_socket]);
  555. /* notice the receive buffer and buffer size */
  556. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  557. {
  558. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  559. }
  560. }
  561. static struct at_urc urc_table[] =
  562. {
  563. {"+CIPSEND:", "\r\n", urc_send_func},
  564. {"+CIPOPEN:", "\r\n", urc_connect_func},
  565. {"+IPCLOSE", "\r\n", urc_close_func},
  566. {"+IPD", "\r\n", urc_recv_func},
  567. };
  568. static const struct at_socket_ops sim76xx_socket_ops =
  569. {
  570. sim76xx_socket_connect,
  571. sim76xx_socket_close,
  572. sim76xx_socket_send,
  573. sim76xx_domain_resolve,
  574. sim76xx_socket_set_event_cb,
  575. #if defined(AT_SW_VERSION_NUM) && AT_SW_VERSION_NUM > 0x10300
  576. RT_NULL,
  577. #endif
  578. };
  579. /* initialize sim76xx device network URC feature */
  580. int sim76xx_socket_init(struct at_device *device)
  581. {
  582. RT_ASSERT(device);
  583. /* register URC data execution function */
  584. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  585. return RT_EOK;
  586. }
  587. /* resgiter sim76xx device socket operations */
  588. int sim76xx_socket_class_register(struct at_device_class *class)
  589. {
  590. RT_ASSERT(class);
  591. class->socket_num = AT_DEVICE_SIM76XX_SOCKETS_NUM;
  592. class->socket_ops = &sim76xx_socket_ops;
  593. return RT_EOK;
  594. }
  595. #endif /* AT_DEVICE_SIM76XX */