Skip to content

Commit 149f17b

Browse files
author
Jarkko Paso
committed
FHSS: Own create api for WS
- Defined some internal callbacks
1 parent e19de33 commit 149f17b

File tree

9 files changed

+129
-64
lines changed

9 files changed

+129
-64
lines changed

nanostack/net_fhss.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ extern "C" {
3838
*/
3939
extern fhss_api_t *ns_fhss_create(const fhss_configuration_t *fhss_configuration, const fhss_timer_t *fhss_timer, fhss_statistics_t *fhss_statistics);
4040

41+
/**
42+
* @brief TODO: description.
43+
* @param fhss_configuration Basic FHSS configuration.
44+
* @param fhss_timer FHSS platform timer interface and configuration.
45+
* @param fhss_statistics FHSS statistics storage.
46+
* @return New FHSS instance if successful, NULL otherwise.
47+
*/
48+
extern fhss_api_t *ns_fhss_ws_create(const fhss_configuration_t *fhss_configuration, const fhss_timer_t *fhss_timer, fhss_statistics_t *fhss_statistics);
49+
4150
/**
4251
* @brief Set synchronization time configuration for FHSS network. Should be only called from Border router.
4352
* @param fhss_api FHSS instance.

source/Service_Libs/fhss/fhss.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ void fhss_failed_list_free(fhss_structure_t *fhss_structure)
658658
}
659659
}
660660

661-
int fhss_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_state, uint16_t pan_id)
661+
static int fhss_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_state, uint16_t pan_id)
662662
{
663663
// State is already set
664664
if (fhss_structure->fhss_state == fhss_state) {
@@ -710,7 +710,7 @@ int fhss_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_sta
710710
return 0;
711711
}
712712

713-
uint32_t fhss_get_compensation(fhss_structure_t *fhss_structure)
713+
static uint32_t fhss_get_compensation(fhss_structure_t *fhss_structure)
714714
{
715715
uint32_t compensation = 0;
716716
/* Drift compensation doesn't work with Linux platform */
@@ -732,7 +732,7 @@ uint32_t fhss_get_compensation(fhss_structure_t *fhss_structure)
732732
return compensation;
733733
}
734734

