Flood a mesh without loops
Decide whether a node forwards a flooded packet: drop duplicates and expired ones. A medium embedded C problem (Mesh networking: Zigbee, Thread and Matter),…
medium · Mesh networking: Zigbee, Thread and Matter · mesh · routing · duplicates
The simplest way to spread a message through a mesh is flooding: every node rebroadcasts what it hears. Without care, packets circle forever. Two rules prevent that:
- duplicate detection: remember the
(source, seq)of recently seen packets (the last 8, overwriting the oldest), and drop any packet already seen, - TTL: each hop decrements the time-to-live; a packet that arrives with TTL 1 or 0 is delivered locally but not forwarded.
Write on_receive(n, p): return 0 to drop a duplicate, 1 to deliver only, or 2 to deliver and forward (in which case decrement p->ttl). Record every new packet as seen.
Starter code
int on_receive(node_t *n, packet_t *p) {
(void)n;
p->ttl--;
return 2;
}Example tests
New, then the same packet again
node_t n = {0};
rx(&n, 7, 1, 5);
rx(&n, 7, 1, 4);Expected output:
from 7 seq 1 ttl 5: forward, ttl now 4
from 7 seq 1 ttl 4: dropTTL runs out
node_t n = {0};
rx(&n, 3, 9, 1);
rx(&n, 3, 10, 0);
rx(&n, 3, 11, 2);Expected output:
from 3 seq 9 ttl 1: deliver
from 3 seq 10 ttl 0: deliver
from 3 seq 11 ttl 2: forward, ttl now 1Learn it first
- Mesh networking: Zigbee, Thread and Matter · Self-healing meshes and the IP-based smart home stack.