diff --git a/ccid.c b/ccid.c index 555413f..56a554b 100644 --- a/ccid.c +++ b/ccid.c @@ -364,7 +364,14 @@ size_t seader_ccid_process(Seader* seader, uint8_t* cmd, size_t cmd_len) { if(message.bMessageType == CCID_MESSAGE_TYPE_RDR_to_PC_Parameters) { FURI_LOG_D(TAG, "Got Parameters"); - seader_t_1_set_IFSD(seader); + if(seader->is_debug_enabled) { + seader_t_1_set_IFSD(seader); + } else { + seader_worker_send_version(seader); + if(seader_worker->callback) { + seader_worker->callback(SeaderWorkerEventSamPresent, seader_worker->context); + } + } } else if(message.bMessageType == CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock) { if(hasSAM) { if(message.bSlot == sam_slot) { diff --git a/sam_api.c b/sam_api.c index 0280c22..3e40b5c 100644 --- a/sam_api.c +++ b/sam_api.c @@ -33,7 +33,7 @@ void* calloc(size_t count, size_t size) { } // Forward declarations -void seader_send_nfc_rx(SeaderUartBridge* seader_uart, uint8_t* buffer, size_t len); +void seader_send_nfc_rx(Seader* seader, uint8_t* buffer, size_t len); PicopassError seader_worker_fake_epurse_update(BitBuffer* tx_buffer, BitBuffer* rx_buffer) { const uint8_t* buffer = bit_buffer_get_data(tx_buffer); @@ -55,9 +55,6 @@ PicopassError seader_worker_fake_epurse_update(BitBuffer* tx_buffer, BitBuffer* } void seader_picopass_state_machine(Seader* seader, uint8_t* buffer, size_t len) { - SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; - BitBuffer* tx_buffer = bit_buffer_alloc(len); bit_buffer_append_bytes(tx_buffer, buffer, len); BitBuffer* rx_buffer = bit_buffer_alloc(SEADER_POLLER_MAX_BUFFER_SIZE); @@ -130,7 +127,7 @@ void seader_picopass_state_machine(Seader* seader, uint8_t* buffer, size_t len) } seader_send_nfc_rx( - seader_uart, + seader, (uint8_t*)bit_buffer_get_data(rx_buffer), bit_buffer_get_size_bytes(rx_buffer)); @@ -139,15 +136,21 @@ void seader_picopass_state_machine(Seader* seader, uint8_t* buffer, size_t len) bit_buffer_free(rx_buffer); } -#define APDU_HEADER_LEN 7 +uint8_t APDU_HEADER_LEN = 5; bool seader_send_apdu( - SeaderUartBridge* seader_uart, + Seader* seader, uint8_t CLA, uint8_t INS, uint8_t P1, uint8_t P2, uint8_t* payload, uint8_t payloadLen) { + SeaderUartBridge* seader_uart = seader->worker->uart; + + if(seader->is_debug_enabled) { + APDU_HEADER_LEN = 7; + } + if(APDU_HEADER_LEN + payloadLen > SEADER_UART_RX_BUF_SIZE) { FURI_LOG_E(TAG, "Cannot send message, too long: %d", APDU_HEADER_LEN + payloadLen); return false; @@ -158,9 +161,15 @@ bool seader_send_apdu( apdu[1] = INS; apdu[2] = P1; apdu[3] = P2; - apdu[4] = 0x00; - apdu[5] = 0x00; - apdu[6] = payloadLen; + + if(seader->is_debug_enabled) { + apdu[4] = 0x00; + apdu[5] = 0x00; + apdu[6] = payloadLen; + } else { + apdu[4] = payloadLen; + } + memcpy(apdu + APDU_HEADER_LEN, payload, payloadLen); uint8_t length = APDU_HEADER_LEN + payloadLen; @@ -170,7 +179,12 @@ bool seader_send_apdu( } FURI_LOG_D(TAG, "seader_send_apdu %s", display); - seader_send_t1(seader_uart, apdu, length); + if(seader->is_debug_enabled) { + seader_send_t1(seader_uart, apdu, length); + } else { + seader_ccid_XfrBlock(seader_uart, apdu, length); + } + return true; } @@ -187,7 +201,7 @@ static int seader_print_struct_callback(const void* buffer, size_t size, void* a } void seader_send_payload( - SeaderUartBridge* seader_uart, + Seader* seader, Payload_t* payload, uint8_t to, uint8_t from, @@ -217,11 +231,11 @@ void seader_send_payload( rBuffer[1] = from; rBuffer[2] = replyTo; - seader_send_apdu(seader_uart, 0xA0, 0xDA, 0x02, 0x63, rBuffer, 6 + er.encoded); + seader_send_apdu(seader, 0xA0, 0xDA, 0x02, 0x63, rBuffer, 6 + er.encoded); } void seader_send_response( - SeaderUartBridge* seader_uart, + Seader* seader, Response_t* response, uint8_t to, uint8_t from, @@ -233,15 +247,12 @@ void seader_send_response( payload->present = Payload_PR_response; payload->choice.response = *response; - seader_send_payload(seader_uart, payload, to, from, replyTo); + seader_send_payload(seader, payload, to, from, replyTo); free(payload); } void seader_send_request_pacs(Seader* seader) { - SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; - RequestPacs_t* requestPacs = 0; requestPacs = calloc(1, sizeof *requestPacs); assert(requestPacs); @@ -264,7 +275,7 @@ void seader_send_request_pacs(Seader* seader) { payload->present = Payload_PR_samCommand; payload->choice.samCommand = *samCommand; - seader_send_payload(seader_uart, payload, 0x44, 0x0a, 0x44); + seader_send_payload(seader, payload, 0x44, 0x0a, 0x44); free(requestPacs); free(samCommand); @@ -272,9 +283,6 @@ void seader_send_request_pacs(Seader* seader) { } void seader_worker_send_serial_number(Seader* seader) { - SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; - SamCommand_t* samCommand = 0; samCommand = calloc(1, sizeof *samCommand); assert(samCommand); @@ -289,16 +297,13 @@ void seader_worker_send_serial_number(Seader* seader) { payload->present = Payload_PR_samCommand; payload->choice.samCommand = *samCommand; - seader_send_payload(seader_uart, payload, 0x44, 0x0a, 0x44); + seader_send_payload(seader, payload, 0x44, 0x0a, 0x44); free(samCommand); free(payload); } void seader_worker_send_version(Seader* seader) { - SeaderWorker* seader_worker = seader->worker; - - SeaderUartBridge* seader_uart = seader_worker->uart; SamCommand_t* samCommand = 0; samCommand = calloc(1, sizeof *samCommand); assert(samCommand); @@ -313,15 +318,13 @@ void seader_worker_send_version(Seader* seader) { payload->present = Payload_PR_samCommand; payload->choice.samCommand = *samCommand; - seader_send_payload(seader_uart, payload, 0x44, 0x0a, 0x44); + seader_send_payload(seader, payload, 0x44, 0x0a, 0x44); free(samCommand); free(payload); } void seader_send_card_detected(Seader* seader, CardDetails_t* cardDetails) { - SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; CardDetected_t* cardDetected = 0; cardDetected = calloc(1, sizeof *cardDetected); assert(cardDetected); @@ -343,7 +346,7 @@ void seader_send_card_detected(Seader* seader, CardDetails_t* cardDetails) { payload->present = Payload_PR_samCommand; payload->choice.samCommand = *samCommand; - seader_send_payload(seader_uart, payload, 0x44, 0x0a, 0x44); + seader_send_payload(seader, payload, 0x44, 0x0a, 0x44); free(payload); free(samCommand); @@ -573,7 +576,7 @@ bool seader_parse_response(Seader* seader, Response_t* response) { return false; } -void seader_send_nfc_rx(SeaderUartBridge* seader_uart, uint8_t* buffer, size_t len) { +void seader_send_nfc_rx(Seader* seader, uint8_t* buffer, size_t len) { OCTET_STRING_t rxData = {.buf = buffer, .size = len}; uint8_t status[] = {0x00, 0x00}; RfStatus_t rfStatus = {.buf = status, .size = 2}; @@ -599,7 +602,7 @@ void seader_send_nfc_rx(SeaderUartBridge* seader_uart, uint8_t* buffer, size_t l response->present = Response_PR_nfcResponse; response->choice.nfcResponse = *nfcResponse; - seader_send_response(seader_uart, response, 0x14, 0x0a, 0x0); + seader_send_response(seader, response, 0x14, 0x0a, 0x0); free(nfcRx); free(nfcResponse); @@ -642,12 +645,7 @@ void seader_iso15693_transmit( PicopassPoller* picopass_poller, uint8_t* buffer, size_t len) { - UNUSED(seader); - UNUSED(buffer); - UNUSED(len); - SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; BitBuffer* tx_buffer = bit_buffer_alloc(len); BitBuffer* rx_buffer = bit_buffer_alloc(SEADER_POLLER_MAX_BUFFER_SIZE); @@ -674,7 +672,7 @@ void seader_iso15693_transmit( seader_capture_sio(tx_buffer, rx_buffer, seader->credential); seader_send_nfc_rx( - seader_uart, + seader, (uint8_t*)bit_buffer_get_data(rx_buffer), bit_buffer_get_size_bytes(rx_buffer)); @@ -699,7 +697,6 @@ void seader_iso14443a_transmit( furi_assert(buffer); furi_assert(iso14443_4a_poller); SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; SeaderCredential* credential = seader->credential; BitBuffer* tx_buffer = bit_buffer_alloc(len); @@ -724,7 +721,7 @@ void seader_iso14443a_transmit( seader_capture_sio(tx_buffer, rx_buffer, credential); seader_send_nfc_rx( - seader_uart, + seader, (uint8_t*)bit_buffer_get_data(rx_buffer), bit_buffer_get_size_bytes(rx_buffer)); @@ -748,7 +745,6 @@ void seader_mfc_transmit( furi_assert(buffer); furi_assert(mfc_poller); SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; BitBuffer* tx_buffer = bit_buffer_alloc(len); BitBuffer* rx_buffer = bit_buffer_alloc(SEADER_POLLER_MAX_BUFFER_SIZE); @@ -879,7 +875,7 @@ void seader_mfc_transmit( } seader_send_nfc_rx( - seader_uart, + seader, (uint8_t*)bit_buffer_get_data(rx_buffer), bit_buffer_get_size_bytes(rx_buffer)); @@ -939,7 +935,7 @@ void seader_parse_nfc_command_transmit( } } -void seader_parse_nfc_off(SeaderUartBridge* seader_uart) { +void seader_parse_nfc_off(Seader* seader) { FURI_LOG_D(TAG, "Set Field Off"); NFCResponse_t* nfcResponse = 0; @@ -955,21 +951,19 @@ void seader_parse_nfc_off(SeaderUartBridge* seader_uart) { response->present = Response_PR_nfcResponse; response->choice.nfcResponse = *nfcResponse; - seader_send_response(seader_uart, response, 0x44, 0x0a, 0); + seader_send_response(seader, response, 0x44, 0x0a, 0); free(response); free(nfcResponse); } void seader_parse_nfc_command(Seader* seader, NFCCommand_t* nfcCommand, SeaderPollerContainer* spc) { - SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; switch(nfcCommand->present) { case NFCCommand_PR_nfcSend: seader_parse_nfc_command_transmit(seader, &nfcCommand->choice.nfcSend, spc); break; case NFCCommand_PR_nfcOff: - seader_parse_nfc_off(seader_uart); + seader_parse_nfc_off(seader); seader->worker->stage = SeaderPollerEventTypeComplete; break; default: