Skip to content

Commit

Permalink
update seader
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Mar 20, 2024
1 parent 9461329 commit 83d7bdd
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 48 deletions.
22 changes: 12 additions & 10 deletions non_catalog_apps/seader/application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions non_catalog_apps/seader/ccid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
78 changes: 47 additions & 31 deletions non_catalog_apps/seader/sam_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -195,14 +194,16 @@ 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(
&asn_DEF_Payload, payload, 1, seader_print_struct_callback, payloadDebug);
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,
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions non_catalog_apps/seader/seader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion non_catalog_apps/seader/seader_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <furi.h>
#include <furi_hal.h>

#define SEADER_UART_RX_BUF_SIZE (128)
#define SEADER_UART_RX_BUF_SIZE (256)

typedef struct {
uint8_t uart_ch;
Expand Down
1 change: 1 addition & 0 deletions non_catalog_apps/seader/seader_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions non_catalog_apps/seader/seader_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion non_catalog_apps/seader/seader_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 83d7bdd

Please sign in to comment.