Skip to content

Commit

Permalink
new: chef test custom cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
rochaferraz committed Aug 17, 2023
1 parent b0b0d58 commit fe51635
Show file tree
Hide file tree
Showing 38 changed files with 15,898 additions and 1 deletion.
21 changes: 21 additions & 0 deletions examples/chef/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,24 @@ To add new devices for chef:
`examples/chef/devices`.
- This is gated by the workflow in `.github/workflows/zap_templates.yaml`.
- All devices added to the repository are built in CD.

## Vendor Defined Clusters

You may add vendor-defined features to chef. The rootnode_onofflight_meisample* device
showcases its usage by using the "cheftestcluster" which is defined on
`src/app/zap-templates/zcl/data-model/chip/chef-test-cluster.xml`

This cluster has one boolean attribute and a command/response pair. The
command takes two uint8 arguments and the command response returns their sum.

You may test the chef-test-cluster via chip-tool using the following commands:
```
# commissioning of on-network chef device
chip-tool pairing onnetwork 1 20202021
# tests command to sum arguments
chip-tool cheftestcluster test-add-arguments 1 1 10 20
# sets attribute1 to false
chip-tool cheftestcluster write attribute1 0 1 1
# reads attribute1
chip-tool cheftestcluster read attribute1 1 1
```
129 changes: 129 additions & 0 deletions examples/chef/common/chef-test-cluster-server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include "chef-test-cluster-server.h"

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Commands.h>
#include <app/CommandHandler.h>
#include <app/ConcreteCommandPath.h>
#include <app/InteractionModelEngine.h>
#include <app/reporting/reporting.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <app/util/util.h>

#include <algorithm>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::ChefTestCluster;
using namespace chip::app::Clusters::ChefTestCluster::Commands;
using namespace chip::app::Clusters::ChefTestCluster::Attributes;


namespace chip {
namespace app {
namespace Clusters {
namespace ChefTestCluster {

CHIP_ERROR ChefTestClusterServer::Init()
{
ChipLogProgress(Zcl,">>>>> ChefTest Init");
ReturnErrorOnFailure(chip::app::InteractionModelEngine::GetInstance()->RegisterCommandHandler(this));
VerifyOrReturnError(registerAttributeAccessOverride(this), CHIP_ERROR_INCORRECT_STATE);
attribute1 = true;
return CHIP_NO_ERROR;
}

void ChefTestClusterServer::InvokeCommand(HandlerContext & ctxt)
{
ChipLogProgress(Zcl,">>>>> Invoke");
switch (ctxt.mRequestPath.mCommandId)
{
case Commands::Test::Id:
HandleCommand<Commands::Test::DecodableType>(ctxt, [this](HandlerContext & ctx, const auto & req) {
ChipLogProgress(Zcl,">>>>> TestCommand");
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::Success);
});
return;
case Commands::TestAddArguments::Id:
HandleCommand<Commands::TestAddArguments::DecodableType>(ctxt, [this](HandlerContext & ctx, const auto & req) {
ChipLogProgress(Zcl,">>>>> TestAddArgumentsCommand");
if (req.arg1 > UINT8_MAX - req.arg2)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::InvalidCommand);
return;
}

TestAddArgumentsResponse::Type responseData;
responseData.returnValue = static_cast<uint8_t>(req.arg1 + req.arg2);
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, responseData);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::Success);
});
return;
}
}

CHIP_ERROR ChefTestClusterServer::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
switch (aPath.mAttributeId)
{
case Attributes::Attribute1::Id:
return aEncoder.Encode(attribute1);

default:
return CHIP_NO_ERROR;
}
}

CHIP_ERROR ChefTestClusterServer::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder)
{
switch (aPath.mAttributeId)
{
case Attributes::Attribute1::Id:
bool value;
ReturnErrorOnFailure(aDecoder.Decode(value));
attribute1 = value;
return CHIP_NO_ERROR;
default:
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}
}

} // namespace ChefTestCluster
} // namespace Clusters
} // namespace app
} // namespace chip

/**********************************************************
* Callbacks Implementation
*********************************************************/

/**
* @brief Chef Test Cluster Cluster Test Command callback (from client)
*/
bool emberAfChefTestClusterClusterTestCallback(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
const Clusters::ChefTestCluster::Commands::Test::DecodableType & commandData)
{
return true;
}

/**
* @brief Chef Test Cluster Cluster TestAddArgumentsResponse Command callback (from server)
*/
bool emberAfChefTestClusterClusterTestAddArgumentsResponseCallback(EndpointId endpoint, CommandSender * commandObj,
uint8_t returnValue)
{
return true;
}

/**
* @brief Chef Test Cluster Cluster TestAddArguments Command callback (from client)
*/
bool emberAfChefTestClusterClusterTestAddArgumentsCallback(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
const Commands::TestAddArguments::DecodableType & commandData)
{
return true;
}

void MatterChefTestClusterPluginServerInitCallback() {}
43 changes: 43 additions & 0 deletions examples/chef/common/chef-test-cluster-server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef SRC_APP_CLUSTERS_CHEF_TEST_CLUSTER_SERVER_CHEF_TEST_CLUSTER_SERVER_H_
#define SRC_APP_CLUSTERS_CHEF_TEST_CLUSTER_SERVER_CHEF_TEST_CLUSTER_SERVER_H_

#include <app-common/zap-generated/cluster-objects.h>
#include <app/AttributeAccessInterface.h>
#include <app/CommandHandlerInterface.h>
#include <app/ConcreteCommandPath.h>
#include <app/util/af-types.h>
#include <app/util/basic-types.h>

#include <vector>

namespace chip {
namespace app {
namespace Clusters {
namespace ChefTestCluster {

class ChefTestClusterServer : public CommandHandlerInterface, public AttributeAccessInterface
{
public:
ChefTestClusterServer(EndpointId aEndpointId) :
CommandHandlerInterface(Optional<EndpointId>(aEndpointId), Id),
AttributeAccessInterface(Optional<EndpointId>(aEndpointId), Id)
{}

CHIP_ERROR Init();

// CommandHandlerInterface
void InvokeCommand(HandlerContext & ctx) override;

// AttributeAccessInterface
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
CHIP_ERROR Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder) override;

private:
bool attribute1;
};
} // namespace ChefTestCluster
} // namespace Clusters
} // namespace app
} // namespace chip

#endif // SRC_APP_CLUSTERS_CHEF_TEST_CLUSTER_SERVER_CHEF_TEST_CLUSTER_SERVER_H_
Loading

0 comments on commit fe51635

Please sign in to comment.