# LaRaWAPS: A Wide-Area Positioning System Based on LoRa Mesh
## 4 System Design
### LoRa Mesh Protocol Design
#### 4.2.1 The Structure of the Mesh Protocol  
The proposed mesh networking protocol consists of four layers: the physical layer, the link layer, the routing layer, and the application layer. Its architecture is shown in Figure 3.  
The physical layer communicates with the hardware and guarantees network reliability at the hardware level. There is a packet queue in the physical layer, as shown in Figure 4. All sending packets and received packets will be serialized in the queue to reduce hardware conflicts while keeping the order of packets. Channel activity detection (CAD) and random delay mechanism are used in the physical layer to reduce data collisions. Specifically, the authors require the device to set CAD mode to check if the channel is occupied before sending data. CAD can detect LoRa lead codes on wireless channels. The lead code is used to keep the receiver in sync with the incoming data stream and is 12 symbols in length by default. If the channel is occupied, the device changes the channel or waits for a random period of time before sending data. In addition, 32-bit Cyclic Redundancy Check (CRC32) is also used to reduce data errors.  
The link layer ensures that packets passing to the upper layers are all packets to be processed by this node. In this layer, the target and source IP are defined in this specific hop, which only represents the previous hop node and the next hop node of the packet. The node will only process the packet whose target IP is the same as its own IP or is a broadcast IP. To prevent the infinite flooding of a broadcast packet, the link layer also maintains a list of nodes traveled.  
The route layer is the key layer of our mesh protocol, maintaining a route table, executing the routing algorithm, and ensuring the best-effort delivery of the packet. The route table contains information on the network collected from received packets. When a Packet is to be sent, the route layer will use the route algorithm to choose a path from the route table, and create a route layer header. We also maintain a waiting list in this layer, which is used to handle replies and re-transmit a packet if no reply is received in a specified  
Figure 4: The packet queue in physical layer.  
Figure 3: Mesh protocol hierarchy.  
-resend interval time, a packet without a reply might be re-transmitted several times before declaring an error.  
The topmost layer is the application layer, whose role is to implement functions such as sending positioning instructions, handling messages, or communicating with the host computer.  
The packet structure is shown in Figure 5, each layer will add a new header. In the physic layer header, LoRa ID is used to distinguish LoRa packets that belong to our system and CRC32 is used to check the payload's Integrity. In the link layer header, destination IP, source IP, and node IPs in this specific hop, the length field represents the payload's length, and the hop number records the number of nodes the packet has passed through. Following the hop number filed, a list records all nodes traveled. The route layer header has seven fields, destination IP and source IP indicates the final and initial nodes which differ from those in the link layer. The application layer packet only contains the type and length field for content indication.