Skip to content

Added Wi-SUN statistics interface #12899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions features/nanostack/mbed-mesh-api/mbed-mesh-api/WisunInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,40 @@ class WisunInterface final : public MeshInterfaceNanostack {
* \param len
* */
bool getRouterIpAddress(char *address, int8_t len);

/**
* \brief Enable Wi-SUN statistics
*
* After enabling statistics those can be read using the network, physical layer,
* MAC and FHSS and Wi-SUN statistics read functions.
*
* \return MESH_ERROR_NONE on success.
* \return MESH_ERROR_UNKNOWN on error
* */
mesh_error_t enable_statistics(void);

/**
* \brief Reads Wi-SUN network statistics
*
* Reads network statistics.
*
* \param statistics Network statistics.
* \return MESH_ERROR_NONE on success.
* \return MESH_ERROR_UNKNOWN on error
* */
mesh_error_t read_nw_statistics(mesh_nw_statistics_t *statistics);

/**
* \brief Reads Wi-SUN MAC statistics
*
* Reads MAC statistics.
*
* \param statistics MAC statistics.
* \return MESH_ERROR_NONE on success.
* \return MESH_ERROR_UNKNOWN on error
* */
mesh_error_t read_mac_statistics(mesh_mac_statistics_t *statistics);

protected:
Nanostack::WisunInterface *get_interface() const;
nsapi_error_t do_initialize() override;
Expand Down
30 changes: 30 additions & 0 deletions features/nanostack/mbed-mesh-api/source/WisunInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,36 @@ mesh_error_t WisunInterface::remove_trusted_certificates(void)
return ret_val;
}

mesh_error_t WisunInterface::enable_statistics(void)
{
mesh_error_t ret_val = MESH_ERROR_NONE;
int status = wisun_tasklet_statistics_start();
if (status < 0) {
ret_val = MESH_ERROR_UNKNOWN;
}
return ret_val;
}

mesh_error_t WisunInterface::read_nw_statistics(mesh_nw_statistics_t *statistics)
{
mesh_error_t ret_val = MESH_ERROR_NONE;
int status = wisun_tasklet_statistics_nw_read(statistics);
if (status < 0) {
ret_val = MESH_ERROR_UNKNOWN;
}
return ret_val;
}

mesh_error_t WisunInterface::read_mac_statistics(mesh_mac_statistics_t *statistics)
{
mesh_error_t ret_val = MESH_ERROR_NONE;
int status = wisun_tasklet_statistics_mac_read(statistics);
if (status < 0) {
ret_val = MESH_ERROR_UNKNOWN;
}
return ret_val;
}

#define WISUN 0x2345
#if MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == WISUN && DEVICE_802_15_4_PHY
MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()
Expand Down
26 changes: 25 additions & 1 deletion features/nanostack/mbed-mesh-api/source/include/wisun_tasklet.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,35 @@ int wisun_tasklet_set_trusted_certificate(uint8_t *cert, uint16_t cert_len);
/*
* \brief Remove trusted certificate from Wi-SUN network
*
* \return 0 if certificates removed successfully
* \return 0 if certificates removed successfully
* \return < 0 in case of errors
*/
int wisun_tasklet_remove_trusted_certificates(void);

/*
* \brief Start Wi-SUN statistics
*
* \return 0 Statistics start successful
* \return < 0 in case of errors
*/
int wisun_tasklet_statistics_start(void);

/*
* \brief Reads Wi-SUN network statistics
*
* \return 0 Statistics read successful
* \return < 0 in case of errors
*/
int wisun_tasklet_statistics_nw_read(mesh_nw_statistics_t *stats);

/*
* \brief Reads Wi-SUN MAC statistics
*
* \return 0 Statistics read successful
* \return < 0 in case of errors
*/
int wisun_tasklet_statistics_mac_read(mesh_mac_statistics_t *stats);

#ifdef __cplusplus
}
#endif
Expand Down
78 changes: 78 additions & 0 deletions features/nanostack/mbed-mesh-api/source/wisun_tasklet.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "sw_mac.h"
#include "ns_list.h"
#include "net_interface.h"
#include "nwk_stats_api.h"
#include "ws_management_api.h" //ws_management_node_init
#ifdef MBED_CONF_MBED_MESH_API_CERTIFICATE_HEADER
#if !defined(MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE) || !defined(MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE) || \
Expand Down Expand Up @@ -89,6 +90,15 @@ static wisun_tasklet_data_str_t *wisun_tasklet_data_ptr = NULL;
static wisun_certificates_t *wisun_certificates_ptr = NULL;
static mac_api_t *mac_api = NULL;

