Skip to content

Commit c0f3cb7

Browse files
author
Arto Kinnunen
committed
mbed-mesh-api: Add new Wi-SUN API
Add new API for setting Wi-SUN: -network name -regulatory domain, operating mode and operating class
1 parent 7e0fb8f commit c0f3cb7

File tree

4 files changed

+173
-12
lines changed

4 files changed

+173
-12
lines changed

features/nanostack/mbed-mesh-api/mbed-mesh-api/WisunInterface.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 ARM Limited. All rights reserved.
2+
* Copyright (c) 2018-2019 ARM Limited. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
* Licensed under the Apache License, Version 2.0 (the License); you may
55
* not use this file except in compliance with the License.
@@ -37,6 +37,44 @@ class WisunInterface : public MeshInterfaceNanostack {
3737
*/
3838
WisunInterface(NanostackRfPhy *phy) : MeshInterfaceNanostack(phy) { }
3939

40+
/**
41+
* \brief Set Wi-SUN network name.
42+
*
43+
* Function stores new network name to mbed-mesh-api and uses it when connect() is called next time.
44+
* If device is already connected to the Wi-SUN network then device will restart network discovery after
45+
* changing the network name.
46+
*
47+
* Function overwrites network name defined by Mbed OS configuration.
48+
*
49+
* \param network_name Network name as NUL terminated string. Can't exceed 32 characters and can't be NULL.
50+
* \return MESH_ERROR_NONE on success.
51+
* \return MESH_ERROR_UNKNOWN in case of failure.
52+
* */
53+
mesh_error_t network_name_set(char *network_name);
54+
55+
/**
56+
* \brief Set Wi-SUN network regulatory domain, operating class and operating mode.
57+
*
58+
* Function stores new parameters to mbed-mesh-api and uses them when connect() is called next time.
59+
* If device is already connected to the Wi-SUN network then device will restart network discovery after
60+
* changing the regulatory_domain, operating_class or operating_mode.
61+
*
62+
* Function overwrites parameters defined by Mbed OS configuration.
63+
*
64+
* \param regulatory_domain Values defined in Wi-SUN PHY-specification
65+
* \param operating_class Values defined in Wi-SUN PHY-specification
66+
* \param operating_mode Values defined in Wi-SUN PHY-specification
67+
* \return MESH_ERROR_NONE on success.
68+
* \return MESH_ERROR_UNKNOWN in case of failure.
69+
* */
70+
mesh_error_t network_regulatory_domain_set(uint8_t regulatory_domain = 0xff, uint8_t operating_class = 0xff, uint8_t operating_mode = 0xff);
71+
72+
/**
73+
* \brief Get router IP address
74+
*
75+
* \param address
76+
* \param len
77+
* */
4078
bool getRouterIpAddress(char *address, int8_t len);
4179
protected:
4280
Nanostack::WisunInterface *get_interface() const;

features/nanostack/mbed-mesh-api/source/WisunInterface.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 ARM Limited. All rights reserved.
2+
* Copyright (c) 2018-2019 ARM Limited. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
* Licensed under the Apache License, Version 2.0 (the License); you may
55
* not use this file except in compliance with the License.
@@ -171,6 +171,30 @@ bool WisunInterface::getRouterIpAddress(char *address, int8_t len)
171171
return _interface->get_gateway(address, len);
172172
}
173173

174+
mesh_error_t WisunInterface::network_name_set(char *network_name)
175+
{
176+
mesh_error_t ret_val = MESH_ERROR_NONE;
177+
178+
int status = wisun_tasklet_network_name_set(get_interface_id(), network_name);
179+
if (status != 0) {
180+
ret_val = MESH_ERROR_UNKNOWN;
181+
}
182+
183+
return ret_val;
184+
}
185+
186+
mesh_error_t WisunInterface::network_regulatory_domain_set(uint8_t regulatory_domain, uint8_t operating_class, uint8_t operating_mode)
187+
{
188+
mesh_error_t ret_val = MESH_ERROR_NONE;
189+
190+
int status = wisun_tasklet_regulatory_domain_set(get_interface_id(), regulatory_domain, operating_class, operating_mode);
191+
if (status != 0) {
192+
ret_val = MESH_ERROR_UNKNOWN;
193+
}
194+
195+
return ret_val;
196+
}
197+
174198
#define WISUN 0x2345
175199
#if MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == WISUN && DEVICE_802_15_4_PHY
176200
MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()

