# On Applicability of Network Coding Technique for 6 LoWPAN-based Sensor Networks
## 3 Network Coding for 6 LoWPAN-Based Wireless Sensor Networks
### Network Coding for 6 LoWPAN (NC6 LoWPAN) WSNs
3:Initialize: \(\mathit{NCO}\mathit{Opportunity}=\) FALSE
4:Read \(\mathit{DestAddr}\) of the \(\mathit{CurrentNativePacket}\)
5:if\(\mathit{DestAddr}\) = \(\mathit{UnicastAddr}\)then
6: Read \(\mathit{NextHeader}\) field of the \(\mathit{CurrentNativePacket}\)
7:if\(\mathit{NextHeader}=\mathit{ICMPv6}\)then
8: Read \(\mathit{MessageType}\) of the \(\mathit{CurrentNativePacket}\)
9:if\(\mathit{MessageType}=\mathit{RPL}\) or \(\mathit{MessageType}=\mathit{IPv6 NeighborDiscoveryTypes}\)then
10:\(\mathit{OutputPacket}\leftarrow\mathit{CurrentNativePacket}\)
11:endif
12:if\(\mathit{DestAddr}=\mathit{MulticastAddr}\)then
13:\(\mathit{OutputPacket}\leftarrow\mathit{CurrentNativePacket}\)
14:endif
15: Read \(\mathit{SrcAddr}\) of the \(\mathit{CurrentNativePacket}\)
16:if\(\mathit{SrcAddr}=\mathit{CurrentNodeAddr}\)then
17:\(\mathit{CurrentNodeRole}\leftarrow\mathit{SourceNode}\)
18:else
19:\(\mathit{CurrentNodeRole}\leftarrow\mathit{RelayNode}\)
20:endif
21:if\(\mathit{CurrentNodeRole}=\mathit{SourceNode}\)then
22: Read \(\mathit{NextHopAddr}\) from the forwarding table of the current node
23:if\(\mathit{NextHopAddr}=\mathit{DestAddr}\)then
24:\(\mathit{OutputPacket}\leftarrow\mathit{CurrentNativePacket}\)
25:else
26:\(\mathit{BufferedNativePacket}\leftarrow\mathit{Copy}\) of \(\mathit{CurrentNativePacket}\)
27:\(\mathit{BufferedNativePacket_role}=\mathit{SourceNode}\)
28: Set \(\mathit{BufferedNativePacket_tz}=\mathit{tc}\)
29:\(\mathit{NCBuffer}\leftarrow\mathit{BufferedNativePacket}\)
30:\(\mathit{OutputPacket}\leftarrow\mathit{CurrentNativePacket}\)
31:endif
32:endif
33:if\(\mathit{CurrentNodeRole}=\mathit{RelayNode}\)then
34:for each\(\mathit{BufferedNativePacket}\in\mathit{NCBuffer}\)do
35:\(\mathit{NCO}\mathit{opportunity}\leftarrow\mathit{Check}\) NC opportunity (\(\mathit{BufferedNativePacket}\), \(\mathit{CurrentNativePacket}\))
36:if\(\mathit{NCO}\mathit{opportunity}=\mathit{TRUE}\)then
37:\(\mathit{NetworkCodedPacket}\leftarrow\mathit{Encode}\) (\(\mathit{BufferedNativePacket}\), \(\mathit{CurrentNativePacket}\))
38:\(\mathit{OutputPacket}\leftarrow\mathit{NetworkCodedPacket}\)
39:else
40:\(\mathit{BufferedNativePacket}\leftarrow\mathit{CurrentNativePacket}\)
41:\(\mathit{BufferedNativePacket_role}=\mathit{RelayNode}\)
42: Set \(\mathit{BufferedNativePacket_tp}=\mathit{tc}\)
43: Set \(\mathit{BufferedNativePacket_tz}=-1\)
44:\(\mathit{NCBuffer}\leftarrow\mathit{BufferedNativePacket}\)
45:endif
46:endfor
47:endif
```  
**Algorithm 1** Encoding Procedure at the NC Layer  
If the coding operation cannot be performed, the current native packet is added in Step 44 to the NC buffer for a short time (_tp_) to give a chance for future encoding (it becomes buffered native packet). To control timeout _tp_, the current system time is written in the _BufferedNativePacket_tz_ variable during the current native packet buffering. The value of tp is a parameter that directly influences the coding efficiency, but also the end-to-end packets delay. This value should be carefully selected, since an additional delay cannot be acceptable for some services. Buffered native packets that have not been used in coding operation are extracted from the NC buffer, sent to the MAC, and then transmitted to its next hop node. In the meantime, the copy of this packet is still buffered for the time _tz_ for further possible decoding process. To do so, _BufferedNativePacket_tz_ variable is used in Step 43 (value \(-1\) is set, to indicate that buffered packet is ready for network coding). Both the time and space complexity of Algorithm 1 is \(O(n)\), and it directly depends on the NC buffer length. Algorithm 2 illustrates a timers control procedure. Buffered native packets that have not been used for coding or decoding operations are deleted after time _tz_ in source nodes (Step 4) and after time _tp_ + _tz_, in relay nodes (Step 16). Figure 4: Network coding scheme in intermediate nodes. Figure 5: NC header format (for two encoded packets). nodes recourses. The NC6 LoWPAN does not acknowledge decoding errors.