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

Bridge-app: Add actions delegate in the Bridge app #37918

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions examples/bridge-app/asr/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ asr_executable("bridge_app") {
output_name = "chip-asr-bridge-example.out"

sources = [
"${chip_root}/examples/bridge-app/bridge-common/src/bridged-actions-stub.cpp",
"${examples_plat_dir}/CHIPDeviceManager.cpp",
"${examples_plat_dir}/init_Matter.cpp",
"${examples_plat_dir}/init_asrPlatform.cpp",
Expand Down Expand Up @@ -104,6 +105,7 @@ asr_executable("bridge_app") {
"${examples_plat_dir}",
"${asr_project_dir}/include",
"${chip_root}/src/lib",
"${chip_root}/examples/bridge-app/bridge-common/include",
]

defines = [ "ASR_NETWORK_LAYER_BLE=${chip_config_network_layer_ble}" ]
Expand Down
18 changes: 18 additions & 0 deletions examples/bridge-app/asr/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <app/util/attribute-storage.h>
#include <app/util/endpoint-config-api.h>
#include <assert.h>
#include <bridged-actions-stub.h>
#include <platform/ASR/NetworkCommissioningDriver.h>
#include <platform/CHIPDeviceLayer.h>
#include <setup_payload/OnboardingCodesUtil.h>
Expand All @@ -52,6 +53,9 @@ constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE;
app::Clusters::NetworkCommissioning::Instance
sWiFiNetworkCommissioningInstance(kNetworkCommissioningEndpointMain /* Endpoint Id */,
&(NetworkCommissioning::ASRWiFiDriver::GetInstance()));

std::unique_ptr<app::Clusters::Actions::ActionsDelegateImpl> sActionsDelegateImpl;
std::unique_ptr<app::Clusters::Actions::ActionsServer> sActionsServer;
} // namespace

void NetWorkCommissioningInstInit()
Expand All @@ -62,6 +66,20 @@ void NetWorkCommissioningInstInit()
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);
}

void emberAfActionsClusterInitCallback(EndpointId endpoint)
{
VerifyOrReturn(endpoint == 1,
ChipLogError(Zcl, "Actions cluster delegate is not implemented for endpoint with id %d.", endpoint));
VerifyOrReturn(emberAfContainsServer(endpoint, app::Clusters::Actions::Id) == true,
ChipLogError(Zcl, "Endpoint %d does not support Actions cluster.", endpoint));
VerifyOrReturn(!sActionsDelegateImpl && !sActionsServer);

sActionsDelegateImpl = std::make_unique<app::Clusters::Actions::ActionsDelegateImpl>();
sActionsServer = std::make_unique<app::Clusters::Actions::ActionsServer>(endpoint, *sActionsDelegateImpl.get());

sActionsServer->Init();
}

static DeviceCallbacks EchoCallbacks;

CHIP_ERROR AppTask::StartAppTask()
Expand Down
80 changes: 80 additions & 0 deletions examples/bridge-app/bridge-common/include/bridged-actions-stub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
*
* Copyright (c) 2024 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.
*/
#pragma once
#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/AttributeAccessInterfaceRegistry.h>
#include <app/clusters/actions-server/actions-server.h>
#include <app/util/attribute-storage.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <vector>

