Skip to content

Commit

Permalink
Add a YAML test for reading/writing a struct-typed attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Jan 14, 2022
1 parent 4c27e98 commit 6768c96
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/app/clusters/test-cluster-server/test-cluster-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,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 @@ -91,6 +94,7 @@ uint8_t gListUint8Data[kAttributeListLength];
OctetStringData gListOctetStringData[kAttributeListLength];
OctetStringData gListOperationalCert[kAttributeListLength];
Structs::TestListStructOctet::Type listStructOctetStringData[kAttributeListLength];
OctetStringData gStructAttributeByteSpanData;
Structs::SimpleStruct::Type gStructAttributeValue = { 0, false, SimpleEnum::kValueA,
ByteSpan(), CharSpan(), BitFlags<SimpleBitmap>(),
0, 0 };
Expand Down Expand Up @@ -217,8 +221,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 @@ -323,9 +326,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 @@ -403,7 +405,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;
}

} // namespace
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 6768c96

Please sign in to comment.