features/nanostack/mbed-mesh-api/source/include/wisun_tasklet.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 ARM Limited
2+
* Copyright (c) 2018-2019 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,6 +76,28 @@ int8_t wisun_tasklet_network_init(int8_t device_id);
7676
*/
7777
int8_t wisun_tasklet_disconnect(bool send_cb);
7878

79+
/*
80+
* \brief Set Wi-SUN network name
81+
*
82+
* \param nwk_interface_id to use for networking
83+
* \param network_name_ptr Address of the new network name. Can't be NULL.
84+
* \return 0 if network name stored successfully
85+
* \return < 0 in case of errors
86+
*/
87+
int wisun_tasklet_network_name_set(int8_t nwk_interface_id, char *network_name_ptr);
88+
89+
/*
90+
* \brief Set Wi-SUN network regulatory domain
91+
*
92+
* \param nwk_interface_id to use for networking
93+
* \param regulatory_domain
94+
* \param operating_class
95+
* \param operating_mode
96+
* \return 0 if regulatory domain is set successfully.
97+
* \return < 0 in case of errors
98+
*/
99+
int wisun_tasklet_regulatory_domain_set(int8_t nwk_interface_id, uint8_t regulatory_domain, uint8_t operating_class, uint8_t operating_mode);
100+
79101
#ifdef __cplusplus
80102
}
81103
#endif

features/nanostack/mbed-mesh-api/source/wisun_tasklet.c

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 ARM Limited. All rights reserved.
2+
* Copyright (c) 2018-2019 ARM Limited. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
* Licensed under the Apache License, Version 2.0 (the License); you may
55
* not use this file except in compliance with the License.
@@ -68,11 +68,20 @@ typedef struct {
6868
int8_t network_interface_id;
6969
} wisun_tasklet_data_str_t;
7070

71+
typedef struct {
72+
char *network_name;
73+
uint8_t regulatory_domain;
74+
uint8_t rd_operating_class;
75+
uint8_t rd_operating_mode;
76+
} wisun_network_settings_t;
77+
78+
#define WS_NA 0xff // Not applicable value
7179

7280
/* Tasklet data */
7381
static wisun_tasklet_data_str_t *wisun_tasklet_data_ptr = NULL;
82+
static wisun_network_settings_t wisun_settings_str = {NULL, WS_NA, WS_NA, WS_NA};
7483
static mac_api_t *mac_api = NULL;
75-
static char *network_name = MBED_CONF_MBED_MESH_API_WISUN_NETWORK_NAME;
84+
7685
extern fhss_timer_t fhss_functions;
7786

7887
/* private function prototypes */
@@ -201,7 +210,7 @@ static void wisun_tasklet_parse_network_event(arm_event_s *event)
201210
*/
202211
static void wisun_tasklet_configure_and_connect_to_network(void)
203212
{
204-
int8_t status;
213+
int status;
205214
fhss_timer_t *fhss_timer_ptr = &fhss_functions;
206215

207216
wisun_tasklet_data_ptr->operating_mode = NET_6LOWPAN_ROUTER;
@@ -212,10 +221,28 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
212221
wisun_tasklet_data_ptr->operating_mode,
213222
wisun_tasklet_data_ptr->operating_mode_extension);
214223

215-
ws_management_node_init(wisun_tasklet_data_ptr->network_interface_id,
216-
MBED_CONF_MBED_MESH_API_WISUN_REGULATORY_DOMAIN,
217-
network_name,
218-
fhss_timer_ptr);
224+
status = ws_management_node_init(wisun_tasklet_data_ptr->network_interface_id,
225+
MBED_CONF_MBED_MESH_API_WISUN_REGULATORY_DOMAIN,
226+
wisun_settings_str.network_name,
227+
fhss_timer_ptr);
228+
if (status < 0) {
229+
tr_error("Failed to initialize WS");
230+
return;
231+
}
232+
233+
if (wisun_settings_str.regulatory_domain != WS_NA ||
234+
wisun_settings_str.rd_operating_class != WS_NA ||
235+
wisun_settings_str.rd_operating_mode != WS_NA) {
236+
status = ws_management_regulatory_domain_set(wisun_tasklet_data_ptr->network_interface_id,
237+
wisun_settings_str.regulatory_domain,
238+
wisun_settings_str.rd_operating_class,
239+
wisun_settings_str.rd_operating_mode);
240+
241+
if (status < 0) {
242+
tr_error("Failed to set regulatory domain!");
243+
return;
244+
}
245+
}
219246

