Skip to content

Commit

Permalink
update states in CCID and webusb
Browse files Browse the repository at this point in the history
  • Loading branch information
z4yx committed Mar 11, 2024
1 parent f2e0d24 commit dee04c8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
51 changes: 30 additions & 21 deletions interfaces/USB/class/ccid/ccid.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <usbd_ccid.h>

#define CCID_UpdateCommandStatus(cmd_status, icc_status) bulkin_data.bStatus = (cmd_status | icc_status)
#define CCID_CardStatus() (bulkin_data.bStatus & BM_ICC_STATUS_MASK)
#define CCID_IsShortCommand() (bulkout_data.dwLength <= SHORT_ABDATA_SIZE)

static uint8_t CCID_CheckCommandParams(uint32_t param_type);
Expand All @@ -27,7 +28,6 @@ static volatile uint32_t send_data_spinlock;
static CAPDU apdu_cmd;
static RAPDU apdu_resp;
uint8_t *global_buffer;
static uint8_t card_status;

void init_apdu_buffer(void) {
global_buffer = bulkin_data.abData;
Expand All @@ -39,7 +39,7 @@ uint8_t CCID_Init(void) {
has_cmd = 0;
apdu_cmd.data = bulkin_data.abData;
apdu_resp.data = bulkin_data.abData;
card_status = BM_ICC_PRESENT_INACTIVE;
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_NO_ERROR, BM_ICC_PRESENT_INACTIVE);
return 0;
}

Expand All @@ -66,6 +66,7 @@ uint8_t CCID_OutEvent(uint8_t *data, uint8_t len) {
// global_buffer is not available, discarding abData
// only PC_to_RDR_XfrBlock and PC_to_RDR_Secure should get here
bulkout_data.bMessageType = PC_TO_RDR_DISCARDED;
DBG_MSG("Discard data because of buffer conflict\n");
} else {
// global_buffer is acquired before use
abData = global_buffer;
Expand Down Expand Up @@ -122,12 +123,13 @@ static uint8_t PC_to_RDR_IccPowerOn(void) {

uint8_t voltage = bulkout_data.bSpecific_0;
if (voltage != 0x00) {
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_PRESENT_ACTIVE);
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, CCID_CardStatus());
return SLOTERROR_BAD_POWERSELECT;
}

// acquire the global buffer before using bulkin_data.abData
if (acquire_apdu_buffer(BUFFER_OWNER_CCID) != 0) {
DBG_MSG("Cannot acquire the buffer\n");
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_NO_ICC_PRESENT);
return SLOTERROR_ICC_MUTE;
}
Expand Down Expand Up @@ -162,7 +164,7 @@ static uint8_t PC_to_RDR_IccPowerOff(void) {
static uint8_t PC_to_RDR_GetSlotStatus(void) {
uint8_t error = CCID_CheckCommandParams(CHK_PARAM_SLOT | CHK_PARAM_DWLENGTH | CHK_PARAM_abRFU3);
if (error != 0) return error;
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_NO_ERROR, card_status);
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_NO_ERROR, CCID_CardStatus());
return SLOT_NO_ERROR;
}

Expand All @@ -178,13 +180,14 @@ uint8_t PC_to_RDR_XfrBlock(void) {
uint8_t error = CCID_CheckCommandParams(CHK_PARAM_SLOT);
if (error != 0) return error;

if (card_status == BM_ICC_NO_ICC_PRESENT) {
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_NO_ICC_PRESENT);
if (CCID_CardStatus() == BM_ICC_NO_ICC_PRESENT) {
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, CCID_CardStatus());
return SLOTERROR_ICC_MUTE;
}

// acquire the global buffer before using bulkin_data.abData
if (acquire_apdu_buffer(BUFFER_OWNER_CCID) != 0) {
DBG_MSG("Cannot acquire the buffer for R-APDU\n");
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_NO_ICC_PRESENT);
return SLOTERROR_ICC_MUTE;
}
Expand Down Expand Up @@ -225,7 +228,7 @@ uint8_t PC_to_RDR_XfrBlock(void) {
static uint8_t PC_to_RDR_GetParameters(void) {
uint8_t error = CCID_CheckCommandParams(CHK_PARAM_SLOT | CHK_PARAM_DWLENGTH | CHK_PARAM_abRFU3);
if (error != 0) return error;
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_NO_ERROR, BM_ICC_PRESENT_ACTIVE);
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_NO_ERROR, CCID_CardStatus());
return SLOT_NO_ERROR;
}

