# Efficient Runtime Firmware Update Mechanism for LoRaWAN Class A Devices
## 3 Developed System
### Uplink and Downlink Configuration  
To enable more efficient interaction between IoT end devices and the application server, the developed solution employs virtual communication ports to organize and handle data based on the type of information involved. LoRaWAN technology facilitates information routing through a port-based logical abstraction, providing an efficient data handling mechanism that organizes and routes information according to the associated communication port. The network server is equipped with the necessary tools to decode data based on the selected port. In the experiment described in this article, six communication ports are defined, as shown in Table 2.  
The data sent by the end devices to the application server, in the uplink, undergo a protocol conversion at the network server to enable the application server to correctly interpret the binary format data sent. After conversion at the network server, the data are transmitted to the application server in ASCII format.  
JavaScript is selected, among other data formatting options, to handle data in the network server. Depending on the communication port, information is filtered and processed differently. For example, if the end device sends the message shown in Figure 4, the JavaScript script decodes the received data from binary format and creates a new message in JSON format with fields corresponding to the message type, serial number, battery level, hardware status, received radio signal power level, expressed by the Received Signal Strength Indicator (RSSI), and signal quality using the signal-to-noise ratio (SNR) value (Figure 5).  
\begin{table}
\begin{tabular}{c c} \hline \hline
**Port** & **Data Type** \\ \hline
4 & Device registration \\
5 & Event signaling \\
6 & Insertion or removal of RFID cards \\
7 & Firmware update check \\
8 & Firmware file request \\
9 & Update status signaling \\ \hline \hline \end{tabular}
\end{table}
Table 2: Classification of information type based on communication port.  
Figure 4: Protocol conversion in the LoRaWAN network.  
Figure 3: Update Process During Runtime.  
On the other hand, whenever there are data available to be sent to the end devices in the downlink, the network server processes the information differently based on the selected communication port and forwards it to the respective end device. The first step in that process involves decoding the data received by the application server, while the second step entails encoding this information into binary format, in accordance with the communication protocol established between the end device and the network server.  
The downlink process implemented in this study is based on enhancing or altering the behavior of a web application or system using custom callbacks known as webhooks. A webhook is triggered by specific events, sending an HTTP request to a pre-configured URL. This mechanism allows the network server, upon receiving a message or command from an application server, to automatically send feedback data to the end device, thereby facilitating bidirectional communication and process automation. Figure 6 illustrates the implemented downlink process. Initially, the application server opens a TCP/IP socket with the network server. The message payload includes the data to be sent: the target end device ID, the network server application ID responsible for forwarding the information to the end device, the associated communication port, and the actual data, referred to as the payload. Figure 6 presents an example of this process, where the payload corresponds to the firmware update file name and the number of blocks contained in the file, which are transmitted via Port 7.  
Figure 5: Decoding of uplink data by the network server.  
Figure 6: Implemented downlink process based on webhook.