/* * * Copyright (c) 2020-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @file CommonDeviceCallbacks.cpp * * Implements all the callbacks to the application from the CHIP Stack * **/ #include "CommonDeviceCallbacks.h" #include "AppTaskBase.h" #include #include #include #include #include #include #include #if CHIP_ENABLE_OPENTHREAD && CHIP_DEVICE_CONFIG_CHIPOBLE_DISABLE_ADVERTISING_WHEN_PROVISIONED #include "openthread-system.h" #endif /* CHIP_ENABLE_OPENTHREAD && CHIP_DEVICE_CONFIG_CHIPOBLE_DISABLE_ADVERTISING_WHEN_PROVISIONED */ #if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR #include "OTARequestorInitiator.h" #endif using namespace chip::app; using namespace ::chip; using namespace ::chip::Inet; using namespace ::chip::System; using namespace ::chip::DeviceLayer; void chip::NXP::App::CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg) { ChipLogDetail(DeviceLayer, "DeviceEventCallback: 0x%04x", event->Type); switch (event->Type) { case DeviceEventType::kWiFiConnectivityChange: OnWiFiConnectivityChange(event); break; case DeviceEventType::kInternetConnectivityChange: OnInternetConnectivityChange(event); break; case DeviceEventType::kInterfaceIpAddressChanged: #if !CHIP_ENABLE_OPENTHREAD // No need to do this for OT mDNS server if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) || (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned)) { // MDNS server restart on any ip assignment: if link local ipv6 is configured, that // will not trigger a 'internet connectivity change' as there is no internet // connectivity. MDNS still wants to refresh its listening interfaces to include the // newly selected address. chip::app::DnssdServer::Instance().StartServer(); } #endif OnInterfaceIpAddressChanged(event); break; #if CHIP_ENABLE_OPENTHREAD case DeviceEventType::kCommissioningComplete: CommonDeviceCallbacks::OnComissioningComplete(event); break; #if CHIP_DEVICE_CONFIG_ENABLE_WPA case DeviceEventType::kThreadConnectivityChange: if (!ConnectivityMgr().IsWiFiStationConnected() && (event->ThreadConnectivityChange.Result == kConnectivity_Established)) { // Restart DnsSd service when operating as Matter over Thread chip::app::DnssdServer::Instance().StartServer(); } break; #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA #endif // CHIP_ENABLE_OPENTHREAD case DeviceLayer::DeviceEventType::kDnssdInitialized: ChipLogProgress(DeviceLayer, "kDnssdInitialized"); #if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR /* Initialize OTA Requestor */ OTARequestorInitiator::Instance().InitOTA(reinterpret_cast(&OTARequestorInitiator::Instance())); #endif break; } } void chip::NXP::App::CommonDeviceCallbacks::OnWiFiConnectivityChange(const ChipDeviceEvent * event) { if (event->WiFiConnectivityChange.Result == kConnectivity_Established) { ChipLogProgress(DeviceLayer, "WiFi connection established"); } else if (event->WiFiConnectivityChange.Result == kConnectivity_Lost) { ChipLogProgress(DeviceLayer, "WiFi connection lost"); } } void chip::NXP::App::CommonDeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { char ip_addr[Inet::IPAddress::kMaxStringLength]; event->InternetConnectivityChange.ipAddress.ToString(ip_addr); ChipLogProgress(DeviceLayer, "Server ready at: %s:%d", ip_addr, CHIP_PORT); #if !CHIP_ENABLE_OPENTHREAD // No need to do this for OT mDNS server chip::app::DnssdServer::Instance().StartServer(); #endif } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) { ChipLogProgress(DeviceLayer, "Lost IPv4 connectivity..."); } if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established) { char ip_addr[Inet::IPAddress::kMaxStringLength]; event->InternetConnectivityChange.ipAddress.ToString(ip_addr); ChipLogProgress(DeviceLayer, "IPv6 Server ready at: [%s]:%d", ip_addr, CHIP_PORT); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) { ChipLogProgress(DeviceLayer, "Lost IPv6 connectivity..."); } } void chip::NXP::App::CommonDeviceCallbacks::OnInterfaceIpAddressChanged(const ChipDeviceEvent * event) { switch (event->InterfaceIpAddressChanged.Type) { case InterfaceIpChangeType::kIpV4_Assigned: ChipLogProgress(DeviceLayer, "Interface IPv4 address assigned"); break; case InterfaceIpChangeType::kIpV4_Lost: ChipLogProgress(DeviceLayer, "Interface IPv4 address lost"); break; case InterfaceIpChangeType::kIpV6_Assigned: ChipLogProgress(DeviceLayer, "Interface IPv6 address assigned"); break; case InterfaceIpChangeType::kIpV6_Lost: ChipLogProgress(DeviceLayer, "Interface IPv6 address lost"); break; } } void chip::NXP::App::CommonDeviceCallbacks::OnSessionEstablished(chip::DeviceLayer::ChipDeviceEvent const *) { /* Empty */ } #if CHIP_ENABLE_OPENTHREAD void chip::NXP::App::CommonDeviceCallbacks::OnComissioningComplete(const chip::DeviceLayer::ChipDeviceEvent * event) { #if CHIP_DEVICE_CONFIG_ENABLE_WPA if (!ConnectivityMgr().IsWiFiStationConnected() && ConnectivityMgr().IsThreadProvisioned()) { // Set WIFI cluster interface attribute to disable. app::Clusters::NetworkCommissioning::Attributes::InterfaceEnabled::Set(0, 0); } #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA #if CHIP_DEVICE_CONFIG_CHIPOBLE_DISABLE_ADVERTISING_WHEN_PROVISIONED /* * If a transceiver supporting a multiprotocol scenario is used, a check of the provisioning state is required, * so that we can inform the transceiver to stop BLE to give the priority to another protocol. * For example it is the case when a K32W0 transceiver supporting OT+BLE+Zigbee is used. When the device is already provisioned, * BLE is no more required and the transceiver needs to be informed so that Zigbee can be switched on and BLE switched off. * * If a transceiver does not support such vendor property the cmd would be ignored. */ if (ConfigurationMgr().IsFullyProvisioned()) { ChipLogDetail(DeviceLayer, "Provisioning complete, stopping BLE\n"); ThreadStackMgrImpl().LockThreadStack(); PlatformMgrImpl().StopBLEConnectivity(); ThreadStackMgrImpl().UnlockThreadStack(); } #endif } #endif