Skip to content

Commit 04dacfa

Browse files
author
Arto Kinnunen
authored
Merge pull request #12766 from mikaleppanen/wisun_stats
Add Wi-SUN statistics interface
2 parents 714708d + f28b8cb commit 04dacfa

File tree

5 files changed

+194
-2
lines changed

5 files changed

+194
-2
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,40 @@ class WisunInterface : public MeshInterfaceNanostack {
131131
* \param len
132132
* */
133133
bool getRouterIpAddress(char *address, int8_t len);
134+
135+
/**
136+
* \brief Enable Wi-SUN statistics
137+
*
138+
* After enabling statistics those can be read using the network, physical layer,
139+
* MAC and FHSS and Wi-SUN statistics read functions.
140+
*
141+
* \return MESH_ERROR_NONE on success.
142+
* \return MESH_ERROR_UNKNOWN on error
143+
* */
144+
mesh_error_t enable_statistics(void);
145+
146+
/**
147+
* \brief Reads Wi-SUN network statistics
148+
*
149+
* Reads network statistics.
150+
*
151+
* \param statistics Network statistics.
152+
* \return MESH_ERROR_NONE on success.
153+
* \return MESH_ERROR_UNKNOWN on error
154+
* */
155+
mesh_error_t read_nw_statistics(mesh_nw_statistics_t *statistics);
156+
157+
/**
158+
* \brief Reads Wi-SUN MAC statistics
159+
*
160+
* Reads MAC statistics.
161+
*
162+
* \param statistics MAC statistics.
163+
* \return MESH_ERROR_NONE on success.
164+
* \return MESH_ERROR_UNKNOWN on error
165+
* */
166+
mesh_error_t read_mac_statistics(mesh_mac_statistics_t *statistics);
167+
134168
protected:
135169
Nanostack::WisunInterface *get_interface() const;
136170
virtual nsapi_error_t do_initialize();

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ typedef enum {
6464
MESH_DEVICE_TYPE_WISUN_BORDER_ROUTER /*<! Wi-SUN border router */
6565
} mesh_device_type_t;
6666

67+
/**
68+
* Mesh network statistics
69+
*/
70+
typedef struct {
71+
uint32_t rpl_total_memory; /*<! RPL current memory usage total. */
72+
uint16_t etx_1st_parent; /*<! Primary parent ETX. */
73+
uint16_t etx_2nd_parent; /*<! Secondary parent ETX. */
74+
uint32_t asynch_tx_count; /*<! Asynch TX counter */
75+
uint32_t asynch_rx_count; /*<! Asynch RX counter */
76+
} mesh_nw_statistics_t;
77+
78+
/**
79+
* Mesh physical layer statistics
80+
*/
81+
typedef struct {
82+
uint32_t mac_rx_count; /*<! MAC RX packet count. */
83+
uint32_t mac_tx_count; /*<! MAC TX packet count. */
84+
uint32_t mac_bc_rx_count; /*<! MAC broadcast RX packet count. */
85+
uint32_t mac_bc_tx_count; /*<! MAC broadcast TX packet count. */
86+
uint32_t mac_tx_bytes; /*<! MAC TX bytes count. */
87+
uint32_t mac_rx_bytes; /*<! MAC RX bytes count. */
88+
uint32_t mac_tx_failed_count; /*<! MAC TX failed count. */
89+
uint32_t mac_retry_count; /*<! MAC TX retry count. */
90+
uint32_t mac_cca_attempts_count; /*<! MAC CCA attempts count. */
91+
uint32_t mac_failed_cca_count; /*<! MAC failed CCA count. */
92+
} mesh_mac_statistics_t;
93+
6794
#ifdef __cplusplus
6895
}
6996
#endif

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,36 @@ mesh_error_t WisunInterface::remove_trusted_certificates(void)
252252
return ret_val;
253253
}
254254

