## 3 EQPD-MAC Protocol Development
### Castalia Implementation and Simulation
#### 3.4.3 Validation  
Packets are first generated at the Application layer, which also assigns their priority. When the packet arrives at VirtualApplication, it sets the source and destination. The application then uses toNetworkLayer command to shift the packet into VirtualRouting, which sends the packet to AODV routing protocol. AODV routing protocol receives the packet at fromApplicationLayer function and checks the priority with other information, e.g., source ID, destination ID, and encapsulates the packet with all the information. The routing protocol then checks the routing table if it has a destination. If it does not have any route to the destination, it will use route discovery operation and finds the route. AODV uses the command toMacLayer to pass the packet to the MAC layer after finding the destination.  
The packet is received by the MAC layer using the fromNetworkLayer function. When packets arrive to the MAC layer, they are decapsulated and checked for priority, source ID, and destination ID by VirtualMAC. A Tx-beacon is then sent by the MAC either to the sink node or the next hop in the network. If the Tx-beacon is received by the sink node, then according to the synchronization time, the sink will wake up and receives the priority packet by exchanging the Rx-beacon, data packet and followed by ACK packet. After the successful transmission of the data packet, the MAC will pass a short frame using toNetworkLayer command that packet has been received by sink and Network layer receives this frame at fromMacLayer. The Network layer decapsulates the frame to verify the source ID and destination ID. After the verification, the frame is passed to the source node that originated the packet to inform that the packet has been received by the sink node. This frame is eventually being received by the Application layer using the command fromNetworkLayer and total end-to-end latency is measured after the frame arrives at the Application layer.  
If the next hop is an intermediate node, then the priority packet is passed to the Network Layer using the command toNetworkLayer. Data packets are sent straight to the next hop by exchanging Tx-beacon and Rx-beacon using toMacLayer command. The hop count will be increased by one for each time a packet is received by an intermediary node. All the packets pass, and results can be validated in the CastaliaTrace file by using the command \"trace\".  
The findings are validated with other protocols after each successful execution of the code. Considering the case when 45 sensor nodes are scattered randomly around the sensor field, EQPD-MAC consumes 0.2945 mJ/bit whereas PMME-MAC, MPQ-MAC and RMP-MAC consume 0.3819, 0.3519, and 0.3619 mJ/bit energy per sensor node, respectively. The average end-to-end latency of EQPD-MAC is 0.1274 s whereas the latency for PMME-MAC, MPQ-MAC and RMP-MAC is 0.1650, 0.3730, and 0.3439 s, respectively. The PDR is almost 100% for both EQPD-MAC and PMME-MAC whereas the PDR for MPQ-MAC and RMP-MAC is 85% and 76%, respectively. Throughput is around 10,000 bps for both EQPD-MAC and PMME-MAC whereas throughput is only 8600 bps and 7649 bps for MPQ-MAC and RMP-MAC, respectively. These results are validated against the published results of the comparative works.