From 8f8b332fd6dbe5f9127fc00b34f556c5af345570 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Mon, 4 Jul 2016 10:11:47 -0700 Subject: [PATCH] Add API to retrieve Leader Data information. (#231) --- include/openthread-types.h | 13 +++++++++++++ include/openthread.h | 12 ++++++++++++ src/cli/cli.cpp | 18 ++++++++++++++++++ src/cli/cli.hpp | 1 + src/core/openthread.cpp | 12 ++++++++++++ src/core/thread/mle.cpp | 18 ++++++++++++++++++ src/core/thread/mle.hpp | 11 +++++++++++ 7 files changed, 85 insertions(+) diff --git a/include/openthread-types.h b/include/openthread-types.h index 4d7d07364..cc157cb87 100644 --- a/include/openthread-types.h +++ b/include/openthread-types.h @@ -452,6 +452,19 @@ typedef struct otEidCacheEntry bool mValid : 1; ///< Indicates whether or not the cache entry is valid } otEidCacheEntry; +/** + * This structure represents the Thread Leader Data. + * + */ +typedef struct otLeaderData +{ + uint32_t mPartitionId; ///< Partition ID + uint8_t mWeighting; ///< Leader Weight + uint8_t mDataVersion; ///< Full Network Data Version + uint8_t mStableDataVersion; ///< Stable Network Data Version + uint8_t mLeaderRouterId; ///< Leader Router ID +} otLeaderData; + /** * This structure represents the MAC layer counters. */ diff --git a/include/openthread.h b/include/openthread.h index 320688a89..5764f64bf 100644 --- a/include/openthread.h +++ b/include/openthread.h @@ -929,6 +929,18 @@ otDeviceRole otGetDeviceRole(void); */ ThreadError otGetEidCacheEntry(uint8_t aIndex, otEidCacheEntry *aEntry); +/** + * This function get the Thread Leader Data. + * + * @param[out] aLeaderData A pointer to where the leader data is placed. + * + * @retval kThreadError_None Successfully retrieved the leader data. + * @retval kThreadError_Detached Not currently attached. + * @retval kThreadError_InvalidArgs @p aLeaderData is NULL. + * + */ +ThreadError otGetLeaderData(otLeaderData *aLeaderData); + /** * Get the Leader's Router ID. * diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 6fff45f48..4b53db210 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -60,6 +60,7 @@ const struct Command Interpreter::sCommands[] = { "extpanid", &ProcessExtPanId }, { "ipaddr", &ProcessIpAddr }, { "keysequence", &ProcessKeySequence }, + { "leaderdata", &ProcessLeaderData }, { "leaderweight", &ProcessLeaderWeight }, { "masterkey", &ProcessMasterKey }, { "mode", &ProcessMode }, @@ -490,6 +491,23 @@ exit: AppendResult(error); } +void Interpreter::ProcessLeaderData(int argc, char *argv[]) +{ + ThreadError error; + otLeaderData leaderData; + + SuccessOrExit(error = otGetLeaderData(&leaderData)); + + sServer->OutputFormat("Partition ID: %d\r\n", leaderData.mPartitionId); + sServer->OutputFormat("Weighting: %d\r\n", leaderData.mWeighting); + sServer->OutputFormat("Data Version: %d\r\n", leaderData.mDataVersion); + sServer->OutputFormat("Stable Data Version: %d\r\n", leaderData.mStableDataVersion); + sServer->OutputFormat("Leader Router ID: %d\r\n", leaderData.mLeaderRouterId); + +exit: + AppendResult(error); +} + void Interpreter::ProcessLeaderWeight(int argc, char *argv[]) { ThreadError error = kThreadError_None; diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 420bdeede..d4ea511f0 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -100,6 +100,7 @@ private: static ThreadError ProcessIpAddrAdd(int argc, char *argv[]); static ThreadError ProcessIpAddrDel(int argc, char *argv[]); static void ProcessKeySequence(int argc, char *argv[]); + static void ProcessLeaderData(int argc, char *argv[]); static void ProcessLeaderWeight(int argc, char *argv[]); static void ProcessMasterKey(int argc, char *argv[]); static void ProcessMode(int argc, char *argv[]); diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index be6fdcf6d..94fa514c2 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -535,6 +535,18 @@ exit: return error; } +ThreadError otGetLeaderData(otLeaderData *aLeaderData) +{ + ThreadError error; + + VerifyOrExit(aLeaderData != NULL, error = kThreadError_InvalidArgs); + + error = sThreadNetif->GetMle().GetLeaderData(*aLeaderData); + +exit: + return error; +} + uint8_t otGetLeaderRouterId(void) { return sThreadNetif->GetMle().GetLeaderDataTlv().GetLeaderRouterId(); diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index f23213dd8..cb1c2495c 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -473,6 +473,24 @@ const LeaderDataTlv &Mle::GetLeaderDataTlv(void) return mLeaderData; } +ThreadError Mle::GetLeaderData(otLeaderData &aLeaderData) +{ + const LeaderDataTlv &leaderData(GetLeaderDataTlv()); + ThreadError error = kThreadError_None; + + VerifyOrExit(mDeviceState != kDeviceStateDisabled && mDeviceState != kDeviceStateDetached, + error = kThreadError_Detached); + + aLeaderData.mPartitionId = leaderData.GetPartitionId(); + aLeaderData.mWeighting = leaderData.GetWeighting(); + aLeaderData.mDataVersion = leaderData.GetDataVersion(); + aLeaderData.mStableDataVersion = leaderData.GetStableDataVersion(); + aLeaderData.mLeaderRouterId = leaderData.GetLeaderRouterId(); + +exit: + return error; +} + void Mle::GenerateNonce(const Mac::ExtAddress &aMacAddr, uint32_t aFrameCounter, uint8_t aSecurityLevel, uint8_t *aNonce) { diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index ed6227ea0..36efb8989 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -540,6 +540,17 @@ public: */ const LeaderDataTlv &GetLeaderDataTlv(void); + /** + * This method gets the Leader Data. + * + * @param[out] aLeaderData A reference to where the leader data is placed. + * + * @retval kThreadError_None Successfully retrieved the leader data. + * @retval kThreadError_Detached Not currently attached. + * + */ + ThreadError GetLeaderData(otLeaderData &aLeaderData); + protected: /** * This method appends an MLE header to a message.