# Efficient Runtime Firmware Update Mechanism for LoRaWAN Class A Devices
## 3 Developed System
### Implementation of the Proposed System
#### 3.5.2 Firmware
Upon analyzing the code, it was found that the debug LED (development support LED, see Figure 8, blue LED) was not disabled before release, resulting in extra energy consumption and thereby reducing battery life. Figure 12 shows the changes that the new version of the firmware produces after it is compiled compared to the initial firmware version (SBW_new.hex, green side) compared to the old file (SBW_old.hex, red side). Figure 11: Segment of the Flash_Memory_Map.h file. Figure 12 shows that, in the example under analysis, to update the end devices firmware via the LoRaWAN network, it is sufficient to send only 17 records of the Intel Hex file. The first record indicates the most significant address of the program memory block where changes will be made, the last record indicates the end of file, and the remaining 15 records are associated with the code update information. The next step involves inserting the hexadecimal file records to be sent to the end devices into the database. For this purpose, the database contains a table named \"fw_update\", where we insert the IDs of the end devices we intend to update, the update name, the total number of records to update, the index of each record, the content of each record, and finally a variable with update execution status. Figure 13 illustrates the database with the records inserted according to the defined criteria above. After inserting the update firmware records to update into the database, the first time that the end device performs an uplink communication, the middleware marks that event occurrence and checks for the existence of firmware update records. If a pending update is  
Figure 12: Changed program memory area viewed from Intel Hex File. Figure 13: Insertion into the fw_update table of the database, the records to be updated. available, the process of sending the update file is initiated by sending the file name and the number of records to update via downlink on Port 7, followed by sending the firmware update records via downlink on Port 8. Thereafter, the TTN network server places these data in a transmission queue for the corresponding end device. During the next uplink event, the content of the records to be updated is sent from the network server to the end device via the LoRaWAN gateway. The data are then forwarded to the UART serial communication port, which routes the information to the PIC18 LF27K42 microcontroller [18]. Upon receiving the file name and the number of records to be updated through communication Port 7 (Figure 14), the microcontroller activates the firmware update application and prepares the device to receive an Intel Hex File for firmware update, which, for the considered example, comprises 17 records. Upon receiving the firmware update records via communication Port 8 (Figure 15), the microcontroller stores each record in the corresponding data structure, which supports up to 64 update records. When the last record is received (end of file), the microcontroller signals the firmware update application to proceed with the update process. The firmware update process is initiated by checking the update name. If the update was already performed previously, it is discarded, and the device signals that the current version is up to date; otherwise, the update process proceeds to the next step, which involves verifying the received data. At this stage, the integrity of each record of the received update file is verified using the checksum error control mechanism. Once the data integrity of each incoming record is validated, the update process fills a backup data structure similar to the one used on program's memory. The data are organized into rows of 128 bytes each in the case of the PIC18 LF27K42 microcontroller [18]. The update process is designed to update up to eight rows or 1024 bytes. After restructuring the data into rows, the next step involves populating each row of the new data structure with existing program memory data corresponding to that row.