Skip to content

Commit

Permalink
sitesurvey
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglongxia committed Apr 23, 2024
1 parent 30aa3e8 commit ff1c054
Show file tree
Hide file tree
Showing 9 changed files with 1,220 additions and 30 deletions.
4 changes: 4 additions & 0 deletions include/openthread/diag.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ extern "C" {
*
*/

typedef void (*otDiagOutputCallback)(void *aContext, const char *aFormat, va_list aArguments);

void otDiagSetOutputCallback(otInstance *aInstance, otDiagOutputCallback aCallback, void *aContext);

/**
* Processes a factory diagnostics command line.
*
Expand Down
12 changes: 12 additions & 0 deletions src/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ Interpreter::Interpreter(Instance *aInstance, otCliOutputCallback aCallback, voi
#if (OPENTHREAD_FTD || OPENTHREAD_MTD) && OPENTHREAD_CONFIG_CLI_REGISTER_IP6_RECV_CALLBACK
otIp6SetReceiveCallback(GetInstancePtr(), &Interpreter::HandleIp6Receive, this);
#endif

#if OPENTHREAD_CONFIG_DIAG_ENABLE
otDiagSetOutputCallback(GetInstancePtr(), &Interpreter::HandleDiagOutput, this);
#endif

ClearAllBytes(mUserCommands);

OutputPrompt();
Expand Down Expand Up @@ -252,6 +257,13 @@ template <> otError Interpreter::Process<Cmd("diag")>(Arg aArgs[])

return error;
}

void Interpreter::HandleDiagOutput(void *aContext, const char *aFormat, va_list aArguments)
{
static_cast<Interpreter *>(aContext)->HandleDiagOutput(aFormat, aArguments);
}

void Interpreter::HandleDiagOutput(const char *aFormat, va_list aArguments) { OutputFormatV(aFormat, aArguments); }
#endif

template <> otError Interpreter::Process<Cmd("version")>(Arg aArgs[])
Expand Down
5 changes: 5 additions & 0 deletions src/cli/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ class Interpreter : public OutputImplementer, public Utils

#endif // OPENTHREAD_FTD || OPENTHREAD_MTD

#if OPENTHREAD_CONFIG_DIAG_ENABLE
static void HandleDiagOutput(void *aContext, const char *aFormat, va_list aArguments);
void HandleDiagOutput(const char *aFormat, va_list aArguments);
#endif

void SetCommandTimeout(uint32_t aTimeoutMilli);

static void HandleTimer(Timer &aTimer);
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ set(COMMON_SOURCES
crypto/sha256.cpp
crypto/storage.cpp
diags/factory_diags.cpp
diags/sitesurvey.cpp
instance/instance.cpp
mac/channel_mask.cpp
mac/data_poll_handler.cpp
Expand Down Expand Up @@ -285,6 +286,7 @@ set(RADIO_COMMON_SOURCES
crypto/crypto_platform.cpp
crypto/storage.cpp
diags/factory_diags.cpp
diags/sitesurvey.cpp
instance/instance.cpp
mac/link_raw.cpp
mac/mac_frame.cpp
Expand Down
7 changes: 7 additions & 0 deletions src/core/api/diags_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@

using namespace ot;

void otDiagSetOutputCallback(otInstance *aInstance, otDiagOutputCallback aCallback, void *aContext)
{
AssertPointerIsNotNull(aCallback);

AsCoreType(aInstance).Get<FactoryDiags::Diags>().SetOutputCallback(aCallback, aContext);
}

otError otDiagProcessCmdLine(otInstance *aInstance, const char *aString, char *aOutput, size_t aOutputMaxLen)
{
AssertPointerIsNotNull(aString);
Expand Down
96 changes: 66 additions & 30 deletions src/core/diags/factory_diags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@
#include "radio/radio.hpp"
#include "utils/parse_cmdline.hpp"

OT_TOOL_WEAK
otError otPlatDiagProcess(otInstance *aInstance,
uint8_t aArgsLength,
char *aArgs[],
char *aOutput,
size_t aOutputMaxLen)
OT_TOOL_WEAK otError
otPlatDiagProcess(otInstance *aInstance, uint8_t aArgsLength, char *aArgs[], char *aOutput, size_t aOutputMaxLen)
{
OT_UNUSED_VARIABLE(aArgsLength);
OT_UNUSED_VARIABLE(aArgs);
Expand Down Expand Up @@ -194,6 +190,7 @@ const struct Diags::Command Diags::sCommands[] = {
{"radio", &Diags::ProcessRadio},
{"repeat", &Diags::ProcessRepeat},
{"send", &Diags::ProcessSend},
{"sitesurvey", &Diags::ProcessSiteSurvey},
{"start", &Diags::ProcessStart},
{"stats", &Diags::ProcessStats},
{"stop", &Diags::ProcessStop},
Expand All @@ -210,6 +207,7 @@ Diags::Diags(Instance &aInstance)
, mTxLen(0)
, mRepeatActive(false)
, mDiagSendOn(false)
, mSiteSurvey(aInstance, mTxPacket)
{
mStats.Clear();
}
Expand All @@ -234,6 +232,7 @@ Error Diags::ProcessChannel(uint8_t aArgsLength, char *aArgs[], char *aOutput, s
mChannel = static_cast<uint8_t>(value);
IgnoreError(Get<Radio>().Receive(mChannel));
otPlatDiagChannelSet(mChannel);
mSiteSurvey.SetChannel(mChannel);

snprintf(aOutput, aOutputMaxLen, "set channel to %d\r\nstatus 0x%02x\r\n", mChannel, error);
}
Expand Down Expand Up @@ -276,6 +275,7 @@ Error Diags::ProcessRepeat(uint8_t aArgsLength, char *aArgs[], char *aOutput, si
Error error = kErrorNone;

VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
VerifyOrExit(mSiteSurvey.IsRunning(), error = kErrorBusy);
VerifyOrExit(aArgsLength > 0, error = kErrorInvalidArgs);

if (strcmp(aArgs[0], "stop") == 0)
Expand Down Expand Up @@ -316,6 +316,7 @@ Error Diags::ProcessSend(uint8_t aArgsLength, char *aArgs[], char *aOutput, size
long value;

VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
// VerifyOrExit(mSiteSurvey.IsRunning(), error = kErrorBusy);
VerifyOrExit(aArgsLength == 2, error = kErrorInvalidArgs);

SuccessOrExit(error = ParseLong(aArgs[0], value));
Expand Down Expand Up @@ -489,11 +490,20 @@ Error Diags::ProcessRadio(uint8_t aArgsLength, char *aArgs[], char *aOutput, siz
return error;
}

Error Diags::ProcessSiteSurvey(uint8_t aArgsLength, char *aArgs[], char *aOutput, size_t aOutputMaxLen)
{
return mSiteSurvey.Process(aArgsLength, aArgs, aOutput, aOutputMaxLen);
}

extern "C" void otPlatDiagAlarmFired(otInstance *aInstance) { AsCoreType(aInstance).Get<Diags>().AlarmFired(); }

void Diags::AlarmFired(void)
{
if (mRepeatActive)
if (mSiteSurvey.IsRunning())
{
mSiteSurvey.TimerFired();
}
else if (mRepeatActive)
{
uint32_t now = otPlatAlarmMilliGetNow();

Expand All @@ -508,52 +518,78 @@ void Diags::AlarmFired(void)

void Diags::ReceiveDone(otRadioFrame *aFrame, Error aError)
{
if (aError == kErrorNone)
if (mSiteSurvey.IsRunning())
{
// for sensitivity test, only record the rssi and lqi for the first and last packet
if (mStats.mReceivedPackets == 0)
VerifyOrExit(aFrame != nullptr);
mSiteSurvey.ReceiveDone(*reinterpret_cast<Mac::RxFrame *>(aFrame), aError);
}
else
{
if (aError == kErrorNone)
{
mStats.mFirstRssi = aFrame->mInfo.mRxInfo.mRssi;
mStats.mFirstLqi = aFrame->mInfo.mRxInfo.mLqi;
}
// for sensitivity test, only record the rssi and lqi for the first and last packet
if (mStats.mReceivedPackets == 0)
{
mStats.mFirstRssi = aFrame->mInfo.mRxInfo.mRssi;
mStats.mFirstLqi = aFrame->mInfo.mRxInfo.mLqi;
}

mStats.mLastRssi = aFrame->mInfo.mRxInfo.mRssi;
mStats.mLastLqi = aFrame->mInfo.mRxInfo.mLqi;
mStats.mLastRssi = aFrame->mInfo.mRxInfo.mRssi;
mStats.mLastLqi = aFrame->mInfo.mRxInfo.mLqi;

mStats.mReceivedPackets++;
mStats.mReceivedPackets++;
}
}

exit:
otPlatDiagRadioReceived(&GetInstance(), aFrame, aError);
}

void Diags::TransmitDone(Error aError)
{
VerifyOrExit(mDiagSendOn);
mDiagSendOn = false;

if (aError == kErrorNone)
if (mSiteSurvey.IsRunning())
{
mSiteSurvey.TransmitDone(aError);
}
else
{
mStats.mSentPackets++;
VerifyOrExit(mDiagSendOn);
mDiagSendOn = false;

if (mTxPackets > 1)
if (aError == kErrorNone)
{
mTxPackets--;
}
else
{
ExitNow();
mStats.mSentPackets++;

if (mTxPackets > 1)
{
mTxPackets--;
}
else
{
ExitNow();
}
}
}

VerifyOrExit(!mRepeatActive);
TransmitPacket();
VerifyOrExit(!mRepeatActive);
TransmitPacket();
}

exit:
return;
}

#endif // OPENTHREAD_RADIO

void Diags::SetOutputCallback(otDiagOutputCallback aCallback, void *aContext)
{
OT_UNUSED_VARIABLE(aCallback);
OT_UNUSED_VARIABLE(aContext);

#if OPENTHREAD_FTD || OPENTHREAD_MTD || (OPENTHREAD_RADIO && OPENTHREAD_RADIO_CLI)
mSiteSurvey.SetOutputCallback(aCallback, aContext);
#endif
}

Error Diags::ProcessContinuousWave(uint8_t aArgsLength, char *aArgs[], char *aOutput, size_t aOutputMaxLen)
{
Error error = kErrorInvalidArgs;
Expand Down
7 changes: 7 additions & 0 deletions src/core/diags/factory_diags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include "common/locator.hpp"
#include "common/non_copyable.hpp"
#include "common/string.hpp"
#include "diags/sitesurvey.hpp"
#include "mac/mac_frame.hpp"

namespace ot {
namespace FactoryDiags {
Expand Down Expand Up @@ -123,6 +125,8 @@ class Diags : public InstanceLocator, private NonCopyable
*/
void TransmitDone(Error aError);

void SetOutputCallback(otDiagOutputCallback aCallback, void *aContext);

private:
static constexpr uint8_t kMaxArgs = OPENTHREAD_CONFIG_DIAG_CMD_LINE_ARGS_MAX;

Expand Down Expand Up @@ -196,6 +200,7 @@ class Diags : public InstanceLocator, private NonCopyable
#if OPENTHREAD_RADIO && !OPENTHREAD_RADIO_CLI
Error ProcessEcho(uint8_t aArgsLength, char *aArgs[], char *aOutput, size_t aOutputMaxLen);
#endif
Error ProcessSiteSurvey(uint8_t aArgsLength, char *aArgs[], char *aOutput, size_t aOutputMaxLen);

Error GetRawPowerSetting(RawPowerSetting &aRawPowerSetting);
Error GetPowerSettings(uint8_t aChannel, PowerSettings &aPowerSettings);
Expand All @@ -219,6 +224,8 @@ class Diags : public InstanceLocator, private NonCopyable
uint8_t mTxLen;
bool mRepeatActive;
bool mDiagSendOn;

SiteSurvey mSiteSurvey;
#endif
};

Expand Down
Loading

0 comments on commit ff1c054

Please sign in to comment.