735-
void fhss_superframe_callback(fhss_structure_t *fhss_structure)
735+
static void fhss_superframe_callback(fhss_structure_t *fhss_structure)
736736
{
737737
if ((fhss_structure->send_synch_info_on_next_broadcast_channel == true) && (fhss_is_current_channel_broadcast(fhss_structure) == true)) {
738738
/* Randomize sending superframe of synchronization frame:
@@ -759,7 +759,7 @@ void fhss_superframe_callback(fhss_structure_t *fhss_structure)
759759
}
760760
}
761761

762-
void fhss_update_channel_callback(fhss_structure_t *fhss_structure)
762+
static void fhss_update_channel_callback(fhss_structure_t *fhss_structure)
763763
{
764764
if (fhss_structure->current_channel_index == 0) {
765765
fhss_structure->synch_infos_sent_counter = 0;
@@ -779,3 +779,11 @@ void fhss_update_channel_callback(fhss_structure_t *fhss_structure)
779779
}
780780
}
781781
}
782+
783+
void fhss_set_internal_callbacks(fhss_structure_t *fhss_structure)
784+
{
785+
fhss_structure->update_channel = fhss_update_channel_callback;
786+
fhss_structure->update_superframe = fhss_superframe_callback;
787+
fhss_structure->read_compensation = fhss_get_compensation;
788+
fhss_structure->handle_state_set = fhss_handle_state_set;
789+
}

source/Service_Libs/fhss/fhss.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ int fhss_flush_beacon_info_storage(fhss_structure_t *fhss_structure);
3636
int fhss_add_beacon_info(fhss_structure_t *fhss_structure, uint16_t pan_id, uint8_t *source_address, uint32_t timestamp, uint8_t *synch_info);
3737
void fhss_update_beacon_info_lifetimes(fhss_structure_t *fhss_structure, uint32_t timestamp);
3838
uint32_t fhss_get_tx_time(fhss_structure_t *fhss_structure, uint16_t bytes_to_send, uint8_t phy_header_length, uint8_t phy_tail_length);
39-
uint32_t fhss_get_compensation(fhss_structure_t *fhss_structure);
40-
void fhss_superframe_callback(fhss_structure_t *fhss_structure);
41-
void fhss_update_channel_callback(fhss_structure_t *fhss_structure);
4239
fhss_failed_tx_t *fhss_failed_handle_find(fhss_structure_t *fhss_structure, uint8_t handle);
4340
int fhss_failed_handle_add(fhss_structure_t *fhss_structure, uint8_t handle);
4441
int fhss_failed_handle_remove(fhss_structure_t *fhss_structure, uint8_t handle);
4542
void fhss_failed_list_free(fhss_structure_t *fhss_structure);
4643
int fhss_reset_synch_monitor(fhss_synch_monitor_s *synch_monitor);
47-
int fhss_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_state, uint16_t pan_id);
44+
void fhss_set_internal_callbacks(fhss_structure_t *fhss_structure);
45+
4846
#define MAX_FHSS_TIMER_DIVIDER 100
4947
#define SYNCH_MONITOR_AVG_SAMPLES 5
5048

source/Service_Libs/fhss/fhss_common.c

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ static fhss_structure_t *fhss_get_object_with_timer_id(const int8_t timer_id);
4141
static void fhss_set_active_event(fhss_structure_t *fhss_structure, uint8_t event_type);
4242
static bool fhss_read_active_event(fhss_structure_t *fhss_structure, uint8_t event_type);
4343

44-
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)
44+
fhss_structure_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)
4545
{
4646
if (!fhss_api || !fhss_configuration || !fhss_timer || fhss_struct) {
4747
tr_err("Invalid FHSS enable configuration");
48-
return -1;
48+
return NULL;
4949
}
5050
int channel_count = channel_list_count_channels(fhss_configuration->channel_mask);
5151
if (channel_count <= 0) {
5252
// There must be at least one configured channel in channel list
53-
return -2;
53+
return NULL;
5454
}
5555
fhss_struct = ns_dyn_mem_alloc(sizeof(fhss_structure_t));
5656
if (!fhss_struct) {
57-
return -3;
57+
return NULL;
5858
}
5959
memset(fhss_struct, 0, sizeof(fhss_structure_t));
6060
fhss_struct->fhss_api = fhss_api;
@@ -83,10 +83,10 @@ int8_t fhss_enable(fhss_api_t *fhss_api, const fhss_configuration_t *fhss_config
8383

8484
if (fhss_beacon_create_tasklet(fhss_struct) < 0) {
8585
// XXX: should we free the fhss_structure here?
86-
return -5;
86+
return NULL;
8787
}
8888

89-
return 0;
89+
return fhss_struct;
9090
}
9191

9292
static void fhss_event_timer_cb(int8_t timer_id, uint16_t slots)
@@ -333,24 +333,6 @@ int fhss_compare_with_synch_parent_address(fhss_structure_t *fhss_structure, con
333333
return ret_val;
334334
}
335335

336-
static void fhss_update_channel(fhss_structure_t *fhss_structure)
337-
{
338-
if (fhss_structure->mode == MODE_FHSS) {
339-
fhss_update_channel_callback(fhss_structure);
340-
} else if (fhss_structure->mode == MODE_W_S) {
341-
fhss_ws_update_channel_callback(fhss_structure);
342-
}
343-
}
344-
345-
static void fhss_update_superframe(fhss_structure_t *fhss_structure)
346-
{
347-
if (fhss_structure->mode == MODE_FHSS) {
348-
fhss_superframe_callback(fhss_structure);
349-
} else if (fhss_structure->mode == MODE_W_S) {
350-
fhss_ws_superframe_callback(fhss_structure);
351-
}
352-
}
353-
354336
void fhss_superframe_handler(const fhss_api_t *fhss_api, uint16_t delay)
355337
{
356338
uint32_t compensation = 0;
@@ -360,8 +342,8 @@ void fhss_superframe_handler(const fhss_api_t *fhss_api, uint16_t delay)
360342
}
361343

362344
if (fhss_structure->current_superframe == 0) {
363-
if (fhss_structure->mode == MODE_FHSS) {
364-
compensation = fhss_get_compensation(fhss_structure);
345+
if (fhss_structure->read_compensation) {
346+
compensation = fhss_structure->read_compensation(fhss_structure);
365347
}
366348
}
367349

@@ -372,9 +354,13 @@ void fhss_superframe_handler(const fhss_api_t *fhss_api, uint16_t delay)
372354
if (++fhss_structure->current_channel_index >= fhss_structure->number_of_channels) {
373355
fhss_structure->current_channel_index = 0;
374356
}
375-
fhss_update_channel(fhss_structure);
357+
if (fhss_structure->update_channel) {
358+
fhss_structure->update_channel(fhss_structure);
359+
}
360+
}
361+
if (fhss_structure->update_superframe) {
362+
fhss_structure->update_superframe(fhss_structure);
376363
}
377-
fhss_update_superframe(fhss_structure);
378364
if (fhss_structure->fhss_timeout) {
379365
fhss_structure->fhss_timer += fhss_structure->synch_configuration.fhss_superframe_length;
380366
if (fhss_structure->fhss_timer >= fhss_structure->fhss_timeout) {

source/Service_Libs/fhss/fhss_common.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#define FHSS_BROADCAST_CHANNEL 3
2222
#define FHSS_UPDATE_SYNCH_INFO_STORAGE 4
2323

24+
typedef struct fhss_structure fhss_structure_t;
25+
2426
typedef struct fhss_beacon_info
2527
{
2628
uint16_t pan_id;
@@ -48,7 +50,35 @@ typedef struct
4850
int channel_counter;
4951
} fhss_synch_monitor_s;
5052

51-
typedef struct fhss_structure_t
53+
/**
54+
* @brief Update listening channel.
55+
* @param fhss_structure FHSS structure.
56+
*/
57+
typedef void fhss_update_channel_cb(fhss_structure_t *fhss_structure);
58+
59+
/**
60+
* @brief Update superframe.
61+
* @param fhss_structure FHSS structure.
62+
*/
63+
typedef void fhss_update_superframe_cb(fhss_structure_t *fhss_structure);
64+
65+
/**
66+
* @brief Read FHSS compensation.
67+
* @param fhss_structure FHSS structure.
68+
* @return Compensation to be added to started dwell time
69+
*/
70+
typedef uint32_t fhss_read_compensation_cb(fhss_structure_t *fhss_structure);
71+
72+
/**
73+
* @brief Set FHSS state.
74+
* @param fhss_structure FHSS structure.
75+
* @param fhss_state FHSS state to set.
76+
* @param pan_id Network PAN id.
77+
* @return 0 Success, -1 Failure.
78+
*/
79+
typedef int fhss_handle_state_set_cb(fhss_structure_t *fhss_structure, fhss_states fhss_state, uint16_t pan_id);
80+
81+
struct fhss_structure
5282
{
5383
fhss_api_t *fhss_api;
5484
uint32_t datarate;
@@ -107,9 +137,14 @@ typedef struct fhss_structure_t
107137
fhss_beacon_info_t *fhss_beacon_info_store;
108138
fhss_failed_tx_list_t fhss_failed_tx_list;
109139
fhss_statistics_t *fhss_stats_ptr;
110-
} fhss_structure_t;
140+
fhss_update_channel_cb *update_channel; /**< Update listening channel */
141+
fhss_update_superframe_cb *update_superframe; /**< Update superframe */
142+
fhss_read_compensation_cb *read_compensation; /**< Read FHSS compensation */
143+
fhss_handle_state_set_cb *handle_state_set; /**< Set FHSS state */
144+
};
145+
111146

112-
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);
147+
fhss_structure_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);
113148
int fhss_down(fhss_structure_t *fhss_structure);
114149
int8_t fhss_set_datarate(fhss_structure_t *fhss_structure, uint32_t datarate);
115150
int8_t fhss_set_synch_configuration(fhss_structure_t *fhss_structure, const fhss_synch_configuration_t *fhss_synch_configuration);

