[posix-app] enhance virtual time for posix app (#3016)

This commit is contained in:
Yakun Xu
2018-09-05 18:08:36 -07:00
committed by Jonathan Hui
parent 2742886134
commit bdffbe5671
28 changed files with 1035 additions and 305 deletions
+2 -2
View File
@@ -47,10 +47,10 @@ after_success:
matrix:
include:
- env: BUILD_TARGET="posix-app-cli" VERBOSE=1
- env: BUILD_TARGET="posix-app-cli" VERBOSE=1 VIRTUAL_TIME=1
os: linux
compiler: gcc
- env: BUILD_TARGET="posix-app-ncp" VERBOSE=1
- env: BUILD_TARGET="posix-app-ncp" VERBOSE=1 VIRTUAL_TIME=1
os: linux
compiler: gcc
- env: BUILD_TARGET="android-build" VERBOSE=1
+5 -5
View File
@@ -542,9 +542,9 @@ set -x
[ $BUILD_TARGET != posix-app-cli ] || {
./bootstrap || die
make -f examples/Makefile-posix || die
VIRTUAL_TIME_UART=1 make -f examples/Makefile-posix || die
make -f src/posix/Makefile-posix || die
OT_CLI_PATH="$(pwd)/$(ls output/posix/*/bin/ot-cli)" RADIO_DEVICE="$(pwd)/$(ls output/*/bin/ot-ncp-radio)" COVERAGE=1 make -f src/posix/Makefile-posix check || die
PYTHONUNBUFFERED=1 OT_CLI_PATH="$(pwd)/$(ls output/posix/*/bin/ot-cli)" RADIO_DEVICE="$(pwd)/$(ls output/*/bin/ot-ncp-radio)" COVERAGE=1 make -f src/posix/Makefile-posix check || die
}
[ $BUILD_TARGET != posix-mtd ] || {
@@ -559,14 +559,14 @@ set -x
[ $BUILD_TARGET != posix-app-ncp ] || {
./bootstrap || die
make -f examples/Makefile-posix || die
VIRTUAL_TIME_UART=1 make -f examples/Makefile-posix || die
make -f src/posix/Makefile-posix || die
OT_NCP_PATH="$(pwd)/$(ls output/posix/*/bin/ot-ncp)" RADIO_DEVICE="$(pwd)/$(ls output/*/bin/ot-ncp-radio)" COVERAGE=1 NODE_TYPE=ncp-sim make -f src/posix/Makefile-posix check || die
PYTHONUNBUFFERED=1 OT_NCP_PATH="$(pwd)/$(ls output/posix/*/bin/ot-ncp)" RADIO_DEVICE="$(pwd)/$(ls output/*/bin/ot-ncp-radio)" COVERAGE=1 NODE_TYPE=ncp-sim make -f src/posix/Makefile-posix check || die
}
[ $BUILD_TARGET != posix-ncp ] || {
./bootstrap || die
NODE_TYPE=ncp-sim make -f examples/Makefile-posix check || die
PYTHONUNBUFFERED=1 NODE_TYPE=ncp-sim make -f examples/Makefile-posix check || die
}
[ $BUILD_TARGET != toranj-test-framework ] || {
+4
View File
@@ -91,6 +91,10 @@ ifeq ($(VIRTUAL_TIME),1)
COMMONCFLAGS += -DOPENTHREAD_POSIX_VIRTUAL_TIME=1
endif
ifeq ($(VIRTUAL_TIME_UART),1)
COMMONCFLAGS += -DOPENTHREAD_POSIX_VIRTUAL_TIME_UART=1
endif
CPPFLAGS += \
$(COMMONCFLAGS) \
$(NULL)
+22 -3
View File
@@ -85,9 +85,11 @@ __forceinline void timersub(struct timeval *a, struct timeval *b, struct timeval
enum
{
OT_SIM_EVENT_ALARM_FIRED = 0,
OT_SIM_EVENT_RADIO_RECEIVED = 1,
OT_EVENT_DATA_MAX_SIZE = 1024,
OT_SIM_EVENT_ALARM_FIRED = 0,
OT_SIM_EVENT_RADIO_RECEIVED = 1,
OT_SIM_EVENT_UART_WRITE = 2,
OT_SIM_EVENT_RADIO_SPINEL_WRITE = 3,
OT_EVENT_DATA_MAX_SIZE = 1024,
};
OT_TOOL_PACKED_BEGIN
@@ -225,4 +227,21 @@ void platformUartProcess(void);
*/
void platformUartRestore(void);
/**
* This function sends a simulation event.
*
* @param[in] aEvent A pointer to the simulation event to send
*
*/
void otSimSendEvent(const struct Event *aEvent);
/**
* This function sends Uart data through simulation.
*
* @param[in] aData A pointer to the UART data.
* @param[in] aLength Length of UART data.
*
*/
void otSimSendUartWriteEvent(const uint8_t *aData, uint16_t aLength);
#endif // PLATFORM_POSIX_H_
+6 -1
View File
@@ -40,7 +40,7 @@
#define US_PER_MS 1000
static uint64_t sNow = 0; // microseconds
extern uint64_t sNow; // microseconds
static bool sIsMsRunning = false;
static uint32_t sMsAlarm = 0;
@@ -109,6 +109,11 @@ int32_t platformAlarmGetNext(void)
int32_t milli = (int32_t)(sMsAlarm - otPlatAlarmMilliGetNow());
remaining = milli * US_PER_MS;
if (remaining < 0 && milli > 0)
{
remaining = INT32_MAX;
}
}
#if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER
+75 -33
View File
@@ -38,6 +38,7 @@
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <libgen.h>
#include <stddef.h>
#include <stdint.h>
@@ -47,6 +48,7 @@
#include <openthread/tasklet.h>
#include <openthread/platform/alarm-milli.h>
#include <openthread/platform/uart.h>
uint32_t NODE_ID = 1;
uint32_t WELLKNOWN_NODE_ID = 34;
@@ -56,9 +58,30 @@ extern bool gPlatformPseudoResetWasRequested;
int gArgumentsCount = 0;
char **gArguments = NULL;
uint64_t sNow = 0; // microseconds
int sSockFd;
uint16_t sPortOffset;
void otSimSendEvent(const struct Event *aEvent)
{
ssize_t rval;
struct sockaddr_in sockaddr;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &sockaddr.sin_addr);
sockaddr.sin_port = htons(9000 + sPortOffset);
rval = sendto(sSockFd, aEvent, offsetof(struct Event, mData) + aEvent->mDataLength, 0, (struct sockaddr *)&sockaddr,
sizeof(sockaddr));
if (rval < 0)
{
perror("sendto");
exit(EXIT_FAILURE);
}
}
static void receiveEvent(otInstance *aInstance)
{
struct Event event;
@@ -80,29 +103,19 @@ static void receiveEvent(otInstance *aInstance)
case OT_SIM_EVENT_RADIO_RECEIVED:
platformRadioReceive(aInstance, event.mData, event.mDataLength);
break;
case OT_SIM_EVENT_UART_WRITE:
otPlatUartReceived(event.mData, event.mDataLength);
break;
default:
assert(false);
}
}
static bool processEvent(otInstance *aInstance)
{
const int flags = POLLIN | POLLRDNORM | POLLERR | POLLNVAL | POLLHUP;
struct pollfd pollfd = {sSockFd, flags, 0};
bool rval = false;
if (POLL(&pollfd, 1, 0) > 0 && (pollfd.revents & flags) != 0)
{
receiveEvent(aInstance);
rval = true;
}
return rval;
}
static void platformSendSleepEvent(void)
{
struct sockaddr_in sockaddr;
struct Event event;
ssize_t rval;
struct Event event;
assert(platformAlarmGetNext() > 0);
@@ -110,21 +123,43 @@ static void platformSendSleepEvent(void)
event.mEvent = OT_SIM_EVENT_ALARM_FIRED;
event.mDataLength = 0;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &sockaddr.sin_addr);
sockaddr.sin_port = htons(9000 + sPortOffset);
rval = sendto(sSockFd, (const char *)&event, offsetof(struct Event, mData), 0, (struct sockaddr *)&sockaddr,
sizeof(sockaddr));
if (rval < 0)
{
perror("sendto");
exit(EXIT_FAILURE);
}
otSimSendEvent(&event);
}
#if OPENTHREAD_POSIX_VIRTUAL_TIME_UART
void platformUartRestore(void)
{
}
otError otPlatUartEnable(void)
{
return OT_ERROR_NONE;
}
otError otPlatUartDisable(void)
{
return OT_ERROR_NONE;
}
otError otPlatUartSend(const uint8_t *aData, uint16_t aLength)
{
otError error = OT_ERROR_NONE;
struct Event event;
event.mDelay = 0;
event.mEvent = OT_SIM_EVENT_UART_WRITE;
event.mDataLength = aLength;
memcpy(event.mData, aData, aLength);
otSimSendEvent(&event);
otPlatUartSendDone();
return error;
}
#endif // OPENTHREAD_POSIX_VIRTUAL_TIME_UART
static void socket_init(void)
{
struct sockaddr_in sockaddr;
@@ -152,7 +187,7 @@ static void socket_init(void)
sockaddr.sin_port = htons(9000 + sPortOffset + NODE_ID);
sockaddr.sin_addr.s_addr = INADDR_ANY;
sSockFd = (int)socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sSockFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sSockFd == -1)
{
@@ -228,7 +263,9 @@ void otSysProcessDrivers(otInstance *aInstance)
FD_SET(sSockFd, &read_fds);
max_fd = sSockFd;
#if OPENTHREAD_POSIX_VIRTUAL_TIME_UART == 0
platformUartUpdateFdSet(&read_fds, &write_fds, &error_fds, &max_fd);
#endif
if (!otTaskletsArePending(aInstance) && platformAlarmGetNext() > 0)
{
@@ -242,12 +279,17 @@ void otSysProcessDrivers(otInstance *aInstance)
exit(EXIT_FAILURE);
}
processEvent(aInstance);
if (FD_ISSET(sSockFd, &read_fds))
{
receiveEvent(aInstance);
}
}
platformAlarmProcess(aInstance);
platformRadioProcess(aInstance);
#if OPENTHREAD_POSIX_VIRTUAL_TIME_UART == 0
platformUartProcess();
#endif
}
#endif // OPENTHREAD_POSIX_VIRTUAL_TIME
+4 -19
View File
@@ -550,12 +550,9 @@ void platformRadioProcess(otInstance *aInstance)
void radioTransmit(struct RadioMessage *aMessage, const struct otRadioFrame *aFrame)
{
struct sockaddr_in sockaddr;
struct Event event;
ssize_t rval;
uint16_t crc = 0;
uint16_t crc_offset = aFrame->mLength - sizeof(uint16_t);
uint16_t crc = 0;
uint16_t crc_offset = aFrame->mLength - sizeof(uint16_t);
struct Event event;
for (uint32_t i = 0; i < crc_offset; i++)
{
@@ -570,19 +567,7 @@ void radioTransmit(struct RadioMessage *aMessage, const struct otRadioFrame *aFr
event.mDataLength = 1 + aFrame->mLength; // include channel in first byte
memcpy(event.mData, aMessage, event.mDataLength);
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &sockaddr.sin_addr);
sockaddr.sin_port = htons(9000 + sPortOffset);
rval = sendto(sSockFd, (const char *)&event, offsetof(struct Event, mData) + event.mDataLength, 0,
(struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (rval < 0)
{
perror("sendto");
exit(EXIT_FAILURE);
}
otSimSendEvent(&event);
}
void radioSendAck(void)
+2
View File
@@ -43,6 +43,7 @@
#include "utils/code_utils.h"
#if OPENTHREAD_POSIX_VIRTUAL_TIME_UART == 0
#ifdef OPENTHREAD_TARGET_LINUX
#include <sys/prctl.h>
int posix_openpt(int oflag);
@@ -285,6 +286,7 @@ void platformUartProcess(void)
}
}
}
#endif // OPENTHREAD_POSIX_VIRTUAL_TIME_UART == 0
#if OPENTHREAD_CONFIG_ENABLE_DEBUG_UART && (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_DEBUG_UART)
+1 -1
View File
@@ -159,7 +159,7 @@ otError Icmp::HandleMessage(Message &aMessage, MessageInfo &aMessageInfo)
if (icmp6Header.GetType() == IcmpHeader::kTypeEchoRequest)
{
HandleEchoRequest(aMessage, aMessageInfo);
ExitNow(error = HandleEchoRequest(aMessage, aMessageInfo));
}
aMessage.MoveOffset(sizeof(icmp6Header));
+4
View File
@@ -80,6 +80,10 @@ COMMONCFLAGS := \
-I$(CONFIG_FILE_PATH) \
$(NULL)
ifeq ($(VIRTUAL_TIME),1)
COMMONCFLAGS += -DOPENTHREAD_POSIX_VIRTUAL_TIME=1
endif
CPPFLAGS += \
$(COMMONCFLAGS) \
$(NULL)
+1
View File
@@ -51,6 +51,7 @@ libopenthread_posix_a_SOURCES = \
settings.cpp \
spi-stubs.c \
spinel.c \
sim.c \
system.c \
uart.c \
$(NULL)
+12 -10
View File
@@ -48,21 +48,20 @@ static uint32_t sMsAlarm = 0;
static bool sIsUsRunning = false;
static uint32_t sUsAlarm = 0;
static uint32_t sSpeedUpFactor = 1;
static uint32_t sSpeedUpFactor = 1;
static struct timeval sStart;
void platformAlarmInit(uint32_t aSpeedUpFactor)
{
sSpeedUpFactor = aSpeedUpFactor;
gettimeofday(&sStart, NULL);
otSysGetTime(&sStart);
}
uint32_t otPlatAlarmMilliGetNow(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
otSysGetTime(&tv);
timersub(&tv, &sStart, &tv);
return (uint32_t)(((uint64_t)tv.tv_sec * sSpeedUpFactor * MS_PER_S) +
@@ -86,7 +85,7 @@ uint32_t otPlatAlarmMicroGetNow(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
otSysGetTime(&tv);
timersub(&tv, &sStart, &tv);
return (uint32_t)(tv.tv_sec * US_PER_S + tv.tv_usec) * sSpeedUpFactor;
@@ -107,22 +106,25 @@ void otPlatAlarmMicroStop(otInstance *aInstance)
void platformAlarmUpdateTimeout(struct timeval *aTimeout)
{
int32_t usRemaining = DEFAULT_TIMEOUT * US_PER_S;
int32_t msRemaining = DEFAULT_TIMEOUT * MS_PER_S;
int32_t usRemaining = INT32_MAX;
int32_t msRemaining = INT32_MAX;
struct timeval now;
if (aTimeout == NULL)
{
return;
}
otSysGetTime(&now);
if (sIsUsRunning)
{
usRemaining = (int32_t)(sUsAlarm - otPlatAlarmMicroGetNow());
usRemaining = (int64_t)(sUsAlarm - otPlatAlarmMicroGetNow());
}
if (sIsMsRunning)
{
msRemaining = (int32_t)(sMsAlarm - otPlatAlarmMilliGetNow());
msRemaining = (int64_t)sMsAlarm * US_PER_MS - now.tv_sec * US_PER_S - now.tv_usec;
}
if (usRemaining <= 0 || msRemaining <= 0)
@@ -132,7 +134,7 @@ void platformAlarmUpdateTimeout(struct timeval *aTimeout)
}
else
{
int64_t remaining = ((int64_t)msRemaining) * US_PER_MS;
int64_t remaining = msRemaining;
if (usRemaining < remaining)
{
@@ -41,7 +41,7 @@
* The platform-specific string to insert into the OpenThread version string.
*
*/
#define OPENTHREAD_CONFIG_PLATFORM_INFO "POSIX"
#define OPENTHREAD_CONFIG_PLATFORM_INFO "POSIX"
/**
* @def OPENTHREAD_CONFIG_LOG_OUTPUT
@@ -50,7 +50,7 @@
*
*/
#ifndef OPENTHREAD_CONFIG_LOG_OUTPUT /* allow command line override */
#define OPENTHREAD_CONFIG_LOG_OUTPUT OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
#define OPENTHREAD_CONFIG_LOG_OUTPUT OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
#endif
/**
@@ -59,7 +59,7 @@
* Define to 1 if you want to support microsecond timer in platform.
*
*/
#define OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER 1
#define OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER 1
/**
* @def OPENTHREAD_CONFIG_POSIX_APP_ENABLE_PTY_DEVICE
@@ -71,4 +71,4 @@
#define OPENTHREAD_CONFIG_POSIX_APP_ENABLE_PTY_DEVICE 1
#endif
#endif // OPENTHREAD_CORE_POSIX_CONFIG_H_
#endif // OPENTHREAD_CORE_POSIX_CONFIG_H_
+109 -3
View File
@@ -58,11 +58,17 @@
#include "openthread-core-config.h"
#ifdef __cplusplus
extern "C" {
#endif
enum
{
OT_SIM_EVENT_ALARM_FIRED = 0,
OT_SIM_EVENT_RADIO_RECEIVED = 1,
OT_EVENT_DATA_MAX_SIZE = 1024,
OT_SIM_EVENT_ALARM_FIRED = 0,
OT_SIM_EVENT_RADIO_RECEIVED = 1,
OT_SIM_EVENT_UART_WRITE = 2,
OT_SIM_EVENT_RADIO_SPINEL_WRITE = 3,
OT_EVENT_DATA_MAX_SIZE = 1024,
};
OT_TOOL_PACKED_BEGIN
@@ -210,4 +216,104 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co
*/
void platformUartRestore(void);
/**
* This function initialize simulation.
*
*/
void otSimInit(void);
/**
* This function deinitialize simulation.
*
*/
void otSimDeinit(void);
/**
* This function gets simulation time.
*
* @param[in] aTime A pointer to a timeval receiving the current time.
*
*/
void otSimGetTime(struct timeval *aTime);
/**
* This function performs 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);
/**
* This function updates the file descriptor sets with file descriptors used by the simulation.
*
* @param[inout] aReadFdSet A pointer to the read file descriptors.
* @param[inout] aWriteFdSet A pointer to the write file descriptors.
* @param[inout] aErrorFdSet A pointer to the error file descriptors.
* @param[inout] aMaxFd A pointer to the max file descriptor.
* @param[inout] aTimeout A pointer to the timeout.
*
*/
void otSimUpdateFdSet(fd_set * aReadFdSet,
fd_set * aWriteFdSet,
fd_set * aErrorFdSet,
int * aMaxFd,
struct timeval *aTimeout);
/**
* This function sends radio spinel frame through 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);
/**
* This function receives a simulation event.
*
* @param[out] aEvent A pointer to the event receiving the event.
*
*/
void otSimReceiveEvent(struct Event *aEvent);
/**
* This function sends sleep event through simulation.
*
* @param[in] aTimeout A pointer to the time sleeping.
*
*/
void otSimSendSleepEvent(const struct timeval *aTimeout);
/**
* This function updates the file descriptor sets with file descriptors used by radio spinel.
*
* @param[out] aTimeout A pointer to the timeout event to be updated.
*
*/
void otSimRadioSpinelUpdate(struct timeval *atimeout);
/**
* This function performs radio spinel processing.
*
* @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);
#if OPENTHREAD_POSIX_VIRTUAL_TIME
#define otSysGetTime(aTime) otSimGetTime(aTime)
#else
#define otSysGetTime(aTime) gettimeofday(aTime, NULL)
#endif
#ifdef __cplusplus
}
#endif
#endif // PLATFORM_POSIX_H_
+107 -25
View File
@@ -31,9 +31,7 @@
* This file implements the spinel based radio transceiver.
*/
extern "C" {
#include "platform-posix.h"
}
#include "radio_spinel.hpp"
@@ -142,6 +140,7 @@ static inline void SuccessOrDie(otError aError)
{
if (aError != OT_ERROR_NONE)
{
// fprintf(stderr, "Operation failed: %s\r\n", otThreadErrorToString(aError));
exit(EXIT_FAILURE);
}
}
@@ -275,7 +274,6 @@ static int OpenPty(const char *aFile, const char *aConfig)
execl(getenv("SHELL"), getenv("SHELL"), "-c", cmd, NULL);
perror("open pty failed");
exit(EXIT_FAILURE);
}
else
@@ -382,36 +380,26 @@ void RadioSpinel::Init(const char *aRadioFile, const char *aRadioConfig)
if (S_ISCHR(st.st_mode))
{
mSockFd = OpenUart(aRadioFile, aRadioConfig);
VerifyOrExit(mSockFd != -1, error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(error = SendReset());
}
#if OPENTHREAD_CONFIG_POSIX_APP_ENABLE_PTY_DEVICE
else if (S_ISREG(st.st_mode))
{
mSockFd = OpenPty(aRadioFile, aRadioConfig);
VerifyOrExit(mSockFd != -1, error = OT_ERROR_INVALID_ARGS);
}
#endif // OPENTHREAD_CONFIG_POSIX_APP_ENABLE_PTY_DEVICE
VerifyOrExit(mSockFd != -1, error = OT_ERROR_INVALID_ARGS);
error = SendReset();
VerifyOrExit(error == OT_ERROR_NONE);
error = WaitResponse();
VerifyOrExit(error == OT_ERROR_NONE);
SuccessOrExit(error = WaitResponse());
VerifyOrExit(mIsReady, error = OT_ERROR_FAILED);
if (OT_ERROR_NONE != Get(SPINEL_PROP_HWADDR, SPINEL_DATATYPE_UINT64_S, &NODE_ID))
{
exit(EXIT_FAILURE);
}
assert(mSockFd != -1);
SuccessOrExit(error = Get(SPINEL_PROP_HWADDR, SPINEL_DATATYPE_UINT64_S, &NODE_ID));
mRxRadioFrame.mPsdu = mRxPsdu;
mTxRadioFrame.mPsdu = mTxPsdu;
exit:
if (error != OT_ERROR_NONE)
{
exit(EXIT_FAILURE);
}
SuccessOrDie(error);
}
void RadioSpinel::Deinit(void)
@@ -670,6 +658,13 @@ exit:
return error;
}
void RadioSpinel::DecodeHdlc(const uint8_t *aData, uint16_t aLength)
{
mIsDecoding = true;
mHdlcDecoder.Decode(aData, aLength);
mIsDecoding = false;
}
void RadioSpinel::ReadAll(void)
{
uint8_t buf[kMaxSpinelFrame];
@@ -686,9 +681,7 @@ void RadioSpinel::ReadAll(void)
if (rval > 0)
{
mIsDecoding = true;
mHdlcDecoder.Decode(buf, static_cast<uint16_t>(rval));
mIsDecoding = false;
DecodeHdlc(buf, static_cast<uint16_t>(rval));
}
}
@@ -990,11 +983,34 @@ otError RadioSpinel::WaitResponse(void)
struct timeval now;
struct timeval timeout = {kMaxWaitTime / 1000, (kMaxWaitTime % 1000) * 1000};
gettimeofday(&now, NULL);
otSysGetTime(&now);
timeradd(&now, &timeout, &end);
do
{
#if OPENTHREAD_POSIX_VIRTUAL_TIME
struct Event event;
otSimSendSleepEvent(&timeout);
otSimReceiveEvent(&event);
switch (event.mEvent)
{
case OT_SIM_EVENT_RADIO_SPINEL_WRITE:
DecodeHdlc(event.mData, event.mDataLength);
break;
case OT_SIM_EVENT_ALARM_FIRED:
FreeTid(mWaitingTid);
mWaitingTid = 0;
ExitNow(mError = OT_ERROR_RESPONSE_TIMEOUT);
break;
default:
assert(false);
break;
}
#else // OPENTHREAD_POSIX_VIRTUAL_TIME
fd_set read_fds;
fd_set error_fds;
int rval;
@@ -1014,6 +1030,7 @@ otError RadioSpinel::WaitResponse(void)
}
else if (FD_ISSET(mSockFd, &error_fds))
{
fprintf(stderr, "NCP error\r\n");
exit(EXIT_FAILURE);
}
else
@@ -1033,8 +1050,9 @@ otError RadioSpinel::WaitResponse(void)
perror("wait response");
exit(EXIT_FAILURE);
}
#endif // OPENTHREAD_POSIX_VIRTUAL_TIME
gettimeofday(&now, NULL);
otSysGetTime(&now);
if (timercmp(&end, &now, >))
{
timersub(&end, &now, &timeout);
@@ -1115,6 +1133,9 @@ otError RadioSpinel::WriteAll(const uint8_t *aBuffer, uint16_t aLength)
{
otError error = OT_ERROR_NONE;
#if OPENTHREAD_POSIX_VIRTUAL_TIME
otSimSendRadioSpinelWriteEvent(aBuffer, aLength);
#else
while (aLength)
{
ssize_t rval = write(mSockFd, aBuffer, aLength);
@@ -1134,8 +1155,8 @@ otError RadioSpinel::WriteAll(const uint8_t *aBuffer, uint16_t aLength)
ExitNow(error = OT_ERROR_FAILED);
}
}
exit:
#endif
return error;
}
@@ -1601,3 +1622,64 @@ int8_t otPlatRadioGetReceiveSensitivity(otInstance *aInstance)
OT_UNUSED_VARIABLE(aInstance);
return sRadioSpinel.GetReceiveSensitivity();
}
#if OPENTHREAD_POSIX_VIRTUAL_TIME
void ot::RadioSpinel::Process(const Event &aEvent)
{
if (!mFrameQueue.IsEmpty())
{
ProcessFrameQueue();
}
// The current event can be other event types
if (aEvent.mEvent == OT_SIM_EVENT_RADIO_SPINEL_WRITE)
{
mHdlcDecoder.Decode(aEvent.mData, aEvent.mDataLength);
ProcessFrameQueue();
}
if (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kDone)
{
mState = OT_RADIO_STATE_RECEIVE;
#if OPENTHREAD_ENABLE_DIAG
if (otPlatDiagModeGet())
{
otPlatDiagRadioTransmitDone(mInstance, mTransmitFrame, mTxError);
}
else
#endif
{
otPlatRadioTxDone(mInstance, mTransmitFrame, (mIsAckRequested ? &mRxRadioFrame : NULL), mTxError);
}
mTxState = kIdle;
}
if (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kIdle)
{
RadioTransmit();
}
}
void ot::RadioSpinel::Update(struct timeval &aTimeout)
{
// Prevent sleep event when transmitting
if (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kIdle)
{
aTimeout.tv_sec = 0;
aTimeout.tv_usec = 0;
}
}
void otSimRadioSpinelUpdate(struct timeval *aTimeout)
{
sRadioSpinel.Update(*aTimeout);
}
void otSimRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent)
{
sRadioSpinel.Process(*aEvent);
OT_UNUSED_VARIABLE(aInstance);
}
#endif // OPENTHREAD_POSIX_VIRTUAL_TIME
+19
View File
@@ -353,6 +353,24 @@ public:
*/
void Process(const fd_set &aReadFdSet, const fd_set &aWriteFdSet);
#if OPENTHREAD_POSIX_VIRTUAL_TIME
/**
* This method performs radio spinel processing in simulation mode.
*
* @param[in] aEvent A reference to the current received simulation event.
*
*/
void Process(const struct Event &aEvent);
/**
* This method updates the @p aTimeout for processing radio spinel in simulation mode.
*
* @param[out] aTimeout A reference to the current timeout.
*
*/
void Update(struct timeval &aTimeout);
#endif
private:
enum
{
@@ -360,6 +378,7 @@ private:
kMaxWaitTime = 2000, ///< Max time to wait for response in milliseconds.
};
void DecodeHdlc(const uint8_t *aData, uint16_t aLength);
void ReadAll(void);
otError WriteAll(const uint8_t *aBuffer, uint16_t aLength);
void ProcessFrameQueue(void);
+205
View File
@@ -0,0 +1,205 @@
/*
* Copyright (c) 2018, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* @brief
* This file implements the posix simulation.
*/
#include "platform-posix.h"
#include <stddef.h>
#include <sys/select.h>
#if OPENTHREAD_POSIX_VIRTUAL_TIME
static const int kBasePort = 18000; ///< This base port for posix app simulation.
static const int kUsPerSecond = 1000000; ///< Number of microseconds per second.
static uint64_t sNow = 0; ///< Time of simulation.
static int sSockFd = -1; ///< Socket used to communicating with simulator.
static uint16_t sPortOffset = 0; ///< Port offset for simulation.
int sNodeId = 0; ///< Node id of this simulated device.
void otSimInit(void)
{
struct sockaddr_in sockaddr;
char * offset;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
offset = getenv("PORT_OFFSET");
if (offset)
{
char *endptr;
sPortOffset = (uint16_t)strtol(offset, &endptr, 0);
if (*endptr != '\0')
{
fprintf(stderr, "Invalid PORT_OFFSET: %s\n", offset);
exit(EXIT_FAILURE);
}
sPortOffset *= WELLKNOWN_NODE_ID;
}
// node id is required for virtual time simulation
sNodeId = atoi(getenv("NODE_ID"));
sockaddr.sin_port = htons(kBasePort + sPortOffset + sNodeId);
sockaddr.sin_addr.s_addr = INADDR_ANY;
sSockFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sSockFd == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}
if (bind(sSockFd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1)
{
perror("bind");
exit(EXIT_FAILURE);
}
// fprintf(stderr, "[%s] sim fd is %d\r\n", getenv("NODE_ID"), sSockFd);
}
void otSimDeinit(void)
{
if (sSockFd != -1)
{
close(sSockFd);
sSockFd = -1;
}
}
static void otSimSendEvent(struct Event *aEvent, size_t aLength)
{
ssize_t rval;
struct sockaddr_in sockaddr;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &sockaddr.sin_addr);
sockaddr.sin_port = htons(9000 + sPortOffset);
rval = sendto(sSockFd, aEvent, aLength, 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (rval < 0)
{
perror("sendto");
exit(EXIT_FAILURE);
}
}
void otSimReceiveEvent(struct Event *aEvent)
{
ssize_t rval = recvfrom(sSockFd, aEvent, sizeof(*aEvent), 0, NULL, NULL);
if (rval < 0 || (uint16_t)rval < offsetof(struct Event, mData))
{
perror("recvfrom");
exit(EXIT_FAILURE);
}
sNow += aEvent->mDelay;
// fprintf(stderr, "--[%s] now is %lu type is %d\n", getenv("NODE_ID"), sNow, aEvent->mEvent);
}
void otSimSendSleepEvent(const struct timeval *aTimeout)
{
struct Event event;
event.mDelay = aTimeout->tv_sec * kUsPerSecond + aTimeout->tv_usec;
event.mEvent = OT_SIM_EVENT_ALARM_FIRED;
event.mDataLength = 0;
otSimSendEvent(&event, offsetof(struct Event, mData));
}
void otSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength)
{
struct Event event;
event.mDelay = 0;
event.mEvent = OT_SIM_EVENT_RADIO_SPINEL_WRITE;
event.mDataLength = aLength;
memcpy(event.mData, aData, aLength);
otSimSendEvent(&event, offsetof(struct Event, mData) + event.mDataLength);
}
void otSimUpdateFdSet(fd_set * aReadFdSet,
fd_set * aWriteFdSet,
fd_set * aErrorFdSet,
int * aMaxFd,
struct timeval *aTimeout)
{
OT_UNUSED_VARIABLE(aWriteFdSet);
OT_UNUSED_VARIABLE(aErrorFdSet);
FD_SET(sSockFd, aReadFdSet);
if (*aMaxFd < sSockFd)
{
*aMaxFd = sSockFd;
}
otSimRadioSpinelUpdate(aTimeout);
}
void otSimProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet, const fd_set *aErrorFdSet)
{
struct Event event = {0};
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aWriteFdSet);
OT_UNUSED_VARIABLE(aErrorFdSet);
if (FD_ISSET(sSockFd, aReadFdSet))
{
otSimReceiveEvent(&event);
// fprintf(stderr, "[%s] receive event\r\n", getenv("NODE_ID"));
}
otSimRadioSpinelProcess(aInstance, &event);
}
void otSimGetTime(struct timeval *aTime)
{
aTime->tv_sec = sNow / kUsPerSecond;
aTime->tv_usec = sNow % kUsPerSecond;
}
#endif // OPENTHREAD_POSIX_VIRTUAL_TIME
+95 -15
View File
@@ -53,7 +53,7 @@ extern bool gPlatformPseudoResetWasRequested;
int gArgumentsCount = 0;
char **gArguments = NULL;
void PrintUsage(const char *aArg0)
static void PrintUsage(const char *aArg0)
{
fprintf(stderr, "Syntax:\n %s [-s TimeSpeedUpFactor] {NodeId|Device DeviceConfig|Command CommandArgs}\n", aArg0);
exit(EXIT_FAILURE);
@@ -109,6 +109,9 @@ void otSysInit(int aArgCount, char *aArgVector[])
gArgumentsCount = aArgCount;
gArguments = aArgVector;
#if OPENTHREAD_POSIX_VIRTUAL_TIME
otSimInit();
#endif
platformAlarmInit(speedUpFactor);
platformRadioInit(radioFile, radioConfig);
platformRandomInit();
@@ -121,25 +124,65 @@ bool otSysPseudoResetWasRequested(void)
void otSysDeinit(void)
{
#if OPENTHREAD_POSIX_VIRTUAL_TIME
otSimDeinit();
#endif
platformRadioDeinit();
}
void otSysProcessDrivers(otInstance *aInstance)
#if OPENTHREAD_POSIX_VIRTUAL_TIME
/**
* This function try selecting the given file descriptors in nonblocking mode.
*
* @param[inout] aReadFdSet A pointer to the read file descriptors.
* @param[inout] aWriteFdSet A pointer to the write file descriptors.
* @param[inout] aErrorFdSet A pointer to the error file descriptors.
* @param[in] aMaxFd The max file descriptor.
*
* @returns The value returned from select().
*
*/
static int trySelect(fd_set *aReadFdSet, fd_set *aWriteFdSet, fd_set *aErrorFdSet, int aMaxFd)
{
fd_set read_fds;
fd_set write_fds;
fd_set error_fds;
struct timeval timeout;
int max_fd = -1;
struct timeval timeout = {0, 0};
fd_set originReadFdSet = *aReadFdSet;
fd_set originWriteFdSet = *aWriteFdSet;
fd_set originErrorFdSet = *aErrorFdSet;
int rval;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&error_fds);
rval = select(aMaxFd + 1, aReadFdSet, aWriteFdSet, aErrorFdSet, &timeout);
if (rval == 0)
{
*aReadFdSet = originReadFdSet;
*aWriteFdSet = originWriteFdSet;
*aErrorFdSet = originErrorFdSet;
}
return rval;
}
#endif // OPENTHREAD_POSIX_VIRTUAL_TIME
void otSysProcessDrivers(otInstance *aInstance)
{
fd_set readFdSet;
fd_set writeFdSet;
fd_set errorFdSet;
struct timeval timeout;
int maxFd = -1;
int rval;
FD_ZERO(&readFdSet);
FD_ZERO(&writeFdSet);
FD_ZERO(&errorFdSet);
platformAlarmUpdateTimeout(&timeout);
platformUartUpdateFdSet(&read_fds, &write_fds, &error_fds, &max_fd);
platformRadioUpdateFdSet(&read_fds, &write_fds, &max_fd, &timeout);
platformUartUpdateFdSet(&readFdSet, &writeFdSet, &errorFdSet, &maxFd);
#if OPENTHREAD_POSIX_VIRTUAL_TIME
otSimUpdateFdSet(&readFdSet, &writeFdSet, &errorFdSet, &maxFd, &timeout);
#else
platformRadioUpdateFdSet(&readFdSet, &writeFdSet, &maxFd, &timeout);
#endif
if (otTaskletsArePending(aInstance))
{
@@ -147,7 +190,40 @@ void otSysProcessDrivers(otInstance *aInstance)
timeout.tv_usec = 0;
}
rval = select(max_fd + 1, &read_fds, &write_fds, &error_fds, &timeout);
#if OPENTHREAD_POSIX_VIRTUAL_TIME
if (timerisset(&timeout))
{
// Make sure there are no data ready in UART
rval = trySelect(&readFdSet, &writeFdSet, &errorFdSet, maxFd);
if (rval == 0)
{
bool noWrite = true;
// If there are write requests, the device is supposed to wake soon
for (int i = 0; i < maxFd + 1; ++i)
{
if (FD_ISSET(i, &writeFdSet))
{
noWrite = false;
break;
}
}
if (noWrite)
{
otSimSendSleepEvent(&timeout);
}
rval = select(maxFd + 1, &readFdSet, &writeFdSet, &errorFdSet, NULL);
assert(rval > 0);
}
}
else
#endif
{
rval = select(maxFd + 1, &readFdSet, &writeFdSet, &errorFdSet, &timeout);
}
if ((rval < 0) && (errno != EINTR))
{
@@ -155,7 +231,11 @@ void otSysProcessDrivers(otInstance *aInstance)
exit(EXIT_FAILURE);
}
platformUartProcess(&read_fds, &write_fds, &error_fds);
platformRadioProcess(aInstance, &read_fds, &write_fds);
#if OPENTHREAD_POSIX_VIRTUAL_TIME
otSimProcess(aInstance, &readFdSet, &writeFdSet, &errorFdSet);
#else
platformRadioProcess(aInstance, &readFdSet, &writeFdSet);
#endif
platformUartProcess(&readFdSet, &writeFdSet, &errorFdSet);
platformAlarmProcess(aInstance);
}
+12 -7
View File
@@ -237,15 +237,20 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co
{
rval = read(s_in_fd, s_receive_buffer, sizeof(s_receive_buffer));
if (rval < 0)
{
perror("read");
exit(EXIT_FAILURE);
}
else if (rval > 0)
if (rval > 0)
{
otPlatUartReceived(s_receive_buffer, (uint16_t)rval);
}
else if (rval < 0)
{
perror("UART read");
exit(EXIT_FAILURE);
}
else
{
fprintf(stderr, "UART ended\r\n");
exit(0);
}
}
if ((s_write_length > 0) && (FD_ISSET(s_out_fd, aWriteFdSet)))
@@ -254,7 +259,7 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co
if (rval <= 0)
{
perror("write");
perror("UART write");
exit(EXIT_FAILURE);
}
@@ -100,6 +100,6 @@ class Cert_5_1_07_MaxChildCount(unittest.TestCase):
if addr[0:4] != 'fe80' and 'ff:fe00' not in addr:
self.assertTrue(self.nodes[LEADER].ping(addr, size=106))
break
if __name__ == '__main__':
unittest.main()
@@ -75,7 +75,6 @@ class Cert_6_3_2_NetworkDataUpdate(unittest.TestCase):
self.simulator.go(5)
addrs = self.nodes[ED].get_addrs()
self.simulator.go(5)
self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
for addr in addrs:
if addr[0:10] == '2001:2:0:1':
@@ -56,7 +56,7 @@ class Cert_6_5_1_ChildResetSynchronize(unittest.TestCase):
def _setUpEd(self):
self.nodes[ED].add_whitelist(self.nodes[LEADER].get_addr64())
self.nodes[ED].enable_whitelist()
self.nodes[ED].enable_whitelist()
def tearDown(self):
for node in list(self.nodes.values()):
+1
View File
@@ -40,6 +40,7 @@ import config
class Node:
def __init__(self, nodeid, is_mtd=False, simulator=None):
self.simulator = simulator
self.interface = None
if sys.platform != 'win32':
self.interface = node_cli.otCli(nodeid, is_mtd, simulator=simulator)
+134 -111
View File
@@ -32,6 +32,7 @@ import sys
import time
import pexpect
import re
import socket
import ipaddress
import config
@@ -69,6 +70,7 @@ class otCli:
if 'RADIO_DEVICE' in os.environ:
cmd += ' %s' % os.environ['RADIO_DEVICE']
os.environ['NODE_ID'] = str(nodeid)
cmd += ' %d' % nodeid
print ("%s" % cmd)
@@ -76,13 +78,20 @@ class otCli:
self.pexpect = pexpect.spawn(cmd, timeout=4)
# Add delay to ensure that the process is ready to receive commands.
time.sleep(0.2)
timeout = 0.4
while timeout > 0:
self.pexpect.send('\r\n')
try:
self.pexpect.expect('> ', timeout=0.1)
break
except pexpect.TIMEOUT:
timeout -= 0.1
def __init_ncp_sim(self, nodeid, mode):
""" Initialize an NCP simulation node. """
if 'RADIO_DEVICE' in os.environ:
args = ' %s' % os.environ['RADIO_DEVICE']
os.environ['NODE_ID'] = str(nodeid)
else:
args = ''
@@ -97,10 +106,28 @@ class otCli:
print ("%s" % cmd)
self.pexpect = pexpect.spawn(cmd, timeout=4)
# Add delay to ensure that the process is ready to receive commands.
time.sleep(0.2)
self.pexpect.expect('spinel-cli >')
self._expect('spinel-cli >')
self.debug(int(os.getenv('DEBUG', '0')))
def _expect(self, pattern, timeout=-1, *args, **kwargs):
""" Process simulator events until expected the pattern. """
if timeout == -1:
timeout = self.pexpect.timeout
assert timeout > 0
while timeout > 0:
try:
return self.pexpect.expect(pattern, 0.1, *args, **kwargs)
except pexpect.TIMEOUT:
timeout -= 0.1
self.simulator.go(0)
if timeout <= 0:
raise
def __init_soc(self, nodeid):
""" Initialize a System-on-a-chip node connected via UART. """
import fdpexpect
@@ -112,26 +139,27 @@ class otCli:
def destroy(self):
if self.pexpect and self.pexpect.isalive():
self.send_command('exit')
self.pexpect.expect(pexpect.EOF)
print("%d: exit" % self.nodeid)
self.pexpect.send('exit\n')
self.pexpect.terminate()
self.pexpect.close(force=True)
self._expect(pexpect.EOF)
self.pexpect.wait()
self.pexpect = None
sys.stdout.flush()
def send_command(self, cmd):
def send_command(self, cmd, go=True):
print("%d: %s" % (self.nodeid, cmd))
self.pexpect.send(cmd + '\n')
if go:
self.simulator.go(0, nodeid=self.nodeid)
sys.stdout.flush()
if isinstance(self.simulator, simulator.VirtualTime):
self.simulator.receive_events()
def get_commands(self):
self.send_command('?')
self.pexpect.expect('Commands:')
self._expect('Commands:')
commands = []
while True:
i = self.pexpect.expect(['Done', '(\S+)'])
i = self._expect(['Done', '(\S+)'])
if i != 0:
commands.append(self.pexpect.match.groups()[0])
else:
@@ -141,56 +169,57 @@ class otCli:
def set_mode(self, mode):
cmd = 'mode ' + mode
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def debug(self, level):
self.send_command('debug '+str(level))
# `debug` command will not trigger interaction with simulator
self.send_command('debug '+ str(level), go=False)
def interface_up(self):
self.send_command('ifconfig up')
self.pexpect.expect('Done')
self._expect('Done')
def interface_down(self):
self.send_command('ifconfig down')
self.pexpect.expect('Done')
self._expect('Done')
def thread_start(self):
self.send_command('thread start')
self.pexpect.expect('Done')
self._expect('Done')
def thread_stop(self):
self.send_command('thread stop')
self.pexpect.expect('Done')
self._expect('Done')
def commissioner_start(self):
cmd = 'commissioner start'
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def commissioner_add_joiner(self, addr, psk):
cmd = 'commissioner joiner add ' + addr + ' ' + psk
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def joiner_start(self, pskd='', provisioning_url=''):
cmd = 'joiner start ' + pskd + ' ' + provisioning_url
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def clear_whitelist(self):
cmd = 'macfilter addr clear'
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def enable_whitelist(self):
cmd = 'macfilter addr whitelist'
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def disable_whitelist(self):
cmd = 'macfilter addr disable'
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def add_whitelist(self, addr, rssi=None):
cmd = 'macfilter addr add ' + addr
@@ -199,19 +228,19 @@ class otCli:
cmd += ' ' + str(rssi)
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def remove_whitelist(self, addr):
cmd = 'macfilter addr remove ' + addr
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_addr16(self):
self.send_command('rloc16')
i = self.pexpect.expect('([0-9a-fA-F]{4})')
i = self._expect('([0-9a-fA-F]{4})')
if i == 0:
addr16 = int(self.pexpect.match.groups()[0], 16)
self.pexpect.expect('Done')
self._expect('Done')
return addr16
def get_router_id(self):
@@ -220,83 +249,83 @@ class otCli:
def get_addr64(self):
self.send_command('extaddr')
i = self.pexpect.expect('([0-9a-fA-F]{16})')
i = self._expect('([0-9a-fA-F]{16})')
if i == 0:
addr64 = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
self._expect('Done')
return addr64
def get_eui64(self):
self.send_command('eui64')
i = self.pexpect.expect('([0-9a-fA-F]{16})')
i = self._expect('([0-9a-fA-F]{16})')
if i == 0:
addr64 = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
self._expect('Done')
return addr64
def get_joiner_id(self):
self.send_command('joinerid')
i = self.pexpect.expect('([0-9a-fA-F]{16})')
i = self._expect('([0-9a-fA-F]{16})')
if i == 0:
addr = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
self._expect('Done')
return addr
def get_channel(self):
self.send_command('channel')
i = self.pexpect.expect('(\d+)\r\n')
i = self._expect('(\d+)\r?\n')
if i == 0:
channel = int(self.pexpect.match.groups()[0])
self.pexpect.expect('Done')
self._expect('Done')
return channel
def set_channel(self, channel):
cmd = 'channel %d' % channel
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_masterkey(self):
self.send_command('masterkey')
i = self.pexpect.expect('([0-9a-fA-F]{32})')
i = self._expect('([0-9a-fA-F]{32})')
if i == 0:
masterkey = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
self._expect('Done')
return masterkey
def set_masterkey(self, masterkey):
cmd = 'masterkey ' + masterkey
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_key_sequence_counter(self):
self.send_command('keysequence counter')
i = self.pexpect.expect('(\d+)\r\n')
i = self._expect('(\d+)\r?\n')
if i == 0:
key_sequence_counter = int(self.pexpect.match.groups()[0])
self.pexpect.expect('Done')
self._expect('Done')
return key_sequence_counter
def set_key_sequence_counter(self, key_sequence_counter):
cmd = 'keysequence counter %d' % key_sequence_counter
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def set_key_switch_guardtime(self, key_switch_guardtime):
cmd = 'keysequence guardtime %d' % key_switch_guardtime
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def set_network_id_timeout(self, network_id_timeout):
cmd = 'networkidtimeout %d' % network_id_timeout
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_network_name(self):
self.send_command('networkname')
while True:
i = self.pexpect.expect(['Done', '(\S+)'])
i = self._expect(['Done', '(\S+)'])
if i != 0:
network_name = self.pexpect.match.groups()[0].decode('utf-8')
else:
@@ -306,103 +335,103 @@ class otCli:
def set_network_name(self, network_name):
cmd = 'networkname ' + network_name
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_panid(self):
self.send_command('panid')
i = self.pexpect.expect('([0-9a-fA-F]{4})')
i = self._expect('([0-9a-fA-F]{4})')
if i == 0:
panid = int(self.pexpect.match.groups()[0], 16)
self.pexpect.expect('Done')
self._expect('Done')
return panid
def set_panid(self, panid):
cmd = 'panid %d' % panid
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_partition_id(self):
self.send_command('leaderpartitionid')
i = self.pexpect.expect('(\d+)\r\n')
i = self._expect('(\d+)\r?\n')
if i == 0:
weight = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
self._expect('Done')
return weight
def set_partition_id(self, partition_id):
cmd = 'leaderpartitionid %d' % partition_id
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def set_router_upgrade_threshold(self, threshold):
cmd = 'routerupgradethreshold %d' % threshold
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def set_router_downgrade_threshold(self, threshold):
cmd = 'routerdowngradethreshold %d' % threshold
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def release_router_id(self, router_id):
cmd = 'releaserouterid %d' % router_id
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_state(self):
states = ['detached', 'child', 'router', 'leader']
self.send_command('state')
match = self.pexpect.expect(states)
self.pexpect.expect('Done')
match = self._expect(states)
self._expect('Done')
return states[match]
def set_state(self, state):
cmd = 'state ' + state
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_timeout(self):
self.send_command('childtimeout')
i = self.pexpect.expect('(\d+)\r\n')
i = self._expect('(\d+)\r?\n')
if i == 0:
timeout = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
self._expect('Done')
return timeout
def set_timeout(self, timeout):
cmd = 'childtimeout %d' % timeout
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def set_max_children(self, number):
cmd = 'childmax %d' % number
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_weight(self):
self.send_command('leaderweight')
i = self.pexpect.expect('(\d+)\r\n')
i = self._expect('(\d+)\r?\n')
if i == 0:
weight = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
self._expect('Done')
return weight
def set_weight(self, weight):
cmd = 'leaderweight %d' % weight
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def add_ipaddr(self, ipaddr):
cmd = 'ipaddr add ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def get_addrs(self):
addrs = []
self.send_command('ipaddr')
while True:
i = self.pexpect.expect(['(\S+:\S+)\r\n', 'Done'])
i = self._expect(['(\S+(:\S*)+)\r?\n', 'Done'])
if i == 0:
addrs.append(self.pexpect.match.groups()[0].decode("utf-8"))
elif i == 1:
@@ -426,7 +455,7 @@ class otCli:
self.send_command('eidcache')
while True:
i = self.pexpect.expect(['([a-fA-F0-9\:]+) ([a-fA-F0-9]+)\r\n', 'Done'])
i = self._expect(['([a-fA-F0-9\:]+) ([a-fA-F0-9]+)\r?\n', 'Done'])
if i == 0:
eid = self.pexpect.match.groups()[0].decode("utf-8")
rloc = self.pexpect.match.groups()[1].decode("utf-8")
@@ -439,12 +468,12 @@ class otCli:
def add_service(self, enterpriseNumber, serviceData, serverData):
cmd = 'service add ' + enterpriseNumber + ' ' + serviceData+ ' ' + serverData
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def remove_service(self, enterpriseNumber, serviceData):
cmd = 'service remove ' + enterpriseNumber + ' ' + serviceData
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def __getLinkLocalAddress(self):
for ip6Addr in self.get_addrs():
@@ -515,40 +544,40 @@ class otCli:
def get_context_reuse_delay(self):
self.send_command('contextreusedelay')
i = self.pexpect.expect('(\d+)\r\n')
i = self._expect('(\d+)\r?\n')
if i == 0:
timeout = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
self._expect('Done')
return timeout
def set_context_reuse_delay(self, delay):
cmd = 'contextreusedelay %d' % delay
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def add_prefix(self, prefix, flags, prf = 'med'):
cmd = 'prefix add ' + prefix + ' ' + flags + ' ' + prf
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def remove_prefix(self, prefix):
cmd = 'prefix remove ' + prefix
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def add_route(self, prefix, prf = 'med'):
cmd = 'route add ' + prefix + ' ' + prf
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def remove_route(self, prefix):
cmd = 'route remove ' + prefix
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def register_netdata(self):
self.send_command('netdataregister')
self.pexpect.expect('Done')
self._expect('Done')
def energy_scan(self, mask, count, period, scan_duration, ipaddr):
cmd = 'commissioner energy ' + str(mask) + ' ' + str(count) + ' ' + str(period) + ' ' + str(scan_duration) + ' ' + ipaddr
@@ -560,7 +589,7 @@ class otCli:
else:
timeout = 8
self.pexpect.expect('Energy:', timeout=timeout)
self._expect('Energy:', timeout=timeout)
def panid_query(self, panid, mask, ipaddr):
cmd = 'commissioner panid ' + str(panid) + ' ' + str(mask) + ' ' + ipaddr
@@ -572,14 +601,14 @@ class otCli:
else:
timeout = 8
self.pexpect.expect('Conflict:', timeout=timeout)
self._expect('Conflict:', timeout=timeout)
def scan(self):
self.send_command('scan')
results = []
while True:
i = self.pexpect.expect(['\|\s(\S+)\s+\|\s(\S+)\s+\|\s([0-9a-fA-F]{4})\s\|\s([0-9a-fA-F]{16})\s\|\s(\d+)\r\n',
i = self._expect(['\|\s(\S+)\s+\|\s(\S+)\s+\|\s([0-9a-fA-F]{4})\s\|\s([0-9a-fA-F]{16})\s\|\s(\d+)\r?\n',
'Done'])
if i == 0:
results.append(self.pexpect.match.groups())
@@ -595,11 +624,6 @@ class otCli:
self.send_command(cmd)
try:
self.pexpect.expect('Done', timeout=0.01)
except pexpect.TIMEOUT:
pass
if isinstance(self.simulator, simulator.VirtualTime):
self.simulator.go(timeout)
@@ -607,87 +631,86 @@ class otCli:
try:
responders = {}
while len(responders) < num_responses:
i = self.pexpect.expect(['from (\S+):'])
i = self._expect(['from (\S+):'])
if i == 0:
responders[self.pexpect.match.groups()[0]] = 1
self.pexpect.expect('\n')
except pexpect.TIMEOUT:
self._expect('\n')
except (pexpect.TIMEOUT, socket.timeout):
result = False
if isinstance(self.simulator, simulator.VirtualTime):
self.simulator.sync_devices()
return result
def reset(self):
self.send_command('reset')
try:
self.pexpect.expect('Done', timeout=0.01)
except pexpect.TIMEOUT:
pass
time.sleep(0.1)
def set_router_selection_jitter(self, jitter):
cmd = 'routerselectionjitter %d' % jitter
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def set_active_dataset(self, timestamp, panid=None, channel=None, channel_mask=None, master_key=None):
self.send_command('dataset clear')
self.pexpect.expect('Done')
self._expect('Done')
cmd = 'dataset activetimestamp %d' % timestamp
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
if panid != None:
cmd = 'dataset panid %d' % panid
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
if channel != None:
cmd = 'dataset channel %d' % channel
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
if channel_mask != None:
cmd = 'dataset channelmask %d' % channel_mask
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
if master_key != None:
cmd = 'dataset masterkey ' + master_key
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
self.send_command('dataset commit active')
self.pexpect.expect('Done')
self._expect('Done')
def set_pending_dataset(self, pendingtimestamp, activetimestamp, panid=None, channel=None):
self.send_command('dataset clear')
self.pexpect.expect('Done')
self._expect('Done')
cmd = 'dataset pendingtimestamp %d' % pendingtimestamp
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
cmd = 'dataset activetimestamp %d' % activetimestamp
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
if panid != None:
cmd = 'dataset panid %d' % panid
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
if channel != None:
cmd = 'dataset channel %d' % channel
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
self.send_command('dataset commit pending')
self.pexpect.expect('Done')
self._expect('Done')
def announce_begin(self, mask, count, period, ipaddr):
cmd = 'commissioner announce ' + str(mask) + ' ' + str(count) + ' ' + str(period) + ' ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def send_mgmt_active_set(self, active_timestamp=None, channel=None, channel_mask=None, extended_panid=None,
panid=None, master_key=None, mesh_local=None, network_name=None, binary=None):
@@ -721,7 +744,7 @@ class otCli:
cmd += 'binary ' + binary + ' '
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
def send_mgmt_pending_set(self, pending_timestamp=None, active_timestamp=None, delay_timer=None, channel=None,
panid=None, master_key=None, mesh_local=None, network_name=None):
@@ -751,4 +774,4 @@ class otCli:
cmd += 'networkname ' + network_name + ' '
self.send_command(cmd)
self.pexpect.expect('Done')
self._expect('Done')
+190 -62
View File
@@ -27,19 +27,25 @@
# POSSIBILITY OF SUCH DAMAGE.
#
import binascii
import bisect
import cmd
import os
import socket
import struct
import time
import sys
import traceback
import time
import io
import config
import message
import pcap
def dbg_print(*args):
if False:
print args
class RealTime:
def __init__(self):
@@ -60,9 +66,29 @@ class RealTime:
class VirtualTime:
OT_SIM_EVENT_ALARM_FIRED = 0
OT_SIM_EVENT_RADIO_RECEIVED = 1
OT_SIM_EVENT_UART_WRITE = 2
OT_SIM_EVENT_RADIO_SPINEL_WRITE = 3
OT_SIM_EVENT_POSTCMD = 4
EVENT_TIME = 0
EVENT_SEQUENCE = 1
EVENT_ADDR = 2
EVENT_TYPE = 3
EVENT_DATA_LENGTH = 4
EVENT_DATA = 5
BASE_PORT = 9000
MAX_NODES = 34
PORT_OFFSET = int(os.getenv('PORT_OFFSET', "0"))
MAX_MESSAGE = 1024
END_OF_TIME = 0x7fffffff
PORT_OFFSET = int(os.getenv('PORT_OFFSET', '0'))
BLOCK_TIMEOUT = 4
RADIO_ONLY = os.getenv('RADIO_DEVICE') != None
NCP_SIM = os.getenv('NODE_TYPE', 'sim') == 'ncp-sim'
def __init__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -73,11 +99,17 @@ class VirtualTime:
self.devices = {}
self.event_queue = []
self.event_count = 0
# there could be events scheduled at exactly the same time
self.event_sequence = 0
self.current_time = 0
self.current_event = None;
self.current_event = None
self.awake_devices = set()
self._pcap = pcap.PcapCodec(os.getenv('TEST_NAME', 'current'))
# the addr for spinel-cli sending OT_SIM_EVENT_POSTCMD
self._spinel_cli_addr = (ip, self.BASE_PORT + self.port)
self.current_nodeid = None
self._pause_time = 0
self._message_factory = config.create_default_thread_message_factory()
@@ -102,7 +134,6 @@ class VirtualTime:
except Exception as e:
# Just print the exception to the console
print("EXCEPTION: %s" % e)
pass
def set_lowpan_context(self, cid, prefix):
self._message_factory.set_lowpan_context(cid, prefix)
@@ -125,24 +156,63 @@ class VirtualTime:
return message.MessagesSet(messages)
def receive_events(self):
def _is_radio(self, addr):
return addr[1] < self.BASE_PORT * 2
if self.current_event is None:
self.sock.setblocking(0)
def _to_core_addr(self, addr):
assert self._is_radio(addr)
return (addr[0], addr[1] + self.BASE_PORT)
def _to_radio_addr(self, addr):
assert not self._is_radio(addr)
return (addr[0], addr[1] - self.BASE_PORT)
def _core_addr_from(self, nodeid):
if self.RADIO_ONLY:
return ('127.0.0.1', self.BASE_PORT + self.port + nodeid)
else:
self.sock.setblocking(1)
return ('127.0.0.1', self.port + nodeid)
def _next_event_time(self):
if len(self.event_queue) == 0:
return self.END_OF_TIME
else:
return self.event_queue[0][0]
def receive_events(self):
""" Receive events until all devices are asleep. """
while True:
try:
msg, addr = self.sock.recvfrom(1024)
except socket.error:
break
if self.current_event or len(self.awake_devices) or (self._next_event_time() > self._pause_time and self.current_nodeid):
self.sock.settimeout(self.BLOCK_TIMEOUT)
try:
msg, addr = self.sock.recvfrom(self.MAX_MESSAGE)
except socket.error:
# print debug information on failure
print('Current nodeid:')
print(self.current_nodeid)
print('Current awake:')
print(self.awake_devices)
print('Current time:')
print(self.current_time)
print('Current event:')
print(self.current_event)
print('Events:')
for event in self.event_queue:
print(event)
raise
else:
self.sock.settimeout(0)
try:
msg, addr = self.sock.recvfrom(self.MAX_MESSAGE)
except socket.error:
break
if addr not in self.devices:
if addr != self._spinel_cli_addr and addr not in self.devices:
self.devices[addr] = {}
self.devices[addr]['alarm'] = None
self.devices[addr]['msgs'] = []
self.devices[addr]['time'] = self.current_time
self.awake_devices.discard(addr)
#print "New device:", addr, self.devices
delay, type, datalen = struct.unpack('=QBH', msg[:11])
@@ -150,31 +220,37 @@ class VirtualTime:
event_time = self.current_time + delay
if type == 0:
if data:
dbg_print("New event: ", event_time, addr, type, datalen, binascii.hexlify(data))
else:
dbg_print("New event: ", event_time, addr, type, datalen)
if type == self.OT_SIM_EVENT_ALARM_FIRED:
# remove any existing alarm event for device
try:
if self.devices[addr]['alarm']:
self.event_queue.remove(self.devices[addr]['alarm'])
#print "-- Remove\t", self.devices[addr]['alarm']
self.devices[addr]['alarm'] = None
except ValueError:
pass
# add alarm event to event queue
event = (event_time, addr, type, datalen)
event = (event_time, self.event_sequence, addr, type, datalen)
self.event_sequence += 1
#print "-- Enqueue\t", event, delay, self.current_time
bisect.insort(self.event_queue, event)
self.devices[addr]['alarm'] = event
if self.current_event is not None and self.current_event[1] == addr:
self.awake_devices.discard(addr)
if self.current_event and self.current_event[self.EVENT_ADDR] == addr:
#print "Done\t", self.current_event
self.current_event = None
return
elif type == 1:
elif type == self.OT_SIM_EVENT_RADIO_RECEIVED:
assert self._is_radio(addr)
# add radio receive events event queue
for device in self.devices:
if device != addr:
event = (event_time, device, type, datalen, data)
if device != addr and self._is_radio(device):
event = (event_time, self.event_sequence, device, type, datalen, data)
self.event_sequence += 1
#print "-- Enqueue\t", event
bisect.insort(self.event_queue, event)
@@ -182,34 +258,67 @@ class VirtualTime:
self._add_message(addr[1] - self.port, data)
# add radio transmit done events to event queue
event = (event_time, addr, type, datalen, data)
event = (event_time, self.event_sequence, addr, type, datalen, data)
self.event_sequence += 1
bisect.insort(self.event_queue, event)
self.awake_devices.add(addr)
elif type == self.OT_SIM_EVENT_RADIO_SPINEL_WRITE:
assert not self._is_radio(addr)
radio_addr = self._to_radio_addr(addr)
if not self.devices.has_key(radio_addr):
self.awake_devices.add(radio_addr)
event = (event_time, self.event_sequence, radio_addr, self.OT_SIM_EVENT_UART_WRITE, datalen, data)
self.event_sequence += 1
bisect.insort(self.event_queue, event)
self.awake_devices.add(addr)
elif type == self.OT_SIM_EVENT_UART_WRITE:
assert self._is_radio(addr)
core_addr = self._to_core_addr(addr)
if not self.devices.has_key(core_addr):
self.awake_devices.add(core_addr)
event = (event_time, self.event_sequence, core_addr, self.OT_SIM_EVENT_RADIO_SPINEL_WRITE, datalen, data)
self.event_sequence += 1
bisect.insort(self.event_queue, event)
self.awake_devices.add(addr)
elif type == self.OT_SIM_EVENT_POSTCMD:
assert self.current_time == self._pause_time
nodeid = struct.unpack('=B', data)[0]
if self.current_nodeid == nodeid:
self.current_nodeid = None
def _send_message(self, message, addr):
while True:
try:
sent = self.sock.sendto(message, addr)
except socket.error:
traceback.print_exc()
time.sleep(0)
else:
break
assert sent == len(message)
def process_next_event(self):
if self.current_event != None:
return
#print "Events", len(self.event_queue)
count = 0
for event in self.event_queue:
#print count, event
count += 1
assert self.current_event is None
assert self._next_event_time() < self.END_OF_TIME
# process next event
try:
event = self.event_queue.pop(0)
except IndexError:
return
event = self.event_queue.pop(0)
#print "Pop\t", event
if len(event) == 4:
event_time, addr, type, datalen = event
if len(event) == 5:
event_time, sequence, addr, type, datalen = event
dbg_print("Pop event: ", event_time, addr, type, datalen)
else:
event_time, addr, type, datalen, data = event
event_time, sequence, addr, type, datalen, data = event
dbg_print("Pop event: ", event_time, addr, type, datalen, binascii.hexlify(data))
self.event_count += 1
self.current_event = event
assert(event_time >= self.current_time)
@@ -220,33 +329,52 @@ class VirtualTime:
message = struct.pack('=QBH', elapsed, type, datalen)
if type == 0:
if type == self.OT_SIM_EVENT_ALARM_FIRED:
self.devices[addr]['alarm'] = None
self.sock.sendto(message, addr)
elif type == 1:
self._send_message(message, addr)
elif type == self.OT_SIM_EVENT_RADIO_RECEIVED:
message += data
self.sock.sendto(message, addr)
self._send_message(message, addr)
elif type == self.OT_SIM_EVENT_RADIO_SPINEL_WRITE:
message += data
self._send_message(message, addr)
elif type == self.OT_SIM_EVENT_UART_WRITE:
message += data
self._send_message(message, addr)
def sync_devices(self):
self.current_time = self._pause_time
for addr in self.devices:
elapsed = self.current_time - self.devices[addr]['time']
if elapsed == 0:
continue
dbg_print('syncing', addr, elapsed)
self.devices[addr]['time'] = self.current_time
message = struct.pack('=QBH', elapsed, 0, 0)
self.sock.sendto(message, addr)
def go(self, duration):
message = struct.pack('=QBH', elapsed, self.OT_SIM_EVENT_ALARM_FIRED, 0)
self._send_message(message, addr)
self.awake_devices.add(addr)
self.receive_events()
self.awake_devices.clear()
def go(self, duration, nodeid=None):
assert self.current_time == self._pause_time
duration = int(duration) * 1000000
start_time = self.current_time
self.current_event = None
print "running for %d us" % duration
dbg_print('running for %d us' % duration)
self._pause_time += duration
if nodeid:
if self.NCP_SIM:
self.current_nodeid = nodeid
self.awake_devices.add(self._core_addr_from(nodeid))
self.receive_events()
while (self.current_time - start_time) < duration:
while self._next_event_time() <= self._pause_time:
self.process_next_event()
self.receive_events()
if duration > 0:
self.sync_devices()
dbg_print('current time %d us' % self.current_time)
self.sync_devices()
if __name__ == '__main__':
simulator = VirtualTime()
while True:
simulator.go(0)
+1
View File
@@ -157,6 +157,7 @@ TESTS = \
# made available to all programs and scripts in TESTS.
TESTS_ENVIRONMENT = \
top_srcdir='$(top_srcdir)' \
$(NULL)
# Source, compiler, and linker options for test programs.
+17
View File
@@ -29,7 +29,10 @@
#include <openthread/diag.h>
#include <openthread/platform/radio.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <unistd.h>
#include "utils/wrap_string.h"
@@ -231,6 +234,20 @@ void TestDiag(void)
// initialize platform layer
#if OPENTHREAD_ENABLE_POSIX_APP
char *argv[] = {(char *)"test_diag", getenv("RADIO_DEVICE"), (char *)"1"};
#if OPENTHREAD_POSIX_VIRTUAL_TIME
char simulator[128];
sprintf(simulator, "%s/tests/scripts/thread-cert/simulator.py", getenv("top_srcdir"));
if (0 == fork())
{
prctl(PR_SET_PDEATHSIG, SIGHUP);
exit(execlp("python", "python", simulator, NULL));
}
setenv("NODE_ID", "1", 0);
sleep(2);
#endif // OPENTHREAD_POSIX_VIRTUAL_TIME
#else
char *argv[] = {(char *)"test_diag", (char *)"1"};
#endif
+1 -1
View File
@@ -112,7 +112,7 @@ while true; do
if [ $? -eq 0 ]; then
break
fi
OFFSET=$(expr $OFFSET + 1)
OFFSET=$(expr $OFFSET + 1)
done
# Run a test