# Firmware Updates over the Air via LoRa: Unicast and Broadcast Combination for Boosting Update Speed
## 4 Analytical Expression of the Total Update Time
### Broadcast + Unicast Method  
This method starts with a set of \(B\) broadcast rounds, in which all the chunks are sent to the air. Each node stores received chunks and builds a bitmap of missing ones. After that, a unicast round starts, asking the first node about the bitmap containing its missing chunks, sending each of them, and so on.  
The time required to broadcast \(M\) chunks \(B\) times, considering the duty cycle limitations, will be:  
\[t_{br}=M\cdot\!T\!oA_{ch}\cdot\!\frac{100}{d}\cdot B.t \tag{8}\]  
The probability that a chunk is not received by node \(j\) after \(B\) broadcast rounds will be \(p_{j}\)\(B\). In the final unicast round, the average number of times that a chunk will be sent to node \(j\) will be \(\frac{1}{1-p_{j}}\), as obtained in (4).  
The time spent in the final unicast rounds of all the \(N\) nodes can be obtained in a similar way to (8), adding two new terms corresponding to the bitmap request (\(T\!oA_{req}\)) and response (\(T\!oA_{bit}\)):  
\[t_{uni}=\ M\cdot\!\frac{100}{d}\cdot\sum_{j=1}^{N}\left[p_{j}\cdot\left(\frac{ 1}{1-p_{j}}\cdot\!T\!oA_{ch}+T\!oA_{ACK}\right)+T\!oA_{req}+T\!oA_{bit}\right]. \tag{9}\]  
To obtain a simplified expression, we can make an approximation that all loss probabilities are approximately equal (i.e., \(p_{j}\approx p\)). Therefore, the sum of \(N\) equal elements can be expressed as a product (by \(N\)):  
\[t_{uni}\approx\ M\cdot\!\frac{100}{d}\cdot\!N\cdot\left[p^{B}\cdot\left(\frac {1}{1-p}\cdot\!T\!oA_{ch}+T\!oA_{ACK}\right)+T\!oA_{req}+T\!oA_{bit}\right]. \tag{10}\]  
The total time required for \(B\) broadcast rounds plus a unicast one for each of the \(N\) nodes can be obtained as the sum of (8) and (10):  
\[t_{br+uni}\ \approx\ M\cdot\!\frac{100}{d}\cdot\left[T\!oA_{ch}\cdot\!B+N[p^{B} \cdot\left(\frac{1}{1-p}\cdot\!T\!oA_{ch}+T\!oA_{ACK}\right)+T\!oA_{req}+T\!oA _{bit}]\right]. \tag{11}\]  
If we obtain the derivative of \(t_{br+uni}\), with respect to \(B\), and equal it to zero, an optimal value of \(B\) that minimizes the total time can be obtained. The expression can be simplified assuming that the length of the chunk is much higher than the length of the ACK, i.e., \(\frac{1}{1-p}\cdot\!T\!oA_{ch}\gg T\!oA_{ACK}\):  
\[B=log_{p}\left(\frac{p-1}{Nln(p)}\right). \tag{12}\]  
As an example, Figure 4 is obtained with \(M\) = 534 chunks (23 bytes of header and 192 of payload each, i.e., a firmware of 1024 kB), a Spreading Factor = 7, and different values of \(N\) and \(B\). In Figure 3(a), the frame loss probability is 1%. It can be observed that the optimal number of broadcast rounds (\(B\)) is always 1. However, if the loss probability is significantly higher (40%), this can be different: in Figure 4b, the optimal value of \(B\) is not 1. Even for 10 nodes, the best option is to run three rounds at the beginning, and the optimal value of \(B\) grows with the number of nodes: for example, with \(N=50\) nodes, the optimal value is \(B=5\). In both figures, it can be observed that the time cost stabilizes after some broadcast rounds: if the number of nodes is high, the time of a broadcast round is negligible with respect to the time spent in the unicast ones. As a conclusion, when the loss probability is high, it is advisable to perform a number of broadcast rounds before the final unicast one.