Expand Down Expand Up @@ -275,6 +278,7 @@ static void RDR_to_PC_Parameters(uint8_t errorCode) {

// acquire the global buffer before using bulkin_data.abData
if (acquire_apdu_buffer(BUFFER_OWNER_CCID) != 0) {
DBG_MSG("Cannot acquire the buffer\n");
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_NO_ICC_PRESENT);
bulkin_data.bError = SLOTERROR_ICC_MUTE;
return;
Expand Down Expand Up @@ -314,37 +318,37 @@ static void RDR_to_PC_Escape(uint8_t errorCode) {
* @retval uint8_t status
*/
static uint8_t CCID_CheckCommandParams(uint32_t param_type) {
bulkin_data.bStatus = BM_ICC_PRESENT_ACTIVE | BM_COMMAND_STATUS_NO_ERROR;
uint32_t parameter = param_type;

if (parameter & CHK_PARAM_SLOT) {
if (bulkout_data.bSlot >= CCID_NUMBER_OF_SLOTS) {
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_NO_ICC_PRESENT);
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, CCID_CardStatus());
return SLOTERROR_BAD_SLOT;
}
}

if (parameter & CHK_PARAM_DWLENGTH) {
if (bulkout_data.dwLength != 0) {
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_PRESENT_ACTIVE);
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, CCID_CardStatus());
return SLOTERROR_BAD_LENTGH;
}
}

if (parameter & CHK_PARAM_abRFU2) {
if ((bulkout_data.bSpecific_1 != 0) || (bulkout_data.bSpecific_2 != 0)) {
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_PRESENT_ACTIVE);
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, CCID_CardStatus());
return SLOTERROR_BAD_ABRFU_2B;
}
}

if (parameter & CHK_PARAM_abRFU3) {
if ((bulkout_data.bSpecific_0 != 0) || (bulkout_data.bSpecific_1 != 0) || (bulkout_data.bSpecific_2 != 0)) {
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_PRESENT_ACTIVE);
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, CCID_CardStatus());
return SLOTERROR_BAD_ABRFU_3B;
}
}

CCID_UpdateCommandStatus(BM_COMMAND_STATUS_NO_ERROR, CCID_CardStatus());
return 0;
}

