at_socket_esp8266.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * File : at_socket_esp8266.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-06-20 chenyong first version
  23. */
  24. #include <at.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <rtthread.h>
  28. #include <sys/socket.h>
  29. #include <at_socket.h>
  30. #if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10200
  31. #error "This AT Client version is older, please check and update latest AT Client!"
  32. #endif
  33. #define LOG_TAG "at.esp8266"
  34. #include <at_log.h>
  35. #ifdef AT_DEVICE_ESP8266
  36. #define ESP8266_MODULE_SEND_MAX_SIZE 2048
  37. #define ESP8266_WAIT_CONNECT_TIME 5000
  38. #define ESP8266_THREAD_STACK_SIZE 1024
  39. #define ESP8266_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  40. /* set real event by current socket and current state */
  41. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  42. /* AT socket event type */
  43. #define ESP8266_EVENT_CONN_OK (1L << 0)
  44. #define ESP8266_EVENT_SEND_OK (1L << 1)
  45. #define ESP8266_EVENT_RECV_OK (1L << 2)
  46. #define ESP8266_EVNET_CLOSE_OK (1L << 3)
  47. #define ESP8266_EVENT_CONN_FAIL (1L << 4)
  48. #define ESP8266_EVENT_SEND_FAIL (1L << 5)
  49. static int cur_socket;
  50. static int cur_send_bfsz;
  51. static rt_event_t at_socket_event;
  52. static rt_mutex_t at_event_lock;
  53. static at_evt_cb_t at_evt_cb_set[] = {
  54. [AT_SOCKET_EVT_RECV] = NULL,
  55. [AT_SOCKET_EVT_CLOSED] = NULL,
  56. };
  57. static int at_socket_event_send(uint32_t event)
  58. {
  59. return (int) rt_event_send(at_socket_event, event);
  60. }
  61. static int at_socket_event_recv(uint32_t event, uint32_t timeout, rt_uint8_t option)
  62. {
  63. int result = 0;
  64. rt_uint32_t recved;
  65. result = rt_event_recv(at_socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  66. if (result != RT_EOK)
  67. {
  68. return -RT_ETIMEOUT;
  69. }
  70. return recved;
  71. }
  72. /**
  73. * close socket by AT commands.
  74. *
  75. * @param current socket
  76. *
  77. * @return 0: close socket success
  78. * -1: send AT commands error
  79. * -2: wait socket event timeout
  80. * -5: no memory
  81. */
  82. static int esp8266_socket_close(int socket)
  83. {
  84. at_response_t resp = RT_NULL;
  85. int result = RT_EOK;
  86. resp = at_create_resp(64, 0, rt_tick_from_millisecond(5000));
  87. if (!resp)
  88. {
  89. LOG_E("No memory for response structure!");
  90. return -RT_ENOMEM;
  91. }
  92. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  93. if (at_exec_cmd(resp, "AT+CIPCLOSE=%d", socket) < 0)
  94. {
  95. result = -RT_ERROR;
  96. goto __exit;
  97. }
  98. __exit:
  99. rt_mutex_release(at_event_lock);
  100. if (resp)
  101. {
  102. at_delete_resp(resp);
  103. }
  104. return result;
  105. }
  106. /**
  107. * create TCP/UDP client or server connect by AT commands.
  108. *
  109. * @param socket current socket
  110. * @param ip server or client IP address
  111. * @param port server or client port
  112. * @param type connect socket type(tcp, udp)
  113. * @param is_client connection is client
  114. *
  115. * @return 0: connect success
  116. * -1: connect failed, send commands error or type error
  117. * -2: wait socket event timeout
  118. * -5: no memory
  119. */
  120. static int esp8266_socket_connect(int socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  121. {
  122. at_response_t resp = RT_NULL;
  123. int result = RT_EOK;
  124. rt_bool_t retryed = RT_FALSE;
  125. RT_ASSERT(ip);
  126. RT_ASSERT(port >= 0);
  127. resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000));
  128. if (!resp)
  129. {
  130. LOG_E("No memory for response structure!");
  131. return -RT_ENOMEM;
  132. }
  133. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  134. __retry:
  135. if (is_client)
  136. {
  137. switch (type)
  138. {
  139. case AT_SOCKET_TCP:
  140. /* send AT commands to connect TCP server */
  141. if (at_exec_cmd(resp, "AT+CIPSTART=%d,\"TCP\",\"%s\",%d,60", socket, ip, port) < 0)
  142. {
  143. result = -RT_ERROR;
  144. }
  145. break;
  146. case AT_SOCKET_UDP:
  147. if (at_exec_cmd(resp, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d", socket, ip, port) < 0)
  148. {
  149. result = -RT_ERROR;
  150. }
  151. break;
  152. default:
  153. LOG_E("Not supported connect type : %d.", type);
  154. result = -RT_ERROR;
  155. goto __exit;
  156. }
  157. }
  158. if (result != RT_EOK && !retryed)
  159. {
  160. LOG_D("socket (%d) connect failed, maybe the socket was not be closed at the last time and now will retry.", socket);
  161. if (esp8266_socket_close(socket) < 0)
  162. {
  163. goto __exit;
  164. }
  165. retryed = RT_TRUE;
  166. result = RT_EOK;
  167. goto __retry;
  168. }
  169. __exit:
  170. rt_mutex_release(at_event_lock);
  171. if (resp)
  172. {
  173. at_delete_resp(resp);
  174. }
  175. return result;
  176. }
  177. /**
  178. * send data to server or client by AT commands.
  179. *
  180. * @param socket current socket
  181. * @param buff send buffer
  182. * @param bfsz send buffer size
  183. * @param type connect socket type(tcp, udp)
  184. *
  185. * @return >=0: the size of send success
  186. * -1: send AT commands error or send data error
  187. * -2: waited socket event timeout
  188. * -5: no memory
  189. */
  190. static int esp8266_socket_send(int socket, const char *buff, size_t bfsz, enum at_socket_type type)
  191. {
  192. int result = RT_EOK;
  193. int event_result = 0;
  194. at_response_t resp = RT_NULL;
  195. size_t cur_pkt_size = 0, sent_size = 0;
  196. RT_ASSERT(buff);
  197. RT_ASSERT(bfsz > 0);
  198. resp = at_create_resp(128, 2, rt_tick_from_millisecond(5000));
  199. if (!resp)
  200. {
  201. LOG_E("No memory for response structure!");
  202. return -RT_ENOMEM;
  203. }
  204. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  205. /* set current socket for send URC event */
  206. cur_socket = socket;
  207. /* set AT client end sign to deal with '>' sign.*/
  208. at_set_end_sign('>');
  209. while (sent_size < bfsz)
  210. {
  211. if (bfsz - sent_size < ESP8266_MODULE_SEND_MAX_SIZE)
  212. {
  213. cur_pkt_size = bfsz - sent_size;
  214. }
  215. else
  216. {
  217. cur_pkt_size = ESP8266_MODULE_SEND_MAX_SIZE;
  218. }
  219. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
  220. if (at_exec_cmd(resp, "AT+CIPSEND=%d,%d", socket, cur_pkt_size) < 0)
  221. {
  222. result = -RT_ERROR;
  223. goto __exit;
  224. }
  225. /* send the real data to server or client */
  226. result = (int) at_client_send(buff + sent_size, cur_pkt_size);
  227. if (result == 0)
  228. {
  229. result = -RT_ERROR;
  230. goto __exit;
  231. }
  232. /* waiting result event from AT URC */
  233. if (at_socket_event_recv(SET_EVENT(socket, 0), rt_tick_from_millisecond(5 * 1000), RT_EVENT_FLAG_OR) < 0)
  234. {
  235. LOG_E("socket (%d) send failed, wait connect result timeout.", socket);
  236. result = -RT_ETIMEOUT;
  237. goto __exit;
  238. }
  239. /* waiting OK or failed result */
  240. if ((event_result = at_socket_event_recv(ESP8266_EVENT_SEND_OK | ESP8266_EVENT_SEND_FAIL, rt_tick_from_millisecond(5 * 1000),
  241. RT_EVENT_FLAG_OR)) < 0)
  242. {
  243. LOG_E("socket (%d) send failed, wait connect OK|FAIL timeout.", socket);
  244. result = -RT_ETIMEOUT;
  245. goto __exit;
  246. }
  247. /* check result */
  248. if (event_result & ESP8266_EVENT_SEND_FAIL)
  249. {
  250. LOG_E("socket (%d) send failed, return failed.", socket);
  251. result = -RT_ERROR;
  252. goto __exit;
  253. }
  254. if (type == AT_SOCKET_TCP)
  255. {
  256. cur_pkt_size = cur_send_bfsz;
  257. }
  258. sent_size += cur_pkt_size;
  259. }
  260. __exit:
  261. /* reset the end sign for data */
  262. at_set_end_sign(0);
  263. rt_mutex_release(at_event_lock);
  264. if (resp)
  265. {
  266. at_delete_resp(resp);
  267. }
  268. return result;
  269. }
  270. /**
  271. * domain resolve by AT commands.
  272. *
  273. * @param name domain name
  274. * @param ip parsed IP address, it's length must be 16
  275. *
  276. * @return 0: domain resolve success
  277. * -2: wait socket event timeout
  278. * -5: no memory
  279. */
  280. static int esp8266_domain_resolve(const char *name, char ip[16])
  281. {
  282. #define RESOLVE_RETRY 5
  283. int i, result = RT_EOK;
  284. char recv_ip[16] = { 0 };
  285. at_response_t resp = RT_NULL;
  286. RT_ASSERT(name);
  287. RT_ASSERT(ip);
  288. resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000));
  289. if (!resp)
  290. {
  291. LOG_E("No memory for response structure!");
  292. return -RT_ENOMEM;
  293. }
  294. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  295. for(i = 0; i < RESOLVE_RETRY; i++)
  296. {
  297. if (at_exec_cmd(resp, "AT+CIPDOMAIN=\"%s\"", name) < 0)
  298. {
  299. result = -RT_ERROR;
  300. goto __exit;
  301. }
  302. /* parse the third line of response data, get the IP address */
  303. if(at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", "+CIPDOMAIN:%s", recv_ip) < 0)
  304. {
  305. rt_thread_delay(rt_tick_from_millisecond(100));
  306. /* resolve failed, maybe receive an URC CRLF */
  307. continue;
  308. }
  309. if (strlen(recv_ip) < 8)
  310. {
  311. rt_thread_delay(rt_tick_from_millisecond(100));
  312. /* resolve failed, maybe receive an URC CRLF */
  313. continue;
  314. }
  315. else
  316. {
  317. strncpy(ip, recv_ip, 15);
  318. ip[15] = '\0';
  319. break;
  320. }
  321. }
  322. __exit:
  323. rt_mutex_release(at_event_lock);
  324. if (resp)
  325. {
  326. at_delete_resp(resp);
  327. }
  328. return result;
  329. }
  330. /**
  331. * set AT socket event notice callback
  332. *
  333. * @param event notice event
  334. * @param cb notice callback
  335. */
  336. static void esp8266_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  337. {
  338. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  339. {
  340. at_evt_cb_set[event] = cb;
  341. }
  342. }
  343. static void urc_send_func(const char *data, rt_size_t size)
  344. {
  345. RT_ASSERT(data && size);
  346. if (strstr(data, "SEND OK"))
  347. {
  348. at_socket_event_send(SET_EVENT(cur_socket, ESP8266_EVENT_SEND_OK));
  349. }
  350. else if (strstr(data, "SEND FAIL"))
  351. {
  352. at_socket_event_send(SET_EVENT(cur_socket, ESP8266_EVENT_SEND_FAIL));
  353. }
  354. }
  355. static void urc_send_bfsz_func(const char *data, rt_size_t size)
  356. {
  357. int send_bfsz = 0;
  358. RT_ASSERT(data && size);
  359. sscanf(data, "Recv %d bytes", &send_bfsz);
  360. cur_send_bfsz = send_bfsz;
  361. }
  362. static void urc_close_func(const char *data, rt_size_t size)
  363. {
  364. int socket = 0;
  365. RT_ASSERT(data && size);
  366. sscanf(data, "%d,CLOSED", &socket);
  367. /* notice the socket is disconnect by remote */
  368. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  369. {
  370. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  371. }
  372. }
  373. static void urc_recv_func(const char *data, rt_size_t size)
  374. {
  375. int socket = 0;
  376. rt_size_t bfsz = 0, temp_size = 0;
  377. rt_int32_t timeout;
  378. char *recv_buf = RT_NULL, temp[8];
  379. RT_ASSERT(data && size);
  380. /* get the current socket and receive buffer size by receive data */
  381. sscanf(data, "+IPD,%d,%d:", &socket, (int *) &bfsz);
  382. /* get receive timeout by receive buffer length */
  383. timeout = bfsz;
  384. if (socket < 0 || bfsz == 0)
  385. return;
  386. recv_buf = rt_calloc(1, bfsz);
  387. if (!recv_buf)
  388. {
  389. LOG_E("no memory for URC receive buffer (%d)!", bfsz);
  390. /* read and clean the coming data */
  391. while (temp_size < bfsz)
  392. {
  393. if (bfsz - temp_size > sizeof(temp))
  394. {
  395. at_client_recv(temp, sizeof(temp), timeout);
  396. }
  397. else
  398. {
  399. at_client_recv(temp, bfsz - temp_size, timeout);
  400. }
  401. temp_size += sizeof(temp);
  402. }
  403. return;
  404. }
  405. /* sync receive data */
  406. if (at_client_recv(recv_buf, bfsz, timeout) != bfsz)
  407. {
  408. LOG_E("receive size(%d) data failed!", bfsz);
  409. rt_free(recv_buf);
  410. return;
  411. }
  412. /* notice the receive buffer and buffer size */
  413. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  414. {
  415. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  416. }
  417. }
  418. static void urc_busy_p_func(const char *data, rt_size_t size)
  419. {
  420. RT_ASSERT(data && size);
  421. LOG_D("system is processing a commands and it cannot respond to the current commands.");
  422. }
  423. static void urc_busy_s_func(const char *data, rt_size_t size)
  424. {
  425. RT_ASSERT(data && size);
  426. LOG_D("system is sending data and it cannot respond to the current commands.");
  427. }
  428. static void urc_func(const char *data, rt_size_t size)
  429. {
  430. RT_ASSERT(data && size);
  431. if(strstr(data, "WIFI CONNECTED"))
  432. {
  433. LOG_I("ESP8266 WIFI is connected.");
  434. }
  435. else if(strstr(data, "WIFI DISCONNECT"))
  436. {
  437. LOG_I("ESP8266 WIFI is disconnect.");
  438. }
  439. }
  440. static struct at_urc urc_table[] = {
  441. {"SEND OK", "\r\n", urc_send_func},
  442. {"SEND FAIL", "\r\n", urc_send_func},
  443. {"Recv", "bytes\r\n", urc_send_bfsz_func},
  444. {"", ",CLOSED\r\n", urc_close_func},
  445. {"+IPD", ":", urc_recv_func},
  446. {"busy p", "\r\n", urc_busy_p_func},
  447. {"busy s", "\r\n", urc_busy_s_func},
  448. {"WIFI CONNECTED", "\r\n", urc_func},
  449. {"WIFI DISCONNECT", "\r\n", urc_func},
  450. };
  451. #define AT_SEND_CMD(resp, cmd) \
  452. do \
  453. { \
  454. if (at_exec_cmd(at_resp_set_info(resp, 256, 0, rt_tick_from_millisecond(5000)), cmd) < 0) \
  455. { \
  456. LOG_E("RT AT send commands(%s) error!", cmd); \
  457. result = -RT_ERROR; \
  458. goto __exit; \
  459. } \
  460. } while(0); \
  461. static void esp8266_init_thread_entry(void *parameter)
  462. {
  463. at_response_t resp = RT_NULL;
  464. rt_err_t result = RT_EOK;
  465. rt_size_t i;
  466. resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000));
  467. if (!resp)
  468. {
  469. LOG_E("No memory for response structure!");
  470. result = -RT_ENOMEM;
  471. goto __exit;
  472. }
  473. rt_thread_delay(rt_tick_from_millisecond(5000));
  474. /* reset module */
  475. AT_SEND_CMD(resp, "AT+RST");
  476. /* reset waiting delay */
  477. rt_thread_delay(rt_tick_from_millisecond(1000));
  478. /* disable echo */
  479. AT_SEND_CMD(resp, "ATE0");
  480. /* set current mode to Wi-Fi station */
  481. AT_SEND_CMD(resp, "AT+CWMODE=1");
  482. /* get module version */
  483. AT_SEND_CMD(resp, "AT+GMR");
  484. /* show module version */
  485. for (i = 0; i < resp->line_counts - 1; i++)
  486. {
  487. LOG_D("%s", at_resp_get_line(resp, i + 1));
  488. }
  489. /* connect to WiFi AP */
  490. if (at_exec_cmd(at_resp_set_info(resp, 128, 0, 20 * RT_TICK_PER_SECOND), "AT+CWJAP=\"%s\",\"%s\"",
  491. AT_DEVICE_WIFI_SSID, AT_DEVICE_WIFI_PASSWORD) != RT_EOK)
  492. {
  493. LOG_E("AT network initialize failed, check ssid(%s) and password(%s).", AT_DEVICE_WIFI_SSID, AT_DEVICE_WIFI_PASSWORD);
  494. result = -RT_ERROR;
  495. goto __exit;
  496. }
  497. AT_SEND_CMD(resp, "AT+CIPMUX=1");
  498. __exit:
  499. if (resp)
  500. {
  501. at_delete_resp(resp);
  502. }
  503. if (!result)
  504. {
  505. LOG_I("AT network initialize success!");
  506. }
  507. else
  508. {
  509. LOG_E("AT network initialize failed (%d)!", result);
  510. }
  511. }
  512. int esp8266_net_init(void)
  513. {
  514. #ifdef PKG_AT_INIT_BY_THREAD
  515. rt_thread_t tid;
  516. tid = rt_thread_create("esp8266_net_init", esp8266_init_thread_entry, RT_NULL,ESP8266_THREAD_STACK_SIZE, ESP8266_THREAD_PRIORITY, 20);
  517. if (tid)
  518. {
  519. rt_thread_startup(tid);
  520. }
  521. else
  522. {
  523. LOG_E("Create AT initialization thread fail!");
  524. }
  525. #else
  526. esp8266_init_thread_entry(RT_NULL);
  527. #endif
  528. return RT_EOK;
  529. }
  530. int esp8266_ping(int argc, char **argv)
  531. {
  532. at_response_t resp = RT_NULL;
  533. static int icmp_seq;
  534. int req_time;
  535. if (argc != 2)
  536. {
  537. rt_kprintf("Please input: at_ping <host address>\n");
  538. return -RT_ERROR;
  539. }
  540. resp = at_create_resp(64, 0, rt_tick_from_millisecond(5000));
  541. if (!resp)
  542. {
  543. rt_kprintf("No memory for response structure!\n");
  544. return -RT_ENOMEM;
  545. }
  546. for(icmp_seq = 1; icmp_seq <= 4; icmp_seq++)
  547. {
  548. if (at_exec_cmd(resp, "AT+PING=\"%s\"", argv[1]) < 0)
  549. {
  550. rt_kprintf("ping: unknown remote server host\n");
  551. at_delete_resp(resp);
  552. return -RT_ERROR;
  553. }
  554. if(at_resp_parse_line_args_by_kw(resp, "+", "+%d", &req_time) < 0)
  555. {
  556. continue;
  557. }
  558. if (req_time)
  559. {
  560. rt_kprintf("32 bytes from %s icmp_seq=%d time=%d ms\n", argv[1], icmp_seq, req_time);
  561. }
  562. }
  563. if (resp)
  564. {
  565. at_delete_resp(resp);
  566. }
  567. return RT_EOK;
  568. }
  569. int esp8266_ifconfig(int argc, char **argv)
  570. {
  571. #define AT_ADDR_LEN 128
  572. int result = RT_EOK;
  573. at_response_t resp = RT_NULL;
  574. char ip[AT_ADDR_LEN], mac[AT_ADDR_LEN];
  575. char gateway[AT_ADDR_LEN], netmask[AT_ADDR_LEN];
  576. const char *resp_expr = "%*[^\"]\"%[^\"]\"";
  577. if (argc != 1)
  578. {
  579. rt_kprintf("Please input: at_ifconfig\n");
  580. return -RT_ERROR;
  581. }
  582. rt_memset(ip, 0x00, sizeof(ip));
  583. rt_memset(mac, 0x00, sizeof(mac));
  584. rt_memset(gateway, 0x00, sizeof(gateway));
  585. rt_memset(netmask, 0x00, sizeof(netmask));
  586. resp = at_create_resp(512, 0, rt_tick_from_millisecond(300));
  587. if (!resp)
  588. {
  589. rt_kprintf("No memory for response structure!\n");
  590. return -RT_ENOMEM;
  591. }
  592. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  593. if (at_exec_cmd(resp, "AT+CIFSR") < 0)
  594. {
  595. rt_kprintf("AT send \"AT+CIFSR\" commands error!\n");
  596. result = -RT_ERROR;
  597. goto __exit;
  598. }
  599. if (at_resp_parse_line_args(resp, 2, resp_expr, mac) <= 0)
  600. {
  601. rt_kprintf("Parse error, current line buff : %s\n", at_resp_get_line(resp, 2));
  602. result = -RT_ERROR;
  603. goto __exit;
  604. }
  605. if (at_exec_cmd(resp, "AT+CIPSTA?") < 0)
  606. {
  607. rt_kprintf("AT send \"AT+CIPSTA?\" commands error!\n");
  608. result = -RT_ERROR;
  609. goto __exit;
  610. }
  611. if (at_resp_parse_line_args(resp, 1, resp_expr, ip) <= 0 ||
  612. at_resp_parse_line_args(resp, 2, resp_expr, gateway) <= 0 ||
  613. at_resp_parse_line_args(resp, 3, resp_expr, netmask) <= 0)
  614. {
  615. rt_kprintf("Prase \"AT+CIPSTA?\" commands resposne data error!");
  616. result = -RT_ERROR;
  617. goto __exit;
  618. }
  619. rt_kprintf("network interface: esp8266\n");
  620. rt_kprintf("MAC: %s\n", mac);
  621. rt_kprintf("ip address: %s\n", ip);
  622. rt_kprintf("gw address: %s\n", gateway);
  623. rt_kprintf("net mask : %s\n", netmask);
  624. __exit:
  625. rt_mutex_release(at_event_lock);
  626. if (resp)
  627. {
  628. at_delete_resp(resp);
  629. }
  630. return result;
  631. }
  632. #ifdef FINSH_USING_MSH
  633. #include <finsh.h>
  634. MSH_CMD_EXPORT_ALIAS(esp8266_net_init, at_net_init, initialize AT network);
  635. MSH_CMD_EXPORT_ALIAS(esp8266_ping, at_ping, AT ping network host);
  636. MSH_CMD_EXPORT_ALIAS(esp8266_ifconfig, at_ifconfig, list the information of network interfaces);
  637. #endif
  638. static const struct at_device_ops esp8266_socket_ops = {
  639. esp8266_socket_connect,
  640. esp8266_socket_close,
  641. esp8266_socket_send,
  642. esp8266_domain_resolve,
  643. esp8266_socket_set_event_cb,
  644. };
  645. static int at_socket_device_init(void)
  646. {
  647. /* create current AT socket event */
  648. at_socket_event = rt_event_create("at_se", RT_IPC_FLAG_FIFO);
  649. if (at_socket_event == RT_NULL)
  650. {
  651. LOG_E("RT AT client port initialize failed! at_sock_event create failed!");
  652. return -RT_ENOMEM;
  653. }
  654. /* create current AT socket event lock */
  655. at_event_lock = rt_mutex_create("at_se", RT_IPC_FLAG_FIFO);
  656. if (at_event_lock == RT_NULL)
  657. {
  658. LOG_E("RT AT client port initialize failed! at_sock_lock create failed!");
  659. rt_event_delete(at_socket_event);
  660. return -RT_ENOMEM;
  661. }
  662. /* initialize AT client */
  663. at_client_init(AT_DEVICE_NAME, AT_DEVICE_RECV_BUFF_LEN);
  664. /* register URC data execution function */
  665. at_set_urc_table(urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  666. /* initialize esp8266 network */
  667. esp8266_net_init();
  668. /* set esp8266 AT Socket options */
  669. at_socket_device_register(&esp8266_socket_ops);
  670. return RT_EOK;
  671. }
  672. INIT_APP_EXPORT(at_socket_device_init);
  673. #endif /* AT_DEVICE_ESP8266 */