Skip to content

Commit 65e751b

Browse files
authored
Merge pull request #6025 from karsev/thread_api_adds
Mesh-api setters for eui64 and pskd
2 parents e8efe11 + f63dbf5 commit 65e751b

File tree

6 files changed

+70
-21
lines changed

6 files changed

+70
-21
lines changed

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class MeshInterfaceNanostack : public MeshInterface {
8282
int8_t _network_interface_id;
8383
/** Registered device ID */
8484
int8_t _device_id;
85-
uint8_t eui64[8];
85+
uint8_t _eui64[8];
8686
char ip_addr_str[40];
8787
char mac_addr_str[24];
8888
Semaphore connect_semaphore;

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ class ThreadInterface : public MeshInterfaceNanostack {
3434
ThreadInterface(NanostackRfPhy *phy) : MeshInterfaceNanostack(phy) { }
3535

3636
nsapi_error_t initialize(NanostackRfPhy *phy);
37+
38+
/**
39+
* \brief Sets the eui64 for the device configuration.
40+
* By default this value is read from the radio driver.
41+
* The value must be set before calling the connect function.
42+
* */
43+
void device_eui64_set(const uint8_t *eui64);
44+
45+
/**
46+
* \brief sets the PSKd for the device configuration.
47+
* The default value is overwritten, which is defined in the mbed_lib.json file in the mesh-api
48+
* The value must be set before calling the connect function.
49+
* \return MESH_ERROR_NONE on success.
50+
* \return MESH_ERROR_PARAM in case of illegal parameters.
51+
* \return MESH_ERROR_MEMORY in case of memory error.
52+
* */
53+
54+
mesh_error_t device_pskd_set(const char *pskd);
55+
3756
virtual int connect();
3857
virtual int disconnect();
3958
private:

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/MeshInterfaceNanostack.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "mesh_system.h"
2020

2121
MeshInterfaceNanostack::MeshInterfaceNanostack()
22-
: phy(NULL), _network_interface_id(-1), _device_id(-1), eui64(),
22+
: phy(NULL), _network_interface_id(-1), _device_id(-1), _eui64(),
2323
ip_addr_str(), mac_addr_str(), connect_semaphore(0)
2424
{
2525
// Nothing to do
@@ -62,8 +62,13 @@ nsapi_error_t MeshInterfaceNanostack::register_phy()
6262
return -1;
6363
}
6464
// Read mac address after registering the device.
65-
phy->get_mac_address(eui64);
66-
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]);
65+
const uint8_t empty_eui64[8] = {0,0,0,0,0,0,0,0};
66+
// if not set by application then read from rf driver
67+
if(!memcmp(_eui64, empty_eui64,8)) {
68+
phy->get_mac_address(_eui64);
69+
}
70+
71+
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]);
6772

6873
nanostack_unlock();
6974

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/ThreadInterface.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ int ThreadInterface::connect()
2020
nanostack_unlock();
2121
return NSAPI_ERROR_DEVICE_ERROR;
2222
}
23-
2423
// After the RF is up, we can seed the random from it.
2524
randLIB_seed_random();
2625

@@ -64,12 +63,9 @@ int ThreadInterface::disconnect()
6463

6564
mesh_error_t ThreadInterface::init()
6665
{
67-
if (eui64 == NULL) {
68-
return MESH_ERROR_PARAM;
69-
}
7066
thread_tasklet_init();
7167
__mesh_handler_set_callback(this);
72-
thread_tasklet_device_config_set(eui64, NULL);
68+
thread_tasklet_device_eui64_set(_eui64);
7369
_network_interface_id = thread_tasklet_network_init(_device_id);
7470

7571
if (_network_interface_id == -2) {
@@ -111,6 +107,15 @@ mesh_error_t ThreadInterface::mesh_connect()
111107
}
112108
}
113109

