Skip to content

Commit

Permalink
[Fabric-Bridge] Refactor platform independent files out of linux fold…
Browse files Browse the repository at this point in the history
…er (project-chip#34327)

* Refactor platform independent files out of linux folder

* Address review comment
  • Loading branch information
yufengwangca authored Jul 15, 2024
1 parent 024b09b commit 4ece978
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 75 deletions.
25 changes: 22 additions & 3 deletions examples/fabric-bridge-app/fabric-bridge-common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,32 @@
# limitations under the License.

import("//build_overrides/chip.gni")

import("${chip_root}/src/app/chip_data_model.gni")

config("config") {
include_dirs = [ "include" ]
}

chip_data_model("fabric-bridge-common") {
zap_file = "fabric-bridge-app.zap"

is_server = true

cflags = [ "-DDYNAMIC_ENDPOINT_COUNT=16" ]
}

source_set("fabric-bridge-lib") {
public_configs = [ ":config" ]

sources = [
"include/CHIPProjectAppConfig.h",
"include/Device.h",
"include/DeviceManager.h",
"src/Device.cpp",
"src/DeviceManager.cpp",
"src/ZCLCallbacks.cpp",
]

deps = [
"${chip_root}/examples/fabric-bridge-app/fabric-bridge-common",
"${chip_root}/src/lib",
]
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
*
* 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 "DeviceManager.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 <lib/support/ZclString.h>

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

#define ZCL_DESCRIPTOR_CLUSTER_REVISION (1u)
#define ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_CLUSTER_REVISION (2u)
#define ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_FEATURE_MAP (0u)

// External attribute read callback function
Protocols::InteractionModel::Status emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata,
uint8_t * buffer, uint16_t maxReadLength)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);
AttributeId attributeId = attributeMetadata->attributeId;

Device * dev = DeviceMgr().GetDevice(endpointIndex);
if (dev != nullptr && clusterId == app::Clusters::BridgedDeviceBasicInformation::Id)
{
using namespace app::Clusters::BridgedDeviceBasicInformation::Attributes;
ChipLogProgress(NotSpecified, "HandleReadBridgedDeviceBasicAttribute: attrId=%d, maxReadLength=%d", attributeId,
maxReadLength);

if ((attributeId == Reachable::Id) && (maxReadLength == 1))
{
*buffer = dev->IsReachable() ? 1 : 0;
}
else if ((attributeId == NodeLabel::Id) && (maxReadLength == 32))
{
MutableByteSpan zclNameSpan(buffer, maxReadLength);
MakeZclCharString(zclNameSpan, dev->GetName());
}
else if ((attributeId == ClusterRevision::Id) && (maxReadLength == 2))
{
uint16_t rev = ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_CLUSTER_REVISION;
memcpy(buffer, &rev, sizeof(rev));
}
else if ((attributeId == FeatureMap::Id) && (maxReadLength == 4))
{
uint32_t featureMap = ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_FEATURE_MAP;
memcpy(buffer, &featureMap, sizeof(featureMap));
}
else
{
return Protocols::InteractionModel::Status::Failure;
}
return Protocols::InteractionModel::Status::Success;
}

return Protocols::InteractionModel::Status::Failure;
}

// External attribute write callback function
Protocols::InteractionModel::Status emberAfExternalAttributeWriteCallback(EndpointId endpoint, ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata,
uint8_t * buffer)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);
Protocols::InteractionModel::Status ret = Protocols::InteractionModel::Status::Failure;

Device * dev = DeviceMgr().GetDevice(endpointIndex);
if (dev != nullptr && dev->IsReachable())
{
ChipLogProgress(NotSpecified, "emberAfExternalAttributeWriteCallback: ep=%d, clusterId=%d", endpoint, clusterId);
ret = Protocols::InteractionModel::Status::Success;
}

