diff --git a/source/MAC/IEEE802_15_4/mac_mcps_sap.c b/source/MAC/IEEE802_15_4/mac_mcps_sap.c index 086de4503474..22b8061c8819 100644 --- a/source/MAC/IEEE802_15_4/mac_mcps_sap.c +++ b/source/MAC/IEEE802_15_4/mac_mcps_sap.c @@ -2351,12 +2351,6 @@ void mcps_sap_pre_parsed_frame_buffer_free(mac_pre_parsed_frame_t *buf) mac_pre_parsed_frame_t *mcps_sap_pre_parsed_frame_buffer_get(const uint8_t *data_ptr, uint16_t frame_length) { - // check that system has enough space to handle the new packet - if (!ns_monitor_packet_allocation_allowed()) { - // stack can not handle new packets for routing - return NULL; - } - mac_pre_parsed_frame_t *buffer = ns_dyn_mem_temporary_alloc(sizeof(mac_pre_parsed_frame_t) + frame_length); if (buffer) { diff --git a/source/MAC/IEEE802_15_4/mac_pd_sap.c b/source/MAC/IEEE802_15_4/mac_pd_sap.c index 54669e879451..799205ceb0e2 100644 --- a/source/MAC/IEEE802_15_4/mac_pd_sap.c +++ b/source/MAC/IEEE802_15_4/mac_pd_sap.c @@ -36,6 +36,7 @@ #include "MAC/IEEE802_15_4/mac_mcps_sap.h" #include "MAC/IEEE802_15_4/mac_cca_threshold.h" #include "MAC/rf_driver_storage.h" +#include "Core/include/ns_monitor.h" #include "ns_trace.h" #define TRACE_GROUP "mPDs" @@ -904,11 +905,17 @@ static int8_t mac_pd_sap_generate_edfe_response(protocol_interface_rf_mac_setup_ 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) { + // Unless receiving Ack, check that system has enough space to handle the new packet + if (fcf_read->frametype != FC_ACK_FRAME) { + if (!ns_monitor_packet_allocation_allowed()) { + // stack can not handle new packets for routing + return NULL; + } + } mac_pre_parsed_frame_t *buffer = mcps_sap_pre_parsed_frame_buffer_get(pd_data_ind->data_ptr, pd_data_ind->data_len); if (!buffer) { return NULL; } - //Copy Pre Parsed values buffer->fcf_dsn = *fcf_read; buffer->timestamp = mac_pd_sap_get_phy_rx_time(rf_ptr); diff --git a/test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.c b/test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.c index cc72f50549d8..9508acf6e153 100644 --- a/test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.c +++ b/test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.c @@ -2194,26 +2194,6 @@ bool test_mcps_sap_pre_parsed_frame_buffer_get() if (memcmp(mac_header_message_start_pointer(buf), buffer, 8) != 0) { return false; } - ns_dyn_mem_free(buf); - - // Test that if dynamic memory is low, then allocation fails - nsdynmemlib_stub.returnCounter = 1; - ns_monitor_stub.return_bool = false; - - buf = mcps_sap_pre_parsed_frame_buffer_get(buffer, 8); - if (buf) { - return false; - } - - // Test that allocation succeeds when there is enough heap - nsdynmemlib_stub.returnCounter = 1; - ns_monitor_stub.return_bool = true; - - buf = mcps_sap_pre_parsed_frame_buffer_get(buffer, 8); - if (!buf || buf->frameLength != 8) { - return false; - } - ns_dyn_mem_free(buf); return true; } diff --git a/test/nanostack/unittest/mac/mac_pd_sap/Makefile b/test/nanostack/unittest/mac/mac_pd_sap/Makefile index 41e17bb44cba..e9dd6cd02d76 100644 --- a/test/nanostack/unittest/mac/mac_pd_sap/Makefile +++ b/test/nanostack/unittest/mac/mac_pd_sap/Makefile @@ -27,6 +27,7 @@ TEST_SRC_FILES = \ ../../stub/buffer_dyn_stub.c \ ../../stub/fhss_config_stub.c \ ../../stub/sw_mac_stub.c \ + ../../stub/ns_monitor_stub.c \ include ../../MakefileWorker.mk diff --git a/test/nanostack/unittest/mac/mac_pd_sap/test_mac_pd_sap.c b/test/nanostack/unittest/mac/mac_pd_sap/test_mac_pd_sap.c index 713fcfc5a73c..a217e6206c71 100644 --- a/test/nanostack/unittest/mac/mac_pd_sap/test_mac_pd_sap.c +++ b/test/nanostack/unittest/mac/mac_pd_sap/test_mac_pd_sap.c @@ -33,6 +33,7 @@ #include "nsdynmemLIB_stub.h" #include "fhss_config_stub.h" #include "sw_mac_stub.h" +#include "ns_monitor_stub.h" #include static int8_t tx_return = -1; @@ -438,7 +439,29 @@ bool test_mac_pd_sap_data_cb() return false; } + // Test when ns monitor don't allow reception + ns_monitor_stub.return_bool = false; + mac_mcps_sap_stub.pre_parsed = frame_buf; + nsdynmemlib_stub.returnCounter = 2; + fcf.frametype = FC_DATA_FRAME; + mac_header_helper_functions_stub.fcf = fcf; + phy_message.message.generic_data_ind.data_len = 32; + ret = mac_pd_sap_data_cb(&rf_ptr, &phy_message); + if (ret != -3) { + return false; + } + // Test when ns monitor allows Ack reception + ns_monitor_stub.return_bool = false; + mac_mcps_sap_stub.pre_parsed = frame_buf; + nsdynmemlib_stub.returnCounter = 2; + fcf.frametype = FC_ACK_FRAME; + mac_header_helper_functions_stub.fcf = fcf; + phy_message.message.generic_data_ind.data_len = 32; + ret = mac_pd_sap_data_cb(&rf_ptr, &phy_message); + if (ret != -1) { + return false; + } //Test MAC15_4_PD_SAP_DATA_TX_CONFIRM phy_message.id = MAC15_4_PD_SAP_DATA_TX_CONFIRM;