Skip to content

Commit

Permalink
Add Mode Select Cluster (#10752)
Browse files Browse the repository at this point in the history
  • Loading branch information
du48s03 authored and pull[bot] committed Jul 11, 2022
1 parent d058cfa commit 9985289
Show file tree
Hide file tree
Showing 66 changed files with 4,144 additions and 206 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 @@ -9500,6 +9500,142 @@
}
]
},
{
"name": "Mode Select",
"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",
"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
1 change: 1 addition & 0 deletions examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/content-launch-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/media-input-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/mode-select-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/low-power-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/keypad-input-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/media-playback-server"
Expand Down
2 changes: 2 additions & 0 deletions examples/all-clusters-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ target_sources(${APP_TARGET} PRIVATE
${APP_CLUSTERS}/low-power-server/low-power-server.cpp
${APP_CLUSTERS}/media-input-server/media-input-server.cpp
${APP_CLUSTERS}/media-playback-server/media-playback-server.cpp
${APP_CLUSTERS}/mode-select-server/mode-select-server.cpp
${APP_CLUSTERS}/mode-select-server/static-supported-modes-manager.cpp
${APP_CLUSTERS}/network-commissioning/network-commissioning-ember.cpp
${APP_CLUSTERS}/network-commissioning/network-commissioning.cpp
${APP_CLUSTERS}/on-off-server/on-off-server.cpp
Expand Down
1 change: 1 addition & 0 deletions examples/chip-tool/templates/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function getTests()
'TestBasicInformation',
'TestIdentifyCluster',
'TestOperationalCredentialsCluster',
'TestModeSelectCluster',
];

const Subscriptions = [
Expand Down
5 changes: 5 additions & 0 deletions src/app/chip_data_model.gni
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ template("chip_data_model") {
"${_app_root}/clusters/${cluster}/${cluster}-ember.cpp",
"${_app_root}/clusters/${cluster}/${cluster}.cpp",
]
} else if (cluster == "mode-select-server") {
sources += [
"${_app_root}/clusters/${cluster}/${cluster}.cpp",
"${_app_root}/clusters/${cluster}/static-supported-modes-manager.cpp",
]
} else {
sources += [ "${_app_root}/clusters/${cluster}/${cluster}.cpp" ]
}
Expand Down
113 changes: 113 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,113 @@
/**
*
* 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.
*/

#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/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-objects.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/static-supported-modes-manager.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/support/CodeUtils.h>

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

namespace {

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

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

ModeSelectAttrAccess gModeSelectAttrAccess;

CHIP_ERROR ModeSelectAttrAccess::Read(const ConcreteAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
VerifyOrDie(aPath.mClusterId == ModeSelect::Id);

const ModeSelect::StaticSupportedModesManager & gSupportedModeManager =
ModeSelect::StaticSupportedModesManager::getStaticSupportedModesManagerInstance();

if (ModeSelect::Attributes::SupportedModes::Id == aPath.mAttributeId)
{
const ModeSelect::StaticSupportedModesManager::IteratorFactory * iteratorFactory =
gSupportedModeManager.getIteratorFactory(aPath.mEndpointId);
if (iteratorFactory == nullptr)
{
aEncoder.Encode(DataModel::List<ModeSelect::Structs::ModeOptionStruct::Type>());
return CHIP_NO_ERROR;
}
CHIP_ERROR err;
err = aEncoder.EncodeList([iteratorFactory](const TagBoundEncoder & encoder) -> CHIP_ERROR {
const auto & end = *(iteratorFactory->end());
for (auto it = *(iteratorFactory->begin()); it != end; ++it)
{
emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelect: dereferencing it");
emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelect: it= %p", (void *) it.operator->());
auto & modeOption = *it;
ReturnErrorOnFailure(encoder.Encode(modeOption));
}
return CHIP_NO_ERROR;
});
ReturnErrorOnFailure(err);
}
return CHIP_NO_ERROR;
}

} // anonymous namespace

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

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

void MatterModeSelectPluginServerInitCallback(void)
{
registerAttributeAccessOverride(&gModeSelectAttrAccess);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Created by Ding, Li-an on 10/21/21.
//
#include <app/clusters/mode-select-server/static-supported-modes-manager.h>
#include <map>
#include <vector>

using namespace std;
using namespace chip;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::ModeSelect;

using ModeOptionStructType = Structs::ModeOptionStruct::Type;
using storage_value_type = const ModeOptionStructType *;
namespace {
Structs::ModeOptionStruct::Type buildModeOptionStruct(const char * label, uint8_t mode, uint32_t semanticTag)
{
Structs::ModeOptionStruct::Type option;
option.label = CharSpan(label, strlen(label));
option.mode = mode;
option.semanticTag = semanticTag;
return option;
}
} // namespace

const Structs::ModeOptionStruct::Type StaticSupportedModesManager::blackOption = buildModeOptionStruct("Black", 0, 0);
const Structs::ModeOptionStruct::Type StaticSupportedModesManager::cappuccinoOption = buildModeOptionStruct("Cappuccino", 4, 0);
const Structs::ModeOptionStruct::Type StaticSupportedModesManager::espressoOption = buildModeOptionStruct("Espresso", 7, 0);
storage_value_type StaticSupportedModesManager::coffeeOptions[] = { &blackOption, &cappuccinoOption, &espressoOption };
const Span<storage_value_type> StaticSupportedModesManager::coffeeOptionsSpan =
Span<storage_value_type>(StaticSupportedModesManager::coffeeOptions);
const map<EndpointId, Span<storage_value_type>> StaticSupportedModesManager::optionsByEndpoints = {
{ 1, StaticSupportedModesManager::coffeeOptionsSpan }
};

const StaticSupportedModesManager StaticSupportedModesManager::instance = StaticSupportedModesManager();

const StaticSupportedModesManager::IteratorFactory * StaticSupportedModesManager::getIteratorFactory(EndpointId endpointId) const
{
const auto & it = _iteratorFactoriesByEndpoints.find(endpointId);
return (it == _iteratorFactoriesByEndpoints.end()) ? nullptr : &(it->second);
}

EmberAfStatus StaticSupportedModesManager::getModeOptionByMode(unsigned short endpointId, unsigned char mode,
const ModeOptionStructType ** dataPtr) const
{
auto * iteratorFactory = this->getIteratorFactory(endpointId);
if (iteratorFactory == nullptr)
{
return EMBER_ZCL_STATUS_UNSUPPORTED_CLUSTER;
}
const StaticSupportedModesManager::Iterator & begin = *(this->getIteratorFactory(endpointId)->begin());
const StaticSupportedModesManager::Iterator & end = *(this->getIteratorFactory(endpointId)->end());
for (auto it = begin; it != end; ++it)
{
auto & modeOption = *it;
if (modeOption.mode == mode)
{
*dataPtr = &modeOption;
return EMBER_ZCL_STATUS_SUCCESS;
}
}
emberAfPrintln(EMBER_AF_PRINT_DEBUG, "Cannot find the mode %c", mode);
return EMBER_ZCL_STATUS_INVALID_ARGUMENT;
}
Loading

0 comments on commit 9985289

Please sign in to comment.