at_device_esp32.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * File : at_socket_esp32.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. * 2019-10-26 zylx first version
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <at_device_esp32.h>
  27. #define LOG_TAG "at.dev.esp32"
  28. #include <at_log.h>
  29. #ifdef AT_DEVICE_USING_ESP32
  30. #define ESP32_WAIT_CONNECT_TIME 5000
  31. #define ESP32_THREAD_STACK_SIZE 2048
  32. #define ESP32_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
  33. /* ============================= esp32 network interface operations ============================= */
  34. static int esp32_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server);
  35. static void esp32_get_netdev_info(struct rt_work *work, void *work_data)
  36. {
  37. #define AT_ADDR_LEN 32
  38. #define AT_ERR_DNS_SERVER "255.255.255.255"
  39. #define AT_DEF_DNS_SERVER "114.114.114.114"
  40. at_response_t resp = RT_NULL;
  41. char ip[AT_ADDR_LEN] = {0}, mac[AT_ADDR_LEN] = {0};
  42. char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0};
  43. char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0};
  44. const char *resp_expr = "%*[^\"]\"%[^\"]\"";
  45. const char *resp_dns = "+CIPDNS:%s";
  46. ip_addr_t ip_addr;
  47. rt_uint32_t mac_addr[6] = {0};
  48. rt_uint32_t num = 0;
  49. rt_uint8_t dhcp_stat = 0;
  50. struct rt_delayed_work *delay_work = (struct rt_delayed_work *)work;
  51. struct at_device *device = (struct at_device *)work_data;
  52. struct netdev *netdev = device->netdev;
  53. struct at_client *client = device->client;
  54. if (delay_work)
  55. {
  56. rt_free(delay_work);
  57. }
  58. resp = at_create_resp(512, 0, rt_tick_from_millisecond(300));
  59. if (resp == RT_NULL)
  60. {
  61. LOG_E("no memory for resp create.");
  62. return;
  63. }
  64. /* send mac addr query commond "AT+CIFSR" and wait response */
  65. if (at_obj_exec_cmd(client, resp, "AT+CIFSR") < 0)
  66. {
  67. goto __exit;
  68. }
  69. if (at_resp_parse_line_args_by_kw(resp, "STAMAC", resp_expr, mac) <= 0)
  70. {
  71. LOG_E("%s device parse \"AT+CIFSR\" cmd error.", device->name);
  72. goto __exit;
  73. }
  74. /* send addr info query commond "AT+CIPSTA?" and wait response */
  75. if (at_obj_exec_cmd(client, resp, "AT+CIPSTA?") < 0)
  76. {
  77. LOG_E("%s device send \"AT+CIPSTA?\" cmd error.", device->name);
  78. goto __exit;
  79. }
  80. if (at_resp_parse_line_args_by_kw(resp, "ip", resp_expr, ip) <= 0 ||
  81. at_resp_parse_line_args_by_kw(resp, "gateway", resp_expr, gateway) <= 0 ||
  82. at_resp_parse_line_args_by_kw(resp, "netmask", resp_expr, netmask) <= 0)
  83. {
  84. LOG_E("%s device prase \"AT+CIPSTA?\" cmd error.", device->name);
  85. goto __exit;
  86. }
  87. /* set netdev info */
  88. inet_aton(gateway, &ip_addr);
  89. netdev_low_level_set_gw(netdev, &ip_addr);
  90. inet_aton(netmask, &ip_addr);
  91. netdev_low_level_set_netmask(netdev, &ip_addr);
  92. inet_aton(ip, &ip_addr);
  93. netdev_low_level_set_ipaddr(netdev, &ip_addr);
  94. sscanf(mac, "%x:%x:%x:%x:%x:%x",
  95. &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
  96. for (num = 0; num < netdev->hwaddr_len; num++)
  97. {
  98. netdev->hwaddr[num] = mac_addr[num];
  99. }
  100. /* send dns server query commond "AT+CIPDNS?" and wait response */
  101. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS?") < 0)
  102. {
  103. LOG_W("please check and update %s device firmware to support the \"AT+CIPDNS?\" cmd.", device->name);
  104. goto __exit;
  105. }
  106. if (at_resp_parse_line_args(resp, 1, resp_dns, dns_server1) <= 0 &&
  107. at_resp_parse_line_args(resp, 2, resp_dns, dns_server2) <= 0)
  108. {
  109. LOG_E("%s device prase \"AT+CIPDNS?\" cmd error.", device->name);
  110. goto __exit;
  111. }
  112. /* set primary DNS server address */
  113. if (rt_strlen(dns_server1) > 0 &&
  114. rt_strncmp(dns_server1, AT_ERR_DNS_SERVER, rt_strlen(AT_ERR_DNS_SERVER)) != 0)
  115. {
  116. inet_aton(dns_server1, &ip_addr);
  117. netdev_low_level_set_dns_server(netdev, 0, &ip_addr);
  118. }
  119. else
  120. {
  121. inet_aton(AT_DEF_DNS_SERVER, &ip_addr);
  122. esp32_netdev_set_dns_server(netdev, 0, &ip_addr);
  123. }
  124. /* set secondary DNS server address */
  125. if (rt_strlen(dns_server2) > 0 )
  126. {
  127. inet_aton(dns_server2, &ip_addr);
  128. netdev_low_level_set_dns_server(netdev, 1, &ip_addr);
  129. }
  130. /* send DHCP query commond " AT+CWDHCP_CUR?" and wait response */
  131. if (at_obj_exec_cmd(client, resp, "AT+CWDHCP?") < 0)
  132. {
  133. goto __exit;
  134. }
  135. /* parse response data, get the DHCP status */
  136. if (at_resp_parse_line_args_by_kw(resp, "+CWDHCP:", "+CWDHCP:%d", &dhcp_stat) < 0)
  137. {
  138. LOG_E("%s device prase DHCP status error.", device->name);
  139. goto __exit;
  140. }
  141. /* Bit0 - SoftAP DHCP status, Bit1 - Station DHCP status */
  142. netdev_low_level_set_dhcp_status(netdev, dhcp_stat & 0x01 ? RT_TRUE : RT_FALSE);
  143. __exit:
  144. if (resp)
  145. {
  146. at_delete_resp(resp);
  147. }
  148. }
  149. static int esp32_net_init(struct at_device *device);
  150. static int esp32_netdev_set_up(struct netdev *netdev)
  151. {
  152. struct at_device *device = RT_NULL;
  153. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  154. if (device == RT_NULL)
  155. {
  156. LOG_E("get device(%s) failed.", netdev->name);
  157. return -RT_ERROR;
  158. }
  159. if (device->is_init == RT_FALSE)
  160. {
  161. esp32_net_init(device);
  162. netdev_low_level_set_status(netdev, RT_TRUE);
  163. LOG_D("network interface device(%s) set up status", netdev->name);
  164. }
  165. return RT_EOK;
  166. }
  167. static int esp32_netdev_set_down(struct netdev *netdev)
  168. {
  169. struct at_device *device = RT_NULL;
  170. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  171. if (device == RT_NULL)
  172. {
  173. LOG_E("get device by netdev(%s) failed.", netdev->name);
  174. return -RT_ERROR;
  175. }
  176. if (device->is_init == RT_TRUE)
  177. {
  178. device->is_init = RT_FALSE;
  179. netdev_low_level_set_status(netdev, RT_FALSE);
  180. LOG_D("network interface device(%s) set down status", netdev->name);
  181. }
  182. return RT_EOK;
  183. }
  184. static int esp32_netdev_set_addr_info(struct netdev *netdev, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
  185. {
  186. #define IPADDR_RESP_SIZE 128
  187. #define IPADDR_SIZE 16
  188. int result = RT_EOK;
  189. at_response_t resp = RT_NULL;
  190. struct at_device *device = RT_NULL;
  191. char ip_str[IPADDR_SIZE] = {0};
  192. char gw_str[IPADDR_SIZE] = {0};
  193. char netmask_str[IPADDR_SIZE] = {0};
  194. RT_ASSERT(netdev);
  195. RT_ASSERT(ip_addr || netmask || gw);
  196. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  197. if (device == RT_NULL)
  198. {
  199. LOG_E("get device(%s) failed.", netdev->name);
  200. return -RT_ERROR;
  201. }
  202. resp = at_create_resp(IPADDR_RESP_SIZE, 0, rt_tick_from_millisecond(300));
  203. if (resp == RT_NULL)
  204. {
  205. LOG_E("no memory for resp create.");
  206. result = -RT_ENOMEM;
  207. goto __exit;
  208. }
  209. /* Convert numeric IP address into decimal dotted ASCII representation. */
  210. if (ip_addr)
  211. rt_memcpy(ip_str, inet_ntoa(*ip_addr), IPADDR_SIZE);
  212. else
  213. rt_memcpy(ip_str, inet_ntoa(netdev->ip_addr), IPADDR_SIZE);
  214. if (gw)
  215. rt_memcpy(gw_str, inet_ntoa(*gw), IPADDR_SIZE);
  216. else
  217. rt_memcpy(gw_str, inet_ntoa(netdev->gw), IPADDR_SIZE);
  218. if (netmask)
  219. rt_memcpy(netmask_str, inet_ntoa(*netmask), IPADDR_SIZE);
  220. else
  221. rt_memcpy(netmask_str, inet_ntoa(netdev->netmask), IPADDR_SIZE);
  222. /* send addr info set commond "AT+CIPSTA_CUR=<ip>[,<gateway>,<netmask>]" and wait response */
  223. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTA_CUR=\"%s\",\"%s\",\"%s\"",
  224. ip_str, gw_str, netmask_str) < 0)
  225. {
  226. LOG_E("%s device set address failed.", device->name);
  227. result = -RT_ERROR;
  228. }
  229. else
  230. {
  231. /* Update netdev information */
  232. if (ip_addr)
  233. netdev_low_level_set_ipaddr(netdev, ip_addr);
  234. if (gw)
  235. netdev_low_level_set_gw(netdev, gw);
  236. if (netmask)
  237. netdev_low_level_set_netmask(netdev, netmask);
  238. LOG_D("%s device set address success.", device->name);
  239. }
  240. __exit:
  241. if (resp)
  242. {
  243. at_delete_resp(resp);
  244. }
  245. return result;
  246. }
  247. static int esp32_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
  248. {
  249. #define DNS_RESP_SIZE 128
  250. int result = RT_EOK;
  251. at_response_t resp = RT_NULL;
  252. struct at_device *device = RT_NULL;
  253. RT_ASSERT(netdev);
  254. RT_ASSERT(dns_server);
  255. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  256. if (device == RT_NULL)
  257. {
  258. LOG_E("get device by netdev(%s) failed.", netdev->name);
  259. return -RT_ERROR;
  260. }
  261. resp = at_create_resp(DNS_RESP_SIZE, 0, rt_tick_from_millisecond(300));
  262. if (resp == RT_NULL)
  263. {
  264. LOG_E("no memory for resp create.");
  265. return -RT_ENOMEM;
  266. }
  267. /* send dns server set commond "AT+CIPDNS_CUR=<enable>[,<DNS server0>,<DNS server1>]" and wait response */
  268. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS_CUR=1,\"%s\"", inet_ntoa(*dns_server)) < 0)
  269. {
  270. LOG_E("%s device set DNS failed.", device->name);
  271. result = -RT_ERROR;
  272. }
  273. else
  274. {
  275. netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
  276. LOG_D("%s device set DNS(%s) success.", device->name, inet_ntoa(*dns_server));
  277. }
  278. if (resp)
  279. {
  280. at_delete_resp(resp);
  281. }
  282. return result;
  283. }
  284. static int esp32_netdev_set_dhcp(struct netdev *netdev, rt_bool_t is_enabled)
  285. {
  286. #define ESP32_STATION 1
  287. #define RESP_SIZE 128
  288. int result = RT_EOK;
  289. at_response_t resp = RT_NULL;
  290. struct at_device *device = RT_NULL;
  291. RT_ASSERT(netdev);
  292. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  293. if (device == RT_NULL)
  294. {
  295. LOG_E("get device by netdev(%s) failed.", netdev->name);
  296. return -RT_ERROR;
  297. }
  298. resp = at_create_resp(RESP_SIZE, 0, rt_tick_from_millisecond(300));
  299. if (resp == RT_NULL)
  300. {
  301. LOG_E("no memory for resp struct.");
  302. return -RT_ENOMEM;
  303. }
  304. /* send dhcp set commond "AT+CWDHCP_CUR=<mode>,<en>" and wait response */
  305. if (at_obj_exec_cmd(device->client, resp, "AT+CWDHCP_CUR=%d,%d", ESP32_STATION, is_enabled) < 0)
  306. {
  307. LOG_E("%s device set DHCP status(%d) failed.", device->name, is_enabled);
  308. result = -RT_ERROR;
  309. goto __exit;
  310. }
  311. else
  312. {
  313. netdev_low_level_set_dhcp_status(netdev, is_enabled);
  314. LOG_D("%s device set DHCP status(%d) ok.", device->name, is_enabled);
  315. }
  316. __exit:
  317. if (resp)
  318. {
  319. at_delete_resp(resp);
  320. }
  321. return result;
  322. }
  323. #ifdef NETDEV_USING_PING
  324. static int esp32_netdev_ping(struct netdev *netdev, const char *host,
  325. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
  326. {
  327. #define ESP32_PING_IP_SIZE 16
  328. rt_err_t result = RT_EOK;
  329. at_response_t resp = RT_NULL;
  330. struct at_device *device = RT_NULL;
  331. char ip_addr[ESP32_PING_IP_SIZE] = {0};
  332. int req_time;
  333. RT_ASSERT(netdev);
  334. RT_ASSERT(host);
  335. RT_ASSERT(ping_resp);
  336. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  337. if (device == RT_NULL)
  338. {
  339. LOG_E("get device(%s) failed.", netdev->name);
  340. return -RT_ERROR;
  341. }
  342. resp = at_create_resp(64, 0, timeout);
  343. if (resp == RT_NULL)
  344. {
  345. LOG_E("no memory for resp create.");
  346. return -RT_ENOMEM;
  347. }
  348. /* send domain commond "AT+CIPDOMAIN=<domain name>" and wait response */
  349. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=\"%s\"", host) < 0)
  350. {
  351. result = -RT_ERROR;
  352. goto __exit;
  353. }
  354. /* parse the third line of response data, get the IP address */
  355. if (at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", "+CIPDOMAIN:%s", ip_addr) < 0)
  356. {
  357. result = -RT_ERROR;
  358. goto __exit;
  359. }
  360. /* send ping commond "AT+PING=<IP>" and wait response */
  361. if (at_obj_exec_cmd(device->client, resp, "AT+PING=\"%s\"", host) < 0)
  362. {
  363. result = -RT_ERROR;
  364. goto __exit;
  365. }
  366. if (at_resp_parse_line_args_by_kw(resp, "+", "+PING:%d", &req_time) < 0)
  367. {
  368. result = -RT_ERROR;
  369. goto __exit;
  370. }
  371. if (req_time)
  372. {
  373. inet_aton(ip_addr, &(ping_resp->ip_addr));
  374. ping_resp->data_len = data_len;
  375. ping_resp->ttl = 0;
  376. ping_resp->ticks = req_time;
  377. }
  378. __exit:
  379. if (resp)
  380. {
  381. at_delete_resp(resp);
  382. }
  383. return result;
  384. }
  385. #endif /* NETDEV_USING_PING */
  386. #ifdef NETDEV_USING_NETSTAT
  387. void esp32_netdev_netstat(struct netdev *netdev)
  388. {
  389. #define ESP32_NETSTAT_RESP_SIZE 320
  390. #define ESP32_NETSTAT_TYPE_SIZE 4
  391. #define ESP32_NETSTAT_IPADDR_SIZE 17
  392. #define ESP32_NETSTAT_EXPRESSION "+CIPSTATUS:%*d,\"%[^\"]\",\"%[^\"]\",%d,%d,%*d"
  393. at_response_t resp = RT_NULL;
  394. struct at_device *device = RT_NULL;
  395. int remote_port, local_port, i;
  396. char *type = RT_NULL;
  397. char *ipaddr = RT_NULL;
  398. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  399. if (device == RT_NULL)
  400. {
  401. LOG_E("get device(%s) failed.", netdev->name);
  402. return;
  403. }
  404. type = (char *) rt_calloc(1, ESP32_NETSTAT_TYPE_SIZE);
  405. ipaddr = (char *) rt_calloc(1, ESP32_NETSTAT_IPADDR_SIZE);
  406. if ((type && ipaddr) == RT_NULL)
  407. {
  408. LOG_E("no memory for ipaddr create.");
  409. goto __exit;
  410. }
  411. resp = at_create_resp(ESP32_NETSTAT_RESP_SIZE, 0, 5 * RT_TICK_PER_SECOND);
  412. if (resp == RT_NULL)
  413. {
  414. LOG_E("no memory for resp create.", device->name);
  415. goto __exit;
  416. }
  417. /* send network connection information commond "AT+CIPSTATUS" and wait response */
  418. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTATUS") < 0)
  419. {
  420. goto __exit;
  421. }
  422. for (i = 1; i <= resp->line_counts; i++)
  423. {
  424. if (strstr(at_resp_get_line(resp, i), "+CIPSTATUS"))
  425. {
  426. /* parse the third line of response data, get the network connection information */
  427. if (at_resp_parse_line_args(resp, i, ESP32_NETSTAT_EXPRESSION, type, ipaddr, &remote_port, &local_port) < 0)
  428. {
  429. goto __exit;
  430. }
  431. else
  432. {
  433. LOG_RAW("%s: %s:%d ==> %s:%d\n", type, inet_ntoa(netdev->ip_addr), local_port, ipaddr, remote_port);
  434. }
  435. }
  436. }
  437. __exit:
  438. if (resp)
  439. {
  440. at_delete_resp(resp);
  441. }
  442. if (type)
  443. {
  444. rt_free(type);
  445. }
  446. if (ipaddr)
  447. {
  448. rt_free(ipaddr);
  449. }
  450. }
  451. #endif /* NETDEV_USING_NETSTAT */
  452. static const struct netdev_ops esp32_netdev_ops =
  453. {
  454. esp32_netdev_set_up,
  455. esp32_netdev_set_down,
  456. esp32_netdev_set_addr_info,
  457. esp32_netdev_set_dns_server,
  458. esp32_netdev_set_dhcp,
  459. #ifdef NETDEV_USING_PING
  460. esp32_netdev_ping,
  461. #endif
  462. #ifdef NETDEV_USING_NETSTAT
  463. esp32_netdev_netstat,
  464. #endif
  465. };
  466. static struct netdev *esp32_netdev_add(const char *netdev_name)
  467. {
  468. #define ETHERNET_MTU 1500
  469. #define HWADDR_LEN 6
  470. struct netdev *netdev = RT_NULL;
  471. RT_ASSERT(netdev_name);
  472. netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
  473. if (netdev == RT_NULL)
  474. {
  475. LOG_E("no memory for netdev create.");
  476. return RT_NULL;
  477. }
  478. netdev->mtu = ETHERNET_MTU;
  479. netdev->ops = &esp32_netdev_ops;
  480. netdev->hwaddr_len = HWADDR_LEN;
  481. #ifdef SAL_USING_AT
  482. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  483. /* set the network interface socket/netdb operations */
  484. sal_at_netdev_set_pf_info(netdev);
  485. #endif
  486. netdev_register(netdev, netdev_name, RT_NULL);
  487. return netdev;
  488. }
  489. /* ============================= esp32 device operations ============================= */
  490. #define AT_SEND_CMD(client, resp, cmd) \
  491. do { \
  492. (resp) = at_resp_set_info((resp), 256, 0, 5 * RT_TICK_PER_SECOND); \
  493. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  494. { \
  495. result = -RT_ERROR; \
  496. goto __exit; \
  497. } \
  498. } while(0) \
  499. static void esp32_netdev_start_delay_work(struct at_device *device)
  500. {
  501. struct rt_delayed_work *net_work = RT_NULL;
  502. net_work = (struct rt_delayed_work *)rt_calloc(1, sizeof(struct rt_delayed_work));
  503. if (net_work == RT_NULL)
  504. {
  505. return;
  506. }
  507. rt_delayed_work_init(net_work, esp32_get_netdev_info, (void *)device);
  508. rt_work_submit(&(net_work->work), RT_TICK_PER_SECOND);
  509. }
  510. static void esp32_init_thread_entry(void *parameter)
  511. {
  512. #define INIT_RETRY 5
  513. struct at_device *device = (struct at_device *) parameter;
  514. struct at_device_esp32 *esp32 = (struct at_device_esp32 *) device->user_data;
  515. struct at_client *client = device->client;
  516. at_response_t resp = RT_NULL;
  517. rt_err_t result = RT_EOK;
  518. rt_size_t i = 0, retry_num = INIT_RETRY;
  519. rt_bool_t wifi_is_conn = RT_FALSE;
  520. LOG_D("%s device initialize start.", device->name);
  521. /* wait esp32 device startup finish */
  522. if (at_client_obj_wait_connect(client, ESP32_WAIT_CONNECT_TIME))
  523. {
  524. return;
  525. }
  526. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  527. if (resp == RT_NULL)
  528. {
  529. LOG_E("no memory for resp create.");
  530. return;
  531. }
  532. while (retry_num--)
  533. {
  534. /* reset module */
  535. AT_SEND_CMD(client, resp, "AT+RST");
  536. /* reset waiting delay */
  537. rt_thread_mdelay(1000);
  538. /* disable echo */
  539. AT_SEND_CMD(client, resp, "ATE0");
  540. /* set current mode to Wi-Fi station */
  541. AT_SEND_CMD(client, resp, "AT+CWMODE=1");
  542. /* get module version */
  543. AT_SEND_CMD(client, resp, "AT+GMR");
  544. /* show module version */
  545. for (i = 0; i < resp->line_counts - 1; i++)
  546. {
  547. LOG_D("%s", at_resp_get_line(resp, i + 1));
  548. }
  549. AT_SEND_CMD(client, resp, "AT+CIPMUX=1");
  550. /* initialize successfully */
  551. result = RT_EOK;
  552. break;
  553. __exit:
  554. if (result != RT_EOK)
  555. {
  556. rt_thread_mdelay(1000);
  557. LOG_I("%s device initialize retry...", device->name);
  558. }
  559. }
  560. /* connect to WiFi AP */
  561. if (at_obj_exec_cmd(client, at_resp_set_info(resp, 512, 0, 20 * RT_TICK_PER_SECOND),
  562. "AT+CWJAP=\"%s\",\"%s\"", esp32->wifi_ssid, esp32->wifi_password) != RT_EOK)
  563. {
  564. LOG_W("%s device wifi connect failed, check ssid(%s) and password(%s).",
  565. device->name, esp32->wifi_ssid, esp32->wifi_password);
  566. }
  567. else
  568. {
  569. wifi_is_conn = RT_TRUE;
  570. }
  571. if (resp)
  572. {
  573. at_delete_resp(resp);
  574. }
  575. if (result != RT_EOK)
  576. {
  577. netdev_low_level_set_status(device->netdev, RT_FALSE);
  578. LOG_E("%s device network initialize failed(%d).", device->name, result);
  579. }
  580. else
  581. {
  582. device->is_init = RT_TRUE;
  583. netdev_low_level_set_status(device->netdev, RT_TRUE);
  584. if (wifi_is_conn)
  585. {
  586. netdev_low_level_set_link_status(device->netdev, RT_TRUE);
  587. }
  588. esp32_netdev_start_delay_work(device);
  589. LOG_I("%s device network initialize successfully.", device->name);
  590. }
  591. }
  592. static int esp32_net_init(struct at_device *device)
  593. {
  594. #ifdef AT_DEVICE_ESP32_INIT_ASYN
  595. rt_thread_t tid;
  596. tid = rt_thread_create("esp_net", esp32_init_thread_entry, (void *) device,
  597. ESP32_THREAD_STACK_SIZE, ESP32_THREAD_PRIORITY, 20);
  598. if (tid)
  599. {
  600. rt_thread_startup(tid);
  601. }
  602. else
  603. {
  604. LOG_E("create %s device init thread failed.", device->name);
  605. return -RT_ERROR;
  606. }
  607. #else
  608. esp32_init_thread_entry(device);
  609. #endif /* AT_DEVICE_ESP32_INIT_ASYN */
  610. return RT_EOK;
  611. }
  612. static void urc_busy_p_func(struct at_client *client, const char *data, rt_size_t size)
  613. {
  614. LOG_D("system is processing a commands...");
  615. }
  616. static void urc_busy_s_func(struct at_client *client, const char *data, rt_size_t size)
  617. {
  618. LOG_D("system is sending data...");
  619. }
  620. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  621. {
  622. struct at_device *device = RT_NULL;
  623. char *client_name = client->device->parent.name;
  624. RT_ASSERT(client && data && size);
  625. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  626. if (device == RT_NULL)
  627. {
  628. LOG_E("get device(%s) failed.", client_name);
  629. return;
  630. }
  631. if (rt_strstr(data, "WIFI CONNECTED"))
  632. {
  633. LOG_I("%s device wifi is connected.", device->name);
  634. if (device->is_init)
  635. {
  636. netdev_low_level_set_link_status(device->netdev, RT_TRUE);
  637. esp32_netdev_start_delay_work(device);
  638. }
  639. }
  640. else if (rt_strstr(data, "WIFI DISCONNECT"))
  641. {
  642. LOG_I("%s device wifi is disconnect.", device->name);
  643. if (device->is_init)
  644. {
  645. netdev_low_level_set_link_status(device->netdev, RT_FALSE);
  646. }
  647. }
  648. }
  649. static const struct at_urc urc_table[] =
  650. {
  651. {"busy p", "\r\n", urc_busy_p_func},
  652. {"busy s", "\r\n", urc_busy_s_func},
  653. {"WIFI CONNECTED", "\r\n", urc_func},
  654. {"WIFI DISCONNECT", "\r\n", urc_func},
  655. };
  656. static int esp32_init(struct at_device *device)
  657. {
  658. struct at_device_esp32 *esp32 = (struct at_device_esp32 *) device->user_data;
  659. /* initialize AT client */
  660. at_client_init(esp32->client_name, esp32->recv_line_num);
  661. device->client = at_client_get(esp32->client_name);
  662. if (device->client == RT_NULL)
  663. {
  664. LOG_E("get AT client(%s) failed.", esp32->client_name);
  665. return -RT_ERROR;
  666. }
  667. /* register URC data execution function */
  668. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  669. #ifdef AT_USING_SOCKET
  670. esp32_socket_init(device);
  671. #endif
  672. /* add esp32 device to the netdev list */
  673. device->netdev = esp32_netdev_add(esp32->device_name);
  674. if (device->netdev == RT_NULL)
  675. {
  676. LOG_E("add netdev(%s) failed.", esp32->device_name);
  677. return -RT_ERROR;
  678. }
  679. /* initialize esp32 device network */
  680. return esp32_netdev_set_up(device->netdev);
  681. }
  682. static int esp32_deinit(struct at_device *device)
  683. {
  684. return esp32_netdev_set_down(device->netdev);
  685. }
  686. /* reset eap8266 device and initialize device network again */
  687. static int esp32_reset(struct at_device *device)
  688. {
  689. int result = RT_EOK;
  690. struct at_client *client = device->client;
  691. /* send "AT+RST" commonds to esp32 device */
  692. result = at_obj_exec_cmd(client, RT_NULL, "AT+RST");
  693. rt_thread_mdelay(1000);
  694. /* waiting 10 seconds for esp32 device reset */
  695. device->is_init = RT_FALSE;
  696. if (at_client_obj_wait_connect(client, ESP32_WAIT_CONNECT_TIME))
  697. {
  698. return -RT_ETIMEOUT;
  699. }
  700. /* initialize esp32 device network */
  701. esp32_net_init(device);
  702. device->is_init = RT_TRUE;
  703. return result;
  704. }
  705. /* change eap8266 wifi ssid and password information */
  706. static int esp32_wifi_info_set(struct at_device *device, struct at_device_ssid_pwd *info)
  707. {
  708. int result = RT_EOK;
  709. struct at_response *resp = RT_NULL;
  710. if (info->ssid == RT_NULL || info->password == RT_NULL)
  711. {
  712. LOG_E("input wifi ssid(%s) and password(%s) error.", info->ssid, info->password);
  713. return -RT_ERROR;
  714. }
  715. resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
  716. if (resp == RT_NULL)
  717. {
  718. LOG_E("no memory for resp create.");
  719. return -RT_ENOMEM;
  720. }
  721. /* connect to input wifi ap */
  722. if (at_obj_exec_cmd(device->client, resp, "AT+CWJAP=\"%s\",\"%s\"", info->ssid, info->password) != RT_EOK)
  723. {
  724. LOG_E("%s device wifi connect failed, check ssid(%s) and password(%s).",
  725. device->name, info->ssid, info->password);
  726. result = -RT_ERROR;
  727. }
  728. if (resp)
  729. {
  730. at_delete_resp(resp);
  731. }
  732. return result;
  733. }
  734. static int esp32_control(struct at_device *device, int cmd, void *arg)
  735. {
  736. int result = -RT_ERROR;
  737. RT_ASSERT(device);
  738. switch (cmd)
  739. {
  740. case AT_DEVICE_CTRL_POWER_ON:
  741. case AT_DEVICE_CTRL_POWER_OFF:
  742. case AT_DEVICE_CTRL_LOW_POWER:
  743. case AT_DEVICE_CTRL_SLEEP:
  744. case AT_DEVICE_CTRL_WAKEUP:
  745. case AT_DEVICE_CTRL_NET_CONN:
  746. case AT_DEVICE_CTRL_NET_DISCONN:
  747. case AT_DEVICE_CTRL_GET_SIGNAL:
  748. case AT_DEVICE_CTRL_GET_GPS:
  749. case AT_DEVICE_CTRL_GET_VER:
  750. LOG_W("not support the control cmd(%d).", cmd);
  751. break;
  752. case AT_DEVICE_CTRL_RESET:
  753. result = esp32_reset(device);
  754. break;
  755. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  756. result = esp32_wifi_info_set(device, (struct at_device_ssid_pwd *) arg);
  757. break;
  758. default:
  759. LOG_E("input error control cmd(%d).", cmd);
  760. break;
  761. }
  762. return result;
  763. }
  764. static const struct at_device_ops esp32_device_ops =
  765. {
  766. esp32_init,
  767. esp32_deinit,
  768. esp32_control,
  769. };
  770. static int esp32_device_class_register(void)
  771. {
  772. struct at_device_class *class = RT_NULL;
  773. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  774. if (class == RT_NULL)
  775. {
  776. LOG_E("no memory for class create.");
  777. return -RT_ENOMEM;
  778. }
  779. /* fill ESP32 device class object */
  780. #ifdef AT_USING_SOCKET
  781. esp32_socket_class_register(class);
  782. #endif
  783. class->device_ops = &esp32_device_ops;
  784. return at_device_class_register(class, AT_DEVICE_CLASS_ESP32);
  785. }
  786. INIT_DEVICE_EXPORT(esp32_device_class_register);
  787. #endif /* AT_DEVICE_USING_ESP32 */