Skip to content

Commit fe06236

Browse files
author
Juha Heiuskanen
committed
MAC ACK RX guarantee update
MAC will allocate 1 buffer dynamically for RX ACK data. Buffer is re-allocated if allocated size is too small. MAC also have static Event structure for guarantee that ACK event is possible to send allways.
1 parent 2388a80 commit fe06236

File tree

8 files changed

+140
-25
lines changed

8 files changed

+140
-25
lines changed

source/MAC/IEEE802_15_4/mac_defines.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
struct cca_structure_s;
2727
struct buffer;
2828
struct mac_pre_build_frame;
29+
struct mac_pre_parsed_frame_s;
2930
struct mlme_key_descriptor_s;
3031
struct arm_device_driver_list;
3132
struct fhss_api;
@@ -204,6 +205,7 @@ typedef struct protocol_interface_rf_mac_setup {
204205
bool macBroadcastDisabled: 1;
205206
bool scan_active: 1;
206207
bool rf_csma_extension_supported: 1;
208+
bool rf_pd_ack_buffer_is_in_use: 1;
207209
uint16_t mac_short_address;
208210
uint16_t pan_id;
209211
uint8_t mac64[8];
@@ -233,7 +235,9 @@ typedef struct protocol_interface_rf_mac_setup {
233235
struct mac_pre_build_frame *pd_data_request_queue_to_go;
234236
struct mac_pre_build_frame *pd_data_request_bc_queue_to_go;
235237
struct mac_pre_build_frame *active_pd_data_request;
238+
struct mac_pre_parsed_frame_s *pd_rx_ack_buffer;
236239
/* MAC Beacon info */
240+
uint16_t allocated_ack_buffer_length;
237241
uint16_t max_beacon_payload_length;
238242
uint8_t *mac_beacon_payload;
239243
uint8_t mac_beacon_payload_size;
@@ -253,6 +257,7 @@ typedef struct protocol_interface_rf_mac_setup {
253257
struct mac_pre_build_frame enhanced_ack_buffer;
254258
uint32_t enhanced_ack_handler_timestamp;
255259
arm_event_t mac_mcps_timer_event;
260+
arm_event_storage_t mac_ack_event;
256261
uint16_t indirect_pending_bytes;
257262
arm_nwk_mlme_event_type_e mac_mlme_event;
258263
mac_event_t timer_mac_event;

source/MAC/IEEE802_15_4/mac_mcps_sap.c

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,6 +2372,18 @@ static mac_pre_build_frame_t *mcps_sap_pd_req_queue_read(protocol_interface_rf_m
23722372

23732373
void mcps_sap_pre_parsed_frame_buffer_free(mac_pre_parsed_frame_t *buf)
23742374
{
2375+
if (!buf) {
2376+
return;
2377+
}
2378+
2379+
if (buf->mac_class_ptr && buf->fcf_dsn.frametype == FC_ACK_FRAME) {
2380+
struct protocol_interface_rf_mac_setup *rf_mac_setup = buf->mac_class_ptr;
2381+
if (rf_mac_setup->rf_pd_ack_buffer_is_in_use) {
2382+
rf_mac_setup->rf_pd_ack_buffer_is_in_use = false;
2383+
return;
2384+
}
2385+
}
2386+
23752387
ns_dyn_mem_free(buf);
23762388
}
23772389

@@ -2388,6 +2400,37 @@ mac_pre_parsed_frame_t *mcps_sap_pre_parsed_frame_buffer_get(const uint8_t *data
23882400
return buffer;
23892401
}
23902402

2403+
mac_pre_parsed_frame_t *mcps_sap_pre_parsed_ack_buffer_get(protocol_interface_rf_mac_setup_s *rf_ptr, const uint8_t *data_ptr, uint16_t frame_length)
2404+
{
2405+
2406+
if (rf_ptr->rf_pd_ack_buffer_is_in_use) {
2407+
#ifdef __linux__
2408+
tr_debug("mac ACK buffer get fail: already active");
2409+
#endif
2410+
return NULL;
2411+
}
2412+
2413+
if (frame_length > rf_ptr->allocated_ack_buffer_length) {
2414+
//Free Current
2415+
if (rf_ptr->pd_rx_ack_buffer) {
2416+
ns_dyn_mem_free(rf_ptr->pd_rx_ack_buffer);
2417+
rf_ptr->allocated_ack_buffer_length = 0;
2418+
}
2419+
rf_ptr->pd_rx_ack_buffer = ns_dyn_mem_alloc(sizeof(mac_pre_parsed_frame_t) + frame_length);
2420+
if (!rf_ptr->pd_rx_ack_buffer) {
2421+
return NULL;
2422+
}
2423+
rf_ptr->allocated_ack_buffer_length = frame_length;
2424+
}
2425+
memset(rf_ptr->pd_rx_ack_buffer, 0, sizeof(mac_pre_parsed_frame_t) + rf_ptr->allocated_ack_buffer_length);
2426+
rf_ptr->pd_rx_ack_buffer->frameLength = frame_length;
2427+
memcpy(mac_header_message_start_pointer(rf_ptr->pd_rx_ack_buffer), data_ptr, frame_length);
2428+
//Mark active ACK buffer state
2429+
rf_ptr->rf_pd_ack_buffer_is_in_use = true;
2430+
return rf_ptr->pd_rx_ack_buffer;
2431+
}
2432+
2433+
23912434
static void mac_set_active_event(protocol_interface_rf_mac_setup_s *rf_mac_setup, uint8_t event_type)
23922435
{
23932436
rf_mac_setup->active_mac_events |= (1 << event_type);
@@ -2458,21 +2501,36 @@ int8_t mcps_sap_pd_confirm_failure(void *mac_ptr)
24582501
return eventOS_event_send(&event);
24592502
}
24602503

2461-
void mcps_sap_pd_ack(void *ack_ptr)
2504+
int8_t mcps_sap_pd_ack(struct protocol_interface_rf_mac_setup *rf_ptr, mac_pre_parsed_frame_t *buffer)
24622505
{
2463-
if (mac_tasklet_event_handler < 0 || !ack_ptr) {
2464-
return;
2506+
if (mac_tasklet_event_handler < 0 || !buffer) {
2507+
return -1;
2508+
}
2509+
2510+
if (buffer->fcf_dsn.frametype == FC_ACK_FRAME) {
2511+
arm_event_storage_t *event = &rf_ptr->mac_ack_event;
2512+
event->data.data_ptr = buffer;
2513+
event->data.event_data = 0;
2514+
event->data.event_id = 0;
2515+
event->data.event_type = MCPS_SAP_DATA_ACK_CNF_EVENT;
2516+
event->data.priority = ARM_LIB_HIGH_PRIORITY_EVENT;
2517+
event->data.sender = 0;
2518+
event->data.receiver = mac_tasklet_event_handler;
2519+
eventOS_event_send_user_allocated(event);
2520+
2521+
return 0;
24652522
}
2523+
24662524
arm_event_s event = {
24672525
.receiver = mac_tasklet_event_handler,
24682526
.sender = 0,
24692527
.event_id = 0,
2470-
.data_ptr = ack_ptr,
2528+
.data_ptr = buffer,
24712529
.event_type = MCPS_SAP_DATA_ACK_CNF_EVENT,
24722530
.priority = ARM_LIB_HIGH_PRIORITY_EVENT,
24732531
};
24742532

2475-
eventOS_event_send(&event);
2533+
return eventOS_event_send(&event);
24762534
}
24772535

24782536
void mcps_sap_trig_tx(void *mac_ptr)
@@ -2566,6 +2624,17 @@ void mac_mcps_buffer_queue_free(protocol_interface_rf_mac_setup_s *rf_mac_setup)
25662624
mcps_sap_prebuild_frame_buffer_free(buffer);
25672625
}
25682626
}
2627+
2628+
if (rf_mac_setup->pd_rx_ack_buffer) {
2629+
if (rf_mac_setup->rf_pd_ack_buffer_is_in_use) {
2630+
eventOS_cancel(&rf_mac_setup->mac_ack_event);
2631+
rf_mac_setup->rf_pd_ack_buffer_is_in_use = false;
2632+
}
2633+
ns_dyn_mem_free(rf_mac_setup->pd_rx_ack_buffer);
2634+
rf_mac_setup->pd_rx_ack_buffer = NULL;
2635+
rf_mac_setup->allocated_ack_buffer_length = 0;
2636+
}
2637+
25692638
}
25702639
/**
25712640
* Function return list start pointer

source/MAC/IEEE802_15_4/mac_mcps_sap.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct mcps_purge_s;
3636
struct mcps_data_req_ie_list;
3737
struct channel_list_s;
3838
struct mcps_enhanced_frame_response_s;
39+
struct mac_pre_parsed_frame_s;
3940

4041
/** Address types */
4142
typedef enum {
@@ -100,6 +101,8 @@ void mcps_sap_pd_req_queue_write(struct protocol_interface_rf_mac_setup *rf_mac_
100101
*/
101102
mac_pre_parsed_frame_t *mcps_sap_pre_parsed_frame_buffer_get(const uint8_t *data_ptr, uint16_t frame_length);
102103

104+
mac_pre_parsed_frame_t *mcps_sap_pre_parsed_ack_buffer_get(struct protocol_interface_rf_mac_setup *rf_ptr, const uint8_t *data_ptr, uint16_t frame_length);
105+
103106
/**
104107
* Forward Buffer for MAC MCPS SAP layer event handler
105108
*/
@@ -112,7 +115,7 @@ int8_t mcps_sap_pd_confirm(void *mac_ptr);
112115

113116
int8_t mcps_sap_pd_confirm_failure(void *mac_ptr);
114117

115-
void mcps_sap_pd_ack(void *ack_ptr);
118+
int8_t mcps_sap_pd_ack(struct protocol_interface_rf_mac_setup *rf_ptr, struct mac_pre_parsed_frame_s *buffer);
116119

117120
int8_t mac_virtual_sap_data_cb(void *identifier, struct arm_phy_sap_msg_s *message);
118121

source/MAC/IEEE802_15_4/mac_pd_sap.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -631,14 +631,19 @@ static int8_t mac_data_interface_tx_done_cb(protocol_interface_rf_mac_setup_s *r
631631
return 0;
632632
}
633633

634-
635-
static int8_t mac_data_interface_tx_done_by_ack_cb(protocol_interface_rf_mac_setup_s *rf_ptr, mac_pre_parsed_frame_t *buf)
634+
static int8_t mac_data_interface_waiting_ack(protocol_interface_rf_mac_setup_s *rf_ptr, const mac_fcf_sequence_t *fcf_read)
636635
{
637-
638-
if (!rf_ptr->macRfRadioTxActive || !rf_ptr->active_pd_data_request || rf_ptr->active_pd_data_request->fcf_dsn.DSN != buf->fcf_dsn.DSN) {
636+
if (!rf_ptr->macRfRadioTxActive || !rf_ptr->active_pd_data_request || rf_ptr->active_pd_data_request->fcf_dsn.DSN != fcf_read->DSN) {
639637
return -1;
640638
}
641639

640+
return 0;
641+
}
642+
643+
644+
static int8_t mac_data_interface_tx_done_by_ack_cb(protocol_interface_rf_mac_setup_s *rf_ptr, mac_pre_parsed_frame_t *buf)
645+
{
646+
642647
timer_mac_stop(rf_ptr);
643648
if (buf->fcf_dsn.framePending) {
644649
rf_ptr->mac_tx_result = MAC_TX_DONE_PENDING;
@@ -647,7 +652,9 @@ static int8_t mac_data_interface_tx_done_by_ack_cb(protocol_interface_rf_mac_set
647652
}
648653
rf_ptr->macRfRadioTxActive = false;
649654
rf_ptr->macTxProcessActive = false;
650-
mcps_sap_pd_ack(buf);
655+
if (mcps_sap_pd_ack(rf_ptr, buf) != 0) {
656+
mcps_sap_pre_parsed_frame_buffer_free(buf);
657+
}
651658

652659
if (rf_ptr->fhss_api) {
653660
rf_ptr->fhss_api->data_tx_done(rf_ptr->fhss_api, false, true, rf_ptr->active_pd_data_request->msduHandle);
@@ -891,22 +898,35 @@ static int8_t mac_pd_sap_generate_edfe_response(protocol_interface_rf_mac_setup_
891898
static mac_pre_parsed_frame_t *mac_pd_sap_allocate_receive_buffer(protocol_interface_rf_mac_setup_s *rf_ptr, const mac_fcf_sequence_t *fcf_read, arm_pd_sap_generic_ind_t *pd_data_ind)
892899
{
893900
// Unless receiving Ack, check that system has enough space to handle the new packet
894-
if (fcf_read->frametype != FC_ACK_FRAME) {
895-
if (!ns_monitor_packet_allocation_allowed()) {
901+
mac_pre_parsed_frame_t *buffer = NULL;
902+
if (fcf_read->frametype != FC_ACK_FRAME || rf_ptr->macProminousMode) {
903+
if (!rf_ptr->macProminousMode && !ns_monitor_packet_allocation_allowed()) {
896904
// stack can not handle new packets for routing
897905
#ifdef __linux__
898906
tr_debug("Packet ingress drop buffer allocation");
899907
#endif
900908
return NULL;
901909
}
902-
}
903-
mac_pre_parsed_frame_t *buffer = mcps_sap_pre_parsed_frame_buffer_get(pd_data_ind->data_ptr, pd_data_ind->data_len);
904-
if (!buffer) {
910+
911+
buffer = mcps_sap_pre_parsed_frame_buffer_get(pd_data_ind->data_ptr, pd_data_ind->data_len);
912+
if (!buffer) {
905913
#ifdef __linux__
906-
tr_debug("macPD buffer allocate fail %u", pd_data_ind->data_len);
914+
tr_debug("macPD buffer allocate fail %u", pd_data_ind->data_len);
907915
#endif
908-
return NULL;
916+
return NULL;
917+
}
918+
} else {
919+
//Allocate ACK buffer
920+
buffer = mcps_sap_pre_parsed_ack_buffer_get(rf_ptr, pd_data_ind->data_ptr, pd_data_ind->data_len);
921+
if (!buffer) {
922+
#ifdef __linux__
923+
tr_debug("macPD ACK buffer allocate fail %u", pd_data_ind->data_len);
924+
#endif
925+
return NULL;
926+
927+
}
909928
}
929+
910930
//Copy Pre Parsed values
911931
buffer->fcf_dsn = *fcf_read;
912932
buffer->timestamp = mac_pd_sap_get_phy_rx_time(rf_ptr);
@@ -1034,6 +1054,13 @@ int8_t mac_pd_sap_data_cb(void *identifier, arm_phy_sap_msg_t *message)
10341054
#endif
10351055
goto ERROR_HANDLER;
10361056
}
1057+
if (fcf_read.frametype == FC_ACK_FRAME && mac_data_interface_waiting_ack(rf_ptr, &fcf_read)) {
1058+
#ifdef __linux__
1059+
tr_debug("Drop a ACK not a proper DSN");
1060+
#endif
1061+
goto ERROR_HANDLER;
1062+
}
1063+
10371064
}
10381065
//Allocate Buffer
10391066
buffer = mac_pd_sap_allocate_receive_buffer(rf_ptr, &fcf_read, pd_data_ind);
@@ -1108,7 +1135,7 @@ int8_t mac_pd_sap_data_cb(void *identifier, arm_phy_sap_msg_t *message)
11081135
//Mark session closed
11091136
rf_ptr->mac_edfe_info->state = MAC_EDFE_FRAME_IDLE;
11101137
rf_ptr->mac_edfe_tx_active = false;
1111-
if (mac_data_interface_tx_done_by_ack_cb(rf_ptr, buffer)) {
1138+
if (mac_data_interface_waiting_ack(rf_ptr, &buffer->fcf_dsn) || mac_data_interface_tx_done_by_ack_cb(rf_ptr, buffer)) {
11121139
mcps_sap_pre_parsed_frame_buffer_free(buffer);
11131140
}
11141141
return 0;

source/Service_Libs/utils/ns_conf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include "nsconfig.h"
1919
#include "ns_types.h"
20-
20+
#include "eventOS_event.h"
2121
#include "Core/include/ns_monitor.h"
2222
#include "mac_api.h" // for mcps_packet_ingress_rate_limit_by_memory
2323
#include "MAC/IEEE802_15_4/mac_mcps_sap.h" // for mcps_packet_ingress_rate_limit_by_memory

test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,7 +2225,7 @@ bool test_mcps_sap_ack_confirm()
22252225
{
22262226
protocol_interface_rf_mac_setup_s *rf_mac_setup = test_mac_rf_mac_class_allocate();
22272227
mac_mcps_sap_tasklet_init();
2228-
mcps_sap_pd_ack(NULL);
2228+
mcps_sap_pd_ack(rf_mac_setup, NULL);
22292229
uint8_t tx_buf[127];
22302230
memset(tx_buf, 1, 127);
22312231
mac_api_t mac_api;
@@ -2241,7 +2241,7 @@ bool test_mcps_sap_ack_confirm()
22412241
buffer->fcf_dsn.frameVersion = FC_ACK_FRAME;
22422242
buffer->mac_class_ptr = rf_mac_setup;
22432243
//Test No active TX
2244-
mcps_sap_pd_ack(buffer);
2244+
mcps_sap_pd_ack(rf_mac_setup, buffer);
22452245
buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 20);
22462246
buffer->fcf_dsn.frameVersion = FC_ACK_FRAME;
22472247
buffer->mac_class_ptr = rf_mac_setup;
@@ -2250,7 +2250,7 @@ bool test_mcps_sap_ack_confirm()
22502250
buf->upper_layer_request = true;
22512251
rf_mac_setup->active_pd_data_request = buf;
22522252
buffer->mac_class_ptr = rf_mac_setup;
2253-
mcps_sap_pd_ack(buffer);
2253+
mcps_sap_pd_ack(rf_mac_setup, buffer);
22542254
if (rf_mac_setup->active_pd_data_request) {
22552255
return false;
22562256
}

test/nanostack/unittest/mac/mac_pd_sap/test_mac_pd_sap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ bool test_mac_ack_handler()
775775
}
776776

777777
fcf.DSN = 0;
778-
778+
rf_ptr.macRfRadioTxActive = true;
779779
mac_header_helper_functions_stub.fcf = fcf;
780780
ret = mac_pd_sap_data_cb(&rf_ptr, &phy_message);
781781
if (ret != 0) {

test/nanostack/unittest/stub/mac_mcps_sap_stub.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ mac_pre_parsed_frame_t *mcps_sap_pre_parsed_frame_buffer_get(const uint8_t *data
110110
return mac_mcps_sap_stub.pre_parsed;
111111
}
112112

113+
mac_pre_parsed_frame_t *mcps_sap_pre_parsed_ack_buffer_get(struct protocol_interface_rf_mac_setup *rf_ptr, const uint8_t *data_ptr, uint16_t frame_length)
114+
{
115+
if (mac_mcps_sap_stub.pre_parsed) {
116+
memset(mac_mcps_sap_stub.pre_parsed, 0, sizeof(mac_pre_parsed_frame_t) + frame_length);
117+
mac_mcps_sap_stub.pre_parsed->frameLength = frame_length;
118+
memcpy(mac_header_message_start_pointer(mac_mcps_sap_stub.pre_parsed), data_ptr, frame_length);
119+
}
120+
return mac_mcps_sap_stub.pre_parsed;
121+
}
122+
113123
int8_t mcps_sap_pd_ind(mac_pre_parsed_frame_t *buffer)
114124
{
115125
return mac_mcps_sap_stub.int8_value;
@@ -125,8 +135,9 @@ int8_t mcps_sap_pd_confirm_failure(void *mac_ptr)
125135
return 0;
126136
}
127137

128-
void mcps_sap_pd_ack(void *ack_ptr)
138+
int8_t mcps_sap_pd_ack(struct protocol_interface_rf_mac_setup *rf_ptr, struct mac_pre_parsed_frame_s *buffer)
129139
{
140+
return 0;
130141
}
131142

132143

0 commit comments

Comments
 (0)