# Secure LoRa Firmware Update with Adaptive Data Rate Techniques +
## 2 Materials and Methods
### Battery Approximation  
After settling on an acceptable communication setting, the device determines whether to initiate the firmware transfer based on the whether the battery has sufficient energy to complete the operation. The total energy consumption is dependent on the size of the firmware image (\(i\)), the status of an existing firmware update (\(i_{s}\)), i.e., whether a partial image already exists, and the setting the data is transferred with (\(s\)). The energy consumption is partitioned into a data transfer portion and a portion required to carry out the application flash write operations as given by Equation (4). \[E_{update}(i,i_{s},s)=E_{Transfer}(i,i_{s},s)+E_{AppFlashWrite}(i). \tag{4}\]  
The data transfer can be further subdivided into the energy consumption associated with receiving data, transmitting acknowledgements, performing AES encryption, and writing the flash to store the firmware image, as shown by Equation (5). Note that the flash  
Figure 4: The convergence times of the different ADR search techniques. write operation stores the image on the device but does not commit it to application memory. The electrical current associated with data reception, packet acknowledgement, AES computation and flash write operations is given by \(I_{rectice}\), \(I_{transmit}\), \(I_{AES}\), and \(I_{extFlashWrite}\), respectively. The transmit and receive time depend on the communication setting but the AES and flash times are fixed. \(V_{s}\) is the supply voltage, and \(\eta\) is the efficiency of the switch mode power converter that supplies energy from the battery to the components. For scenarios in which external flash is not being used, \(T_{extFlashWrite}\) = 0. \[E_{Transfer}(i,i_{s},s)=\frac{(i-i_{s})V_{s}}{\eta}(I_{recieve}T_{ receive}(s)+I_{transmit}T_{transmit}(s)+\\ 2I_{AES}T_{AES}+I_{extFlashWrite}T_{extFlashWrite}) \tag{5}\]  
The energy to write the application flash depends solely on the firmware image size and is broken down into external memory read, AES encryption, and the application flash read and write components, as shown in Equation (6). The external memory read and AES encryption operations occur twice: first to validate the firmware image and second to write the image to flash. For scenarios in which external flash is not being used, \(T_{extFlashRead}\) and \(T_{AES}\) are set to 0. The specifics of this process are covered in Section 2.5, _Committing Code to Flash_. \[E_{AppFlashWrite}(i)=\frac{iV_{s}}{\eta}(2I_{extFlashRead}T_{ extFlashRead}+2I_{AES}T_{AES}+\\ I_{flashWrite}T_{flashWrite}+I_{flashRead}T_{flashRead}) \tag{6}\]  
After the setting and firmware image parameters are defined, the device will use these energy consumption calculations to compute the expected battery capacity after completing the update. This is accomplished using Equation (7) where the capacity \(C\) is defined as the percent charge left in the battery, similar to what a phone or laptop computer would indicate for battery life. \(B_{SOH}\) is the state of health of the battery which begins at 1 and degrades to 0 over time. Rechargeable batteries are generally rated to maintain 80% of full capacity up to 2000 cycles. The energy associated with a fully charged battery (\(E_{fullBattery}\)) can be calculated using the amp-hour specification for the battery and the average output voltage. The battery capacity estimation techniques used here are based on those presented in [24]. \[C_{AfterUpdate}(i)=C_{Battery}-100\frac{E_{update}}{B_{SOH}E_{FullBattery}}. \tag{7}\]  
After computing the final capacity value, the device then determines if it can commit to the firmware update process. The exact threshold depends on several factors including the importance of the firmware update, the ability of the system to recharge, the external temperature, the consequence of a discharged battery, and the current capacity of the device. While the cutoff threshold is application-specific, we use a 50% threshold for illustration in this work.