diff --git a/src/app/clusters/switch-server/switch-server.cpp b/src/app/clusters/switch-server/switch-server.cpp index 2cab1043dd50d9..6829f20de449a2 100644 --- a/src/app/clusters/switch-server/switch-server.cpp +++ b/src/app/clusters/switch-server/switch-server.cpp @@ -15,6 +15,7 @@ * limitations under the License. */ +#include "switch-server.h" #include #include #include @@ -35,166 +36,123 @@ using namespace chip::app::Clusters::Switch; using namespace chip::app::Clusters::Switch::Attributes; using chip::DeviceLayer::DeviceControlServer; -namespace { +namespace chip { +namespace app { +namespace Clusters { +namespace Switch { -class SwitchDelegate : public DeviceLayer::SwitchDeviceControlDelegate +Server Server::instance; + +/********************************************************** + * Server Implementation + *********************************************************/ + +Server & Server::Instance() { - /** - * @brief - * Called when the latching switch is moved to a new position. - */ - void OnSwitchLatched(uint8_t newPosition) override - { - ChipLogProgress(Zcl, "SwitchDelegate: OnSwitchLatched"); - - for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id)) - { - // Record SwitchLatched event - EventNumber eventNumber; - Events::SwitchLatched::Type event{ newPosition }; - - if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber)) - { - ChipLogError(Zcl, "SwitchDelegate: Failed to record SwitchLatched event"); - } - } - } + return instance; +} - /** - * @brief - * Called when the momentary switch starts to be pressed. - */ - void OnInitialPressed(uint8_t newPosition) override +void Server::OnSwitchLatch(EndpointId endpoint, uint8_t newPosition) +{ + ChipLogProgress(Zcl, "Server: OnSwitchLatch"); + + // Record SwitchLatched event + EventNumber eventNumber; + Events::SwitchLatched::Type event{ newPosition }; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) { - ChipLogProgress(Zcl, "SwitchDelegate: OnInitialPressed"); - - for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id)) - { - // Record InitialPress event - EventNumber eventNumber; - Events::InitialPress::Type event{ newPosition }; - - if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber)) - { - ChipLogError(Zcl, "SwitchDelegate: Failed to record InitialPress event"); - } - } + ChipLogError(Zcl, "Server: Failed to record SwitchLatched event"); } +} - /** - * @brief - * Called when the momentary switch has been pressed for a "long" time. - */ - void OnLongPressed(uint8_t newPosition) override +void Server::OnInitialPress(EndpointId endpoint, uint8_t newPosition) +{ + ChipLogProgress(Zcl, "Server: OnInitialPress"); + + // Record InitialPress event + EventNumber eventNumber; + Events::InitialPress::Type event{ newPosition }; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) { - ChipLogProgress(Zcl, "SwitchDelegate: OnLongPressed"); - - for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id)) - { - // Record LongPress event - EventNumber eventNumber; - Events::LongPress::Type event{ newPosition }; - - if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber)) - { - ChipLogError(Zcl, "SwitchDelegate: Failed to record LongPress event"); - } - } + ChipLogError(Zcl, "Server: Failed to record InitialPress event"); } +} - /** - * @brief - * Called when the momentary switch has been released. - */ - void OnShortReleased(uint8_t previousPosition) override +void Server::OnLongPress(EndpointId endpoint, uint8_t newPosition) +{ + ChipLogProgress(Zcl, "Server: OnLongPress"); + + // Record LongPress event + EventNumber eventNumber; + Events::LongPress::Type event{ newPosition }; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) { - ChipLogProgress(Zcl, "SwitchDelegate: OnShortReleased"); - - for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id)) - { - // Record ShortRelease event - EventNumber eventNumber; - Events::ShortRelease::Type event{ previousPosition }; - - if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber)) - { - ChipLogError(Zcl, "SwitchDelegate: Failed to record ShortRelease event"); - } - } + ChipLogError(Zcl, "Server: Failed to record LongPress event"); } +} - /** - * @brief - * Called when the momentary switch has been released (after debouncing) - * and after having been pressed for a long time. - */ - void OnLongReleased(uint8_t previousPosition) override +void Server::OnShortRelease(EndpointId endpoint, uint8_t previousPosition) +{ + ChipLogProgress(Zcl, "Server: OnShortRelease"); + + // Record ShortRelease event + EventNumber eventNumber; + Events::ShortRelease::Type event{ previousPosition }; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) { - ChipLogProgress(Zcl, "SwitchDelegate: OnLongReleased"); - - for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id)) - { - // Record LongRelease event - EventNumber eventNumber; - Events::LongRelease::Type event{ previousPosition }; - - if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber)) - { - ChipLogError(Zcl, "SwitchDelegate: Failed to record LongRelease event"); - } - } + ChipLogError(Zcl, "Server: Failed to record ShortRelease event"); } +} + +void Server::OnLongRelease(EndpointId endpoint, uint8_t previousPosition) +{ + ChipLogProgress(Zcl, "Server: OnLongRelease"); - /** - * @brief - * Called to indicate how many times the momentary switch has been pressed - * in a multi-press sequence, during that sequence. - */ - void OnMultiPressOngoing(uint8_t newPosition, uint8_t count) override + // Record LongRelease event + EventNumber eventNumber; + Events::LongRelease::Type event{ previousPosition }; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) { - ChipLogProgress(Zcl, "SwitchDelegate: OnMultiPressOngoing"); - - for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id)) - { - // Record MultiPressOngoing event - EventNumber eventNumber; - Events::MultiPressOngoing::Type event{ newPosition, count }; - - if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber)) - { - ChipLogError(Zcl, "SwitchDelegate: Failed to record MultiPressOngoing event"); - } - } + ChipLogError(Zcl, "Server: Failed to record LongRelease event"); } +} + +void Server::OnMultiPressOngoing(EndpointId endpoint, uint8_t newPosition, uint8_t count) +{ + ChipLogProgress(Zcl, "Server: OnMultiPressOngoing"); - /** - * @brief - * Called to indicate how many times the momentary switch has been pressed - * in a multi-press sequence, after it has been detected that the sequence has ended. - */ - void OnMultiPressComplete(uint8_t newPosition, uint8_t count) override + // Record MultiPressOngoing event + EventNumber eventNumber; + Events::MultiPressOngoing::Type event{ newPosition, count }; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) { - ChipLogProgress(Zcl, "SwitchDelegate: OnMultiPressComplete"); - - for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id)) - { - // Record MultiPressComplete event - EventNumber eventNumber; - Events::MultiPressComplete::Type event{ newPosition, count }; - - if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber)) - { - ChipLogError(Zcl, "SwitchDelegate: Failed to record MultiPressComplete event"); - } - } + ChipLogError(Zcl, "Server: Failed to record MultiPressOngoing event"); } -}; +} -SwitchDelegate gSwitchDelegate; +void Server::OnMultiPressComplete(EndpointId endpoint, uint8_t newPosition, uint8_t count) +{ + ChipLogProgress(Zcl, "Server: OnMultiPressComplete"); -} // anonymous namespace + // Record MultiPressComplete event + EventNumber eventNumber; + Events::MultiPressComplete::Type event{ newPosition, count }; -void MatterSwitchPluginServerInitCallback() -{ - DeviceControlServer::DeviceControlSvr().SetSwitchDelegate(&gSwitchDelegate); + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) + { + ChipLogError(Zcl, "Server: Failed to record MultiPressComplete event"); + } } + +} // namespace Switch +} // namespace Clusters +} // namespace app +} // namespace chip + +void MatterSwitchPluginServerInitCallback() {} diff --git a/src/app/clusters/switch-server/switch-server.h b/src/app/clusters/switch-server/switch-server.h new file mode 100644 index 00000000000000..3724f1f6cd056f --- /dev/null +++ b/src/app/clusters/switch-server/switch-server.h @@ -0,0 +1,91 @@ +/* + * + * Copyright (c) 2021 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. + */ + +#pragma once + +#include +#include +#include +#include + +namespace chip { +namespace app { +namespace Clusters { +namespace Switch { + +/** + * @brief switch-server class + */ +class Server +{ +public: + static Server & Instance(); + + /** + * @brief + * Called when the latching switch is moved to a new position. + */ + void OnSwitchLatch(EndpointId endpoint, uint8_t newPosition); + + /** + * @brief + * Called when the momentary switch starts to be pressed. + */ + void OnInitialPress(EndpointId endpoint, uint8_t newPosition); + + /** + * @brief + * Called when the momentary switch has been pressed for a "long" time. + */ + void OnLongPress(EndpointId endpoint, uint8_t newPosition); + + /** + * @brief + * Called when the momentary switch has been released. + */ + void OnShortRelease(EndpointId endpoint, uint8_t previousPosition); + + /** + * @brief + * Called when the momentary switch has been released (after debouncing) + * after having been pressed for a long time. + */ + void OnLongRelease(EndpointId endpoint, uint8_t previousPosition); + + /** + * @brief + * Called to indicate how many times the momentary switch has been pressed + * in a multi-press sequence, during that sequence. + */ + void OnMultiPressOngoing(EndpointId endpoint, uint8_t newPosition, uint8_t count); + + /** + * @brief + * Called to indicate how many times the momentary switch has been pressed + * in a multi-press sequence, after it has been detected that the sequence has ended. + */ + void OnMultiPressComplete(EndpointId endpoint, uint8_t newPosition, uint8_t count); + +private: + static Server instance; +}; + +} // namespace Switch +} // namespace Clusters +} // namespace app +} // namespace chip diff --git a/src/include/platform/DeviceControlServer.h b/src/include/platform/DeviceControlServer.h index 6dc4ee7d07fdba..b43ba515c7f935 100644 --- a/src/include/platform/DeviceControlServer.h +++ b/src/include/platform/DeviceControlServer.h @@ -29,60 +29,6 @@ namespace chip { namespace DeviceLayer { -/** - * Defines the Swtich Device Control Delegate class to notify platform events. - */ -class SwitchDeviceControlDelegate -{ -public: - virtual ~SwitchDeviceControlDelegate() {} - - /** - * @brief - * Called when the latching switch is moved to a new position. - */ - virtual void OnSwitchLatched(uint8_t newPosition) {} - - /** - * @brief - * Called when the momentary switch starts to be pressed. - */ - virtual void OnInitialPressed(uint8_t newPosition) {} - - /** - * @brief - * Called when the momentary switch has been pressed for a "long" time. - */ - virtual void OnLongPressed(uint8_t newPosition) {} - - /** - * @brief - * Called when the momentary switch has been released. - */ - virtual void OnShortReleased(uint8_t previousPosition) {} - - /** - * @brief - * Called when the momentary switch has been released (after debouncing) - * and after having been pressed for a long time. - */ - virtual void OnLongReleased(uint8_t previousPosition) {} - - /** - * @brief - * Called to indicate how many times the momentary switch has been pressed - * in a multi-press sequence, during that sequence. - */ - virtual void OnMultiPressOngoing(uint8_t newPosition, uint8_t count) {} - - /** - * @brief - * Called to indicate how many times the momentary switch has been pressed - * in a multi-press sequence, after it has been detected that the sequence has ended. - */ - virtual void OnMultiPressComplete(uint8_t newPosition, uint8_t count) {} -}; - class DeviceControlServer final { public: @@ -92,8 +38,6 @@ class DeviceControlServer final CHIP_ERROR SetRegulatoryConfig(uint8_t location, const CharSpan & countryCode); CHIP_ERROR ConnectNetworkForOperational(ByteSpan networkID); - void SetSwitchDelegate(SwitchDeviceControlDelegate * delegate) { mSwitchDelegate = delegate; } - SwitchDeviceControlDelegate * GetSwitchDelegate() const { return mSwitchDelegate; } FailSafeContext & GetFailSafeContext() { return mFailSafeContext; } static DeviceControlServer & DeviceControlSvr(); @@ -102,7 +46,6 @@ class DeviceControlServer final // ===== Members for internal use by the following friends. static DeviceControlServer sInstance; FailSafeContext mFailSafeContext; - SwitchDeviceControlDelegate * mSwitchDelegate = nullptr; // ===== Private members reserved for use by this class only. diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index b800e6a23baa8a..0be5146e547ac0 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -78,9 +78,6 @@ void SignalHandler(int signum) case SIGTTIN: PlatformMgrImpl().HandleGeneralFault(GeneralDiagnostics::Events::NetworkFaultChange::Id); break; - case SIGTSTP: - PlatformMgrImpl().HandleSwitchEvent(Switch::Events::SwitchLatched::Id); - break; default: break; } @@ -328,89 +325,6 @@ void PlatformManagerImpl::HandleSoftwareFault(uint32_t EventId) } } -void PlatformManagerImpl::HandleSwitchEvent(uint32_t EventId) -{ - SwitchDeviceControlDelegate * delegate = DeviceControlServer::DeviceControlSvr().GetSwitchDelegate(); - - if (delegate == nullptr) - { - ChipLogError(DeviceLayer, "No delegate registered to handle Switch event"); - return; - } - - if (EventId == Switch::Events::SwitchLatched::Id) - { - uint8_t newPosition = 0; - -#if CHIP_CONFIG_TEST - newPosition = 100; -#endif - delegate->OnSwitchLatched(newPosition); - } - else if (EventId == Switch::Events::InitialPress::Id) - { - uint8_t newPosition = 0; - -#if CHIP_CONFIG_TEST - newPosition = 100; -#endif - delegate->OnInitialPressed(newPosition); - } - else if (EventId == Switch::Events::LongPress::Id) - { - uint8_t newPosition = 0; - -#if CHIP_CONFIG_TEST - newPosition = 100; -#endif - delegate->OnLongPressed(newPosition); - } - else if (EventId == Switch::Events::ShortRelease::Id) - { - uint8_t previousPosition = 0; - -#if CHIP_CONFIG_TEST - previousPosition = 50; -#endif - delegate->OnShortReleased(previousPosition); - } - else if (EventId == Switch::Events::LongRelease::Id) - { - uint8_t previousPosition = 0; - -#if CHIP_CONFIG_TEST - previousPosition = 50; -#endif - delegate->OnLongReleased(previousPosition); - } - else if (EventId == Switch::Events::MultiPressOngoing::Id) - { - uint8_t newPosition = 0; - uint8_t currentNumberOfPressesCounted = 0; - -#if CHIP_CONFIG_TEST - newPosition = 10; - currentNumberOfPressesCounted = 5; -#endif - delegate->OnMultiPressOngoing(newPosition, currentNumberOfPressesCounted); - } - else if (EventId == Switch::Events::MultiPressComplete::Id) - { - uint8_t newPosition = 0; - uint8_t totalNumberOfPressesCounted = 0; - -#if CHIP_CONFIG_TEST - newPosition = 10; - totalNumberOfPressesCounted = 5; -#endif - delegate->OnMultiPressComplete(newPosition, totalNumberOfPressesCounted); - } - else - { - ChipLogError(DeviceLayer, "Unknow event ID:%d", EventId); - } -} - #if CHIP_WITH_GIO GDBusConnection * PlatformManagerImpl::GetGDBusConnection() { diff --git a/src/platform/Linux/PlatformManagerImpl.h b/src/platform/Linux/PlatformManagerImpl.h index 9628e4f2bcabb6..37f7cce7edf5ca 100644 --- a/src/platform/Linux/PlatformManagerImpl.h +++ b/src/platform/Linux/PlatformManagerImpl.h @@ -58,7 +58,6 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener void HandleGeneralFault(uint32_t EventId); void HandleSoftwareFault(uint32_t EventId); - void HandleSwitchEvent(uint32_t EventId); private: // ===== Methods that implement the PlatformManager abstract interface.