RPL trace analysis from Cooja logs
Reconstruct DODAG parent choices and count DIO and DAO messages from a saved Cooja trace.

Packet Pete
Packet Pete: inspect the input, calculation and observed output before choosing a route.
Predict the reading, then compare it with the measurement.
Python 3 in your browser (JupyterLite)
Python · no installOpen the notebook in your browser and run each Python cell; no install or account is needed.
Three ways to run: use JupyterLite here with no install; run main.py locally from the downloadable lab folder; or open the same notebook in Google Colab.
Steps
Step 1
- Do
- In the JupyterLite notebook, run step 1 to verify the saved Cooja log hash and parse timestamped rows.
- You will see
Run the cell. You should see:
- REAL saved Contiki-NG Cooja headless serial/test log; source=practice/cooja-rpl-parent-switch/expected-output.txt; sha256=a04f5ac80bbd7000942935d334a16369b8134cede785167bd7204ccf9ee46f05
- Why it matters
- The hash ties the analysis to the saved real simulator run.
Code for step 1import platform import pyodide print(f"Runtime: Python {platform.python_version()} / Pyodide {pyodide.__version__}") """Parse the saved, real Cooja serial/test log from the parent-switch lab.""" from collections import Counter from hashlib import sha256 from pathlib import Path import re SOURCE = "practice/cooja-rpl-parent-switch/expected-output.txt" SHA256 = "a04f5ac80bbd7000942935d334a16369b8134cede785167bd7204ccf9ee46f05" ROW = re.compile(r"^(\d+) ID:(\d+) (.*)$") SWITCH = re.compile(r"parent switch: (.*?) -> (.*)$") ADDRESS = re.compile(r"fe80::20([0-9a-f]+):", re.I) FAULT_US = 90_065_000 RESTORE_US = 210_065_000 def parent(address): match = ADDRESS.search(address) return int(match.group(1), 16) if match else None def events(text): result = [] for line in text.splitlines(): row = ROW.match(line) if row: result.append((int(row.group(1)), int(row.group(2)), row.group(3))) return result def switches(trace): result = [] for time, node, message in trace: match = SWITCH.search(message) if match: result.append((time, node, parent(match.group(1)), parent(match.group(2)))) return result def table_at(changes, time_us): table = {1: None} for when, node, old, new in changes: if when > time_us: break table[node] = new return table def table_string(table): return " ".join(f"{node}->{table.get(node) or '-'}" for node in range(1, 7)) def message_counts(trace, start=0, end=10**15): window = [(t, m) for t, _, m in trace if start <= t < end] dio = sum(bool(re.search(r"sending a (?:unicast|multicast)-DIO\b", m)) for _, m in window) dao = sum(bool(re.search(r"sending a DAO seqno\b", m)) for _, m in window) return dio, dao def run_step(n, text): trace = events(text) changes = switches(trace) if n == 1: lines = ["REAL saved Contiki-NG Cooja headless serial/test log", f"source={SOURCE}", f"sha256={sha256(text.encode()).hexdigest()}", f"parsed timestamped rows={len(trace)}; parent events={len(changes)}", "Times are simulated microseconds; no packet capture is inferred."] elif n == 2: lines = ["DODAG parent table reconstructed from parent-switch lines", "time_s node->parent (- means no selected parent)", *[f"{t/1e6:>7.3f} {table_string(table_at(changes,t))}" for t in (85_065_000, 100_000_000, 101_000_000, 140_065_000, 260_065_000)], "Root 1 has no parent; unchanged entries carry forward."] elif n == 3: target = [row for row in changes if row[1] == 5] real = next(row for row in target if row[2] == 3 and row[3] == 2) lines = ["Target 5 parent transition around relay removal", f"fault scheduled={FAULT_US/1e6:.6f} s; relay 3 moved away", f"switch={real[0]/1e6:.6f} s: node 5 parent {real[2]} -> {real[3]}", f"delay from fault={(real[0]-FAULT_US)/1e6:.6f} s", f"target parent events in log={len(target)}"] elif n == 4: around = [row for row in changes if FAULT_US <= row[0] < 140_065_000] lines = ["Other observed parent events after removal, before 140.065 s", "time_s node old->new", *[f"{t/1e6:.6f} {node} {old or '-'}->{new or '-'}" for t,node,old,new in around[:8]], f"event count in this window={len(around)}"] elif n == 5: phases = (("before fault", 0, FAULT_US), ("fault to restore", FAULT_US, RESTORE_US), ("after restore", RESTORE_US, 10**15)) total = message_counts(trace) lines = ["Outgoing RPL control messages in serial log", "phase DIO DAO (DAO-ACK excluded)", *[f"{name:<16} {message_counts(trace,a,b)[0]:>3} {message_counts(trace,a,b)[1]:>3}" for name,a,b in phases], f"total outgoing DIO={total[0]}; DAO={total[1]}", "Counts are logged sends, not on-air delivery counts."] elif n == 6: switch = next(t for t,node,old,new in changes if node==5 and old==3 and new==2) lines = ["RESULT CARD: real saved Cooja run, seed 1", f"target 5 parent 3->2 at {switch/1e6:.6f} s", f"first post-fault parent change delay={(switch-FAULT_US)/1e6:.6f} s", f"at 140.065 s: {table_string(table_at(changes,140_065_000))}", f"outgoing DIO={message_counts(trace)[0]}, DAO={message_counts(trace)[1]}", "This does not establish delivery of every control packet or global recovery."] else: raise ValueError(n) lines.append(f"STEP {n} complete") print("\n".join(lines)) from pyodide.http import pyfetch log_url='/practice/cooja-rpl-parent-switch/expected-output.txt' response=await pyfetch(log_url) assert response.ok, f'HTTP {response.status}' log_text=await response.string() assert sha256(log_text.encode()).hexdigest()==SHA256, 'Saved Cooja input changed' run_step(1, log_text)
Step 1 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 2
- Do
- In the JupyterLite notebook, run step 2 to rebuild the parent table over time.
- You will see
Run the cell. You should see:
- DODAG parent table reconstructed from parent-switch lines; time_s node->parent (- means no selected parent); 85.065 1->- 2->1 3->1 4->2 5->3 6->3
- Why it matters
- Explicit switch events provide a reproducible parent timeline.
Code for step 2run_step(2, log_text)
Step 2 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 3
- Do
- In the JupyterLite notebook, run step 3 to locate target 5’s parent switch after relay removal.
- You will see
Run the cell. You should see:
- Target 5 parent transition around relay removal; fault scheduled=90.065000 s; relay 3 moved away; switch=100.760728 s: node 5 parent 3 -> 2
- Why it matters
- The timestamp difference measures observed parent-change delay.
Code for step 3run_step(3, log_text)
Step 3 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 4
- Do
- In the JupyterLite notebook, run step 4 to inspect other parent changes.
- You will see
Run the cell. You should see:
- Other observed parent events after removal, before 140.065 s; time_s node old->new; 100.760728 5 3->2
- Why it matters
- The rest of the DODAG may behave differently from target 5.
Code for step 4run_step(4, log_text)
Step 4 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 5
- Do
- In the JupyterLite notebook, run step 5 to count outgoing DIO and DAO lines.
- You will see
Run the cell. You should see:
- Outgoing RPL control messages in serial log; phase DIO DAO (DAO-ACK excluded); before fault 47 5
- Why it matters
- Logged sends are countable without assuming delivery.
Code for step 5run_step(5, log_text)
Step 5 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 6
- Do
- In the JupyterLite notebook, run step 6 to record the result and its evidence limit.
- You will see
Run the cell. You should see:
- RESULT CARD: real saved Cooja run, seed 1; target 5 parent 3->2 at 100.760728 s; first post-fault parent change delay=10.695728 s
- Why it matters
- A target recovery result does not prove global repair.
Code for step 6run_step(6, log_text)
Step 6 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.
Chapter checks
These questions refer to the chapter’s examples. Use the return links to review their answers.
A node has selected a preferred parent after hearing compatible DIO messages. Which extra RPL evidence matters when gateway-to-node commands are part of the claim?
Return to the chapter’s knowledge checkA trace shows one DAO from a node, then a parent repair occurs. Later, root-to-node commands fail. What is the strongest RPL review response?
Return to the chapter’s knowledge check
Caution
Before you start
Read this before you start.
The input is the saved real Cooja headless log at SHA-256 a04f5ac80bbd7000942935d334a16369b8134cede785167bd7204ccf9ee46f05. Logged sends do not prove every packet arrived or that the whole DODAG recovered.
Technical details
- Version tested
- Version tested: Python 3.12.7 / Pyodide 0.27.6 in JupyterLite 0.6.4; Chromium 148.0.7778.96; captureSource playwright:jupyterlite. Date: 2026-10-08.
https://iotclass.org/labs/rpl-trace-analysis.html