Skip to content

Commit

Permalink
Remove recursion from MAC layer ARMmbed#2 (ARMmbed#1830)
Browse files Browse the repository at this point in the history
Use event to report data request failure to avoid recursion. In case
of event sending fails then call error handler directly.
  • Loading branch information
Arto Kinnunen authored Sep 24, 2018
1 parent 2f5a2ce commit 2b8a011
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 24 deletions.
6 changes: 6 additions & 0 deletions source/MAC/IEEE802_15_4/mac_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ typedef struct mac_tx_status_t {
uint8_t retry;
} mac_tx_status_t;

typedef struct mac_mcps_data_conf_fail_s {
uint8_t msduHandle; /**< Handle associated with MSDU */
uint8_t status; /**< Status of the failing MSDU transmission */
} mac_mcps_data_conf_fail_t;

typedef struct protocol_interface_rf_mac_setup {
int8_t mac_interface_id;
bool macUpState;
Expand Down Expand Up @@ -231,6 +236,7 @@ typedef struct protocol_interface_rf_mac_setup {
uint8_t max_ED;
uint16_t mlme_ED_counter;
mac_tx_status_t mac_tx_status;
mac_mcps_data_conf_fail_t mac_mcps_data_conf_fail;
struct cca_structure_s *cca_structure;
/* MAC Security components */
struct mlme_device_descriptor_s *device_description_table;
Expand Down
56 changes: 46 additions & 10 deletions source/MAC/IEEE802_15_4/mac_mcps_sap.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static void mac_set_active_event(protocol_interface_rf_mac_setup_s *rf_mac_setup
static void mac_clear_active_event(protocol_interface_rf_mac_setup_s *rf_mac_setup, uint8_t event_type);
static bool mac_read_active_event(protocol_interface_rf_mac_setup_s *rf_mac_setup, uint8_t event_type);
static int8_t mcps_pd_data_cca_trig(protocol_interface_rf_mac_setup_s *rf_ptr, mac_pre_build_frame_t *buffer);
static void mac_pd_data_confirm_failure_handle(protocol_interface_rf_mac_setup_s *rf_mac_setup);

static int8_t mac_tasklet_event_handler = -1;

Expand Down Expand Up @@ -362,13 +363,14 @@ void mcps_sap_data_req_handler_ext(protocol_interface_rf_mac_setup_s *rf_mac_set

verify_status:
if (status != MLME_SUCCESS){
mcps_data_conf_t confirm;
memset(&confirm, 0, sizeof(mcps_data_conf_t));
confirm.msduHandle = data_req->msduHandle;
confirm.status = status;
tr_debug("DATA REQ Fail %u", status);
rf_mac_setup->mac_mcps_data_conf_fail.msduHandle = data_req->msduHandle;
rf_mac_setup->mac_mcps_data_conf_fail.status = status;
mcps_sap_prebuild_frame_buffer_free(buffer);
mcps_data_confirm_cb(rf_mac_setup, &confirm, NULL);
if (mcps_sap_pd_confirm_failure(rf_mac_setup) != 0) {
// event sending failed, calling handler directly
mac_pd_data_confirm_failure_handle(rf_mac_setup);
}
}
}

Expand Down Expand Up @@ -1093,7 +1095,6 @@ void mac_mcps_trig_buffer_from_queue(protocol_interface_rf_mac_setup_s *rf_mac_s
}
}


static int8_t mac_ack_sap_rx_handler(mac_pre_parsed_frame_t *buf, protocol_interface_rf_mac_setup_s *rf_mac_setup)
{
//allocate Data ind primitiv and parse packet to that
Expand Down Expand Up @@ -1144,6 +1145,14 @@ static void mac_pd_data_confirm_handle(protocol_interface_rf_mac_setup_s *rf_mac
mac_mcps_trig_buffer_from_queue(rf_mac_setup);
}

static void mac_pd_data_confirm_failure_handle(protocol_interface_rf_mac_setup_s *rf_mac_setup)
{
mcps_data_conf_t mcps_data_conf;
memset(&mcps_data_conf, 0, sizeof(mcps_data_conf_t));
mcps_data_conf.msduHandle = rf_mac_setup->mac_mcps_data_conf_fail.msduHandle;
mcps_data_conf.status = rf_mac_setup->mac_mcps_data_conf_fail.status;
mcps_data_confirm_cb(rf_mac_setup, &mcps_data_conf, NULL);
}

static void mac_pd_data_ack_handler(mac_pre_parsed_frame_t *buf)
{
Expand Down Expand Up @@ -1187,6 +1196,10 @@ static void mac_mcps_sap_data_tasklet(arm_event_s *event)
mac_pd_data_confirm_handle((protocol_interface_rf_mac_setup_s*)event->data_ptr);
break;

case MCPS_SAP_DATA_CNF_FAIL_EVENT:
mac_pd_data_confirm_failure_handle((protocol_interface_rf_mac_setup_s*)event->data_ptr);
break;

case MCPS_SAP_DATA_ACK_CNF_EVENT:
mac_pd_data_ack_handler((mac_pre_parsed_frame_t*)event->data_ptr);
break;
Expand Down Expand Up @@ -1907,7 +1920,14 @@ void mcps_sap_pd_req_queue_write(protocol_interface_rf_mac_setup_s *rf_mac_setup
if (mcps_pd_data_request(rf_mac_setup, buffer) != 0) {
rf_mac_setup->mac_tx_result = MAC_TX_PRECOND_FAIL;
rf_mac_setup->macTxRequestAck = false;
mcps_sap_pd_confirm(rf_mac_setup);
if (mcps_sap_pd_confirm(rf_mac_setup) != 0) {
// can't send event, try calling error handler directly
rf_mac_setup->mac_mcps_data_conf_fail.msduHandle = buffer->msduHandle;
rf_mac_setup->mac_mcps_data_conf_fail.status = buffer->status;
mcps_sap_prebuild_frame_buffer_free(buffer);
rf_mac_setup->active_pd_data_request = NULL;
mac_pd_data_confirm_failure_handle(rf_mac_setup);
}
}

return;
Expand Down Expand Up @@ -2087,10 +2107,10 @@ int8_t mcps_sap_pd_ind(mac_pre_parsed_frame_t *buffer)
return eventOS_event_send(&event);
}

void mcps_sap_pd_confirm(void *mac_ptr)
int8_t mcps_sap_pd_confirm(void *mac_ptr)
{
if (mac_tasklet_event_handler < 0 || !mac_ptr) {
return;
return -2;
}
arm_event_s event = {
.receiver = mac_tasklet_event_handler,
Expand All @@ -2101,8 +2121,24 @@ void mcps_sap_pd_confirm(void *mac_ptr)
.priority = ARM_LIB_HIGH_PRIORITY_EVENT,
};

eventOS_event_send(&event);
return eventOS_event_send(&event);
}

int8_t mcps_sap_pd_confirm_failure(void *mac_ptr)
{
if (mac_tasklet_event_handler < 0 || !mac_ptr) {
return -2;
}
arm_event_s event = {
.receiver = mac_tasklet_event_handler,
.sender = 0,
.event_id = 0,
.data_ptr = mac_ptr,
.event_type = MCPS_SAP_DATA_CNF_FAIL_EVENT,
.priority = ARM_LIB_HIGH_PRIORITY_EVENT,
};

return eventOS_event_send(&event);
}

void mcps_sap_pd_ack(void *ack_ptr)
Expand Down
20 changes: 11 additions & 9 deletions source/MAC/IEEE802_15_4/mac_mcps_sap.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ typedef enum {
#define MAC_PD_DATA_MEDIUM_PRIORITY 1 //Indirect Data which is polled
#define MAC_PD_DATA_HIGH_PRIOTITY 2 //Beacon request Beacon response

#define MCPS_SAP_DATA_IND_EVENT 1
#define MCPS_SAP_DATA_CNF_EVENT 2
#define MAC_MLME_EVENT_HANDLER 3
#define MAC_MCPS_INDIRECT_TIMER_CB 4
#define MAC_MLME_SCAN_CONFIRM_HANDLER 5
#define MAC_SAP_TRIG_TX 6
#define MCPS_SAP_DATA_ACK_CNF_EVENT 7

#define MCPS_SAP_DATA_IND_EVENT 1
#define MCPS_SAP_DATA_CNF_EVENT 2
#define MCPS_SAP_DATA_CNF_FAIL_EVENT 3
#define MAC_MLME_EVENT_HANDLER 4
#define MAC_MCPS_INDIRECT_TIMER_CB 5
#define MAC_MLME_SCAN_CONFIRM_HANDLER 6
#define MAC_SAP_TRIG_TX 7
#define MCPS_SAP_DATA_ACK_CNF_EVENT 8

/**
* @brief struct mac_aux_security_header_t MAC auxiliarity security header structure
Expand Down Expand Up @@ -180,7 +180,9 @@ int8_t mcps_sap_pd_ind(mac_pre_parsed_frame_t *buffer);
/**
* MAC MCPS SAP layer data confirmation event trig
*/
void mcps_sap_pd_confirm(void *mac_ptr);
int8_t mcps_sap_pd_confirm(void *mac_ptr);

int8_t mcps_sap_pd_confirm_failure(void *mac_ptr);

void mcps_sap_pd_ack(void *ack_ptr);

Expand Down
6 changes: 2 additions & 4 deletions test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2463,8 +2463,7 @@ bool test_mcps_sap_data_req_handler()
nsdynmemlib_stub.returnCounter = 1;
data_req.msduHandle = msdu_handle++;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
/* async callback is not called, active_pd_data_request available in rf_mac_setup */
if (!rf_mac_setup->active_pd_data_request || data_confirm_valid) {
if (rf_mac_setup->active_pd_data_request || !data_confirm_valid) {
return false;
}

Expand All @@ -2477,8 +2476,7 @@ bool test_mcps_sap_data_req_handler()
data_req.TxAckReq = true;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
data_req.msduHandle = msdu_handle++;
/* async callback is not called, active_pd_data_request available in rf_mac_setup */
if (!rf_mac_setup->active_pd_data_request || data_confirm_valid) {
if (rf_mac_setup->active_pd_data_request || !data_confirm_valid) {
return false;
}

Expand Down
8 changes: 7 additions & 1 deletion test/nanostack/unittest/stub/mac_mcps_sap_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,14 @@ int8_t mcps_sap_pd_ind(mac_pre_parsed_frame_t *buffer)
return mac_mcps_sap_stub.int8_value;
}

void mcps_sap_pd_confirm(void *mac_ptr)
int8_t mcps_sap_pd_confirm(void *mac_ptr)
{
return 0;
}

int8_t mcps_sap_pd_confirm_failure(void *mac_ptr)
{
return 0;
}

void mcps_sap_pd_ack(void *ack_ptr)
Expand Down

0 comments on commit 2b8a011

Please sign in to comment.