220247
#if defined(MBED_CONF_MBED_MESH_API_CERTIFICATE_HEADER)
221248
arm_certificate_chain_entry_s chain_info;
@@ -267,7 +294,7 @@ int8_t wisun_tasklet_get_router_ip_address(char *address, int8_t len)
267294

268295
int8_t wisun_tasklet_connect(mesh_interface_cb callback, int8_t nwk_interface_id)
269296
{
270-
int8_t re_connecting = true;
297+
bool re_connecting = true;
271298
int8_t tasklet_id = wisun_tasklet_data_ptr->tasklet;
272299

273300
if (wisun_tasklet_data_ptr->network_interface_id != INVALID_INTERFACE_ID) {
@@ -318,7 +345,8 @@ int8_t wisun_tasklet_disconnect(bool send_cb)
318345
void wisun_tasklet_init(void)
319346
{
320347
if (wisun_tasklet_data_ptr == NULL) {
321-
wisun_tasklet_data_ptr = ns_dyn_mem_alloc(sizeof(wisun_tasklet_data_str_t));
348+
wisun_tasklet_data_ptr = (wisun_tasklet_data_str_t *)ns_dyn_mem_alloc(sizeof(wisun_tasklet_data_str_t));
349+
// allocation not validated, in case of failure execution stops here
322350
memset(wisun_tasklet_data_ptr, 0, sizeof(wisun_tasklet_data_str_t));
323351
wisun_tasklet_data_ptr->tasklet_state = TASKLET_STATE_CREATED;
324352
wisun_tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID;
@@ -336,5 +364,54 @@ int8_t wisun_tasklet_network_init(int8_t device_id)
336364
if (!mac_api) {
337365
mac_api = ns_sw_mac_create(device_id, &storage_sizes);
338366
}
367+
368+
if (!wisun_settings_str.network_name) {
369+
// No network name set by API, use network name from configuration
370+
int wisun_network_name_len = sizeof(MBED_CONF_MBED_MESH_API_WISUN_NETWORK_NAME);
371+
wisun_settings_str.network_name = (char *)ns_dyn_mem_alloc(wisun_network_name_len);
372+
if (!wisun_settings_str.network_name) {
373+
return -3;
374+
}
375+
strncpy(wisun_settings_str.network_name, MBED_CONF_MBED_MESH_API_WISUN_NETWORK_NAME, wisun_network_name_len);
376+
}
377+
339378
return arm_nwk_interface_lowpan_init(mac_api, INTERFACE_NAME);
340379
}
380+
381+
int wisun_tasklet_network_name_set(int8_t nwk_interface_id, char *network_name_ptr)
382+
{
383+
if (!network_name_ptr || strlen(network_name_ptr) > 32) {
384+
return -1;
385+
}
386+
387+
// save the network name to have support for disconnect/connect
388+
ns_dyn_mem_free(wisun_settings_str.network_name);
389+
wisun_settings_str.network_name = (char *)ns_dyn_mem_alloc(strlen(network_name_ptr) + 1);
390+
if (!wisun_settings_str.network_name) {
391+
return -2;
392+
}
393+
394+
strcpy(wisun_settings_str.network_name, network_name_ptr);
395+
396+
if (wisun_tasklet_data_ptr && wisun_tasklet_data_ptr->tasklet_state == TASKLET_STATE_BOOTSTRAP_READY) {
397+
// interface is up, try to change name dynamically
398+
return ws_management_network_name_set(nwk_interface_id, wisun_settings_str.network_name);
399+
}
400+
401+
return 0;
402+
}
403+
404+
int wisun_tasklet_regulatory_domain_set(int8_t nwk_interface_id, uint8_t regulatory_domain, uint8_t operating_class, uint8_t operating_mode)
405+
{
406+
int status = 0;
407+
408+
wisun_settings_str.regulatory_domain = regulatory_domain;
409+
wisun_settings_str.rd_operating_class = operating_class;
410+
wisun_settings_str.rd_operating_mode = operating_mode;
411+
412+
if (wisun_tasklet_data_ptr && wisun_tasklet_data_ptr->tasklet_state == TASKLET_STATE_BOOTSTRAP_READY) {
413+
status = ws_management_regulatory_domain_set(nwk_interface_id, regulatory_domain, operating_class, operating_mode);
414+
}
415+
416+
return status;
417+
}

0 commit comments

Comments
 (0)