/*---------------------------------------------------------------------------*/ static uint16_t print_routes(char *msg, int size) { uip_ds6_route_t *r = uip_ds6_route_head(); int len = strlen(msg); while(r != NULL && len < size + 30) { /* output a route entry */ len += sprint_addr6(&msg[len], &r->ipaddr); len += snprintf(&msg[len], size, "->"); len += sprint_addr6(&msg[len], uip_ds6_route_nexthop(r)); len += snprintf(&msg[len], size, "\n"); r = uip_ds6_route_next(r); } return len; }
/*---------------------------------------------------------------------------*/ static void res_rpl_parent_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) { uint16_t length; rpl_instance_t *instance; char message[REST_MAX_CHUNK_SIZE]; memset(message, 0, REST_MAX_CHUNK_SIZE); instance = rpl_get_instance(RPL_DEFAULT_INSTANCE); /* Print the parent */ if(instance != NULL && instance->current_dag != NULL && instance->current_dag->preferred_parent != NULL) { sprintf(&message[0], "RPL Parent: "); sprint_addr6(&message[0], rpl_get_parent_ipaddr(instance->current_dag->preferred_parent)); } else { sprintf(&message[0], "No parent yet\n"); } length = strlen(&message[0]); memcpy(buffer, message, length); REST.set_header_content_type(response, REST.type.TEXT_PLAIN); REST.set_header_etag(response, (uint8_t *)&length, 1); REST.set_response_payload(response, buffer, length); }
/*---------------------------------------------------------------------------*/ static uint16_t print_prefixes(char *msg) { sprint_time(msg, uptime_min); int i; for (i=0; i<UIP_DS6_PREFIX_NB; i++) { uip_ds6_prefix_t *prefix = &uip_ds6_prefix_list[i]; if (prefix->isused) { sprintf(msg+strlen(msg), "Addr: "); sprint_addr6(msg, &prefix->ipaddr); #if UIP_MULTI_IFACES uint8_t if_type = prefix->iface == NULL ? 0 : prefix->iface->ll_type; sprintf(msg+strlen(msg), " Len: %u, IF: %u\n", prefix->length, if_type); #else sprintf(msg+strlen(msg), "\n"); #endif } } return strlen(msg); }
/*---------------------------------------------------------------------------*/ static void res_rpl_info_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) { /*Return RPL information in JSON format */ uint16_t length = 0; rpl_instance_t *instance; uip_ds6_nbr_t *nbr; char message[REST_MAX_CHUNK_SIZE]; memset(message, 0, sizeof(message)); instance = rpl_get_instance(RPL_DEFAULT_INSTANCE); if(instance != NULL && instance->current_dag != NULL && instance->current_dag->preferred_parent != NULL) { nbr = rpl_get_nbr(instance->current_dag->preferred_parent); } else { nbr = NULL; } /* Write all RPL info in JSON format */ snprintf(message, sizeof(message) - 1, "{\"parent\":\""); length = strlen(message); if(instance != NULL && instance->current_dag != NULL && instance->current_dag->preferred_parent != NULL) { sprint_addr6(&message[length], rpl_get_parent_ipaddr(instance->current_dag->preferred_parent)); length = strlen(message); snprintf(&message[length], sizeof(message) - length - 1, "\""); } else { snprintf(&message[length], sizeof(message) - length - 1, "None\""); } length = strlen(message); snprintf(&message[length], sizeof(message) - length - 1, " ,\"rank\":\""); length = strlen(message); if(instance != NULL && instance->current_dag != NULL) { snprintf(&message[length], sizeof(message) - length - 1, "%u.%02u\"", (instance->current_dag->rank / RPL_DAG_MC_ETX_DIVISOR), (100 * (instance->current_dag->rank % RPL_DAG_MC_ETX_DIVISOR)) / RPL_DAG_MC_ETX_DIVISOR); } else { snprintf(&message[length], sizeof(message) - length - 1, "inf\""); } length = strlen(message); snprintf(&message[length], sizeof(message) - length - 1, " ,\"link-metric\":\""); length = strlen(message); if(nbr != NULL) { snprintf(&message[length], sizeof(message) - length - 1, "%u.%02u\"", nbr->link_metric / RPL_DAG_MC_ETX_DIVISOR, (100 * (nbr->link_metric % RPL_DAG_MC_ETX_DIVISOR)) / RPL_DAG_MC_ETX_DIVISOR); } else { snprintf(&message[length], sizeof(message) - length - 1, "inf\""); } length = strlen(message); snprintf(&message[length], sizeof(message) - length - 1, "}"); length = strlen(message); memcpy(buffer, message, length); REST.set_header_content_type(response, REST.type.APPLICATION_JSON); REST.set_header_etag(response, (uint8_t *)&length, 1); REST.set_response_payload(response, buffer, length); }