source/Service_Libs/fhss/fhss_configuration_interface.c

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,27 @@
2323
#include "nsdynmemLIB.h"
2424
#include "Service_Libs/fhss/fhss_common.h"
2525
#include "Service_Libs/fhss/fhss.h"
26+
#include "Service_Libs/fhss/fhss_ws.h"
2627
#include "Service_Libs/fhss/fhss_mac_interface.h"
2728
#include "ns_trace.h"
2829

2930
#define TRACE_GROUP "fhss"
3031

32+
static void fhss_set_api_callbacks(fhss_api_t *fhss_api)
33+
{
34+
fhss_api->is_broadcast_channel = &fhss_is_broadcast_channel_cb;
35+
fhss_api->use_broadcast_queue = &fhss_use_broadcast_queue_cb;
36+
fhss_api->tx_handle = &fhss_tx_handle_cb;
37+
fhss_api->check_tx_conditions = &fhss_check_tx_conditions_cb;
38+
fhss_api->receive_frame = &fhss_receive_frame_cb;
39+
fhss_api->data_tx_done = &fhss_data_tx_done_cb;
40+
fhss_api->data_tx_fail = &fhss_data_tx_fail_cb;
41+
fhss_api->synch_state_set = &fhss_synch_state_set_cb;
42+
fhss_api->read_timestamp = &fhss_read_timestamp_cb;
43+
fhss_api->get_retry_period = &fhss_get_retry_period_cb;
44+
fhss_api->write_synch_info = &fhss_write_synch_info_cb;
45+
fhss_api->init_callbacks = &fhss_init_callbacks_cb;
46+
}
3147

