Skip to content

Commit

Permalink
[test] add missing platform APIs in FakePlatform (openthread#10844)
Browse files Browse the repository at this point in the history
This commit adds some missing platform API implementation for
FakePlatform.

These are added so that FakePlatform can be used to write unit tests
in ot-br-posix.

The commit also guards the microSecondTimer code in `FakePlatform`
with the marcro `OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE` because
`otPlatAlarmMicroFired` is only defined in OT timer.cpp when
`OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE` is true.

This commit also removes some parameter names in the fake
implementation to avoid unused warnings.
  • Loading branch information
Irving-cl authored Oct 16, 2024
1 parent 31f92b6 commit a225ee9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
64 changes: 52 additions & 12 deletions tests/gtest/fake_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <string.h>
#include <sys/time.h>

#include <openthread/error.h>
#include <openthread/instance.h>
#include <openthread/tasklet.h>
#include <openthread/tcat.h>
Expand All @@ -47,7 +48,8 @@
#include <openthread/platform/logging.h>
#include <openthread/platform/misc.h>
#include <openthread/platform/toolchain.h>
#include "openthread/error.h"
#include <openthread/platform/trel.h>
#include <openthread/platform/udp.h>

using namespace ot;

Expand Down Expand Up @@ -90,6 +92,7 @@ FakePlatform::~FakePlatform()
sPlatform = nullptr;
}

#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
void FakePlatform::StartMicroAlarm(uint32_t aT0, uint32_t aDt)
{
uint64_t start = mNow;
Expand All @@ -102,6 +105,10 @@ void FakePlatform::StartMicroAlarm(uint32_t aT0, uint32_t aDt)

mMicroAlarmStart = start;
}

void FakePlatform::StopMicroAlarm() { mMicroAlarmStart = kAlarmStop; }
#endif

void FakePlatform::StartMilliAlarm(uint32_t aT0, uint32_t aDt)
{
uint64_t start = mNow - (mNow % OT_US_PER_MS);
Expand All @@ -115,21 +122,19 @@ void FakePlatform::StartMilliAlarm(uint32_t aT0, uint32_t aDt)
mMilliAlarmStart = start;
}

void FakePlatform::StopMicroAlarm() { mMicroAlarmStart = kAlarmStop; }

void FakePlatform::StopMilliAlarm() { mMilliAlarmStart = kAlarmStop; }

void FakePlatform::ProcessAlarm(uint64_t &aTimeout)
{
uint64_t end = mNow + aTimeout;

uint64_t *alarm = &end;

#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
if (mMicroAlarmStart < *alarm)
{
alarm = &mMicroAlarmStart;
}

#endif
if (mMilliAlarmStart < *alarm)
{
alarm = &mMilliAlarmStart;
Expand All @@ -141,12 +146,14 @@ void FakePlatform::ProcessAlarm(uint64_t &aTimeout)
mNow = *alarm;
}
*alarm = kAlarmStop;

#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
if (alarm == &mMicroAlarmStart)
{
otPlatAlarmMicroFired(mInstance);
}
else if (alarm == &mMilliAlarmStart)
else
#endif
if (alarm == &mMilliAlarmStart)
{
otPlatAlarmMilliFired(mInstance);
}
Expand Down Expand Up @@ -244,7 +251,7 @@ otError FakePlatform::SettingsDelete(uint16_t aKey, int aIndex)
return OT_ERROR_NOT_FOUND;
}

if (aIndex >= setting->second.size())
if (static_cast<std::size_t>(aIndex) >= setting->second.size())
{
return OT_ERROR_NOT_FOUND;
}
Expand Down Expand Up @@ -309,12 +316,14 @@ void otPlatAlarmMilliStartAt(otInstance *, uint32_t aT0, uint32_t aDt)
}

uint32_t otPlatAlarmMilliGetNow(void) { return FakePlatform::CurrentPlatform().GetNow() / OT_US_PER_MS; }
void otPlatAlarmMicroStop(otInstance *) { FakePlatform::CurrentPlatform().StopMicroAlarm(); }

#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
void otPlatAlarmMicroStop(otInstance *) { FakePlatform::CurrentPlatform().StopMicroAlarm(); }
void otPlatAlarmMicroStartAt(otInstance *, uint32_t aT0, uint32_t aDt)
{
FakePlatform::CurrentPlatform().StartMicroAlarm(aT0, aDt);
}
#endif

uint64_t otPlatTimeGet(void) { return FakePlatform::CurrentPlatform().GetNow(); }
uint16_t otPlatTimeGetXtalAccuracy(void) { return 0; }
Expand Down Expand Up @@ -392,6 +401,11 @@ bool otPlatRadioIsCoexEnabled(otInstance *) { return true; }

otError otPlatRadioSetCoexEnabled(otInstance *, bool) { return OT_ERROR_NOT_IMPLEMENTED; }

otError otPlatRadioConfigureEnhAckProbing(otInstance *, otLinkMetrics, otShortAddress, const otExtAddress *)
{
return OT_ERROR_NOT_IMPLEMENTED;
}

void otPlatReset(otInstance *) {}

otPlatResetReason otPlatGetResetReason(otInstance *) { return OT_PLAT_RESET_REASON_POWER_ON; }
Expand Down Expand Up @@ -426,7 +440,7 @@ void otPlatDiagRadioReceived(otInstance *, otRadioFrame *, otError) {}

