at_socket_sim76xx.c 21 KB

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