Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restyle Simplfy the implementation of Mode Select Cluster #11532

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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;
}
1 change: 1 addition & 0 deletions examples/all-clusters-app/ameba/chip_main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ list(
${chip_dir}/zzz_generated/all-clusters-app/zap-generated/CHIPClusters.cpp

${chip_dir}/examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp
${chip_dir}/examples/all-clusters-app/all-clusters-common/src/static-supported-modes-manager.cpp

${chip_dir}/examples/all-clusters-app/ameba/main/chipinterface.cpp
${chip_dir}/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp
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 @@ -19,6 +19,7 @@
# The list of src and include dirs must be in sync with that in all-clusters-app/esp32/main/component.mk
set(PRIV_INCLUDE_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/all-clusters-app"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/all-clusters-app/all-clusters-common/include"
"${CMAKE_CURRENT_LIST_DIR}/include"
)
set(SRC_DIRS_LIST
Expand Down
23 changes: 23 additions & 0 deletions examples/all-clusters-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

static_library("static-supported-modes-manager") {
sources = [ "${chip_root}/examples/all-clusters-app/all-clusters-common/src/static-supported-modes-manager.cpp" ]

#zap_pregenerated_dir = "${chip_root}/zzz_generated"

deps = []
deps += [
"${chip_root}/src/app",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/controller",
"${chip_root}/src/lib/core",
]
include_dirs = [
"include",
"${chip_root}/examples/all-clusters-app/all-clusters-common/include",
"${chip_root}/src",
"${chip_root}/zzz_generated/all-clusters-app",
]

cflags = [ "-Wconversion" ]
}

executable("chip-all-clusters-app") {
sources = [
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp",
Expand All @@ -23,6 +45,7 @@ executable("chip-all-clusters-app") {
]

deps = [
":static-supported-modes-manager",
"${chip_root}/examples/all-clusters-app/all-clusters-common",
"${chip_root}/examples/platform/linux:app-main",
"${chip_root}/src/lib",
Expand Down
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
4 changes: 3 additions & 1 deletion examples/all-clusters-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ target_include_directories(${APP_TARGET} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/main/include/
${APP_ROOT}/all-clusters-common
${APP_ROOT}/all-clusters-common/include
${APP_CLUSTERS}
${MBED_COMMON}/util/include
${CHIP_ROOT}/src/app
${CHIP_ROOT}/third_party/nlio/repo/include
Expand Down Expand Up @@ -67,6 +69,7 @@ target_sources(${APP_TARGET} PRIVATE
${CHIP_ROOT}/src/app/server/CommissioningWindowManager.cpp

${CHIP_ROOT}/examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp
${CHIP_ROOT}/examples/all-clusters-app/all-clusters-common/src/static-supported-modes-manager.cpp

${APP_UTIL}/DataModelHandler.cpp
${APP_UTIL}/af-event.cpp
Expand Down Expand Up @@ -112,7 +115,6 @@ target_sources(${APP_TARGET} PRIVATE
${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
26 changes: 26 additions & 0 deletions examples/all-clusters-app/p6/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,38 @@ p6_sdk_sources("all_clusters_app_sdk_sources") {
public_configs = [ "${chip_root}/third_party/p6:p6_sdk_config" ]
}

static_library("static-supported-modes-manager") {
sources = [ "src/static-supported-modes-manager.cpp" ]

#zap_pregenerated_dir = "${chip_root}/zzz_generated"

public_deps = []
public_deps += [
"${chip_root}/src/app",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/controller",
"${chip_root}/src/lib/core",
]
include_dirs = [
"${chip_root}",
"${chip_root}/src",
"${chip_root}/zzz_generated/all-clusters-app",
"${chip_root}/examples/all-clusters-app/all-clusters-common/include",
]

cflags = [ "-Wconversion" ]

print("include_dirs = ")
}

p6_executable("clusters_app") {
include_dirs = []
defines = []
output_name = "chip-p6-clusters-example.out"

sources = [
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp",
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/static-supported-modes-manager.cpp",
"${examples_plat_dir}/LEDWidget.cpp",
"${examples_plat_dir}/init_p6Platform.cpp",
"src/AppTask.cpp",
Expand All @@ -72,6 +97,7 @@ p6_executable("clusters_app") {

deps = [
":all_clusters_app_sdk_sources",
":static-supported-modes-manager",
"${chip_root}/examples/all-clusters-app/all-clusters-common",
"${chip_root}/examples/common/QRCode",
"${chip_root}/src/lib",
Expand Down
2 changes: 1 addition & 1 deletion src/app/chip_data_model.gni
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ template("chip_data_model") {
} else if (cluster == "mode-select-server") {
sources += [
"${_app_root}/clusters/${cluster}/${cluster}.cpp",
"${_app_root}/clusters/${cluster}/static-supported-modes-manager.cpp",
"${_app_root}/clusters/${cluster}/supported-modes-manager.h",
]
} else {
sources += [ "${_app_root}/clusters/${cluster}/${cluster}.cpp" ]
Expand Down
Loading