at_socket_esp8266.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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-06-20 chenyong first version
  9. * 2019-05-09 chenyong multi AT socket client support
  10. * 2022-06-02 xiangxistu add AT server mode
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <at_device_esp8266.h>
  15. #define LOG_TAG "at.skt.esp"
  16. #include <at_log.h>
  17. #if defined(AT_DEVICE_USING_ESP8266) && defined(AT_USING_SOCKET)
  18. #define ESP8266_MODULE_SERVER_SUPPORT_NUM 1
  19. #define ESP8266_MODULE_SEND_MAX_SIZE 2048
  20. /* set real event by current socket and current state */
  21. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  22. /* AT socket event type */
  23. #define ESP8266_EVENT_CONN_OK (1L << 0)
  24. #define ESP8266_EVENT_SEND_OK (1L << 1)
  25. #define ESP8266_EVENT_RECV_OK (1L << 2)
  26. #define ESP8266_EVNET_CLOSE_OK (1L << 3)
  27. #define ESP8266_EVENT_CONN_FAIL (1L << 4)
  28. #define ESP8266_EVENT_SEND_FAIL (1L << 5)
  29. static at_evt_cb_t at_evt_cb_set[] = {
  30. [AT_SOCKET_EVT_RECV] = NULL,
  31. [AT_SOCKET_EVT_CLOSED] = NULL,
  32. #ifdef AT_USING_SOCKET_SERVER
  33. [AT_SOCKET_EVT_CONNECTED] = NULL,
  34. #endif
  35. };
  36. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size);
  37. static void urc_send_bfsz_func(struct at_client *client, const char *data, rt_size_t size);
  38. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size);
  39. #ifdef AT_USING_SOCKET_SERVER
  40. static void urc_connected_func(struct at_client *client, const char *data, rt_size_t size);
  41. #endif
  42. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size);
  43. static const struct at_urc urc_table[] =
  44. {
  45. {"SEND OK", "\r\n", urc_send_func},
  46. {"SEND FAIL", "\r\n", urc_send_func},
  47. {"Recv", "bytes\r\n", urc_send_bfsz_func},
  48. {"", ",CLOSED\r\n", urc_close_func},
  49. {"+IPD", ":", urc_recv_func},
  50. };
  51. #ifdef AT_USING_SOCKET_SERVER
  52. static const struct at_urc urc_table_with_server[] =
  53. {
  54. {"SEND OK", "\r\n", urc_send_func},
  55. {"SEND FAIL", "\r\n", urc_send_func},
  56. {"Recv", "bytes\r\n", urc_send_bfsz_func},
  57. {"", ",CONNECT\r\n", urc_connected_func},
  58. {"", ",CLOSED\r\n", urc_close_func},
  59. {"+IPD", ":", urc_recv_func},
  60. };
  61. #endif
  62. #ifdef AT_USING_SOCKET_SERVER
  63. static int esp8266_server_number = 0;
  64. #endif
  65. static int esp8266_socket_event_send(struct at_device *device, uint32_t event)
  66. {
  67. return (int) rt_event_send(device->socket_event, event);
  68. }
  69. static int esp8266_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  70. {
  71. int result = 0;
  72. rt_uint32_t recved;
  73. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  74. if (result != RT_EOK)
  75. {
  76. return -RT_ETIMEOUT;
  77. }
  78. return recved;
  79. }
  80. /**
  81. * close socket by AT commands.
  82. *
  83. * @param current socket
  84. *
  85. * @return 0: close socket success
  86. * -1: send AT commands error
  87. * -2: wait socket event timeout
  88. * -5: no memory
  89. */
  90. static int esp8266_socket_close(struct at_socket *socket)
  91. {
  92. int result = RT_EOK;
  93. at_response_t resp = RT_NULL;
  94. int device_socket = (int) socket->user_data;
  95. struct at_device *device = (struct at_device *) socket->device;
  96. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  97. if (resp == RT_NULL)
  98. {
  99. LOG_E("no memory for resp create.");
  100. return -RT_ENOMEM;
  101. }
  102. result = at_obj_exec_cmd(device->client, resp, "AT+CIPCLOSE=%d", device_socket);
  103. if (resp)
  104. {
  105. at_delete_resp(resp);
  106. }
  107. return result;
  108. }
  109. /**
  110. * create TCP/UDP client or server connect by AT commands.
  111. *
  112. * @param socket current socket
  113. * @param ip server or client IP address
  114. * @param port server or client port
  115. * @param type connect socket type(tcp, udp)
  116. * @param is_client connection is client
  117. *
  118. * @return 0: connect success
  119. * -1: connect failed, send commands error or type error
  120. * -2: wait socket event timeout
  121. * -5: no memory
  122. */
  123. static int esp8266_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  124. {
  125. int result = RT_EOK;
  126. rt_bool_t retryed = RT_FALSE;
  127. at_response_t resp = RT_NULL;
  128. int device_socket = (int) socket->user_data;
  129. struct at_device *device = (struct at_device *) socket->device;
  130. RT_ASSERT(ip);
  131. RT_ASSERT(port >= 0);
  132. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  133. if (resp == RT_NULL)
  134. {
  135. LOG_E("no memory for resp create.");
  136. return -RT_ENOMEM;
  137. }
  138. __retry:
  139. if (is_client)
  140. {
  141. switch (type)
  142. {
  143. case AT_SOCKET_TCP:
  144. /* send AT commands to connect TCP server */
  145. if (at_obj_exec_cmd(device->client, resp,
  146. "AT+CIPSTART=%d,\"TCP\",\"%s\",%d,60", device_socket, ip, port) < 0)
  147. {
  148. result = -RT_ERROR;
  149. }
  150. break;
  151. case AT_SOCKET_UDP:
  152. if (at_obj_exec_cmd(device->client, resp,
  153. "AT+CIPSTART=%d,\"UDP\",\"%s\",%d", device_socket, ip, port) < 0)
  154. {
  155. result = -RT_ERROR;
  156. }
  157. break;
  158. default:
  159. LOG_E("not supported connect type %d.", type);
  160. result = -RT_ERROR;
  161. goto __exit;
  162. }
  163. }
  164. if (result != RT_EOK && retryed == RT_FALSE)
  165. {
  166. LOG_D("%s device socket (%d) connect failed, the socket was not be closed and now will connect retry.",
  167. device->name, device_socket);
  168. if (esp8266_socket_close(socket) < 0)
  169. {
  170. goto __exit;
  171. }
  172. retryed = RT_TRUE;
  173. result = RT_EOK;
  174. goto __retry;
  175. }
  176. __exit:
  177. if (resp)
  178. {
  179. at_delete_resp(resp);
  180. }
  181. return result;
  182. }
  183. #ifdef AT_USING_SOCKET_SERVER
  184. /**
  185. * create TCP/UDP or server connect by AT commands.
  186. *
  187. * @param socket current socket
  188. * @param backlog waiting to handdle work, useless in "at mode"
  189. *
  190. * @return 0: connect success
  191. * -1: connect failed, send commands error or type error
  192. * -2: wait socket event timeout
  193. * -5: no memory
  194. */
  195. int esp8266_socket_listen(struct at_socket *socket, int backlog)
  196. {
  197. int result = RT_EOK;
  198. at_response_t resp = RT_NULL;
  199. struct at_device *device = RT_NULL;
  200. int listen_port;
  201. listen_port = (int)socket->listen.port;
  202. device = at_device_get_first_initialized();
  203. if (device == RT_NULL)
  204. {
  205. LOG_E("get first init device failed.");
  206. return -RT_ERROR;
  207. }
  208. resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
  209. if (resp == RT_NULL)
  210. {
  211. LOG_E("no memory for resp create.");
  212. return -RT_ENOMEM;
  213. }
  214. if(esp8266_server_number >= ESP8266_MODULE_SERVER_SUPPORT_NUM)
  215. {
  216. LOG_E("no memory for server to listen(%05d).", socket->listen.port);
  217. return -RT_ENOMEM;
  218. }
  219. esp8266_server_number++;
  220. /* AT+CIPSERVER=1,<port> */
  221. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSERVER=1,%d", listen_port) < 0)
  222. {
  223. result = -RT_ERROR;
  224. goto __exit;
  225. }
  226. at_obj_set_urc_table(device->client, urc_table_with_server, sizeof(urc_table_with_server) / sizeof(urc_table_with_server[0]));
  227. __exit:
  228. if (resp)
  229. {
  230. at_delete_resp(resp);
  231. }
  232. return result;
  233. }
  234. #endif
  235. /**
  236. * send data to server or client by AT commands.
  237. *
  238. * @param socket current socket
  239. * @param buff send buffer
  240. * @param bfsz send buffer size
  241. * @param type connect socket type(tcp, udp)
  242. *
  243. * @return >=0: the size of send success
  244. * -1: send AT commands error or send data error
  245. * -2: waited socket event timeout
  246. * -5: no memory
  247. */
  248. static int esp8266_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  249. {
  250. int result = RT_EOK;
  251. int event_result = 0;
  252. size_t cur_pkt_size = 0, sent_size = 0;
  253. at_response_t resp = RT_NULL;
  254. int device_socket = (int) socket->user_data;
  255. struct at_device *device = (struct at_device *) socket->device;
  256. struct at_device_esp8266 *esp8266 = (struct at_device_esp8266 *) device->user_data;
  257. rt_mutex_t lock = device->client->lock;
  258. RT_ASSERT(buff);
  259. RT_ASSERT(bfsz > 0);
  260. resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
  261. if (resp == RT_NULL)
  262. {
  263. LOG_E("no memory for resp create.");
  264. return -RT_ENOMEM;
  265. }
  266. rt_mutex_take(lock, RT_WAITING_FOREVER);
  267. /* set current socket for send URC event */
  268. esp8266->user_data = (void *) device_socket;
  269. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  270. /* set AT client end sign to deal with '>' sign */
  271. at_obj_set_end_sign(device->client, '>');
  272. while (sent_size < bfsz)
  273. {
  274. if (bfsz - sent_size < ESP8266_MODULE_SEND_MAX_SIZE)
  275. {
  276. cur_pkt_size = bfsz - sent_size;
  277. }
  278. else
  279. {
  280. cur_pkt_size = ESP8266_MODULE_SEND_MAX_SIZE;
  281. }
  282. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line */
  283. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
  284. {
  285. result = -RT_ERROR;
  286. goto __exit;
  287. }
  288. /* send the real data to server or client */
  289. result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
  290. if (result == 0)
  291. {
  292. result = -RT_ERROR;
  293. goto __exit;
  294. }
  295. /* waiting result event from AT URC */
  296. if (esp8266_socket_event_recv(device, SET_EVENT(device_socket, 0),
  297. 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  298. {
  299. LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
  300. result = -RT_ETIMEOUT;
  301. goto __exit;
  302. }
  303. /* waiting OK or failed result */
  304. event_result = esp8266_socket_event_recv(device, ESP8266_EVENT_SEND_OK | ESP8266_EVENT_SEND_FAIL,
  305. 5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  306. if (event_result < 0)
  307. {
  308. LOG_E("%s device socket(%d) wait connect OK|FAIL timeout.", device->name, device_socket);
  309. result = -RT_ETIMEOUT;
  310. goto __exit;
  311. }
  312. /* check result */
  313. if (event_result & ESP8266_EVENT_SEND_FAIL)
  314. {
  315. LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
  316. result = -RT_ERROR;
  317. goto __exit;
  318. }
  319. sent_size += cur_pkt_size;
  320. }
  321. __exit:
  322. /* reset the end sign for data */
  323. at_obj_set_end_sign(device->client, 0);
  324. rt_mutex_release(lock);
  325. if (resp)
  326. {
  327. at_delete_resp(resp);
  328. }
  329. #ifdef AT_USING_SOCKET_SERVER
  330. at_obj_set_urc_table(device->client, urc_table_with_server, sizeof(urc_table_with_server) / sizeof(urc_table_with_server[0]));
  331. #endif
  332. return result > 0 ? sent_size : result;
  333. }
  334. /**
  335. * domain resolve by AT commands.
  336. *
  337. * @param name domain name
  338. * @param ip parsed IP address, it's length must be 16
  339. *
  340. * @return 0: domain resolve success
  341. * -2: wait socket event timeout
  342. * -5: no memory
  343. */
  344. static int esp8266_domain_resolve(const char *name, char ip[16])
  345. {
  346. #define RESOLVE_RETRY 5
  347. int i, result = RT_EOK;
  348. char recv_ip[16] = { 0 };
  349. at_response_t resp = RT_NULL;
  350. struct at_device *device = RT_NULL;
  351. RT_ASSERT(name);
  352. RT_ASSERT(ip);
  353. device = at_device_get_first_initialized();
  354. if (device == RT_NULL)
  355. {
  356. LOG_E("get first init device failed.");
  357. return -RT_ERROR;
  358. }
  359. resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
  360. if (resp == RT_NULL)
  361. {
  362. LOG_E("no memory for resp create.");
  363. return -RT_ENOMEM;
  364. }
  365. for (i = 0; i < RESOLVE_RETRY; i++)
  366. {
  367. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=\"%s\"", name) < 0)
  368. {
  369. result = -RT_ERROR;
  370. goto __exit;
  371. }
  372. /* parse the third line of response data, get the IP address */
  373. if (at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", "+CIPDOMAIN:%s", recv_ip) < 0)
  374. {
  375. rt_thread_mdelay(100);
  376. /* resolve failed, maybe receive an URC CRLF */
  377. continue;
  378. }
  379. if (rt_strlen(recv_ip) < 8)
  380. {
  381. rt_thread_mdelay(100);
  382. /* resolve failed, maybe receive an URC CRLF */
  383. continue;
  384. }
  385. else
  386. {
  387. rt_strncpy(ip, recv_ip, 15);
  388. ip[15] = '\0';
  389. break;
  390. }
  391. }
  392. __exit:
  393. if (resp)
  394. {
  395. at_delete_resp(resp);
  396. }
  397. return result;
  398. }
  399. /**
  400. * set AT socket event notice callback
  401. *
  402. * @param event notice event
  403. * @param cb notice callback
  404. */
  405. static void esp8266_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  406. {
  407. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  408. {
  409. at_evt_cb_set[event] = cb;
  410. }
  411. }
  412. static const struct at_socket_ops esp8266_socket_ops =
  413. {
  414. esp8266_socket_connect,
  415. esp8266_socket_close,
  416. esp8266_socket_send,
  417. esp8266_domain_resolve,
  418. esp8266_socket_set_event_cb,
  419. #if defined(AT_SW_VERSION_NUM) && AT_SW_VERSION_NUM > 0x10300
  420. RT_NULL,
  421. #ifdef AT_USING_SOCKET_SERVER
  422. esp8266_socket_listen,
  423. #endif
  424. #endif
  425. };
  426. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  427. {
  428. int device_socket = 0;
  429. struct at_device *device = RT_NULL;
  430. struct at_device_esp8266 *esp8266 = RT_NULL;
  431. char *client_name = client->device->parent.name;
  432. RT_ASSERT(data && size);
  433. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  434. if (device == RT_NULL)
  435. {
  436. LOG_E("get device(%s) failed.", client_name);
  437. return;
  438. }
  439. esp8266 = (struct at_device_esp8266 *) device->user_data;
  440. device_socket = (int) esp8266->user_data;
  441. if (rt_strstr(data, "SEND OK"))
  442. {
  443. esp8266_socket_event_send(device, SET_EVENT(device_socket, ESP8266_EVENT_SEND_OK));
  444. }
  445. else if (rt_strstr(data, "SEND FAIL"))
  446. {
  447. esp8266_socket_event_send(device, SET_EVENT(device_socket, ESP8266_EVENT_SEND_FAIL));
  448. }
  449. }
  450. static void urc_send_bfsz_func(struct at_client *client, const char *data, rt_size_t size)
  451. {
  452. static int cur_send_bfsz = 0;
  453. RT_ASSERT(data && size);
  454. sscanf(data, "Recv %d bytes", &cur_send_bfsz);
  455. }
  456. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  457. {
  458. int index = 0;
  459. struct at_socket *socket = RT_NULL;
  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. sscanf(data, "%d,CLOSED", &index);
  470. #ifdef AT_USING_SOCKET_SERVER
  471. socket = at_get_base_socket(index);
  472. #else
  473. socket = &(device->sockets[index]);
  474. #endif
  475. /* notice the socket is disconnect by remote */
  476. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  477. {
  478. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  479. }
  480. }
  481. #ifdef AT_USING_SOCKET_SERVER
  482. static void urc_connected_func(struct at_client *client, const char *data, rt_size_t size)
  483. {
  484. int socket;
  485. struct at_device *device = RT_NULL;
  486. char socket_info[AT_SOCKET_INFO_LEN] = {0};
  487. char *client_name = client->device->parent.name;
  488. RT_ASSERT(data && size);
  489. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  490. if (device == RT_NULL)
  491. {
  492. LOG_E("get device(%s) failed.", client_name);
  493. return;
  494. }
  495. sscanf(data, "%d,CONNECT", &socket);
  496. rt_memset(&socket_info[0], 0, AT_SOCKET_INFO_LEN);
  497. rt_sprintf(&socket_info[0], "SOCKET:%d", socket);
  498. /* notice at socket to alloc a new socket */
  499. if (at_evt_cb_set[AT_SOCKET_EVT_CONNECTED])
  500. {
  501. at_evt_cb_set[AT_SOCKET_EVT_CONNECTED](RT_NULL, AT_SOCKET_EVT_CONNECTED, &socket_info[0], AT_SOCKET_INFO_LEN);
  502. }
  503. }
  504. #endif
  505. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  506. {
  507. int device_socket = 0;
  508. rt_int32_t timeout = 0;
  509. rt_size_t bfsz = 0, temp_size = 0;
  510. char *recv_buf = RT_NULL, temp[8] = {0};
  511. struct at_socket *socket = RT_NULL;
  512. struct at_device *device = RT_NULL;
  513. char *client_name = client->device->parent.name;
  514. RT_ASSERT(data && size);
  515. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  516. if (device == RT_NULL)
  517. {
  518. LOG_E("get device(%s) failed.", client_name);
  519. return;
  520. }
  521. /* get the at deveice socket and receive buffer size by receive data */
  522. sscanf(data, "+IPD,%d,%d:", &device_socket, (int *) &bfsz);
  523. /* set receive timeout by receive buffer length, not less than 10ms */
  524. timeout = bfsz > 10 ? bfsz : 10;
  525. if (device_socket < 0 || bfsz == 0)
  526. return;
  527. recv_buf = (char *) rt_calloc(1, bfsz);
  528. if (recv_buf == RT_NULL)
  529. {
  530. LOG_E("no memory 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. #ifdef AT_USING_SOCKET_SERVER
  555. socket = at_get_base_socket(device_socket);
  556. #else
  557. socket = &(device->sockets[device_socket]);
  558. #endif
  559. /* notice the receive buffer and buffer size */
  560. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  561. {
  562. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  563. }
  564. }
  565. int esp8266_socket_init(struct at_device *device)
  566. {
  567. RT_ASSERT(device);
  568. /* register URC data execution function */
  569. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  570. return RT_EOK;
  571. }
  572. int esp8266_socket_class_register(struct at_device_class *class)
  573. {
  574. RT_ASSERT(class);
  575. class->socket_num = AT_DEVICE_ESP8266_SOCKETS_NUM;
  576. class->socket_ops = &esp8266_socket_ops;
  577. return RT_EOK;
  578. }
  579. #endif /* AT_DEVICE_USING_ESP8266 && AT_USING_SOCKET */