## 3 Hybrid Topology Architecture
### Implementation
#### 3.2.6 Topology Manager  
This component facilitates the intelligent management of a LoRaWAN network topology updated by knowing the link quality of data transmission between end devices and the gateway at any time. This version of the topology manager is a preliminary proposal toward further development, including the use of machine learning techniques for improved/smart decision-making. The topology manager module was programmed in Python 3. Algorithm 1 represents the simplified pseudo-code of the topology decision-making procedure. First, the information from the end device, including its current topology mode, is retrieved from the database. Then, the average of the last five values from the transmission link quality parameters (SNR and RSSI) that have been stored for each end device is calculated. This allows the user to assess which end devices have good connectivity and which have more difficulties. Consequently, these data can be used to keep track of the evolution of the network link parameters and discern the moment when certain end devices would benefit from changing their transmission mode to send their data through a neighbor enabled with multi-hop transmissions within their coverage area. If there were packet loss cases, the algorithm would not update its running averages for RSSI and SNR, as it relies exclusively on received messages. Furthermore, to address potential concerns about missing values, the system compensates for connectivity issues by dynamically adjusting the network topology. This mechanism ensures network reliability despite temporal variations in link quality metrics. ```
1:Initialize:
2:average_rssi \(\leftarrow\) 0
3:average_snr \(\leftarrow\) 0
4:message_counter \(\leftarrow\) 0
5:current_mode \(\leftarrow\) GetInitialMode()
6:while true do
7: message \(\leftarrow\) ReceiveMessage()
8: rssi,snr \(\leftarrow\) GetRSSIandSNR(message)
9: average_rssi \(\leftarrow\) (average_rssi \(\times\) message_counter \(+\) rssi)\(/(\)message_counter \(+\) 1)
10: average_snr \(\leftarrow\) (average_snr \(\times\) message_counter \(+\) snr)\(/(\)message_counter \(+\) 1)
11: message_counter \(\leftarrow\) message_counter \(+\) 1
12:if message_counter \(\geq\) 5 then
13: average_rssi \(\leftarrow\) average_rssi/5
14: average_snr \(\leftarrow\) average_snr/5
15:if average_rssi \(>\) rssi_threshold **and** average_snr \(>\) snr_threshold **then**
16: recommended_mode \(\leftarrow\) GetRecommendedMode()
17:if current_mode \(\
eq\) recommended_mode then
18: SendTopologyChangeMessage()
19: current_mode \(\leftarrow\) recommended_mode
20:endif
21:endif
22: message_counter \(\leftarrow\) 0
23: average_rssi \(\leftarrow\) 0
24: average_snr \(\leftarrow\) 0
25:endif
26:endwhile
```  
**Algorithm 1** Adaptive Topology AdjustmentUsers can establish the threshold values for the SNR and RSSI to decide if an end device should change its topology/transmission mode. The thresholds are maximum RSSI, minimum RSSI, and maximum SNR. The maximum RSSI and SNR thresholds, if exceeded, are used to determine if the end device needs to change its topology mode. The minimum RSSI is established to assess when an end device should change back into star topology mode. The frequency at which this evaluation process is performed is also adjustable by the user. The process of changing topologies is carried out by sending messages to each end device with the new transmission mode (single-hop or multi-hop) they need to adopt so as to update the network topology. The end devices with worse network quality metrics should change into mesh mode, and the end devices with better network quality metrics should change into hybrid mode. As a result, the messages with a request for a transmission mode change can be forwarded to two or more end devices in the network. When changing topologies, the first end devices to perform the change are those changing into hybrid (star and mesh) mode, and the petition for a topology change directed to the end devices that need to change into mesh mode is put on hold.