From 769264aef02674c04dfadb76664af6173d795714 Mon Sep 17 00:00:00 2001 From: Tennessee Carmel-Veilleux Date: Mon, 2 May 2022 16:38:58 -0400 Subject: [PATCH] Reduce RAM usage from CHIPDeviceEvent (#17959) * Reduce RAM usage from CHIPDeviceEvent - CHIPDeviceEvent has had a historical `InternetConnectivityChange` event that embeds a 46 byte string (instead of a 16 byte IPAddress) and is *only used for logging* in examples.= - This event is in a union and is the largest of all the sub-structs, therefore forcing max-sized events to that event. Issue #9261 This PR: - Changes the `address` from a string to an IPAddress - Renames to `ipAddress` to break external clients using this struct so that they notice on the next roll to master (usage change is trivial). - Updates all examples to no longer log the address, since it was only logged for IPv4 and not IPv6, and IPv6 is the standard, so if it was good enough for IPv6 not to log, it's good enough for IPv4. - Make IPAddress trivially copyable by removing a non-trivial `operator=` that was actually implementing the trivial copy. Upstream OpenWeave code that was the basis for the IPAddress.h file removed it already in late 2021, > 1.5 years after import (see https://github.com/openweave/openweave-core/commit/fbbb01c501a8516df871842403a4ea926f5e4996#diff-33121ed998c085b8dc0026f30f24aaadb8b8b36042e38b449cec15c32c8ba86e) - Update all platforms that populated the address to use the actual address, not the string Testing done: - All cert tests pass - All unit tests pass * Restyled by clang-format * Fix CI on newer compilers * Restyled by clang-format Co-authored-by: Restyled.io --- .../ameba/main/DeviceCallbacks.cpp | 2 +- .../esp32/main/DeviceCallbacks.cpp | 2 +- .../bridge-app/esp32/main/DeviceCallbacks.cpp | 2 +- examples/chef/esp32/main/main.cpp | 2 +- .../ameba/main/DeviceCallbacks.cpp | 2 +- .../bouffalolab/bl602/src/DeviceCallbacks.cpp | 2 +- .../esp32/main/DeviceCallbacks.cpp | 2 +- .../lock-app/esp32/main/DeviceCallbacks.cpp | 2 +- .../esp32/main/DeviceCallbacks.cpp | 2 +- .../ameba/main/DeviceCallbacks.cpp | 2 +- .../esp32/main/DeviceCallbacks.cpp | 2 +- .../esp32/main/DeviceCallbacks.cpp | 2 +- src/include/platform/CHIPDeviceEvent.h | 10 +++- src/inet/IPAddress.cpp | 13 ----- src/inet/IPAddress.h | 15 ++--- src/inet/IPPrefix.cpp | 11 ---- src/inet/IPPrefix.h | 15 ----- .../Ameba/ConnectivityManagerImpl.cpp | 9 +-- .../EFR32/ConnectivityManagerImpl_WIFI.cpp | 9 +-- .../ESP32/ConnectivityManagerImpl_WiFi.cpp | 9 +-- .../Linux/ConnectivityManagerImpl.cpp | 14 +++-- src/platform/Linux/PlatformManagerImpl.cpp | 10 ++-- src/platform/P6/ConnectivityManagerImpl.cpp | 8 +-- .../mbed/NetworkCommissioningWiFiDriver.cpp | 56 +++++++++++-------- 24 files changed, 89 insertions(+), 114 deletions(-) diff --git a/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp b/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp index 31b18003d65e05..8f18f730999cde 100644 --- a/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp @@ -99,7 +99,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ChipLogProgress(DeviceLayer, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ChipLogProgress(DeviceLayer, "IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) diff --git a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp index a3046af0f59c0b..0b05da8e240985 100644 --- a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp @@ -212,7 +212,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event static bool isOTAInitialized = false; if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ESP_LOGI(TAG, "IPv4 Server ready..."); wifiLED.Set(true); chip::app::DnssdServer::Instance().StartServer(); diff --git a/examples/bridge-app/esp32/main/DeviceCallbacks.cpp b/examples/bridge-app/esp32/main/DeviceCallbacks.cpp index 0c6b50607b739d..58a59b2603d6de 100644 --- a/examples/bridge-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/bridge-app/esp32/main/DeviceCallbacks.cpp @@ -73,7 +73,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ESP_LOGI(TAG, "IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) diff --git a/examples/chef/esp32/main/main.cpp b/examples/chef/esp32/main/main.cpp index 8dadae04d554f6..03751d641dafd1 100644 --- a/examples/chef/esp32/main/main.cpp +++ b/examples/chef/esp32/main/main.cpp @@ -78,7 +78,7 @@ void DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg) case DeviceEventType::kInternetConnectivityChange: if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ChipLogProgress(Shell, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ChipLogProgress(Shell, "IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) diff --git a/examples/lighting-app/ameba/main/DeviceCallbacks.cpp b/examples/lighting-app/ameba/main/DeviceCallbacks.cpp index bb8fe22d4e609d..cb023f73b6ea87 100644 --- a/examples/lighting-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/lighting-app/ameba/main/DeviceCallbacks.cpp @@ -78,7 +78,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - printf("Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + printf("IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) diff --git a/examples/lighting-app/bouffalolab/bl602/src/DeviceCallbacks.cpp b/examples/lighting-app/bouffalolab/bl602/src/DeviceCallbacks.cpp index 69dc7bb2744e02..852522e7832e07 100644 --- a/examples/lighting-app/bouffalolab/bl602/src/DeviceCallbacks.cpp +++ b/examples/lighting-app/bouffalolab/bl602/src/DeviceCallbacks.cpp @@ -129,7 +129,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - log_info("Server ready at: %s:%d\r\n", event->InternetConnectivityChange.address, CHIP_PORT); + log_info("IPv4 Server ready...\r\n"); // TODO // wifiLED.Set(true); chip::app::DnssdServer::Instance().StartServer(); diff --git a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp index 9c1d21f03c7af6..eaffb0b8efee36 100644 --- a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp @@ -161,7 +161,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event static bool isOTAInitialized = false; if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ESP_LOGI(TAG, "IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); if (!isOTAInitialized) diff --git a/examples/lock-app/esp32/main/DeviceCallbacks.cpp b/examples/lock-app/esp32/main/DeviceCallbacks.cpp index 1d176bdb86f54c..10a2c37d223889 100644 --- a/examples/lock-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/lock-app/esp32/main/DeviceCallbacks.cpp @@ -100,7 +100,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ESP_LOGI(TAG, "IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) diff --git a/examples/ota-provider-app/esp32/main/DeviceCallbacks.cpp b/examples/ota-provider-app/esp32/main/DeviceCallbacks.cpp index 83174c06e1c17a..b4ed0159ebf89c 100644 --- a/examples/ota-provider-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/ota-provider-app/esp32/main/DeviceCallbacks.cpp @@ -82,7 +82,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ESP_LOGI(TAG, "IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) diff --git a/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp b/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp index 31b18003d65e05..8f18f730999cde 100644 --- a/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp @@ -99,7 +99,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ChipLogProgress(DeviceLayer, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ChipLogProgress(DeviceLayer, "IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) diff --git a/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp b/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp index 4daa73dd48a00b..c58596631b8f70 100644 --- a/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp @@ -90,7 +90,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event static bool isOTAInitialized = false; if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ESP_LOGI(TAG, "IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); if (!isOTAInitialized) { diff --git a/examples/temperature-measurement-app/esp32/main/DeviceCallbacks.cpp b/examples/temperature-measurement-app/esp32/main/DeviceCallbacks.cpp index 80c2320c26781a..ea388f20a06b89 100644 --- a/examples/temperature-measurement-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/temperature-measurement-app/esp32/main/DeviceCallbacks.cpp @@ -88,7 +88,7 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { - ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); + ESP_LOGI(TAG, "IPv4 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) diff --git a/src/include/platform/CHIPDeviceEvent.h b/src/include/platform/CHIPDeviceEvent.h index 2ff9526b58ce28..796e4ab7a3dfc8 100644 --- a/src/include/platform/CHIPDeviceEvent.h +++ b/src/include/platform/CHIPDeviceEvent.h @@ -25,6 +25,7 @@ #pragma once #include +#include #include namespace chip { @@ -373,7 +374,14 @@ struct ChipDeviceEvent final { ConnectivityChange IPv4; ConnectivityChange IPv6; - char address[INET6_ADDRSTRLEN]; + // WARNING: There used to be `char address[INET6_ADDRSTRLEN]` here and it is + // deprecated/removed since it was too large and only used for logging. + // Consider not relying on ipAddress field either since the platform + // layer *does not actually validate* that the actual internet is reachable + // before issuing this event *and* there may be multiple addresses + // (especially IPv6) so it's recommended to use `ChipDevicePlatformEvent` + // instead and do something that is better for your platform. + chip::Inet::IPAddress ipAddress; } InternetConnectivityChange; struct { diff --git a/src/inet/IPAddress.cpp b/src/inet/IPAddress.cpp index 6c7415412b10b5..f3c8cd9175eeba 100644 --- a/src/inet/IPAddress.cpp +++ b/src/inet/IPAddress.cpp @@ -57,19 +57,6 @@ bool IPAddress::operator!=(const IPAddress & other) const return Addr[0] != other.Addr[0] || Addr[1] != other.Addr[1] || Addr[2] != other.Addr[2] || Addr[3] != other.Addr[3]; } -IPAddress & IPAddress::operator=(const IPAddress & other) -{ - if (this != &other) - { - Addr[0] = other.Addr[0]; - Addr[1] = other.Addr[1]; - Addr[2] = other.Addr[2]; - Addr[3] = other.Addr[3]; - } - - return *this; -} - #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT IPAddress::IPAddress(const ip6_addr_t & ipv6Addr) diff --git a/src/inet/IPAddress.h b/src/inet/IPAddress.h index e71de199a5d105..b7a11f17b22467 100644 --- a/src/inet/IPAddress.h +++ b/src/inet/IPAddress.h @@ -120,6 +120,7 @@ union SockAddr * @details * The CHIP Inet Layer uses objects of this class to represent Internet * protocol addresses (independent of protocol version). + * */ class DLL_EXPORT IPAddress { @@ -141,8 +142,7 @@ class DLL_EXPORT IPAddress static constexpr uint16_t kMaxStringLength = OT_IP6_ADDRESS_STRING_SIZE; #endif - IPAddress() = default; - IPAddress(const IPAddress & other) = default; + IPAddress() = default; #if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT explicit IPAddress(const ip6_addr_t & ipv6Addr); @@ -324,15 +324,6 @@ class DLL_EXPORT IPAddress */ bool operator!=(const IPAddress & other) const; - /** - * @brief Conventional assignment operator. - * - * @param[in] other The address to copy. - * - * @return A reference to this object. - */ - IPAddress & operator=(const IPAddress & other); - /** * @brief Emit the IP address in conventional text presentation format. * @@ -667,5 +658,7 @@ class DLL_EXPORT IPAddress static IPAddress Any; }; +static_assert(std::is_trivial::value, "IPAddress is not trivial"); + } // namespace Inet } // namespace chip diff --git a/src/inet/IPPrefix.cpp b/src/inet/IPPrefix.cpp index a9d443701d9ac8..fb921b916fdf56 100644 --- a/src/inet/IPPrefix.cpp +++ b/src/inet/IPPrefix.cpp @@ -49,17 +49,6 @@ bool IPPrefix::operator!=(const IPPrefix & other) const return IPAddr != other.IPAddr || Length != other.Length; } -IPPrefix & IPPrefix::operator=(const IPPrefix & other) -{ - if (this != &other) - { - IPAddr = other.IPAddr; - Length = other.Length; - } - - return *this; -} - bool IPPrefix::MatchAddress(const IPAddress & addr) const { uint8_t l = (Length <= 128) ? Length : 128; diff --git a/src/inet/IPPrefix.h b/src/inet/IPPrefix.h index ce58c1b3a700a3..74528ef4ed97a9 100644 --- a/src/inet/IPPrefix.h +++ b/src/inet/IPPrefix.h @@ -48,12 +48,6 @@ class IPPrefix IPPrefix() = default; IPPrefix(const IPAddress & ipAddress, uint8_t length) : IPAddr(ipAddress), Length(length) {} - /** - * Copy constructor for the IPPrefix class. - * - */ - IPPrefix(const IPPrefix & other) = default; - /** An IPv6 or IPv4 address. */ IPAddress IPAddr; @@ -107,15 +101,6 @@ class IPPrefix */ bool operator!=(const IPPrefix & other) const; - /** - * @brief Conventional assignment operator. - * - * @param[in] other the prefix to copy. - * - * @return a reference to this object. - */ - IPPrefix & operator=(const IPPrefix & other); - /** * @brief Test if an address matches the prefix. * diff --git a/src/platform/Ameba/ConnectivityManagerImpl.cpp b/src/platform/Ameba/ConnectivityManagerImpl.cpp index 1427fa939f96a5..ee294acb78d665 100644 --- a/src/platform/Ameba/ConnectivityManagerImpl.cpp +++ b/src/platform/Ameba/ConnectivityManagerImpl.cpp @@ -699,10 +699,11 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void) // Alert other components of the state change. ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn); - event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn); - addr.ToString(event.InternetConnectivityChange.address); + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn); + event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn); + event.InternetConnectivityChange.ipAddress = addr; + PlatformMgr().PostEventOrDie(&event); if (haveIPv4Conn != hadIPv4Conn) diff --git a/src/platform/EFR32/ConnectivityManagerImpl_WIFI.cpp b/src/platform/EFR32/ConnectivityManagerImpl_WIFI.cpp index 7c0a9bfd8d39e8..a279c808c4ccb5 100644 --- a/src/platform/EFR32/ConnectivityManagerImpl_WIFI.cpp +++ b/src/platform/EFR32/ConnectivityManagerImpl_WIFI.cpp @@ -450,10 +450,11 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void) // Alert other components of the state change. ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn); - event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn); - addr.ToString(event.InternetConnectivityChange.address, sizeof(event.InternetConnectivityChange.address)); + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn); + event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn); + event.InternetConnectivityChange.ipAddress = addr; + (void) PlatformMgr().PostEvent(&event); if (haveIPv4Conn != hadIPv4Conn) diff --git a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp index 8ebc260031e872..8af78d9bcc6bf0 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp @@ -1011,10 +1011,11 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void) // Alert other components of the state change. ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn); - event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn); - addr.ToString(event.InternetConnectivityChange.address); + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn); + event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn); + event.InternetConnectivityChange.ipAddress = addr; + PlatformMgr().PostEventOrDie(&event); if (haveIPv4Conn != hadIPv4Conn) diff --git a/src/platform/Linux/ConnectivityManagerImpl.cpp b/src/platform/Linux/ConnectivityManagerImpl.cpp index 2a2c5fcff050ba..e2044e11c8712c 100644 --- a/src/platform/Linux/ConnectivityManagerImpl.cpp +++ b/src/platform/Linux/ConnectivityManagerImpl.cpp @@ -1181,13 +1181,15 @@ void ConnectivityManagerImpl::PostNetworkConnect() if ((it.GetAddress(addr) == CHIP_NO_ERROR) && addr.IsIPv4()) { ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_Established; - event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; - addr.ToString(event.InternetConnectivityChange.address); + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_Established; + event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; + event.InternetConnectivityChange.ipAddress = addr; - ChipLogDetail(DeviceLayer, "Got IP address on interface: %s IP: %s", ifName, - event.InternetConnectivityChange.address); + char ipStrBuf[chip::Inet::IPAddress::kMaxStringLength] = { 0 }; + addr.ToString(ipStrBuf); + + ChipLogDetail(DeviceLayer, "Got IP address on interface: %s IP: %s", ifName, ipStrBuf); PlatformMgr().PostEventOrDie(&event); } diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index 21817dab467e62..dc16c1c3da8754 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -149,15 +149,15 @@ void PlatformManagerImpl::WiFIIPChangeListener() continue; } + char ipStrBuf[chip::Inet::IPAddress::kMaxStringLength] = { 0 }; + inet_ntop(AF_INET, RTA_DATA(routeInfo), ipStrBuf, sizeof(ipStrBuf)); + ChipLogDetail(DeviceLayer, "Got IP address on interface: %s IP: %s", name, ipStrBuf); + ChipDeviceEvent event; event.Type = DeviceEventType::kInternetConnectivityChange; event.InternetConnectivityChange.IPv4 = kConnectivity_Established; event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; - inet_ntop(AF_INET, RTA_DATA(routeInfo), event.InternetConnectivityChange.address, - sizeof(event.InternetConnectivityChange.address)); - - ChipLogDetail(DeviceLayer, "Got IP address on interface: %s IP: %s", name, - event.InternetConnectivityChange.address); + VerifyOrDie(chip::Inet::IPAddress::FromString(ipStrBuf, event.InternetConnectivityChange.ipAddress)); CHIP_ERROR status = PlatformMgr().PostEvent(&event); if (status != CHIP_NO_ERROR) diff --git a/src/platform/P6/ConnectivityManagerImpl.cpp b/src/platform/P6/ConnectivityManagerImpl.cpp index 7b6590a359825a..22034d9f561b66 100644 --- a/src/platform/P6/ConnectivityManagerImpl.cpp +++ b/src/platform/P6/ConnectivityManagerImpl.cpp @@ -657,10 +657,10 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void) // Alert other components of the state change. ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn); - event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn); - addr.ToString(event.InternetConnectivityChange.address); + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn); + event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn); + event.InternetConnectivityChange.ipAddress = addr; PlatformMgr().PostEventOrDie(&event); if (haveIPv4Conn != hadIPv4Conn) diff --git a/src/platform/mbed/NetworkCommissioningWiFiDriver.cpp b/src/platform/mbed/NetworkCommissioningWiFiDriver.cpp index b96a3953180746..6eca69d3eb4866 100644 --- a/src/platform/mbed/NetworkCommissioningWiFiDriver.cpp +++ b/src/platform/mbed/NetworkCommissioningWiFiDriver.cpp @@ -439,9 +439,10 @@ void WiFiDriverImpl::OnNetworkConnected() // Unexpected change, forward to the application mIp4Address = IPAddress::Any; ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_Lost; - event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_Lost; + event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; + event.InternetConnectivityChange.ipAddress = mIp4Address; ConnectivityMgrImpl().PostEvent(&event, true); ChipLogError(DeviceLayer, "Unexpected loss of Ip4 address"); } @@ -451,9 +452,10 @@ void WiFiDriverImpl::OnNetworkConnected() // Unexpected change, forward to the application mIp6Address = IPAddress::Any; ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; - event.InternetConnectivityChange.IPv6 = kConnectivity_Lost; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; + event.InternetConnectivityChange.IPv6 = kConnectivity_Lost; + event.InternetConnectivityChange.ipAddress = mIp6Address; ConnectivityMgrImpl().PostEvent(&event, true); ChipLogError(DeviceLayer, "Unexpected loss of Ip6 address"); } @@ -467,9 +469,10 @@ void WiFiDriverImpl::OnNetworkConnected() { mIp4Address = addr; ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_Established; - event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_Established; + event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; + event.InternetConnectivityChange.ipAddress = mIp4Address; ConnectivityMgrImpl().PostEvent(&event, true); ChipLogProgress(DeviceLayer, "New Ip4 address set: %s", address.get_ip_address()); } @@ -482,9 +485,10 @@ void WiFiDriverImpl::OnNetworkConnected() // Unexpected change, forward to the application mIp6Address = IPAddress::Any; ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; - event.InternetConnectivityChange.IPv6 = kConnectivity_Lost; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; + event.InternetConnectivityChange.IPv6 = kConnectivity_Lost; + event.InternetConnectivityChange.ipAddress = mIp6Address; ConnectivityMgrImpl().PostEvent(&event, true); ChipLogError(DeviceLayer, "Unexpected loss of Ip6 address"); } @@ -495,9 +499,10 @@ void WiFiDriverImpl::OnNetworkConnected() { mIp6Address = addr; ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; - event.InternetConnectivityChange.IPv6 = kConnectivity_Established; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; + event.InternetConnectivityChange.IPv6 = kConnectivity_Established; + event.InternetConnectivityChange.ipAddress = mIp6Address; ConnectivityMgrImpl().PostEvent(&event, true); ChipLogProgress(DeviceLayer, "New Ip6 address set %s", address.get_ip_address()); } @@ -509,9 +514,10 @@ void WiFiDriverImpl::OnNetworkConnected() { mIp6Address = addr; ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; - event.InternetConnectivityChange.IPv6 = kConnectivity_Established; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; + event.InternetConnectivityChange.IPv6 = kConnectivity_Established; + event.InternetConnectivityChange.ipAddress = mIp6Address; ConnectivityMgrImpl().PostEvent(&event, true); ChipLogProgress(DeviceLayer, "New Ip6 address set %s", address.get_ip_address()); } @@ -534,9 +540,10 @@ void WiFiDriverImpl::OnNetworkDisconnected() // Unexpected change, forward to the application mIp4Address = IPAddress::Any; ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_Lost; - event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_Lost; + event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; + event.InternetConnectivityChange.ipAddress = mIp4Address; ConnectivityMgrImpl().PostEvent(&event, true); ChipLogError(DeviceLayer, "Loss of Ip4 address"); } @@ -546,9 +553,10 @@ void WiFiDriverImpl::OnNetworkDisconnected() // Unexpected change, forward to the application mIp6Address = IPAddress::Any; ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; - event.InternetConnectivityChange.IPv6 = kConnectivity_Lost; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; + event.InternetConnectivityChange.IPv6 = kConnectivity_Lost; + event.InternetConnectivityChange.ipAddress = mIp6Address; ConnectivityMgrImpl().PostEvent(&event, true); ChipLogError(DeviceLayer, "Loss of Ip6 address"); }