From 83d7bdd993cb5f9238d1aa25dc8b5a0824945bb2 Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Wed, 20 Mar 2024 13:28:40 +0300 Subject: [PATCH] update seader --- non_catalog_apps/seader/application.fam | 22 +++---- non_catalog_apps/seader/ccid.c | 4 +- non_catalog_apps/seader/sam_api.c | 78 +++++++++++++++---------- non_catalog_apps/seader/seader.c | 1 + non_catalog_apps/seader/seader_bridge.h | 2 +- non_catalog_apps/seader/seader_i.h | 1 + non_catalog_apps/seader/seader_worker.c | 6 +- non_catalog_apps/seader/seader_worker.h | 2 +- 8 files changed, 68 insertions(+), 48 deletions(-) diff --git a/non_catalog_apps/seader/application.fam b/non_catalog_apps/seader/application.fam index f45b28f24e6..3b5c6bce07a 100644 --- a/non_catalog_apps/seader/application.fam +++ b/non_catalog_apps/seader/application.fam @@ -7,24 +7,26 @@ App( entry_point="seader_app", cdefines=["APP_SEADER"], requires=[ - "gui", "storage", "nfc", + "gui", + "storage", + "nfc", ], stack_size=7 * 1024, order=20, sources=[ - "*.c", - "aeabi_uldivmod.sx", + "*.c", + "aeabi_uldivmod.sx", ], fap_icon="icons/logo.png", fap_category="NFC", - fap_version="2.5", + fap_version="2.6", fap_author="bettse", -# fap_extbuild=( -# ExtFile( -# path="${FAP_SRC_DIR}/lib/asn1/asn_system.h", -# command="asn1c -D ${FAP_SRC_DIR}/lib/asn1 -no-gen-example -pdu=all ${FAP_SRC_DIR}/seader.asn1" -# ), -# ), + # fap_extbuild=( + # ExtFile( + # path="${FAP_SRC_DIR}/lib/asn1/asn_system.h", + # command="asn1c -D ${FAP_SRC_DIR}/lib/asn1 -no-gen-example -pdu=all ${FAP_SRC_DIR}/seader.asn1" + # ), + # ), fap_libs=["mbedtls"], fap_private_libs=[ Lib( diff --git a/non_catalog_apps/seader/ccid.c b/non_catalog_apps/seader/ccid.c index 4633efcbc1d..5d12895d255 100644 --- a/non_catalog_apps/seader/ccid.c +++ b/non_catalog_apps/seader/ccid.c @@ -309,7 +309,7 @@ size_t seader_ccid_process(Seader* seader, uint8_t* cmd, size_t cmd_len) { FURI_LOG_I(TAG, "SAM ATR!"); hasSAM = true; sam_slot = message.bSlot; - seader_worker_send_version(seader_worker); + seader_worker_send_version(seader); if(seader_worker->callback) { seader_worker->callback( SeaderWorkerEventSamPresent, seader_worker->context); @@ -318,7 +318,7 @@ size_t seader_ccid_process(Seader* seader, uint8_t* cmd, size_t cmd_len) { FURI_LOG_I(TAG, "SAM ATR2!"); hasSAM = true; sam_slot = message.bSlot; - seader_worker_send_version(seader_worker); + seader_worker_send_version(seader); if(seader_worker->callback) { seader_worker->callback( SeaderWorkerEventSamPresent, seader_worker->context); diff --git a/non_catalog_apps/seader/sam_api.c b/non_catalog_apps/seader/sam_api.c index 46adb4df948..67cd5bfe7b5 100644 --- a/non_catalog_apps/seader/sam_api.c +++ b/non_catalog_apps/seader/sam_api.c @@ -14,7 +14,6 @@ const uint8_t picopass_iclass_key[] = {0xaf, 0xa7, 0x85, 0xa7, 0xda, 0xb3, 0x33, static char display[SEADER_UART_RX_BUF_SIZE * 2 + 1] = {0}; char asn1_log[SEADER_UART_RX_BUF_SIZE] = {0}; -bool requestPacs = true; uint8_t read4Block6[] = {RFAL_PICOPASS_CMD_READ4, 0x06, 0x45, 0x56}; uint8_t read4Block9[] = {RFAL_PICOPASS_CMD_READ4, 0x09, 0xB2, 0xAE}; @@ -195,7 +194,7 @@ void seader_send_payload( #ifdef ASN1_DEBUG if(er.encoded > -1) { - char payloadDebug[384] = {0}; + char payloadDebug[1024] = {0}; memset(payloadDebug, 0, sizeof(payloadDebug)); (&asn_DEF_Payload) ->op->print_struct( @@ -203,6 +202,8 @@ void seader_send_payload( if(strlen(payloadDebug) > 0) { FURI_LOG_D(TAG, "Sending payload[%d %d %d]: %s", to, from, replyTo, payloadDebug); } + } else { + FURI_LOG_W(TAG, "Failed to print_struct payload"); } #endif //0xa0, 0xda, 0x02, 0x63, 0x00, 0x00, 0x0a, @@ -232,7 +233,10 @@ void seader_send_response( ASN_STRUCT_FREE(asn_DEF_Payload, payload); } -void sendRequestPacs(SeaderUartBridge* seader_uart) { +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); @@ -244,6 +248,8 @@ void sendRequestPacs(SeaderUartBridge* seader_uart) { assert(samCommand); samCommand->present = SamCommand_PR_requestPacs; + seader->samCommand = samCommand->present; + samCommand->choice.requestPacs = *requestPacs; Payload_t* payload = 0; @@ -260,13 +266,16 @@ void sendRequestPacs(SeaderUartBridge* seader_uart) { ASN_STRUCT_FREE(asn_DEF_Payload, payload); } -void seader_worker_send_serial_number(SeaderWorker* seader_worker) { +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); samCommand->present = SamCommand_PR_serialNumber; + seader->samCommand = samCommand->present; Payload_t* payload = 0; payload = calloc(1, sizeof *payload); @@ -281,13 +290,16 @@ void seader_worker_send_serial_number(SeaderWorker* seader_worker) { ASN_STRUCT_FREE(asn_DEF_Payload, payload); } -void seader_worker_send_version(SeaderWorker* seader_worker) { +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); samCommand->present = SamCommand_PR_version; + seader->samCommand = samCommand->present; Payload_t* payload = 0; payload = calloc(1, sizeof *payload); @@ -302,7 +314,9 @@ void seader_worker_send_version(SeaderWorker* seader_worker) { ASN_STRUCT_FREE(asn_DEF_Payload, payload); } -void seader_send_card_detected(SeaderUartBridge* seader_uart, CardDetails_t* cardDetails) { +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); @@ -314,6 +328,7 @@ void seader_send_card_detected(SeaderUartBridge* seader_uart, CardDetails_t* car assert(samCommand); samCommand->present = SamCommand_PR_cardDetected; + seader->samCommand = samCommand->present; samCommand->choice.cardDetected = *cardDetected; Payload_t* payload = 0; @@ -505,31 +520,37 @@ bool seader_parse_serial_number(Seader* seader, uint8_t* buf, size_t size) { bool seader_parse_sam_response(Seader* seader, SamResponse_t* samResponse) { SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; - if(samResponse->size == 0) { - if(requestPacs) { - FURI_LOG_D(TAG, "samResponse %d => requesting PACS", samResponse->size); - sendRequestPacs(seader_uart); - requestPacs = false; - } else { - FURI_LOG_D( - TAG, "samResponse %d, PACS already requested, pushing view", samResponse->size); - view_dispatcher_send_custom_event( - seader->view_dispatcher, SeaderCustomEventWorkerExit); - } - } else if(seader_parse_version(seader_worker, samResponse->buf, samResponse->size)) { - seader_worker_send_serial_number(seader_worker); - } else if(seader_parse_serial_number(seader, samResponse->buf, samResponse->size)) { - // no-op - } else if(seader_unpack_pacs(seader, samResponse->buf, samResponse->size)) { + switch(seader->samCommand) { + case SamCommand_PR_requestPacs: + FURI_LOG_I(TAG, "samResponse SamCommand_PR_requestPacs"); + seader_unpack_pacs(seader, samResponse->buf, samResponse->size); view_dispatcher_send_custom_event(seader->view_dispatcher, SeaderCustomEventPollerSuccess); - } else { + seader->samCommand = SamCommand_PR_NOTHING; + break; + case SamCommand_PR_version: + FURI_LOG_I(TAG, "samResponse SamCommand_PR_version"); + seader_parse_version(seader_worker, samResponse->buf, samResponse->size); + seader_worker_send_serial_number(seader); + break; + case SamCommand_PR_serialNumber: + FURI_LOG_I(TAG, "samResponse SamCommand_PR_serialNumber"); + seader_parse_serial_number(seader, samResponse->buf, samResponse->size); + seader->samCommand = SamCommand_PR_NOTHING; + break; + case SamCommand_PR_cardDetected: + FURI_LOG_I(TAG, "samResponse SamCommand_PR_cardDetected"); + seader_send_request_pacs(seader); + break; + case SamCommand_PR_NOTHING: + FURI_LOG_I(TAG, "samResponse SamCommand_PR_NOTHING"); memset(display, 0, sizeof(display)); for(uint8_t i = 0; i < samResponse->size; i++) { snprintf(display + (i * 2), sizeof(display), "%02x", samResponse->buf[i]); } - FURI_LOG_D(TAG, "Unknown samResponse %d: %s", samResponse->size, display); + FURI_LOG_I(TAG, "Unknown samResponse %d: %s", samResponse->size, display); + view_dispatcher_send_custom_event(seader->view_dispatcher, SeaderCustomEventWorkerExit); + break; } return false; @@ -873,11 +894,6 @@ NfcCommand seader_worker_card_detect( UNUSED(ats); UNUSED(ats_len); - // We're telling the SAM we've seen a new card, so reset out requestPacs check - requestPacs = true; - - SeaderWorker* seader_worker = seader->worker; - SeaderUartBridge* seader_uart = seader_worker->uart; SeaderCredential* credential = seader->credential; CardDetails_t* cardDetails = 0; @@ -910,7 +926,7 @@ NfcCommand seader_worker_card_detect( } } - seader_send_card_detected(seader_uart, cardDetails); + seader_send_card_detected(seader, cardDetails); ASN_STRUCT_FREE(asn_DEF_CardDetails, cardDetails); return NfcCommandContinue; diff --git a/non_catalog_apps/seader/seader.c b/non_catalog_apps/seader/seader.c index d9d880a9d2f..3d757bd7274 100644 --- a/non_catalog_apps/seader/seader.c +++ b/non_catalog_apps/seader/seader.c @@ -29,6 +29,7 @@ Seader* seader_alloc() { furi_hal_power_enable_otg(); } seader->is_debug_enabled = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug); + seader->samCommand = SamCommand_PR_NOTHING; seader->worker = seader_worker_alloc(); seader->view_dispatcher = view_dispatcher_alloc(); diff --git a/non_catalog_apps/seader/seader_bridge.h b/non_catalog_apps/seader/seader_bridge.h index c621deb25c6..06005766a2d 100644 --- a/non_catalog_apps/seader/seader_bridge.h +++ b/non_catalog_apps/seader/seader_bridge.h @@ -9,7 +9,7 @@ #include #include -#define SEADER_UART_RX_BUF_SIZE (128) +#define SEADER_UART_RX_BUF_SIZE (256) typedef struct { uint8_t uart_ch; diff --git a/non_catalog_apps/seader/seader_i.h b/non_catalog_apps/seader/seader_i.h index 5efc80a392a..bf8b76252a3 100644 --- a/non_catalog_apps/seader/seader_i.h +++ b/non_catalog_apps/seader/seader_i.h @@ -93,6 +93,7 @@ struct Seader { SceneManager* scene_manager; SeaderUartBridge* uart; SeaderCredential* credential; + SamCommand_PR samCommand; char text_store[SEADER_TEXT_STORE_SIZE + 1]; FuriString* text_box_store; diff --git a/non_catalog_apps/seader/seader_worker.c b/non_catalog_apps/seader/seader_worker.c index 12ede3d54a4..7630742cdcb 100644 --- a/non_catalog_apps/seader/seader_worker.c +++ b/non_catalog_apps/seader/seader_worker.c @@ -98,7 +98,7 @@ bool seader_process_success_response(Seader* seader, uint8_t* apdu, size_t len) if(seader_process_success_response_i(seader, apdu, len, false, NULL)) { // no-op, message was processed } else { - FURI_LOG_I(TAG, "Queue New SAM Message, %d bytes", len); + FURI_LOG_I(TAG, "Enqueue SAM message, %d bytes", len); uint32_t space = furi_message_queue_get_space(seader_worker->messages); if(space > 0) { SeaderAPDU seaderApdu = {}; @@ -166,7 +166,7 @@ void seader_worker_virtual_credential(Seader* seader) { if(furi_mutex_acquire(seader_worker->mq_mutex, 0) == FuriStatusOk) { uint32_t count = furi_message_queue_get_count(seader_worker->messages); if(count > 0) { - FURI_LOG_D(TAG, "MessageQueue: %ld messages", count); + FURI_LOG_I(TAG, "Dequeue SAM message [%ld messages]", count); SeaderAPDU seaderApdu = {}; FuriStatus status = @@ -225,7 +225,7 @@ void seader_worker_poller_conversation(Seader* seader, SeaderPollerContainer* sp furi_thread_set_current_priority(FuriThreadPriorityHighest); uint32_t count = furi_message_queue_get_count(seader_worker->messages); if(count > 0) { - FURI_LOG_D(TAG, "MessageQueue: %ld messages", count); + FURI_LOG_I(TAG, "Dequeue SAM message [%ld messages]", count); SeaderAPDU seaderApdu = {}; FuriStatus status = diff --git a/non_catalog_apps/seader/seader_worker.h b/non_catalog_apps/seader/seader_worker.h index 9868b20947a..0194a762f99 100644 --- a/non_catalog_apps/seader/seader_worker.h +++ b/non_catalog_apps/seader/seader_worker.h @@ -62,7 +62,7 @@ void seader_worker_start( void seader_worker_stop(SeaderWorker* seader_worker); bool seader_worker_process_sam_message(Seader* seader, CCID_Message* message); -void seader_worker_send_version(SeaderWorker* seader_worker); +void seader_worker_send_version(Seader* seader); NfcCommand seader_worker_poller_callback_iso14443_4a(NfcGenericEvent event, void* context);