Skip to content

Commit 8529d5c

Browse files
authored
Merge pull request #12893 from mikaleppanen/wisun_stats_5_15
Add Wi-SUN statistics interface (Mbed OS 5.15)
2 parents ffe6b49 + 7b6ee06 commit 8529d5c

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
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
@@ -399,6 +399,40 @@ class WisunInterface : public MeshInterfaceNanostack {
399399
* \param len
400400
* */
401401
bool getRouterIpAddress(char *address, int8_t len);
402+
403+
/**
404+
* \brief Enable Wi-SUN statistics
405+
*
406+
* After enabling statistics those can be read using the network, physical layer,
407+
* MAC and FHSS and Wi-SUN statistics read functions.
408+
*
409+
* \return MESH_ERROR_NONE on success.
410+
* \return MESH_ERROR_UNKNOWN on error
411+
* */
412+
mesh_error_t enable_statistics(void);
413+
414+
/**
415+
* \brief Reads Wi-SUN network statistics
416+
*
417+
* Reads network statistics.
418+
*
419+
* \param statistics Network statistics.
420+
* \return MESH_ERROR_NONE on success.
421+
* \return MESH_ERROR_UNKNOWN on error
422+
* */
423+
mesh_error_t read_nw_statistics(mesh_nw_statistics_t *statistics);
424+
425+
/**
426+
* \brief Reads Wi-SUN MAC statistics
427+
*
428+
* Reads MAC statistics.
429+
*
430+
* \param statistics MAC statistics.
431+
* \return MESH_ERROR_NONE on success.
432+
* \return MESH_ERROR_UNKNOWN on error
433+
* */
434+
mesh_error_t read_mac_statistics(mesh_mac_statistics_t *statistics);
435+
402436
protected:
403437
Nanostack::WisunInterface *get_interface() const;
404438
virtual nsapi_error_t do_initialize();

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,36 @@ mesh_error_t WisunInterface::remove_trusted_certificates(void)
512512
return ret_val;
513513
}
514514

515+
mesh_error_t WisunInterface::enable_statistics(void)
516+
{
517+
mesh_error_t ret_val = MESH_ERROR_NONE;
518+
int status = wisun_tasklet_statistics_start();
519+
if (status < 0) {
520+
ret_val = MESH_ERROR_UNKNOWN;
521+
}
522+
return ret_val;
523+
}
524+
525+
mesh_error_t WisunInterface::read_nw_statistics(mesh_nw_statistics_t *statistics)
526+
{
527+
mesh_error_t ret_val = MESH_ERROR_NONE;
528+
int status = wisun_tasklet_statistics_nw_read(statistics);
529+
if (status < 0) {
530+
ret_val = MESH_ERROR_UNKNOWN;
531+
}
532+
return ret_val;
533+
}
534+
535+
mesh_error_t WisunInterface::read_mac_statistics(mesh_mac_statistics_t *statistics)
536+
{
537+
mesh_error_t ret_val = MESH_ERROR_NONE;
538+
int status = wisun_tasklet_statistics_mac_read(statistics);
539+
if (status < 0) {
540+
ret_val = MESH_ERROR_UNKNOWN;
541+
}
542+
return ret_val;
543+
}
544+
515545
#define WISUN 0x2345
516546
#if MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == WISUN && DEVICE_802_15_4_PHY
517547
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
@@ -112,11 +112,35 @@ int wisun_tasklet_set_trusted_certificate(uint8_t *cert, uint16_t cert_len);
112112
/*
113113
* \brief Remove trusted certificate from Wi-SUN network
114114
*
115-
* \return 0 if certificates removed successfully
115+
* \return 0 if certificates removed successfully
116116
* \return < 0 in case of errors
117117
*/
118118
int wisun_tasklet_remove_trusted_certificates(void);
119119

120+
/*
121+
* \brief Start Wi-SUN statistics
122+
*
123+
* \return 0 Statistics start successful
124+
* \return < 0 in case of errors
125+
*/
126+
int wisun_tasklet_statistics_start(void);
127+
128+
/*
129+
* \brief Reads Wi-SUN network statistics
130+
*
131+
* \return 0 Statistics read successful
132+
* \return < 0 in case of errors
133+
*/
134+
int wisun_tasklet_statistics_nw_read(mesh_nw_statistics_t *stats);
135+
136+
/*
137+
* \brief Reads Wi-SUN MAC statistics
138+
*
139+
* \return 0 Statistics read successful
140+
* \return < 0 in case of errors
141+
*/
142+
int wisun_tasklet_statistics_mac_read(mesh_mac_statistics_t *stats);
143+
120144
#ifdef __cplusplus
121145
}
122146
#endif

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

