Skip to content

Commit

Permalink
ESP: Enable Wi-Fi and Ethernet coexisting and add platform mdns for E…
Browse files Browse the repository at this point in the history
…thernet (project-chip#32428)

* ESP32: Add platform mdns support for Ethernet network interface

* Enable Wi-Fi and Ethernet coexisting

* Restyled by clang-format

* readme file changes

* spell check

* Restyled by prettier-markdown

* review changes

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
wqx6 and restyled-commits authored Mar 28, 2024
1 parent 4e582fc commit b30ef3b
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 107 deletions.
5 changes: 3 additions & 2 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -441,16 +441,17 @@ if(CONFIG_OPENTHREAD_ENABLED)
endif()
endif()

if((NOT CONFIG_USE_MINIMAL_MDNS) AND (CONFIG_ENABLE_WIFI_STATION OR CONFIG_ENABLE_WIFI_AP))
if(NOT CONFIG_USE_MINIMAL_MDNS)
idf_build_get_property(build_components BUILD_COMPONENTS)
# For IDF v5.x, the mdns component was moved to idf_managed_components.
# We should use 'espressif__mdns' for 'idf_component_get_property'.
if("espressif__mdns" IN_LIST build_components)
idf_component_get_property(mdns_lib espressif__mdns COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${mdns_lib}>)
elseif("mdns" IN_LIST build_components)
idf_component_get_property(mdns_lib mdns COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${mdns_lib}>)
endif()
list(APPEND chip_libraries $<TARGET_FILE:${mdns_lib}>)
endif()

if (CONFIG_ENABLE_ENCRYPTED_OTA)
Expand Down
19 changes: 19 additions & 0 deletions examples/all-clusters-app/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ guides to get started.
- [Cluster control](#cluster-control)
- [Matter OTA guide](../../../docs/guides/esp32/ota.md)
- [RPC console and Device Tracing](../../../docs/guides/esp32/rpc_console.md)
- [Multiple Network Interfaces](#multiple-network-interfaces)

---

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

### Multiple Network Interfaces

The data model of this example includes a secondary NetworkCommissioning
Endpoint with another NetworkCommissioning cluster. The Endpoint Id for the
secondary NetworkCommissioning Endpoint is 65534. The secondary
NetworkCommissioning Endpoint can be used to manage the driver of extra network
interface.

For ESP32-C6 DevKits, if `CHIP_DEVICE_CONFIG_ENABLE_WIFI` and
`CHIP_DEVICE_CONFIG_ENABLE_THREAD` are both enabled, the NetworkCommissioning
cluster in Endpoint 0 will be used for Thread network driver and the same
cluster on Endpoint 65534 will be used for Wi-Fi network driver.

For ESP32-Ethernet-Kits, if `CHIP_DEVICE_CONFIG_ENABLE_WIFI` and
`CHIP_DEVICE_CONFIG_ENABLE_ETHERNET` are both enabled, the NetworkCommissioning
cluster in Endpoint 0 will be used for Ethernet network driver and the same
cluster on Endpoint 65534 will be used for Wi-Fi network driver.

---

This demo app illustrates controlling OnOff cluster (Server) attributes of an
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static void InitServer(intptr_t context)
{
Esp32AppServer::Init(&sCallbacks); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config

#if !(CHIP_DEVICE_CONFIG_ENABLE_WIFI && CHIP_DEVICE_CONFIG_ENABLE_THREAD)
#if !(CHIP_DEVICE_CONFIG_ENABLE_WIFI && (CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET))
// We only have network commissioning on endpoint 0.
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);
#endif
Expand Down
16 changes: 10 additions & 6 deletions examples/platform/esp32/common/Esp32AppServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ static constexpr char TAG[] = "ESP32Appserver";

namespace {
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
constexpr chip::EndpointId kNetworkCommissioningEndpointWiFi = 0xFFFE;
#else
constexpr chip::EndpointId kNetworkCommissioningEndpointWiFi = 0;
#endif
app::Clusters::NetworkCommissioning::Instance
sWiFiNetworkCommissioningInstance(kNetworkCommissioningEndpointWiFi, &(NetworkCommissioning::ESPWiFiDriver::GetInstance()));
#elif CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
static app::Clusters::NetworkCommissioning::Instance
sEthernetNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPEthernetDriver::GetInstance()));
#endif

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

#if CONFIG_TEST_EVENT_TRIGGER_ENABLED
#if CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR
static int hex_digit_to_int(char hex)
{
if ('A' <= hex && hex <= 'F')
Expand Down Expand Up @@ -112,7 +113,7 @@ static size_t hex_string_to_binary(const char * hex_string, uint8_t * buf, size_

return buf_size;
}
#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED
#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR

void Esp32AppServer::DeInitBLEIfCommissioned(void)
{
Expand Down Expand Up @@ -175,7 +176,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
VerifyOrDie(sTestEventTriggerDelegate.Init(ByteSpan(sTestEventTriggerEnableKey)) == CHIP_NO_ERROR);
VerifyOrDie(sTestEventTriggerDelegate.AddHandler(&sOtaTestEventTriggerHandler) == CHIP_NO_ERROR);
initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate;
#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED
#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR
(void) initParams.InitializeStaticResourcesBeforeServerInit();
if (sAppDelegate != nullptr)
{
Expand All @@ -190,6 +191,9 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
sWiFiNetworkCommissioningInstance.Init();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
sEthernetNetworkCommissioningInstance.Init();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() &&
(chip::Server::GetInstance().GetFabricTable().FabricCount() != 0))
Expand Down
29 changes: 16 additions & 13 deletions src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,24 @@ static_library("ESP32") {
"NetworkCommissioningDriver.cpp",
"NetworkCommissioningDriver.h",
]
}

if (chip_mdns == "platform") {
sources += [ "DnssdImpl.cpp" ]
}

if (chip_enable_ethernet) {
sources += [
"ConnectivityManagerImpl_Ethernet.cpp",
"NetworkCommissioningDriver_Ethernet.cpp",
]
}

if (chip_enable_ethernet || chip_enable_wifi) {
if (chip_mdns == "platform") {
sources += [
"WiFiDnssdImpl.cpp",
"WiFiDnssdImpl.h",
"ESP32DnssdImpl.cpp",
"ESP32DnssdImpl.h",
]
}
if (chip_mdns == "minimal") {
Expand All @@ -141,17 +155,6 @@ static_library("ESP32") {
}
}

if (chip_mdns == "platform") {
sources += [ "DnssdImpl.cpp" ]
}

if (chip_enable_ethernet) {
sources += [
"ConnectivityManagerImpl_Ethernet.cpp",
"NetworkCommissioningDriver_Ethernet.cpp",
]
}

if (chip_enable_openthread) {
sources += [
"../OpenThread/GenericNetworkCommissioningThreadDriver.cpp",
Expand Down
13 changes: 6 additions & 7 deletions src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT CONFIG_OPENTHREAD_DNS_CLIENT

#if CONFIG_ENABLE_ETHERNET_TELEMETRY
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI 0
#define CHIP_DEVICE_CONFIG_ENABLE_ETHERNET 1
#elif CONFIG_IDF_TARGET_ESP32H2
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI 0
#else
#endif

#define CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP | CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY CONFIG_ENABLE_WIFI_TELEMETRY
#define CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL CONFIG_WIFI_STATION_RECONNECT_INTERVAL
#define CHIP_DEVICE_CONFIG_MAX_SCAN_NETWORKS_RESULTS CONFIG_MAX_SCAN_NETWORKS_RESULTS
#define CHIP_DEVICE_CONFIG_WIFI_SCAN_COMPLETION_TIMEOUT CONFIG_WIFI_SCAN_COMPLETION_TIMEOUT
Expand All @@ -69,9 +70,7 @@
#define CHIP_DEVICE_CONFIG_WIFI_AP_BEACON_INTERVAL CONFIG_WIFI_AP_BEACON_INTERVAL
#define CHIP_DEVICE_CONFIG_WIFI_AP_IDLE_TIMEOUT CONFIG_WIFI_AP_IDLE_TIMEOUT
#endif /* CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP */
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY CONFIG_ENABLE_WIFI_TELEMETRY
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP | CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
#endif // CONFIG_IDF_TARGET_ESP32H2
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

#if CONFIG_ENABLE_ICD_SERVER
#define CHIP_DEVICE_CONFIG_ICD_SLOW_POLL_INTERVAL chip::System::Clock::Milliseconds32(CONFIG_ICD_SLOW_POLL_INTERVAL_MS)
Expand Down
54 changes: 34 additions & 20 deletions src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <platform/ESP32/NetworkCommissioningDriver.h>
#include <platform/internal/BLEManager.h>

#include "esp_eth_com.h"
#include "esp_eth.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_wifi.h"
Expand Down Expand Up @@ -88,28 +88,42 @@ void ConnectivityManagerImpl::OnEthernetIPv6AddressAvailable(const ip_event_got_

void ConnectivityManagerImpl::OnEthernetPlatformEvent(const ChipDeviceEvent * event)
{
switch (event->Platform.ESPSystemEvent.Id)
if (event->Type == DeviceEventType::kESPSystemEvent)
{
case IP_EVENT_ETH_GOT_IP:
OnEthernetIPv4AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp);
break;
case IP_EVENT_ETH_LOST_IP:
OnEthernetIPv4AddressLost();
break;
case IP_EVENT_GOT_IP6:
if (strcmp(esp_netif_get_ifkey(event->Platform.ESPSystemEvent.Data.IpGotIp6.esp_netif), "ETH_DEF") == 0)
if (event->Platform.ESPSystemEvent.Base == IP_EVENT)
{
OnEthernetIPv6AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp6);
switch (event->Platform.ESPSystemEvent.Id)
{
case IP_EVENT_ETH_GOT_IP:
OnEthernetIPv4AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp);
break;
case IP_EVENT_ETH_LOST_IP:
OnEthernetIPv4AddressLost();
break;
case IP_EVENT_GOT_IP6:
if (strcmp(esp_netif_get_ifkey(event->Platform.ESPSystemEvent.Data.IpGotIp6.esp_netif), "ETH_DEF") == 0)
{
OnEthernetIPv6AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp6);
}
break;
default:
break;
}
}
else if (event->Platform.ESPSystemEvent.Base == ETH_EVENT)
{
switch (event->Platform.ESPSystemEvent.Id)
{
case ETHERNET_EVENT_START:
ChipLogProgress(DeviceLayer, "Ethernet Started");
break;
case ETHERNET_EVENT_STOP:
ChipLogProgress(DeviceLayer, "Ethernet Stopped");
break;
default:
break;
}
}
break;
case ETHERNET_EVENT_START:
ChipLogProgress(DeviceLayer, "Ethernet Started");
break;
case ETHERNET_EVENT_STOP:
ChipLogProgress(DeviceLayer, "Ethernet Stopped");
break;
default:
break;
}
}

Expand Down
37 changes: 17 additions & 20 deletions src/platform/ESP32/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

#include "lib/dnssd/platform/Dnssd.h"
#include "platform/CHIPDeviceLayer.h"
#include "platform/ESP32/ESP32Utils.h"

#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <platform/OpenThread/OpenThreadDnssdImpl.h>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include <platform/ESP32/WiFiDnssdImpl.h>
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
#include <platform/ESP32/ESP32DnssdImpl.h>
#endif

using namespace ::chip::DeviceLayer;
Expand All @@ -35,8 +36,8 @@ namespace Dnssd {

CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
ReturnErrorOnFailure(WiFiDnssdInit(initCallback, errorCallback, context));
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
ReturnErrorOnFailure(EspDnssdInit(initCallback, errorCallback, context));
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
ReturnErrorOnFailure(OpenThreadDnssdInit(initCallback, errorCallback, context));
Expand All @@ -48,11 +49,8 @@ void ChipDnssdShutdown() {}

CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
if (ConnectivityMgr().IsWiFiStationProvisioned())
{
ReturnErrorOnFailure(WiFiDnssdPublishService(service, callback, context));
}
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
ReturnErrorOnFailure(EspDnssdPublishService(service, callback, context));
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
if (ConnectivityMgr().IsThreadProvisioned())
Expand All @@ -65,11 +63,8 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCal

CHIP_ERROR ChipDnssdRemoveServices()
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
if (ConnectivityMgr().IsWiFiStationProvisioned())
{
ReturnErrorOnFailure(WiFiDnssdRemoveServices());
}
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
ReturnErrorOnFailure(EspDnssdRemoveServices());
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
if (ConnectivityMgr().IsThreadProvisioned())
Expand All @@ -95,10 +90,11 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi
chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context,
intptr_t * browseIdentifier)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
if (ConnectivityMgr().IsWiFiStationProvisioned())
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
if (ConnectivityMgr().IsWiFiStationProvisioned() ||
Internal::ESP32Utils::HasIPv6LinkLocalAddress(Internal::ESP32Utils::kDefaultEthernetNetifKey))
{
ReturnErrorOnFailure(WiFiDnssdBrowse(type, protocol, addressType, interface, callback, context, browseIdentifier));
ReturnErrorOnFailure(EspDnssdBrowse(type, protocol, addressType, interface, callback, context, browseIdentifier));
}
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
Expand All @@ -118,10 +114,11 @@ CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback,
void * context)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
if (ConnectivityMgr().IsWiFiStationProvisioned())
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
if (ConnectivityMgr().IsWiFiStationProvisioned() ||
Internal::ESP32Utils::HasIPv6LinkLocalAddress(Internal::ESP32Utils::kDefaultEthernetNetifKey))
{
ReturnErrorOnFailure(WiFiDnssdResolve(service, interface, callback, context));
ReturnErrorOnFailure(EspDnssdResolve(service, interface, callback, context));
}
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
Expand Down
Loading

0 comments on commit b30ef3b

Please sign in to comment.