Skip to content

Commit f7ab36c

Browse files
author
Jarkko Paso
committed
FHSS: Created FHSS state set handler
1 parent 2cef8ac commit f7ab36c

File tree

5 files changed

+63
-45
lines changed

5 files changed

+63
-45
lines changed

source/Service_Libs/fhss/fhss.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "ns_trace.h"
2929
#include "eventOS_event.h"
3030
#include "eventOS_callback_timer.h"
31+
#include "platform/arm_hal_interrupt.h"
3132
#include "randLIB.h"
3233
#include <string.h>
3334

@@ -746,3 +747,55 @@ void fhss_failed_list_free(fhss_structure_t *fhss_structure)
746747
fhss_failed_handle_remove(fhss_structure, i);
747748
}
748749
}
750+
751+
int fhss_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_state, uint16_t pan_id)
752+
{
753+
// State is already set
754+
if (fhss_structure->fhss_state == fhss_state) {
755+
tr_debug("Synch same state %u", fhss_state);
756+
return -1;
757+
}
758+
759+
if (fhss_state == FHSS_UNSYNCHRONIZED) {
760+
tr_debug("FHSS down");
761+
fhss_down(fhss_structure);
762+
} else {
763+
// Do not synchronize to current pan
764+
if (fhss_structure->synch_panid == pan_id) {
765+
tr_debug("Synch same panid %u", pan_id);
766+
return -1;
767+
}
768+
if (fhss_structure->fhss_scramble_table == NULL) {
769+
if (fhss_generate_scramble_table(fhss_structure)) {
770+
tr_error("Failed to generate scramble table");
771+
return -1;
772+
}
773+
}
774+
uint32_t datarate = fhss_structure->callbacks.read_datarate(fhss_structure->fhss_api);
775+
fhss_set_datarate(fhss_structure, datarate);
776+
uint8_t mac_address[8];
777+
fhss_structure->callbacks.read_mac_address(fhss_structure->fhss_api, mac_address);
778+
fhss_structure->uc_channel_index = fhss_get_offset(fhss_structure, mac_address);
779+
// Get Beacon info from storage
780+
fhss_beacon_info_t *beacon_info = fhss_get_beacon_info(fhss_structure, pan_id);
781+
if (beacon_info) {
782+
memcpy(fhss_structure->synch_parent, beacon_info->source_address, 8);
783+
platform_enter_critical();
784+
// Calculate time since the Beacon was received
785+
uint32_t elapsed_time = fhss_structure->fhss_api->read_timestamp(fhss_structure->fhss_api) - beacon_info->timestamp;
786+
// Synchronize to given PAN
787+
fhss_beacon_received(fhss_structure, beacon_info->synch_info, elapsed_time);
788+
platform_exit_critical();
789+
// Delete stored Beacon infos
790+
fhss_flush_beacon_info_storage(fhss_structure);
791+
fhss_structure->synch_panid = pan_id;
792+
} else if (fhss_is_synch_root(fhss_structure) == true) {
793+
// Synch root will start new network
794+
fhss_start_timer(fhss_structure, fhss_structure->synch_configuration.fhss_superframe_length, fhss_superframe_handler);
795+
} else {
796+
tr_error("Synch info not found");
797+
return -1;
798+
}
799+
}
800+
return 0;
801+
}

source/Service_Libs/fhss/fhss.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int fhss_failed_handle_remove(fhss_structure_t *fhss_structure, uint8_t handle);
4343
int fhss_generate_scramble_table(fhss_structure_t *fhss_structure);
4444
void fhss_failed_list_free(fhss_structure_t *fhss_structure);
4545
int fhss_reset_synch_monitor(fhss_synch_monitor_s *synch_monitor);
46+
int fhss_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_state, uint16_t pan_id);
4647
#define MAX_FHSS_TIMER_DIVIDER 100
4748
#define SYNCH_MONITOR_AVG_SAMPLES 5
4849

source/Service_Libs/fhss/fhss_mac_interface.c

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -197,52 +197,10 @@ void fhss_synch_state_set_cb(const fhss_api_t *api, fhss_states fhss_state, uint
197197
if (!fhss_structure) {
198198
return;
199199
}
200+
if (fhss_structure->mode == MODE_FHSS) {
201+
fhss_handle_state_set(fhss_structure, fhss_state, pan_id);
202+
} else if (fhss_structure->mode == MODE_W_S) {
200203

201-
// State is already set
202-
if (fhss_structure->fhss_state == fhss_state) {
203-
tr_debug("Synch same state %u", fhss_state);
204-
return;
205-
}
206-
207-
if (fhss_state == FHSS_UNSYNCHRONIZED) {
208-
tr_debug("FHSS down");
209-
fhss_down(fhss_structure);
210-
} else {
211-
// Do not synchronize to current pan
212-
if (fhss_structure->synch_panid == pan_id) {
213-
tr_debug("Synch same panid %u", pan_id);
214-
return;
215-
}
216-
if (fhss_structure->fhss_scramble_table == NULL) {
217-
if (fhss_generate_scramble_table(fhss_structure)) {
218-
tr_error("Failed to generate scramble table");
219-
return;
220-
}
221-
}
222-
uint32_t datarate = fhss_structure->callbacks.read_datarate(api);
223-
fhss_set_datarate(fhss_structure, datarate);
224-
uint8_t mac_address[8];
225-
fhss_structure->callbacks.read_mac_address(fhss_structure->fhss_api, mac_address);
226-
fhss_structure->uc_channel_index = fhss_get_offset(fhss_structure, mac_address);
227-
// Get Beacon info from storage
228-
fhss_beacon_info_t *beacon_info = fhss_get_beacon_info(fhss_structure, pan_id);
229-
if (beacon_info) {
230-
memcpy(fhss_structure->synch_parent, beacon_info->source_address, 8);
231-
platform_enter_critical();
232-
// Calculate time since the Beacon was received
233-
uint32_t elapsed_time = api->read_timestamp(api) - beacon_info->timestamp;
234-
// Synchronize to given PAN
235-
fhss_beacon_received(fhss_structure, beacon_info->synch_info, elapsed_time);
236-
platform_exit_critical();
237-
// Delete stored Beacon infos
238-
fhss_flush_beacon_info_storage(fhss_structure);
239-
fhss_structure->synch_panid = pan_id;
240-
} else if (fhss_is_synch_root(fhss_structure) == true) {
241-
// Synch root will start new network
242-
fhss_start_timer(fhss_structure, fhss_structure->synch_configuration.fhss_superframe_length, fhss_superframe_handler);
243-
} else {
244-
tr_error("Synch info not find");
245-
}
246204
}
247205
fhss_structure->fhss_state = fhss_state;
248206
}

test/nanostack/unittest/service_libs/fhss/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ TEST_SRC_FILES = \
2727
../../stub/event_stub.c \
2828
../../stub/fhss_callbacks_stub.c \
2929
../../stub/ns_list_stub.c \
30+
../../stub/platform_stub.c \
3031

3132
include ../../MakefileWorker.mk
3233

test/nanostack/unittest/stub/fhss_stub.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,8 @@ int fhss_generate_scramble_table(fhss_structure_t *fhss_structure)
191191
{
192192
return 0;
193193
}
194+
195+
int fhss_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_state, uint16_t pan_id)
196+
{
197+
return 0;
198+
}

0 commit comments

Comments
 (0)