From e522428b028976ef394860ea547e36ad54236d1e Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 19 Apr 2017 21:35:04 -0700 Subject: [PATCH] Provide a helper method `Mac:ConvertBeaconToActiveScanResult()` (#1628) This commit provides a new static helper method in `Mac` class `ConvertBeaconToActiveScanResult()` to convert a received beacon frame (from active scan operation) to a `otActiveScanResult` struct. This is in turn used to simplify the `HandleActiveScanResult()` callback implementation in `linke_api.cpp`. --- src/core/api/link_api.cpp | 37 ++---------------------------------- src/core/mac/mac.cpp | 40 +++++++++++++++++++++++++++++++++++++++ src/core/mac/mac.hpp | 15 ++++++++++++++- 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/core/api/link_api.cpp b/src/core/api/link_api.cpp index 66a24b91b..b14b487ca 100644 --- a/src/core/api/link_api.cpp +++ b/src/core/api/link_api.cpp @@ -306,42 +306,9 @@ void HandleActiveScanResult(void *aContext, Mac::Frame *aFrame) { otInstance *aInstance = static_cast(aContext); otActiveScanResult result; - Mac::Address address; - Mac::Beacon *beacon = NULL; - Mac::BeaconPayload *beaconPayload = NULL; - uint8_t payloadLength; - - memset(&result, 0, sizeof(otActiveScanResult)); - - if (aFrame == NULL) - { - aInstance->mActiveScanCallback(NULL, aInstance->mActiveScanCallbackContext); - ExitNow(); - } - - SuccessOrExit(aFrame->GetSrcAddr(address)); - VerifyOrExit(address.mLength == sizeof(address.mExtAddress)); - memcpy(&result.mExtAddress, &address.mExtAddress, sizeof(result.mExtAddress)); - - aFrame->GetSrcPanId(result.mPanId); - result.mChannel = aFrame->GetChannel(); - result.mRssi = aFrame->GetPower(); - result.mLqi = aFrame->GetLqi(); - - payloadLength = aFrame->GetPayloadLength(); - - beacon = reinterpret_cast(aFrame->GetPayload()); - beaconPayload = reinterpret_cast(beacon->GetPayload()); - - if ((payloadLength >= (sizeof(*beacon) + sizeof(*beaconPayload))) && beacon->IsValid() && beaconPayload->IsValid()) - { - result.mVersion = beaconPayload->GetProtocolVersion(); - result.mIsJoinable = beaconPayload->IsJoiningPermitted(); - result.mIsNative = beaconPayload->IsNative(); - memcpy(&result.mNetworkName, beaconPayload->GetNetworkName(), sizeof(result.mNetworkName)); - memcpy(&result.mExtendedPanId, beaconPayload->GetExtendedPanId(), sizeof(result.mExtendedPanId)); - } + VerifyOrExit(aFrame != NULL, aInstance->mActiveScanCallback(NULL, aInstance->mActiveScanCallbackContext)); + Mac::Mac::ConvertBeaconToActiveScanResult(aFrame, result); aInstance->mActiveScanCallback(&result, aInstance->mActiveScanCallbackContext); exit: diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 5d5eef24d..757da87ea 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -263,6 +263,46 @@ bool Mac::IsInTransmitState(void) return (mState == kStateTransmitData) || (mState == kStateTransmitBeacon); } +ThreadError Mac::ConvertBeaconToActiveScanResult(Frame *aBeaconFrame, otActiveScanResult &aResult) +{ + ThreadError error = kThreadError_None; + Address address; + Beacon *beacon = NULL; + BeaconPayload *beaconPayload = NULL; + uint8_t payloadLength; + + memset(&aResult, 0, sizeof(otActiveScanResult)); + + VerifyOrExit(aBeaconFrame != NULL, error = kThreadError_InvalidArgs); + + VerifyOrExit(aBeaconFrame->GetType() == Frame::kFcfFrameBeacon, error = kThreadError_Parse); + SuccessOrExit(error = aBeaconFrame->GetSrcAddr(address)); + VerifyOrExit(address.mLength == sizeof(address.mExtAddress), error = kThreadError_Parse); + memcpy(&aResult.mExtAddress, &address.mExtAddress, sizeof(aResult.mExtAddress)); + + aBeaconFrame->GetSrcPanId(aResult.mPanId); + aResult.mChannel = aBeaconFrame->GetChannel(); + aResult.mRssi = aBeaconFrame->GetPower(); + aResult.mLqi = aBeaconFrame->GetLqi(); + + payloadLength = aBeaconFrame->GetPayloadLength(); + + beacon = reinterpret_cast(aBeaconFrame->GetPayload()); + beaconPayload = reinterpret_cast(beacon->GetPayload()); + + if ((payloadLength >= (sizeof(*beacon) + sizeof(*beaconPayload))) && beacon->IsValid() && beaconPayload->IsValid()) + { + aResult.mVersion = beaconPayload->GetProtocolVersion(); + aResult.mIsJoinable = beaconPayload->IsJoiningPermitted(); + aResult.mIsNative = beaconPayload->IsNative(); + memcpy(&aResult.mNetworkName, beaconPayload->GetNetworkName(), sizeof(aResult.mNetworkName)); + memcpy(&aResult.mExtendedPanId, beaconPayload->GetExtendedPanId(), sizeof(aResult.mExtendedPanId)); + } + +exit: + return error; +} + void Mac::StartEnergyScan(void) { mState = kStateEnergyScan; diff --git a/src/core/mac/mac.hpp b/src/core/mac/mac.hpp index 37423d7f2..08925a952 100644 --- a/src/core/mac/mac.hpp +++ b/src/core/mac/mac.hpp @@ -249,12 +249,25 @@ public: */ ThreadError ActiveScan(uint32_t aScanChannels, uint16_t aScanDuration, ActiveScanHandler aHandler, void *aContext); + /** + * This static method converts a beacon frame to an active scan result of type `otActiveScanResult`. + * + * @param[in] aBeaconFrame A pointer to a beacon frame. + * @param[out] aResult A reference to `otActiveScanResult` where the result is stored. + * + * @retval kThreadError_None Successfully converted the beacon into active scan result. + * @retavl kThreadError_InvalidArgs The @a aBeaconFrame was NULL. + * @retval kThreadError_Parse Failed parsing the beacon frame. + * + */ + static ThreadError ConvertBeaconToActiveScanResult(Frame *aBeaconFrame, otActiveScanResult &aResult); + /** * This function pointer is called during an "Energy Scan" when the result for a channel is ready or the scan * completes. * - * @param[in] aResult A valid pointer to the energy scan result information or NULL when the energy scan completes. * @param[in] aContext A pointer to arbitrary context information. + * @param[in] aResult A valid pointer to the energy scan result information or NULL when the energy scan completes. * */ typedef void (*EnergyScanHandler)(void *aContext, otEnergyScanResult *aResult);