3248
fhss_api_t *ns_fhss_create(const fhss_configuration_t *fhss_configuration, const fhss_timer_t *fhss_timer, fhss_statistics_t *fhss_statistics)
3349
{
@@ -36,24 +52,32 @@ fhss_api_t *ns_fhss_create(const fhss_configuration_t *fhss_configuration, const
3652
return NULL;
3753
}
3854
// Create FHSS object
39-
int8_t retval = fhss_enable(this, fhss_configuration, fhss_timer, fhss_statistics);
40-
if (retval) {
41-
tr_err("Failed to enable FHSS, return code: %i", retval);
55+
fhss_structure_t *fhss_struct = fhss_enable(this, fhss_configuration, fhss_timer, fhss_statistics);
56+
if (!fhss_struct) {
57+
tr_err("Failed to enable FHSS");
58+
ns_dyn_mem_free(this);
59+
return NULL;
60+
}
61+
fhss_set_api_callbacks(this);
62+
fhss_set_internal_callbacks(fhss_struct);
63+
return this;
64+
}
65+
66+
fhss_api_t *ns_fhss_ws_create(const fhss_configuration_t *fhss_configuration, const fhss_timer_t *fhss_timer, fhss_statistics_t *fhss_statistics)
67+
{
68+
fhss_api_t *this = ns_dyn_mem_alloc(sizeof(fhss_api_t));
69+
if (!this) {
70+
return NULL;
71+
}
72+
// Create FHSS object
73+
fhss_structure_t *fhss_struct = fhss_enable(this, fhss_configuration, fhss_timer, fhss_statistics);
74+
if (!fhss_struct) {
75+
tr_err("Failed to enable FHSS");
4276
ns_dyn_mem_free(this);
4377
return NULL;
4478
}
45-
this->is_broadcast_channel = &fhss_is_broadcast_channel_cb;
46-
this->use_broadcast_queue = &fhss_use_broadcast_queue_cb;
47-
this->tx_handle = &fhss_tx_handle_cb;
48-
this->check_tx_conditions = &fhss_check_tx_conditions_cb;
49-
this->receive_frame = &fhss_receive_frame_cb;
50-
this->data_tx_done = &fhss_data_tx_done_cb;
51-
this->data_tx_fail = &fhss_data_tx_fail_cb;
52-
this->synch_state_set = &fhss_synch_state_set_cb;
53-
this->read_timestamp = &fhss_read_timestamp_cb;
54-
this->get_retry_period = &fhss_get_retry_period_cb;
55-
this->write_synch_info = &fhss_write_synch_info_cb;
56-
this->init_callbacks = &fhss_init_callbacks_cb;
79+
fhss_set_api_callbacks(this);
80+
fhss_ws_set_internal_callbacks(fhss_struct);
5781
return this;
5882
}
5983

source/Service_Libs/fhss/fhss_mac_interface.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,8 @@ void fhss_synch_state_set_cb(const fhss_api_t *api, fhss_states fhss_state, uint
198198
if (!fhss_structure) {
199199
return;
200200
}
201-
if (fhss_structure->mode == MODE_FHSS) {
202-
fhss_handle_state_set(fhss_structure, fhss_state, pan_id);
203-
} else if (fhss_structure->mode == MODE_W_S) {
204-
fhss_ws_handle_state_set(fhss_structure, fhss_state);
201+
if (fhss_structure->handle_state_set) {
202+
fhss_structure->handle_state_set(fhss_structure, fhss_state, pan_id);
205203
}
206204
fhss_structure->fhss_state = fhss_state;
207205
}

source/Service_Libs/fhss/fhss_ws.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@
2727
// Enable this flag to use channel traces
2828
#define FHSS_CHANNEL_DEBUG
2929

30-
int fhss_ws_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_state)
30+
static int fhss_ws_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_state, uint16_t pan_id)
3131
{
32+
(void) pan_id;
3233
fhss_start_timer(fhss_structure, fhss_structure->synch_configuration.fhss_superframe_length, fhss_superframe_handler);
3334
return 0;
3435
}
3536

