at_device_esp32.c 25 KB

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