return ret;
}
5 changes: 1 addition & 4 deletions examples/fabric-bridge-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ if (bridge_enable_pw_rpc) {
executable("fabric-bridge-app") {
sources = [
"${chip_root}/examples/fabric-bridge-app/fabric-bridge-common/include/CHIPProjectAppConfig.h",
"Device.cpp",
"DeviceManager.cpp",
"include/Device.h",
"include/DeviceManager.h",
"main.cpp",
]

deps = [
"${chip_root}/examples/fabric-bridge-app/fabric-bridge-common",
"${chip_root}/examples/fabric-bridge-app/fabric-bridge-common:fabric-bridge-lib",
"${chip_root}/examples/platform/linux:app-main",
"${chip_root}/src/lib",
]
Expand Down
74 changes: 6 additions & 68 deletions examples/fabric-bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "DeviceManager.h"

#include <app/AttributeAccessInterfaceRegistry.h>
#include <lib/support/ZclString.h>

#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
#include "RpcClient.h"
Expand All @@ -40,10 +39,6 @@ using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::AdministratorCommissioning;

#define ZCL_DESCRIPTOR_CLUSTER_REVISION (1u)
#define ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_CLUSTER_REVISION (2u)
#define ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_FEATURE_MAP (0u)

namespace {

constexpr uint16_t kPollIntervalMs = 100;
Expand Down Expand Up @@ -158,6 +153,8 @@ AdministratorCommissioningCommandHandler gAdministratorCommissioningCommandHandl

void ApplicationInit()
{
ChipLogDetail(NotSpecified, "Fabric-Bridge: ApplicationInit()");

InteractionModelEngine::GetInstance()->RegisterCommandHandler(&gAdministratorCommissioningCommandHandler);

#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
Expand All @@ -172,7 +169,10 @@ void ApplicationInit()
DeviceMgr().Init();
}

void ApplicationShutdown() {}
void ApplicationShutdown()
{
ChipLogDetail(NotSpecified, "Fabric-Bridge: ApplicationShutdown()");
}

int main(int argc, char * argv[])
{
Expand All @@ -185,65 +185,3 @@ int main(int argc, char * argv[])

return 0;
}

// External attribute read callback function
Protocols::InteractionModel::Status emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata,
uint8_t * buffer, uint16_t maxReadLength)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);
AttributeId attributeId = attributeMetadata->attributeId;

Device * dev = DeviceMgr().GetDevice(endpointIndex);
if (dev != nullptr && clusterId == app::Clusters::BridgedDeviceBasicInformation::Id)
{
using namespace app::Clusters::BridgedDeviceBasicInformation::Attributes;
ChipLogProgress(NotSpecified, "HandleReadBridgedDeviceBasicAttribute: attrId=%d, maxReadLength=%d", attributeId,
maxReadLength);

if ((attributeId == Reachable::Id) && (maxReadLength == 1))
{
*buffer = dev->IsReachable() ? 1 : 0;
}
else if ((attributeId == NodeLabel::Id) && (maxReadLength == 32))
{
MutableByteSpan zclNameSpan(buffer, maxReadLength);
MakeZclCharString(zclNameSpan, dev->GetName());
}
else if ((attributeId == ClusterRevision::Id) && (maxReadLength == 2))
{
uint16_t rev = ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_CLUSTER_REVISION;
memcpy(buffer, &rev, sizeof(rev));
}
else if ((attributeId == FeatureMap::Id) && (maxReadLength == 4))
{
uint32_t featureMap = ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_FEATURE_MAP;
memcpy(buffer, &featureMap, sizeof(featureMap));
}
else
{
return Protocols::InteractionModel::Status::Failure;
}
return Protocols::InteractionModel::Status::Success;
}

return Protocols::InteractionModel::Status::Failure;
}

// External attribute write callback function
Protocols::InteractionModel::Status emberAfExternalAttributeWriteCallback(EndpointId endpoint, ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata,
uint8_t * buffer)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);
Protocols::InteractionModel::Status ret = Protocols::InteractionModel::Status::Failure;

Device * dev = DeviceMgr().GetDevice(endpointIndex);
if (dev != nullptr && dev->IsReachable())
{
ChipLogProgress(NotSpecified, "emberAfExternalAttributeWriteCallback: ep=%d, clusterId=%d", endpoint, clusterId);
ret = Protocols::InteractionModel::Status::Success;
}

return ret;
}

0 comments on commit 4ece978

Please sign in to comment.