From e7e69065b6d5577c66b73d6974f7da07b0a34a43 Mon Sep 17 00:00:00 2001 From: Colin Tan <66396411+ctan-g@users.noreply.github.com> Date: Mon, 6 Jul 2020 18:15:10 -0400 Subject: [PATCH] [otns] add radio event status emission (#5195) This commit adds code in base radio implementation to emit radio events to logs, when OTNS is turned on. This is for Silk-OTNS integration's message visualizations. --- src/core/mac/mac_frame.hpp | 25 ++++++++++++++++--------- src/core/radio/radio.cpp | 9 +++++++++ src/core/radio/radio.hpp | 2 +- src/core/utils/otns.cpp | 26 ++++++++++++++++++++++++++ src/core/utils/otns.hpp | 8 ++++++++ 5 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/core/mac/mac_frame.hpp b/src/core/mac/mac_frame.hpp index 81385af4e..616f8cb9a 100644 --- a/src/core/mac/mac_frame.hpp +++ b/src/core/mac/mac_frame.hpp @@ -965,6 +965,14 @@ public: */ InfoString ToInfoString(void) const; + /** + * This method returns the Frame Control field of the frame. + * + * @returns The Frame Control field. + * + */ + uint16_t GetFrameControlField(void) const; + protected: enum { @@ -974,15 +982,14 @@ protected: kSequenceIndex = kFcfSize, }; - uint16_t GetFrameControlField(void) const; - uint8_t FindDstPanIdIndex(void) const; - uint8_t FindDstAddrIndex(void) const; - uint8_t FindSrcPanIdIndex(void) const; - uint8_t FindSrcAddrIndex(void) const; - uint8_t SkipAddrFieldIndex(void) const; - uint8_t FindSecurityHeaderIndex(void) const; - uint8_t SkipSecurityHeaderIndex(void) const; - uint8_t FindPayloadIndex(void) const; + uint8_t FindDstPanIdIndex(void) const; + uint8_t FindDstAddrIndex(void) const; + uint8_t FindSrcPanIdIndex(void) const; + uint8_t FindSrcAddrIndex(void) const; + uint8_t SkipAddrFieldIndex(void) const; + uint8_t FindSecurityHeaderIndex(void) const; + uint8_t SkipSecurityHeaderIndex(void) const; + uint8_t FindPayloadIndex(void) const; #if OPENTHREAD_CONFIG_MAC_HEADER_IE_SUPPORT uint8_t FindHeaderIeIndex(void) const; #endif diff --git a/src/core/radio/radio.cpp b/src/core/radio/radio.cpp index 9f3cebe01..33571b6c4 100644 --- a/src/core/radio/radio.cpp +++ b/src/core/radio/radio.cpp @@ -51,4 +51,13 @@ void Radio::SetShortAddress(Mac::ShortAddress aShortAddress) #endif } +otError Radio::Transmit(Mac::TxFrame &aFrame) +{ +#if (OPENTHREAD_MTD || OPENTHREAD_FTD) && OPENTHREAD_CONFIG_OTNS_ENABLE + Get().EmitTransmit(aFrame); +#endif + + return otPlatRadioTransmit(GetInstance(), &aFrame); +} + } // namespace ot diff --git a/src/core/radio/radio.hpp b/src/core/radio/radio.hpp index 0ebc15a38..f014e6fb6 100644 --- a/src/core/radio/radio.hpp +++ b/src/core/radio/radio.hpp @@ -428,7 +428,7 @@ public: * @retval OT_ERROR_INVALID_STATE The radio was not in the Receive state. * */ - otError Transmit(Mac::TxFrame &aFrame) { return otPlatRadioTransmit(GetInstance(), &aFrame); } + otError Transmit(Mac::TxFrame &aFrame); /** * This method gets the most recent RSSI measurement. diff --git a/src/core/utils/otns.cpp b/src/core/utils/otns.cpp index 3065c2b3a..f2d8a7373 100644 --- a/src/core/utils/otns.cpp +++ b/src/core/utils/otns.cpp @@ -130,6 +130,32 @@ void Otns::EmitNeighborChange(otNeighborTableEvent aEvent, Neighbor &aNeighbor) } } +void Otns::EmitTransmit(const Mac::TxFrame &aFrame) +{ + Mac::Address dst; + uint16_t frameControlField = aFrame.GetFrameControlField(); + uint8_t channel = aFrame.GetChannel(); + uint8_t sequence = aFrame.GetSequence(); + + IgnoreError(aFrame.GetDstAddr(dst)); + + if (dst.IsShort()) + { + EmitStatus("transmit=%d,%04x,%d,%04x", channel, frameControlField, sequence, dst.GetShort()); + } + else if (dst.IsExtended()) + { + Mac::ExtAddress revExtAddress; + revExtAddress.Set(dst.GetExtended().m8, Mac::ExtAddress::kReverseByteOrder); + EmitStatus("transmit=%d,%04x,%d,%s", channel, frameControlField, sequence, + revExtAddress.ToString().AsCString()); + } + else + { + EmitStatus("transmit=%d,%04x,%d", channel, frameControlField, sequence); + } +} + } // namespace Utils } // namespace ot diff --git a/src/core/utils/otns.hpp b/src/core/utils/otns.hpp index 65fc1a38c..bb747946b 100644 --- a/src/core/utils/otns.hpp +++ b/src/core/utils/otns.hpp @@ -124,6 +124,14 @@ public: */ static void EmitNeighborChange(otNeighborTableEvent aEvent, Neighbor &aNeighbor); + /** + * This function emits a transmit event to OTNS. + * + * @param[in] aFrame The frame of the transmission. + * + */ + static void EmitTransmit(const Mac::TxFrame &aFrame); + private: static void EmitStatus(const char *aFmt, ...);