[posix-app] support dry run (#3176)

This commit is contained in:
Yakun Xu
2018-10-17 23:18:26 +02:00
committed by Jonathan Hui
parent 465a46c4c8
commit c711dc9d0c
2 changed files with 67 additions and 28 deletions
+6 -3
View File
@@ -155,7 +155,7 @@ static inline void VerifyOrDie(bool aCondition)
{
if (!aCondition)
{
exit(EXIT_FAILURE);
exit(OT_EXIT_FAILURE);
}
}
@@ -526,7 +526,7 @@ void RadioSpinel::Init(const char *aRadioFile, const char *aRadioConfig)
// not allowed to initialize again.
assert(mSockFd == -1);
VerifyOrExit(stat(aRadioFile, &st) == 0, perror("stat ncp file failed"));
VerifyOrExit(stat(aRadioFile, &st) == 0, perror("stat ncp file failed"); error = OT_ERROR_INVALID_ARGS);
if (S_ISCHR(st.st_mode))
{
@@ -575,7 +575,10 @@ void RadioSpinel::Init(const char *aRadioFile, const char *aRadioConfig)
SuccessOrExit(error = Get(SPINEL_PROP_RADIO_CAPS, SPINEL_DATATYPE_UINT_PACKED_S, &caps));
mRadioCaps = static_cast<otRadioCaps>(caps);
VerifyOrExit((mRadioCaps & kRequiredRadioCaps) == kRequiredRadioCaps, error = OT_ERROR_NOT_CAPABLE);
if ((mRadioCaps & kRequiredRadioCaps) != kRequiredRadioCaps)
{
exit(OT_EXIT_INCOMPATIBLE_RADIO_SPINEL);
}
}
mRxRadioFrame.mPsdu = mRxPsdu;
+61 -25
View File
@@ -36,6 +36,7 @@
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <libgen.h>
#include <stddef.h>
#include <stdint.h>
@@ -50,18 +51,25 @@ uint64_t gNodeId = 0;
extern bool gPlatformPseudoResetWasRequested;
static void PrintUsage(const char *aArg0)
static void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode)
{
fprintf(stderr, "Syntax:\n %s [-s TimeSpeedUpFactor] {NodeId|Device DeviceConfig|Command CommandArgs}\n", aArg0);
fprintf(aStream,
"Syntax:\n"
" %s [Options] NodeId|Device|Command [DeviceConfig|CommandArgs]\n"
"Options:\n"
" -s --time-speed factor Time speed up factor.\n"
" -n --dry-run Just verify if arguments is valid and radio spinel is compatible.\n"
" -h --help Display this usage information.\n",
aProgramName);
exit(aExitCode);
}
void otSysInit(int aArgCount, char *aArgVector[])
{
int i;
uint32_t speedUpFactor = 1;
char * endptr;
const char *radioFile = NULL;
const char *radioConfig = "";
bool isDryRun = false;
const char *radioFile = NULL;
const char *radioConfig = "";
if (gPlatformPseudoResetWasRequested)
{
@@ -69,36 +77,59 @@ void otSysInit(int aArgCount, char *aArgVector[])
return;
}
if (aArgCount < 2)
while (true)
{
PrintUsage(aArgVector[0]);
exit(OT_EXIT_INVALID_ARGUMENTS);
}
int option;
const struct option options[] = {{"dry-run", no_argument, NULL, 'n'},
{"help", no_argument, NULL, 'h'},
{"time-speed", required_argument, NULL, 's'},
{0, 0, 0, 0}};
i = 1;
if (!strcmp(aArgVector[i], "-s"))
{
++i;
speedUpFactor = (uint32_t)strtol(aArgVector[i], &endptr, 0);
option = getopt_long(aArgCount, aArgVector, "hns:", options, NULL);
if (*endptr != '\0' || speedUpFactor == 0)
if (option == -1)
{
fprintf(stderr, "Invalid value for TimerSpeedUpFactor: %s\n", aArgVector[i]);
exit(OT_EXIT_INVALID_ARGUMENTS);
break;
}
switch (option)
{
case 'h':
PrintUsage(aArgVector[0], stdout, OT_EXIT_SUCCESS);
break;
case 'n':
isDryRun = true;
break;
case 's':
{
char *endptr = NULL;
speedUpFactor = (uint32_t)strtol(optarg, &endptr, 0);
if (*endptr != '\0' || speedUpFactor == 0)
{
fprintf(stderr, "Invalid value for TimerSpeedUpFactor: %s\n", optarg);
exit(OT_EXIT_INVALID_ARGUMENTS);
}
break;
}
case '?':
PrintUsage(aArgVector[0], stderr, OT_EXIT_INVALID_ARGUMENTS);
break;
default:
assert(false);
break;
}
++i;
}
if (i >= aArgCount)
if (optind >= aArgCount)
{
PrintUsage(aArgVector[0]);
exit(OT_EXIT_INVALID_ARGUMENTS);
PrintUsage(aArgVector[0], stderr, OT_EXIT_INVALID_ARGUMENTS);
}
radioFile = aArgVector[i];
if (i + 1 < aArgCount)
radioFile = aArgVector[optind];
if (optind + 1 < aArgCount)
{
radioConfig = aArgVector[i + 1];
radioConfig = aArgVector[optind + 1];
}
platformLoggingInit(basename(aArgVector[0]));
@@ -112,6 +143,11 @@ void otSysInit(int aArgCount, char *aArgVector[])
#if OPENTHREAD_ENABLE_PLATFORM_UDP
platformUdpInit();
#endif
if (isDryRun)
{
exit(OT_EXIT_SUCCESS);
}
}
bool otSysPseudoResetWasRequested(void)