Expand All @@ -353,7 +357,6 @@ void CCID_Loop(void) {
has_cmd = 0;

uint8_t errorCode;
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_NO_ERROR, card_status);
switch (bulkout_data.bMessageType) {
case PC_TO_RDR_ICCPOWERON:
DBG_MSG("Slot power on\n");
Expand All @@ -366,7 +369,7 @@ void CCID_Loop(void) {
RDR_to_PC_SlotStatus(errorCode);
break;
case PC_TO_RDR_GETSLOTSTATUS:
DBG_MSG("Slot get status\n");
// DBG_MSG("Slot get status\n");
errorCode = PC_to_RDR_GetSlotStatus();
RDR_to_PC_SlotStatus(errorCode);
break;
Expand All @@ -391,6 +394,7 @@ void CCID_Loop(void) {
RDR_to_PC_DataBlock(SLOTERROR_CMD_NOT_SUPPORTED);
break;
case PC_TO_RDR_DISCARDED:
DBG_MSG("Respond to a data-discarded message\n");
bulkin_data.dwLength = 0;
CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_NO_ICC_PRESENT);
RDR_to_PC_DataBlock(SLOTERROR_ICC_MUTE);
Expand All @@ -413,7 +417,10 @@ void CCID_Loop(void) {

void CCID_InFinished(uint8_t is_time_extension_request)
{
if (is_time_extension_request) return;
if (is_time_extension_request) {
DBG_MSG("Time-ext sent\n");
return;
}

// Release the buffer after bulkin_data is transmitted
// If the buffer has not been acquired by CCID, ownership is unchanged
Expand All @@ -437,10 +444,12 @@ void CCID_TimeExtensionLoop(void) {
device_set_timeout(CCID_TimeExtensionLoop, TIME_EXTENSION_PERIOD);
}

void CCID_eject(void) {
card_status = BM_ICC_NO_ICC_PRESENT;
}
// void CCID_eject(void) {
// DBG_MSG("EJ\n");
// CCID_UpdateCommandStatus(BM_COMMAND_STATUS_FAILED, BM_ICC_NO_ICC_PRESENT);
// }

void CCID_insert(void) {
card_status = BM_ICC_PRESENT_INACTIVE;
}
// void CCID_insert(void) {
// DBG_MSG("INS\n");
// CCID_UpdateCommandStatus(BM_COMMAND_STATUS_NO_ERROR, BM_ICC_PRESENT_INACTIVE);
// }
5 changes: 3 additions & 2 deletions interfaces/USB/class/ccid/ccid.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ typedef struct {
#define SLOTERROR_CMD_SLOT_BUSY 0xE0
#define SLOTERROR_CMD_NOT_SUPPORTED 0x00

#define BM_ICC_STATUS_MASK 0x03
#define BM_ICC_PRESENT_ACTIVE 0x00
#define BM_ICC_PRESENT_INACTIVE 0x01
#define BM_ICC_NO_ICC_PRESENT 0x02
Expand Down Expand Up @@ -136,7 +137,7 @@ void CCID_InFinished(uint8_t is_time_extension_request);
void CCID_Loop(void);
void CCID_TimeExtensionLoop(void);
uint8_t PC_to_RDR_XfrBlock(void); // Exported for test purposes
void CCID_eject(void);
void CCID_insert(void);
// void CCID_eject(void);
// void CCID_insert(void);

#endif //_CCID_H_
27 changes: 22 additions & 5 deletions interfaces/USB/class/webusb/webusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ enum {
STATE_PROCESS = 1,
STATE_SENDING_RESP = 0,
STATE_SENT_RESP = 2,
STATE_RECVING = 3,
STATE_HOLD_BUF = 4,
};

static uint8_t state;
static int8_t state;
static uint16_t apdu_buffer_size;
static CAPDU apdu_cmd;
static RAPDU apdu_resp;
Expand All @@ -29,22 +31,30 @@ uint8_t USBD_WEBUSB_Init(USBD_HandleTypeDef *pdev) {
}

uint8_t USBD_WEBUSB_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) {
CCID_eject();
// CCID_eject();
last_keepalive = device_get_tick();
switch (req->bRequest) {
case WEBUSB_REQ_CMD:
if (state != STATE_IDLE && state != STATE_HOLD_BUF) {
ERR_MSG("Wrong state %d\n", state);
USBD_CtlError(pdev, req);
return USBD_FAIL;
}
if (acquire_apdu_buffer(BUFFER_OWNER_WEBUSB) != 0) {
ERR_MSG("Busy\n");
USBD_CtlError(pdev, req);
return USBD_FAIL;
}
state = STATE_HOLD_BUF;
//DBG_MSG("Buf Acquired\n");
if (req->wLength > APDU_BUFFER_SIZE) {
ERR_MSG("Overflow\n");
USBD_CtlError(pdev, req);
return USBD_FAIL;
}
USBD_CtlPrepareRx(pdev, global_buffer, req->wLength);
apdu_buffer_size = req->wLength;
state = STATE_RECVING;
break;

case WEBUSB_REQ_RESP:
Expand All @@ -71,7 +81,12 @@ uint8_t USBD_WEBUSB_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) {
}

void WebUSB_Loop(void) {
if (device_get_tick() - last_keepalive > 2000) CCID_insert();
if (device_get_tick() - last_keepalive > 2000 && state == STATE_HOLD_BUF) {
DBG_MSG("Release buffer after time-out\n");
release_apdu_buffer(BUFFER_OWNER_WEBUSB);
// CCID_insert();
state = STATE_IDLE;
}
if (state != STATE_PROCESS) return;

DBG_MSG("C: ");
Expand Down Expand Up @@ -99,9 +114,10 @@ void WebUSB_Loop(void) {
uint8_t USBD_WEBUSB_TxSent(USBD_HandleTypeDef *pdev) {
UNUSED(pdev);

//DBG_MSG("state = %d\n", state);
if (state == STATE_SENT_RESP) {
release_apdu_buffer(BUFFER_OWNER_WEBUSB);
state = STATE_IDLE;
// release_apdu_buffer(BUFFER_OWNER_WEBUSB);
state = STATE_HOLD_BUF;
}

return USBD_OK;
Expand All @@ -110,6 +126,7 @@ uint8_t USBD_WEBUSB_TxSent(USBD_HandleTypeDef *pdev) {
uint8_t USBD_WEBUSB_RxReady(USBD_HandleTypeDef *pdev) {
UNUSED(pdev);

// state should be STATE_RECVING now
state = STATE_PROCESS;

return USBD_OK;
Expand Down

0 comments on commit dee04c8

Please sign in to comment.