Skip to content

Commit

Permalink
Add a YAML test for reading/writing a struct-typed attribute. (#13558)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Jan 27, 2024
1 parent 1e18f94 commit 1507266
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 27 deletions.
48 changes: 22 additions & 26 deletions src/app/clusters/test-cluster-server/test-cluster-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ class OctetStringData
{
public:
uint8_t * Data() { return mDataBuf; }
const uint8_t * Data() const { return mDataBuf; }
size_t Length() const { return mDataLen; }
void SetLength(size_t size) { mDataLen = size; }

ByteSpan AsSpan() const { return ByteSpan(Data(), Length()); }

private:
uint8_t mDataBuf[kAttributeEntryLength];
size_t mDataLen = 0;
Expand Down Expand Up @@ -93,6 +96,7 @@ uint8_t gListUint8Data[kAttributeListLength];
OctetStringData gListOctetStringData[kAttributeListLength];
OctetStringData gListOperationalCert[kAttributeListLength];
Structs::TestListStructOctet::Type listStructOctetStringData[kAttributeListLength];
OctetStringData gStructAttributeByteSpanData;
Structs::SimpleStruct::Type gStructAttributeValue;
NullableStruct::TypeInfo::Type gNullableStructAttributeValue;

Expand Down Expand Up @@ -220,8 +224,7 @@ CHIP_ERROR TestAttrAccess::ReadListOctetStringAttribute(AttributeValueEncoder &
return aEncoder.EncodeList([](const auto & encoder) -> CHIP_ERROR {
for (uint8_t index = 0; index < kAttributeListLength; index++)
{
ByteSpan span(gListOctetStringData[index].Data(), gListOctetStringData[index].Length());
ReturnErrorOnFailure(encoder.Encode(span));
ReturnErrorOnFailure(encoder.Encode(gListOctetStringData[index].AsSpan()));
}
return CHIP_NO_ERROR;
});
Expand Down Expand Up @@ -326,9 +329,8 @@ CHIP_ERROR TestAttrAccess::WriteListStructOctetStringAttribute(AttributeValueDec
memcpy(gListOperationalCert[index].Data(), entry.operationalCert.data(), entry.operationalCert.size());
gListOperationalCert[index].SetLength(entry.operationalCert.size());

listStructOctetStringData[index].fabricIndex = entry.fabricIndex;
listStructOctetStringData[index].operationalCert =
ByteSpan(gListOperationalCert[index].Data(), gListOperationalCert[index].Length());
listStructOctetStringData[index].fabricIndex = entry.fabricIndex;
listStructOctetStringData[index].operationalCert = gListOperationalCert[index].AsSpan();
index++;
}

Expand Down Expand Up @@ -406,7 +408,19 @@ CHIP_ERROR TestAttrAccess::ReadStructAttribute(AttributeValueEncoder & aEncoder)

CHIP_ERROR TestAttrAccess::WriteStructAttribute(AttributeValueDecoder & aDecoder)
{
return aDecoder.Decode(gStructAttributeValue);
// We don't support a nonempty charspan here for now.
Structs::SimpleStruct::DecodableType temp;
ReturnErrorOnFailure(aDecoder.Decode(temp));

VerifyOrReturnError(temp.e.empty(), CHIP_ERROR_BUFFER_TOO_SMALL);
const size_t octet_size = temp.d.size();
VerifyOrReturnError(octet_size <= kAttributeEntryLength, CHIP_ERROR_BUFFER_TOO_SMALL);

gStructAttributeValue = temp;
memcpy(gStructAttributeByteSpanData.Data(), temp.d.data(), octet_size);
gStructAttributeByteSpanData.SetLength(octet_size);
gStructAttributeValue.d = gStructAttributeByteSpanData.AsSpan();
return CHIP_NO_ERROR;
}

CHIP_ERROR TestAttrAccess::ReadListFabricScopedAttribute(AttributeValueEncoder & aEncoder)
Expand Down Expand Up @@ -445,29 +459,11 @@ bool emberAfTestClusterClusterTestCallback(app::CommandHandler *, const app::Con
}
gSimpleEnumCount = 0;

gStructAttributeValue.a = 0;
gStructAttributeValue.b = false;
gStructAttributeValue.c = SimpleEnum::kValueA;
gStructAttributeValue.d = ByteSpan();
gStructAttributeValue.e = CharSpan();
gStructAttributeValue.f = BitFlags<SimpleBitmap>();
gStructAttributeValue.g = 0;
gStructAttributeValue.h = 0;
gStructAttributeValue = Structs::SimpleStruct::Type();

gNullableStructAttributeValue.SetNull();

gNullablesAndOptionalsStruct.nullableInt.SetNull();
gNullablesAndOptionalsStruct.optionalInt = NullOptional;
gNullablesAndOptionalsStruct.nullableOptionalInt = NullOptional;
gNullablesAndOptionalsStruct.nullableString.SetNull();
gNullablesAndOptionalsStruct.optionalString = NullOptional;
gNullablesAndOptionalsStruct.nullableOptionalString = NullOptional;
gNullablesAndOptionalsStruct.nullableStruct.SetNull();
gNullablesAndOptionalsStruct.optionalStruct = NullOptional;
gNullablesAndOptionalsStruct.nullableOptionalStruct = NullOptional;
gNullablesAndOptionalsStruct.nullableList.SetNull();
gNullablesAndOptionalsStruct.optionalList = NullOptional;
gNullablesAndOptionalsStruct.nullableOptionalList = NullOptional;
gNullablesAndOptionalsStruct = Structs::NullablesAndOptionalsStruct::Type();

emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
return true;
Expand Down
33 changes: 33 additions & 0 deletions src/app/tests/suites/TestClusterComplexTypes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,36 @@ tests:
attribute: "boolean"
arguments:
value: false

# Struct-typed attribute
- label: "Write struct-typed attribute"
command: "writeAttribute"
attribute: "struct_attr"
arguments:
value:
{
a: 5,
b: true,
c: 2,
d: "abc",
e: "",
f: 17,
g: 1.5,
h: 3.14159265358979,
}

- label: "Read struct-typed attribute"
command: "readAttribute"
attribute: "struct_attr"
response:
value:
{
a: 5,
b: true,
c: 2,
d: "abc",
e: "",
f: 17,
g: 1.5,
h: 3.14159265358979,
}
81 changes: 80 additions & 1 deletion zzz_generated/chip-tool/zap-generated/test/Commands.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1507266

Please sign in to comment.