# Narrowband-Internet of Things Device-to-Device Simulation: An Open-Sourced Framework
## 3 NB-IoT D2D Framework, Model, and Simulation
### LEACH Routing Protocol  
In our simulation, the main purpose of using a single-hop clustering protocol was to downgrade the several-server queuing, decrease inter-cluster collisions, and prevent attacks from malicious nodes. The LEACH protocol is adapted to the NB-IoT D2D setting so as to route network traffic. An upside of using LEACH is that, compared to other traditional multi-hop routing protocols, LEACH is difficult to compromise [21].  
Indeed, the nodes around the BS in traditional multi-hop routing procedures are more often engaged, increasing the likelihood of compromise, while only the CHs communicate directly with the BS in LEACH. These CHs are determined randomly in the network, independent of the BS; they also change periodically. As a result, it is very difficult for an attacker to spot the CHs in a LEACH protocol. However, the CHs are responsible for collecting and routing essential data, so a compromised CH is dangerous. An important method for enhancing cybersecurity is key management. While many key distribution systems are available, most are not appropriate for the IoT [35]. For instance, a significant amount of computation is needed for public key-based distribution, a significant amount of storage is required for full pairwise keying, and worldwide keying is threatened. The IoT has limited resources, processing power, communication abilities, and memory, making it difficult to implement cybersecurity mechanisms efficiently.  
Returning to the communications principles of LEACH, the protocol selects randomly a few nodes that act as CHs based on a higher energy principle, while the other nodes act  
Figure 3: Flowchart of the MATLAB-based NB-IoT D2D simulation.  
as Non-CHs (NCHs). NCHs aggregate data from one or several measurement sensors, and send it to the CH. The aggregated and compressed data are then sent by the CHs to the respective BSs. In this simulation, OFDMA with BPSK or QPSK and SC-FDMA with QPSK are applied for uplink and downlink, respectively, instead of Time Division Multiple Access (TDMA)/Code-Division Multiple Access (CDMA), as used previously in studies involving LEACH.  
LEACH is used in the clustering concept to minimize energy dissipation and maximize network lifetime. Consequently, using the LEACH protocol, operations are classified into two stages: Setup and steady steps [36]. The clusters are created in the setup stage, and a CH is elected for each cluster with a probability \(p\). The LEACH threshold value \(T(n)\) is commonly described by (1). Electing a CH that belongs to the set of embedded devices \(G\) that were not selected as CH in the last \(1/p\) rounds is achieved based on \(T(n)\). Each iteration for electing a CH is called a round, denoted by \(r\):  
\[T(n)=\frac{p}{1-p\times(r\times mod(1/p))}\ \forall n\ \in G. \tag{1}\]  
Each device selects a random number \(X\in\left]0,1\right[\). If \(X<T(n)\), then, for the current \(r\), this device becomes CH. CHs subsequently broadcast a message to NCHs to request them to connect to their clusters. The NCH devices decide which CH to connect to based on the received signal power, and the NCH transmits an acceptance message to the CHs that will be under its cluster. This communications could also be assisted by the BS but will not change the overall performance and behavior. Thus, each CH establishes an OFDMA/SC-OFDMA schedule to organize transmitting slots between NCH devices in its own cluster based upon certain criteria, such as the number of NCH devices and the collected data type. The schedule of OFDMA/SC-OFDMA is transmitted to all the devices belonging to the cluster.  
In the steady stage, the NCH senses data and transmits it to its CH based on the OFDMA/SC-OFDMA schedule. Consequently, the CHs send the sense data to the closest BS. The NB-IoT returns to the initialization (setup) process after a predetermined time, and a new CH is picked.