diff --git a/nanostack/sw_mac.h b/nanostack/sw_mac.h index e4a018fc4e9..894b8b75c4b 100644 --- a/nanostack/sw_mac.h +++ b/nanostack/sw_mac.h @@ -67,6 +67,13 @@ extern int8_t ns_sw_mac_virtual_client_unregister(struct mac_api_s *api); */ extern int ns_sw_mac_fhss_register(struct mac_api_s *mac_api, struct fhss_api *fhss_api); +/** + * @brief Unregister FHSS API instance from given software MAC instance. + * @param mac_api MAC instance. + * @return 0 on success, -1 on fail. + */ +extern int ns_sw_mac_fhss_unregister(struct mac_api_s *mac_api); + /** * @brief Request registered FHSS API instance from software MAC instance. * @param mac_api MAC instance. diff --git a/source/6LoWPAN/ws/ws_bootstrap.c b/source/6LoWPAN/ws/ws_bootstrap.c index 71dca085899..2d5c029fbd0 100644 --- a/source/6LoWPAN/ws/ws_bootstrap.c +++ b/source/6LoWPAN/ws/ws_bootstrap.c @@ -423,8 +423,7 @@ static void ws_bootstrap_llc_hopping_update(struct protocol_interface_info_entry static int8_t ws_fhss_initialize(protocol_interface_info_entry_t *cur) { fhss_api_t *fhss_api = ns_sw_mac_get_fhss_api(cur->mac_api); - - if (!fhss_api || (fhss_api && cur->ws_info->fhss_owner)) { + if (!fhss_api) { // When FHSS doesn't exist yet, create one fhss_ws_configuration_t fhss_configuration; memset(&fhss_configuration, 0, sizeof(fhss_ws_configuration_t)); @@ -440,26 +439,11 @@ static int8_t ws_fhss_initialize(protocol_interface_info_entry_t *cur) fhss_configuration.ws_bc_channel_function = (fhss_ws_channel_functions)cur->ws_info->fhss_bc_channel_function; fhss_configuration.fhss_bc_dwell_interval = cur->ws_info->fhss_bc_dwell_interval; fhss_configuration.fhss_broadcast_interval = cur->ws_info->fhss_bc_interval; - + fhss_api = ns_fhss_ws_create(&fhss_configuration, cur->ws_info->fhss_timer_ptr); if (!fhss_api) { - fhss_api = ns_fhss_ws_create(&fhss_configuration, cur->ws_info->fhss_timer_ptr); - if (!fhss_api) { - tr_error("fhss create failed"); - return -1; - } - ns_sw_mac_fhss_register(cur->mac_api, fhss_api); - cur->ws_info->fhss_owner = true; - } else { - //Configuration set - ns_fhss_ws_configuration_set(cur->ws_info->fhss_api, &fhss_configuration); - - if (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER) { - ns_fhss_ws_set_hop_count(cur->ws_info->fhss_api, 0); - } else { - //Clear OWN HOP - ns_fhss_ws_set_hop_count(cur->ws_info->fhss_api, 0xff); - } + return -1; } + ns_sw_mac_fhss_register(cur->mac_api, fhss_api); } else { // Read defaults from the configuration to help FHSS testing const fhss_ws_configuration_t *fhss_configuration = ns_fhss_ws_configuration_get(fhss_api); @@ -810,6 +794,9 @@ static int8_t ws_bootstrap_down(protocol_interface_info_entry_t *cur) tr_info("Wi-SUN ifdown"); // Reset MAC for safe upper layer memory free protocol_mac_reset(cur); + ns_sw_mac_fhss_unregister(cur->mac_api); + ns_fhss_delete(cur->ws_info->fhss_api); + cur->ws_info->fhss_api = NULL; // Reset WS information // ws_common_reset(cur) ws_llc_reset(cur); diff --git a/source/6LoWPAN/ws/ws_common.h b/source/6LoWPAN/ws/ws_common.h index 4fae3664f93..d8929df5a98 100644 --- a/source/6LoWPAN/ws/ws_common.h +++ b/source/6LoWPAN/ws/ws_common.h @@ -97,7 +97,6 @@ typedef struct ws_info_s { bool trickle_pa_running: 1; bool trickle_pcs_running: 1; bool trickle_pc_running: 1; - bool fhss_owner: 1; ws_pending_key_index_t pending_key_index_info; // default fhss parameters for this device uint8_t fhss_uc_dwell_interval; diff --git a/source/MAC/IEEE802_15_4/sw_mac.c b/source/MAC/IEEE802_15_4/sw_mac.c index ca52231bac2..a5b679eb284 100644 --- a/source/MAC/IEEE802_15_4/sw_mac.c +++ b/source/MAC/IEEE802_15_4/sw_mac.c @@ -197,6 +197,20 @@ int ns_sw_mac_fhss_register(mac_api_t *mac_api, fhss_api_t *fhss_api) return 0; } +int ns_sw_mac_fhss_unregister(mac_api_t *mac_api) +{ + if (!mac_api) { + return -1; + } + // Get a pointer to MAC setup structure + protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_mac_api(mac_api); + if (!mac_setup) { + return -1; + } + mac_setup->fhss_api = NULL; + return 0; +} + struct fhss_api *ns_sw_mac_get_fhss_api(struct mac_api_s *mac_api) { if (!mac_api) { diff --git a/source/Service_Libs/fhss/fhss_common.c b/source/Service_Libs/fhss/fhss_common.c index d2826ca7e85..25cd4b8b821 100644 --- a/source/Service_Libs/fhss/fhss_common.c +++ b/source/Service_Libs/fhss/fhss_common.c @@ -113,6 +113,8 @@ int8_t fhss_disable(fhss_structure_t *fhss_structure) } fhss_structure->fhss_api->synch_state_set(fhss_structure->fhss_api, FHSS_UNSYNCHRONIZED, 0); ns_dyn_mem_free(fhss_structure->bs); + ns_dyn_mem_free(fhss_structure->ws->tr51_channel_table); + ns_dyn_mem_free(fhss_structure->ws->tr51_output_table); ns_dyn_mem_free(fhss_structure->ws); ns_dyn_mem_free(fhss_structure); fhss_struct = 0; diff --git a/source/Service_Libs/fhss/fhss_ws.c b/source/Service_Libs/fhss/fhss_ws.c index 72f33461085..d8c07b8a738 100644 --- a/source/Service_Libs/fhss/fhss_ws.c +++ b/source/Service_Libs/fhss/fhss_ws.c @@ -305,8 +305,11 @@ static void fhss_event_timer_cb(int8_t timer_id, uint16_t slots) { (void) slots; uint16_t queue_size = 0; - fhss_structure_t *fhss_structure = fhss_get_object_with_timer_id(timer_id); + fhss_structure_t *fhss_structure = fhss_get_object_with_timer_id(timer_id); + if (!fhss_structure) { + return; + } if (fhss_structure->ws->is_on_bc_channel == true) { queue_size = fhss_structure->callbacks.read_tx_queue_size(fhss_structure->fhss_api, true); @@ -407,6 +410,9 @@ static int16_t fhss_ws_synch_state_set_callback(const fhss_api_t *api, fhss_stat } } else if (fhss_state == FHSS_UNSYNCHRONIZED) { fhss_structure->ws->synchronization_time = 0; + eventOS_callback_timer_stop(fhss_structure->fhss_event_timer); + fhss_stop_timer(fhss_structure, fhss_unicast_handler); + fhss_stop_timer(fhss_structure, fhss_broadcast_handler); } fhss_structure->fhss_state = fhss_state;