# Design and Analysis of Adaptive Hierarchical Low-Power Long-Range Networks
## 4 Software Implementation
### Adaptive Sub-Network Organization
Then, two methods are provided for enabling and disabling the module, which is useful when it should only be run at certain points in time. After the module is enabled, the calculate_coordinator() method is called and starts the sub-network formation. Next, a method for setting the parameters of the algorithm is provided, which also sets the parameters for every other component. Then, another method is provided for registering a callback in order to get notifications upon events. Finally, CC provides a set of functions to access useful information such as the coordinator id, the parent device (if any), etc. The interface of the module is presented in Listing 1. ```
template<typenameOsModel, typenameRadio, typenameTimer, typenameCoordinatorSelection, typenameJoinDecision, typenameIterator> classCoreComponent{ public: voidinit(Radio&,Timer&,CS&,JD&,IT&); voidenable(void); voiddisable(void); voidset_parameters(parameters_t*); voidfind_head(void); template<typenameT,void(T::*TMethod)(uint8_t)> intreg_changed_callback(T*obj); node_id_tparent() node_id_tsub_network_id() boolis_controller(void);... };
```  
Listing 1: Core Component. The CC components also provide a public interface _that implements the Wiselib concept of Sub-Network_ and thus provides the coordinator's ID, and also allows for registering a function callback in order to be able to deliver events to external components whenever an change to the sub-network occurs, e.g., when the device joined a new sub-network, or a device from a different sub-network was discovered, or a new sub-network was formed, etc. **Coordinator Selection (CS) Concept. ** In the CS concept, a method is provided for setting the parameters (e.g., a probability value that the module will use). Additionally, there is the method for calculating if the current device is a coordinator and a method to get this result. The interface of the module is presented in Listing 2. ```
template<typenameRadio> classCoordinatorSelection{ public: voidinit(Radio&); voidenable(void); voiddisable(void); voidset_parameters(parameters_t*); boolis_coordinator(void); boolcalculate_coordinator();

**Join Decision (JD) Concept. ** In the JD concept, a method is supplied that gives the hop distance from the coordinator of the sub-network after the device has joined one. It also provides methods to generate the join request, accept and deny messages. Finally, the available join method is invoked once an invitation payload arrives. The interface of the module in Wiselib is available in Listing 3. ```
template<typenameRadio> classJoinDecision{ public: voidinit(Radio&); voidenable(void); voiddisable(void); inthops(); voidget_join_request_payload(block_data_t*); voidget_join_accept_payload(block_data_t*); voidget_join_deny_payload(block_data_t*); size_tgt_payload_length(int); booljoin(uint8_t*,uint8_t); };
```
**Iterator (IT) Concept. ** This module provides methods for getting the sub-network ID and the parent of the device in the sub-network's routing table. Moreover, the next_neighbor() method allows the device to communicate with the rest of the devices in the sub-network. If the sub-network information is available, a callback function can be registered so that the Iterator can call back to inform about changes in the topology. The interface of the module is available in Listing 4. ``` template<typenameRadio,typenameTimer> classIterator{ public:... voidinit(Radio&,Timer&); voidenable(void); voiddisable(void); voiddisable(void); node_id_tsub_network_id(void); node_id_tparent(void); node_id_tnext_neighbor(); template<typenameT,void(T::*TMethod)(uint8_t)> intreg_next_callback(T*obj); private: vector_tsub_network_neighbors_; vector_tnon_sub_network_neighbors_; node_id_tparent_;... };