Skip to content

Commit 3d306d7

Browse files
DejinChenwqx6
andauthored
ESP: Enable Wi-Fi and Ethernet coexisting and add platform mdns for Ethernet (project-chip#32428) (project-chip#33378)
Co-authored-by: Wang Qixiang <43193572+wqx6@users.noreply.github.com>
1 parent a4562ae commit 3d306d7

File tree

11 files changed

+147
-107
lines changed

11 files changed

+147
-107
lines changed

config/esp32/components/chip/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -441,16 +441,17 @@ if(CONFIG_OPENTHREAD_ENABLED)
441441
endif()
442442
endif()
443443

444-
if((NOT CONFIG_USE_MINIMAL_MDNS) AND (CONFIG_ENABLE_WIFI_STATION OR CONFIG_ENABLE_WIFI_AP))
444+
if(NOT CONFIG_USE_MINIMAL_MDNS)
445445
idf_build_get_property(build_components BUILD_COMPONENTS)
446446
# For IDF v5.x, the mdns component was moved to idf_managed_components.
447447
# We should use 'espressif__mdns' for 'idf_component_get_property'.
448448
if("espressif__mdns" IN_LIST build_components)
449449
idf_component_get_property(mdns_lib espressif__mdns COMPONENT_LIB)
450+
list(APPEND chip_libraries $<TARGET_FILE:${mdns_lib}>)
450451
elseif("mdns" IN_LIST build_components)
451452
idf_component_get_property(mdns_lib mdns COMPONENT_LIB)
453+
list(APPEND chip_libraries $<TARGET_FILE:${mdns_lib}>)
452454
endif()
453-
list(APPEND chip_libraries $<TARGET_FILE:${mdns_lib}>)
454455
endif()
455456

456457
if (CONFIG_ENABLE_ENCRYPTED_OTA)

examples/all-clusters-app/esp32/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ guides to get started.
1414
- [Cluster control](#cluster-control)
1515
- [Matter OTA guide](../../../docs/guides/esp32/ota.md)
1616
- [RPC console and Device Tracing](../../../docs/guides/esp32/rpc_console.md)
17+
- [Multiple Network Interfaces](#multiple-network-interfaces)
1718

1819
---
1920

@@ -37,6 +38,24 @@ Usage:
3738
$ ./out/debug/chip-tool levelcontrol move-to-level Level=10 TransitionTime=0 OptionMask=0 OptionOverride=0 <NODE ID> <ENDPOINT>
3839
```
3940

41+
### Multiple Network Interfaces
42+
43+
The data model of this example includes a secondary NetworkCommissioning
44+
Endpoint with another NetworkCommissioning cluster. The Endpoint Id for the
45+
secondary NetworkCommissioning Endpoint is 65534. The secondary
46+
NetworkCommissioning Endpoint can be used to manage the driver of extra network
47+
interface.
48+
49+
For ESP32-C6 DevKits, if `CHIP_DEVICE_CONFIG_ENABLE_WIFI` and
50+
`CHIP_DEVICE_CONFIG_ENABLE_THREAD` are both enabled, the NetworkCommissioning
51+
cluster in Endpoint 0 will be used for Thread network driver and the same
52+
cluster on Endpoint 65534 will be used for Wi-Fi network driver.
53+
54+
For ESP32-Ethernet-Kits, if `CHIP_DEVICE_CONFIG_ENABLE_WIFI` and
55+
`CHIP_DEVICE_CONFIG_ENABLE_ETHERNET` are both enabled, the NetworkCommissioning
56+
cluster in Endpoint 0 will be used for Ethernet network driver and the same
57+
cluster on Endpoint 65534 will be used for Wi-Fi network driver.
58+
4059
---
4160

4261
This demo app illustrates controlling OnOff cluster (Server) attributes of an

examples/all-clusters-app/esp32/main/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static void InitServer(intptr_t context)
114114
{
115115
Esp32AppServer::Init(&sCallbacks); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config
116116

117-
#if !(CHIP_DEVICE_CONFIG_ENABLE_WIFI && CHIP_DEVICE_CONFIG_ENABLE_THREAD)
117+
#if !(CHIP_DEVICE_CONFIG_ENABLE_WIFI && (CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET))
118118
// We only have network commissioning on endpoint 0.
119119
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);
120120
#endif

examples/platform/esp32/common/Esp32AppServer.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,20 @@ static constexpr char TAG[] = "ESP32Appserver";
5151

5252
namespace {
5353
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
54-
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
54+
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
5555
constexpr chip::EndpointId kNetworkCommissioningEndpointWiFi = 0xFFFE;
5656
#else
5757
constexpr chip::EndpointId kNetworkCommissioningEndpointWiFi = 0;
5858
#endif
5959
app::Clusters::NetworkCommissioning::Instance
6060
sWiFiNetworkCommissioningInstance(kNetworkCommissioningEndpointWiFi, &(NetworkCommissioning::ESPWiFiDriver::GetInstance()));
61-
#elif CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
61+
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
62+
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
6263
static app::Clusters::NetworkCommissioning::Instance
6364
sEthernetNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPEthernetDriver::GetInstance()));
6465
#endif
6566

66-
#if CONFIG_TEST_EVENT_TRIGGER_ENABLED
67+
#if CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR
6768
static uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
6869
0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
6970
0xcc, 0xdd, 0xee, 0xff };
@@ -73,7 +74,7 @@ static ICDSubscriptionCallback sICDSubscriptionHandler;
7374
#endif
7475
} // namespace
7576

76-
#if CONFIG_TEST_EVENT_TRIGGER_ENABLED
77+
#if CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR
7778
static int hex_digit_to_int(char hex)
7879
{
7980
if ('A' <= hex && hex <= 'F')
@@ -112,7 +113,7 @@ static size_t hex_string_to_binary(const char * hex_string, uint8_t * buf, size_
112113

113114
return buf_size;
114115
}
115-
#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED
116+
#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR
116117

117118
void Esp32AppServer::DeInitBLEIfCommissioned(void)
118119
{
@@ -140,7 +141,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
140141
VerifyOrDie(sTestEventTriggerDelegate.Init(ByteSpan(sTestEventTriggerEnableKey)) == CHIP_NO_ERROR);
141142
VerifyOrDie(sTestEventTriggerDelegate.AddHandler(&sOtaTestEventTriggerHandler) == CHIP_NO_ERROR);
142143
initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate;
143-
#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED
144+
#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR
144145
(void) initParams.InitializeStaticResourcesBeforeServerInit();
145146
if (sAppDelegate != nullptr)
146147
{
@@ -155,6 +156,9 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
155156
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
156157
sWiFiNetworkCommissioningInstance.Init();
157158
#endif
159+
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
160+
sEthernetNetworkCommissioningInstance.Init();
161+
#endif
158162
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
159163
if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() &&
160164
(chip::Server::GetInstance().GetFabricTable().FabricCount() != 0))

src/platform/ESP32/BUILD.gn

+16-13
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,24 @@ static_library("ESP32") {
122122
"NetworkCommissioningDriver.cpp",
123123
"NetworkCommissioningDriver.h",
124124
]
125+
}
126+
127+
if (chip_mdns == "platform") {
128+
sources += [ "DnssdImpl.cpp" ]
129+
}
130+
131+
if (chip_enable_ethernet) {
132+
sources += [
133+
"ConnectivityManagerImpl_Ethernet.cpp",
134+
"NetworkCommissioningDriver_Ethernet.cpp",
135+
]
136+
}
137+
138+
if (chip_enable_ethernet || chip_enable_wifi) {
125139
if (chip_mdns == "platform") {
126140
sources += [
127-
"WiFiDnssdImpl.cpp",
128-
"WiFiDnssdImpl.h",
141+
"ESP32DnssdImpl.cpp",
142+
"ESP32DnssdImpl.h",
129143
]
130144
}
131145
if (chip_mdns == "minimal") {
@@ -141,17 +155,6 @@ static_library("ESP32") {
141155
}
142156
}
143157

144-
if (chip_mdns == "platform") {
145-
sources += [ "DnssdImpl.cpp" ]
146-
}
147-
148-
if (chip_enable_ethernet) {
149-
sources += [
150-
"ConnectivityManagerImpl_Ethernet.cpp",
151-
"NetworkCommissioningDriver_Ethernet.cpp",
152-
]
153-
}
154-
155158
if (chip_enable_openthread) {
156159
sources += [
157160
"../OpenThread/GenericNetworkCommissioningThreadDriver.cpp",

src/platform/ESP32/CHIPDevicePlatformConfig.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@
5353
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT CONFIG_OPENTHREAD_DNS_CLIENT
5454

5555
#if CONFIG_ENABLE_ETHERNET_TELEMETRY
56-
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI 0
5756
#define CHIP_DEVICE_CONFIG_ENABLE_ETHERNET 1
58-
#elif CONFIG_IDF_TARGET_ESP32H2
59-
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI 0
60-
#else
57+
#endif
58+
59+
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP | CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
60+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
61+
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY CONFIG_ENABLE_WIFI_TELEMETRY
6162
#define CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL CONFIG_WIFI_STATION_RECONNECT_INTERVAL
6263
#define CHIP_DEVICE_CONFIG_MAX_SCAN_NETWORKS_RESULTS CONFIG_MAX_SCAN_NETWORKS_RESULTS
6364
#define CHIP_DEVICE_CONFIG_WIFI_SCAN_COMPLETION_TIMEOUT CONFIG_WIFI_SCAN_COMPLETION_TIMEOUT
@@ -69,9 +70,7 @@
6970
#define CHIP_DEVICE_CONFIG_WIFI_AP_BEACON_INTERVAL CONFIG_WIFI_AP_BEACON_INTERVAL
7071
#define CHIP_DEVICE_CONFIG_WIFI_AP_IDLE_TIMEOUT CONFIG_WIFI_AP_IDLE_TIMEOUT
7172
#endif /* CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP */
72-
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY CONFIG_ENABLE_WIFI_TELEMETRY
73-
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP | CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
74-
#endif // CONFIG_IDF_TARGET_ESP32H2
73+
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
7574

7675
#if CONFIG_ENABLE_ICD_SERVER
7776
#define CHIP_DEVICE_CONFIG_ICD_SLOW_POLL_INTERVAL chip::System::Clock::Milliseconds32(CONFIG_ICD_SLOW_POLL_INTERVAL_MS)

src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp

+34-20
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <platform/ESP32/NetworkCommissioningDriver.h>
3131
#include <platform/internal/BLEManager.h>
3232

33-
#include "esp_eth_com.h"
33+
#include "esp_eth.h"
3434
#include "esp_event.h"
3535
#include "esp_netif.h"
3636
#include "esp_wifi.h"
@@ -88,28 +88,42 @@ void ConnectivityManagerImpl::OnEthernetIPv6AddressAvailable(const ip_event_got_
8888

8989
void ConnectivityManagerImpl::OnEthernetPlatformEvent(const ChipDeviceEvent * event)
9090
{
91-
switch (event->Platform.ESPSystemEvent.Id)
91+
if (event->Type == DeviceEventType::kESPSystemEvent)
9292
{
93-
case IP_EVENT_ETH_GOT_IP:
94-
OnEthernetIPv4AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp);
95-
break;
96-
case IP_EVENT_ETH_LOST_IP:
97-
OnEthernetIPv4AddressLost();
98-
break;
99-
case IP_EVENT_GOT_IP6:
100-
if (strcmp(esp_netif_get_ifkey(event->Platform.ESPSystemEvent.Data.IpGotIp6.esp_netif), "ETH_DEF") == 0)
93+
if (event->Platform.ESPSystemEvent.Base == IP_EVENT)
10194
{
102-
OnEthernetIPv6AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp6);
95+
switch (event->Platform.ESPSystemEvent.Id)
96+
{
97+
case IP_EVENT_ETH_GOT_IP:
98+
OnEthernetIPv4AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp);
99+
break;
100+
case IP_EVENT_ETH_LOST_IP:
101+
OnEthernetIPv4AddressLost();
102+
break;
103+
case IP_EVENT_GOT_IP6:
104+
if (strcmp(esp_netif_get_ifkey(event->Platform.ESPSystemEvent.Data.IpGotIp6.esp_netif), "ETH_DEF") == 0)
105+
{
106+
OnEthernetIPv6AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp6);
107+
}
108+
break;
109+
default:
110+
break;
111+
}
112+
}
113+
else if (event->Platform.ESPSystemEvent.Base == ETH_EVENT)
114+
{
115+
switch (event->Platform.ESPSystemEvent.Id)
116+
{
117+
case ETHERNET_EVENT_START:
118+
ChipLogProgress(DeviceLayer, "Ethernet Started");
119+
break;
120+
case ETHERNET_EVENT_STOP:
121+
ChipLogProgress(DeviceLayer, "Ethernet Stopped");
122+
break;
123+
default:
124+
break;
125+
}
103126
}
104-
break;
105-
case ETHERNET_EVENT_START:
106-
ChipLogProgress(DeviceLayer, "Ethernet Started");
107-
break;
108-
case ETHERNET_EVENT_STOP:
109-
ChipLogProgress(DeviceLayer, "Ethernet Stopped");
110-
break;
111-
default:
112-
break;
113127
}
114128
}
115129

src/platform/ESP32/DnssdImpl.cpp

+17-20
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717

1818
#include "lib/dnssd/platform/Dnssd.h"
1919
#include "platform/CHIPDeviceLayer.h"
20+
#include "platform/ESP32/ESP32Utils.h"
2021

2122
#include <lib/support/CHIPMem.h>
2223
#include <lib/support/CodeUtils.h>
2324
#include <lib/support/logging/CHIPLogging.h>
2425
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
2526
#include <platform/OpenThread/OpenThreadDnssdImpl.h>
2627
#endif
27-
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
28-
#include <platform/ESP32/WiFiDnssdImpl.h>
28+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
29+
#include <platform/ESP32/ESP32DnssdImpl.h>
2930
#endif
3031

3132
using namespace ::chip::DeviceLayer;
@@ -35,8 +36,8 @@ namespace Dnssd {
3536

3637
CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context)
3738
{
38-
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
39-
ReturnErrorOnFailure(WiFiDnssdInit(initCallback, errorCallback, context));
39+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
40+
ReturnErrorOnFailure(EspDnssdInit(initCallback, errorCallback, context));
4041
#endif
4142
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
4243
ReturnErrorOnFailure(OpenThreadDnssdInit(initCallback, errorCallback, context));
@@ -48,11 +49,8 @@ void ChipDnssdShutdown() {}
4849

4950
CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context)
5051
{
51-
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
52-
if (ConnectivityMgr().IsWiFiStationProvisioned())
53-
{
54-
ReturnErrorOnFailure(WiFiDnssdPublishService(service, callback, context));
55-
}
52+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
53+
ReturnErrorOnFailure(EspDnssdPublishService(service, callback, context));
5654
#endif
5755
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
5856
if (ConnectivityMgr().IsThreadProvisioned())
@@ -65,11 +63,8 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCal
6563

6664
CHIP_ERROR ChipDnssdRemoveServices()
6765
{
68-
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
69-
if (ConnectivityMgr().IsWiFiStationProvisioned())
70-
{
71-
ReturnErrorOnFailure(WiFiDnssdRemoveServices());
72-
}
66+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
67+
ReturnErrorOnFailure(EspDnssdRemoveServices());
7368
#endif
7469
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
7570
if (ConnectivityMgr().IsThreadProvisioned())
@@ -95,10 +90,11 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi
9590
chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context,
9691
intptr_t * browseIdentifier)
9792
{
98-
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
99-
if (ConnectivityMgr().IsWiFiStationProvisioned())
93+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
94+
if (ConnectivityMgr().IsWiFiStationProvisioned() ||
95+
Internal::ESP32Utils::HasIPv6LinkLocalAddress(Internal::ESP32Utils::kDefaultEthernetNetifKey))
10096
{
101-
ReturnErrorOnFailure(WiFiDnssdBrowse(type, protocol, addressType, interface, callback, context, browseIdentifier));
97+
ReturnErrorOnFailure(EspDnssdBrowse(type, protocol, addressType, interface, callback, context, browseIdentifier));
10298
}
10399
#endif
104100
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
@@ -118,10 +114,11 @@ CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
118114
CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback,
119115
void * context)
120116
{
121-
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
122-
if (ConnectivityMgr().IsWiFiStationProvisioned())
117+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
118+
if (ConnectivityMgr().IsWiFiStationProvisioned() ||
119+
Internal::ESP32Utils::HasIPv6LinkLocalAddress(Internal::ESP32Utils::kDefaultEthernetNetifKey))
123120
{
124-
ReturnErrorOnFailure(WiFiDnssdResolve(service, interface, callback, context));
121+
ReturnErrorOnFailure(EspDnssdResolve(service, interface, callback, context));
125122
}
126123
#endif
127124
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD

0 commit comments

Comments
 (0)