From 14521445b97a31963175fdfcbdac1ca454f345a0 Mon Sep 17 00:00:00 2001 From: Artur Tynecki <77382963+ATmobica@users.noreply.github.com> Date: Sat, 6 Nov 2021 00:53:33 +0100 Subject: [PATCH] [Mbed] ConnectivityManager improvements (#11467) * Implement set WiFi AP mode for mbed * Improve IP address setting --- src/platform/mbed/ConnectivityManagerImpl.cpp | 70 +++++++++++-------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/src/platform/mbed/ConnectivityManagerImpl.cpp b/src/platform/mbed/ConnectivityManagerImpl.cpp index cded2de30b12f3..1a3ac24fa599d4 100644 --- a/src/platform/mbed/ConnectivityManagerImpl.cpp +++ b/src/platform/mbed/ConnectivityManagerImpl.cpp @@ -92,7 +92,19 @@ bool ConnectivityManagerImpl::_IsWiFiStationProvisioned(void) CHIP_ERROR ConnectivityManagerImpl::_SetWiFiAPMode(WiFiAPMode val) { - return CHIP_ERROR_NOT_IMPLEMENTED; + CHIP_ERROR err = CHIP_NO_ERROR; + + VerifyOrExit(val != kWiFiAPMode_NotSupported, err = CHIP_ERROR_INVALID_ARGUMENT); + + if (mWiFiAPMode != val) + { + ChipLogDetail(DeviceLayer, "WiFi AP mode change: %s -> %s", WiFiAPModeToStr(mWiFiAPMode), WiFiAPModeToStr(val)); + } + + mWiFiAPMode = val; + +exit: + return err; } // ==================== ConnectivityManager Platform Internal Methods ==================== @@ -252,10 +264,10 @@ CHIP_ERROR ConnectivityManagerImpl::OnStationConnected() ChipLogProgress(DeviceLayer, "Event - StationConnected"); } - // Update IPv4 address + // Update IP address SocketAddress address; auto error = mWifiInterface->get_ip_address(&address); - if (error) + if (error != NSAPI_ERROR_OK) { if (mIp4Address != IPAddress::Any) { @@ -268,26 +280,7 @@ CHIP_ERROR ConnectivityManagerImpl::OnStationConnected() ReturnErrorOnFailure(PlatformMgr().PostEvent(&event)); ChipLogError(DeviceLayer, "Unnexpected loss of Ip4 address"); } - } - else - { - IPAddress addr; - if (IPAddress::FromString(address.get_ip_address(), addr) && addr != mIp4Address) - { - mIp4Address = addr; - ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_Established; - event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; - ReturnErrorOnFailure(PlatformMgr().PostEvent(&event)); - ChipLogProgress(DeviceLayer, "New Ip4 address set: %s", address.get_ip_address()); - } - } - // Update IPv6 address - error = mWifiInterface->get_ipv6_link_local_address(&address); - if (error) - { if (mIp6Address != IPAddress::Any) { // Unnexpected change, forward to the application @@ -303,17 +296,34 @@ CHIP_ERROR ConnectivityManagerImpl::OnStationConnected() else { IPAddress addr; - if (IPAddress::FromString(address.get_ip_address(), addr) && addr != mIp6Address) + if (address.get_ip_version() == NSAPI_IPv4) { - mIp6Address = addr; - ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; - event.InternetConnectivityChange.IPv6 = kConnectivity_Established; - ReturnErrorOnFailure(PlatformMgr().PostEvent(&event)); - ChipLogProgress(DeviceLayer, "New Ip6 address set %s", address.get_ip_address()); + if (IPAddress::FromString(address.get_ip_address(), addr) && addr != mIp4Address) + { + mIp4Address = addr; + ChipDeviceEvent event; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_Established; + event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; + ReturnErrorOnFailure(PlatformMgr().PostEvent(&event)); + ChipLogProgress(DeviceLayer, "New Ip4 address set: %s", address.get_ip_address()); + } + } + else + { + if (IPAddress::FromString(address.get_ip_address(), addr) && addr != mIp6Address) + { + mIp6Address = addr; + ChipDeviceEvent event; + event.Type = DeviceEventType::kInternetConnectivityChange; + event.InternetConnectivityChange.IPv4 = kConnectivity_NoChange; + event.InternetConnectivityChange.IPv6 = kConnectivity_Established; + ReturnErrorOnFailure(PlatformMgr().PostEvent(&event)); + ChipLogProgress(DeviceLayer, "New Ip6 address set %s", address.get_ip_address()); + } } } + return CHIP_NO_ERROR; }