mirror of
https://github.com/espressif/openthread.git
synced 2026-08-29 21:39:54 +00:00
[cli] add hex string payload for udp send (#4241)
This commit is contained in:
+61
-28
@@ -134,35 +134,20 @@ otError UdpExample::ProcessOpen(int argc, char *argv[])
|
||||
|
||||
otError UdpExample::ProcessSend(int argc, char *argv[])
|
||||
{
|
||||
otError error;
|
||||
otError error = OT_ERROR_NONE;
|
||||
otMessageInfo messageInfo;
|
||||
otMessage * message = NULL;
|
||||
int curArg = 0;
|
||||
bool autoGenPayload = false;
|
||||
unsigned long autoGenPayloadLength = 0;
|
||||
otMessage * message = NULL;
|
||||
int curArg = 0;
|
||||
uint16_t payloadLength = 0;
|
||||
PayloadType payloadType = kTypeText;
|
||||
|
||||
memset(&messageInfo, 0, sizeof(messageInfo));
|
||||
|
||||
VerifyOrExit(argc == 1 || argc == 3 || argc == 4, error = OT_ERROR_INVALID_ARGS);
|
||||
VerifyOrExit(argc >= 1 && argc <= 4, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
if (argc == 4)
|
||||
{
|
||||
if (strcmp(argv[curArg++], "-s") == 0)
|
||||
{
|
||||
autoGenPayload = true;
|
||||
error = Interpreter::ParseUnsignedLong(argv[curArg++], autoGenPayloadLength);
|
||||
SuccessOrExit(error);
|
||||
}
|
||||
else
|
||||
{
|
||||
ExitNow(error = OT_ERROR_INVALID_ARGS);
|
||||
}
|
||||
}
|
||||
|
||||
if (argc >= 3)
|
||||
if (argc > 2)
|
||||
{
|
||||
long value;
|
||||
|
||||
error = otIp6AddressFromString(argv[curArg++], &messageInfo.mPeerAddr);
|
||||
SuccessOrExit(error);
|
||||
|
||||
@@ -172,18 +157,66 @@ otError UdpExample::ProcessSend(int argc, char *argv[])
|
||||
messageInfo.mPeerPort = static_cast<uint16_t>(value);
|
||||
}
|
||||
|
||||
if (argc == 2 || argc == 4)
|
||||
{
|
||||
int typePos = curArg++;
|
||||
|
||||
if (strcmp(argv[typePos], "-s") == 0)
|
||||
{
|
||||
unsigned long value;
|
||||
payloadType = kTypeAutoSize;
|
||||
SuccessOrExit(error = Interpreter::ParseUnsignedLong(argv[curArg], value));
|
||||
payloadLength = static_cast<uint16_t>(value);
|
||||
}
|
||||
else if (strcmp(argv[typePos], "-x") == 0)
|
||||
{
|
||||
payloadLength = static_cast<uint16_t>(strlen(argv[curArg]));
|
||||
payloadType = kTypeHexString;
|
||||
}
|
||||
else if (strcmp(argv[typePos], "-t") == 0)
|
||||
{
|
||||
payloadType = kTypeText;
|
||||
}
|
||||
}
|
||||
|
||||
message = otUdpNewMessage(mInterpreter.mInstance, NULL);
|
||||
VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS);
|
||||
|
||||
if (autoGenPayload)
|
||||
switch (payloadType)
|
||||
{
|
||||
error = WriteCharToBuffer(message, static_cast<uint16_t>(autoGenPayloadLength));
|
||||
}
|
||||
else
|
||||
case kTypeText:
|
||||
SuccessOrExit(error = otMessageAppend(message, argv[curArg], static_cast<uint16_t>(strlen(argv[curArg]))));
|
||||
break;
|
||||
case kTypeAutoSize:
|
||||
SuccessOrExit(error = WriteCharToBuffer(message, payloadLength));
|
||||
break;
|
||||
case kTypeHexString:
|
||||
{
|
||||
error = otMessageAppend(message, argv[curArg], static_cast<uint16_t>(strlen(argv[curArg])));
|
||||
uint8_t buf[50];
|
||||
int16_t bufLen;
|
||||
uint16_t conversionLength = 0;
|
||||
const char *hexString = argv[curArg];
|
||||
|
||||
while (payloadLength > 0)
|
||||
{
|
||||
bufLen = static_cast<int16_t>(Interpreter::Hex2Bin(hexString, buf, sizeof(buf), true));
|
||||
|
||||
VerifyOrExit(bufLen > 0, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
conversionLength = static_cast<uint16_t>(bufLen * 2);
|
||||
|
||||
if ((payloadLength & 0x01) != 0)
|
||||
{
|
||||
conversionLength -= 1;
|
||||
}
|
||||
|
||||
hexString += conversionLength;
|
||||
payloadLength -= conversionLength;
|
||||
SuccessOrExit(error = otMessageAppend(message, buf, static_cast<uint16_t>(bufLen)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
SuccessOrExit(error);
|
||||
|
||||
error = otUdpSend(&mSocket, message, &messageInfo);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user