110+
void ThreadInterface::device_eui64_set(const uint8_t *eui64)
111+
{
112+
memcpy(_eui64, eui64, 8);
113+
}
114+
115+
mesh_error_t ThreadInterface::device_pskd_set(const char *pskd)
116+
{
117+
return (mesh_error_t)thread_tasklet_device_pskd_set(pskd);
118+
}
114119

115120
mesh_error_t ThreadInterface::mesh_disconnect()
116121
{

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/include/thread_tasklet.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,17 @@ void thread_tasklet_init(void);
6868
int8_t thread_tasklet_network_init(int8_t device_id);
6969

7070
/*
71-
* \brief Set device configuration for thread network
72-
* \param eui64 mac address of the registered rf device
71+
* \brief Sets eui64 for the device configuration
72+
* \param eui64 eui64 to be set
7373
* \param pskd private shared key
7474
*/
75-
void thread_tasklet_device_config_set(uint8_t *eui64, char *pskd);
75+
void thread_tasklet_device_eui64_set(const uint8_t *eui64);
76+
77+
/*
78+
* \brief Sets PSKd for the device configuration
79+
* \param pskd private shared key to be set
80+
*/
81+
uint8_t thread_tasklet_device_pskd_set(const char *pskd);
7682

7783
/*
7884
* \brief Disconnect network interface.

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/thread_tasklet.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,11 @@ void thread_tasklet_configure_and_connect_to_network(void)
278278

279279
// PSKd
280280
const char PSKd[] = MBED_CONF_MBED_MESH_API_THREAD_PSKD;
281-
MBED_ASSERT(sizeof(PSKd) > 5 && sizeof(PSKd) < 33);
281+
if(device_configuration.PSKd_len==0) {
282+
int ret = thread_tasklet_device_pskd_set(PSKd);
283+
MBED_ASSERT(!ret);
284+
}
282285

283-
char *dyn_buf = ns_dyn_mem_alloc(sizeof(PSKd));
284-
strcpy(dyn_buf, PSKd);
285-
ns_dyn_mem_free(device_configuration.PSKd_ptr);
286-
device_configuration.PSKd_ptr = (uint8_t*)dyn_buf;
287-
device_configuration.PSKd_len = sizeof(PSKd) - 1;
288-
289286
if (true == MBED_CONF_MBED_MESH_API_THREAD_USE_STATIC_LINK_CONFIG) {
290287
read_link_configuration();
291288
temp_link_config = &thread_tasklet_data_ptr->link_config;
@@ -436,12 +433,29 @@ int8_t thread_tasklet_network_init(int8_t device_id)
436433
return arm_nwk_interface_lowpan_init(api, INTERFACE_NAME);
437434
}
438435

439-
void thread_tasklet_device_config_set(uint8_t *eui64, char *pskd)
436+
void thread_tasklet_device_eui64_set(const uint8_t *eui64)
440437
{
441-
(void) pskd; // this parameter is delivered via mbed configuration
442438
memcpy(device_configuration.eui64, eui64, 8);
443439
}
444440

441+
uint8_t thread_tasklet_device_pskd_set(const char *pskd)
442+
{
443+
int len = strlen(pskd);
444+
if(len < 6 || len > 32) {
445+
return MESH_ERROR_PARAM;
446+
}
447+
char *dyn_buf = ns_dyn_mem_alloc(strlen(pskd)+1);
448+
if (!dyn_buf) {
449+
return MESH_ERROR_MEMORY;
450+
}
451+
strcpy(dyn_buf, pskd);
452+
ns_dyn_mem_free(device_configuration.PSKd_ptr);
453+
device_configuration.PSKd_ptr = (uint8_t*)dyn_buf;
454+
device_configuration.PSKd_len = strlen(pskd);
455+
return 0;
456+
}
457+
458+
445459
int8_t thread_tasklet_data_poll_rate_set(uint32_t timeout)
446460
{
447461
int8_t status = -1;

0 commit comments

Comments
 (0)