Skip to content

Commit

Permalink
Stop using the external attribute callback in lighting-app.
Browse files Browse the repository at this point in the history
Instead handle ethernet network diagnostics via an attribute interceptor.
  • Loading branch information
bzbarsky-apple committed Sep 14, 2021
1 parent 68c5c78 commit 6216258
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 100 deletions.
99 changes: 0 additions & 99 deletions examples/lighting-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include <platform/CHIPDeviceLayer.h>
#include <platform/PlatformManager.h>

#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/cluster-id.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/chip-zcl-zpro-codec.h>
Expand Down Expand Up @@ -89,103 +87,6 @@ void emberAfOnOffClusterInitCallback(EndpointId endpoint)
// TODO: implement any additional Cluster Server init actions
}

EmberAfStatus HandleReadEthernetNetworkDiagnosticsAttribute(chip::AttributeId attributeId, uint8_t * buffer, uint16_t maxReadLength)
{
EmberAfStatus ret = EMBER_ZCL_STATUS_FAILURE;

switch (attributeId)
{
case ZCL_PACKET_RX_COUNT_ATTRIBUTE_ID:
if (maxReadLength == sizeof(uint64_t))
{
uint64_t packetRxCount;

if (ConnectivityMgr().GetEthPacketRxCount(packetRxCount) == CHIP_NO_ERROR)
{
memcpy(buffer, &packetRxCount, maxReadLength);
ret = EMBER_ZCL_STATUS_SUCCESS;
}
}
break;
case ZCL_PACKET_TX_COUNT_ATTRIBUTE_ID:
if (maxReadLength == sizeof(uint64_t))
{
uint64_t packetTxCount;

if (ConnectivityMgr().GetEthPacketTxCount(packetTxCount) == CHIP_NO_ERROR)
{
memcpy(buffer, &packetTxCount, maxReadLength);
ret = EMBER_ZCL_STATUS_SUCCESS;
}
}
break;
case ZCL_TX_ERR_COUNT_ATTRIBUTE_ID:
if (maxReadLength == sizeof(uint64_t))
{
uint64_t txErrCount;

if (ConnectivityMgr().GetEthTxErrCount(txErrCount) == CHIP_NO_ERROR)
{
memcpy(buffer, &txErrCount, maxReadLength);
ret = EMBER_ZCL_STATUS_SUCCESS;
}
}
break;
case ZCL_COLLISION_COUNT_ATTRIBUTE_ID:
if (maxReadLength == sizeof(uint64_t))
{
uint64_t collisionCount;

if (ConnectivityMgr().GetEthCollisionCount(collisionCount) == CHIP_NO_ERROR)
{
memcpy(buffer, &collisionCount, maxReadLength);
ret = EMBER_ZCL_STATUS_SUCCESS;
}
}
break;
case ZCL_ETHERNET_OVERRUN_COUNT_ATTRIBUTE_ID:
if (maxReadLength == sizeof(uint64_t))
{
uint64_t overrunCount;

if (ConnectivityMgr().GetEthOverrunCount(overrunCount) == CHIP_NO_ERROR)
{
memcpy(buffer, &overrunCount, maxReadLength);
ret = EMBER_ZCL_STATUS_SUCCESS;
}
}
break;
default:
ChipLogProgress(Zcl, "Unhandled attribute ID: %d", attributeId);
break;
}

return ret;
}

EmberAfStatus emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId,
EmberAfAttributeMetadata * attributeMetadata, uint16_t manufacturerCode,
uint8_t * buffer, uint16_t maxReadLength, int32_t index)
{
EmberAfStatus ret = EMBER_ZCL_STATUS_FAILURE;

ChipLogProgress(Zcl,
"emberAfExternalAttributeReadCallback - Cluster ID: '0x%04x', EndPoint ID: '0x%02x', Attribute ID: '0x%04x'",
clusterId, endpoint, attributeMetadata->attributeId);

switch (clusterId)
{
case ZCL_ETHERNET_NETWORK_DIAGNOSTICS_CLUSTER_ID:
ret = HandleReadEthernetNetworkDiagnosticsAttribute(attributeMetadata->attributeId, buffer, maxReadLength);
break;
default:
ChipLogError(Zcl, "Unhandled cluster ID: %d", clusterId);
break;
}

return ret;
}

int main(int argc, char * argv[])
{
if (ChipLinuxAppInit(argc, argv) != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
*/

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/AttributeAccessInterceptor.h>
#include <app/CommandHandler.h>
#include <app/MessageDef/AttributeDataElement.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/core/Optional.h>
#include <platform/ConnectivityManager.h>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::EthernetNetworkDiagnostics::Attributes;
using chip::DeviceLayer::ConnectivityManager;

namespace {

Expand All @@ -52,6 +57,8 @@ class EthernetDiagosticsAttrInterceptor : public AttributeAccessInterceptor

private:
static bool sIsRegistered;

CHIP_ERROR ReadDataIfSupported(CHIP_ERROR (ConnectivityManager::*getter)(uint64_t &), TLV::TLVWriter * aWriter);
};

EthernetDiagosticsAttrInterceptor gAttrInterceptor;
Expand All @@ -60,10 +67,54 @@ bool EthernetDiagosticsAttrInterceptor::sIsRegistered = false;
CHIP_ERROR EthernetDiagosticsAttrInterceptor::ReadAttributeData(ClusterInfo & aClusterInfo, TLV::TLVWriter * aWriter,
bool * aDataRead)
{
*aDataRead = false;
if (aClusterInfo.mClusterId != EthernetNetworkDiagnostics::Id)
{
// We shouldn't have been called at all.
return CHIP_ERROR_INVALID_ARGUMENT;
}

*aDataRead = true;
switch (aClusterInfo.mFieldId)
{
case Ids::PacketRxCount: {
return ReadDataIfSupported(&ConnectivityManager::GetEthPacketRxCount, aWriter);
}
case Ids::PacketTxCount: {
return ReadDataIfSupported(&ConnectivityManager::GetEthPacketTxCount, aWriter);
}
case Ids::TxErrCount: {
return ReadDataIfSupported(&ConnectivityManager::GetEthTxErrCount, aWriter);
}
case Ids::CollisionCount: {
return ReadDataIfSupported(&ConnectivityManager::GetEthCollisionCount, aWriter);
}
case Ids::OverrunCount: {
return ReadDataIfSupported(&ConnectivityManager::GetEthOverrunCount, aWriter);
}
default: {
*aDataRead = false;
break;
}
}
return CHIP_NO_ERROR;
}

CHIP_ERROR EthernetDiagosticsAttrInterceptor::ReadDataIfSupported(CHIP_ERROR (ConnectivityManager::*getter)(uint64_t &),
TLV::TLVWriter * aWriter)
{
uint64_t data;
CHIP_ERROR err = (DeviceLayer::ConnectivityMgr().*getter)(data);
if (err == CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE)
{
data = 0;
}
else if (err != CHIP_NO_ERROR)
{
return err;
}

return aWriter->Put(TLV::ContextTag(AttributeDataElement::kCsTag_Data), data);
}
} // anonymous namespace

bool emberAfEthernetNetworkDiagnosticsClusterResetCountsCallback(EndpointId endpoint, app::CommandHandler * commandObj)
Expand Down

0 comments on commit 6216258

Please sign in to comment.