Skip to content

Commit

Permalink
Commit the draft to add_event in post attr change
Browse files Browse the repository at this point in the history
Commit the draft to add_event in post attribute change callback
  • Loading branch information
erwinpan1 committed Apr 11, 2024
1 parent f29ccbe commit 33e93f2
Show file tree
Hide file tree
Showing 7 changed files with 936 additions and 14 deletions.
187 changes: 187 additions & 0 deletions examples/chef/common/clusters/switch/SwitchDelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
*
* Copyright (c) 2022 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.
*/

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/clusters/switch-server/switch-server.h>
#include <app/server/Server.h>
#include <app/util/att-storage.h>
#include <platform/PlatformManager.h>
#include "stubs.h"

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::Switch;
using namespace chip::DeviceLayer;

class SwitchEventHandler
{
public:
SwitchEventHandler(EndpointId endpoint):mEndpointId(endpoint){};

/**
* Should be called when the latching switch is moved to a new position.
*/
void OnSwitchLatchedHandler(uint8_t newPosition);

/**
* Should be called when the momentary switch starts to be pressed.
*/
void OnSwitchInitialPressedHandler(uint8_t newPosition);

/**
* Should be called when the momentary switch has been pressed for a "long" time.
*/
void OnSwitchLongPressedHandler(uint8_t newPosition);

/**
* Should be called when the momentary switch has been released.
*/
void OnSwitchShortReleasedHandler(uint8_t previousPosition);

/**
* Should be called when the momentary switch has been released after having been pressed for a long time.
*/
void OnSwitchLongReleasedHandler(uint8_t previousPosition);

/**
* Should be called to indicate how many times the momentary switch has been pressed in a multi-press
* sequence, during that sequence.
*/
void OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count);

