Introduce rpl_neighbor_snprint, for consistent printing of a RPL neighbor to a string

This commit is contained in:
Simon Duquennoy 2018-05-15 12:05:29 -07:00
parent 29061e9885
commit 97a9d7707c
2 changed files with 68 additions and 26 deletions

View File

@ -46,6 +46,7 @@
#include "net/routing/rpl-lite/rpl.h"
#include "net/link-stats.h"
#include "net/nbr-table.h"
#include "net/ipv6/uiplib.h"
/* Log configuration */
#include "sys/log.h"
@ -85,28 +86,24 @@ acceptable_rank(rpl_rank_t rank)
&& rank <= max_acceptable_rank();
}
/*---------------------------------------------------------------------------*/
void
rpl_neighbor_print_list(const char *str)
int
rpl_neighbor_snprint(char *buf, int buflen, rpl_nbr_t *nbr)
{
if(curr_instance.used) {
int curr_dio_interval = curr_instance.dag.dio_intcurrent;
int curr_rank = curr_instance.dag.rank;
rpl_nbr_t *nbr = nbr_table_head(rpl_neighbors);
int index = 0;
rpl_nbr_t *best = best_parent(0);
const struct link_stats *stats = rpl_neighbor_get_link_stats(nbr);
clock_time_t clock_now = clock_time();
LOG_INFO("nbr: own state, addr ");
LOG_INFO_6ADDR(rpl_get_global_address());
LOG_INFO_(", DAG state: %s, MOP %u OCP %u rank %u max-rank %u, dioint %u, nbr count %u (%s)\n",
rpl_dag_state_to_str(curr_instance.dag.state),
curr_instance.mop, curr_instance.of->ocp, curr_rank,
max_acceptable_rank(),
curr_dio_interval, rpl_neighbor_count(), str);
while(nbr != NULL) {
const struct link_stats *stats = rpl_neighbor_get_link_stats(nbr);
LOG_INFO("nbr: ");
LOG_INFO_6ADDR(rpl_neighbor_get_ipaddr(nbr));
LOG_INFO_(" %5u, %5u => %5u -- %2u %c%c%c%c%c",
index += snprintf(buf+index, buflen-index, "nbr: ");
if(index >= buflen) {
return index;
}
index += uiplib_ipaddr_snprint(buf+index, buflen-index, rpl_neighbor_get_ipaddr(nbr));
if(index >= buflen) {
return index;
}
index += snprintf(buf+index, buflen-index,
"%5u, %5u => %5u -- %2u %c%c%c%c%c",
nbr->rank,
rpl_neighbor_get_link_metric(nbr),
rpl_neighbor_rank_via_nbr(nbr),
@ -117,16 +114,50 @@ rpl_neighbor_print_list(const char *str)
link_stats_is_fresh(stats) ? 'f' : ' ',
nbr == curr_instance.dag.preferred_parent ? 'p' : ' '
);
if(index >= buflen) {
return index;
}
if(stats->last_tx_time > 0) {
LOG_INFO_(" (last tx %u min ago", (unsigned)((clock_now - stats->last_tx_time) / (60 * CLOCK_SECOND)));
index += snprintf(buf+index, buflen-index,
" (last tx %u min ago",
(unsigned)((clock_now - stats->last_tx_time) / (60 * CLOCK_SECOND)));
} else {
LOG_INFO_(" (no tx");
index += snprintf(buf+index, buflen-index,
" (no tx");
}
if(index >= buflen) {
return index;
}
if(nbr->better_parent_since > 0) {
LOG_INFO_(", better since %u min)\n", (unsigned)((clock_now - nbr->better_parent_since) / (60 * CLOCK_SECOND)));
index += snprintf(buf+index, buflen-index,
", better since %u min)",
(unsigned)((clock_now - nbr->better_parent_since) / (60 * CLOCK_SECOND)));
} else {
LOG_INFO_(")\n");
index += snprintf(buf+index, buflen-index,
")");
}
return index;
}
/*---------------------------------------------------------------------------*/
void
rpl_neighbor_print_list(const char *str)
{
if(curr_instance.used) {
int curr_dio_interval = curr_instance.dag.dio_intcurrent;
int curr_rank = curr_instance.dag.rank;
rpl_nbr_t *nbr = nbr_table_head(rpl_neighbors);
LOG_INFO("nbr: own state, addr ");
LOG_INFO_6ADDR(rpl_get_global_address());
LOG_INFO_(", DAG state: %s, MOP %u OCP %u rank %u max-rank %u, dioint %u, nbr count %u (%s)\n",
rpl_dag_state_to_str(curr_instance.dag.state),
curr_instance.mop, curr_instance.of->ocp, curr_rank,
max_acceptable_rank(),
curr_dio_interval, rpl_neighbor_count(), str);
while(nbr != NULL) {
char buf[120];
rpl_neighbor_snprint(buf, sizeof(buf), nbr);
LOG_INFO("%s\n", buf);
nbr = nbr_table_next(rpl_neighbors, nbr);
}
LOG_INFO("nbr: end of list\n");

View File

@ -192,6 +192,17 @@ void rpl_neighbor_remove_all(void);
*/
rpl_nbr_t *rpl_neighbor_select_best(void);
/**
* Print a textual description of RPL neighbor into a string
*
* \param buf The buffer where to write content
* \param len The buffer len
* \param addr A pointer to a RPL neighbor that will be written to the buffer
* \return Identical to snprintf: number of bytes written excluding ending null
* byte. A value >= buflen if the buffer was too small.
*/
int rpl_neighbor_snprint(char *buf, int buflen, rpl_nbr_t *nbr);
typedef rpl_nbr_t rpl_parent_t;
#define rpl_parent_get_from_ipaddr(addr) rpl_neighbor_get_from_ipaddr(addr)
#define rpl_parent_get_ipaddr(nbr) rpl_neighbor_get_ipaddr(nbr)