| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087 |
- /*
- * Copyright (c) 2006-2023, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2019-10-26 zylx first version
- */
- #include <stdio.h>
- #include <string.h>
- #include <at_device_esp32.h>
- #define LOG_TAG "at.dev.esp32"
- #include <at_log.h>
- #ifdef AT_DEVICE_USING_ESP32
- #define ESP32_WAIT_CONNECT_TIME 5000
- #define ESP32_THREAD_STACK_SIZE 2048
- #define ESP32_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
- unsigned int ESP32_GMR_AT_VERSION;
- /* ============================= esp32 network interface operations ============================= */
- static int esp32_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server);
- static void esp32_get_netdev_info(struct rt_work *work, void *work_data)
- {
- #define AT_ADDR_LEN 32
- #define AT_ERR_DNS_SERVER "255.255.255.255"
- #define AT_DEF_DNS_SERVER "114.114.114.114"
- at_response_t resp = RT_NULL;
- char ip[AT_ADDR_LEN] = {0}, mac[AT_ADDR_LEN] = {0};
- char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0};
- char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0};
- const char *resp_expr = "%*[^\"]\"%[^\"]\"";
- ip_addr_t ip_addr;
- rt_uint32_t mac_addr[6] = {0};
- rt_uint32_t num = 0;
- rt_uint32_t dhcp_stat = 0;
- struct at_device *device = (struct at_device *)work_data;
- struct netdev *netdev = device->netdev;
- struct at_client *client = device->client;
- if (work != RT_NULL)
- {
- rt_free(work);
- }
- resp = at_create_resp(512, 0, rt_tick_from_millisecond(300));
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp create.");
- return;
- }
- /* send mac addr query commond "AT+CIFSR" and wait response */
- if (at_obj_exec_cmd(client, resp, "AT+CIFSR") < 0)
- {
- goto __exit;
- }
- if (at_resp_parse_line_args_by_kw(resp, "STAMAC", resp_expr, mac) <= 0)
- {
- LOG_E("%s device parse \"AT+CIFSR\" cmd error.", device->name);
- goto __exit;
- }
- /* send addr info query commond "AT+CIPSTA?" and wait response */
- if (at_obj_exec_cmd(client, resp, "AT+CIPSTA?") < 0)
- {
- LOG_E("%s device send \"AT+CIPSTA?\" cmd error.", device->name);
- goto __exit;
- }
- if (at_resp_parse_line_args_by_kw(resp, "ip", resp_expr, ip) <= 0 ||
- at_resp_parse_line_args_by_kw(resp, "gateway", resp_expr, gateway) <= 0 ||
- at_resp_parse_line_args_by_kw(resp, "netmask", resp_expr, netmask) <= 0)
- {
- LOG_E("%s device prase \"AT+CIPSTA?\" cmd error.", device->name);
- goto __exit;
- }
- /* set netdev info */
- inet_aton(gateway, &ip_addr);
- netdev_low_level_set_gw(netdev, &ip_addr);
- inet_aton(netmask, &ip_addr);
- netdev_low_level_set_netmask(netdev, &ip_addr);
- inet_aton(ip, &ip_addr);
- netdev_low_level_set_ipaddr(netdev, &ip_addr);
- rt_sscanf(mac, "%x:%x:%x:%x:%x:%x",
- &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
- for (num = 0; num < netdev->hwaddr_len; num++)
- {
- netdev->hwaddr[num] = mac_addr[num];
- }
- /* send dns server query commond "AT+CIPDNS?" and wait response */
- if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS?") < 0)
- {
- LOG_W("please check and update %s device firmware to support the \"AT+CIPDNS?\" cmd.", device->name);
- goto __exit;
- }
- /* +CIPDNS:0,"208.67.222.222","8.8.8.8" */
- if (at_resp_parse_line_args_by_kw(resp, "+CIPDNS:", "%*[^\"]\"%[^\"]\",\"%[^\"]\"", dns_server1, dns_server2) < 0)
- {
- LOG_E("%s device prase \"AT+CIPDNS?\" cmd error.", device->name);
- goto __exit;
- }
- /* set primary DNS server address */
- if (rt_strlen(dns_server1) > 0 &&
- rt_strncmp(dns_server1, AT_ERR_DNS_SERVER, rt_strlen(AT_ERR_DNS_SERVER)) != 0)
- {
- inet_aton(dns_server1, &ip_addr);
- netdev_low_level_set_dns_server(netdev, 0, &ip_addr);
- }
- else
- {
- inet_aton(AT_DEF_DNS_SERVER, &ip_addr);
- esp32_netdev_set_dns_server(netdev, 0, &ip_addr);
- }
- /* set secondary DNS server address */
- if (rt_strlen(dns_server2) > 0 )
- {
- inet_aton(dns_server2, &ip_addr);
- netdev_low_level_set_dns_server(netdev, 1, &ip_addr);
- }
- /* send DHCP query commond " AT+CWDHCP" and wait response */
- if (at_obj_exec_cmd(client, resp, "AT+CWDHCP?") < 0)
- {
- goto __exit;
- }
- /* parse response data, get the DHCP status */
- if (at_resp_parse_line_args_by_kw(resp, "+CWDHCP:", "+CWDHCP:%d", &dhcp_stat) < 0)
- {
- LOG_E("%s device prase DHCP status error.", device->name);
- goto __exit;
- }
- /* Bit0 - Station DHCP status, Bit1 - SoftAP DHCP status */
- netdev_low_level_set_dhcp_status(netdev, dhcp_stat & 0x01 ? RT_TRUE : RT_FALSE);
- __exit:
- if (resp)
- {
- at_delete_resp(resp);
- }
- }
- static int esp32_net_init(struct at_device *device);
- static int esp32_netdev_set_up(struct netdev *netdev)
- {
- struct at_device *device = RT_NULL;
- device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
- if (device == RT_NULL)
- {
- LOG_E("get device(%s) failed.", netdev->name);
- return -RT_ERROR;
- }
- if (device->is_init == RT_FALSE)
- {
- esp32_net_init(device);
- netdev_low_level_set_status(netdev, RT_TRUE);
- LOG_D("network interface device(%s) set up status", netdev->name);
- }
- return RT_EOK;
- }
- static int esp32_netdev_set_down(struct netdev *netdev)
- {
- struct at_device *device = RT_NULL;
- device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
- if (device == RT_NULL)
- {
- LOG_E("get device by netdev(%s) failed.", netdev->name);
- return -RT_ERROR;
- }
- if (device->is_init == RT_TRUE)
- {
- device->is_init = RT_FALSE;
- netdev_low_level_set_status(netdev, RT_FALSE);
- LOG_D("network interface device(%s) set down status", netdev->name);
- }
- return RT_EOK;
- }
- static int esp32_netdev_set_addr_info(struct netdev *netdev, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
- {
- #define IPADDR_RESP_SIZE 128
- #define IPADDR_SIZE 16
- int result = RT_EOK;
- at_response_t resp = RT_NULL;
- struct at_device *device = RT_NULL;
- char ip_str[IPADDR_SIZE] = {0};
- char gw_str[IPADDR_SIZE] = {0};
- char netmask_str[IPADDR_SIZE] = {0};
- RT_ASSERT(netdev);
- RT_ASSERT(ip_addr || netmask || gw);
- device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
- if (device == RT_NULL)
- {
- LOG_E("get device(%s) failed.", netdev->name);
- return -RT_ERROR;
- }
- resp = at_create_resp(IPADDR_RESP_SIZE, 0, rt_tick_from_millisecond(300));
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp create.");
- result = -RT_ENOMEM;
- goto __exit;
- }
- /* Convert numeric IP address into decimal dotted ASCII representation. */
- if (ip_addr)
- rt_memcpy(ip_str, inet_ntoa(*ip_addr), IPADDR_SIZE);
- else
- rt_memcpy(ip_str, inet_ntoa(netdev->ip_addr), IPADDR_SIZE);
- if (gw)
- rt_memcpy(gw_str, inet_ntoa(*gw), IPADDR_SIZE);
- else
- rt_memcpy(gw_str, inet_ntoa(netdev->gw), IPADDR_SIZE);
- if (netmask)
- rt_memcpy(netmask_str, inet_ntoa(*netmask), IPADDR_SIZE);
- else
- rt_memcpy(netmask_str, inet_ntoa(netdev->netmask), IPADDR_SIZE);
- /* send addr info set commond "AT+CIPSTA=<ip>[,<gateway>,<netmask>]" and wait response */
- if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTA=\"%s\",\"%s\",\"%s\"",
- ip_str, gw_str, netmask_str) < 0)
- {
- LOG_E("%s device set address failed.", device->name);
- result = -RT_ERROR;
- }
- else
- {
- /* Update netdev information */
- if (ip_addr)
- netdev_low_level_set_ipaddr(netdev, ip_addr);
- if (gw)
- netdev_low_level_set_gw(netdev, gw);
- if (netmask)
- netdev_low_level_set_netmask(netdev, netmask);
- LOG_D("%s device set address success.", device->name);
- }
- __exit:
- if (resp)
- {
- at_delete_resp(resp);
- }
- return result;
- }
- static int esp32_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
- {
- #define DNS_RESP_SIZE 128
- int result = RT_EOK;
- at_response_t resp = RT_NULL;
- struct at_device *device = RT_NULL;
- RT_ASSERT(netdev);
- RT_ASSERT(dns_server);
- device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
- if (device == RT_NULL)
- {
- LOG_E("get device by netdev(%s) failed.", netdev->name);
- return -RT_ERROR;
- }
- resp = at_create_resp(DNS_RESP_SIZE, 0, rt_tick_from_millisecond(300));
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp create.");
- return -RT_ENOMEM;
- }
- /* send dns server set commond "AT+CIPDNS=<enable>[,<"DNS IP1">][,<"DNS IP2">][,<"DNS IP3">]" and wait response */
- if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS=1,\"%s\"", inet_ntoa(*dns_server)) < 0)
- {
- LOG_E("%s device set DNS failed.", device->name);
- result = -RT_ERROR;
- }
- else
- {
- netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
- LOG_D("%s device set DNS(%s) success.", device->name, inet_ntoa(*dns_server));
- }
- if (resp)
- {
- at_delete_resp(resp);
- }
- return result;
- }
- static int esp32_netdev_set_dhcp(struct netdev *netdev, rt_bool_t is_enabled)
- {
- #define ESP32_STATION 1 << 0
- #define ESP32_SOFTAP 1 << 1
- #define RESP_SIZE 128
- int result = RT_EOK;
- at_response_t resp = RT_NULL;
- struct at_device *device = RT_NULL;
- RT_ASSERT(netdev);
- device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
- if (device == RT_NULL)
- {
- LOG_E("get device by netdev(%s) failed.", netdev->name);
- return -RT_ERROR;
- }
- resp = at_create_resp(RESP_SIZE, 0, rt_tick_from_millisecond(300));
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp struct.");
- return -RT_ENOMEM;
- }
- /* send dhcp set commond "AT+CWDHCP=<operate>,<mode>" and wait response */
- if (at_obj_exec_cmd(device->client, resp, "AT+CWDHCP=%d,%d", is_enabled, ESP32_STATION) < 0)
- {
- LOG_E("%s device set DHCP status(%d) failed.", device->name, is_enabled);
- result = -RT_ERROR;
- goto __exit;
- }
- else
- {
- netdev_low_level_set_dhcp_status(netdev, is_enabled);
- LOG_D("%s device set DHCP status(%d) ok.", device->name, is_enabled);
- }
- __exit:
- if (resp)
- {
- at_delete_resp(resp);
- }
- return result;
- }
- #ifdef NETDEV_USING_PING
- static int esp32_netdev_ping(struct netdev *netdev, const char *host,
- size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
- #if RT_VER_NUM >= 0x50100
- , rt_bool_t is_bind
- #endif
- )
- {
- #define ESP32_PING_IP_SIZE 16
- rt_err_t result = RT_EOK;
- at_response_t resp = RT_NULL;
- struct at_device *device = RT_NULL;
- char ip_addr[ESP32_PING_IP_SIZE] = {0};
- int req_time;
- #if RT_VER_NUM >= 0x50100
- RT_UNUSED(is_bind);
- #endif
- RT_ASSERT(netdev);
- RT_ASSERT(host);
- RT_ASSERT(ping_resp);
- device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
- if (device == RT_NULL)
- {
- LOG_E("get device(%s) failed.", netdev->name);
- return -RT_ERROR;
- }
- resp = at_create_resp(64, 0, timeout);
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp create.");
- return -RT_ENOMEM;
- }
- /* send domain commond "AT+CIPDOMAIN=<domain name>" and wait response */
- if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=\"%s\"", host) < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- /* parse the third line of response data, get the IP address */
- if (at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", (esp32_get_at_version() <= ESP32_DEFAULT_AT_VERSION_NUM) ? \
- "+CIPDOMAIN:%s" : "+CIPDOMAIN:\"%[^\"]\"", ip_addr) < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- /* send ping commond "AT+PING=<IP>" and wait response */
- if (at_obj_exec_cmd(device->client, resp, "AT+PING=\"%s\"", host) < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- if (at_resp_parse_line_args_by_kw(resp, "+", (esp32_get_at_version() <= ESP32_DEFAULT_AT_VERSION_NUM) ?
- "+%d" : "+PING:%d", &req_time) < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- if (req_time)
- {
- inet_aton(ip_addr, &(ping_resp->ip_addr));
- ping_resp->data_len = data_len;
- ping_resp->ttl = 0;
- ping_resp->ticks = req_time;
- }
- __exit:
- if (resp)
- {
- at_delete_resp(resp);
- }
- return result;
- }
- #endif /* NETDEV_USING_PING */
- #ifdef NETDEV_USING_NETSTAT
- void esp32_netdev_netstat(struct netdev *netdev)
- {
- #define ESP32_NETSTAT_RESP_SIZE 320
- #define ESP32_NETSTAT_TYPE_SIZE 4
- #define ESP32_NETSTAT_IPADDR_SIZE 17
- #define ESP32_NETSTAT_EXPRESSION "+CIPSTATUS:%*d,\"%[^\"]\",\"%[^\"]\",%d,%d,%*d"
- at_response_t resp = RT_NULL;
- struct at_device *device = RT_NULL;
- int remote_port, local_port, i;
- char *type = RT_NULL;
- char *ipaddr = RT_NULL;
- device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
- if (device == RT_NULL)
- {
- LOG_E("get device(%s) failed.", netdev->name);
- return;
- }
- type = (char *) rt_calloc(1, ESP32_NETSTAT_TYPE_SIZE);
- ipaddr = (char *) rt_calloc(1, ESP32_NETSTAT_IPADDR_SIZE);
- if ((type && ipaddr) == RT_NULL)
- {
- LOG_E("no memory for ipaddr create.");
- goto __exit;
- }
- resp = at_create_resp(ESP32_NETSTAT_RESP_SIZE, 0, 5 * RT_TICK_PER_SECOND);
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp create.", device->name);
- goto __exit;
- }
- /* send network connection information commond "AT+CIPSTATUS" and wait response */
- if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTATUS") < 0)
- {
- goto __exit;
- }
- for (i = 1; i <= resp->line_counts; i++)
- {
- if (strstr(at_resp_get_line(resp, i), "+CIPSTATUS"))
- {
- /* parse the third line of response data, get the network connection information */
- if (at_resp_parse_line_args(resp, i, ESP32_NETSTAT_EXPRESSION, type, ipaddr, &remote_port, &local_port) < 0)
- {
- goto __exit;
- }
- else
- {
- LOG_RAW("%s: %s:%d ==> %s:%d\n", type, inet_ntoa(netdev->ip_addr), local_port, ipaddr, remote_port);
- }
- }
- }
- __exit:
- if (resp)
- {
- at_delete_resp(resp);
- }
- if (type)
- {
- rt_free(type);
- }
- if (ipaddr)
- {
- rt_free(ipaddr);
- }
- }
- #endif /* NETDEV_USING_NETSTAT */
- static const struct netdev_ops esp32_netdev_ops =
- {
- esp32_netdev_set_up,
- esp32_netdev_set_down,
- esp32_netdev_set_addr_info,
- esp32_netdev_set_dns_server,
- esp32_netdev_set_dhcp,
- #ifdef NETDEV_USING_PING
- esp32_netdev_ping,
- #endif
- #ifdef NETDEV_USING_NETSTAT
- esp32_netdev_netstat,
- #endif
- };
- static struct netdev *esp32_netdev_add(const char *netdev_name)
- {
- #define ETHERNET_MTU 1500
- #define HWADDR_LEN 6
- struct netdev *netdev = RT_NULL;
- RT_ASSERT(netdev_name);
- netdev = netdev_get_by_name(netdev_name);
- if (netdev != RT_NULL)
- {
- return (netdev);
- }
- netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
- if (netdev == RT_NULL)
- {
- LOG_E("no memory for netdev create.");
- return RT_NULL;
- }
- netdev->mtu = ETHERNET_MTU;
- netdev->ops = &esp32_netdev_ops;
- netdev->hwaddr_len = HWADDR_LEN;
- #ifdef SAL_USING_AT
- extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
- /* set the network interface socket/netdb operations */
- sal_at_netdev_set_pf_info(netdev);
- #endif
- netdev_register(netdev, netdev_name, RT_NULL);
- return netdev;
- }
- /* ============================= esp32 device operations ============================= */
- #define AT_SEND_CMD(client, resp, cmd) \
- do { \
- (resp) = at_resp_set_info((resp), 256, 0, 5 * RT_TICK_PER_SECOND); \
- if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
- { \
- result = -RT_ERROR; \
- goto __exit; \
- } \
- } while(0) \
- static void esp32_netdev_start_delay_work(struct at_device *device)
- {
- struct rt_work *net_work = RT_NULL;
- net_work = (struct rt_work *)rt_calloc(1, sizeof(struct rt_work));
- if (net_work == RT_NULL)
- {
- return;
- }
- rt_work_init(net_work, esp32_get_netdev_info, (void *)device);
- rt_work_submit(net_work, RT_TICK_PER_SECOND);
- }
- static void esp32_init_thread_entry(void *parameter)
- {
- #define INIT_RETRY 5
- struct at_device *device = (struct at_device *) parameter;
- struct at_device_esp32 *esp32 = (struct at_device_esp32 *)rt_container_of(device, struct at_device_esp32, device);
- struct at_client *client = device->client;
- at_response_t resp = RT_NULL;
- rt_err_t result = RT_EOK;
- rt_size_t i = 0, retry_num = INIT_RETRY;
- rt_bool_t wifi_is_conn = RT_FALSE;
- LOG_D("%s device initialize start.", device->name);
- /* wait esp32 device startup finish */
- if (at_client_obj_wait_connect(client, ESP32_WAIT_CONNECT_TIME))
- {
- return;
- }
- resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp create.");
- return;
- }
- while (retry_num--)
- {
- /* reset module */
- AT_SEND_CMD(client, resp, "AT+RST");
- /* reset waiting delay */
- rt_thread_mdelay(1000);
- /* disable echo */
- AT_SEND_CMD(client, resp, "ATE0");
- /* set current mode to Wi-Fi station */
- AT_SEND_CMD(client, resp, "AT+CWMODE=1");
- /* get module version */
- AT_SEND_CMD(client, resp, "AT+GMR");
- ESP32_GMR_AT_VERSION = esp32_at_version_to_hex(at_resp_get_line(resp, 1));
- /* show module version */
- for (i = 0; i < resp->line_counts - 1; i++)
- {
- LOG_D("%s", at_resp_get_line(resp, i + 1));
- }
- AT_SEND_CMD(client, resp, "AT+CIPMUX=1");
- /* initialize successfully */
- result = RT_EOK;
- break;
- __exit:
- if (result != RT_EOK)
- {
- rt_thread_mdelay(1000);
- LOG_I("%s device initialize retry...", device->name);
- }
- }
- /* connect to WiFi AP */
- if (at_obj_exec_cmd(client, at_resp_set_info(resp, 512, 0, 20 * RT_TICK_PER_SECOND),
- "AT+CWJAP=\"%s\",\"%s\"", esp32->wifi_ssid, esp32->wifi_password) != RT_EOK)
- {
- LOG_W("%s device wifi connect failed, check ssid(%s) and password(%s).",
- device->name, esp32->wifi_ssid, esp32->wifi_password);
- }
- else
- {
- wifi_is_conn = RT_TRUE;
- }
- if (resp)
- {
- at_delete_resp(resp);
- }
- if (result != RT_EOK)
- {
- netdev_low_level_set_status(device->netdev, RT_FALSE);
- LOG_E("%s device network initialize failed(%d).", device->name, result);
- }
- else
- {
- device->is_init = RT_TRUE;
- netdev_low_level_set_status(device->netdev, RT_TRUE);
- if (wifi_is_conn)
- {
- netdev_low_level_set_link_status(device->netdev, RT_TRUE);
- }
- esp32_netdev_start_delay_work(device);
- LOG_I("%s device network initialize successfully.", device->name);
- }
- }
- static int esp32_net_init(struct at_device *device)
- {
- #ifdef AT_DEVICE_ESP32_INIT_ASYN
- rt_thread_t tid;
- tid = rt_thread_create("esp_net", esp32_init_thread_entry, (void *) device,
- ESP32_THREAD_STACK_SIZE, ESP32_THREAD_PRIORITY, 20);
- if (tid)
- {
- rt_thread_startup(tid);
- }
- else
- {
- LOG_E("create %s device init thread failed.", device->name);
- return -RT_ERROR;
- }
- #else
- esp32_init_thread_entry(device);
- #endif /* AT_DEVICE_ESP32_INIT_ASYN */
- return RT_EOK;
- }
- static void urc_busy_p_func(struct at_client *client, const char *data, rt_size_t size)
- {
- LOG_D("system is processing a commands...");
- }
- static void urc_busy_s_func(struct at_client *client, const char *data, rt_size_t size)
- {
- LOG_D("system is sending data...");
- }
- static void urc_func(struct at_client *client, const char *data, rt_size_t size)
- {
- struct at_device *device = RT_NULL;
- RT_ASSERT(client && data && size);
- char *client_name = client->device->parent.name;
- device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
- if (device == RT_NULL)
- {
- LOG_E("get device(%s) failed.", client_name);
- return;
- }
- if (rt_strstr(data, "WIFI CONNECTED"))
- {
- LOG_I("%s device wifi is connected.", device->name);
- if (device->is_init)
- {
- netdev_low_level_set_link_status(device->netdev, RT_TRUE);
- esp32_netdev_start_delay_work(device);
- }
- }
- else if (rt_strstr(data, "WIFI DISCONNECT"))
- {
- LOG_I("%s device wifi is disconnect.", device->name);
- if (device->is_init)
- {
- netdev_low_level_set_link_status(device->netdev, RT_FALSE);
- }
- }
- }
- static const struct at_urc urc_table[] =
- {
- {"busy p", "\r\n", urc_busy_p_func},
- {"busy s", "\r\n", urc_busy_s_func},
- {"WIFI CONNECTED", "\r\n", urc_func},
- {"WIFI DISCONNECT", "\r\n", urc_func},
- };
- static int esp32_init(struct at_device *device)
- {
- struct at_device_esp32 *esp32 = rt_container_of(device, struct at_device_esp32, device);
- /* initialize AT client */
- #if RT_VER_NUM >= 0x50100
- at_client_init(esp32->client_name, esp32->recv_line_num, esp32->recv_line_num);
- #else
- at_client_init(esp32->client_name, esp32->recv_line_num);
- #endif
- device->client = at_client_get(esp32->client_name);
- if (device->client == RT_NULL)
- {
- LOG_E("get AT client(%s) failed.", esp32->client_name);
- return -RT_ERROR;
- }
- /* register URC data execution function */
- at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
- #ifdef AT_USING_SOCKET
- esp32_socket_init(device);
- #endif
- /* add esp32 device to the netdev list */
- device->netdev = esp32_netdev_add(esp32->device_name);
- if (device->netdev == RT_NULL)
- {
- LOG_E("add netdev(%s) failed.", esp32->device_name);
- return -RT_ERROR;
- }
- /* initialize esp32 device network */
- return esp32_netdev_set_up(device->netdev);
- }
- static int esp32_deinit(struct at_device *device)
- {
- return esp32_netdev_set_down(device->netdev);
- }
- /**
- * Set the host name of the ESP32 device.
- *
- * @param device Pointer to the AT device structure.
- * @param host_name Host name string to set for the ESP32 device.
- *
- * @return RT_EOK on success, -RT_ERROR on failure.
- */
- static int esp32_set_host_name(struct at_device *device, const char *host_name)
- {
- int result = RT_EOK;
- struct at_client *client = device->client;
- if (host_name == RT_NULL || device->is_init == RT_FALSE)
- {
- return -RT_ERROR;
- }
- result = at_obj_exec_cmd(client, RT_NULL, "AT+CWHOSTNAME=\"%s\"", host_name);
- return result;
- }
- /* reset esp32 device and initialize device network again */
- static int esp32_reset(struct at_device *device)
- {
- int result = RT_EOK;
- struct at_client *client = device->client;
- /* send "AT+RST" commonds to esp32 device */
- result = at_obj_exec_cmd(client, RT_NULL, "AT+RST");
- rt_thread_mdelay(1000);
- /* waiting 10 seconds for esp32 device reset */
- device->is_init = RT_FALSE;
- if (at_client_obj_wait_connect(client, ESP32_WAIT_CONNECT_TIME))
- {
- return -RT_ETIMEOUT;
- }
- /* initialize esp32 device network */
- esp32_net_init(device);
- device->is_init = RT_TRUE;
- return result;
- }
- /* change esp32 wifi ssid and password information */
- static int esp32_wifi_info_set(struct at_device *device, struct at_device_ssid_pwd *info)
- {
- int result = RT_EOK;
- struct at_response *resp = RT_NULL;
- if (info->ssid == RT_NULL || info->password == RT_NULL)
- {
- LOG_E("input wifi ssid(%s) and password(%s) error.", info->ssid, info->password);
- return -RT_ERROR;
- }
- resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp create.");
- return -RT_ENOMEM;
- }
- /* connect to input wifi ap */
- if (at_obj_exec_cmd(device->client, resp, "AT+CWJAP=\"%s\",\"%s\"", info->ssid, info->password) != RT_EOK)
- {
- LOG_E("%s device wifi connect failed, check ssid(%s) and password(%s).",
- device->name, info->ssid, info->password);
- result = -RT_ERROR;
- }
- if (resp)
- {
- at_delete_resp(resp);
- }
- return result;
- }
- static int esp32_control(struct at_device *device, int cmd, void *arg)
- {
- int result = -RT_ERROR;
- RT_ASSERT(device);
- switch (cmd)
- {
- case AT_DEVICE_CTRL_POWER_ON:
- case AT_DEVICE_CTRL_POWER_OFF:
- case AT_DEVICE_CTRL_LOW_POWER:
- case AT_DEVICE_CTRL_SLEEP:
- case AT_DEVICE_CTRL_WAKEUP:
- case AT_DEVICE_CTRL_NET_CONN:
- case AT_DEVICE_CTRL_NET_DISCONN:
- case AT_DEVICE_CTRL_GET_SIGNAL:
- case AT_DEVICE_CTRL_GET_GPS:
- case AT_DEVICE_CTRL_GET_VER:
- LOG_W("not support the control cmd(%d).", cmd);
- break;
- case AT_DEVICE_CTRL_RESET:
- result = esp32_reset(device);
- break;
- case AT_DEVICE_CTRL_SET_WIFI_INFO:
- result = esp32_wifi_info_set(device, (struct at_device_ssid_pwd *) arg);
- break;
- case AT_DEVICE_CTRL_SET_HOST_NAME:
- result = esp32_set_host_name(device, (const char *)arg);
- break;
- default:
- LOG_E("input error control cmd(%d).", cmd);
- break;
- }
- return result;
- }
- static const struct at_device_ops esp32_device_ops =
- {
- esp32_init,
- esp32_deinit,
- esp32_control,
- };
- static int esp32_device_class_register(void)
- {
- struct at_device_class *class = RT_NULL;
- class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
- if (class == RT_NULL)
- {
- LOG_E("no memory for class create.");
- return -RT_ENOMEM;
- }
- /* fill ESP32 device class object */
- #ifdef AT_USING_SOCKET
- esp32_socket_class_register(class);
- #endif
- class->device_ops = &esp32_device_ops;
- return at_device_class_register(class, AT_DEVICE_CLASS_ESP32);
- }
- INIT_DEVICE_EXPORT(esp32_device_class_register);
- /**
- * Convert the ESP32 AT version string to hexadecimal
- *
- * @param string containing the AT version
- *
- * @return hex_number: Hexadecimal AT version number, example: 1.4.1.1 -> 0x1040101
- */
- unsigned int esp32_at_version_to_hex(const char *str)
- {
- const char *version_prefix = "AT version:";
- char *version_start;
- unsigned int numbers[4];
- unsigned int hex_number;
- if (str == NULL || *str == '\0')
- {
- LOG_E("Invalid AT version format\n");
- return 0;
- }
- /* Get AT version string */
- version_start = strstr(str, version_prefix);
- if (version_start)
- {
- if (rt_sscanf(version_start, "AT version:%d.%d.%d.%d", &numbers[0], &numbers[1], &numbers[2], &numbers[3]) == 4)
- {
- hex_number = (numbers[0] << 24) | (numbers[1] << 16) | (numbers[2] << 8) | numbers[3];
- }
- else
- {
- LOG_W("The AT instruction format is not standard\n");
- return 0;
- }
- }
- else
- {
- LOG_W("The AT+GMR instruction is not supported\n");
- return 0;
- }
- return hex_number;
- }
- /**
- * This function is used to obtain the ESP32 AT version number.
- *
- * @return hex_number: Hexadecimal AT version number, example: 1.4.1.1 -> 0x1040101
- */
- unsigned int esp32_get_at_version(void)
- {
- return ESP32_GMR_AT_VERSION;
- }
- int esp32_scan_ap(struct at_device *device, at_ap_info_t *ap_info, uint8_t num)
- {
- int result = RT_EOK;
- struct at_client *client = device->client;
- struct at_response *resp = RT_NULL;
- uint8_t ap_count = 0;
- RT_ASSERT(device && ap_info && num);
- resp = at_create_resp(2048, 0, 20 * RT_TICK_PER_SECOND);
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp create.");
- return 0;
- }
- if (at_obj_exec_cmd(client, resp, "AT+CWLAP") < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- //+CWLAP:(3,"HONOR 20",-51,"22:a6:f3:40:da:3d",1)
- at_ap_info_t *ap_info_ptr;
- for (int i = 1; i <= resp->line_counts && ap_count < num; i++)
- {
- ap_info_ptr = &ap_info[ap_count];
- char mac[32];
- int temp_ecn, temp_rssi, temp_channel;
- if (at_resp_parse_line_args(resp, i,
- "%*[^(](%d,\"%31[^\"]\",%d,\"%31[^\"]\",%d)",
- &temp_ecn, ap_info_ptr->ssid, &temp_rssi,
- mac, &temp_channel) == 5)
- {
- ap_info_ptr->ecn = (uint8_t)temp_ecn;
- ap_info_ptr->rssi = (int8_t)temp_rssi;
- ap_info_ptr->channel = (uint8_t)temp_channel;
- rt_uint32_t mac_addr[6] = {0};
- rt_sscanf(mac, "%x:%x:%x:%x:%x:%x",
- &mac_addr[0], &mac_addr[1], &mac_addr[2],
- &mac_addr[3], &mac_addr[4], &mac_addr[5]);
- for (int j = 0; j < 6; j++)
- {
- ap_info_ptr->mac[j] = (uint8_t)mac_addr[j];
- }
- ap_count++;
- }
- }
- __exit:
- if (resp)
- {
- at_delete_resp(resp);
- }
- if (result != RT_EOK)
- {
- return 0;
- }
- else
- {
- return ap_count;
- }
- }
- #endif /* AT_DEVICE_USING_ESP32 */
|