From 143ea37a0ab78b34f7e509430db4f5c426719320 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Thu, 28 Jul 2016 10:55:23 -0700 Subject: [PATCH] Add API to set the IEEE 802.15.4 Extended Address. (#289) --- include/openthread-types.h | 3 ++- include/openthread.h | 11 +++++++++++ src/cli/README.md | 14 ++++++++++++-- src/cli/cli.cpp | 23 ++++++++++++++++++----- src/core/mac/mac.cpp | 8 +++++++- src/core/mac/mac_frame.hpp | 18 ++++++++++++++++++ src/core/openthread.cpp | 13 +++++++++++++ src/core/thread/mle.cpp | 11 +++++++++++ src/core/thread/mle.hpp | 10 ++++++++++ src/ncp/ncp_base.cpp | 8 ++++++++ 10 files changed, 110 insertions(+), 9 deletions(-) diff --git a/include/openthread-types.h b/include/openthread-types.h index 04ddc608b..d9d4487ff 100644 --- a/include/openthread-types.h +++ b/include/openthread-types.h @@ -265,7 +265,8 @@ enum OT_THREAD_CHILD_ADDED = 1 << 6, ///< Child was added OT_THREAD_CHILD_REMOVED = 1 << 7, ///< Child was removed - OT_IP6_ML_ADDR_CHANGED = 1 << 8, ///< The mesh-local address has changed + OT_IP6_LL_ADDR_CHANGED = 1 << 8, ///< The link-local address has changed + OT_IP6_ML_ADDR_CHANGED = 1 << 9, ///< The mesh-local address has changed }; /** diff --git a/include/openthread.h b/include/openthread.h index 78eccbbd8..a494623d4 100644 --- a/include/openthread.h +++ b/include/openthread.h @@ -316,6 +316,17 @@ void otSetChildTimeout(uint32_t aTimeout); */ const uint8_t *otGetExtendedAddress(void); +/** + * This function sets the IEEE 802.15.4 Extended Address. + * + * @param[in] aExtendedAddress A pointer to the IEEE 802.15.4 Extended Address. + * + * @retval kThreadError_None Successfully set the IEEE 802.15.4 Extended Address. + * @retval kThreadError_InvalidArgs @p aExtendedAddress was NULL. + * + */ +ThreadError otSetExtendedAddress(const otExtAddress *aExtendedAddress); + /** * Get the IEEE 802.15.4 Extended PAN ID. * diff --git a/src/cli/README.md b/src/cli/README.md index 205e5030b..e18dc6476 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -185,7 +185,17 @@ Get the IEEE 802.15.4 Extended Address. ```bash > extaddr -0xdead00beef00cafe +dead00beef00cafe +Done +``` + +### extaddr \ + +Set the IEEE 802.15.4 Extended Address. + +```bash +> extaddr dead00beef00cafe +dead00beef00cafe Done ``` @@ -195,7 +205,7 @@ Get the Thread Extended PAN ID value. ```bash > extpanid -0xdead00beef00cafe +dead00beef00cafe Done ``` diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 5b3ba72dd..5615ee85f 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -386,11 +386,24 @@ exit: void Interpreter::ProcessExtAddress(int argc, char *argv[]) { - OutputBytes(otGetExtendedAddress(), OT_EXT_ADDRESS_SIZE); - sServer->OutputFormat("\r\n"); - AppendResult(kThreadError_None); - (void)argc; - (void)argv; + ThreadError error = kThreadError_None; + + if (argc == 0) + { + OutputBytes(otGetExtendedAddress(), OT_EXT_ADDRESS_SIZE); + sServer->OutputFormat("\r\n"); + } + else + { + otExtAddress extAddress; + + VerifyOrExit(Hex2Bin(argv[0], extAddress.m8, sizeof(otExtAddress)) >= 0, error = kThreadError_Parse); + + otSetExtendedAddress(&extAddress); + } + +exit: + AppendResult(error); } void Interpreter::ProcessExtPanId(int argc, char *argv[]) diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index d9fda4bf6..7b3d34db0 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -199,15 +199,21 @@ const ExtAddress *Mac::GetExtAddress(void) const ThreadError Mac::SetExtAddress(const ExtAddress &aExtAddress) { + ThreadError error; uint8_t buf[8]; + VerifyOrExit(!aExtAddress.IsGroup(), error = kThreadError_InvalidArgs); + for (size_t i = 0; i < sizeof(buf); i++) { buf[i] = mExtAddress.m8[7 - i]; } + SuccessOrExit(error = otPlatRadioSetExtendedAddress(buf)); mExtAddress = aExtAddress; - return otPlatRadioSetExtendedAddress(buf); + +exit: + return error; } ShortAddress Mac::GetShortAddress(void) const diff --git a/src/core/mac/mac_frame.hpp b/src/core/mac/mac_frame.hpp index 45cfe26df..c8ba39b43 100644 --- a/src/core/mac/mac_frame.hpp +++ b/src/core/mac/mac_frame.hpp @@ -81,6 +81,15 @@ typedef otShortAddress ShortAddress; class ExtAddress: public otExtAddress { public: + /** + * This method indicates whether or not the Group bit is set. + * + * @retval TRUE If the group bit is set. + * @retval FALSE If the group bit is not set. + * + */ + bool IsGroup(void) const { return (m8[0] & kGroupFlag) != 0; } + /** * This method sets the Group bit. * @@ -96,6 +105,15 @@ public: } } + /** + * This method indicates whether or not the Local bit is set. + * + * @retval TRUE If the local bit is set. + * @retval FALSE If the local bit is not set. + * + */ + bool IsLocal(void) const { return (m8[0] & kLocalFlag) != 0; } + /** * This method sets the Local bit. * diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index 375b8e780..eee7af671 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -96,6 +96,19 @@ const uint8_t *otGetExtendedAddress(void) return reinterpret_cast(sThreadNetif->GetMac().GetExtAddress()); } +ThreadError otSetExtendedAddress(const otExtAddress *aExtAddress) +{ + ThreadError error = kThreadError_None; + + VerifyOrExit(aExtAddress != NULL, error = kThreadError_InvalidArgs); + + SuccessOrExit(error = sThreadNetif->GetMac().SetExtAddress(*static_cast(aExtAddress))); + SuccessOrExit(error = sThreadNetif->GetMle().UpdateLinkLocalAddress()); + +exit: + return error; +} + const uint8_t *otGetExtendedPanId(void) { return sThreadNetif->GetMac().GetExtendedPanId(); diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index aa731ccd6..877059984 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -342,6 +342,17 @@ exit: return error; } +ThreadError Mle::UpdateLinkLocalAddress(void) +{ + mNetif.RemoveUnicastAddress(mLinkLocal64); + mLinkLocal64.GetAddress().SetIid(*mMac.GetExtAddress()); + mNetif.AddUnicastAddress(mLinkLocal64); + + mNetif.SetStateChangedFlags(OT_IP6_LL_ADDR_CHANGED); + + return kThreadError_None; +} + const uint8_t *Mle::GetMeshLocalPrefix(void) const { return mMeshLocal16.GetAddress().mFields.m8; diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index e2b89f20d..ca9d1fd83 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -406,6 +406,16 @@ public: */ ThreadError SetMeshLocalPrefix(const uint8_t *aPrefix); + /** + * This method updates the link local address. + * + * Call this method when the IEEE 802.15.4 Extended Address has changed. + * + * @retval kThreadError_None Successfully updated the link local address. + * + */ + ThreadError UpdateLinkLocalAddress(void); + /** * This method returns a pointer to the link-local all Thread nodes multicast address. * diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index 078914acb..4fbdd9f76 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -477,6 +477,14 @@ void NcpBase::UpdateChangedProps() ResetReasonToSpinelStatus(otPlatGetResetReason()) ); } + else if ((mChangedFlags & OT_IP6_LL_ADDR_CHANGED) != 0) + { + mChangedFlags &= ~OT_IP6_LL_ADDR_CHANGED; + HandleCommandPropertyGet( + SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, + SPINEL_PROP_IPV6_LL_ADDR + ); + } else if ((mChangedFlags & OT_IP6_ML_ADDR_CHANGED) != 0) { mChangedFlags &= ~OT_IP6_ML_ADDR_CHANGED;