[meshcop] simplify processing of Commissioner ID TLV (#9543)

This commit updates CommissionerIdTlv to be defined as `StringTlvInfo`
(a TLV with a UTF-8 string value with a specified maximum length). This
allows us to use `Tlv` helper methods to `Find` and `Append` this
TLV, simplifying the code.

This commit also adds a helper `StringCopy()` method that copies a
C string into a given target string buffer array if it fits in the array.
This method can also optionally perform an encoding check on the string,
such as a UTF-8 encoding check. This helper method is used to simplify
setting different strings, such as Commissioner ID, Provisioning URL,
Vendor Name, etc.
This commit is contained in:
Abtin Keshavarzian
2023-10-18 16:11:49 -07:00
committed by GitHub
parent 2457ba7c18
commit a69c2db333
11 changed files with 139 additions and 142 deletions
+31
View File
@@ -366,6 +366,36 @@ void TestStringParseUint8(void)
printf("\n\n -- PASS\n");
}
void TestStringCopy(void)
{
char buffer[10];
char smallBuffer[1];
printf("\nTest 11: StringCopy() function\n");
SuccessOrQuit(StringCopy(buffer, "foo", kStringCheckUtf8Encoding));
VerifyOrQuit(StringMatch(buffer, "foo"));
SuccessOrQuit(StringCopy(buffer, nullptr, kStringCheckUtf8Encoding));
VerifyOrQuit(StringMatch(buffer, ""));
SuccessOrQuit(StringCopy(buffer, "", kStringCheckUtf8Encoding));
VerifyOrQuit(StringMatch(buffer, ""));
SuccessOrQuit(StringCopy(buffer, "123456789", kStringCheckUtf8Encoding));
VerifyOrQuit(StringMatch(buffer, "123456789"));
VerifyOrQuit(StringCopy(buffer, "1234567890") == kErrorInvalidArgs);
VerifyOrQuit(StringCopy(buffer, "1234567890abcdef") == kErrorInvalidArgs);
SuccessOrQuit(StringCopy(smallBuffer, "", kStringCheckUtf8Encoding));
VerifyOrQuit(StringMatch(smallBuffer, ""));
VerifyOrQuit(StringCopy(smallBuffer, "a") == kErrorInvalidArgs);
printf(" -- PASS\n");
}
// gcc-4 does not support constexpr function
#if __GNUC__ > 4
static_assert(ot::AreStringsInOrder("a", "b"), "AreStringsInOrder() failed");
@@ -389,6 +419,7 @@ int main(void)
ot::TestStringMatch();
ot::TestStringToLowercase();
ot::TestStringParseUint8();
ot::TestStringCopy();
printf("\nAll tests passed.\n");
return 0;
}