void otPlatDiagAlarmCallback(otInstance *) {}

OT_TOOL_WEAK void otPlatLog(otLogLevel aLevel, otLogRegion aRegion, const char *aFormat, ...) {}
OT_TOOL_WEAK void otPlatLog(otLogLevel, otLogRegion, const char *, ...) {}

void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }

Expand All @@ -438,7 +452,7 @@ otError otPlatInfraIfSendIcmp6Nd(uint32_t, const otIp6Address *, const uint8_t *

otError otPlatInfraIfDiscoverNat64Prefix(uint32_t) { return OT_ERROR_FAILED; }

void otPlatDsoEnableListening(otInstance *aInstance, bool) {}
void otPlatDsoEnableListening(otInstance *, bool) {}

void otPlatDsoConnect(otPlatDsoConnection *, const otSockAddr *) {}

Expand All @@ -450,7 +464,7 @@ otError otPlatBleEnable(otInstance *) { return OT_ERROR_NONE; }

otError otPlatBleDisable(otInstance *) { return OT_ERROR_NONE; }

otError otPlatBleGetAdvertisementBuffer(otInstance *, uint8_t **aAdvertisementBuffer) { return OT_ERROR_NO_BUFS; }
otError otPlatBleGetAdvertisementBuffer(otInstance *, uint8_t **) { return OT_ERROR_NO_BUFS; }

otError otPlatBleGapAdvStart(otInstance *, uint16_t) { return OT_ERROR_NONE; }

Expand All @@ -468,6 +482,10 @@ bool otPlatBleSupportsMultiRadio(otInstance *) { return false; }

otError otPlatBleGapAdvSetData(otInstance *, uint8_t *, uint16_t) { return OT_ERROR_NONE; }

void otPlatSettingsInit(otInstance *, const uint16_t *, uint16_t) {}

void otPlatSettingsDeinit(otInstance *) {}

otError otPlatSettingsGet(otInstance *, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength)
{
return FakePlatform::CurrentPlatform().SettingsGet(aKey, aIndex, aValue, aValueLength);
Expand Down Expand Up @@ -506,4 +524,26 @@ void otPlatFlashWrite(otInstance *, uint8_t aSwapIndex, uint32_t aOffset, const
FakePlatform::CurrentPlatform().FlashWrite(aSwapIndex, aOffset, aData, aSize);
}

void otPlatTrelEnable(otInstance *, uint16_t *) {}
void otPlatTrelDisable(otInstance *) {}
void otPlatTrelRegisterService(otInstance *, uint16_t, const uint8_t *, uint8_t) {}
void otPlatTrelSend(otInstance *, const uint8_t *, uint16_t, const otSockAddr *) {}
const otPlatTrelCounters *otPlatTrelGetCounters(otInstance *) { return nullptr; }
void otPlatTrelResetCounters(otInstance *) {}

otError otPlatUdpSocket(otUdpSocket *) { return OT_ERROR_NOT_IMPLEMENTED; }
otError otPlatUdpClose(otUdpSocket *) { return OT_ERROR_NOT_IMPLEMENTED; }
otError otPlatUdpBind(otUdpSocket *) { return OT_ERROR_NOT_IMPLEMENTED; }
otError otPlatUdpBindToNetif(otUdpSocket *, otNetifIdentifier) { return OT_ERROR_NOT_IMPLEMENTED; }
otError otPlatUdpConnect(otUdpSocket *) { return OT_ERROR_NOT_IMPLEMENTED; }
otError otPlatUdpSend(otUdpSocket *, otMessage *, const otMessageInfo *) { return OT_ERROR_NOT_IMPLEMENTED; }
otError otPlatUdpJoinMulticastGroup(otUdpSocket *, otNetifIdentifier, const otIp6Address *)
{
return OT_ERROR_NOT_IMPLEMENTED;
}
otError otPlatUdpLeaveMulticastGroup(otUdpSocket *, otNetifIdentifier, const otIp6Address *)
{
return OT_ERROR_NOT_IMPLEMENTED;
}

} // extern "C"
8 changes: 6 additions & 2 deletions tests/gtest/fake_platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#ifndef OT_GTEST_FAKE_PLATFORM_HPP_
#define OT_GTEST_FAKE_PLATFORM_HPP_

#include <openthread/config.h>
#include "openthread-core-config.h"

#include <map>
#include <vector>
Expand Down Expand Up @@ -69,8 +69,10 @@ class FakePlatform

virtual void StartMilliAlarm(uint32_t aT0, uint32_t aDt);
virtual void StopMilliAlarm();
#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
virtual void StartMicroAlarm(uint32_t aT0, uint32_t aDt);
virtual void StopMicroAlarm();
#endif

uint8_t GetReceiveChannel(void) const { return mChannel; }
virtual otRadioFrame *GetTransmitBuffer() { return &mTransmitFrame; }
Expand Down Expand Up @@ -107,8 +109,10 @@ class FakePlatform

otInstance *mInstance = nullptr;

uint64_t mNow = 0;
uint64_t mNow = 0;
#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
uint64_t mMicroAlarmStart = kAlarmStop;
#endif
uint64_t mMilliAlarmStart = kAlarmStop;

otRadioFrame mTransmitFrame;
Expand Down

0 comments on commit a225ee9

Please sign in to comment.