/**
* Should be 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 OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count);

private:
EndpointId mEndpointId;
};


void SwitchEventHandler::OnSwitchLatchedHandler(uint8_t newPosition)
{
ChipLogDetail(NotSpecified, "The latching switch is moved to a new position:%d", newPosition);

Clusters::SwitchServer::Instance().OnSwitchLatch(mEndpointId, newPosition);
}

void SwitchEventHandler::OnSwitchInitialPressedHandler(uint8_t newPosition)
{
ChipLogDetail(NotSpecified, "The new position when the momentary switch starts to be pressed:%d", newPosition);

Clusters::SwitchServer::Instance().OnInitialPress(mEndpointId, newPosition);
}

void SwitchEventHandler::OnSwitchLongPressedHandler(uint8_t newPosition)
{
ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed for a long time:%d", newPosition);

Clusters::SwitchServer::Instance().OnLongPress(mEndpointId, newPosition);
}

void SwitchEventHandler::OnSwitchShortReleasedHandler(uint8_t previousPosition)
{
ChipLogDetail(NotSpecified, "The the previous value of the CurrentPosition when the momentary switch has been released:%d",
previousPosition);

Clusters::SwitchServer::Instance().OnShortRelease(mEndpointId, previousPosition);
}

void SwitchEventHandler::OnSwitchLongReleasedHandler(uint8_t previousPosition)
{
ChipLogDetail(NotSpecified,
"The the previous value of the CurrentPosition when the momentary switch has been released after having been "
"pressed for a long time:%d",
previousPosition);

Clusters::SwitchServer::Instance().OnLongRelease(mEndpointId, previousPosition);
}

void SwitchEventHandler::OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count)
{
ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed in a multi-press sequence:%d",
newPosition);
ChipLogDetail(NotSpecified, "%d times the momentary switch has been pressed", count);

Clusters::SwitchServer::Instance().OnMultiPressOngoing(mEndpointId, newPosition, count);
}

void SwitchEventHandler::OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count)
{
ChipLogDetail(NotSpecified, "The previous position when the momentary switch has been pressed in a multi-press sequence:%d",
previousPosition);
ChipLogDetail(NotSpecified, "%d times the momentary switch has been pressed", count);

Clusters::SwitchServer::Instance().OnMultiPressComplete(mEndpointId, previousPosition, count);
}

static std::map<int, SwitchEventHandler *> gSwitchEventHandlers{};


class SwitchAttributeDelegate : public AttributeDelegate
{
public:
SwitchAttributeDelegate (ClusterId clusterId) : AttributeDelegate(clusterId) {}

void PostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, uint8_t * value) override;

private:
};


SwitchEventHandler * GetSwitchEventHandler(EndpointId endpointId)
{
if (gSwitchEventHandlers.find(endpointId) == gSwitchEventHandlers.end()) {
return nullptr;
}

return gSwitchEventHandlers[endpointId];
}

void SwitchAttributeDelegate::PostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, uint8_t * value)
{
printf("\033[41m %s, %d \033[0m \n", __func__, __LINE__);
ChipLogProgress(Zcl, "SwitchAttributeDelegate::PostAttributeChangeCallback Endpoint: %d, Cluster: " ChipLogFormatMEI ", Type: %u, length %u", attributePath.mEndpointId, ChipLogValueMEI(attributePath.mClusterId), type, size);

switch (attributePath.mAttributeId) {
case Attributes::CurrentPosition::Id: {
SwitchEventHandler *eventHandler = GetSwitchEventHandler(attributePath.mEndpointId);
uint8_t newPosition = *value;

if (eventHandler) {
eventHandler->OnSwitchLatchedHandler(newPosition);
}
}
break;
default:
break;
}
}

void emberAfSwitchClusterInitCallback(EndpointId endpoint)
{
ChipLogProgress(Zcl, "Chef: emberAfSwitchClusterInitCallback");
gSwitchEventHandlers[endpoint] = new SwitchEventHandler(endpoint);
RegisterApplicationAttributeDelegate(Switch::Id, new SwitchAttributeDelegate(Switch::Id));
printf("\033[44m %s, %d, Switch::ID=%u \033[0m \n", __func__, __LINE__, Switch::Id);
}


43 changes: 43 additions & 0 deletions examples/chef/common/clusters/switch/SwitchDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*
* Copyright (c) 2022 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/clusters/switch-server/switch-server.h>

#include <protocols/interaction_model/StatusCode.h>

namespace chip {
namespace app {
namespace Clusters {

namespace Switch {

} // namespace Switch
} // namespace Clusters
} // namespace app
} // namespace chip



//class AllClustersCommandDelegate : public NamedPipeCommandDelegate
//{
//public:
// void OnEventCommandReceived(const char * json) override;
//};
59 changes: 47 additions & 12 deletions examples/chef/common/stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,36 @@
#include "chef-concentration-measurement.h"
#endif

#include "stubs.h"
#include <map>

using chip::app::DataModel::Nullable;

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;

static std::map<ClusterId, AttributeDelegate * > gApplicationAttributeDelegates{};

void chip::app::RegisterApplicationAttributeDelegate(ClusterId clusterId, AttributeDelegate * delegate)
{
// TODO assert (gApplicationAttributeDelegates.find(clusterId) == gApplicationAttributeDelegates.end() )

gApplicationAttributeDelegates[clusterId] = delegate;

}


AttributeDelegate * GetApplicationAttributeDelegate(ClusterId clusterId)
{
if (gApplicationAttributeDelegates.find(clusterId) == gApplicationAttributeDelegates.end()) {
return nullptr;
}

return gApplicationAttributeDelegates[clusterId];
}


Protocols::InteractionModel::Status emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata,
uint8_t * buffer, uint16_t maxReadLength)
Expand Down Expand Up @@ -116,22 +140,33 @@ void emberAfPluginSmokeCoAlarmSelfTestRequestCommand(EndpointId endpointId) {}
void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
uint8_t * value)
{
ClusterId clusterId = attributePath.mClusterId;
AttributeId attributeId = attributePath.mAttributeId;
ChipLogProgress(Zcl, "Cluster callback: " ChipLogFormatMEI, ChipLogValueMEI(clusterId));
// AttributeId attributeId = attributePath.mAttributeId;
ChipLogProgress(Zcl, "MatterPostAttributeChangeCallback Endpoint: %d, Cluster: " ChipLogFormatMEI ", Type: %u, length %u", attributePath.mEndpointId, ChipLogValueMEI(attributePath.mClusterId), type, size);
printf("\033[41m %s, %d \033[0m \n", __func__, __LINE__);

if (clusterId == OnOff::Id && attributeId == OnOff::Attributes::OnOff::Id)
{
ChipLogProgress(Zcl, "OnOff attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u", ChipLogValueMEI(attributeId),
type, *value, size);
AttributeDelegate * delegate = GetApplicationAttributeDelegate(attributePath.mClusterId);
if (delegate) {
delegate->PostAttributeChangeCallback(attributePath, type, size, value);
}
else if (clusterId == LevelControl::Id)
{
ChipLogProgress(Zcl, "Level Control attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u",
ChipLogValueMEI(attributeId), type, *value, size);

// WIP Apply attribute change to Light
#if 0
switch (clusterId) {
case OnOff::Id:
ChipLogProgress(Zcl, "OnOff Cluster attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u", ChipLogValueMEI(attributeId),
type, *value, size);
break;
case LevelControl::Id:
ChipLogProgress(Zcl, "Level Control Cluster attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u",
ChipLogValueMEI(attributeId), type, *value, size);
break;
case Switch::Id:
ChipLogProgress(Zcl, "Switch Cluster attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u",
ChipLogValueMEI(attributeId), type, *value, size);
break;
default:
break;
}
#endif
}

/** @brief OnOff Cluster Init
Expand Down
61 changes: 61 additions & 0 deletions examples/chef/common/stubs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
*
* Copyright (c) 2022 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 <protocols/interaction_model/StatusCode.h>

namespace chip {
namespace app {

class AttributeDelegate
{
public:
AttributeDelegate (ClusterId clusterId) : mClusterId(clusterId) {}
/**
* xxxx
*/
virtual void PostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, uint8_t * value) {}
/**
* xxxx
*/
virtual Protocols::InteractionModel::Status ExternalAttributeWriteCallback(EndpointId endpoint, ClusterId clusterId, const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer) {return Protocols::InteractionModel::Status::Success;}
/**
* xxxx
*/
virtual Protocols::InteractionModel::Status ExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId, const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer, uint16_t maxReadLength) {return Protocols::InteractionModel::Status::Success;}

virtual ~AttributeDelegate() {}

private:
ClusterId mClusterId;
};

void RegisterApplicationAttributeDelegate(ClusterId clusterId, AttributeDelegate * delegate);

} // namespace app
} // namespace chip



//class AllClustersCommandDelegate : public NamedPipeCommandDelegate
//{
//public:
// void OnEventCommandReceived(const char * json) override;
//};
Loading

0 comments on commit 33e93f2

Please sign in to comment.