mirror of
https://github.com/espressif/openthread.git
synced 2026-09-02 15:20:07 +00:00
[jam-detector] pause jam detection when thread is disabled (#2652)
Once jamming detection is enabled/started, `JamDetector` will also monitor the Thread device role. RSSI sampling is stopped if the device role changes to `DISABLED` and is resumed if the role changes. This commit also adds new logs to this class to indicate when jamming is detected or cleared and/or when any of the parameters get changed.
This commit is contained in:
committed by
Jonathan Hui
parent
5fcb34c22f
commit
9690339837
@@ -37,6 +37,7 @@
|
||||
|
||||
#include "common/code_utils.hpp"
|
||||
#include "common/instance.hpp"
|
||||
#include "common/logging.hpp"
|
||||
#include "common/owner-locator.hpp"
|
||||
#include "common/random.hpp"
|
||||
#include "thread/thread_netif.hpp"
|
||||
@@ -50,7 +51,7 @@ JamDetector::JamDetector(Instance &aInstance)
|
||||
: InstanceLocator(aInstance)
|
||||
, mHandler(NULL)
|
||||
, mContext(NULL)
|
||||
, mRssiThreshold(kDefaultRssiThreshold)
|
||||
, mNotifierCallback(HandleStateChanged, this)
|
||||
, mTimer(aInstance, &JamDetector::HandleTimer, this)
|
||||
, mHistoryBitmap(0)
|
||||
, mCurSecondStartTime(0)
|
||||
@@ -60,7 +61,9 @@ JamDetector::JamDetector(Instance &aInstance)
|
||||
, mEnabled(false)
|
||||
, mAlwaysAboveThreshold(false)
|
||||
, mJamState(false)
|
||||
, mRssiThreshold(kDefaultRssiThreshold)
|
||||
{
|
||||
aInstance.GetNotifier().RegisterCallback(mNotifierCallback);
|
||||
}
|
||||
|
||||
otError JamDetector::Start(Handler aHandler, void *aContext)
|
||||
@@ -72,16 +75,11 @@ otError JamDetector::Start(Handler aHandler, void *aContext)
|
||||
|
||||
mHandler = aHandler;
|
||||
mContext = aContext;
|
||||
|
||||
mEnabled = true;
|
||||
|
||||
mCurSecondStartTime = TimerMilli::GetNow();
|
||||
mAlwaysAboveThreshold = true;
|
||||
mHistoryBitmap = 0;
|
||||
mJamState = false;
|
||||
mSampleInterval = kMaxSampleInterval;
|
||||
otLogInfoUtil(GetInstance(), "JamDetector - Started");
|
||||
|
||||
mTimer.Start(kMinSampleInterval);
|
||||
CheckState();
|
||||
|
||||
exit:
|
||||
return error;
|
||||
@@ -98,13 +96,43 @@ otError JamDetector::Stop(void)
|
||||
|
||||
mTimer.Stop();
|
||||
|
||||
otLogInfoUtil(GetInstance(), "JamDetector - Stopped");
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
void JamDetector::CheckState(void)
|
||||
{
|
||||
VerifyOrExit(mEnabled);
|
||||
|
||||
switch (GetInstance().Get<Mle::MleRouter>().GetRole())
|
||||
{
|
||||
case OT_DEVICE_ROLE_DISABLED:
|
||||
VerifyOrExit(mTimer.IsRunning());
|
||||
mTimer.Stop();
|
||||
SetJamState(false);
|
||||
break;
|
||||
|
||||
default:
|
||||
VerifyOrExit(!mTimer.IsRunning());
|
||||
mCurSecondStartTime = TimerMilli::GetNow();
|
||||
mAlwaysAboveThreshold = true;
|
||||
mHistoryBitmap = 0;
|
||||
mJamState = false;
|
||||
mSampleInterval = kMaxSampleInterval;
|
||||
mTimer.Start(kMinSampleInterval);
|
||||
break;
|
||||
}
|
||||
|
||||
exit:
|
||||
return;
|
||||
}
|
||||
|
||||
otError JamDetector::SetRssiThreshold(int8_t aThreshold)
|
||||
{
|
||||
mRssiThreshold = aThreshold;
|
||||
otLogInfoUtil(GetInstance(), "JamDetector - RSSI threshold set to %d", mRssiThreshold);
|
||||
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
@@ -117,6 +145,7 @@ otError JamDetector::SetWindow(uint8_t aWindow)
|
||||
VerifyOrExit(aWindow <= kMaxWindow, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
mWindow = aWindow;
|
||||
otLogInfoUtil(GetInstance(), "JamDetector - window set to %d", mWindow);
|
||||
|
||||
exit:
|
||||
return error;
|
||||
@@ -130,6 +159,7 @@ otError JamDetector::SetBusyPeriod(uint8_t aBusyPeriod)
|
||||
VerifyOrExit(aBusyPeriod <= mWindow, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
mBusyPeriod = aBusyPeriod;
|
||||
otLogInfoUtil(GetInstance(), "JamDetector - busy period set to %d", mBusyPeriod);
|
||||
|
||||
exit:
|
||||
return error;
|
||||
@@ -217,7 +247,6 @@ void JamDetector::UpdateJamState(void)
|
||||
{
|
||||
uint8_t numJammedSeconds = 0;
|
||||
uint64_t bitmap = mHistoryBitmap;
|
||||
bool oldJamState = mJamState;
|
||||
|
||||
// Clear all history bits beyond the current window size
|
||||
bitmap &= (static_cast<uint64_t>(1) << mWindow) - 1;
|
||||
@@ -229,16 +258,39 @@ void JamDetector::UpdateJamState(void)
|
||||
bitmap &= (bitmap - 1);
|
||||
}
|
||||
|
||||
// Update the Jam state
|
||||
mJamState = (numJammedSeconds >= mBusyPeriod);
|
||||
SetJamState(numJammedSeconds >= mBusyPeriod);
|
||||
}
|
||||
|
||||
// If there is a change, invoke the handler.
|
||||
if ((mJamState != oldJamState) || (mJamState == true))
|
||||
void JamDetector::SetJamState(bool aNewState)
|
||||
{
|
||||
bool shouldInvokeHandler = aNewState;
|
||||
|
||||
if (aNewState != mJamState)
|
||||
{
|
||||
mJamState = aNewState;
|
||||
shouldInvokeHandler = true;
|
||||
otLogInfoUtil(GetInstance(), "JamDetector - jamming %s", mJamState ? "detected" : "cleared");
|
||||
}
|
||||
|
||||
if (shouldInvokeHandler)
|
||||
{
|
||||
mHandler(mJamState, mContext);
|
||||
}
|
||||
}
|
||||
|
||||
void JamDetector::HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags)
|
||||
{
|
||||
aCallback.GetOwner<JamDetector>().HandleStateChanged(aFlags);
|
||||
}
|
||||
|
||||
void JamDetector::HandleStateChanged(uint32_t aFlags)
|
||||
{
|
||||
if (aFlags & OT_CHANGED_THREAD_ROLE)
|
||||
{
|
||||
CheckState();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Utils
|
||||
} // namespace ot
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include "openthread-core-config.h"
|
||||
|
||||
#include "common/locator.hpp"
|
||||
#include "common/notifier.hpp"
|
||||
#include "common/timer.hpp"
|
||||
#include "utils/wrap_stdint.h"
|
||||
|
||||
@@ -172,12 +173,6 @@ public:
|
||||
*/
|
||||
uint64_t GetHistoryBitmap(void) const { return mHistoryBitmap; }
|
||||
|
||||
private:
|
||||
static void HandleTimer(Timer &aTimer);
|
||||
void HandleTimer(void);
|
||||
void UpdateHistory(bool aThresholdExceeded);
|
||||
void UpdateJamState(void);
|
||||
|
||||
private:
|
||||
enum
|
||||
{
|
||||
@@ -188,21 +183,30 @@ private:
|
||||
kMinSampleInterval = 2, // in ms
|
||||
kMaxRandomDelay = 4, // in ms
|
||||
kOneSecondInterval = 1000 // in ms
|
||||
|
||||
};
|
||||
|
||||
Handler mHandler; // Handler/callback to inform about jamming state
|
||||
void * mContext; // Context for handler/callback
|
||||
int8_t mRssiThreshold; // RSSI threshold for jam detection
|
||||
TimerMilli mTimer; // RSSI sample timer
|
||||
uint64_t mHistoryBitmap; // History bitmap, each bit correspond to 1 sec interval
|
||||
uint32_t mCurSecondStartTime; // Start time for current 1 sec interval
|
||||
uint16_t mSampleInterval; // Current sample interval
|
||||
uint8_t mWindow : 6; // Window (in sec) to monitor jamming
|
||||
uint8_t mBusyPeriod : 6; // BusyPeriod (in sec) with mWindow to alert jamming
|
||||
bool mEnabled : 1; // If jam detection is enabled
|
||||
bool mAlwaysAboveThreshold : 1; // State for current 1 sec interval
|
||||
bool mJamState : 1; // Current jam state
|
||||
void CheckState(void);
|
||||
void SetJamState(bool aNewState);
|
||||
static void HandleTimer(Timer &aTimer);
|
||||
void HandleTimer(void);
|
||||
void UpdateHistory(bool aThresholdExceeded);
|
||||
void UpdateJamState(void);
|
||||
static void HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags);
|
||||
void HandleStateChanged(uint32_t aFlags);
|
||||
|
||||
Handler mHandler; // Handler/callback to inform about jamming state
|
||||
void * mContext; // Context for handler/callback
|
||||
Notifier::Callback mNotifierCallback; // Notifier callback
|
||||
TimerMilli mTimer; // RSSI sample timer
|
||||
uint64_t mHistoryBitmap; // History bitmap, each bit correspond to 1 sec interval
|
||||
uint32_t mCurSecondStartTime; // Start time for current 1 sec interval
|
||||
uint16_t mSampleInterval; // Current sample interval
|
||||
uint8_t mWindow : 6; // Window (in sec) to monitor jamming
|
||||
uint8_t mBusyPeriod : 6; // BusyPeriod (in sec) with mWindow to alert jamming
|
||||
bool mEnabled : 1; // If jam detection is enabled
|
||||
bool mAlwaysAboveThreshold : 1; // State for current 1 sec interval
|
||||
bool mJamState : 1; // Current jam state
|
||||
int8_t mRssiThreshold; // RSSI threshold for jam detection
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user