Skip to content

Commit e1b8f06

Browse files
authored
Merge pull request #12899 from mikaleppanen/wisun_stats_
Added Wi-SUN statistics interface
2 parents 8f87735 + ee5f077 commit e1b8f06

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
@@ -390,6 +390,40 @@ class WisunInterface final : public MeshInterfaceNanostack {
390390
* \param len
391391
* */
392392
bool getRouterIpAddress(char *address, int8_t len);
393+
394+
/**
395+
* \brief Enable Wi-SUN statistics
396+
*
397+
* After enabling statistics those can be read using the network, physical layer,
398+
* MAC and FHSS and Wi-SUN statistics read functions.
399+
*
400+
* \return MESH_ERROR_NONE on success.
401+
* \return MESH_ERROR_UNKNOWN on error
402+
* */
403+
mesh_error_t enable_statistics(void);
404+
405+
/**
406+
* \brief Reads Wi-SUN network statistics
407+
*
408+
* Reads network statistics.
409+
*
410+
* \param statistics Network statistics.
411+
* \return MESH_ERROR_NONE on success.
412+
* \return MESH_ERROR_UNKNOWN on error
413+
* */
414+
mesh_error_t read_nw_statistics(mesh_nw_statistics_t *statistics);
415+
416+
/**
417+
* \brief Reads Wi-SUN MAC statistics
418+
*
419+
* Reads MAC statistics.
420+
*
421+
* \param statistics MAC statistics.
422+
* \return MESH_ERROR_NONE on success.
423+
* \return MESH_ERROR_UNKNOWN on error
424+
* */
425+
mesh_error_t read_mac_statistics(mesh_mac_statistics_t *statistics);
426+
393427
protected:
394428
Nanostack::WisunInterface *get_interface() const;
395429
nsapi_error_t do_initialize() override;

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

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

517+
mesh_error_t WisunInterface::enable_statistics(void)
518+
{
519+
mesh_error_t ret_val = MESH_ERROR_NONE;
520+
int status = wisun_tasklet_statistics_start();
521+
if (status < 0) {
522+
ret_val = MESH_ERROR_UNKNOWN;
523+
}
524+
return ret_val;
525+
}
526+
527+
mesh_error_t WisunInterface::read_nw_statistics(mesh_nw_statistics_t *statistics)
528+
{
529+
mesh_error_t ret_val = MESH_ERROR_NONE;
530+
int status = wisun_tasklet_statistics_nw_read(statistics);
531+
if (status < 0) {
532+
ret_val = MESH_ERROR_UNKNOWN;
533+
}
534+
return ret_val;
535+
}
536+
537+
mesh_error_t WisunInterface::read_mac_statistics(mesh_mac_statistics_t *statistics)
538+
{
539+
mesh_error_t ret_val = MESH_ERROR_NONE;
540+
int status = wisun_tasklet_statistics_mac_read(statistics);
541+
if (status < 0) {
542+
ret_val = MESH_ERROR_UNKNOWN;
543+
}
544+
return ret_val;
545+
}
546+
517547
#define WISUN 0x2345
518548
#if MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == WISUN && DEVICE_802_15_4_PHY
519549
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.
@@ -301,6 +312,10 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
301312
arm_network_own_certificate_add((const arm_certificate_entry_s *)&own_cert);
302313
#endif
303314

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

0 commit comments

Comments
 (0)