-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplfy the implementation of Mode Select Cluster
Restyled by whitespace Restyled by clang-format Update submodules
- Loading branch information
1 parent
ff5f946
commit 327b91b
Showing
12 changed files
with
180 additions
and
248 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
examples/all-clusters-app/all-clusters-common/include/static-supported-modes-manager.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* | ||
* 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/clusters/mode-select-server/supported-modes-manager.h> | ||
#include <app/util/af.h> | ||
#include <cstring> | ||
|
||
namespace chip { | ||
namespace app { | ||
namespace Clusters { | ||
namespace ModeSelect { | ||
|
||
/** | ||
* This implementation statically defines the options. | ||
*/ | ||
|
||
class StaticSupportedModesManager : public chip::app::Clusters::ModeSelect::SupportedModesManager | ||
{ | ||
using ModeOptionStructType = Structs::ModeOptionStruct::Type; | ||
using storage_value_type = const ModeOptionStructType; | ||
|
||
struct EndpointSpanPair | ||
{ | ||
const EndpointId mEndpointId; | ||
const Span<storage_value_type> mSpan; | ||
|
||
EndpointSpanPair(const EndpointId aEndpointId, const Span<storage_value_type> && aSpan) : | ||
mEndpointId(aEndpointId), mSpan(aSpan) | ||
{} | ||
|
||
EndpointSpanPair() : mEndpointId(0), mSpan(Span<storage_value_type>()) {} | ||
}; | ||
|
||
static storage_value_type coffeeOptions[]; | ||
static const EndpointSpanPair supportedOptionsByEndpoints[EMBER_AF_MODE_SELECT_CLUSTER_SERVER_ENDPOINT_COUNT]; | ||
|
||
public: | ||
static const StaticSupportedModesManager instance; | ||
|
||
const SupportedModesManager::ModeOptionsProvider getModeOptionsProvider(EndpointId endpointId) const override; | ||
|
||
EmberAfStatus getModeOptionByMode(EndpointId endpointId, uint8_t mode, const ModeOptionStructType ** dataPtr) const override; | ||
|
||
~StaticSupportedModesManager(){}; | ||
|
||
StaticSupportedModesManager() {} | ||
|
||
static inline const StaticSupportedModesManager & getStaticSupportedModesManagerInstance() { return instance; } | ||
}; | ||
|
||
const SupportedModesManager * getSupportedModesManager(); | ||
|
||
} // namespace ModeSelect | ||
} // namespace Clusters | ||
} // namespace app | ||
} // namespace chip |
71 changes: 71 additions & 0 deletions
71
examples/all-clusters-app/all-clusters-common/src/static-supported-modes-manager.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <static-supported-modes-manager.h> | ||
|
||
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 | ||
|
||
// TODO: Configure your options for each endpoint | ||
storage_value_type StaticSupportedModesManager::coffeeOptions[] = { buildModeOptionStruct("Black", 0, 0), | ||
buildModeOptionStruct("Cappuccino", 4, 0), | ||
buildModeOptionStruct("Espresso", 7, 0) }; | ||
const StaticSupportedModesManager::EndpointSpanPair | ||
StaticSupportedModesManager::supportedOptionsByEndpoints[EMBER_AF_MODE_SELECT_CLUSTER_SERVER_ENDPOINT_COUNT] = { | ||
EndpointSpanPair(1, Span<storage_value_type>(StaticSupportedModesManager::coffeeOptions)) // Options for Endpoint 1 | ||
}; | ||
|
||
const StaticSupportedModesManager StaticSupportedModesManager::instance = StaticSupportedModesManager(); | ||
|
||
const SupportedModesManager::ModeOptionsProvider StaticSupportedModesManager::getModeOptionsProvider(EndpointId endpointId) const | ||
{ | ||
for (auto & endpointSpanPair : supportedOptionsByEndpoints) | ||
{ | ||
if (endpointSpanPair.mEndpointId == endpointId) | ||
{ | ||
return ModeOptionsProvider(endpointSpanPair.mSpan.data(), endpointSpanPair.mSpan.end()); | ||
} | ||
} | ||
return ModeOptionsProvider(nullptr, nullptr); | ||
} | ||
|
||
EmberAfStatus StaticSupportedModesManager::getModeOptionByMode(unsigned short endpointId, unsigned char mode, | ||
const ModeOptionStructType ** dataPtr) const | ||
{ | ||
auto modeOptionsProvider = this->getModeOptionsProvider(endpointId); | ||
if (modeOptionsProvider.begin() == nullptr) | ||
{ | ||
return EMBER_ZCL_STATUS_UNSUPPORTED_CLUSTER; | ||
} | ||
auto * begin = this->getModeOptionsProvider(endpointId).begin(); | ||
auto * end = this->getModeOptionsProvider(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 %" PRIu8, mode); | ||
return EMBER_ZCL_STATUS_INVALID_ARGUMENT; | ||
} | ||
|
||
const ModeSelect::SupportedModesManager * ModeSelect::getSupportedModesManager() | ||
{ | ||
return &StaticSupportedModesManager::instance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
src/app/clusters/mode-select-server/static-supported-modes-manager.cpp
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.