Skip to content

Commit

Permalink
Add Mode Select Cluster
Browse files Browse the repository at this point in the history
Problem
Add the implementation for Mode Select Cluster. The specification for
Mode Select Cluster can be found at https://github.com/CHIP-Specifications/connectedhomeip-spec/blob/master/src/app_clusters/ModeSelect.adoc

Change overview
* Add src/app/zap-templates/zcl/data-model/chip/mode-select-cluster.xml
* Add the ModeSelectCluster to the all-cluster-app
* Add the ModeSelectCluster to the chip-tool
* Add src/app/clusters/mode-select-server/mode-select-server.cpp, the
main logic for the server of the ModeSelectCluster
* Add src/app/clusters/mode-select-server/supported-modes-manager.h,
which contains helper method for managing the supported options.
* Generate code for Mode Select Cluster
* Also fixed the src/app/clusters/operational-credentials-server/operational-credentials-server.cpp

Testing
How was this tested? (at least one bullet point required)

If manually tested, what platforms controller and device platforms were manually tested, and how?
Manually tested with chip-tool built on MacOs. Tested by issuing the read command and the "change-to-mode" command, and verified that all the attributes can be read, and "change-to-mode" correctly changes the "current-mode" attribute.
  • Loading branch information
dinglamazon committed Oct 21, 2021
1 parent b5ad57f commit 63e78d1
Show file tree
Hide file tree
Showing 53 changed files with 10,889 additions and 8,295 deletions.
136 changes: 136 additions & 0 deletions examples/all-clusters-app/all-clusters-common/all-clusters-app.zap
Original file line number Diff line number Diff line change
Expand Up @@ -3817,6 +3817,142 @@
}
]
},
{
"name": "Mode Select Cluster",
"code": 80,
"mfgCode": null,
"define": "MODE_SELECT_CLUSTER",
"side": "client",
"enabled": 0,
"commands": [
{
"name": "ChangeToMode",
"code": 0,
"mfgCode": null,
"source": "client",
"incoming": 1,
"outgoing": 1
}
],
"attributes": [
{
"name": "ClusterRevision",
"code": 65533,
"mfgCode": null,
"side": "client",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "1",
"reportable": 0,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
}
]
},
{
"name": "Mode Select Cluster",
"code": 80,
"mfgCode": null,
"define": "MODE_SELECT_CLUSTER",
"side": "server",
"enabled": 1,
"commands": [],
"attributes": [
{
"name": "CurrentMode",
"code": 0,
"mfgCode": null,
"side": "server",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "0",
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "SupportedModes",
"code": 1,
"mfgCode": null,
"side": "server",
"included": 1,
"storageOption": "External",
"singleton": 0,
"bounded": 0,
"defaultValue": "0",
"reportable": 0,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "OnMode",
"code": 2,
"mfgCode": null,
"side": "server",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "0",
"reportable": 0,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "StartUpMode",
"code": 3,
"mfgCode": null,
"side": "server",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "0",
"reportable": 0,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "Description",
"code": 4,
"mfgCode": null,
"side": "server",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "Coffee",
"reportable": 0,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "ClusterRevision",
"code": 65533,
"mfgCode": null,
"side": "server",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "1",
"reportable": 0,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
}
]
},
{
"name": "Door Lock",
"code": 257,
Expand Down
161 changes: 161 additions & 0 deletions src/app/clusters/mode-select-server/mode-select-server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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.
*/

/**
*
* Copyright (c) 2021 Silicon Labs
*
* 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
* @brief Routines for the Mode Select plugin, the
*server implementation of the Mode Select Cluster.
*******************************************************************************
******************************************************************************/
#include <string>

#include <app-common/zap-generated/af-structs.h>
#include <app-common/zap-generated/att-storage.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/command-id.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/AttributeAccessInterface.h>
#include <app/CommandHandler.h>
#include <app/ConcreteCommandPath.h>
#include <app/clusters/mode-select-server/supported-modes-manager.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/support/CodeUtils.h>
#include <platform/PlatformManager.h>

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

namespace {

class ModeSelectAttrAccess : public chip::app::AttributeAccessInterface
{
public:
ModeSelectAttrAccess() : chip::app::AttributeAccessInterface(Optional<EndpointId>::Missing(), ModeSelectCluster::Id) {}

CHIP_ERROR Read(const ConcreteAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
};

ModeSelectCluster::Structs::ModeOptionStruct::Type buildModeOptionStruct(const char * label, size_t labelLength, uint8_t mode,
uint32_t semanticTag)
{
ModeSelectCluster::Structs::ModeOptionStruct::Type option;
option.label = ByteSpan((const unsigned char *) label, labelLength);
option.mode = mode;
option.semanticTag = semanticTag;
return option;
}

ModeSelectAttrAccess gModeSelectAttrAccess;

// TODO: Add as many ModeOptions as necessary.
ModeSelectCluster::Structs::ModeOptionStruct::Type option1 = buildModeOptionStruct("Black", 5, 0, 0);
ModeSelectCluster::Structs::ModeOptionStruct::Type option2 = buildModeOptionStruct("White", 5, 4, 0);
ModeSelectCluster::Structs::ModeOptionStruct::Type option3 = buildModeOptionStruct("Half-and-half", 13, 7, 0);

const ModeSelectCluster::SupportedModesManager & gSupportedModeManager = ModeSelectCluster::SupportedModesManager::Builder()
// TODO: Add as many ModeOptions as necessary.
.addSupportedMode((unsigned short) 0u, option1)
.addSupportedMode((unsigned short) 0u, option2)
.addSupportedMode((unsigned short) 0u, option3)
.build();

CHIP_ERROR ModeSelectAttrAccess::Read(const ConcreteAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
if (aPath.mClusterId != ModeSelectCluster::Id)
{
// We shouldn't have been called at all.
return CHIP_ERROR_INVALID_ARGUMENT;
}

switch (aPath.mAttributeId)
{
case ModeSelectCluster::Attributes::SupportedModes::Id:
const vector<ModeSelectCluster::Structs::ModeOptionStruct::Type> & supportedOptions =
gSupportedModeManager.getSupportedModesForEndpoint(aPath.mEndpointId);
CHIP_ERROR err;
err = aEncoder.EncodeList([supportedOptions](const TagBoundEncoder & encoder) -> CHIP_ERROR {
for (vector<ModeSelectCluster::Structs::ModeOptionStruct::Type>::const_iterator it = supportedOptions.begin();
it != supportedOptions.end(); ++it)
{
const ModeSelectCluster::Structs::ModeOptionStruct::Type & modeOption = *it;
ReturnErrorOnFailure(encoder.Encode(modeOption));
}
return CHIP_NO_ERROR;
});
ReturnErrorOnFailure(err);
emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelectCluster: Successfully read SupportedModes");
}
return CHIP_NO_ERROR;
}

} // anonymous namespace

bool emberAfModeSelectClusterClusterChangeToModeCallback(
chip::app::CommandHandler * commandHandler, const chip::app::ConcreteCommandPath & commandPath,
const ModeSelectCluster::Commands::ChangeToMode::DecodableType & commandData)
{
emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelectCluster: Entering emberAfModeSelectClusterClusterChangeToModeCallback");
EndpointId endpointId = commandPath.mEndpointId;
uint8_t newMode = commandData.newMode;
// Check that the newMode matches one of the supported options
const ModeSelectCluster::Structs::ModeOptionStruct::Type * modeOptionPtr;
EmberAfStatus checkSupportedModeStatus = gSupportedModeManager.getModeOptionByMode(endpointId, newMode, modeOptionPtr);
if (EMBER_ZCL_STATUS_SUCCESS != checkSupportedModeStatus)
{
emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelectCluster: Failed to find the option with mode %hhu", newMode);
emberAfSendImmediateDefaultResponse(checkSupportedModeStatus);
return false;
}
// TODO: Implement application logic

emberAfWriteAttribute(endpointId, ModeSelectCluster::Id, ModeSelectCluster::Attributes::CurrentMode::Id, CLUSTER_MASK_SERVER,
&newMode, ZCL_INT8U_ATTRIBUTE_TYPE);

emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelectCluster: ChangeToMode successful");
emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
return true;
}

void emberAfModeSelectClusterClusterInitCallback(EndpointId endpoint)
{
static bool attrAccessRegistered = false;
if (!attrAccessRegistered)
{
registerAttributeAccessOverride(&gModeSelectAttrAccess);
attrAccessRegistered = true;
}
}
Loading

0 comments on commit 63e78d1

Please sign in to comment.