#ifndef RADIO_HPP_
#define RADIO_HPP_
#include "openthread-core-config.h"
#include <openthread/platform/radio.h>
#include "common/locator.hpp"
#include "common/non_copyable.hpp"
#include "mac/mac_frame.hpp"
#include "utils/static_assert.hpp"
namespace ot {
class Radio : public InstanceLocator, private NonCopyable
{
friend class Instance;
public:
enum
{
#if (OPENTHREAD_CONFIG_RADIO_2P4GHZ_OQPSK_SUPPORT && OPENTHREAD_CONFIG_RADIO_915MHZ_OQPSK_SUPPORT)
kNumChannelPages = 2,
kSupportedChannels = OT_RADIO_915MHZ_OQPSK_CHANNEL_MASK | OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MASK,
kChannelMin = OT_RADIO_915MHZ_OQPSK_CHANNEL_MIN,
kChannelMax = OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MAX,
kSupportedChannelPages = OT_RADIO_CHANNEL_PAGE_0_MASK | OT_RADIO_CHANNEL_PAGE_2_MASK,
#elif OPENTHREAD_CONFIG_RADIO_915MHZ_OQPSK_SUPPORT
kNumChannelPages = 1,
kSupportedChannels = OT_RADIO_915MHZ_OQPSK_CHANNEL_MASK,
kChannelMin = OT_RADIO_915MHZ_OQPSK_CHANNEL_MIN,
kChannelMax = OT_RADIO_915MHZ_OQPSK_CHANNEL_MAX,
kSupportedChannelPages = OT_RADIO_CHANNEL_PAGE_2_MASK,
#elif OPENTHREAD_CONFIG_RADIO_2P4GHZ_OQPSK_SUPPORT
kNumChannelPages = 1,
kSupportedChannels = OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MASK,
kChannelMin = OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MIN,
kChannelMax = OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MAX,
kSupportedChannelPages = OT_RADIO_CHANNEL_PAGE_0_MASK,
#endif
};
OT_STATIC_ASSERT((OPENTHREAD_CONFIG_RADIO_2P4GHZ_OQPSK_SUPPORT || OPENTHREAD_CONFIG_RADIO_915MHZ_OQPSK_SUPPORT),
"OPENTHREAD_CONFIG_RADIO_2P4GHZ_OQPSK_SUPPORT or OPENTHREAD_CONFIG_RADIO_915MHZ_OQPSK_SUPPORT "
"must be set to 1 to specify the radio mode");
class Callbacks : public InstanceLocator
{
friend class Radio;
public:
void HandleReceiveDone(Mac::RxFrame *aFrame, otError aError);
void HandleTransmitStarted(Mac::TxFrame &aFrame);
void HandleTransmitDone(Mac::TxFrame &aFrame, Mac::RxFrame *aAckFrame, otError aError);
void HandleEnergyScanDone(int8_t aMaxRssi);
#if OPENTHREAD_CONFIG_DIAG_ENABLE
void HandleDiagsReceiveDone(Mac::RxFrame *aFrame, otError aError);
void HandleDiagsTransmitDone(Mac::TxFrame &aFrame, otError aError);
#endif
private:
explicit Callbacks(Instance &aInstance)
: InstanceLocator(aInstance)
{
}
};
Radio(Instance &aInstance)
: InstanceLocator(aInstance)
, mCallbacks(aInstance)
{
}
otRadioCaps GetCaps(void) { return otPlatRadioGetCaps(GetInstance()); }
const char *GetVersionString(void) { return otPlatRadioGetVersionString(GetInstance()); }
int8_t GetReceiveSensitivity(void) { return otPlatRadioGetReceiveSensitivity(GetInstance()); }
void GetIeeeEui64(Mac::ExtAddress &aIeeeEui64) { otPlatRadioGetIeeeEui64(GetInstance(), aIeeeEui64.m8); }
void SetPanId(Mac::PanId aPanId) { otPlatRadioSetPanId(GetInstance(), aPanId); }
void SetExtendedAddress(const Mac::ExtAddress &aExtAddress);
void SetShortAddress(Mac::ShortAddress aShortAddress);
void SetMacKey(uint8_t aKeyIdMode,
uint8_t aKeyId,
const Mac::Key &aPrevKey,
const Mac::Key &aCurrKey,
const Mac::Key &aNextKey)
{
otPlatRadioSetMacKey(GetInstance(), aKeyIdMode, aKeyId, &aPrevKey, &aCurrKey, &aNextKey);
}
otError GetTransmitPower(int8_t &aPower) { return otPlatRadioGetTransmitPower(GetInstance(), &aPower); }
otError SetTransmitPower(int8_t aPower) { return otPlatRadioSetTransmitPower(GetInstance(), aPower); }
otError GetCcaEnergyDetectThreshold(int8_t &aThreshold)
{
return otPlatRadioGetCcaEnergyDetectThreshold(GetInstance(), &aThreshold);
}
otError SetCcaEnergyDetectThreshold(int8_t aThreshold)
{
return otPlatRadioSetCcaEnergyDetectThreshold(GetInstance(), aThreshold);
}
bool GetPromiscuous(void) { return otPlatRadioGetPromiscuous(GetInstance()); }
void SetPromiscuous(bool aEnable) { otPlatRadioSetPromiscuous(GetInstance(), aEnable); }
otRadioState GetState(void) { return otPlatRadioGetState(GetInstance()); }
otError Enable(void) { return otPlatRadioEnable(GetInstance()); }
otError Disable(void) { return otPlatRadioDisable(GetInstance()); }
bool IsEnabled(void) { return otPlatRadioIsEnabled(GetInstance()); }
otError Sleep(void) { return otPlatRadioSleep(GetInstance()); }
otError Receive(uint8_t aChannel) { return otPlatRadioReceive(GetInstance(), aChannel); }
Mac::TxFrame &GetTransmitBuffer(void)
{
return *static_cast<Mac::TxFrame *>(otPlatRadioGetTransmitBuffer(GetInstance()));
}
otError Transmit(Mac::TxFrame &aFrame) { return otPlatRadioTransmit(GetInstance(), &aFrame); }
int8_t GetRssi(void) { return otPlatRadioGetRssi(GetInstance()); }
otError EnergyScan(uint8_t aScanChannel, uint16_t aScanDuration)
{
return otPlatRadioEnergyScan(GetInstance(), aScanChannel, aScanDuration);
}
void EnableSrcMatch(bool aEnable) { return otPlatRadioEnableSrcMatch(GetInstance(), aEnable); }
otError AddSrcMatchShortEntry(Mac::ShortAddress aShortAddress)
{
return otPlatRadioAddSrcMatchShortEntry(GetInstance(), aShortAddress);
}
otError AddSrcMatchExtEntry(const Mac::ExtAddress &aExtAddress)
{
return otPlatRadioAddSrcMatchExtEntry(GetInstance(), &aExtAddress);
}
otError ClearSrcMatchShortEntry(Mac::ShortAddress aShortAddress)
{
return otPlatRadioClearSrcMatchShortEntry(GetInstance(), aShortAddress);
}
otError ClearSrcMatchExtEntry(const Mac::ExtAddress &aExtAddress)
{
return otPlatRadioClearSrcMatchExtEntry(GetInstance(), &aExtAddress);
}
void ClearSrcMatchShortEntries(void) { otPlatRadioClearSrcMatchShortEntries(GetInstance()); }
void ClearSrcMatchExtEntries(void) { otPlatRadioClearSrcMatchExtEntries(GetInstance()); }
uint32_t GetSupportedChannelMask(void) { return otPlatRadioGetSupportedChannelMask(GetInstance()); }
uint32_t GetPreferredChannelMask(void) { return otPlatRadioGetPreferredChannelMask(GetInstance()); }
private:
otInstance *GetInstance(void) { return reinterpret_cast<otInstance *>(&InstanceLocator::GetInstance()); }
Callbacks mCallbacks;
};
}
#endif