Skip to content

Commit feeee0f

Browse files
author
Jarkko Paso
committed
FHSS: Synch callback to write ufsi
1 parent 3e8112b commit feeee0f

File tree

9 files changed

+78
-53
lines changed

9 files changed

+78
-53
lines changed

nanostack/fhss_api.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,6 @@ typedef struct fhss_callback fhss_callback_t;
3838
#define FHSS_SYNCH_REQUEST_FRAME 1 /**< FHSS synchronization request frame */
3939
#define FHSS_DATA_FRAME 2 /**< FHSS data frame */
4040

41-
/**
42-
* @brief FHSS information element types.
43-
*/
44-
#define FHSS_UTT_IE 0 /**< UTT-IE */
45-
#define FHSS_BT_IE 1 /**< BT-IE */
46-
#define FHSS_US_IE 2 /**< US-IE */
47-
#define FHSS_BS_IE 3 /**< BS-IE */
48-
#define FHSS_PLAIN_SYNCH_INFO 4 /**< Plain synchronization information */
49-
5041
/**
5142
* @brief FHSS synchronization info length.
5243
*/
@@ -91,7 +82,6 @@ typedef bool fhss_use_broadcast_queue(const fhss_api_t *api, bool is_broadcast_a
9182
* @return -1 Transmission of the packet is currently not allowed, try again.
9283
* @return -2 Invalid api.
9384
* @return -3 Broadcast packet on Unicast channel (not allowed), push packet back to queue.
94-
* @return -4 Synchronization info missing.
9585
*/
9686
typedef int fhss_tx_handle(const fhss_api_t *api, bool is_broadcast_addr, uint8_t *destination_address, int frame_type, uint16_t frame_length, uint8_t phy_header_length, uint8_t phy_tail_length, uint32_t tx_time);
9787

@@ -164,13 +154,13 @@ typedef uint16_t fhss_get_retry_period(const fhss_api_t *api, uint8_t *destinati
164154
/**
165155
* @brief Write synchronization info to given pointer.
166156
* @param api FHSS instance.
167-
* @param info_ptr Pointer to written data.
168-
* @param info_type Type of the written info (UTT-IE, BT-IE, US-IE, BS-IE).
169-
* @param frame_type_id Frame type of packet which will be written in info element.
157+
* @param ptr Pointer to data. Start of written data for Synch frame. Start of IE header for Data frame.
158+
* @param length Length of IE header. Ignored when Synch frame.
159+
* @param frame_type Frame type of packet (Frames types are defined by FHSS api).
170160
* @param tx_time TX time must be referenced to the first symbol following the SFD of the transmitted frame.
171161
* @return -1 on fail, write length otherwise.
172162
*/
173-
typedef int16_t fhss_write_synch_info(const fhss_api_t *api, uint8_t *info_ptr, int info_type, int frame_type_id, uint32_t tx_time);
163+
typedef int16_t fhss_write_synch_info(const fhss_api_t *api, uint8_t *ptr, uint8_t length, int frame_type, uint32_t tx_time);
174164

175165
/**
176166
* @brief Initialize MAC functions.

source/MAC/IEEE802_15_4/mac_header_helper_functions.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ns_trace.h"
2121
#include "mlme.h"
2222
#include "mac_api.h"
23+
#include "fhss_api.h"
2324
#include "common_functions.h"
2425
#include "mac_common_defines.h"
2526
#include "MAC/IEEE802_15_4/mac_defines.h"
@@ -483,7 +484,7 @@ static uint8_t *mac_security_interface_aux_security_header_write(uint8_t *ptr, c
483484
return ptr;
484485
}
485486

486-
uint8_t * mac_generic_packet_write(uint8_t *ptr, const mac_pre_build_frame_t *buffer)
487+
uint8_t * mac_generic_packet_write(struct protocol_interface_rf_mac_setup *rf_ptr, uint8_t *ptr, const mac_pre_build_frame_t *buffer)
487488
{
488489
ptr = mac_header_write_fcf_dsn(&buffer->fcf_dsn, ptr);
489490

@@ -507,13 +508,16 @@ uint8_t * mac_generic_packet_write(uint8_t *ptr, const mac_pre_build_frame_t *bu
507508
if (buffer->fcf_dsn.securityEnabled) {
508509
ptr = mac_security_interface_aux_security_header_write(ptr, &buffer->aux_header);
509510
}
510-
511+
uint8_t *ie_start = ptr;
511512
//Copy Payload and set IE Elemets
512513
ptr = mac_header_information_elements_write(buffer, ptr);
513514
if (buffer->mac_payload_length) {
514515
memcpy(ptr, buffer->mac_payload, buffer->mac_payload_length );
515516
ptr += buffer->mac_payload_length;
516517
}
518+
if (rf_ptr->fhss_api) {
519+
rf_ptr->fhss_api->write_synch_info(rf_ptr->fhss_api, ie_start, buffer->headerIeLength, FHSS_DATA_FRAME, 0);
520+
}
517521
return ptr;
518522
}
519523

source/MAC/IEEE802_15_4/mac_header_helper_functions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct mac_pre_parsed_frame_s;
2525
struct mac_pre_build_frame;
2626
struct mac_aux_security_header_s;
2727
struct ns_ie_iovec;
28+
struct protocol_interface_rf_mac_setup;
2829

2930
uint8_t mac_security_mic_length_get(uint8_t security_level);
3031
uint8_t mac_header_security_aux_header_length(uint8_t security_level, uint8_t keyIdmode);
@@ -64,7 +65,7 @@ uint8_t mcps_mac_command_frame_id_get(const struct mac_pre_parsed_frame_s *buffe
6465
uint8_t *mcps_mac_payload_pointer_get(const struct mac_pre_parsed_frame_s *buffer);
6566
uint8_t *mcps_security_mic_pointer_get(const struct mac_pre_parsed_frame_s *buffer);
6667
/* Write Mac Header and payload */
67-
uint8_t * mac_generic_packet_write(uint8_t *ptr, const struct mac_pre_build_frame *buffer);
68+
uint8_t * mac_generic_packet_write(struct protocol_interface_rf_mac_setup *rf_ptr, uint8_t *ptr, const struct mac_pre_build_frame *buffer);
6869

6970

7071
/** get pointer to Mac header start point*/

source/MAC/IEEE802_15_4/mac_mcps_sap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ static int8_t mcps_generic_packet_build(protocol_interface_rf_mac_setup_s *rf_pt
14351435
tx_buf->len = frame_length;
14361436

14371437
uint8_t *mhr_start = ptr;
1438-
ptr = mac_generic_packet_write(ptr, buffer);
1438+
ptr = mac_generic_packet_write(rf_ptr, ptr, buffer);
14391439

14401440
if (buffer->fcf_dsn.securityEnabled) {
14411441
mac_security_data_params_set(ccm_ptr, (mhr_start + (buffer->mac_header_length_with_security + open_payload)), (mac_payload_length - open_payload));

source/MAC/IEEE802_15_4/mac_pd_sap.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ void mac_pd_sap_state_machine(protocol_interface_rf_mac_setup_s *rf_mac_setup)
220220
// FHSS synchronization info is written in the end of transmitted (Beacon) buffer
221221
dev_driver_tx_buffer_s *tx_buf = &rf_mac_setup->dev_driver_tx_buffer;
222222
synch_info = tx_buf->buf + rf_mac_setup->dev_driver->phy_driver->phy_header_length + tx_buf->len - FHSS_SYNCH_INFO_LENGTH;
223-
//TODO: Add proper frame type when they are defined
224-
rf_mac_setup->fhss_api->write_synch_info(rf_mac_setup->fhss_api, synch_info, FHSS_PLAIN_SYNCH_INFO, 0, 0);
223+
rf_mac_setup->fhss_api->write_synch_info(rf_mac_setup->fhss_api, synch_info, 0, FHSS_SYNCH_FRAME, 0);
225224
}
226225
// Change to destination channel and write synchronization info to Beacon frames here
227226
int tx_handle_retval = rf_mac_setup->fhss_api->tx_handle(rf_mac_setup->fhss_api, !mac_is_ack_request_set(active_buf),

source/Service_Libs/fhss/fhss.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -929,22 +929,15 @@ static void fhss_update_channel_callback(fhss_structure_t *fhss_structure)
929929
}
930930
}
931931

932-
static int16_t fhss_write_synch_info_callback(const fhss_api_t *api, uint8_t *info_ptr, int info_type, int frame_type_id, uint32_t tx_time)
932+
static int16_t fhss_write_synch_info_callback(const fhss_api_t *api, uint8_t *ptr, uint8_t length, int frame_type, uint32_t tx_time)
933933
{
934934
fhss_structure_t *fhss_structure = fhss_get_object_with_api(api);
935-
(void) frame_type_id;
936935
(void) tx_time;
937-
if (!fhss_structure) {
936+
if (!fhss_structure || !ptr || (frame_type != FHSS_SYNCH_FRAME)) {
938937
return -1;
939938
}
940-
if (info_type == FHSS_PLAIN_SYNCH_INFO) {
941-
if (!info_ptr) {
942-
return -1;
943-
}
944-
fhss_beacon_build(fhss_structure, info_ptr);
945-
return FHSS_SYNCH_INFO_LENGTH;
946-
}
947-
return -1;
939+
fhss_beacon_build(fhss_structure, ptr);
940+
return FHSS_SYNCH_INFO_LENGTH;
948941
}
949942

950943
static void fhss_data_tx_done_callback(const fhss_api_t *api, bool waiting_ack, bool tx_completed, uint8_t handle)

source/Service_Libs/fhss/fhss_ws.c

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "fhss_ws.h"
2626
#include "fhss_mac_interface.h"
2727
#include "nsdynmemLIB.h"
28+
#include "common_functions.h"
2829
#include "ns_trace.h"
2930
#include <string.h>
3031

@@ -34,6 +35,16 @@
3435
#define FHSS_CHANNEL_DEBUG
3536

3637
#define DEF_2E16 16777216
38+
#define IE_HEADER_LENGTH_MASK 0x007f
39+
#define IE_HEADER_ID_MASK 0x7f80
40+
#define WH_IE_ID 0x2a
41+
#define WH_SUB_ID_UTT 1
42+
43+
struct ws_ie_t {
44+
uint8_t *content_ptr;
45+
unsigned length:7;
46+
uint8_t id;
47+
};
3748

3849
static void fhss_ws_update_uc_channel_callback(fhss_structure_t *fhss_structure);
3950
static void fhss_unicast_handler(const fhss_api_t *fhss_api, uint16_t delay);
@@ -262,18 +273,43 @@ static bool fhss_ws_check_tx_conditions_callback(const fhss_api_t *api, bool is_
262273
return true;
263274
}
264275

265-
static int16_t fhss_ws_write_synch_info_callback(const fhss_api_t *api, uint8_t *info_ptr, int info_type, int frame_type_id, uint32_t tx_time)
276+
static uint8_t fhss_ws_ie_header_discover(uint8_t *header_ptr, uint16_t length, struct ws_ie_t *header_ie, uint8_t sub_id)
277+
{
278+
struct ws_ie_t ie_element;
279+
uint8_t *sub_id_ptr;
280+
uint16_t ie_dummy;
281+
while (length > 2) {
282+
ie_dummy = common_read_16_bit_inverse(header_ptr);
283+
ie_element.length = (ie_dummy & IE_HEADER_LENGTH_MASK);
284+
ie_element.id = ((ie_dummy & IE_HEADER_ID_MASK) >> 7);
285+
ie_element.content_ptr = header_ptr + 2;
286+
sub_id_ptr = ie_element.content_ptr;
287+
if (ie_element.length && (header_ie->id == ie_element.id) && (*sub_id_ptr == sub_id)) {
288+
sub_id_ptr++;
289+
ie_element.length--;
290+
header_ie->content_ptr = sub_id_ptr;
291+
header_ie->length = ie_element.length;
292+
return ie_element.length;
293+
}
294+
length -= ie_element.length + 2;
295+
header_ptr += ie_element.length + 2;
296+
}
297+
return 0;
298+
}
299+
300+
static int16_t fhss_ws_write_synch_info_callback(const fhss_api_t *api, uint8_t *ptr, uint8_t length, int frame_type, uint32_t tx_time)
266301
{
267302
fhss_structure_t *fhss_structure = fhss_get_object_with_api(api);
268-
(void) frame_type_id;
269-
(void) tx_time;
270-
(void) info_ptr;
271-
(void) info_type;
272-
if (!fhss_structure) {
303+
if (!fhss_structure || !ptr || (frame_type != FHSS_DATA_FRAME)) {
273304
return -1;
274305
}
275-
276-
return -1;
306+
struct ws_ie_t header_ie;
307+
header_ie.id = WH_IE_ID;
308+
if (fhss_ws_ie_header_discover(ptr, length, &header_ie, WH_SUB_ID_UTT)) {
309+
uint32_t ufsi = fhss_ws_calculate_ufsi(fhss_structure, tx_time);
310+
common_write_24_bit_inverse(ufsi, header_ie.content_ptr+1);
311+
}
312+
return 0;
277313
}
278314

279315
static void fhss_ws_data_tx_done_callback(const fhss_api_t *api, bool waiting_ack, bool tx_completed, uint8_t handle)

test/nanostack/unittest/mac/mac_header_helper_functions/test_mac_header_helper_functions.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ bool test_mac_generic_packet_write()
480480
uint8_t ie_payload_test[30];
481481
uint8_t ie_header_test[30];
482482
uint8_t payload[20];
483+
protocol_interface_rf_mac_setup_s rf_ptr;
484+
memset(&rf_ptr, 0, sizeof(protocol_interface_rf_mac_setup_s));
483485

484486
ns_ie_iovec_t header_ie;
485487
ns_ie_iovec_t payload_ie;
@@ -500,29 +502,29 @@ bool test_mac_generic_packet_write()
500502
frame2->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
501503
frame2->fcf_dsn.intraPan = true;
502504
frame2->fcf_dsn.DstPanPresents = true;
503-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
505+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
504506
//3+4+2
505507
if (test_pointer_move_length(ptr, buffer_ptr) != 9) {
506508
return false;
507509
}
508510

509511
frame2->fcf_dsn.intraPan = false;
510512
frame2->fcf_dsn.SrcPanPresents = true;
511-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
513+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
512514
//3+4+4
513515
if (test_pointer_move_length(ptr, buffer_ptr) != 11) {
514516
return false;
515517
}
516518
frame2->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_64_BIT;
517-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
519+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
518520
//3+10+4
519521
if (test_pointer_move_length(ptr, buffer_ptr) != 17) {
520522
return false;
521523
}
522524

523525
frame2->fcf_dsn.intraPan = true;
524526
frame2->fcf_dsn.SrcPanPresents = false;
525-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
527+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
526528
//3+10+2
527529
if (test_pointer_move_length(ptr, buffer_ptr) != 15) {
528530
return false;
@@ -531,13 +533,13 @@ bool test_mac_generic_packet_write()
531533
frame2->fcf_dsn.SrcPanPresents = true;
532534
frame2->fcf_dsn.DstPanPresents = false;
533535
frame2->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_NONE;
534-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
536+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
535537
//3+4
536538
if (test_pointer_move_length(ptr, buffer_ptr) != 7) {
537539
return false;
538540
}
539541
frame2->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_64_BIT;
540-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
542+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
541543
//3+10
542544
if (test_pointer_move_length(ptr, buffer_ptr) != 13) {
543545
return false;
@@ -557,7 +559,7 @@ bool test_mac_generic_packet_write()
557559

558560
frame2->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
559561

560-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
562+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
561563
//3+8+4+30
562564
if (test_pointer_move_length(ptr, buffer_ptr) != 45) {
563565
return false;
@@ -567,15 +569,15 @@ bool test_mac_generic_packet_write()
567569
frame2->ie_elements.payloadIovLength = 1;
568570
frame2->payloadsIeLength = 30;
569571

570-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
572+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
571573
//3+8+4+30 +2 +30
572574
if (test_pointer_move_length(ptr, buffer_ptr) != 77) {
573575
return false;
574576
}
575577

576578
frame2->mac_payload = payload;
577579
frame2->mac_payload_length =20;
578-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
580+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
579581
//3+8+4+30 +2 +30 +20
580582
if (test_pointer_move_length(ptr, buffer_ptr) != 99) {
581583
return false;
@@ -588,27 +590,27 @@ bool test_mac_generic_packet_write()
588590
frame2->aux_header.KeyIndex = 1;
589591
frame2->aux_header.securityLevel = 7;
590592

591-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
593+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
592594
//3+8+4+30 +2 +30 +10 +6 +16
593595
if (test_pointer_move_length(ptr, buffer_ptr) != 94) {
594596
return false;
595597
}
596598
frame2->aux_header.KeyIdMode = MAC_KEY_ID_MODE_IDX;
597-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
599+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
598600
//3+8+4+30 +2 +30 +10 +6 +16
599601
if (test_pointer_move_length(ptr, buffer_ptr) != 95) {
600602
return false;
601603
}
602604

603605
frame2->aux_header.KeyIdMode = MAC_KEY_ID_MODE_SRC4_IDX;
604-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
606+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
605607
//3+8+4+30 +2 +30 +10 +6 +16
606608
if (test_pointer_move_length(ptr, buffer_ptr) != 99) {
607609
return false;
608610
}
609611

610612
frame2->aux_header.KeyIdMode = MAC_KEY_ID_MODE_SRC8_IDX;
611-
ptr = mac_generic_packet_write(buffer_ptr, frame2);
613+
ptr = mac_generic_packet_write(&rf_ptr, buffer_ptr, frame2);
612614
//3+8+4+30 +2 +30 +10 +6 +16
613615
if (test_pointer_move_length(ptr, buffer_ptr) != 103) {
614616
return false;

test/nanostack/unittest/stub/mac_header_helper_functions_stub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ uint32_t mcps_mac_security_frame_counter_read(const mac_pre_parsed_frame_t *buff
117117
return mac_header_helper_functions_stub.security_header.frameCounter;
118118
}
119119

120-
uint8_t * mac_generic_packet_write(uint8_t *ptr, const mac_pre_build_frame_t *buffer)
120+
uint8_t * mac_generic_packet_write(struct protocol_interface_rf_mac_setup *rf_ptr, uint8_t *ptr, const struct mac_pre_build_frame *buffer)
121121
{
122122
return mac_header_helper_functions_stub.uint8_ptr;
123123
}

0 commit comments

Comments
 (0)