Skip to content

Commit 37425ca

Browse files
author
Jarkko Paso
committed
FHSS: Separated common FHSS functions to own module
1 parent 59b7b76 commit 37425ca

File tree

11 files changed

+486
-437
lines changed

11 files changed

+486
-437
lines changed

source/Service_Libs/fhss/fhss.c

Lines changed: 4 additions & 320 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ns_types.h"
1919
#include "fhss_api.h"
2020
#include "fhss_config.h"
21+
#include "fhss_common.h"
2122
#include "fhss.h"
2223
#include "fhss_channel.h"
2324
#include "channel_list.h"
@@ -32,116 +33,9 @@
3233

3334
#define TRACE_GROUP "fhss"
3435

35-
// TODO: create linked list
36-
// FHSS object pointer
37-
fhss_structure_t *fhss_struct = 0;
38-
36+
static bool fhss_is_bc_sending_superframe(fhss_structure_t *fhss_structure);
3937
static bool fhss_check_remaining_tx_time(fhss_structure_t *fhss_structure, uint16_t tx_length, uint8_t phy_header_length, uint8_t phy_tail_length);
40-
static void fhss_event_timer_cb(int8_t timer_id, uint16_t slots);
41-
static fhss_structure_t *fhss_get_object_with_timer_id(const int8_t timer_id);
4238
static bool fhss_is_there_common_divisor(uint16_t i, uint8_t j);
43-
static void fhss_update_channel(fhss_structure_t *fhss_structure);
44-
static int fhss_reset_synch_monitor(fhss_synch_monitor_s *synch_monitor, bool reset_compensation);
45-
static int fhss_reset(fhss_structure_t *fhss_structure);
46-
static void fhss_failed_list_free(fhss_structure_t *fhss_structure);
47-
48-
49-
int8_t fhss_enable(fhss_api_t *fhss_api, const fhss_configuration_t *fhss_configuration, const fhss_timer_t *fhss_timer, fhss_statistics_t *fhss_statistics)
50-
{
51-
if (!fhss_api || !fhss_configuration || !fhss_timer || fhss_struct) {
52-
tr_err("Invalid FHSS enable configuration");
53-
return -1;
54-
}
55-
int channel_count = channel_list_count_channels(fhss_configuration->channel_mask);
56-
if (channel_count <= 0) {
57-
// There must be at least one configured channel in channel list
58-
return -2;
59-
}
60-
fhss_struct = ns_dyn_mem_alloc(sizeof(fhss_structure_t));
61-
if (!fhss_struct) {
62-
return -3;
63-
}
64-
memset(fhss_struct, 0, sizeof(fhss_structure_t));
65-
fhss_struct->fhss_api = fhss_api;
66-
fhss_struct->fhss_configuration = *fhss_configuration;
67-
fhss_struct->platform_functions = *fhss_timer;
68-
fhss_struct->fhss_stats_ptr = fhss_statistics;
69-
fhss_struct->number_of_channels = channel_count;
70-
71-
// set a invalid id to tasklet_id, so we know that one is not started yet
72-
fhss_struct->beacon_tasklet_id = -1;
73-
74-
if (!fhss_struct->platform_functions.fhss_resolution_divider) {
75-
fhss_struct->platform_functions.fhss_resolution_divider = 1;
76-
}
77-
// Default synch interval is 240 seconds
78-
if (!fhss_struct->fhss_configuration.fhss_max_synch_interval) {
79-
fhss_struct->fhss_configuration.fhss_max_synch_interval = 240;
80-
}
81-
ns_list_init(&fhss_struct->fhss_failed_tx_list);
82-
fhss_struct->own_hop = 0xff;
83-
fhss_reset(fhss_struct);
84-
fhss_reset_synch_monitor(&fhss_struct->synch_monitor, true);
85-
fhss_struct->active_fhss_events = 0;
86-
fhss_struct->fhss_beacon_info_store = NULL;
87-
fhss_struct->fhss_event_timer = eventOS_callback_timer_register(fhss_event_timer_cb);
88-
89-
if (fhss_beacon_create_tasklet(fhss_struct) < 0) {
90-
// XXX: should we free the fhss_structure here?
91-
return -5;
92-
}
93-
94-
return 0;
95-
}
96-
97-
int8_t fhss_set_datarate(fhss_structure_t *fhss_structure, uint32_t datarate)
98-
{
99-
if (!fhss_structure) {
100-
return -1;
101-
}
102-
// If datarate is not set, use default 250kbit/s. Datarate is used as divider later.
103-
if (!datarate) {
104-
datarate = 250000;
105-
}
106-
fhss_structure->datarate = datarate;
107-
return 0;
108-
}
109-
110-
int8_t fhss_set_synch_configuration(fhss_structure_t *fhss_structure, const fhss_synch_configuration_t *fhss_synch_configuration)
111-
{
112-
if (!fhss_structure) {
113-
return -1;
114-
}
115-
if (!fhss_synch_configuration) {
116-
return -2;
117-
}
118-
// None of the configurations can be set zero
119-
if( fhss_synch_configuration->fhss_number_of_bc_channels == 0 || fhss_synch_configuration->fhss_number_of_tx_slots == 0
120-
|| fhss_synch_configuration->fhss_number_of_superframes == 0 || fhss_synch_configuration->fhss_superframe_length == 0) {
121-
return -3;
122-
}
123-
// Number of channels must be divisible with the number of broadcast channels.
124-
// Number of superframes must be divisible with the number of TX slots
125-
if (((fhss_structure->number_of_channels % fhss_synch_configuration->fhss_number_of_bc_channels) != 0) ||
126-
((fhss_synch_configuration->fhss_number_of_superframes % fhss_synch_configuration->fhss_number_of_tx_slots) != 0) ||
127-
(fhss_synch_configuration->fhss_number_of_superframes <= fhss_synch_configuration->fhss_number_of_tx_slots)) {
128-
return -4;
129-
}
130-
fhss_structure->synch_configuration = *fhss_synch_configuration;
131-
fhss_structure->own_hop = 0;
132-
return 0;
133-
}
134-
135-
fhss_structure_t *fhss_get_object_with_api(const fhss_api_t *fhss_api)
136-
{
137-
if (!fhss_api || !fhss_struct) {
138-
return NULL;
139-
}
140-
if (fhss_struct->fhss_api == fhss_api) {
141-
return fhss_struct;
142-
}
143-
return NULL;
144-
}
14539

