[cli] add hex string payload for udp send (#4241)

This commit is contained in:
Rongli Sun
2019-10-25 01:33:08 +08:00
committed by Jonathan Hui
parent ed8c9f18df
commit eac27c0f45
6 changed files with 126 additions and 40 deletions
+13 -3
View File
@@ -261,16 +261,20 @@ Interpreter::Interpreter(Instance *aInstance)
#endif // OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
}
int Interpreter::Hex2Bin(const char *aHex, uint8_t *aBin, uint16_t aBinLength)
int Interpreter::Hex2Bin(const char *aHex, uint8_t *aBin, uint16_t aBinLength, bool aAllowTruncate)
{
size_t hexLength = strlen(aHex);
const char *hexEnd = aHex + hexLength;
uint8_t * cur = aBin;
uint8_t numChars = hexLength & 1;
uint8_t byte = 0;
int len = 0;
int rval;
VerifyOrExit((hexLength + 1) / 2 <= aBinLength, rval = -1);
if (!aAllowTruncate)
{
VerifyOrExit((hexLength + 1) / 2 <= aBinLength, rval = -1);
}
while (aHex < hexEnd)
{
@@ -299,6 +303,12 @@ int Interpreter::Hex2Bin(const char *aHex, uint8_t *aBin, uint16_t aBinLength)
numChars = 0;
*cur++ = byte;
byte = 0;
len++;
if (len == aBinLength)
{
ExitNow(rval = len);
}
}
else
{
@@ -306,7 +316,7 @@ int Interpreter::Hex2Bin(const char *aHex, uint8_t *aBin, uint16_t aBinLength)
}
}
rval = static_cast<int>(cur - aBin);
rval = len;
exit:
return rval;