aci_status_code_t do_aci_setup(aci_state_t *aci_stat) { uint8_t setup_offset = 0; uint16_t i = 0x0000; aci_evt_t * aci_evt = NULL; /* We are using the same buffer since we are copying the contents of the buffer when queuing and immediately processing the buffer when receiving */ hal_aci_evt_t *aci_data = (hal_aci_evt_t *)&msg_to_send; aci_evt->params.cmd_rsp.cmd_status = ACI_STATUS_ERROR_CRC_MISMATCH; while (aci_evt->params.cmd_rsp.cmd_status != ACI_STATUS_TRANSACTION_COMPLETE) { if (setup_offset < aci_stat->aci_setup_info.num_setup_msgs) { aci_setup_fill(aci_stat, &setup_offset); } i++; //i is used as a guard counter, if this counter overflows, there is an error if (i > 0xFFFE) { return ACI_STATUS_ERROR_INTERNAL; } if (true == lib_aci_event_get(aci_stat, aci_data)) { aci_evt = &(aci_data->evt); if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode ) { //Got something other than a command response evt -> Error return ACI_STATUS_ERROR_INTERNAL; } switch (aci_evt->params.cmd_rsp.cmd_status) { case ACI_STATUS_TRANSACTION_CONTINUE: //Go back to the the top of the loop so the queue can be filled up again break; case ACI_STATUS_TRANSACTION_COMPLETE: //Break out of the while loop when this status code appears break; default: //Any other status code is an error return (aci_status_code_t )aci_evt->params.cmd_rsp.cmd_status; break; } } } return (aci_status_code_t)aci_evt->params.cmd_rsp.cmd_status; }
uint8_t do_aci_setup(aci_state_t *aci_stat) { uint8_t setup_offset = 0; uint32_t i = 0x0000; aci_evt_t * aci_evt = NULL; aci_status_code_t cmd_status = ACI_STATUS_ERROR_CRC_MISMATCH; /* We are using the same buffer since we are copying the contents of the buffer when queuing and immediately processing the buffer when receiving */ hal_aci_evt_t *aci_data = (hal_aci_evt_t *)&msg_to_send; /* Messages in the outgoing queue must be handled before the Setup routine can run. * If it is non-empty we return. The user should then process the messages before calling * do_aci_setup() again. */ if (!lib_aci_command_queue_empty()) { return SETUP_FAIL_COMMAND_QUEUE_NOT_EMPTY; } /* If there are events pending from the device that are not relevant to setup, we return false * so that the user can handle them. At this point we don't care what the event is, * as any event is an error. */ if (lib_aci_event_peek(aci_data)) { return SETUP_FAIL_EVENT_QUEUE_NOT_EMPTY; } /* Fill the ACI command queue with as many Setup messages as it will hold. */ aci_setup_fill(aci_stat, &setup_offset); while (cmd_status != ACI_STATUS_TRANSACTION_COMPLETE) { /* This counter is used to ensure that this function does not loop forever. When the device * returns a valid response, we reset the counter. */ if (i++ > 0xFFFFE) { return SETUP_FAIL_TIMEOUT; } if (lib_aci_event_peek(aci_data)) { aci_evt = &(aci_data->evt); if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode) { //Receiving something other than a Command Response Event is an error. return SETUP_FAIL_NOT_COMMAND_RESPONSE; } cmd_status = (aci_status_code_t) aci_evt->params.cmd_rsp.cmd_status; switch (cmd_status) { case ACI_STATUS_TRANSACTION_CONTINUE: //As the device is responding, reset guard counter i = 0; /* As the device has processed the Setup messages we put in the command queue earlier, * we can proceed to fill the queue with new messages */ aci_setup_fill(aci_stat, &setup_offset); break; case ACI_STATUS_TRANSACTION_COMPLETE: //Break out of the while loop when this status code appears break; default: //An event with any other status code should be handled by the application return SETUP_FAIL_NOT_SETUP_EVENT; } /* If we haven't returned at this point, the event was either ACI_STATUS_TRANSACTION_CONTINUE * or ACI_STATUS_TRANSACTION_COMPLETE. We don't need the event itself, so we simply * remove it from the queue. */ lib_aci_event_get (aci_stat, aci_data); } } return SETUP_SUCCESS; }