36-
void fhss_ws_superframe_callback(fhss_structure_t *fhss_structure)
37+
static void fhss_ws_superframe_callback(fhss_structure_t *fhss_structure)
3738
{
3839

3940
}
4041

41-
void fhss_ws_update_channel_callback(fhss_structure_t *fhss_structure)
42+
static void fhss_ws_update_channel_callback(fhss_structure_t *fhss_structure)
4243
{
4344
uint8_t mac_address[8];
4445
fhss_structure->callbacks.read_mac_address(fhss_structure->fhss_api, mac_address);
@@ -49,3 +50,11 @@ void fhss_ws_update_channel_callback(fhss_structure_t *fhss_structure)
4950
#endif /*FHSS_CHANNEL_DEBUG*/
5051
fhss_structure->callbacks.change_channel(fhss_structure->fhss_api, next_channel);
5152
}
53+
54+
void fhss_ws_set_internal_callbacks(fhss_structure_t *fhss_structure)
55+
{
56+
fhss_structure->update_channel = fhss_ws_update_channel_callback;
57+
fhss_structure->update_superframe = fhss_ws_superframe_callback;
58+
fhss_structure->read_compensation = NULL;
59+
fhss_structure->handle_state_set = fhss_ws_handle_state_set;
60+
}

source/Service_Libs/fhss/fhss_ws.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@
1515
* limitations under the License.
1616
*/
1717

18-
int fhss_ws_handle_state_set(fhss_structure_t *fhss_structure, fhss_states fhss_state);
19-
void fhss_ws_superframe_callback(fhss_structure_t *fhss_structure);
20-
void fhss_ws_update_channel_callback(fhss_structure_t *fhss_structure);
18+
void fhss_ws_set_internal_callbacks(fhss_structure_t *fhss_structure);

0 commit comments

Comments
 (0)