Lines changed: 78 additions & 0 deletions
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) || \
@@ -89,6 +90,15 @@ static wisun_tasklet_data_str_t *wisun_tasklet_data_ptr = NULL;
8990
static wisun_certificates_t *wisun_certificates_ptr = NULL;
9091
static mac_api_t *mac_api = NULL;
9192

93+
typedef struct {
94+
nwk_stats_t nwk_stats;
95+
mac_statistics_t mac_statistics;
96+
ws_statistics_t ws_statistics;
97+
} wisun_statistics_t;
98+
99+
static bool statistics_started = false;
100+
static wisun_statistics_t *statistics = NULL;
101+
92102
extern fhss_timer_t fhss_functions;
93103

94104
/* private function prototypes */
@@ -99,6 +109,7 @@ static void wisun_tasklet_configure_and_connect_to_network(void);
99109
static void wisun_tasklet_clear_stored_certificates(void) ;
100110
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);
101111
static int wisun_tasklet_add_stored_certificates(void) ;
112+
static void wisun_tasklet_statistics_do_start(void);
102113

103114
/*
104115
* \brief A function which will be eventually called by NanoStack OS when ever the OS has an event to deliver.
@@ -300,6 +311,10 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
300311
arm_network_own_certificate_add((const arm_certificate_entry_s *)&own_cert);
301312
#endif
302313

314+
if (statistics_started) {
315+
wisun_tasklet_statistics_do_start();
316+
}
317+
303318
status = arm_nwk_interface_up(wisun_tasklet_data_ptr->network_interface_id);
304319
if (status >= 0) {
305320
wisun_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED;
@@ -566,3 +581,66 @@ int wisun_tasklet_set_trusted_certificate(uint8_t *cert, uint16_t cert_len)
566581
}
567582
return wisun_tasklet_store_certificate_data(cert, cert_len, NULL, 0, false, false, true);
568583
}
584+
585+
int wisun_tasklet_statistics_start(void)
586+
{
587+
statistics_started = true;
588+
589+
if (statistics == NULL) {
590+
statistics = ns_dyn_mem_alloc(sizeof(wisun_statistics_t));
591+
}
592+
if (statistics == NULL) {
593+
return -1;
594+
}
595+
memset(statistics, 0, sizeof(wisun_statistics_t));
596+
597+
wisun_tasklet_statistics_do_start();
598+
599+
return 0;
600+
}
601+
602+
static void wisun_tasklet_statistics_do_start(void)
603+
{
604+
if (!wisun_tasklet_data_ptr || wisun_tasklet_data_ptr->network_interface_id < 0 || !mac_api) {
605+
return;
606+
}
607+
608+
protocol_stats_start(&statistics->nwk_stats);
609+
ns_sw_mac_statistics_start(mac_api, &statistics->mac_statistics);
610+
ws_statistics_start(wisun_tasklet_data_ptr->network_interface_id, &statistics->ws_statistics);
611+
}
612+
613+
int wisun_tasklet_statistics_nw_read(mesh_nw_statistics_t *stats)
614+
{
615+
if (!statistics || !stats) {
616+
return -1;
617+
}
618+
619+
stats->rpl_total_memory = statistics->nwk_stats.rpl_total_memory;
620+
stats->etx_1st_parent = statistics->nwk_stats.etx_1st_parent;
621+
stats->etx_2nd_parent = statistics->nwk_stats.etx_2nd_parent;
622+
stats->asynch_tx_count = statistics->ws_statistics.asynch_tx_count;
623+
stats->asynch_rx_count = statistics->ws_statistics.asynch_rx_count;
624+
625+
return 0;
626+
}
627+
628+
int wisun_tasklet_statistics_mac_read(mesh_mac_statistics_t *stats)
629+
{
630+
if (!statistics || !stats) {
631+
return -1;
632+
}
633+
634+
stats->mac_rx_count = statistics->mac_statistics.mac_rx_count;
635+
stats->mac_tx_count = statistics->mac_statistics.mac_tx_count;
636+
stats->mac_bc_rx_count = statistics->mac_statistics.mac_bc_rx_count;
637+
stats->mac_bc_tx_count = statistics->mac_statistics.mac_bc_tx_count;
638+
stats->mac_tx_bytes = statistics->mac_statistics.mac_tx_bytes;
639+
stats->mac_rx_bytes = statistics->mac_statistics.mac_rx_bytes;
640+
stats->mac_tx_failed_count = statistics->mac_statistics.mac_tx_failed_count;
641+
stats->mac_retry_count = statistics->mac_statistics.mac_retry_count;
642+
stats->mac_cca_attempts_count = statistics->mac_statistics.mac_cca_attempts_count;
643+
stats->mac_failed_cca_count = statistics->mac_statistics.mac_failed_cca_count;
644+
645+
return 0;
646+
}

0 commit comments

Comments
 (0)