diff --git a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h index be8397d290..b79dabf633 100644 --- a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h +++ b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h @@ -82,7 +82,7 @@ protected: int8_t _network_interface_id; /** Registered device ID */ int8_t _device_id; - uint8_t eui64[8]; + uint8_t _eui64[8]; char ip_addr_str[40]; char mac_addr_str[24]; Semaphore connect_semaphore; diff --git a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h index 5aeba87e7f..a1eacf1d41 100644 --- a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h +++ b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h @@ -34,6 +34,25 @@ public: ThreadInterface(NanostackRfPhy *phy) : MeshInterfaceNanostack(phy) { } nsapi_error_t initialize(NanostackRfPhy *phy); + + /** + * \brief Sets the eui64 for the device configuration. + * By default this value is read from the radio driver. + * The value must be set before calling the connect function. + * */ + void device_eui64_set(const 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 + * The value must be set before calling the connect function. + * \return MESH_ERROR_NONE on success. + * \return MESH_ERROR_PARAM in case of illegal parameters. + * \return MESH_ERROR_MEMORY in case of memory error. + * */ + + mesh_error_t device_pskd_set(const char *pskd); + virtual int connect(); virtual int disconnect(); private: diff --git a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/MeshInterfaceNanostack.cpp b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/MeshInterfaceNanostack.cpp index 6ff4892fb3..888568c9aa 100644 --- a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/MeshInterfaceNanostack.cpp +++ b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/MeshInterfaceNanostack.cpp @@ -19,7 +19,7 @@ #include "mesh_system.h" MeshInterfaceNanostack::MeshInterfaceNanostack() - : phy(NULL), _network_interface_id(-1), _device_id(-1), eui64(), + : phy(NULL), _network_interface_id(-1), _device_id(-1), _eui64(), ip_addr_str(), mac_addr_str(), connect_semaphore(0) { // Nothing to do @@ -62,8 +62,13 @@ nsapi_error_t MeshInterfaceNanostack::register_phy() return -1; } // Read mac address after registering the device. - phy->get_mac_address(eui64); - sprintf(mac_addr_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", eui64[0], eui64[1], eui64[2], eui64[3], eui64[4], eui64[5], eui64[6], eui64[7]); + const uint8_t empty_eui64[8] = {0,0,0,0,0,0,0,0}; + // if not set by application then read from rf driver + if(!memcmp(_eui64, empty_eui64,8)) { + phy->get_mac_address(_eui64); + } + + sprintf(mac_addr_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", _eui64[0], _eui64[1], _eui64[2], _eui64[3], _eui64[4], _eui64[5], _eui64[6], _eui64[7]); nanostack_unlock(); diff --git a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/ThreadInterface.cpp b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/ThreadInterface.cpp index 350fb665cb..5511653bb0 100644 --- a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/ThreadInterface.cpp +++ b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/ThreadInterface.cpp @@ -20,7 +20,6 @@ int ThreadInterface::connect() nanostack_unlock(); return NSAPI_ERROR_DEVICE_ERROR; } - // After the RF is up, we can seed the random from it. randLIB_seed_random(); @@ -64,12 +63,9 @@ int ThreadInterface::disconnect() mesh_error_t ThreadInterface::init() { - if (eui64 == NULL) { - return MESH_ERROR_PARAM; - } thread_tasklet_init(); __mesh_handler_set_callback(this); - thread_tasklet_device_config_set(eui64, NULL); + thread_tasklet_device_eui64_set(_eui64); _network_interface_id = thread_tasklet_network_init(_device_id); if (_network_interface_id == -2) { @@ -111,6 +107,15 @@ mesh_error_t ThreadInterface::mesh_connect() } } +void ThreadInterface::device_eui64_set(const uint8_t *eui64) +{ + memcpy(_eui64, eui64, 8); +} + +mesh_error_t ThreadInterface::device_pskd_set(const char *pskd) +{ + return (mesh_error_t)thread_tasklet_device_pskd_set(pskd); +} mesh_error_t ThreadInterface::mesh_disconnect() { diff --git a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/include/thread_tasklet.h b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/include/thread_tasklet.h index d544242164..97777279e2 100644 --- a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/include/thread_tasklet.h +++ b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/include/thread_tasklet.h @@ -68,11 +68,17 @@ void thread_tasklet_init(void); int8_t thread_tasklet_network_init(int8_t device_id); /* - * \brief Set device configuration for thread network - * \param eui64 mac address of the registered rf device + * \brief Sets eui64 for the device configuration + * \param eui64 eui64 to be set * \param pskd private shared key */ -void thread_tasklet_device_config_set(uint8_t *eui64, char *pskd); +void thread_tasklet_device_eui64_set(const uint8_t *eui64); + +/* + * \brief Sets PSKd for the device configuration + * \param pskd private shared key to be set + */ +uint8_t thread_tasklet_device_pskd_set(const char *pskd); /* * \brief Disconnect network interface. diff --git a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/thread_tasklet.c b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/thread_tasklet.c index 5a307159da..d936d62bfe 100644 --- a/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/thread_tasklet.c +++ b/features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/thread_tasklet.c @@ -278,14 +278,11 @@ void thread_tasklet_configure_and_connect_to_network(void) // PSKd const char PSKd[] = MBED_CONF_MBED_MESH_API_THREAD_PSKD; - MBED_ASSERT(sizeof(PSKd) > 5 && sizeof(PSKd) < 33); + if(device_configuration.PSKd_len==0) { + int ret = thread_tasklet_device_pskd_set(PSKd); + MBED_ASSERT(!ret); + } - char *dyn_buf = ns_dyn_mem_alloc(sizeof(PSKd)); - strcpy(dyn_buf, PSKd); - ns_dyn_mem_free(device_configuration.PSKd_ptr); - device_configuration.PSKd_ptr = (uint8_t*)dyn_buf; - device_configuration.PSKd_len = sizeof(PSKd) - 1; - if (true == MBED_CONF_MBED_MESH_API_THREAD_USE_STATIC_LINK_CONFIG) { read_link_configuration(); temp_link_config = &thread_tasklet_data_ptr->link_config; @@ -436,12 +433,29 @@ int8_t thread_tasklet_network_init(int8_t device_id) return arm_nwk_interface_lowpan_init(api, INTERFACE_NAME); } -void thread_tasklet_device_config_set(uint8_t *eui64, char *pskd) +void thread_tasklet_device_eui64_set(const uint8_t *eui64) { - (void) pskd; // this parameter is delivered via mbed configuration memcpy(device_configuration.eui64, eui64, 8); } +uint8_t thread_tasklet_device_pskd_set(const char *pskd) +{ + int len = strlen(pskd); + if(len < 6 || len > 32) { + return MESH_ERROR_PARAM; + } + char *dyn_buf = ns_dyn_mem_alloc(strlen(pskd)+1); + if (!dyn_buf) { + return MESH_ERROR_MEMORY; + } + strcpy(dyn_buf, pskd); + ns_dyn_mem_free(device_configuration.PSKd_ptr); + device_configuration.PSKd_ptr = (uint8_t*)dyn_buf; + device_configuration.PSKd_len = strlen(pskd); + return 0; +} + + int8_t thread_tasklet_data_poll_rate_set(uint32_t timeout) { int8_t status = -1;