From bc9934ee5a9cc511a10c9e383dd86229c58d0de0 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Fri, 18 May 2018 05:52:09 -0700 Subject: [PATCH] Implement function uip_sr_link_snprint --- os/net/ipv6/uip-sr.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ os/net/ipv6/uip-sr.h | 11 +++++++++++ 2 files changed, 57 insertions(+) diff --git a/os/net/ipv6/uip-sr.c b/os/net/ipv6/uip-sr.c index 15b123552..a82491827 100644 --- a/os/net/ipv6/uip-sr.c +++ b/os/net/ipv6/uip-sr.c @@ -41,6 +41,7 @@ #include "contiki.h" #include "net/ipv6/uip-sr.h" +#include "net/ipv6/uiplib.h" #include "net/routing/routing.h" #include "lib/list.h" #include "lib/memb.h" @@ -246,4 +247,49 @@ uip_sr_free_all(void) num_nodes--; } } +/*---------------------------------------------------------------------------*/ +int +uip_sr_link_snprint(char *buf, int buflen, uip_sr_node_t *link) +{ + int index = 0; + uip_ipaddr_t child_ipaddr; + uip_ipaddr_t parent_ipaddr; + + NETSTACK_ROUTING.get_sr_node_ipaddr(&child_ipaddr, link); + NETSTACK_ROUTING.get_sr_node_ipaddr(&parent_ipaddr, link->parent); + + index += uiplib_ipaddr_snprint(buf+index, buflen-index, &child_ipaddr); + if(index >= buflen) { + return index; + } + + if(link->parent == NULL) { + index += snprintf(buf+index, buflen-index, " (DODAG root)"); + if(index >= buflen) { + return index; + } + } else { + index += snprintf(buf+index, buflen-index, " to "); + if(index >= buflen) { + return index; + } + index += uiplib_ipaddr_snprint(buf+index, buflen-index, &parent_ipaddr); + if(index >= buflen) { + return index; + } + } + if(link->lifetime != UIP_SR_INFINITE_LIFETIME) { + index += snprintf(buf+index, buflen-index, + " (lifetime: %lu seconds)", (unsigned long)link->lifetime); + if(index >= buflen) { + return index; + } + } else { + index += snprintf(buf+index, buflen-index, " (lifetime: infinite)"); + if(index >= buflen) { + return index; + } + } + return index; +} /** @} */ diff --git a/os/net/ipv6/uip-sr.h b/os/net/ipv6/uip-sr.h index 7224cea14..01ed57c1c 100644 --- a/os/net/ipv6/uip-sr.h +++ b/os/net/ipv6/uip-sr.h @@ -180,6 +180,17 @@ void uip_sr_init(void); */ void uip_sr_free_all(void); +/** +* Print a textual description of a source routing link +* +* \param buf The buffer where to write content +* \param buflen The buffer len +* \param link A pointer to the source routing link +* \return Identical to snprintf: number of bytes written excluding ending null +* byte. A value >= buflen if the buffer was too small. +*/ +int uip_sr_link_snprint(char *buf, int buflen, uip_sr_node_t *link); + /** @} */ #endif /* UIP_SR_H */