Skip to content

Commit

Permalink
Moved State machine and timer functions to own files
Browse files Browse the repository at this point in the history
Reorganized bootstrap file a bit to place function related to others
in closer proximity

Moved Wi-SUN FFN functions to own files and renamed
  • Loading branch information
Mika Tervonen authored and Mika Tervonen committed Sep 3, 2021
1 parent edb8bec commit 2d063d3
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 355 deletions.
520 changes: 169 additions & 351 deletions source/6LoWPAN/ws/ws_bootstrap.c

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion source/6LoWPAN/ws/ws_bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ void ws_bootstrap_test_procedure_trigger_exec(protocol_interface_info_entry_t *c

void ws_bootstrap_network_down(protocol_interface_info_entry_t *cur);

// Bootstrap functions
void ws_bootstrap_start_authentication(protocol_interface_info_entry_t *cur);


// Bootstrap state machine state Functions
bool ws_bootstrap_state_discovery(struct protocol_interface_info_entry *cur);
Expand All @@ -160,15 +163,19 @@ void ws_bootstrap_candidate_parent_store(parent_info_t *parent, const struct mcp
void ws_bootstrap_candidate_table_reset(protocol_interface_info_entry_t *cur);
parent_info_t *ws_bootstrap_candidate_parent_get(struct protocol_interface_info_entry *cur, const uint8_t *addr, bool create);
void ws_bootstrap_candidate_parent_sort(struct protocol_interface_info_entry *cur, parent_info_t *new_entry);
parent_info_t *ws_bootstrap_candidate_parent_get_best(protocol_interface_info_entry_t *cur);

void ws_bootstrap_primary_parent_set(struct protocol_interface_info_entry *cur, struct llc_neighbour_req *neighbor_info, ws_parent_synch_e synch_req);
void ws_bootstrap_parent_confirm(protocol_interface_info_entry_t *cur, struct rpl_instance *instance);
bool ws_bootstrap_neighbor_info_request(struct protocol_interface_info_entry *interface, const uint8_t *mac_64, struct llc_neighbour_req *neighbor_buffer, bool request_new);
void ws_bootstrap_neighbor_list_clean(struct protocol_interface_info_entry *interface);
int8_t ws_bootstrap_neighbor_set(protocol_interface_info_entry_t *cur, parent_info_t *parent_ptr, bool clear_list);
void ws_nud_table_reset(protocol_interface_info_entry_t *cur);
void ws_address_registration_update(protocol_interface_info_entry_t *interface, const uint8_t addr[16]);


void ws_bootstrap_configure_csma_ca_backoffs(protocol_interface_info_entry_t *cur, uint8_t max_backoffs, uint8_t min_be, uint8_t max_be);
int8_t ws_fhss_configure(protocol_interface_info_entry_t *cur, bool discovery);
void ws_fhss_configure_channel_masks(protocol_interface_info_entry_t *cur, fhss_ws_configuration_t *fhss_configuration);
int8_t ws_fhss_set_defaults(protocol_interface_info_entry_t *cur, fhss_ws_configuration_t *fhss_configuration);
void ws_bootstrap_fhss_activate(protocol_interface_info_entry_t *cur);
Expand All @@ -190,7 +197,6 @@ void ws_bootstrap_packet_congestion_init(protocol_interface_info_entry_t *cur);

void ws_bootstrap_asynch_trickle_stop(protocol_interface_info_entry_t *cur);
void ws_bootstrap_advertise_start(protocol_interface_info_entry_t *cur);
void ws_bootstrap_network_discovery_configure(protocol_interface_info_entry_t *cur);

void ws_bootstrap_network_start(protocol_interface_info_entry_t *cur);

Expand Down
41 changes: 41 additions & 0 deletions source/6LoWPAN/ws/ws_bootstrap_6lbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,45 @@ void ws_bootstrap_6lbr_event_handler(protocol_interface_info_entry_t *cur, arm_e
}
}

void ws_bootstrap_6lbr_state_machine(protocol_interface_info_entry_t *cur)
{

switch (cur->nwk_bootstrap_state) {
case ER_WAIT_RESTART:
tr_debug("WS SM:Wait for startup");
ws_bootstrap_event_discovery_start(cur);
break;
case ER_ACTIVE_SCAN:
tr_debug("WS SM:Active Scan");
break;
case ER_SCAN:
tr_debug("WS SM:configuration Scan");
break;
case ER_PANA_AUTH:
tr_info("authentication start");
break;
case ER_RPL_SCAN:
tr_debug("WS SM:Wait RPL to contact DODAG root");
break;
case ER_BOOTSRAP_DONE:
tr_debug("WS SM:Bootstrap Done");
// Bootstrap_done event to application
nwk_bootsrap_state_update(ARM_NWK_BOOTSTRAP_READY, cur);
break;
case ER_RPL_NETWORK_LEAVING:
tr_debug("WS SM:RPL Leaving ready trigger discovery");
ws_bootstrap_event_discovery_start(cur);
break;
default:
tr_warn("WS SM:Invalid state %d", cur->nwk_bootstrap_state);
}
}

void ws_bootstrap_6lbr_seconds_timer(protocol_interface_info_entry_t *cur, uint32_t seconds)
{
(void)cur;
(void)seconds;
}


#endif //HAVE_WS_BORDER_ROUTER && HAVE_WS
4 changes: 4 additions & 0 deletions source/6LoWPAN/ws/ws_bootstrap_6lbr.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
void ws_bootstrap_6lbr_asynch_ind(struct protocol_interface_info_entry *cur, const struct mcps_data_ind_s *data, const struct mcps_data_ie_list *ie_ext, uint8_t message_type);
void ws_bootstrap_6lbr_asynch_confirm(struct protocol_interface_info_entry *interface, uint8_t asynch_message);
void ws_bootstrap_6lbr_event_handler(protocol_interface_info_entry_t *cur, arm_event_s *event);
void ws_bootstrap_6lbr_state_machine(protocol_interface_info_entry_t *cur);
void ws_bootstrap_6lbr_seconds_timer(protocol_interface_info_entry_t *cur, uint32_t seconds);

#define wisun_mode_border_router(cur) (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER)

Expand All @@ -31,6 +33,8 @@ void ws_bootstrap_6lbr_event_handler(protocol_interface_info_entry_t *cur, arm_e
#define ws_bootstrap_6lbr_asynch_ind(cur, data, ie_ext, message_type) ((void) 0)
#define ws_bootstrap_6lbr_asynch_confirm(interface, asynch_message) ((void) 0)
#define ws_bootstrap_6lbr_event_handler(cur, event) ((void) 0)
#define ws_bootstrap_6lbr_state_machine(cur) ((void) 0)
#define ws_bootstrap_6lbr_seconds_timer(cur, seconds) ((void) 0)

#define wisun_mode_border_router(cur) (false)

Expand Down
196 changes: 194 additions & 2 deletions source/6LoWPAN/ws/ws_bootstrap_ffn.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ static void ws_bootstrap_ffn_pan_information_store(struct protocol_interface_inf
return;
}

void ws_bootstrap_ffn_network_discovery_configure(protocol_interface_info_entry_t *cur)
{
// Reset information to defaults
cur->ws_info->network_pan_id = 0xffff;

ws_common_regulatory_domain_config(cur, &cur->ws_info->hopping_schdule);
ws_bootstrap_set_domain_rf_config(cur);
ws_fhss_configure(cur, true);

//Set Network names, Pan information configure, hopping schedule & GTKHash
ws_llc_set_network_name(cur, (uint8_t *)cur->ws_info->cfg->gen.network_name, strlen(cur->ws_info->cfg->gen.network_name));
}

// Start network scan
static void ws_bootstrap_ffn_start_discovery(protocol_interface_info_entry_t *cur)
{
Expand Down Expand Up @@ -193,7 +206,6 @@ static void ws_bootstrap_ffn_start_discovery(protocol_interface_info_entry_t *cu
tr_info("Making parent selection in %u s", (cur->bootsrap_state_machine_cnt / 10));
}


// Start configuration learning
static void ws_bootstrap_ffn_start_configuration_learn(protocol_interface_info_entry_t *cur)
{
Expand All @@ -214,6 +226,19 @@ static void ws_bootstrap_ffn_start_configuration_learn(protocol_interface_info_e
trickle_inconsistent_heard(&cur->ws_info->trickle_pan_config_solicit, &cur->ws_info->trickle_params_pan_discovery);
}

static void ws_bootstrap_ffn_network_configuration_learn(protocol_interface_info_entry_t *cur)
{
tr_debug("Start using PAN configuration");

// Timing information can be modified here
ws_llc_set_pan_information_pointer(cur, &cur->ws_info->pan_information);
uint8_t *gtkhash = ws_pae_controller_gtk_hash_ptr_get(cur);
ws_llc_set_gtkhash(cur, gtkhash);
// TODO update own fhss schedules we are starting to follow first parent

return;
}

static void ws_bootstrap_ffn_pan_advertisement_analyse_active(struct protocol_interface_info_entry *cur, ws_pan_information_t *pan_information)
{
/* In Active state
Expand Down Expand Up @@ -736,7 +761,7 @@ void ws_bootstrap_ffn_event_handler(protocol_interface_info_entry_t *cur, arm_ev
// Clear learned neighbours
ws_bootstrap_neighbor_list_clean(cur);
// Configure LLC for network discovery
ws_bootstrap_network_discovery_configure(cur);
ws_bootstrap_ffn_network_discovery_configure(cur);
ws_bootstrap_fhss_activate(cur);
// Set retry configuration for discovery state
ws_bootstrap_configure_max_retries(cur, WS_MAX_FRAME_RETRIES_BOOTSTRAP);
Expand Down Expand Up @@ -802,4 +827,171 @@ void ws_bootstrap_ffn_event_handler(protocol_interface_info_entry_t *cur, arm_ev
}
}

/*
* Statemachine state functions
* */

static void ws_bootstrap_ffn_network_scan_process(protocol_interface_info_entry_t *cur)
{

parent_info_t *selected_parent_ptr;

tr_debug("analyze network discovery result");

select_best_candidate:
selected_parent_ptr = ws_bootstrap_candidate_parent_get_best(cur);

if (!selected_parent_ptr) {
// Configure LLC for network discovery
ws_bootstrap_ffn_network_discovery_configure(cur);
// randomize new channel and start MAC
ws_bootstrap_fhss_activate(cur);
// Next check will be after one trickle
uint32_t random_start = cur->ws_info->trickle_params_pan_discovery.Imin + randLIB_get_random_in_range(0, cur->ws_info->trickle_params_pan_discovery.Imin);
if (random_start > 0xffff) {
random_start = 0xffff;
}
cur->bootsrap_state_machine_cnt = random_start;

tr_info("Making parent selection in %u s", (cur->bootsrap_state_machine_cnt / 10));
return;
}
tr_info("selected parent:%s panid %u", trace_array(selected_parent_ptr->addr, 8), selected_parent_ptr->pan_id);

if (ws_bootstrap_neighbor_set(cur, selected_parent_ptr, false) < 0) {
goto select_best_candidate;
}

ws_pae_controller_set_target(cur, selected_parent_ptr->pan_id, selected_parent_ptr->addr); // temporary!!! store since auth
ws_bootstrap_event_authentication_start(cur);
return;
}

static void ws_bootstrap_ffn_configure_process(protocol_interface_info_entry_t *cur)
{

if (cur->ws_info->configuration_learned) {
ws_bootstrap_ffn_network_configuration_learn(cur);
ws_bootstrap_event_operation_start(cur);
return;
}
return;
}

void ws_bootstrap_ffn_rpl_wait_process(protocol_interface_info_entry_t *cur)
{

if (cur->ws_info->rpl_state == RPL_EVENT_DAO_DONE) {
// RPL routing is ready
cur->ws_info->connected_time = cur->ws_info->uptime;
ws_bootstrap_event_routing_ready(cur);
} else if (!rpl_control_have_dodag(cur->rpl_domain)) {
// RPL not ready send DIS message if possible
if (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_ROUTER) {
// TODO Multicast DIS should be sent only if no DIO heard for some time
rpl_control_transmit_dis(cur->rpl_domain, cur, 0, 0, NULL, 0, ADDR_LINK_LOCAL_ALL_RPL_NODES);
}
// set timer for next DIS
cur->bootsrap_state_machine_cnt = randLIB_get_random_in_range(WS_RPL_DIS_TIMEOUT / 2, WS_RPL_DIS_TIMEOUT);
}
return;
}


/*
* State machine
*/

void ws_bootstrap_ffn_state_machine(protocol_interface_info_entry_t *cur)
{

switch (cur->nwk_bootstrap_state) {
case ER_WAIT_RESTART:
tr_debug("WS SM:Wait for startup");
ws_bootstrap_event_discovery_start(cur);
break;
case ER_ACTIVE_SCAN:
tr_debug("WS SM:Active Scan");
ws_bootstrap_ffn_network_scan_process(cur);
break;
case ER_SCAN:
tr_debug("WS SM:configuration Scan");
ws_bootstrap_ffn_configure_process(cur);
break;
case ER_PANA_AUTH:
tr_info("authentication start");
// Advertisements stopped during the EAPOL
ws_bootstrap_asynch_trickle_stop(cur);
ws_fhss_configure(cur, false);
int8_t new_default = cur->ws_info->weakest_received_rssi - 1;
if ((new_default < CCA_DEFAULT_DBM) && (new_default >= CCA_LOW_LIMIT) && (new_default <= CCA_HIGH_LIMIT)) {
// Restart automatic CCA threshold using weakest received RSSI as new default
mac_helper_start_auto_cca_threshold(cur->id, cur->ws_info->hopping_schdule.number_of_channels, cur->ws_info->weakest_received_rssi - 1, CCA_HIGH_LIMIT, CCA_LOW_LIMIT);
}
ws_bootstrap_start_authentication(cur);
break;
case ER_RPL_SCAN:
tr_debug("WS SM:Wait RPL to contact DODAG root");
ws_bootstrap_ffn_rpl_wait_process(cur);
break;
case ER_BOOTSRAP_DONE:
tr_debug("WS SM:Bootstrap Done");
// Bootstrap_done event to application
nwk_bootsrap_state_update(ARM_NWK_BOOTSTRAP_READY, cur);
break;
case ER_RPL_NETWORK_LEAVING:
tr_debug("WS SM:RPL Leaving ready trigger discovery");
ws_bootstrap_event_discovery_start(cur);
break;
default:
tr_warn("WS SM:Invalid state %d", cur->nwk_bootstrap_state);
}
}

void ws_bootstrap_ffn_seconds_timer(protocol_interface_info_entry_t *cur, uint32_t seconds)
{
/* Border router keep alive check
*/
if (cur->ws_info->pan_timeout_timer) {
// PAN version timer running
if (cur->ws_info->pan_timeout_timer > seconds) {
cur->ws_info->pan_timeout_timer -= seconds;
if (cur->ws_info->pan_timeout_timer < cur->ws_info->cfg->timing.pan_timeout / 10) {
/* pan timeout is closing need to verify that DAO is tested before the pan times out.
This will give some extra time for RPL to find better parents.
Border router liveliness can be checked from version number change or from successful DAO registrations
in this case there has not been any version number changes during this PAN lifetime.
*/
rpl_control_dao_timeout(cur->rpl_domain, 20);
}
} else {
// Border router has timed out
//Clear Timeout timer
cur->ws_info->pan_timeout_timer = 0;
tr_warn("Border router has timed out");
ws_bootstrap_event_disconnect(cur, WS_FAST_DISCONNECT);
}
}
if (cur->ws_info->aro_registration_timer) {
if (cur->ws_info->aro_registration_timer > seconds) {
cur->ws_info->aro_registration_timer -= seconds;
} else {
// Update all addressess. This function will update the timer value if needed
cur->ws_info->aro_registration_timer = 0;
ws_address_registration_update(cur, NULL);
}
}

if (cur->ws_info->ws_bsi_block.block_time) {
if (cur->ws_info->ws_bsi_block.block_time > seconds) {
cur->ws_info->ws_bsi_block.block_time -= seconds;
} else {
//Clear A BSI blokker
cur->ws_info->ws_bsi_block.block_time = 0;
cur->ws_info->ws_bsi_block.old_bsi = 0;
}
}
}


#endif //HAVE_WS_BORDER_ROUTER && HAVE_WS
4 changes: 4 additions & 0 deletions source/6LoWPAN/ws/ws_bootstrap_ffn.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
void ws_bootstrap_ffn_asynch_ind(struct protocol_interface_info_entry *cur, const struct mcps_data_ind_s *data, const struct mcps_data_ie_list *ie_ext, uint8_t message_type);
void ws_bootstrap_ffn_asynch_confirm(struct protocol_interface_info_entry *interface, uint8_t asynch_message);
void ws_bootstrap_ffn_event_handler(protocol_interface_info_entry_t *cur, arm_event_s *event);
void ws_bootstrap_ffn_state_machine(protocol_interface_info_entry_t *cur);
void ws_bootstrap_ffn_seconds_timer(protocol_interface_info_entry_t *cur, uint32_t seconds);

#define wisun_mode_router(cur) (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_ROUTER)

Expand All @@ -31,6 +33,8 @@ void ws_bootstrap_ffn_event_handler(protocol_interface_info_entry_t *cur, arm_ev
#define ws_bootstrap_ffn_asynch_ind(cur, data, ie_ext, message_type) ((void) 0)
#define ws_bootstrap_ffn_asynch_confirm(interface, asynch_message) ((void) 0)
#define ws_bootstrap_ffn_event_handler(cur, event) ((void) 0)
#define ws_bootstrap_ffn_state_machine(cur) ((void) 0)
#define ws_bootstrap_ffn_seconds_timer(cur, seconds) ((void) 0)

#define wisun_mode_router(cur) (false)

Expand Down
39 changes: 39 additions & 0 deletions source/6LoWPAN/ws/ws_bootstrap_lfn.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,43 @@ void ws_bootstrap_lfn_event_handler(protocol_interface_info_entry_t *cur, arm_ev
}
}

void ws_bootstrap_lfn_state_machine(protocol_interface_info_entry_t *cur)
{

switch (cur->nwk_bootstrap_state) {
case ER_WAIT_RESTART:
tr_debug("WS SM:Wait for startup");
break;
case ER_ACTIVE_SCAN:
tr_debug("WS SM:Active Scan");
break;
case ER_SCAN:
tr_debug("WS SM:configuration Scan");
break;
case ER_PANA_AUTH:
tr_info("authentication start");
// Advertisements stopped during the EAPOL
break;
case ER_RPL_SCAN:
tr_debug("WS SM:Wait RPL to contact DODAG root");
break;
case ER_BOOTSRAP_DONE:
tr_debug("WS SM:Bootstrap Done");
// Bootstrap_done event to application
break;
case ER_RPL_NETWORK_LEAVING:
tr_debug("WS SM:RPL Leaving ready trigger discovery");
break;
default:
tr_warn("WS SM:Invalid state %d", cur->nwk_bootstrap_state);
}
}

void ws_bootstrap_lfn_seconds_timer(protocol_interface_info_entry_t *cur, uint32_t seconds)
{
(void)cur;
(void)seconds;
}


#endif //HAVE_WS_BORDER_ROUTER && HAVE_WS
Loading

0 comments on commit 2d063d3

Please sign in to comment.