-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathNF_ESP32_Network.cpp
118 lines (91 loc) · 2.85 KB
/
NF_ESP32_Network.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
// This file includes common method for networking code
#include "NF_ESP32_Network.h"
#include "esp_netif_net_stack.h"
// Wait for the network interface to become available
int NF_ESP32_Wait_NetNumber(int num)
{
int number = 0;
esp_netif_t *espNetif;
while (true)
{
switch (num)
{
case IDF_WIFI_STA_DEF:
espNetif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
break;
case IDF_WIFI_AP_DEF:
espNetif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
break;
case IDF_ETH_DEF:
espNetif = esp_netif_get_handle_from_ifkey("ETH_DEF");
break;
case IDF_OT_DEF:
espNetif = esp_netif_get_handle_from_ifkey("OT_DEF");
break;
default:
// can't reach here
HAL_AssertEx();
break;
}
if (espNetif != NULL)
{
break;
}
vTaskDelay(20 / portTICK_PERIOD_MS);
}
return espNetif->lwip_netif->num;
}
HAL_Configuration_NetworkInterface *NF_ESP32_GetNetworkConfigBlock(int index)
{
HAL_Configuration_NetworkInterface *networkConfig =
(HAL_Configuration_NetworkInterface *)platform_malloc(sizeof(HAL_Configuration_NetworkInterface));
if (networkConfig != NULL)
{
if (ConfigurationManager_GetConfigurationBlock(networkConfig, DeviceConfigurationOption_Network, index))
{
return networkConfig;
}
}
return NULL;
}
//
// Configure network settings for a espressif network interface
//
esp_err_t NF_ESP32_ConfigureNetwork(esp_netif_t *netIf, HAL_Configuration_NetworkInterface *config)
{
esp_err_t ec;
esp_netif_ip_info_t ip_info;
ec = esp_netif_get_ip_info(netIf, &ip_info);
if (ec != ESP_OK)
{
return ec;
}
bool enableDHCP = (config->StartupAddressMode == AddressMode_DHCP);
// Set static addresses
if (config->IPv4Address != 0)
{
ip_info.ip.addr = config->IPv4Address;
ip_info.netmask.addr = config->IPv4NetMask;
ip_info.gw.addr = config->IPv4GatewayAddress;
ec = esp_netif_set_ip_info(netIf, &ip_info);
// Make sure DHCP client is disabled
netIf->flags = (esp_netif_flags_t)(netIf->flags & ~ESP_NETIF_DHCP_CLIENT);
}
return ec;
}
esp_err_t NF_ESP32_ConfigureNetworkByIndex(int index, esp_netif_t *netIf)
{
esp_err_t ec;
HAL_Configuration_NetworkInterface *networkConfig = NF_ESP32_GetNetworkConfigBlock(index);
if (networkConfig == NULL)
{
return ESP_FAIL;
}
ec = NF_ESP32_ConfigureNetwork(netIf, networkConfig);
platform_free(networkConfig);
return ec;
}