diff --git a/.travis/script.sh b/.travis/script.sh index 8366883d7..06af529e1 100755 --- a/.travis/script.sh +++ b/.travis/script.sh @@ -400,10 +400,10 @@ build_samr21() { [ $BUILD_TARGET != posix-app-cli ] || { ./bootstrap || die # enable code coverage for OpenThread transceiver only - REFERENCE_DEVICE=1 COVERAGE=1 VIRTUAL_TIME_UART=1 make -f examples/Makefile-posix || die + COVERAGE=1 VIRTUAL_TIME_UART=1 make -f examples/Makefile-posix || die # readline supports pipe, editline does not REFERENCE_DEVICE=1 COVERAGE=1 READLINE=readline make -f src/posix/Makefile-posix || die - REFERENCE_DEVICE=1 COVERAGE=1 PYTHONUNBUFFERED=1 OT_CLI_PATH="$(pwd)/$(ls output/posix/*/bin/ot-cli)" RADIO_DEVICE="$(pwd)/$(ls output/*/bin/ot-rcp)" make -f src/posix/Makefile-posix check || die + REFERENCE_DEVICE=1 COVERAGE=1 PYTHONUNBUFFERED=1 OT_CLI_PATH="$(pwd)/$(ls output/posix/*/bin/ot-cli) -v" RADIO_DEVICE="$(pwd)/$(ls output/*/bin/ot-rcp)" make -f src/posix/Makefile-posix check || die } [ $BUILD_TARGET != posix-app-pty ] || { diff --git a/examples/common-switches.mk b/examples/common-switches.mk index de4bfbc03..fac832b89 100644 --- a/examples/common-switches.mk +++ b/examples/common-switches.mk @@ -48,6 +48,9 @@ ECDSA ?= 0 JAM_DETECTION ?= 0 JOINER ?= 0 LEGACY ?= 0 +ifeq ($(REFERENCE_DEVICE),1) +LOG_OUTPUT ?= APP +endif LINK_RAW ?= 0 MAC_FILTER ?= 0 MTD_NETDIAG ?= 0 @@ -146,6 +149,10 @@ ifeq ($(LINK_RAW),1) COMMONCFLAGS += -DOPENTHREAD_CONFIG_LINK_RAW_ENABLE=1 endif +ifneq ($(LOG_OUTPUT),) +COMMONCFLAGS += -DOPENTHREAD_CONFIG_LOG_OUTPUT=OPENTHREAD_CONFIG_LOG_OUTPUT_$(LOG_OUTPUT) +endif + ifeq ($(MAC_FILTER),1) COMMONCFLAGS += -DOPENTHREAD_CONFIG_MAC_FILTER_ENABLE=1 endif @@ -160,7 +167,6 @@ endif # Enable features only required for reference device during certification. ifeq ($(REFERENCE_DEVICE),1) -COMMONCFLAGS += -DOPENTHREAD_CONFIG_LOG_OUTPUT=OPENTHREAD_CONFIG_LOG_OUTPUT_APP COMMONCFLAGS += -DOPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE=1 endif diff --git a/src/posix/Makefile-posix b/src/posix/Makefile-posix index 73c86cc64..e4071630d 100644 --- a/src/posix/Makefile-posix +++ b/src/posix/Makefile-posix @@ -53,6 +53,7 @@ JAM_DETECTION ?= 1 JOINER ?= 1 LEGACY ?= 1 LINK_RAW ?= 0 +LOG_OUTPUT ?= PLATFORM_DEFINED MAC_FILTER ?= 1 MTD_NETDIAG ?= 1 READLINE ?= readline diff --git a/src/posix/main.c b/src/posix/main.c index 5643200af..142adfff9 100644 --- a/src/posix/main.c +++ b/src/posix/main.c @@ -26,15 +26,17 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include + #include - -#include "openthread-core-config.h" -#include "platform-posix.h" - #include +#include +#include #include #include #include +#include +#include #include #ifdef __linux__ #include @@ -45,6 +47,7 @@ #include #include +#include #if OPENTHREAD_POSIX_APP_TYPE == OPENTHREAD_POSIX_APP_TYPE_NCP #include #elif OPENTHREAD_POSIX_APP_TYPE == OPENTHREAD_POSIX_APP_TYPE_CLI @@ -56,19 +59,155 @@ #else #error "Unknown posix app type!" #endif -#include +#include -#include "openthread-system.h" - -jmp_buf gResetJump; +static jmp_buf gResetJump; void __gcov_flush(); +static const struct option kOptions[] = {{"dry-run", no_argument, NULL, 'n'}, + {"help", no_argument, NULL, 'h'}, + {"interface-name", required_argument, NULL, 'I'}, + {"no-reset", no_argument, NULL, 0}, + {"radio-version", no_argument, NULL, 0}, + {"time-speed", required_argument, NULL, 's'}, + {"verbose", no_argument, NULL, 'v'}, + {0, 0, 0, 0}}; + +static void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode) +{ + fprintf(aStream, + "Syntax:\n" + " %s [Options] NodeId|Device|Command [DeviceConfig|CommandArgs]\n" + "Options:\n" + " -I --interface-name name Thread network interface name.\n" + " -n --dry-run Just verify if arguments is valid and radio spinel is compatible.\n" + " --no-reset Do not reset RCP on initialization\n" + " --radio-version Print radio firmware version\n" + " -s --time-speed factor Time speed up factor.\n" + " -v --verbose Also log to stderr.\n" + " -h --help Display this usage information.\n", + aProgramName); + exit(aExitCode); +} + +static otInstance *InitInstance(int aArgCount, char *aArgVector[]) +{ + otPlatformConfig config; + otInstance * instance = NULL; + bool isDryRun = false; + bool printRadioVersion = false; + bool isVerbose = false; + + memset(&config, 0, sizeof(config)); + + config.mSpeedUpFactor = 1; + config.mResetRadio = true; + + optind = 1; + + while (true) + { + int index = 0; + int option = getopt_long(aArgCount, aArgVector, "hI:ns:v", kOptions, &index); + + if (option == -1) + { + break; + } + + switch (option) + { + case 'h': + PrintUsage(aArgVector[0], stdout, OT_EXIT_SUCCESS); + break; + case 'I': + config.mInterfaceName = optarg; + break; + case 'n': + isDryRun = true; + break; + case 's': + { + char *endptr = NULL; + config.mSpeedUpFactor = (uint32_t)strtol(optarg, &endptr, 0); + + if (*endptr != '\0' || config.mSpeedUpFactor == 0) + { + fprintf(stderr, "Invalid value for TimerSpeedUpFactor: %s\n", optarg); + exit(OT_EXIT_INVALID_ARGUMENTS); + } + break; + } + case 'v': + isVerbose = true; + break; + + case 0: + if (!strcmp(kOptions[index].name, "radio-version")) + { + printRadioVersion = true; + } + else if (!strcmp(kOptions[index].name, "no-reset")) + { + config.mResetRadio = false; + } + break; + case '?': + PrintUsage(aArgVector[0], stderr, OT_EXIT_INVALID_ARGUMENTS); + break; + default: + assert(false); + break; + } + } + +#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED + openlog(aArgVector[0], LOG_PID | (isVerbose ? LOG_PERROR : 0), LOG_DAEMON); + setlogmask(setlogmask(0) & LOG_UPTO(LOG_DEBUG)); +#endif + + if (optind >= aArgCount) + { + PrintUsage(aArgVector[0], stderr, OT_EXIT_INVALID_ARGUMENTS); + } + + config.mRadioFile = aArgVector[optind]; + + if (optind + 1 < aArgCount) + { + config.mRadioConfig = aArgVector[optind + 1]; + } + + instance = otSysInit(&config); + + if (printRadioVersion) + { + printf("%s\n", otPlatRadioGetVersionString(instance)); + } + + if (isDryRun) + { + exit(OT_EXIT_SUCCESS); + } + + return instance; +} + void otTaskletsSignalPending(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); } +void otPlatReset(otInstance *aInstance) +{ + otInstanceFinalize(aInstance); + otSysDeinit(); + + longjmp(gResetJump, 1); + assert(false); +} + int main(int argc, char *argv[]) { otInstance *instance; @@ -88,14 +227,11 @@ int main(int argc, char *argv[]) execvp(argv[0], argv); } - instance = otSysInit(argc, argv); + instance = InitInstance(argc, argv); #if OPENTHREAD_POSIX_APP_TYPE == OPENTHREAD_POSIX_APP_TYPE_NCP otNcpInit(instance); #elif OPENTHREAD_POSIX_APP_TYPE == OPENTHREAD_POSIX_APP_TYPE_CLI -#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE - otSysInitNetif(instance); -#endif #if OPENTHREAD_USE_CONSOLE otxConsoleInit(instance); #else @@ -145,24 +281,3 @@ int main(int argc, char *argv[]) return 0; } - -/* - * Provide, if required an "otPlatLog()" function - */ -#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_APP -void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) -{ - OT_UNUSED_VARIABLE(aLogLevel); - OT_UNUSED_VARIABLE(aLogRegion); - OT_UNUSED_VARIABLE(aFormat); - - va_list ap; - va_start(ap, aFormat); -#if OPENTHREAD_POSIX_APP_TYPE == OPENTHREAD_POSIX_APP_TYPE_NCP - otNcpPlatLogv(aLogLevel, aLogRegion, aFormat, ap); -#elif OPENTHREAD_POSIX_APP_TYPE == OPENTHREAD_POSIX_APP_TYPE_CLI - otCliPlatLogv(aLogLevel, aLogRegion, aFormat, ap); -#endif - va_end(ap); -} -#endif diff --git a/src/posix/platform/Makefile.am b/src/posix/platform/Makefile.am index cbc4e260a..fb2323174 100644 --- a/src/posix/platform/Makefile.am +++ b/src/posix/platform/Makefile.am @@ -55,12 +55,18 @@ libopenthread_posix_a_SOURCES = \ $(NULL) noinst_HEADERS = \ - openthread-system.h \ platform-posix.h \ hdlc_interface.hpp \ radio_spinel.hpp \ $(NULL) +openthread_HEADERS = \ + openthread-system.h \ + $(NULL) + +openthreaddir = $(includedir)/openthread +dist_openthread_HEADERS = $(openthread_headers) + PRETTY_FILES = \ $(libopenthread_posix_a_SOURCES) \ $(noinst_HEADERS) \ diff --git a/src/posix/platform/alarm.c b/src/posix/platform/alarm.c index bb659af63..778efe9bc 100644 --- a/src/posix/platform/alarm.c +++ b/src/posix/platform/alarm.c @@ -52,7 +52,7 @@ static uint32_t sUsAlarm = 0; static uint32_t sSpeedUpFactor = 1; #if !OPENTHREAD_POSIX_VIRTUAL_TIME -uint64_t otSysGetTime(void) +uint64_t platformGetTime(void) { struct timespec now; @@ -68,7 +68,7 @@ uint64_t otSysGetTime(void) static uint64_t platformAlarmGetNow(void) { - return otSysGetTime() * sSpeedUpFactor; + return platformGetTime() * sSpeedUpFactor; } void platformAlarmInit(uint32_t aSpeedUpFactor) @@ -99,7 +99,7 @@ void otPlatAlarmMilliStop(otInstance *aInstance) #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE uint32_t otPlatAlarmMicroGetNow(void) { - return (uint32_t)(otSysGetTime()); + return (uint32_t)(platformGetTime()); } void otPlatAlarmMicroStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) diff --git a/src/posix/platform/hdlc_interface.cpp b/src/posix/platform/hdlc_interface.cpp index 875a08804..ea6155528 100644 --- a/src/posix/platform/hdlc_interface.cpp +++ b/src/posix/platform/hdlc_interface.cpp @@ -224,7 +224,7 @@ otError HdlcInterface::Write(const uint8_t *aFrame, uint16_t aLength) { otError error = OT_ERROR_NONE; #if OPENTHREAD_POSIX_VIRTUAL_TIME - otSimSendRadioSpinelWriteEvent(aFrame, aLength); + platformSimSendRadioSpinelWriteEvent(aFrame, aLength); #else while (aLength) { @@ -254,7 +254,7 @@ otError HdlcInterface::WaitForWritable(void) { otError error = OT_ERROR_NONE; struct timeval timeout = {kMaxWaitTime / 1000, (kMaxWaitTime % 1000) * 1000}; - uint64_t now = otSysGetTime(); + uint64_t now = platformGetTime(); uint64_t end = now + kMaxWaitTime * US_PER_MS; fd_set writeFds; fd_set errorFds; @@ -289,7 +289,7 @@ otError HdlcInterface::WaitForWritable(void) DieNow(OT_EXIT_ERROR_ERRNO); } - now = otSysGetTime(); + now = platformGetTime(); if (end > now) { @@ -337,7 +337,10 @@ int HdlcInterface::OpenFile(const char *aFile, const char *aConfig) tios.c_cflag = CS8 | HUPCL | CREAD | CLOCAL; // example: 115200N1 - sscanf(aConfig, "%u%c%d", &speed, &parity, &cstopb); + if (aConfig != NULL) + { + sscanf(aConfig, "%u%c%d", &speed, &parity, &cstopb); + } switch (parity) { diff --git a/src/posix/platform/logging.c b/src/posix/platform/logging.c index 8e47132ed..3f549055a 100644 --- a/src/posix/platform/logging.c +++ b/src/posix/platform/logging.c @@ -41,22 +41,8 @@ #define LOGGING_MAX_LOG_STRING_SIZE 512 -void platformLoggingInit(const char *aName) -{ -#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED) || \ - (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_NCP_SPINEL) - - openlog(aName, LOG_PID, LOG_DAEMON); - setlogmask(setlogmask(0) & LOG_UPTO(LOG_DEBUG)); - -#else - OT_UNUSED_VARIABLE(aName); -#endif -} - -#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED) || \ - (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_NCP_SPINEL) -OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) +#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED +void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) { OT_UNUSED_VARIABLE(aLogRegion); @@ -105,5 +91,4 @@ exit: } syslog(logLevel, "%s", logString); } - -#endif // #if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED) +#endif // OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED diff --git a/src/posix/platform/misc.c b/src/posix/platform/misc.c index 48f10ef73..3c08fa101 100644 --- a/src/posix/platform/misc.c +++ b/src/posix/platform/misc.c @@ -38,23 +38,11 @@ #include #include "code_utils.h" -#include "openthread-system.h" #include "common/logging.hpp" -extern jmp_buf gResetJump; - static otPlatResetReason sPlatResetReason = OT_PLAT_RESET_REASON_POWER_ON; static otPlatMcuPowerState gPlatMcuPowerState = OT_PLAT_MCU_POWER_STATE_ON; -void otPlatReset(otInstance *aInstance) -{ - otInstanceFinalize(aInstance); - otSysDeinit(); - - longjmp(gResetJump, 1); - assert(false); -} - otPlatResetReason otPlatGetResetReason(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); diff --git a/src/posix/platform/openthread-system.h b/src/posix/platform/openthread-system.h index 3a40357af..3c1d79733 100644 --- a/src/posix/platform/openthread-system.h +++ b/src/posix/platform/openthread-system.h @@ -35,35 +35,81 @@ #ifndef OPENTHREAD_SYSTEM_H_ #define OPENTHREAD_SYSTEM_H_ +#include +#include +#include #include +#include #include #ifdef __cplusplus extern "C" { #endif +/** + * This enumeration represents exit codes used when OpenThread exits. + * + */ +enum +{ + /** + * Success. + */ + OT_EXIT_SUCCESS = 0, + + /** + * Generic failure. + */ + OT_EXIT_FAILURE = 1, + + /** + * Invalid arguments. + */ + OT_EXIT_INVALID_ARGUMENTS = 2, + + /** + * Incompatible radio spinel. + */ + OT_EXIT_RADIO_SPINEL_INCOMPATIBLE = 3, + + /** + * Unexpected radio spinel reset. + */ + OT_EXIT_RADIO_SPINEL_RESET = 4, + + /** + * System call or library function error. + */ + OT_EXIT_ERROR_ERRNO = 5, +}; + +/** + * This structure represents platform specific configurations. + * + */ +typedef struct otPlatformConfig +{ + uint64_t mNodeId; /// Unique node ID. + const char *mInterfaceName; /// Thread network interface name. + const char *mRadioFile; /// Radio file path. + const char *mRadioConfig; /// Radio configurations. + uint32_t mSpeedUpFactor; /// Speed up factor. + bool mResetRadio; /// Whether to reset RCP when initializing. +} otPlatformConfig; + /** * This function performs all platform-specific initialization of OpenThread's drivers. * * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function * when initialization of OpenThread's drivers is most appropriate. * - * @param[in] argc Number of arguments in @p argv. - * @param[in] argv Argument vector. + * @param[in] aPlatformConfig Argument vector. * * @returns A pointer to the OpenThread instance. * */ -otInstance *otSysInit(int argc, char *argv[]); - -/** - * This function performs platform network interface initialization. - * - * @param[in] aInstance A pointer to the OpenThread instance. - * - */ -void otSysInitNetif(otInstance *aInstance); +otInstance *otSysInit(otPlatformConfig *aPlatformConfig); /** * This function performs all platform-specific deinitialization for OpenThread's drivers. @@ -118,15 +164,6 @@ int otSysMainloopPoll(otSysMainloopContext *aMainloop); */ void otSysMainloopProcess(otInstance *aInstance, const otSysMainloopContext *aMainloop); -/** - * This function is called whenever platform drivers needs processing. - * - * @note This function is not handled by the OpenThread library. Instead, the system/RTOS should handle this function - * and schedule a call to `otSysProcessDrivers()`. - * - */ -extern void otSysEventSignalPending(void); - #ifdef __cplusplus } // end of extern "C" #endif diff --git a/src/posix/platform/platform-config.h b/src/posix/platform/platform-config.h index e6f6739f4..98371b67e 100644 --- a/src/posix/platform/platform-config.h +++ b/src/posix/platform/platform-config.h @@ -26,6 +26,9 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#ifndef OPENTHREAD_PLATFORM_CONFIG_H_ +#define OPENTHREAD_PLATFORM_CONFIG_H_ + /** * @file * @brief @@ -51,3 +54,5 @@ #ifndef OPENTHREAD_POSIX_APP_SOCKET_BASENAME #define OPENTHREAD_POSIX_APP_SOCKET_BASENAME "/tmp/openthread" #endif + +#endif // OPENTHREAD_PLATFORM_CONFIG_H_ diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index f17f50726..6893df1c4 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -41,6 +41,7 @@ #include #include +#include #include #include @@ -75,43 +76,6 @@ struct Event uint8_t mData[OT_EVENT_DATA_MAX_SIZE]; } OT_TOOL_PACKED_END; -/** - * This enumeration represents exit codes used when OpenThread exits. - * - */ -enum -{ - /** - * Success. - */ - OT_EXIT_SUCCESS = 0, - - /** - * Generic failure. - */ - OT_EXIT_FAILURE = 1, - - /** - * Invalid arguments. - */ - OT_EXIT_INVALID_ARGUMENTS = 2, - - /** - * Incompatible radio spinel. - */ - OT_EXIT_RADIO_SPINEL_INCOMPATIBLE = 3, - - /** - * Unexpected radio spinel reset. - */ - OT_EXIT_RADIO_SPINEL_RESET = 4, - - /** - * System call or library function error. - */ - OT_EXIT_ERROR_ERRNO = 5, -}; - /** * This function converts an exit code into a string. * @@ -351,32 +315,33 @@ void platformNetifUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, fd_set *a void platformNetifProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, const fd_set *aErrorFdSet); /** - * This function initialize simulation. + * This function initialize virtual time simulation. * */ -void otSimInit(void); +void platformSimInit(void); /** - * This function deinitialize simulation. + * This function deinitialize virtual time simulation. * */ -void otSimDeinit(void); +void platformSimDeinit(void); /** - * This function performs simulation processing. + * This function performs virtual time simulation processing. * * @param[in] aInstance A pointer to the OpenThread instance. * @param[in] aReadFdSet A pointer to the read file descriptors. * @param[in] aWriteFdSet A pointer to the write file descriptors. * */ -void otSimProcess(otInstance * aInstance, - const fd_set *aReadFdSet, - const fd_set *aWriteFdSet, - const fd_set *aErrorFdSet); +void platformSimProcess(otInstance * aInstance, + const fd_set *aReadFdSet, + const fd_set *aWriteFdSet, + const fd_set *aErrorFdSet); /** - * This function updates the file descriptor sets with file descriptors used by the simulation. + * This function updates the file descriptor sets with file descriptors + * used by the virtual time simulation. * * @param[inout] aReadFdSet A pointer to the read file descriptors. * @param[inout] aWriteFdSet A pointer to the write file descriptors. @@ -385,53 +350,54 @@ void otSimProcess(otInstance * aInstance, * @param[inout] aTimeout A pointer to the timeout. * */ -void otSimUpdateFdSet(fd_set * aReadFdSet, - fd_set * aWriteFdSet, - fd_set * aErrorFdSet, - int * aMaxFd, - struct timeval *aTimeout); +void platformSimUpdateFdSet(fd_set * aReadFdSet, + fd_set * aWriteFdSet, + fd_set * aErrorFdSet, + int * aMaxFd, + struct timeval *aTimeout); /** - * This function sends radio spinel frame through simulation. + * This function sends radio spinel event of virtual time simulation. * * @param[in] aData A pointer to the spinel frame. * @param[in] aLength Length of the spinel frame. * */ -void otSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength); +void platformSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength); /** - * This function receives a simulation event. + * This function receives an event of virtual time simulation. * * @param[out] aEvent A pointer to the event receiving the event. * */ -void otSimReceiveEvent(struct Event *aEvent); +void platformSimReceiveEvent(struct Event *aEvent); /** - * This function sends sleep event through simulation. + * This function sends sleep event through virtual time simulation. * * @param[in] aTimeout A pointer to the time sleeping. * */ -void otSimSendSleepEvent(const struct timeval *aTimeout); +void platformSimSendSleepEvent(const struct timeval *aTimeout); /** - * This function updates the file descriptor sets with file descriptors used by radio spinel. + * This function updates the file descriptor sets with file descriptors + * used by radio spinel of virtual time simulation. * * @param[out] aTimeout A pointer to the timeout event to be updated. * */ -void otSimRadioSpinelUpdate(struct timeval *atimeout); +void platformSimRadioSpinelUpdate(struct timeval *atimeout); /** - * This function performs radio spinel processing. + * This function performs radio spinel processing of virtual time simulation. * * @param[in] aInstance A pointer to the OpenThread instance. * @param[in] aEvent A pointer to the current event. * */ -void otSimRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent); +void platformSimRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent); /** * This function gets system time in microseconds without applying speed up factor. @@ -439,7 +405,7 @@ void otSimRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent); * @returns System time in microseconds. * */ -uint64_t otSysGetTime(void); +uint64_t platformGetTime(void); /** * This function initializes platform UDP driver. diff --git a/src/posix/platform/radio_spinel.cpp b/src/posix/platform/radio_spinel.cpp index a1eb825ed..55c02768e 100644 --- a/src/posix/platform/radio_spinel.cpp +++ b/src/posix/platform/radio_spinel.cpp @@ -717,7 +717,7 @@ void RadioSpinel::UpdateFdSet(fd_set &aReadFdSet, fd_set &aWriteFdSet, int &aMax } else if (mState == kStateTransmitting) { - uint64_t now = otSysGetTime(); + uint64_t now = platformGetTime(); if (now < mTxRadioEndUs) { @@ -774,7 +774,7 @@ void RadioSpinel::Process(const fd_set &aReadFdSet, const fd_set &aWriteFdSet) mTxError); } } - else if (mState == kStateTransmitting && otSysGetTime() >= mTxRadioEndUs) + else if (mState == kStateTransmitting && platformGetTime() >= mTxRadioEndUs) { DieNowWithMessage("radio tx timeout", OT_EXIT_FAILURE); } @@ -975,7 +975,7 @@ otError RadioSpinel::Remove(spinel_prop_key_t aKey, const char *aFormat, ...) otError RadioSpinel::WaitResponse(void) { - uint64_t now = otSysGetTime(); + uint64_t now = platformGetTime(); uint64_t end = now + kMaxWaitTime * US_PER_MS; struct timeval timeout = {kMaxWaitTime / 1000, (kMaxWaitTime % 1000) * 1000}; @@ -984,8 +984,8 @@ otError RadioSpinel::WaitResponse(void) #if OPENTHREAD_POSIX_VIRTUAL_TIME struct Event event; - otSimSendSleepEvent(&timeout); - otSimReceiveEvent(&event); + platformSimSendSleepEvent(&timeout); + platformSimReceiveEvent(&event); switch (event.mEvent) { @@ -1044,7 +1044,7 @@ otError RadioSpinel::WaitResponse(void) } #endif // OPENTHREAD_POSIX_VIRTUAL_TIME - now = otSysGetTime(); + now = platformGetTime(); if (end > now) { @@ -1107,7 +1107,7 @@ void RadioSpinel::RadioTransmit(void) if (error == OT_ERROR_NONE) { - mTxRadioEndUs = otSysGetTime() + TX_WAIT_US; + mTxRadioEndUs = platformGetTime() + TX_WAIT_US; mState = kStateTransmitting; } else @@ -1307,7 +1307,6 @@ otError RadioSpinel::Receive(uint8_t aChannel) mState = kStateReceive; exit: - assert(error == OT_ERROR_NONE); return error; } @@ -1678,12 +1677,12 @@ void ot::PosixApp::RadioSpinel::Update(struct timeval &aTimeout) } } -void otSimRadioSpinelUpdate(struct timeval *aTimeout) +void platformSimRadioSpinelUpdate(struct timeval *aTimeout) { sRadioSpinel.Update(*aTimeout); } -void otSimRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent) +void platformSimRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent) { sRadioSpinel.Process(*aEvent); OT_UNUSED_VARIABLE(aInstance); diff --git a/src/posix/platform/sim.c b/src/posix/platform/sim.c index 5558c9445..f3c8b92b9 100644 --- a/src/posix/platform/sim.c +++ b/src/posix/platform/sim.c @@ -53,7 +53,7 @@ static int sSockFd = -1; ///< Socket used to communicating with simulat static uint16_t sPortOffset = 0; ///< Port offset for simulation. static int sNodeId = 0; ///< Node id of this simulated device. -void otSimInit(void) +void platformSimInit(void) { struct sockaddr_in sockaddr; char * offset; @@ -100,7 +100,7 @@ void otSimInit(void) } } -void otSimDeinit(void) +void platformSimDeinit(void) { if (sSockFd != -1) { @@ -109,7 +109,7 @@ void otSimDeinit(void) } } -static void otSimSendEvent(struct Event *aEvent, size_t aLength) +static void platformSimSendEvent(struct Event *aEvent, size_t aLength) { ssize_t rval; struct sockaddr_in sockaddr; @@ -127,7 +127,7 @@ static void otSimSendEvent(struct Event *aEvent, size_t aLength) } } -void otSimReceiveEvent(struct Event *aEvent) +void platformSimReceiveEvent(struct Event *aEvent) { ssize_t rval = recvfrom(sSockFd, aEvent, sizeof(*aEvent), 0, NULL, NULL); @@ -139,7 +139,7 @@ void otSimReceiveEvent(struct Event *aEvent) sNow += aEvent->mDelay; } -void otSimSendSleepEvent(const struct timeval *aTimeout) +void platformSimSendSleepEvent(const struct timeval *aTimeout) { struct Event event; @@ -147,10 +147,10 @@ void otSimSendSleepEvent(const struct timeval *aTimeout) event.mEvent = OT_SIM_EVENT_ALARM_FIRED; event.mDataLength = 0; - otSimSendEvent(&event, offsetof(struct Event, mData)); + platformSimSendEvent(&event, offsetof(struct Event, mData)); } -void otSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength) +void platformSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength) { struct Event event; @@ -160,14 +160,14 @@ void otSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength) memcpy(event.mData, aData, aLength); - otSimSendEvent(&event, offsetof(struct Event, mData) + event.mDataLength); + platformSimSendEvent(&event, offsetof(struct Event, mData) + event.mDataLength); } -void otSimUpdateFdSet(fd_set * aReadFdSet, - fd_set * aWriteFdSet, - fd_set * aErrorFdSet, - int * aMaxFd, - struct timeval *aTimeout) +void platformSimUpdateFdSet(fd_set * aReadFdSet, + fd_set * aWriteFdSet, + fd_set * aErrorFdSet, + int * aMaxFd, + struct timeval *aTimeout) { OT_UNUSED_VARIABLE(aWriteFdSet); OT_UNUSED_VARIABLE(aErrorFdSet); @@ -178,10 +178,13 @@ void otSimUpdateFdSet(fd_set * aReadFdSet, *aMaxFd = sSockFd; } - otSimRadioSpinelUpdate(aTimeout); + platformSimRadioSpinelUpdate(aTimeout); } -void otSimProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet, const fd_set *aErrorFdSet) +void platformSimProcess(otInstance * aInstance, + const fd_set *aReadFdSet, + const fd_set *aWriteFdSet, + const fd_set *aErrorFdSet) { struct Event event = {0}; @@ -191,13 +194,13 @@ void otSimProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set if (FD_ISSET(sSockFd, aReadFdSet)) { - otSimReceiveEvent(&event); + platformSimReceiveEvent(&event); } - otSimRadioSpinelProcess(aInstance, &event); + platformSimRadioSpinelProcess(aInstance, &event); } -uint64_t otSysGetTime(void) +uint64_t platformGetTime(void) { return sNow; } diff --git a/src/posix/platform/system.c b/src/posix/platform/system.c index 4b6f43d17..36aa920a3 100644 --- a/src/posix/platform/system.c +++ b/src/posix/platform/system.c @@ -36,154 +36,41 @@ #include "platform-posix.h" #include -#include -#include -#include -#include -#include -#include -#include #include #include #include #include -#include "openthread-system.h" - -static const struct option kOptions[] = { - {"dry-run", no_argument, NULL, 'n'}, {"no-reset", no_argument, NULL, 0}, - {"radio-version", no_argument, NULL, 0}, {"help", no_argument, NULL, 'h'}, - {"time-speed", required_argument, NULL, 's'}, {0, 0, 0, 0}}; - uint64_t gNodeId = 0; -static void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode) +otInstance *otSysInit(otPlatformConfig *aPlatformConfig) { - fprintf(aStream, - "Syntax:\n" - " %s [Options] NodeId|Device|Command [DeviceConfig|CommandArgs]\n" - "Options:\n" - " -n --dry-run Just verify if arguments is valid and radio spinel is compatible.\n" - " --no-reset Do not reset RCP on initialization\n" - " --radio-version Print radio firmware version\n" - " -s --time-speed factor Time speed up factor.\n" - " -h --help Display this usage information.\n", - aProgramName); - exit(aExitCode); -} - -otInstance *otSysInit(int aArgCount, char *aArgVector[]) -{ - const char *radioFile = NULL; - const char *radioConfig = ""; - otInstance *instance = NULL; - uint32_t speedUpFactor = 1; - bool isDryRun = false; - bool reset = true; - bool printRadioVersion = false; - - optind = 1; - - while (true) - { - int index = 0; - int option = getopt_long(aArgCount, aArgVector, "hns:", kOptions, &index); - - if (option == -1) - { - 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 0: - if (!strcmp(kOptions[index].name, "radio-version")) - { - printRadioVersion = true; - } - else if (!strcmp(kOptions[index].name, "no-reset")) - { - reset = false; - } - break; - case '?': - PrintUsage(aArgVector[0], stderr, OT_EXIT_INVALID_ARGUMENTS); - break; - default: - assert(false); - break; - } - } - - if (optind >= aArgCount) - { - PrintUsage(aArgVector[0], stderr, OT_EXIT_INVALID_ARGUMENTS); - } - - radioFile = aArgVector[optind]; - if (optind + 1 < aArgCount) - { - radioConfig = aArgVector[optind + 1]; - } - - platformLoggingInit(basename(aArgVector[0])); + otInstance *instance = NULL; #if OPENTHREAD_POSIX_VIRTUAL_TIME - otSimInit(); + platformSimInit(); #endif - platformAlarmInit(speedUpFactor); - platformRadioInit(radioFile, radioConfig, reset); + platformAlarmInit(aPlatformConfig->mSpeedUpFactor); + platformRadioInit(aPlatformConfig->mRadioFile, aPlatformConfig->mRadioConfig, aPlatformConfig->mResetRadio); platformRandomInit(); -#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE && OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE == 0 - platformUdpInit(getenv("PLATFORM_NETIF")); -#endif instance = otInstanceInitSingle(); - assert(instance); + assert(instance != NULL); - if (printRadioVersion) - { - printf("%s\n", otPlatRadioGetVersionString(instance)); - } - - if (isDryRun) - { - exit(OT_EXIT_SUCCESS); - } +#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE + platformNetifInit(instance); +#elif OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE + platformUdpInit(aPlatformConfig->mInterfaceName); +#endif return instance; } -#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE -void otSysInitNetif(otInstance *aInstance) -{ - platformNetifInit(aInstance); -} -#endif - void otSysDeinit(void) { #if OPENTHREAD_POSIX_VIRTUAL_TIME - otSimDeinit(); + platformSimDeinit(); #endif platformRadioDeinit(); } @@ -234,8 +121,8 @@ void otSysMainloopUpdate(otInstance *aInstance, otSysMainloopContext *aMainloop) &aMainloop->mMaxFd); #endif #if OPENTHREAD_POSIX_VIRTUAL_TIME - otSimUpdateFdSet(&aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mErrorFdSet, &aMainloop->mMaxFd, - &aMainloop->mTimeout); + platformSimUpdateFdSet(&aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mErrorFdSet, &aMainloop->mMaxFd, + &aMainloop->mTimeout); #else platformRadioUpdateFdSet(&aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mMaxFd, &aMainloop->mTimeout); #endif @@ -273,7 +160,7 @@ int otSysMainloopPoll(otSysMainloopContext *aMainloop) if (noWrite) { - otSimSendSleepEvent(&aMainloop->mTimeout); + platformSimSendSleepEvent(&aMainloop->mTimeout); } rval = select(aMainloop->mMaxFd + 1, &aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, @@ -293,7 +180,7 @@ int otSysMainloopPoll(otSysMainloopContext *aMainloop) void otSysMainloopProcess(otInstance *aInstance, const otSysMainloopContext *aMainloop) { #if OPENTHREAD_POSIX_VIRTUAL_TIME - otSimProcess(aInstance, &aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mErrorFdSet); + platformSimProcess(aInstance, &aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mErrorFdSet); #else platformRadioProcess(aInstance, &aMainloop->mReadFdSet, &aMainloop->mWriteFdSet); #endif diff --git a/tests/scripts/thread-cert/node.py b/tests/scripts/thread-cert/node.py index c8394bf2b..475c7dbb8 100755 --- a/tests/scripts/thread-cert/node.py +++ b/tests/scripts/thread-cert/node.py @@ -78,7 +78,7 @@ class Node: cmd = './ot-cli-%s' % mode if 'RADIO_DEVICE' in os.environ: - cmd += ' %s' % os.environ['RADIO_DEVICE'] + cmd += ' -v %s' % os.environ['RADIO_DEVICE'] os.environ['NODE_ID'] = str(nodeid) cmd += ' %d' % nodeid