From 0d54e51ebead7011bf81014b5f48c63557f5973b Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Fri, 16 Sep 2016 20:43:44 +0800 Subject: [PATCH] Add Hash MAC address get cli (#611) - move Hash Mac address calculation from joiner.cpp to mac.cpp - add hashmacaddr cli --- include/openthread.h | 12 ++++++++++++ src/cli/README.md | 22 ++++++++++++++++++++++ src/cli/cli.cpp | 17 +++++++++++++++++ src/cli/cli.hpp | 1 + src/core/mac/mac.cpp | 14 ++++++++++++++ src/core/mac/mac.hpp | 11 +++++++++++ src/core/meshcop/joiner.cpp | 12 ++---------- src/core/openthread.cpp | 5 +++++ 8 files changed, 84 insertions(+), 10 deletions(-) diff --git a/include/openthread.h b/include/openthread.h index cc80d386e..43862dd39 100644 --- a/include/openthread.h +++ b/include/openthread.h @@ -511,6 +511,18 @@ void otSetExtendedPanId(otInstance *aInstance, const uint8_t *aExtendedPanId); */ void otGetFactoryAssignedIeeeEui64(otInstance *aInstance, otExtAddress *aEui64); +/** + * Get the Hash Mac Address. + * + * Hash Mac Address is the first 64 bits of the result of computing SHA-256 over factory-assigned + * IEEE EUI-64, which is used as IEEE 802.15.4 Extended Address during commissioning process. + * + * @param[in] aInstance A pointer to the OpenThread instance. + * @param[out] aHashMacAddress A pointer to where the Hash Mac Address is placed. + * + */ +void otGetHashMacAddress(otInstance *aInstance, otExtAddress *aHashMacAddress); + /** * This function returns a pointer to the Leader's RLOC. * diff --git a/src/cli/README.md b/src/cli/README.md index 957cc8352..80b3acf67 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -17,8 +17,10 @@ OpenThread test scripts use the CLI to execute test cases. * [dataset](#dataset) * [discover](#discover) * [eidcache](#eidcache) +* [eui64](#eui64) * [extaddr](#extaddr) * [extpanid](#extpanid) +* [hashmacaddr](#hashmacaddr) * [ifconfig](#ifconfig) * [ipaddr](#ipaddr) * [keysequence](#keysequence) @@ -552,6 +554,16 @@ fdde:ad00:beef:0:110a:e041:8399:17cd 6000 Done ``` +### eui64 + +Get the factory-assigned IEEE EUI-64. + +```bash +> eui64 +0615aae900124b00 +Done +``` + ### extaddr Get the IEEE 802.15.4 Extended Address. @@ -591,6 +603,16 @@ Set the Thread Extended PAN ID value. Done ``` +### hashmacaddr + +Get the HashMac address. + +```bash +> hashmacaddr +e0b220eb7d8dda7e +Done +``` + ### ifconfig up Bring up the IPv6 interface. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index be3673741..81fcf8ecd 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -89,6 +89,7 @@ const struct Command Interpreter::sCommands[] = #endif { "extaddr", &Interpreter::ProcessExtAddress }, { "extpanid", &Interpreter::ProcessExtPanId }, + { "hashmacaddr", &Interpreter::ProcessHashMacAddress }, { "ifconfig", &Interpreter::ProcessIfconfig }, { "ipaddr", &Interpreter::ProcessIpAddr }, #if OPENTHREAD_ENABLE_JOINER @@ -617,6 +618,22 @@ exit: AppendResult(error); } +void Interpreter::ProcessHashMacAddress(int argc, char *argv[]) +{ + ThreadError error = kThreadError_None; + otExtAddress hashMacAddress; + + VerifyOrExit(argc == 0, error = kThreadError_Parse); + + otGetHashMacAddress(mInstance, &hashMacAddress); + OutputBytes(hashMacAddress.m8, OT_EXT_ADDRESS_SIZE); + sServer->OutputFormat("\r\n"); + +exit: + (void)argv; + AppendResult(error); +} + void Interpreter::ProcessIfconfig(int argc, char *argv[]) { ThreadError error = kThreadError_None; diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 9f33683e7..679770632 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -159,6 +159,7 @@ private: #endif void ProcessExtAddress(int argc, char *argv[]); void ProcessExtPanId(int argc, char *argv[]); + void ProcessHashMacAddress(int argc, char *argv[]); void ProcessIfconfig(int argc, char *argv[]); void ProcessIpAddr(int argc, char *argv[]); ThreadError ProcessIpAddrAdd(int argc, char *argv[]); diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 1c3fe3bc9..4d54c4257 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -299,6 +300,19 @@ exit: return error; } +void Mac::GetHashMacAddress(ExtAddress *aHashMacAddress) +{ + Crypto::Sha256 sha256; + uint8_t buf[Crypto::Sha256::kHashSize]; + + otPlatRadioGetIeeeEui64(NULL, buf); + sha256.Start(); + sha256.Update(buf, OT_EXT_ADDRESS_SIZE); + sha256.Finish(buf); + + memcpy(aHashMacAddress->m8, buf, OT_EXT_ADDRESS_SIZE); +} + ShortAddress Mac::GetShortAddress(void) const { return mShortAddress; diff --git a/src/core/mac/mac.hpp b/src/core/mac/mac.hpp index e1b5da78b..4fa15efb6 100644 --- a/src/core/mac/mac.hpp +++ b/src/core/mac/mac.hpp @@ -290,6 +290,17 @@ public: */ ThreadError SetExtAddress(const ExtAddress &aExtAddress); + /** + * This method gets the Hash Mac Address. + * + * Hash Mac Address is the first 64 bits of the result of computing SHA-256 over factory-assigned + * IEEE EUI-64, which is used as IEEE 802.15.4 Extended Address during commissioning process. + * + * @param[out] aHashMacAddress A pointer to where the Hash Mac Address is placed. + * + */ + void GetHashMacAddress(ExtAddress *aHashMacAddress); + /** * This method returns the IEEE 802.15.4 Short Address. * diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index 7918e250e..fbe612ebb 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -67,18 +67,10 @@ Joiner::Joiner(ThreadNetif &aNetif): ThreadError Joiner::Start(const char *aPSKd) { ThreadError error; - union - { - Mac::ExtAddress extAddress; - uint8_t buf[Crypto::Sha256::kHashSize]; - }; - Crypto::Sha256 sha256; + Mac::ExtAddress extAddress; // use extended address based on factory-assigned IEEE EUI-64 - otPlatRadioGetIeeeEui64(NULL, buf); - sha256.Start(); - sha256.Update(buf, OT_EXT_ADDRESS_SIZE); - sha256.Finish(buf); + mNetif.GetMac().GetHashMacAddress(&extAddress); mNetif.GetMac().SetExtAddress(extAddress); mNetif.GetMle().UpdateLinkLocalAddress(); diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index cca425112..c805e502b 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -181,6 +181,11 @@ void otGetFactoryAssignedIeeeEui64(otInstance *aInstance, otExtAddress *aEui64) otPlatRadioGetIeeeEui64(aInstance, aEui64->m8); } +void otGetHashMacAddress(otInstance *, otExtAddress *aHashMacAddress) +{ + sThreadNetif->GetMac().GetHashMacAddress(static_cast(aHashMacAddress)); +} + ThreadError otGetLeaderRloc(otInstance *, otIp6Address *aAddress) { ThreadError error;