From fc5c2fa84b2490b99796ffe5ceaa966069a5c90e Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Thu, 7 Jun 2018 12:06:59 +0300 Subject: [PATCH] Add API to read Thread EUI-64 Previously get_mac_address on a ThreadInterface returned the EUI-64 reported by the radio driver. This was required for commissioning, but was inconsistent with other interfaces, and the API concept. 5.9.0 inadvertently changed this so that get_mac_address returned the actual MAC address used by the radio, which is a hash result of the EUI-64 for Thread. The original "return the EUI-64" form was somewhat faulty, as get_mac_address would not return the EUI-64 set by set_device_eui64() or another mechanism before connect() was called. Rather than revert to old behaviour, add a new API to get the device EUI-64 to ThreadInterface, alongside the existing set API. --- .../mbed-mesh-api/ThreadInterface.h | 14 +++++--- .../mbed-mesh-api/source/ThreadInterface.cpp | 35 ++++++++++++++----- .../source/include/thread_tasklet.h | 7 +++- .../mbed-mesh-api/source/thread_tasklet.c | 5 +++ 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/features/nanostack/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h b/features/nanostack/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h index 6033d68b54..2e3fe9990f 100644 --- a/features/nanostack/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h +++ b/features/nanostack/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h @@ -30,12 +30,12 @@ public: * * Must initialize to initialize the mesh on a phy. */ - ThreadInterface() : user_set_eui64(false) { } + ThreadInterface() { } /** Create an initialized ThreadInterface * */ - ThreadInterface(NanostackRfPhy *phy) : MeshInterfaceNanostack(phy), user_set_eui64(false) { } + ThreadInterface(NanostackRfPhy *phy) : MeshInterfaceNanostack(phy) { } /** * \brief Sets the eui64 for the device configuration. @@ -44,6 +44,13 @@ public: * */ void device_eui64_set(const uint8_t *eui64); + /** + * \brief Reads the eui64 from the device configuration. + * By default this value is read from the radio driver, but it may have + * been set by device_eui64_set(). + * */ + void device_eui64_get(uint8_t *eui64); + /** * \brief sets the PSKd for the device configuration. * The default value is overwritten, which is defined in the mbed_lib.json file in the mesh-api @@ -59,9 +66,6 @@ public: virtual int disconnect(); protected: Nanostack::ThreadInterface *get_interface() const; - -private: - bool user_set_eui64; }; #endif // THREADINTERFACE_H diff --git a/features/nanostack/mbed-mesh-api/source/ThreadInterface.cpp b/features/nanostack/mbed-mesh-api/source/ThreadInterface.cpp index 3b2c416bcc..14d7ab51f3 100644 --- a/features/nanostack/mbed-mesh-api/source/ThreadInterface.cpp +++ b/features/nanostack/mbed-mesh-api/source/ThreadInterface.cpp @@ -18,7 +18,7 @@ public: friend Nanostack; friend class ::ThreadInterface; private: - ThreadInterface(NanostackRfPhy &phy) : MeshInterface(phy), user_eui64_set(false) { } + ThreadInterface(NanostackRfPhy &phy) : MeshInterface(phy), eui64_set(false) { } /* * \brief Initialization of the interface. @@ -52,6 +52,11 @@ private: * */ void device_eui64_set(const uint8_t *eui64); + /** + * \brief Reads the eui64 from the device configuration. + * */ + void device_eui64_get(uint8_t *eui64); + /** * \brief sets the PSKd for the device configuration. * The default value is overwritten, which is defined in the mbed_lib.json file in the mesh-api @@ -63,7 +68,7 @@ private: mesh_error_t device_pskd_set(const char *pskd); - bool user_eui64_set; + bool eui64_set; }; Nanostack::ThreadInterface *ThreadInterface::get_interface() const @@ -161,11 +166,7 @@ mesh_error_t Nanostack::ThreadInterface::init() { thread_tasklet_init(); __mesh_handler_set_callback(this); - if (!user_eui64_set) { - uint8_t eui64[8]; - get_phy().get_mac_address(eui64); - thread_tasklet_device_eui64_set(eui64); - } + device_eui64_get(NULL); // Ensure we've selected the EUI-64 - this does it interface_id = thread_tasklet_network_init(_device_id); if (interface_id == -2) { @@ -216,12 +217,30 @@ void ThreadInterface::device_eui64_set(const uint8_t *eui64) get_interface()->device_eui64_set(eui64); } +void ThreadInterface::device_eui64_get(uint8_t *eui64) +{ + get_interface()->device_eui64_get(eui64); +} + void Nanostack::ThreadInterface::device_eui64_set(const uint8_t *eui64) { - user_eui64_set = true; + eui64_set = true; thread_tasklet_device_eui64_set(eui64); } +void Nanostack::ThreadInterface::device_eui64_get(uint8_t *eui64) +{ + if (!eui64_set) { + uint8_t eui64_buf[8]; + get_phy().get_mac_address(eui64_buf); + device_eui64_set(eui64_buf); + } + + if (eui64) { + thread_tasklet_device_eui64_get(eui64); + } +} + mesh_error_t ThreadInterface::device_pskd_set(const char *pskd) { return get_interface()->device_pskd_set(pskd); diff --git a/features/nanostack/mbed-mesh-api/source/include/thread_tasklet.h b/features/nanostack/mbed-mesh-api/source/include/thread_tasklet.h index e26807e95f..610ca33d5f 100644 --- a/features/nanostack/mbed-mesh-api/source/include/thread_tasklet.h +++ b/features/nanostack/mbed-mesh-api/source/include/thread_tasklet.h @@ -59,10 +59,15 @@ int8_t thread_tasklet_network_init(int8_t device_id); /* * \brief Sets eui64 for the device configuration * \param eui64 eui64 to be set - * \param pskd private shared key */ void thread_tasklet_device_eui64_set(const uint8_t *eui64); +/* + * \brief Gets eui64 from the device configuration + * \param eui64 buffer for output eui64 + */ +void thread_tasklet_device_eui64_get(uint8_t *eui64); + /* * \brief Sets PSKd for the device configuration * \param pskd private shared key to be set diff --git a/features/nanostack/mbed-mesh-api/source/thread_tasklet.c b/features/nanostack/mbed-mesh-api/source/thread_tasklet.c index d9b018ad4d..8859c48ef3 100644 --- a/features/nanostack/mbed-mesh-api/source/thread_tasklet.c +++ b/features/nanostack/mbed-mesh-api/source/thread_tasklet.c @@ -474,6 +474,11 @@ void thread_tasklet_device_eui64_set(const uint8_t *eui64) memcpy(device_configuration.eui64, eui64, 8); } +void thread_tasklet_device_eui64_get(uint8_t *eui64) +{ + memcpy(eui64, device_configuration.eui64, 8); +} + uint8_t thread_tasklet_device_pskd_set(const char *pskd) { int len = strlen(pskd);