255+
mesh_error_t WisunInterface::enable_statistics(void)
256+
{
257+
mesh_error_t ret_val = MESH_ERROR_NONE;
258+
int status = wisun_tasklet_statistics_start();
259+
if (status < 0) {
260+
ret_val = MESH_ERROR_UNKNOWN;
261+
}
262+
return ret_val;
263+
}
264+
265+
mesh_error_t WisunInterface::read_nw_statistics(mesh_nw_statistics_t *statistics)
266+
{
267+
mesh_error_t ret_val = MESH_ERROR_NONE;
268+
int status = wisun_tasklet_statistics_nw_read(statistics);
269+
if (status < 0) {
270+
ret_val = MESH_ERROR_UNKNOWN;
271+
}
272+
return ret_val;
273+
}
274+
275+
mesh_error_t WisunInterface::read_mac_statistics(mesh_mac_statistics_t *statistics)
276+
{
277+
mesh_error_t ret_val = MESH_ERROR_NONE;
278+
int status = wisun_tasklet_statistics_mac_read(statistics);
279+
if (status < 0) {
280+
ret_val = MESH_ERROR_UNKNOWN;
281+
}
282+
return ret_val;
283+
}
284+
255285
#define WISUN 0x2345
256286
#if MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == WISUN && DEVICE_802_15_4_PHY
257287
MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,35 @@ int wisun_tasklet_set_trusted_certificate(uint8_t *cert, uint16_t cert_len);
134134
/*
135135
* \brief Remove trusted certificate from Wi-SUN network
136136
*
137-
* \return 0 if certificates removed successfully
137+
* \return 0 if certificates removed successfully
138138
* \return < 0 in case of errors
139139
*/
140140
int wisun_tasklet_remove_trusted_certificates(void);
141141

142+
/*
143+
* \brief Start Wi-SUN statistics
144+
*
145+
* \return 0 Statistics start successful
146+
* \return < 0 in case of errors
147+
*/
148+
int wisun_tasklet_statistics_start(void);
149+
150+
/*
151+
* \brief Reads Wi-SUN network statistics
152+
*
153+
* \return 0 Statistics read successful
154+
* \return < 0 in case of errors
155+
*/
156+
int wisun_tasklet_statistics_nw_read(mesh_nw_statistics_t *stats);
157+
158+
/*
159+
* \brief Reads Wi-SUN MAC statistics
160+
*
161+
* \return 0 Statistics read successful
162+
* \return < 0 in case of errors
163+
*/
164+
int wisun_tasklet_statistics_mac_read(mesh_mac_statistics_t *stats);
165+
142166
#ifdef __cplusplus
143167
}
144168
#endif

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

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "sw_mac.h"
3030
#include "ns_list.h"
3131
#include "net_interface.h"
32+
#include "nwk_stats_api.h"
3233
#include "ws_management_api.h" //ws_management_node_init
3334
#ifdef MBED_CONF_MBED_MESH_API_CERTIFICATE_HEADER
3435
#if !defined(MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE) || !defined(MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE) || \
@@ -118,6 +119,15 @@ static wisun_network_settings_t wisun_settings_str = {
118119
};
119120
static mac_api_t *mac_api = NULL;
120121

122+
typedef struct {
123+
nwk_stats_t nwk_stats;
124+
mac_statistics_t mac_statistics;
125+
ws_statistics_t ws_statistics;
126+
} wisun_statistics_t;
127+
128+
static bool statistics_started = false;
129+
static wisun_statistics_t *statistics = NULL;
130+
121131
extern fhss_timer_t fhss_functions;
122132

123133
/* private function prototypes */
@@ -128,6 +138,7 @@ static void wisun_tasklet_configure_and_connect_to_network(void);
128138
static void wisun_tasklet_clear_stored_certificates(void) ;
129139
static int wisun_tasklet_store_certificate_data(const uint8_t *cert, uint16_t cert_len, const uint8_t *cert_key, uint16_t cert_key_len, bool remove_own, bool remove_trusted, bool trusted_cert);
130140
static int wisun_tasklet_add_stored_certificates(void) ;
141+
static void wisun_tasklet_statistics_do_start(void);
131142

