at_device_esp32.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. unsigned int ESP32_GMR_AT_VERSION;
  20. /* ============================= esp32 network interface operations ============================= */
  21. static int esp32_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server);
  22. static void esp32_get_netdev_info(struct rt_work *work, void *work_data)
  23. {
  24. #define AT_ADDR_LEN 32
  25. #define AT_ERR_DNS_SERVER "255.255.255.255"
  26. #define AT_DEF_DNS_SERVER "114.114.114.114"
  27. at_response_t resp = RT_NULL;
  28. char ip[AT_ADDR_LEN] = {0}, mac[AT_ADDR_LEN] = {0};
  29. char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0};
  30. char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0};
  31. const char *resp_expr = "%*[^\"]\"%[^\"]\"";
  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. rt_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. /* +CIPDNS:0,"208.67.222.222","8.8.8.8" */
  92. if (at_resp_parse_line_args_by_kw(resp, "+CIPDNS:", "%*[^\"]\"%[^\"]\",\"%[^\"]\"", dns_server1, 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" 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 - Station DHCP status, Bit1 - SoftAP 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=<ip>[,<gateway>,<netmask>]" and wait response */
  208. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTA=\"%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=<enable>[,<"DNS IP1">][,<"DNS IP2">][,<"DNS IP3">]" and wait response */
  253. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS=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 << 0
  272. #define ESP32_SOFTAP 1 << 1
  273. #define RESP_SIZE 128
  274. int result = RT_EOK;
  275. at_response_t resp = RT_NULL;
  276. struct at_device *device = RT_NULL;
  277. RT_ASSERT(netdev);
  278. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  279. if (device == RT_NULL)
  280. {
  281. LOG_E("get device by netdev(%s) failed.", netdev->name);
  282. return -RT_ERROR;
  283. }
  284. resp = at_create_resp(RESP_SIZE, 0, rt_tick_from_millisecond(300));
  285. if (resp == RT_NULL)
  286. {
  287. LOG_E("no memory for resp struct.");
  288. return -RT_ENOMEM;
  289. }
  290. /* send dhcp set commond "AT+CWDHCP=<operate>,<mode>" and wait response */
  291. if (at_obj_exec_cmd(device->client, resp, "AT+CWDHCP=%d,%d", is_enabled, ESP32_STATION) < 0)
  292. {
  293. LOG_E("%s device set DHCP status(%d) failed.", device->name, is_enabled);
  294. result = -RT_ERROR;
  295. goto __exit;
  296. }
  297. else
  298. {
  299. netdev_low_level_set_dhcp_status(netdev, is_enabled);
  300. LOG_D("%s device set DHCP status(%d) ok.", device->name, is_enabled);
  301. }
  302. __exit:
  303. if (resp)
  304. {
  305. at_delete_resp(resp);
  306. }
  307. return result;
  308. }
  309. #ifdef NETDEV_USING_PING
  310. static int esp32_netdev_ping(struct netdev *netdev, const char *host,
  311. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
  312. #if RT_VER_NUM >= 0x50100
  313. , rt_bool_t is_bind
  314. #endif
  315. )
  316. {
  317. #define ESP32_PING_IP_SIZE 16
  318. rt_err_t result = RT_EOK;
  319. at_response_t resp = RT_NULL;
  320. struct at_device *device = RT_NULL;
  321. char ip_addr[ESP32_PING_IP_SIZE] = {0};
  322. int req_time;
  323. #if RT_VER_NUM >= 0x50100
  324. RT_UNUSED(is_bind);
  325. #endif
  326. RT_ASSERT(netdev);
  327. RT_ASSERT(host);
  328. RT_ASSERT(ping_resp);
  329. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  330. if (device == RT_NULL)
  331. {
  332. LOG_E("get device(%s) failed.", netdev->name);
  333. return -RT_ERROR;
  334. }
  335. resp = at_create_resp(64, 0, timeout);
  336. if (resp == RT_NULL)
  337. {
  338. LOG_E("no memory for resp create.");
  339. return -RT_ENOMEM;
  340. }
  341. /* send domain commond "AT+CIPDOMAIN=<domain name>" and wait response */
  342. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=\"%s\"", host) < 0)
  343. {
  344. result = -RT_ERROR;
  345. goto __exit;
  346. }
  347. /* parse the third line of response data, get the IP address */
  348. if (at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", (esp32_get_at_version() <= ESP32_DEFAULT_AT_VERSION_NUM) ? \
  349. "+CIPDOMAIN:%s" : "+CIPDOMAIN:\"%[^\"]\"", ip_addr) < 0)
  350. {
  351. result = -RT_ERROR;
  352. goto __exit;
  353. }
  354. /* send ping commond "AT+PING=<IP>" and wait response */
  355. if (at_obj_exec_cmd(device->client, resp, "AT+PING=\"%s\"", host) < 0)
  356. {
  357. result = -RT_ERROR;
  358. goto __exit;
  359. }
  360. if (at_resp_parse_line_args_by_kw(resp, "+", (esp32_get_at_version() <= ESP32_DEFAULT_AT_VERSION_NUM) ?
  361. "+%d" : "+PING:%d", &req_time) < 0)
  362. {
  363. result = -RT_ERROR;
  364. goto __exit;
  365. }
  366. if (req_time)
  367. {
  368. inet_aton(ip_addr, &(ping_resp->ip_addr));
  369. ping_resp->data_len = data_len;
  370. ping_resp->ttl = 0;
  371. ping_resp->ticks = req_time;
  372. }
  373. __exit:
  374. if (resp)
  375. {
  376. at_delete_resp(resp);
  377. }
  378. return result;
  379. }
  380. #endif /* NETDEV_USING_PING */
  381. #ifdef NETDEV_USING_NETSTAT
  382. void esp32_netdev_netstat(struct netdev *netdev)
  383. {
  384. #define ESP32_NETSTAT_RESP_SIZE 320
  385. #define ESP32_NETSTAT_TYPE_SIZE 4
  386. #define ESP32_NETSTAT_IPADDR_SIZE 17
  387. #define ESP32_NETSTAT_EXPRESSION "+CIPSTATUS:%*d,\"%[^\"]\",\"%[^\"]\",%d,%d,%*d"
  388. at_response_t resp = RT_NULL;
  389. struct at_device *device = RT_NULL;
  390. int remote_port, local_port, i;
  391. char *type = RT_NULL;
  392. char *ipaddr = RT_NULL;
  393. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  394. if (device == RT_NULL)
  395. {
  396. LOG_E("get device(%s) failed.", netdev->name);
  397. return;
  398. }
  399. type = (char *) rt_calloc(1, ESP32_NETSTAT_TYPE_SIZE);
  400. ipaddr = (char *) rt_calloc(1, ESP32_NETSTAT_IPADDR_SIZE);
  401. if ((type && ipaddr) == RT_NULL)
  402. {
  403. LOG_E("no memory for ipaddr create.");
  404. goto __exit;
  405. }
  406. resp = at_create_resp(ESP32_NETSTAT_RESP_SIZE, 0, 5 * RT_TICK_PER_SECOND);
  407. if (resp == RT_NULL)
  408. {
  409. LOG_E("no memory for resp create.", device->name);
  410. goto __exit;
  411. }
  412. /* send network connection information commond "AT+CIPSTATUS" and wait response */
  413. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTATUS") < 0)
  414. {
  415. goto __exit;
  416. }
  417. for (i = 1; i <= resp->line_counts; i++)
  418. {
  419. if (strstr(at_resp_get_line(resp, i), "+CIPSTATUS"))
  420. {
  421. /* parse the third line of response data, get the network connection information */
  422. if (at_resp_parse_line_args(resp, i, ESP32_NETSTAT_EXPRESSION, type, ipaddr, &remote_port, &local_port) < 0)
  423. {
  424. goto __exit;
  425. }
  426. else
  427. {
  428. LOG_RAW("%s: %s:%d ==> %s:%d\n", type, inet_ntoa(netdev->ip_addr), local_port, ipaddr, remote_port);
  429. }
  430. }
  431. }
  432. __exit:
  433. if (resp)
  434. {
  435. at_delete_resp(resp);
  436. }
  437. if (type)
  438. {
  439. rt_free(type);
  440. }
  441. if (ipaddr)
  442. {
  443. rt_free(ipaddr);
  444. }
  445. }
  446. #endif /* NETDEV_USING_NETSTAT */
  447. static const struct netdev_ops esp32_netdev_ops =
  448. {
  449. esp32_netdev_set_up,
  450. esp32_netdev_set_down,
  451. esp32_netdev_set_addr_info,
  452. esp32_netdev_set_dns_server,
  453. esp32_netdev_set_dhcp,
  454. #ifdef NETDEV_USING_PING
  455. esp32_netdev_ping,
  456. #endif
  457. #ifdef NETDEV_USING_NETSTAT
  458. esp32_netdev_netstat,
  459. #endif
  460. };
  461. static struct netdev *esp32_netdev_add(const char *netdev_name)
  462. {
  463. #define ETHERNET_MTU 1500
  464. #define HWADDR_LEN 6
  465. struct netdev *netdev = RT_NULL;
  466. RT_ASSERT(netdev_name);
  467. netdev = netdev_get_by_name(netdev_name);
  468. if (netdev != RT_NULL)
  469. {
  470. return (netdev);
  471. }
  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_work *net_work = RT_NULL;
  502. net_work = (struct rt_work *)rt_calloc(1, sizeof(struct rt_work));
  503. if (net_work == RT_NULL)
  504. {
  505. return;
  506. }
  507. rt_work_init(net_work, esp32_get_netdev_info, (void *)device);
  508. rt_work_submit(net_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 *)rt_container_of(device, struct at_device_esp32, device);
  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. ESP32_GMR_AT_VERSION = esp32_at_version_to_hex(at_resp_get_line(resp, 1));
  545. /* show module version */
  546. for (i = 0; i < resp->line_counts - 1; i++)
  547. {
  548. LOG_D("%s", at_resp_get_line(resp, i + 1));
  549. }
  550. AT_SEND_CMD(client, resp, "AT+CIPMUX=1");
  551. /* initialize successfully */
  552. result = RT_EOK;
  553. break;
  554. __exit:
  555. if (result != RT_EOK)
  556. {
  557. rt_thread_mdelay(1000);
  558. LOG_I("%s device initialize retry...", device->name);
  559. }
  560. }
  561. /* connect to WiFi AP */
  562. if (at_obj_exec_cmd(client, at_resp_set_info(resp, 512, 0, 20 * RT_TICK_PER_SECOND),
  563. "AT+CWJAP=\"%s\",\"%s\"", esp32->wifi_ssid, esp32->wifi_password) != RT_EOK)
  564. {
  565. LOG_W("%s device wifi connect failed, check ssid(%s) and password(%s).",
  566. device->name, esp32->wifi_ssid, esp32->wifi_password);
  567. }
  568. else
  569. {
  570. wifi_is_conn = RT_TRUE;
  571. }
  572. if (resp)
  573. {
  574. at_delete_resp(resp);
  575. }
  576. if (result != RT_EOK)
  577. {
  578. netdev_low_level_set_status(device->netdev, RT_FALSE);
  579. LOG_E("%s device network initialize failed(%d).", device->name, result);
  580. }
  581. else
  582. {
  583. device->is_init = RT_TRUE;
  584. netdev_low_level_set_status(device->netdev, RT_TRUE);
  585. if (wifi_is_conn)
  586. {
  587. netdev_low_level_set_link_status(device->netdev, RT_TRUE);
  588. }
  589. esp32_netdev_start_delay_work(device);
  590. LOG_I("%s device network initialize successfully.", device->name);
  591. }
  592. }
  593. static int esp32_net_init(struct at_device *device)
  594. {
  595. #ifdef AT_DEVICE_ESP32_INIT_ASYN
  596. rt_thread_t tid;
  597. tid = rt_thread_create("esp_net", esp32_init_thread_entry, (void *) device,
  598. ESP32_THREAD_STACK_SIZE, ESP32_THREAD_PRIORITY, 20);
  599. if (tid)
  600. {
  601. rt_thread_startup(tid);
  602. }
  603. else
  604. {
  605. LOG_E("create %s device init thread failed.", device->name);
  606. return -RT_ERROR;
  607. }
  608. #else
  609. esp32_init_thread_entry(device);
  610. #endif /* AT_DEVICE_ESP32_INIT_ASYN */
  611. return RT_EOK;
  612. }
  613. static void urc_busy_p_func(struct at_client *client, const char *data, rt_size_t size)
  614. {
  615. LOG_D("system is processing a commands...");
  616. }
  617. static void urc_busy_s_func(struct at_client *client, const char *data, rt_size_t size)
  618. {
  619. LOG_D("system is sending data...");
  620. }
  621. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  622. {
  623. struct at_device *device = RT_NULL;
  624. RT_ASSERT(client && data && size);
  625. char *client_name = client->device->parent.name;
  626. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  627. if (device == RT_NULL)
  628. {
  629. LOG_E("get device(%s) failed.", client_name);
  630. return;
  631. }
  632. if (rt_strstr(data, "WIFI CONNECTED"))
  633. {
  634. LOG_I("%s device wifi is connected.", device->name);
  635. if (device->is_init)
  636. {
  637. netdev_low_level_set_link_status(device->netdev, RT_TRUE);
  638. esp32_netdev_start_delay_work(device);
  639. }
  640. }
  641. else if (rt_strstr(data, "WIFI DISCONNECT"))
  642. {
  643. LOG_I("%s device wifi is disconnect.", device->name);
  644. if (device->is_init)
  645. {
  646. netdev_low_level_set_link_status(device->netdev, RT_FALSE);
  647. }
  648. }
  649. }
  650. static const struct at_urc urc_table[] =
  651. {
  652. {"busy p", "\r\n", urc_busy_p_func},
  653. {"busy s", "\r\n", urc_busy_s_func},
  654. {"WIFI CONNECTED", "\r\n", urc_func},
  655. {"WIFI DISCONNECT", "\r\n", urc_func},
  656. };
  657. static int esp32_init(struct at_device *device)
  658. {
  659. struct at_device_esp32 *esp32 = rt_container_of(device, struct at_device_esp32, device);
  660. /* initialize AT client */
  661. #if RT_VER_NUM >= 0x50100
  662. at_client_init(esp32->client_name, esp32->recv_line_num, esp32->recv_line_num);
  663. #else
  664. at_client_init(esp32->client_name, esp32->recv_line_num);
  665. #endif
  666. device->client = at_client_get(esp32->client_name);
  667. if (device->client == RT_NULL)
  668. {
  669. LOG_E("get AT client(%s) failed.", esp32->client_name);
  670. return -RT_ERROR;
  671. }
  672. /* register URC data execution function */
  673. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  674. #ifdef AT_USING_SOCKET
  675. esp32_socket_init(device);
  676. #endif
  677. /* add esp32 device to the netdev list */
  678. device->netdev = esp32_netdev_add(esp32->device_name);
  679. if (device->netdev == RT_NULL)
  680. {
  681. LOG_E("add netdev(%s) failed.", esp32->device_name);
  682. return -RT_ERROR;
  683. }
  684. /* initialize esp32 device network */
  685. return esp32_netdev_set_up(device->netdev);
  686. }
  687. static int esp32_deinit(struct at_device *device)
  688. {
  689. return esp32_netdev_set_down(device->netdev);
  690. }
  691. /**
  692. * Set the host name of the ESP32 device.
  693. *
  694. * @param device Pointer to the AT device structure.
  695. * @param host_name Host name string to set for the ESP32 device.
  696. *
  697. * @return RT_EOK on success, -RT_ERROR on failure.
  698. */
  699. static int esp32_set_host_name(struct at_device *device, const char *host_name)
  700. {
  701. int result = RT_EOK;
  702. struct at_client *client = device->client;
  703. if (host_name == RT_NULL || device->is_init == RT_FALSE)
  704. {
  705. return -RT_ERROR;
  706. }
  707. result = at_obj_exec_cmd(client, RT_NULL, "AT+CWHOSTNAME=\"%s\"", host_name);
  708. return result;
  709. }
  710. /* reset esp32 device and initialize device network again */
  711. static int esp32_reset(struct at_device *device)
  712. {
  713. int result = RT_EOK;
  714. struct at_client *client = device->client;
  715. /* send "AT+RST" commonds to esp32 device */
  716. result = at_obj_exec_cmd(client, RT_NULL, "AT+RST");
  717. rt_thread_mdelay(1000);
  718. /* waiting 10 seconds for esp32 device reset */
  719. device->is_init = RT_FALSE;
  720. if (at_client_obj_wait_connect(client, ESP32_WAIT_CONNECT_TIME))
  721. {
  722. return -RT_ETIMEOUT;
  723. }
  724. /* initialize esp32 device network */
  725. esp32_net_init(device);
  726. device->is_init = RT_TRUE;
  727. return result;
  728. }
  729. /* change esp32 wifi ssid and password information */
  730. static int esp32_wifi_info_set(struct at_device *device, struct at_device_ssid_pwd *info)
  731. {
  732. int result = RT_EOK;
  733. struct at_response *resp = RT_NULL;
  734. if (info->ssid == RT_NULL || info->password == RT_NULL)
  735. {
  736. LOG_E("input wifi ssid(%s) and password(%s) error.", info->ssid, info->password);
  737. return -RT_ERROR;
  738. }
  739. resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
  740. if (resp == RT_NULL)
  741. {
  742. LOG_E("no memory for resp create.");
  743. return -RT_ENOMEM;
  744. }
  745. /* connect to input wifi ap */
  746. if (at_obj_exec_cmd(device->client, resp, "AT+CWJAP=\"%s\",\"%s\"", info->ssid, info->password) != RT_EOK)
  747. {
  748. LOG_E("%s device wifi connect failed, check ssid(%s) and password(%s).",
  749. device->name, info->ssid, info->password);
  750. result = -RT_ERROR;
  751. }
  752. if (resp)
  753. {
  754. at_delete_resp(resp);
  755. }
  756. return result;
  757. }
  758. static int esp32_control(struct at_device *device, int cmd, void *arg)
  759. {
  760. int result = -RT_ERROR;
  761. RT_ASSERT(device);
  762. switch (cmd)
  763. {
  764. case AT_DEVICE_CTRL_POWER_ON:
  765. case AT_DEVICE_CTRL_POWER_OFF:
  766. case AT_DEVICE_CTRL_LOW_POWER:
  767. case AT_DEVICE_CTRL_SLEEP:
  768. case AT_DEVICE_CTRL_WAKEUP:
  769. case AT_DEVICE_CTRL_NET_CONN:
  770. case AT_DEVICE_CTRL_NET_DISCONN:
  771. case AT_DEVICE_CTRL_GET_SIGNAL:
  772. case AT_DEVICE_CTRL_GET_GPS:
  773. case AT_DEVICE_CTRL_GET_VER:
  774. LOG_W("not support the control cmd(%d).", cmd);
  775. break;
  776. case AT_DEVICE_CTRL_RESET:
  777. result = esp32_reset(device);
  778. break;
  779. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  780. result = esp32_wifi_info_set(device, (struct at_device_ssid_pwd *) arg);
  781. break;
  782. case AT_DEVICE_CTRL_SET_HOST_NAME:
  783. result = esp32_set_host_name(device, (const char *)arg);
  784. break;
  785. default:
  786. LOG_E("input error control cmd(%d).", cmd);
  787. break;
  788. }
  789. return result;
  790. }
  791. static const struct at_device_ops esp32_device_ops =
  792. {
  793. esp32_init,
  794. esp32_deinit,
  795. esp32_control,
  796. };
  797. static int esp32_device_class_register(void)
  798. {
  799. struct at_device_class *class = RT_NULL;
  800. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  801. if (class == RT_NULL)
  802. {
  803. LOG_E("no memory for class create.");
  804. return -RT_ENOMEM;
  805. }
  806. /* fill ESP32 device class object */
  807. #ifdef AT_USING_SOCKET
  808. esp32_socket_class_register(class);
  809. #endif
  810. class->device_ops = &esp32_device_ops;
  811. return at_device_class_register(class, AT_DEVICE_CLASS_ESP32);
  812. }
  813. INIT_DEVICE_EXPORT(esp32_device_class_register);
  814. /**
  815. * Convert the ESP32 AT version string to hexadecimal
  816. *
  817. * @param string containing the AT version
  818. *
  819. * @return hex_number: Hexadecimal AT version number, example: 1.4.1.1 -> 0x1040101
  820. */
  821. unsigned int esp32_at_version_to_hex(const char *str)
  822. {
  823. const char *version_prefix = "AT version:";
  824. char *version_start;
  825. unsigned int numbers[4];
  826. unsigned int hex_number;
  827. if (str == NULL || *str == '\0')
  828. {
  829. LOG_E("Invalid AT version format\n");
  830. return 0;
  831. }
  832. /* Get AT version string */
  833. version_start = strstr(str, version_prefix);
  834. if (version_start)
  835. {
  836. if (rt_sscanf(version_start, "AT version:%d.%d.%d.%d", &numbers[0], &numbers[1], &numbers[2], &numbers[3]) == 4)
  837. {
  838. hex_number = (numbers[0] << 24) | (numbers[1] << 16) | (numbers[2] << 8) | numbers[3];
  839. }
  840. else
  841. {
  842. LOG_W("The AT instruction format is not standard\n");
  843. return 0;
  844. }
  845. }
  846. else
  847. {
  848. LOG_W("The AT+GMR instruction is not supported\n");
  849. return 0;
  850. }
  851. return hex_number;
  852. }
  853. /**
  854. * This function is used to obtain the ESP32 AT version number.
  855. *
  856. * @return hex_number: Hexadecimal AT version number, example: 1.4.1.1 -> 0x1040101
  857. */
  858. unsigned int esp32_get_at_version(void)
  859. {
  860. return ESP32_GMR_AT_VERSION;
  861. }
  862. int esp32_scan_ap(struct at_device *device, at_ap_info_t *ap_info, uint8_t num)
  863. {
  864. int result = RT_EOK;
  865. struct at_client *client = device->client;
  866. struct at_response *resp = RT_NULL;
  867. uint8_t ap_count = 0;
  868. RT_ASSERT(device && ap_info && num);
  869. resp = at_create_resp(2048, 0, 20 * RT_TICK_PER_SECOND);
  870. if (resp == RT_NULL)
  871. {
  872. LOG_E("no memory for resp create.");
  873. return 0;
  874. }
  875. if (at_obj_exec_cmd(client, resp, "AT+CWLAP") < 0)
  876. {
  877. result = -RT_ERROR;
  878. goto __exit;
  879. }
  880. //+CWLAP:(3,"HONOR 20",-51,"22:a6:f3:40:da:3d",1)
  881. at_ap_info_t *ap_info_ptr;
  882. for (int i = 1; i <= resp->line_counts && ap_count < num; i++)
  883. {
  884. ap_info_ptr = &ap_info[ap_count];
  885. char mac[32];
  886. int temp_ecn, temp_rssi, temp_channel;
  887. if (at_resp_parse_line_args(resp, i,
  888. "%*[^(](%d,\"%31[^\"]\",%d,\"%31[^\"]\",%d)",
  889. &temp_ecn, ap_info_ptr->ssid, &temp_rssi,
  890. mac, &temp_channel) == 5)
  891. {
  892. ap_info_ptr->ecn = (uint8_t)temp_ecn;
  893. ap_info_ptr->rssi = (int8_t)temp_rssi;
  894. ap_info_ptr->channel = (uint8_t)temp_channel;
  895. rt_uint32_t mac_addr[6] = {0};
  896. rt_sscanf(mac, "%x:%x:%x:%x:%x:%x",
  897. &mac_addr[0], &mac_addr[1], &mac_addr[2],
  898. &mac_addr[3], &mac_addr[4], &mac_addr[5]);
  899. for (int j = 0; j < 6; j++)
  900. {
  901. ap_info_ptr->mac[j] = (uint8_t)mac_addr[j];
  902. }
  903. ap_count++;
  904. }
  905. }
  906. __exit:
  907. if (resp)
  908. {
  909. at_delete_resp(resp);
  910. }
  911. if (result != RT_EOK)
  912. {
  913. return 0;
  914. }
  915. else
  916. {
  917. return ap_count;
  918. }
  919. }
  920. #endif /* AT_DEVICE_USING_ESP32 */