Skip to content

Commit

Permalink
Refactor the switch delegate APIs from platform layer to application …
Browse files Browse the repository at this point in the history
…domain (#18742)

* The application should register and call the delegate APIs for each switch event

* Update src/app/clusters/switch-server/switch-server.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/app/clusters/switch-server/switch-server.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/app/clusters/switch-server/switch-server.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/app/clusters/switch-server/switch-server.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/app/clusters/switch-server/switch-server.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Address review comments

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jul 7, 2023
1 parent 3ed9be5 commit 5161539
Showing 5 changed files with 188 additions and 283 deletions.
236 changes: 97 additions & 139 deletions src/app/clusters/switch-server/switch-server.cpp
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
* limitations under the License.
*/

#include "switch-server.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Attributes.h>
@@ -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() {}
91 changes: 91 additions & 0 deletions src/app/clusters/switch-server/switch-server.h
Original file line number Diff line number Diff line change
@@ -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 <app-common/zap-generated/cluster-objects.h>
#include <app/AttributeAccessInterface.h>
#include <app/CommandResponseHelper.h>
#include <app/util/af.h>

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
Loading

0 comments on commit 5161539

Please sign in to comment.