Skip to content

Commit

Permalink
Add Label to FabricDescriptorStruct and hook up logic with UpdateFabr…
Browse files Browse the repository at this point in the history
…icLabel (#6670)

* Adds Label to FabricDescriptor struct + regens files
Adds fabric label to AdminPairingInfo with persistence support
Hooks up UpdateFabricLabel to SetFabricLabel and writes the fabric label into the attribute list upon changes.

How was this tested? (at least one bullet point required)
- Added UI in chiptool ios to test this and tested that the label can be modifiled, nulled out and persists after m5stack reboots

* regen files

* Restyled by whitespace

* Restyled by clang-format

* switching to -std=gnu++14

* Restyled by clang-format

* switch from 14 to 17

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
shana-apple and restyled-commits committed Jun 3, 2021
1 parent 575f99d commit dbdc8bf
Show file tree
Hide file tree
Showing 23 changed files with 176 additions and 39 deletions.
3 changes: 3 additions & 0 deletions config/nrfconnect/app/enable-gnu-std.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(gnu17 INTERFACE)
target_compile_options(gnu17 INTERFACE -std=gnu++17 -D_SYS__PTHREADTYPES_H_)
target_link_libraries(app PRIVATE gnu17)
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
{
case 0x0001: // fabrics list
{
entryLength = 18;
entryLength = 52;
if (((index - 1) * entryLength) > (am->size - entryLength))
{
ChipLogError(Zcl, "Index %l is invalid.", index);
Expand All @@ -434,7 +434,15 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
copyListMember(write ? dest : (uint8_t *) &entry->VendorId, write ? (uint8_t *) &entry->VendorId : src, write,
&entryOffset, sizeof(entry->VendorId)); // INT16U
copyListMember(write ? dest : (uint8_t *) &entry->NodeId, write ? (uint8_t *) &entry->NodeId : src, write, &entryOffset,
sizeof(entry->NodeId)); // NODE_ID
sizeof(entry->NodeId)); // NODE_ID
chip::ByteSpan * LabelSpan = &entry->Label; // OCTET_STRING
if (CHIP_NO_ERROR !=
(write ? WriteByteSpan(dest + entryOffset, 34, LabelSpan) : ReadByteSpan(src + entryOffset, 34, LabelSpan)))
{
ChipLogError(Zcl, "Index %l is invalid. Not enough remaining space", index);
return 0;
}
entryOffset = static_cast<uint16_t>(entryOffset + 34);
break;
}
}
Expand Down Expand Up @@ -858,7 +866,7 @@ uint16_t emberAfAttributeValueListSize(ClusterId clusterId, AttributeId attribut
{
case 0x0001: // fabrics list
// Struct _FabricDescriptor
entryLength = 18;
entryLength = 52;
break;
}
break;
Expand Down
1 change: 1 addition & 0 deletions examples/chip-tool/commands/clusters/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ static void OnOperationalCredentialsFabricsListListAttributeResponse(void * cont
ChipLogProgress(chipTool, " FabricId: %" PRIu64 "", entries[i].FabricId);
ChipLogProgress(chipTool, " VendorId: %" PRIu16 "", entries[i].VendorId);
ChipLogProgress(chipTool, " NodeId: %" PRIu64 "", entries[i].NodeId);
ChipLogProgress(Zcl, " Label: %zu", entries[i].Label.size());
}

ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
Expand Down
14 changes: 11 additions & 3 deletions examples/lighting-app/lighting-common/gen/attribute-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
{
case 0x0001: // fabrics list
{
entryLength = 18;
entryLength = 52;
if (((index - 1) * entryLength) > (am->size - entryLength))
{
ChipLogError(Zcl, "Index %l is invalid.", index);
Expand All @@ -140,7 +140,15 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
copyListMember(write ? dest : (uint8_t *) &entry->VendorId, write ? (uint8_t *) &entry->VendorId : src, write,
&entryOffset, sizeof(entry->VendorId)); // INT16U
copyListMember(write ? dest : (uint8_t *) &entry->NodeId, write ? (uint8_t *) &entry->NodeId : src, write, &entryOffset,
sizeof(entry->NodeId)); // NODE_ID
sizeof(entry->NodeId)); // NODE_ID
chip::ByteSpan * LabelSpan = &entry->Label; // OCTET_STRING
if (CHIP_NO_ERROR !=
(write ? WriteByteSpan(dest + entryOffset, 34, LabelSpan) : ReadByteSpan(src + entryOffset, 34, LabelSpan)))
{
ChipLogError(Zcl, "Index %l is invalid. Not enough remaining space", index);
return 0;
}
entryOffset = static_cast<uint16_t>(entryOffset + 34);
break;
}
}
Expand Down Expand Up @@ -334,7 +342,7 @@ uint16_t emberAfAttributeValueListSize(ClusterId clusterId, AttributeId attribut
{
case 0x0001: // fabrics list
// Struct _FabricDescriptor
entryLength = 18;
entryLength = 52;
break;
}
break;
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})

project(chip-nrfconnect-lighting-example)

include(${CHIP_ROOT}/config/nrfconnect/app/enable-gnu-std.cmake)

target_compile_options(app PRIVATE -Werror)

target_include_directories(app PRIVATE
Expand Down
14 changes: 11 additions & 3 deletions examples/lock-app/lock-common/gen/attribute-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
{
case 0x0001: // fabrics list
{
entryLength = 18;
entryLength = 52;
if (((index - 1) * entryLength) > (am->size - entryLength))
{
ChipLogError(Zcl, "Index %l is invalid.", index);
Expand All @@ -140,7 +140,15 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
copyListMember(write ? dest : (uint8_t *) &entry->VendorId, write ? (uint8_t *) &entry->VendorId : src, write,
&entryOffset, sizeof(entry->VendorId)); // INT16U
copyListMember(write ? dest : (uint8_t *) &entry->NodeId, write ? (uint8_t *) &entry->NodeId : src, write, &entryOffset,
sizeof(entry->NodeId)); // NODE_ID
sizeof(entry->NodeId)); // NODE_ID
chip::ByteSpan * LabelSpan = &entry->Label; // OCTET_STRING
if (CHIP_NO_ERROR !=
(write ? WriteByteSpan(dest + entryOffset, 34, LabelSpan) : ReadByteSpan(src + entryOffset, 34, LabelSpan)))
{
ChipLogError(Zcl, "Index %l is invalid. Not enough remaining space", index);
return 0;
}
entryOffset = static_cast<uint16_t>(entryOffset + 34);
break;
}
}
Expand Down Expand Up @@ -334,7 +342,7 @@ uint16_t emberAfAttributeValueListSize(ClusterId clusterId, AttributeId attribut
{
case 0x0001: // fabrics list
// Struct _FabricDescriptor
entryLength = 18;
entryLength = 52;
break;
}
break;
Expand Down
2 changes: 2 additions & 0 deletions examples/lock-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ target_compile_options(app PRIVATE -Werror)

project(chip-nrfconnect-lock-example)

include(${CHIP_ROOT}/config/nrfconnect/app/enable-gnu-std.cmake)

target_include_directories(app PRIVATE
main/include
${LOCK_COMMON}
Expand Down
2 changes: 2 additions & 0 deletions examples/pigweed-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})

project(chip-nrf52840-pigweed-example)

include(${CHIP_ROOT}/config/nrfconnect/app/enable-gnu-std.cmake)

target_compile_options(app PRIVATE -Werror)

include(${PIGWEED_ROOT}/pw_build/pigweed.cmake)
Expand Down
2 changes: 2 additions & 0 deletions examples/pump-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ target_compile_options(app PRIVATE -Werror)

project(chip-nrfconnect-pump-example)

include(${CHIP_ROOT}/config/nrfconnect/app/enable-gnu-std.cmake)

target_include_directories(app PRIVATE
main/include
${PUMP_COMMON}
Expand Down
14 changes: 11 additions & 3 deletions examples/pump-app/pump-common/gen/attribute-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
{
case 0x0001: // fabrics list
{
entryLength = 18;
entryLength = 52;
if (((index - 1) * entryLength) > (am->size - entryLength))
{
ChipLogError(Zcl, "Index %l is invalid.", index);
Expand All @@ -140,7 +140,15 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
copyListMember(write ? dest : (uint8_t *) &entry->VendorId, write ? (uint8_t *) &entry->VendorId : src, write,
&entryOffset, sizeof(entry->VendorId)); // INT16U
copyListMember(write ? dest : (uint8_t *) &entry->NodeId, write ? (uint8_t *) &entry->NodeId : src, write, &entryOffset,
sizeof(entry->NodeId)); // NODE_ID
sizeof(entry->NodeId)); // NODE_ID
chip::ByteSpan * LabelSpan = &entry->Label; // OCTET_STRING
if (CHIP_NO_ERROR !=
(write ? WriteByteSpan(dest + entryOffset, 34, LabelSpan) : ReadByteSpan(src + entryOffset, 34, LabelSpan)))
{
ChipLogError(Zcl, "Index %l is invalid. Not enough remaining space", index);
return 0;
}
entryOffset = static_cast<uint16_t>(entryOffset + 34);
break;
}
}
Expand Down Expand Up @@ -334,7 +342,7 @@ uint16_t emberAfAttributeValueListSize(ClusterId clusterId, AttributeId attribut
{
case 0x0001: // fabrics list
// Struct _FabricDescriptor
entryLength = 18;
entryLength = 52;
break;
}
break;
Expand Down
2 changes: 2 additions & 0 deletions examples/pump-controller-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ target_compile_options(app PRIVATE -Werror)

project(chip-nrfconnect-pump-controller-example)

include(${CHIP_ROOT}/config/nrfconnect/app/enable-gnu-std.cmake)

target_include_directories(app PRIVATE
main/include
${PUMPC_COMMON}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
{
case 0x0001: // fabrics list
{
entryLength = 18;
entryLength = 52;
if (((index - 1) * entryLength) > (am->size - entryLength))
{
ChipLogError(Zcl, "Index %l is invalid.", index);
Expand All @@ -140,7 +140,15 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
copyListMember(write ? dest : (uint8_t *) &entry->VendorId, write ? (uint8_t *) &entry->VendorId : src, write,
&entryOffset, sizeof(entry->VendorId)); // INT16U
copyListMember(write ? dest : (uint8_t *) &entry->NodeId, write ? (uint8_t *) &entry->NodeId : src, write, &entryOffset,
sizeof(entry->NodeId)); // NODE_ID
sizeof(entry->NodeId)); // NODE_ID
chip::ByteSpan * LabelSpan = &entry->Label; // OCTET_STRING
if (CHIP_NO_ERROR !=
(write ? WriteByteSpan(dest + entryOffset, 34, LabelSpan) : ReadByteSpan(src + entryOffset, 34, LabelSpan)))
{
ChipLogError(Zcl, "Index %l is invalid. Not enough remaining space", index);
return 0;
}
entryOffset = static_cast<uint16_t>(entryOffset + 34);
break;
}
}
Expand Down Expand Up @@ -334,7 +342,7 @@ uint16_t emberAfAttributeValueListSize(ClusterId clusterId, AttributeId attribut
{
case 0x0001: // fabrics list
// Struct _FabricDescriptor
entryLength = 18;
entryLength = 52;
break;
}
break;
Expand Down
2 changes: 2 additions & 0 deletions examples/shell/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})

project(chip-nrfconnect-shell-example)

include(${CHIP_ROOT}/config/nrfconnect/app/enable-gnu-std.cmake)

target_compile_options(app PRIVATE -Werror)

target_include_directories(app PRIVATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
{
case 0x0001: // fabrics list
{
entryLength = 18;
entryLength = 52;
if (((index - 1) * entryLength) > (am->size - entryLength))
{
ChipLogError(Zcl, "Index %l is invalid.", index);
Expand All @@ -140,7 +140,15 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
copyListMember(write ? dest : (uint8_t *) &entry->VendorId, write ? (uint8_t *) &entry->VendorId : src, write,
&entryOffset, sizeof(entry->VendorId)); // INT16U
copyListMember(write ? dest : (uint8_t *) &entry->NodeId, write ? (uint8_t *) &entry->NodeId : src, write, &entryOffset,
sizeof(entry->NodeId)); // NODE_ID
sizeof(entry->NodeId)); // NODE_ID
chip::ByteSpan * LabelSpan = &entry->Label; // OCTET_STRING
if (CHIP_NO_ERROR !=
(write ? WriteByteSpan(dest + entryOffset, 34, LabelSpan) : ReadByteSpan(src + entryOffset, 34, LabelSpan)))
{
ChipLogError(Zcl, "Index %l is invalid. Not enough remaining space", index);
return 0;
}
entryOffset = static_cast<uint16_t>(entryOffset + 34);
break;
}
}
Expand Down Expand Up @@ -179,7 +187,7 @@ uint16_t emberAfAttributeValueListSize(ClusterId clusterId, AttributeId attribut
{
case 0x0001: // fabrics list
// Struct _FabricDescriptor
entryLength = 18;
entryLength = 52;
break;
}
break;
Expand Down
14 changes: 11 additions & 3 deletions examples/tv-app/tv-common/gen/attribute-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
{
case 0x0001: // fabrics list
{
entryLength = 18;
entryLength = 52;
if (((index - 1) * entryLength) > (am->size - entryLength))
{
ChipLogError(Zcl, "Index %l is invalid.", index);
Expand All @@ -338,7 +338,15 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
copyListMember(write ? dest : (uint8_t *) &entry->VendorId, write ? (uint8_t *) &entry->VendorId : src, write,
&entryOffset, sizeof(entry->VendorId)); // INT16U
copyListMember(write ? dest : (uint8_t *) &entry->NodeId, write ? (uint8_t *) &entry->NodeId : src, write, &entryOffset,
sizeof(entry->NodeId)); // NODE_ID
sizeof(entry->NodeId)); // NODE_ID
chip::ByteSpan * LabelSpan = &entry->Label; // OCTET_STRING
if (CHIP_NO_ERROR !=
(write ? WriteByteSpan(dest + entryOffset, 34, LabelSpan) : ReadByteSpan(src + entryOffset, 34, LabelSpan)))
{
ChipLogError(Zcl, "Index %l is invalid. Not enough remaining space", index);
return 0;
}
entryOffset = static_cast<uint16_t>(entryOffset + 34);
break;
}
}
Expand Down Expand Up @@ -674,7 +682,7 @@ uint16_t emberAfAttributeValueListSize(ClusterId clusterId, AttributeId attribut
{
case 0x0001: // fabrics list
// Struct _FabricDescriptor
entryLength = 18;
entryLength = 52;
break;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
#include <app/server/Server.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/core/CHIPSafeCasts.h>
#include <lib/core/PeerId.h>
#include <platform/CHIPDeviceLayer.h>
#include <string.h>
#include <support/CodeUtils.h>
#include <support/ScopedBuffer.h>
#include <support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -77,14 +79,19 @@ EmberAfStatus writeFabricAttribute(uint8_t * buffer, int32_t index = -1)
index + 1);
}

EmberAfStatus writeFabric(FabricId fabricId, NodeId nodeId, uint16_t vendorId, int32_t index)
EmberAfStatus writeFabric(FabricId fabricId, NodeId nodeId, uint16_t vendorId, const uint8_t * fabricLabel, int32_t index)
{
EmberAfStatus status = EMBER_ZCL_STATUS_SUCCESS;

EmberAfFabricDescriptor fabricDescriptor;
fabricDescriptor.FabricId = fabricId;
fabricDescriptor.NodeId = nodeId;
fabricDescriptor.VendorId = vendorId;
if (fabricLabel != nullptr)
{
size_t lengthToStore = strnlen(Uint8::to_const_char(fabricLabel), kFabricLabelMaxLengthInBytes);
fabricDescriptor.Label = ByteSpan(fabricLabel, lengthToStore);
}

emberAfPrintln(EMBER_AF_PRINT_DEBUG,
"OpCreds: Writing admin into attribute store at index %d: fabricId 0x" ChipLogFormatX64
Expand All @@ -103,9 +110,10 @@ CHIP_ERROR writeAdminsIntoFabricsListAttribute()
uint32_t fabricIndex = 0;
for (auto & pairing : GetGlobalAdminPairingTable())
{
NodeId nodeId = pairing.GetNodeId();
uint64_t fabricId = pairing.GetFabricId();
uint16_t vendorId = pairing.GetVendorId();
NodeId nodeId = pairing.GetNodeId();
uint64_t fabricId = pairing.GetFabricId();
uint16_t vendorId = pairing.GetVendorId();
const uint8_t * fabricLabel = pairing.GetFabricLabel();

// Skip over uninitialized admins
if (nodeId == kUndefinedNodeId || fabricId == kUndefinedFabricId || vendorId == kUndefinedVendorId)
Expand All @@ -116,7 +124,7 @@ CHIP_ERROR writeAdminsIntoFabricsListAttribute()
ChipLogValueX64(fabricId), ChipLogValueX64(nodeId), vendorId);
continue;
}
else if (writeFabric(fabricId, nodeId, vendorId, fabricIndex) != EMBER_ZCL_STATUS_SUCCESS)
else if (writeFabric(fabricId, nodeId, vendorId, fabricLabel, fabricIndex) != EMBER_ZCL_STATUS_SUCCESS)
{
emberAfPrintln(EMBER_AF_PRINT_DEBUG,
"OpCreds: Failed to write admin with fabricId 0x" ChipLogFormatX64 " in fabrics list",
Expand Down Expand Up @@ -292,7 +300,23 @@ bool emberAfOperationalCredentialsClusterUpdateFabricLabelCallback(chip::app::Co
{
emberAfPrintln(EMBER_AF_PRINT_DEBUG, "OpCreds: UpdateFabricLabel");

EmberAfStatus status = EMBER_ZCL_STATUS_FAILURE;
EmberAfStatus status = EMBER_ZCL_STATUS_SUCCESS;
CHIP_ERROR err;

// Fetch current fabric
AdminPairingInfo * admin = retrieveCurrentAdmin();
VerifyOrExit(admin != nullptr, status = EMBER_ZCL_STATUS_FAILURE);

// Set Label on fabric
err = admin->SetFabricLabel(Label);
VerifyOrExit(err == CHIP_NO_ERROR, status = EMBER_ZCL_STATUS_FAILURE);

// Persist updated fabric
err = GetGlobalAdminPairingTable().Store(admin->GetAdminId());
VerifyOrExit(err == CHIP_NO_ERROR, status = EMBER_ZCL_STATUS_FAILURE);

exit:
writeAdminsIntoFabricsListAttribute();
emberAfSendImmediateDefaultResponse(status);
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/app/common/gen/af-structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ typedef struct _FabricDescriptor
chip::FabricId FabricId;
uint16_t VendorId;
chip::NodeId NodeId;
chip::ByteSpan Label;
} EmberAfFabricDescriptor;

// Struct for GpPairingConfigurationGroupList
Expand Down
Loading

0 comments on commit dbdc8bf

Please sign in to comment.