132143
/*
133144
* \brief A function which will be eventually called by NanoStack OS when ever the OS has an event to deliver.
@@ -257,7 +268,6 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
257268
} else {
258269
wisun_tasklet_data_ptr->operating_mode = NET_6LOWPAN_ROUTER;
259270
}
260-
261271
wisun_tasklet_data_ptr->operating_mode_extension = NET_6LOWPAN_WS;
262272

263273
arm_nwk_interface_configure_6lowpan_bootstrap_set(
@@ -367,6 +377,10 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
367377
arm_network_own_certificate_add((const arm_certificate_entry_s *)&own_cert);
368378
#endif
369379

380+
if (statistics_started) {
381+
wisun_tasklet_statistics_do_start();
382+
}
383+
370384
status = arm_nwk_interface_up(wisun_tasklet_data_ptr->network_interface_id);
371385
if (status >= 0) {
372386
wisun_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED;
@@ -681,3 +695,66 @@ int wisun_tasklet_set_trusted_certificate(uint8_t *cert, uint16_t cert_len)
681695
}
682696
return wisun_tasklet_store_certificate_data(cert, cert_len, NULL, 0, false, false, true);
683697
}
698+
699+
int wisun_tasklet_statistics_start(void)
700+
{
701+
statistics_started = true;
702+
703+
if (statistics == NULL) {
704+
statistics = ns_dyn_mem_alloc(sizeof(wisun_statistics_t));
705+
}
706+
if (statistics == NULL) {
707+
return -1;
708+
}
709+
memset(statistics, 0, sizeof(wisun_statistics_t));
710+
711+
wisun_tasklet_statistics_do_start();
712+
713+
return 0;
714+
}
715+
716+
static void wisun_tasklet_statistics_do_start(void)
717+
{
718+
if (!wisun_tasklet_data_ptr || wisun_tasklet_data_ptr->network_interface_id < 0 || !mac_api) {
719+
return;
720+
}
721+
722+
protocol_stats_start(&statistics->nwk_stats);
723+
ns_sw_mac_statistics_start(mac_api, &statistics->mac_statistics);
724+
ws_statistics_start(wisun_tasklet_data_ptr->network_interface_id, &statistics->ws_statistics);
725+
}
726+
727+
int wisun_tasklet_statistics_nw_read(mesh_nw_statistics_t *stats)
728+
{
729+
if (!statistics || !stats) {
730+
return -1;
731+
}
732+
733+
stats->rpl_total_memory = statistics->nwk_stats.rpl_total_memory;
734+
stats->etx_1st_parent = statistics->nwk_stats.etx_1st_parent;
735+
stats->etx_2nd_parent = statistics->nwk_stats.etx_2nd_parent;
736+
stats->asynch_tx_count = statistics->ws_statistics.asynch_tx_count;
737+
stats->asynch_rx_count = statistics->ws_statistics.asynch_rx_count;
738+
739+
return 0;
740+
}
741+
742+
int wisun_tasklet_statistics_mac_read(mesh_mac_statistics_t *stats)
743+
{
744+
if (!statistics || !stats) {
745+
return -1;
746+
}
747+
748+
stats->mac_rx_count = statistics->mac_statistics.mac_rx_count;
749+
stats->mac_tx_count = statistics->mac_statistics.mac_tx_count;
750+
stats->mac_bc_rx_count = statistics->mac_statistics.mac_bc_rx_count;
751+
stats->mac_bc_tx_count = statistics->mac_statistics.mac_bc_tx_count;
752+
stats->mac_tx_bytes = statistics->mac_statistics.mac_tx_bytes;
753+
stats->mac_rx_bytes = statistics->mac_statistics.mac_rx_bytes;
754+
stats->mac_tx_failed_count = statistics->mac_statistics.mac_tx_failed_count;
755+
stats->mac_retry_count = statistics->mac_statistics.mac_retry_count;
756+
stats->mac_cca_attempts_count = statistics->mac_statistics.mac_cca_attempts_count;
757+
stats->mac_failed_cca_count = statistics->mac_statistics.mac_failed_cca_count;
758+
759+
return 0;
760+
}

0 commit comments

Comments
 (0)