You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Contiki/Cooja中为6tisch simple-node实现RPL存储模式并打印路由表

How to Print Routing Tables on Every RPL Classic Node Every 60 Seconds

Got it, let's tweak your code so every node (not just the root) prints its full routing table every minute. Since you've already switched to RPL Classic via the Makefile, each node will maintain its own routing entries—we just need to add code to iterate through those entries and print them out.

Modified Code

Here's the updated version of your process with routing table printing logic:

#include "contiki.h"
#include "sys/node-id.h"
#include "sys/log.h"
#include "net/ipv6/uip-ds6-route.h"
#include "net/ipv6/uip-sr.h"
#include "net/mac/tsch/tsch.h"
#include "net/routing/routing.h"
#define DEBUG DEBUG_PRINT
#include "net/ipv6/uip-debug.h"

/*---------------------------------------------------------------------------*/
// Helper function to print a single route entry
static void
print_route_entry(const uip_ds6_route_t *route)
{
  uint8_t i;
  PRINTF("Route to ");
  for(i = 0; i < 16; i++) {
    PRINTF("%02x", route->ipaddr.u8[i]);
    if(i < 15) {
      PRINTF(":");
    }
  }
  PRINTF(" (prefix length: %u, next hop: ", route->length);
  uip_debug_ipaddr_print(&route->nexthop);
  PRINTF(", lifetime: %u seconds)\n", route->lifetime);
}

/*---------------------------------------------------------------------------*/
PROCESS(node_process, "RPL Node");
AUTOSTART_PROCESSES(&node_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(node_process, ev, data)
{
  int is_coordinator;

  PROCESS_BEGIN();

  is_coordinator = 0;
#if CONTIKI_TARGET_COOJA || CONTIKI_TARGET_Z1
  is_coordinator = (node_id == 1);
#endif

  if(is_coordinator) {
    NETSTACK_ROUTING.root_start();
  }

  NETSTACK_MAC.on();

  {
    static struct etimer et;
    /* Print out routing tables every minute */
    etimer_set(&et, CLOCK_SECOND * 60);
    while(1) {
      PROCESS_YIELD_UNTIL(etimer_expired(&et));
      etimer_reset(&et);

      PRINTF("=== Node %u Routing Table ===\n", node_id);
      uint16_t route_count = uip_ds6_route_num_routes();
      PRINTF("Total routing entries: %u\n", route_count);

      // Iterate through all route entries and print them
      if(route_count > 0) {
        uip_ds6_route_foreach(print_route_entry, NULL);
      }
      PRINTF("============================\n");
    }
  }

  PROCESS_END();
}

Key Changes Explained

  • Added a helper function print_route_entry: This takes a single uip_ds6_route_t entry and prints its destination IPv6 address, prefix length, next-hop address, and remaining lifetime. It uses Contiki's built-in uip_debug_ipaddr_print for clean IPv6 address formatting.
  • Adjusted the timer loop: Now we wait for the timer to expire first, then print the table. This ensures the first print happens after 60 seconds (instead of immediately on startup, when no routes exist yet).
  • Used uip_ds6_route_foreach: This is the critical function for RPL Classic—it iterates over all routing entries stored on the node and calls our helper function for each one. In RPL Lite, this would only return entries on the root, but with RPL Classic enabled, every node will have its own entries.
  • Added node ID to the printout: Makes it easier to distinguish which node's table you're looking at when running multiple nodes (like in Cooja).

Important Notes

  1. Confirm your Makefile setting: Double-check that MAKE_ROUTING = MAKE_ROUTING_RPL_CLASSIC is correctly added—this is what enables the per-node routing tables in the first place.
  2. Wait for network formation: When nodes first start up, they won't have any routing entries until they've joined the RPL network. Give it a minute or two for the network to stabilize, and you'll start seeing entries appear on non-root nodes.
  3. Debug output: The DEBUG_PRINT define ensures all the PRINTF statements show up. If you're using a hardware target, make sure your serial output is set up correctly to capture these messages.

内容的提问来源于stack exchange,提问作者silverzombi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 21:47:36