| /* |
| * |
| * Copyright (c) 2020 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 DeviceCallbacks.cpp |
| * |
| * Implements all the callbacks to the application from the CHIP Stack |
| * |
| **/ |
| #include "DeviceCallbacks.h" |
| #include "CHIPDeviceManager.h" |
| #include "LEDWidget.h" |
| #include "WindowCovering.h" |
| #if DLPS_EN |
| #include "matter_gpio.h" |
| #endif |
| #include <app-common/zap-generated/attributes/Accessors.h> |
| #include <app-common/zap-generated/ids/Attributes.h> |
| #include <app-common/zap-generated/ids/Clusters.h> |
| #include <app/CommandHandler.h> |
| #include <app/server/Dnssd.h> |
| #include <lib/dnssd/Advertiser.h> |
| #include <support/CodeUtils.h> |
| #include <support/logging/CHIPLogging.h> |
| #include <support/logging/Constants.h> |
| |
| #if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR |
| #include <ota/OTAInitializer.h> |
| #endif |
| |
| using namespace ::chip; |
| using namespace ::chip::app; |
| using namespace ::chip::app::Clusters::WindowCovering; |
| using namespace ::chip::Inet; |
| using namespace ::chip::System; |
| using namespace ::chip::DeviceLayer; |
| using namespace ::chip::DeviceManager; |
| using namespace ::chip::Logging; |
| |
| constexpr uint32_t kInitOTARequestorDelaySec = 3; |
| |
| bool sIsNetworkProvisioned = false; |
| bool sIsNetworkEnabled = false; |
| bool sHaveBLEConnections = false; |
| |
| namespace LedConsts { |
| constexpr uint32_t kBlinkRate_ms{ 500 }; |
| constexpr uint32_t kIdentifyBlinkRate_ms{ 500 }; |
| |
| namespace StatusLed { |
| namespace Unprovisioned { |
| constexpr uint32_t kOn_ms{ 100 }; |
| constexpr uint32_t kOff_ms{ kOn_ms }; |
| } /* namespace Unprovisioned */ |
| namespace Provisioned { |
| constexpr uint32_t kOn_ms{ 50 }; |
| constexpr uint32_t kOff_ms{ 950 }; |
| } /* namespace Provisioned */ |
| |
| } /* namespace StatusLed */ |
| } /* namespace LedConsts */ |
| |
| #if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR |
| static bool isOTAInitialized = false; |
| |
| void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) |
| { |
| ChipLogProgress(Zcl, "InitOTARequestorHandler"); |
| OTAInitializer::Instance().InitOTARequestor(); |
| } |
| #endif |
| |
| extern LEDWidget identifyLED; |
| |
| void DeviceCallbacks::UpdateStatusLED() |
| { |
| // Update the status LED. |
| // |
| // If IPv6 network and service provisioned, keep the LED Off constantly. |
| // |
| // If the system has ble connection(s) uptill the stage above, THEN blink the LED at an even |
| // rate of 100ms. |
| // |
| // Otherwise, blink the LED for a very short time. |
| if (sIsNetworkProvisioned && sIsNetworkEnabled) |
| { |
| identifyLED.Set(false); |
| } |
| else if (sHaveBLEConnections) |
| { |
| identifyLED.Blink(LedConsts::StatusLed::Unprovisioned::kOn_ms, LedConsts::StatusLed::Unprovisioned::kOff_ms); |
| } |
| else |
| { |
| identifyLED.Blink(LedConsts::StatusLed::Provisioned::kOn_ms, LedConsts::StatusLed::Provisioned::kOff_ms); |
| } |
| } |
| |
| void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg) |
| { |
| switch (event->Type) |
| { |
| case DeviceEventType::kCHIPoBLEAdvertisingChange: |
| sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0; |
| UpdateStatusLED(); |
| break; |
| |
| case DeviceEventType::kInternetConnectivityChange: |
| OnInternetConnectivityChange(event); |
| break; |
| |
| case DeviceEventType::kInterfaceIpAddressChanged: |
| 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(); |
| } |
| break; |
| |
| case DeviceEventType::kThreadConnectivityChange: |
| break; |
| |
| case DeviceEventType::kThreadStateChange: |
| sIsNetworkProvisioned = ConnectivityMgr().IsThreadProvisioned(); |
| sIsNetworkEnabled = ConnectivityMgr().IsThreadEnabled(); |
| UpdateStatusLED(); |
| break; |
| |
| case DeviceEventType::kServerReady: |
| |
| #if DLPS_EN |
| matter_gpio_allow_to_enter_dlps(); |
| #endif |
| |
| #if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR |
| if (!isOTAInitialized) |
| { |
| chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), |
| InitOTARequestorHandler, nullptr); |
| isOTAInitialized = true; |
| } |
| #endif |
| break; |
| |
| case DeviceEventType::kCommissioningComplete: |
| break; |
| } |
| } |
| |
| void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) |
| { |
| if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) |
| { |
| ChipLogProgress(Zcl, "IPv4 Server ready..."); |
| chip::app::DnssdServer::Instance().StartServer(); |
| } |
| else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) |
| { |
| ChipLogProgress(Zcl, "Lost IPv4 connectivity..."); |
| } |
| if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established) |
| { |
| ChipLogProgress(Zcl, "IPv6 Server ready..."); |
| chip::app::DnssdServer::Instance().StartServer(); |
| } |
| else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) |
| { |
| ChipLogProgress(Zcl, "Lost IPv6 connectivity..."); |
| } |
| } |
| |
| /* Forwards all attributes changes */ |
| void MatterWindowCoveringClusterServerAttributeChangedCallback(const app::ConcreteAttributePath & attributePath) |
| { |
| if (attributePath.mEndpointId == WindowCovering::Endpoint()) |
| { |
| switch (attributePath.mAttributeId) |
| { |
| case Attributes::TargetPositionLiftPercent100ths::Id: |
| WindowCovering::Instance().StartMove(WindowCovering::MoveType::LIFT); |
| break; |
| case Attributes::TargetPositionTiltPercent100ths::Id: |
| WindowCovering::Instance().StartMove(WindowCovering::MoveType::TILT); |
| break; |
| case Attributes::CurrentPositionLiftPercent100ths::Id: |
| WindowCovering::Instance().PositionLEDUpdate(WindowCovering::MoveType::LIFT); |
| break; |
| case Attributes::CurrentPositionTiltPercent100ths::Id: |
| WindowCovering::Instance().PositionLEDUpdate(WindowCovering::MoveType::TILT); |
| break; |
| default: |
| WindowCovering::Instance().SchedulePostAttributeChange(attributePath.mEndpointId, attributePath.mAttributeId); |
| break; |
| }; |
| } |
| } |