Skip to content

Commit 1ba3ddb

Browse files
authored
support static IP on ESP-IDF platforms (#2156)
* add static IP support * Update cmd_tokenizer.c * Update cmd_tokenizer.c * Update new_common.h * Update cmd_tokenizer.c
1 parent faf548a commit 1ba3ddb

3 files changed

Lines changed: 127 additions & 3 deletions

File tree

src/cmnds/cmd_tokenizer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static int tok_flags = 0;
2323

2424
int str_to_ip(const char *s, byte *ip) {
2525
#if PLATFORM_W600 || PLATFORM_LN882H || PLATFORM_REALTEK || PLATFORM_ECR6600 || PLATFORM_TR6260 \
26-
|| PLATFORM_XRADIO || PLATFORM_TXW81X || PLATFORM_LN8825 || PLATFORM_ESP8266 || PLATFORM_GD32VW553
26+
|| PLATFORM_XRADIO || PLATFORM_TXW81X || PLATFORM_LN8825 || PLATFORM_ESP8266 || PLATFORM_ESPIDF || PLATFORM_GD32VW553
2727
// %hhu is a C99 thing, and newlib-nano/mculib don't support it
2828
int tmp_ip[4];
2929
int res;

src/hal/espidf/hal_wifi_espidf.c

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,98 @@ static esp_netif_ip_info_t g_ip_info;
4343
esp_event_handler_instance_t instance_any_id, instance_got_ip;
4444
bool handlers_registered = false;
4545

46+
#if PLATFORM_ESPIDF
47+
static obkStaticIP_t g_staticIP;
48+
static bool g_bStaticIP = false;
49+
50+
static void HAL_ESP_IP4AddrFromBytes(ip4_addr_t* out, const unsigned char* src)
51+
{
52+
IP4_ADDR(out, src[0], src[1], src[2], src[3]);
53+
}
54+
55+
static bool HAL_ESP_IsStaticIPConfigured(const obkStaticIP_t* ip)
56+
{
57+
return ip != NULL && ip->localIPAddr[0] != 0;
58+
}
59+
60+
static void HAL_ESP_RecordStaticIPConfig(const obkStaticIP_t* ip)
61+
{
62+
g_bStaticIP = false;
63+
memset(&g_staticIP, 0, sizeof(g_staticIP));
64+
65+
if (!HAL_ESP_IsStaticIPConfigured(ip)) {
66+
return;
67+
}
68+
69+
memcpy(&g_staticIP, ip, sizeof(g_staticIP));
70+
g_bStaticIP = true;
71+
}
72+
73+
static void HAL_ESP_BuildStaticIPInfo(esp_netif_ip_info_t* ip_info)
74+
{
75+
ip4_addr_t tmp;
76+
77+
memset(ip_info, 0, sizeof(*ip_info));
78+
79+
HAL_ESP_IP4AddrFromBytes(&tmp, g_staticIP.localIPAddr);
80+
ip_info->ip.addr = tmp.addr;
81+
HAL_ESP_IP4AddrFromBytes(&tmp, g_staticIP.netMask);
82+
ip_info->netmask.addr = tmp.addr;
83+
HAL_ESP_IP4AddrFromBytes(&tmp, g_staticIP.gatewayIPAddr);
84+
ip_info->gw.addr = tmp.addr;
85+
}
86+
87+
static bool HAL_ESP_StaticIPMatches(const esp_netif_ip_info_t* ip_info)
88+
{
89+
esp_netif_ip_info_t static_ip_info;
90+
91+
if (!g_bStaticIP || ip_info == NULL) {
92+
return true;
93+
}
94+
95+
HAL_ESP_BuildStaticIPInfo(&static_ip_info);
96+
return ip_info->ip.addr == static_ip_info.ip.addr
97+
&& ip_info->netmask.addr == static_ip_info.netmask.addr
98+
&& ip_info->gw.addr == static_ip_info.gw.addr;
99+
}
100+
101+
static bool HAL_ESP_ApplyStaticIP(void)
102+
{
103+
esp_err_t err;
104+
esp_netif_ip_info_t ip_info;
105+
esp_netif_dns_info_t dns_info;
106+
ip4_addr_t dns;
107+
108+
if (!g_bStaticIP || sta_netif == NULL) {
109+
return false;
110+
}
111+
112+
err = esp_netif_dhcpc_stop(sta_netif);
113+
if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
114+
ADDLOG_ERROR(LOG_FEATURE_MAIN, "ESP static IP: failed to stop DHCP client");
115+
return false;
116+
}
117+
118+
HAL_ESP_BuildStaticIPInfo(&ip_info);
119+
if (esp_netif_set_ip_info(sta_netif, &ip_info) != ESP_OK) {
120+
ADDLOG_ERROR(LOG_FEATURE_MAIN, "ESP static IP: failed to set IP info");
121+
return false;
122+
}
123+
124+
HAL_ESP_IP4AddrFromBytes(&dns, g_staticIP.dnsServerIpAddr);
125+
memset(&dns_info, 0, sizeof(dns_info));
126+
dns_info.ip.type = IPADDR_TYPE_V4;
127+
dns_info.ip.u_addr.ip4.addr = dns.addr;
128+
if (esp_netif_set_dns_info(sta_netif, ESP_NETIF_DNS_MAIN, &dns_info) != ESP_OK) {
129+
ADDLOG_ERROR(LOG_FEATURE_MAIN, "ESP static IP: failed to set DNS");
130+
return false;
131+
}
132+
133+
g_ip_info = ip_info;
134+
return true;
135+
}
136+
#endif
137+
46138
// This must return correct IP for both SOFT_AP and STATION modes,
47139
// because, for example, javascript control panel requires it
48140
const char* HAL_GetMyIPString()
@@ -60,6 +152,18 @@ const char* HAL_GetMyDNSString()
60152
#if PLATFORM_ESP8266
61153
ip4_addr_t dns = dhcps_dns_getserver();
62154
return ipaddr_ntoa(&dns);
155+
#elif PLATFORM_ESPIDF
156+
static char dnsStr[16];
157+
esp_netif_dns_info_t dns;
158+
159+
if (sta_netif != NULL
160+
&& esp_netif_get_dns_info(sta_netif, ESP_NETIF_DNS_MAIN, &dns) == ESP_OK
161+
&& dns.ip.type == IPADDR_TYPE_V4) {
162+
strncpy(dnsStr, ipaddr_ntoa((ip4_addr_t*)&dns.ip.u_addr.ip4), sizeof(dnsStr));
163+
dnsStr[sizeof(dnsStr) - 1] = 0;
164+
return dnsStr;
165+
}
166+
return "error";
63167
#else
64168
return "error";
65169
#endif
@@ -147,6 +251,12 @@ void event_handler(void* arg, esp_event_base_t event_base,
147251
ADDLOG_INFO(LOG_FEATURE_MAIN, "WiFi Connecting...");
148252
esp_wifi_connect();
149253
}
254+
#if PLATFORM_ESPIDF
255+
else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED && !g_bOpenAccessPointMode)
256+
{
257+
HAL_ESP_ApplyStaticIP();
258+
}
259+
#endif
150260
else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
151261
{
152262
if(g_wifiStatusCallback != NULL)
@@ -158,6 +268,13 @@ void event_handler(void* arg, esp_event_base_t event_base,
158268
else if(event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
159269
{
160270
ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
271+
#if PLATFORM_ESPIDF
272+
if(g_bStaticIP && !HAL_ESP_StaticIPMatches(&event->ip_info))
273+
{
274+
HAL_ESP_ApplyStaticIP();
275+
return;
276+
}
277+
#endif
161278
g_ip_info = event->ip_info;
162279
if(g_wifiStatusCallback != NULL)
163280
{
@@ -187,6 +304,11 @@ void HAL_ConnectToWiFi(const char* oob_ssid, const char* connect_key, obkStaticI
187304
delay_ms(50);
188305
}
189306

307+
sta_netif = esp_netif_create_default_wifi_sta();
308+
#if PLATFORM_ESPIDF
309+
HAL_ESP_RecordStaticIPConfig(ip);
310+
#endif
311+
190312
if(!handlers_registered)
191313
{
192314
esp_event_handler_instance_register(WIFI_EVENT,
@@ -202,7 +324,6 @@ void HAL_ConnectToWiFi(const char* oob_ssid, const char* connect_key, obkStaticI
202324
handlers_registered = true;
203325
}
204326

205-
sta_netif = esp_netif_create_default_wifi_sta();
206327
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
207328
esp_wifi_init(&cfg);
208329

@@ -235,6 +356,9 @@ void HAL_DisconnectFromWifi()
235356
int HAL_SetupWiFiOpenAccessPoint(const char* ssid)
236357
{
237358
g_bOpenAccessPointMode = 1;
359+
#if PLATFORM_ESPIDF
360+
g_bStaticIP = false;
361+
#endif
238362
ap_netif = esp_netif_create_default_wifi_ap();
239363
sta_netif = esp_netif_create_default_wifi_sta();
240364
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

src/new_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ typedef enum
12621262
} WIFI_RSSI_LEVEL;
12631263

12641264
#if PLATFORM_LN882H || PLATFORM_REALTEK || PLATFORM_ECR6600 || PLATFORM_TR6260 || PLATFORM_XRADIO \
1265-
|| PLATFORM_TXW81X || PLATFORM_LN8825 || PLATFORM_ESP8266 || PLATFORM_GD32VW553
1265+
|| PLATFORM_TXW81X || PLATFORM_LN8825 || PLATFORM_ESP8266 || PLATFORM_ESPIDF || PLATFORM_GD32VW553
12661266
#define IP_STRING_FORMAT "%u.%u.%u.%u"
12671267
#else
12681268
#define IP_STRING_FORMAT "%hhu.%hhu.%hhu.%hhu"

0 commit comments

Comments
 (0)