typedef struct {
nwk_stats_t nwk_stats;
mac_statistics_t mac_statistics;
ws_statistics_t ws_statistics;
} wisun_statistics_t;

static bool statistics_started = false;
static wisun_statistics_t *statistics = NULL;

extern fhss_timer_t fhss_functions;

/* private function prototypes */
Expand All @@ -99,6 +109,7 @@ static void wisun_tasklet_configure_and_connect_to_network(void);
static void wisun_tasklet_clear_stored_certificates(void) ;
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);
static int wisun_tasklet_add_stored_certificates(void) ;
static void wisun_tasklet_statistics_do_start(void);

/*
* \brief A function which will be eventually called by NanoStack OS when ever the OS has an event to deliver.
Expand Down Expand Up @@ -301,6 +312,10 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
arm_network_own_certificate_add((const arm_certificate_entry_s *)&own_cert);
#endif

if (statistics_started) {
wisun_tasklet_statistics_do_start();
}

status = arm_nwk_interface_up(wisun_tasklet_data_ptr->network_interface_id);
if (status >= 0) {
wisun_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED;
Expand Down Expand Up @@ -564,3 +579,66 @@ int wisun_tasklet_set_trusted_certificate(uint8_t *cert, uint16_t cert_len)
// Stack is inactive store the certificates and activate when connect() called
return wisun_tasklet_store_certificate_data(cert, cert_len, NULL, 0, false, false, true);
}

int wisun_tasklet_statistics_start(void)
{
statistics_started = true;

if (statistics == NULL) {
statistics = ns_dyn_mem_alloc(sizeof(wisun_statistics_t));
}
if (statistics == NULL) {
return -1;
}
memset(statistics, 0, sizeof(wisun_statistics_t));

wisun_tasklet_statistics_do_start();

return 0;
}

static void wisun_tasklet_statistics_do_start(void)
{
if (!wisun_tasklet_data_ptr || wisun_tasklet_data_ptr->network_interface_id < 0 || !mac_api) {
return;
}

protocol_stats_start(&statistics->nwk_stats);
ns_sw_mac_statistics_start(mac_api, &statistics->mac_statistics);
ws_statistics_start(wisun_tasklet_data_ptr->network_interface_id, &statistics->ws_statistics);
}

int wisun_tasklet_statistics_nw_read(mesh_nw_statistics_t *stats)
{
if (!statistics || !stats) {
return -1;
}

stats->rpl_total_memory = statistics->nwk_stats.rpl_total_memory;
stats->etx_1st_parent = statistics->nwk_stats.etx_1st_parent;
stats->etx_2nd_parent = statistics->nwk_stats.etx_2nd_parent;
stats->asynch_tx_count = statistics->ws_statistics.asynch_tx_count;
stats->asynch_rx_count = statistics->ws_statistics.asynch_rx_count;

return 0;
}

int wisun_tasklet_statistics_mac_read(mesh_mac_statistics_t *stats)
{
if (!statistics || !stats) {
return -1;
}

stats->mac_rx_count = statistics->mac_statistics.mac_rx_count;
stats->mac_tx_count = statistics->mac_statistics.mac_tx_count;
stats->mac_bc_rx_count = statistics->mac_statistics.mac_bc_rx_count;
stats->mac_bc_tx_count = statistics->mac_statistics.mac_bc_tx_count;
stats->mac_tx_bytes = statistics->mac_statistics.mac_tx_bytes;
stats->mac_rx_bytes = statistics->mac_statistics.mac_rx_bytes;
stats->mac_tx_failed_count = statistics->mac_statistics.mac_tx_failed_count;
stats->mac_retry_count = statistics->mac_statistics.mac_retry_count;
stats->mac_cca_attempts_count = statistics->mac_statistics.mac_cca_attempts_count;
stats->mac_failed_cca_count = statistics->mac_statistics.mac_failed_cca_count;

return 0;
}