namespace chip {
namespace app {
namespace Clusters {
namespace Actions {
class ActionsDelegateImpl : public Delegate
{
private:
std::vector<ActionStructStorage> kActionList = {
ActionStructStorage(0, CharSpan::fromCharString("TurnOnLight"), ActionTypeEnum::kScene, 0, 0, ActionStateEnum::kInactive),
ActionStructStorage(1, CharSpan::fromCharString("TurnOffLight"), ActionTypeEnum::kScene, 1, 0, ActionStateEnum::kInactive),
ActionStructStorage(2, CharSpan::fromCharString("ToggleLight"), ActionTypeEnum::kScene, 2, 0, ActionStateEnum::kInactive)
};

std::vector<EndpointId> firstEpList = { 0 };
std::vector<EndpointId> secondEpList = { 0, 1 };
std::vector<EndpointId> thirdEpList = { 1, 2, 3 };

std::vector<EndpointListStorage> kEndpointList = {
EndpointListStorage(0, CharSpan::fromCharString("On"), EndpointListTypeEnum::kOther,
DataModel::List<const EndpointId>(firstEpList.data(), firstEpList.size())),
EndpointListStorage(1, CharSpan::fromCharString("Off"), EndpointListTypeEnum::kOther,
DataModel::List<const EndpointId>(secondEpList.data(), secondEpList.size())),
EndpointListStorage(2, CharSpan::fromCharString("Toggle"), EndpointListTypeEnum::kOther,
DataModel::List<const EndpointId>(thirdEpList.data(), thirdEpList.size()))
};

CHIP_ERROR ReadActionAtIndex(uint16_t index, ActionStructStorage & action) override;
CHIP_ERROR ReadEndpointListAtIndex(uint16_t index, EndpointListStorage & epList) override;
bool HaveActionWithId(uint16_t actionId, uint16_t & actionIndex) override;

Protocols::InteractionModel::Status HandleInstantAction(uint16_t actionId, Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandleInstantActionWithTransition(uint16_t actionId, uint16_t transitionTime,
Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandleStartAction(uint16_t actionId, Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandleStartActionWithDuration(uint16_t actionId, uint32_t duration,
Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandleStopAction(uint16_t actionId, Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandlePauseAction(uint16_t actionId, Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandlePauseActionWithDuration(uint16_t actionId, uint32_t duration,
Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandleResumeAction(uint16_t actionId, Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandleEnableAction(uint16_t actionId, Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandleEnableActionWithDuration(uint16_t actionId, uint32_t duration,
Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandleDisableAction(uint16_t actionId, Optional<uint32_t> invokeId) override;
Protocols::InteractionModel::Status HandleDisableActionWithDuration(uint16_t actionId, uint32_t duration,
Optional<uint32_t> invokeId) override;
};
} // namespace Actions
} // namespace Clusters
} // namespace app
} // namespace chip
131 changes: 131 additions & 0 deletions examples/bridge-app/bridge-common/src/bridged-actions-stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
*
* Copyright (c) 2024 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 <bridged-actions-stub.h>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::Actions;
using namespace chip::app::Clusters::Actions::Attributes;
using namespace chip::Protocols::InteractionModel;

CHIP_ERROR ActionsDelegateImpl::ReadActionAtIndex(uint16_t index, ActionStructStorage & action)
{
if (index >= kActionList.size())
{
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
}
action = kActionList[index];
return CHIP_NO_ERROR;
}

CHIP_ERROR ActionsDelegateImpl::ReadEndpointListAtIndex(uint16_t index, EndpointListStorage & epList)
{
if (index >= kEndpointList.size())
{
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
}
epList = kEndpointList[index];
return CHIP_NO_ERROR;
}

bool ActionsDelegateImpl::HaveActionWithId(uint16_t actionId, uint16_t & actionIndex)
{
for (size_t i = 0; i < kEndpointList.size(); i++)
{
if (kActionList[i].actionID == actionId)
{
actionIndex = (uint16_t) i;
return true;
}
}
return false;
}

Status ActionsDelegateImpl::HandleInstantAction(uint16_t actionId, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandleInstantActionWithTransition(uint16_t actionId, uint16_t transitionTime,
Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandleStartAction(uint16_t actionId, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandleStartActionWithDuration(uint16_t actionId, uint32_t duration, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandleStopAction(uint16_t actionId, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandlePauseAction(uint16_t actionId, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandlePauseActionWithDuration(uint16_t actionId, uint32_t duration, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandleResumeAction(uint16_t actionId, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandleEnableAction(uint16_t actionId, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandleEnableActionWithDuration(uint16_t actionId, uint32_t duration, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandleDisableAction(uint16_t actionId, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}

Status ActionsDelegateImpl::HandleDisableActionWithDuration(uint16_t actionId, uint32_t duration, Optional<uint32_t> invokeId)
{
// Not implemented
return Status::NotFound;
}
4 changes: 3 additions & 1 deletion examples/bridge-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ get_filename_component(APP_COMMON_GEN_DIR ${CHIP_ROOT}/zzz_generated/app-common/
idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_CURRENT_LIST_DIR}/include"
"${CHIP_ROOT}/examples/providers"
"${CHIP_ROOT}/examples/bridge-app/bridge-common/include"
SRC_DIRS
"${CMAKE_CURRENT_LIST_DIR}"
"${APP_COMMON_GEN_DIR}/attributes"
"${APP_COMMON_GEN_DIR}"
"${CHIP_ROOT}/examples/platform/esp32/common"
"${CHIP_ROOT}/examples/providers")
"${CHIP_ROOT}/examples/providers"
"${CHIP_ROOT}/examples/bridge-app/bridge-common/src")

include(${CHIP_ROOT}/src/app/chip_data_model.cmake)
chip_configure_data_model(${COMPONENT_LIB}
Expand Down
78 changes: 0 additions & 78 deletions examples/bridge-app/esp32/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
* limitations under the License.
*/

#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/AttributeAccessInterfaceRegistry.h>
#include <app/util/attribute-storage.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

Expand All @@ -43,75 +37,3 @@ void AppDeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Clus
clusterId, endpointId, attributeId);
ESP_LOGI(TAG, "Current free heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT));
}

namespace {

class ActionsAttrAccess : public AttributeAccessInterface
{
public:
// Register for the Actions cluster on all endpoints.
ActionsAttrAccess() : AttributeAccessInterface(Optional<EndpointId>::Missing(), Actions::Id) {}

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

private:
static constexpr uint16_t ClusterRevision = 1;

CHIP_ERROR ReadActionListAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadEndpointListAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadClusterRevision(EndpointId endpoint, AttributeValueEncoder & aEncoder);
};

constexpr uint16_t ActionsAttrAccess::ClusterRevision;

CHIP_ERROR ActionsAttrAccess::ReadActionListAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
// Just return an empty list
return aEncoder.EncodeEmptyList();
}

CHIP_ERROR ActionsAttrAccess::ReadEndpointListAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
// Just return an empty list
return aEncoder.EncodeEmptyList();
}

CHIP_ERROR ActionsAttrAccess::ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
static const char SetupUrl[] = "https://example.com";
return aEncoder.Encode(chip::CharSpan::fromCharString(SetupUrl));
}

CHIP_ERROR ActionsAttrAccess::ReadClusterRevision(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
return aEncoder.Encode(ClusterRevision);
}

ActionsAttrAccess gAttrAccess;

CHIP_ERROR ActionsAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
VerifyOrDie(aPath.mClusterId == Actions::Id);

switch (aPath.mAttributeId)
{
case ActionList::Id:
return ReadActionListAttribute(aPath.mEndpointId, aEncoder);
case EndpointLists::Id:
return ReadEndpointListAttribute(aPath.mEndpointId, aEncoder);
case SetupURL::Id:
return ReadSetupUrlAttribute(aPath.mEndpointId, aEncoder);
case ClusterRevision::Id:
return ReadClusterRevision(aPath.mEndpointId, aEncoder);
default:
break;
}
return CHIP_NO_ERROR;
}
} // anonymous namespace

void MatterActionsPluginServerInitCallback(void)
{
AttributeAccessInterfaceRegistry::Instance().Register(&gAttrAccess);
}
Loading
Loading