14640
bool fhss_is_synch_root(fhss_structure_t *fhss_structure)
14741
{
@@ -151,35 +45,6 @@ bool fhss_is_synch_root(fhss_structure_t *fhss_structure)
15145
return true;
15246
}
15347

154-
void fhss_set_active_event(fhss_structure_t *fhss_structure, uint8_t event_type)
155-
{
156-
fhss_structure->active_fhss_events |= (1 << event_type);
157-
}
158-
159-
void fhss_clear_active_event(fhss_structure_t *fhss_structure, uint8_t event_type)
160-
{
161-
fhss_structure->active_fhss_events &= ~(1 << event_type);
162-
}
163-
164-
bool fhss_read_active_event(fhss_structure_t *fhss_structure, uint8_t event_type)
165-
{
166-
if (fhss_structure->active_fhss_events & (1 << event_type)) {
167-
return true;
168-
}
169-
return false;
170-
}
171-
172-
static fhss_structure_t *fhss_get_object_with_timer_id(const int8_t timer_id)
173-
{
174-
if (timer_id <0 || !fhss_struct) {
175-
return NULL;
176-
}
177-
if (fhss_struct->fhss_event_timer == timer_id) {
178-
return fhss_struct;
179-
}
180-
return NULL;
181-
}
182-
18348
static bool fhss_is_bc_sending_superframe(fhss_structure_t *fhss_structure)
18449
{
18550
if (fhss_structure->current_superframe >= fhss_structure->broadcast_start_superframe) {
@@ -288,95 +153,18 @@ bool fhss_check_tx_allowed(fhss_structure_t *fhss_structure, bool is_bc, uint16_
288153
return true;
289154
}
290155

291-
static int fhss_reset_synch_monitor(fhss_synch_monitor_s *synch_monitor, bool reset_compensation)
156+
int fhss_reset_synch_monitor(fhss_synch_monitor_s *synch_monitor)
292157
{
293158
if (synch_monitor) {
294159
synch_monitor->avg_synch_fix = 0;
295160
// Initialize to -1 instead of 0 to drop the first beacon after network scan (from synch monitoring)
296161
synch_monitor->avg_synch_fix_counter = -1;
297-
if (reset_compensation == true) {
298-
synch_monitor->drift_compensation = 0;
299-
}
300162
synch_monitor->channel_counter = 0;
301163
return 0;
302164
}
303165
return -1;
304166
}
305167

306-
static void fhss_destroy_scramble_table(fhss_structure_t *fhss_structure)
307-
{
308-
if (fhss_structure->fhss_scramble_table) {
309-
ns_dyn_mem_free(fhss_structure->fhss_scramble_table);
310-
fhss_structure->fhss_scramble_table = NULL;
311-
}
312-
}
313-
314-
static int fhss_reset(fhss_structure_t *fhss_structure)
315-
{
316-
if (fhss_structure) {
317-
fhss_structure->platform_functions.fhss_timer_stop(fhss_superframe_handler, fhss_structure->fhss_api);
318-
fhss_destroy_scramble_table(fhss_structure);
319-
fhss_struct->synch_panid = 0xffff;
320-
fhss_beacon_periodic_stop(fhss_structure);
321-
fhss_struct->current_superframe = 0;
322-
fhss_struct->current_channel_index = 0;
323-
fhss_struct->channel_list_counter = 0;
324-
if (fhss_is_synch_root(fhss_structure) == false) {
325-
fhss_struct->own_hop = 0xff;
326-
}
327-
fhss_struct->tx_allowed = false;
328-
fhss_struct->synch_interval = (uint32_t) (fhss_struct->fhss_configuration.fhss_max_synch_interval/BEACON_INTERVAL_INIT_DIVIDER) * 1000;
329-
fhss_struct->rx_channel = 0;
330-
fhss_struct->beacons_received_timer = 0;
331-
memset(fhss_struct->synch_parent, 0xff, 8);
332-
fhss_struct->send_synch_info_on_next_broadcast_channel = false;
333-
memset(&fhss_struct->synch_configuration, 0, sizeof(fhss_synch_configuration_t));
334-
fhss_struct->synch_infos_sent_counter = 0;
335-
fhss_struct->broadcast_start_superframe = 0;
336-
fhss_failed_list_free(fhss_structure);
337-
fhss_struct->fhss_state = FHSS_UNSYNCHRONIZED;
338-
return 0;
339-
}
340-
return -1;
341-
}
342-
343-
int fhss_down(fhss_structure_t *fhss_structure)
344-
{
345-
if (fhss_structure) {
346-
fhss_reset(fhss_structure);
347-
fhss_reset_synch_monitor(&fhss_struct->synch_monitor, false);
348-
fhss_stats_update(fhss_structure, STATS_FHSS_DRIFT_COMP, fhss_structure->synch_monitor.drift_compensation);
349-
fhss_stats_update(fhss_structure, STATS_FHSS_AVG_SYNCH_FIX, fhss_structure->synch_monitor.avg_synch_fix);
350-
fhss_stats_update(fhss_structure, STATS_FHSS_SYNCH_INTERVAL, fhss_structure->synch_interval / 1000);
351-
return 0;
352-
}
353-
return -1;
354-
}
355-
356-
357-
int8_t fhss_disable(fhss_structure_t *fhss_structure)
358-
{
359-
if (!fhss_structure) {
360-
return -1;
361-
}
362-
fhss_destroy_scramble_table(fhss_structure);
363-
ns_dyn_mem_free(fhss_structure);
364-
fhss_structure = 0;
365-
fhss_struct = 0;
366-
return 0;
367-
}
368-
369-
void fhss_start_timer(fhss_structure_t *fhss_structure, uint32_t time, void (*callback)(const fhss_api_t *fhss_api, uint16_t))
370-
{
371-
if (callback){
372-
// Don't allow starting with zero slots
373-
if (time < fhss_structure->platform_functions.fhss_resolution_divider) {
374-
time = fhss_structure->platform_functions.fhss_resolution_divider;
375-
}
376-
fhss_structure->platform_functions.fhss_timer_start(time / fhss_structure->platform_functions.fhss_resolution_divider, callback, fhss_structure->fhss_api);
377-
}
378-
}
379-
380168
uint32_t fhss_get_remaining_time_to_next_superframe(const fhss_structure_t *fhss_structure)
381169
{
382170
const uint32_t slots = fhss_structure->platform_functions.fhss_get_remaining_slots(fhss_superframe_handler, fhss_structure->fhss_api);
@@ -467,26 +255,6 @@ void fhss_superframe_handler(const fhss_api_t *fhss_api, uint16_t delay)
467255
}
468256
}
469257

470-
int fhss_timeout_start(fhss_structure_t *fhss_structure, uint32_t time)
471-
{
472-
if (!fhss_structure) {
473-
return -1;
474-
}
475-
fhss_structure->fhss_timeout = time;
476-
fhss_structure->fhss_timer = 0;
477-
return 0;
478-
}
479-
480-
int fhss_timeout_stop(fhss_structure_t *fhss_structure)
481-
{
482-
if (!fhss_structure) {
483-
return -1;
484-
}
485-
fhss_structure->fhss_timeout = 0;
486-
fhss_structure->fhss_timer = 0;
487-
return 0;
488-
}
489-
490258
int fhss_update_txrx_slots(fhss_structure_t *fhss_structure)
491259
{
492260
uint8_t cur_superframe = fhss_structure->current_superframe;
@@ -781,81 +549,6 @@ static bool fhss_check_remaining_tx_time(fhss_structure_t *fhss_structure, uint1
781549
return retval;
782550
}
783551

784-
int fhss_update_synch_parent_address(fhss_structure_t *fhss_structure)
785-
{
786-
uint8_t parent_address[8];
787-
788-
if (!fhss_get_parent_address(fhss_structure, parent_address)) {
789-
memcpy(fhss_structure->synch_parent, parent_address, 8);
790-
return 0;
791-
}
792-
return -1;
793-
}
794-
795-
void fhss_trig_event(fhss_structure_t *fhss_structure, uint8_t event_type)
796-
{
797-
if (fhss_read_active_event(fhss_structure, event_type) == true) {
798-
return;
799-
}
800-
arm_event_s event;
801-
event.receiver = fhss_structure->beacon_tasklet_id;
802-
event.sender = 0;
803-
event.event_type = event_type;
804-
event.event_id = 0;
805-
event.data_ptr = fhss_structure;
806-
event.priority = ARM_LIB_HIGH_PRIORITY_EVENT;
807-
event.event_data = 0;
808-
if (eventOS_event_send(&event) != 0) {
809-
tr_error("Event trigger failed: eventOS_event_send() failed");
810-
} else {
811-
fhss_set_active_event(fhss_structure, event_type);
812-
}
813-
}
814-
815-
int fhss_get_parent_address(fhss_structure_t *fhss_structure, uint8_t *p_addr)
816-
{
817-
int ret_val = -1;
818-
if (!fhss_structure || !p_addr) {
819-
return -1;
820-
}
821-
822-
ret_val = fhss_structure->callbacks.read_coord_mac_address(fhss_structure->fhss_api, p_addr);
823-
824-
if (ret_val) {
825-
// Use default synchronization parent when RPL parent not found
826-
memcpy(p_addr, fhss_structure->synch_parent, 8);
827-
ret_val = 0;
828-
}
829-
return ret_val;
830-
}
831-
832-
int fhss_compare_with_synch_parent_address(fhss_structure_t *fhss_structure, const uint8_t *source_addr)
833-
{
834-
int ret_val = -1;
835-
if (!fhss_structure || !source_addr) {
836-
return ret_val;
837-
}
838-
uint8_t parent_address[8];
839-
840-
if (fhss_is_synch_root(fhss_structure) == false) {
841-
if (!fhss_get_parent_address(fhss_structure, parent_address)) {
842-
ret_val = memcmp(source_addr, parent_address, 8);
843-
}
844-
}
845-
return ret_val;
846-
}
847-
848-
static void fhss_update_channel(fhss_structure_t *fhss_structure)
849-
{
850-
// If channel is broadcast channel (true), send event
851-
if (fhss_change_to_next_channel(fhss_structure) == true) {
852-
// Only if device is border router
853-
if (fhss_structure->own_hop == 0) {
854-
fhss_trig_event(fhss_structure, FHSS_BROADCAST_CHANNEL);
855-
}
856-
}
857-
}
858-
859552
static bool fhss_is_there_common_divisor(uint16_t i, uint8_t j)
860553
{
861554
if (i < j) {
@@ -890,15 +583,6 @@ int fhss_generate_scramble_table(fhss_structure_t *fhss_structure)
890583
return 0;
891584
}
892585

893-
static void fhss_event_timer_cb(int8_t timer_id, uint16_t slots)
894-
{
895-
(void) slots;
896-
fhss_structure_t *fhss_structure = fhss_get_object_with_timer_id(timer_id);
897-
if (fhss_structure) {
898-
fhss_structure->callbacks.tx_poll(fhss_structure->fhss_api);
899-
}
900-
}
901-
902586
fhss_beacon_info_t *fhss_get_beacon_info(fhss_structure_t *fhss_structure, uint16_t pan_id)
903587
{
904588
fhss_beacon_info_t *beacon_info;
@@ -1056,7 +740,7 @@ int fhss_failed_handle_remove(fhss_structure_t *fhss_structure, uint8_t handle)
1056740
return 0;
1057741
}
1058742

1059-
static void fhss_failed_list_free(fhss_structure_t *fhss_structure)
743+
void fhss_failed_list_free(fhss_structure_t *fhss_structure)
1060744
{
1061745
for (uint16_t i = 0; i<256; i++) {
1062746
fhss_failed_handle_remove(fhss_structure, i);

0 commit comments

Comments
 (0)