From 4da87d3f962eb6cb444ee402f685dcbb4950e5f5 Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Thu, 17 Oct 2019 11:46:59 +0200 Subject: [PATCH] Fix Client Response for RAW command. Client should receives now all response octects. --- armsrc/felica.c | 41 ++++++++++++++++++++++++----------------- armsrc/felica.h | 1 + client/cmdhffelica.c | 44 +++++++++++--------------------------------- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/armsrc/felica.c b/armsrc/felica.c index 0dfad3a61..26e45292c 100644 --- a/armsrc/felica.c +++ b/armsrc/felica.c @@ -417,6 +417,7 @@ bool WaitForFelicaReply(uint16_t maxbytes) { if (DBGLEVEL > 3) Dbprintf("timeout set: %i", timeout); //TODO FIX THIS METHOD - Race Condition or something: TIMING/MEMORY ISSUES + // If you add content here (dbprintf), timing problems appear?! Last Bytes (CRC) of frame will be cutoff. for (;;) { WDT_HIT(); if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { @@ -442,7 +443,7 @@ bool WaitForFelicaReply(uint16_t maxbytes) { } else if (c++ > timeout && FelicaFrame.state == STATE_UNSYNCD) { if (DBGLEVEL > 3) Dbprintf("Error: Timeout! STATE_UNSYNCD"); return false; - } // If you add content here, timing problems appear?! + } } } } @@ -488,6 +489,14 @@ static void iso18092_setup(uint8_t fpga_minor_mode) { LED_D_ON(); } + +void felica_reset_frame_mode(){ + switch_off(); + //Resetting Frame mode (First set in fpgaloader.c) + AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF | SSC_FRAME_MODE_WORDS_PER_TRANSFER(0); +} + + //----------------------------------------------------------------------------- // RAW FeliCa commands. Send out commands and store answers. //----------------------------------------------------------------------------- @@ -516,11 +525,12 @@ void felica_sendraw(PacketCommandNG *c) { // if failed selecting, turn off antenna and quite. if (!(param & FELICA_NO_SELECT)) { arg0 = felica_select_card(&card); - reply_old(CMD_ACK, arg0, sizeof(card.uid), 0, &card, sizeof(felica_card_select_t)); + reply_mix(CMD_ACK, arg0, sizeof(card.uid), 0, &card, sizeof(felica_card_select_t)); if (arg0 > 0){ Dbprintf("Error: Failed selecting card! "); + felica_reset_frame_mode(); + return; } - goto OUT; } }else{ if (DBGLEVEL > 3) Dbprintf("No card selection"); @@ -552,24 +562,21 @@ void felica_sendraw(PacketCommandNG *c) { TransmitFor18092_AsReader(buf, buf[2] + 4, NULL, 1, 0); arg0 = WaitForFelicaReply(1024); if (DBGLEVEL > 3) { - Dbprintf("Received Frame: %d", arg0); + Dbprintf("Received Frame Code: %d", arg0); Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes, 0); }; - reply_old(CMD_ACK, arg0, 0, 0, FelicaFrame.framebytes + 2, FelicaFrame.len - 2); - FelicaFrameReset(); + uint32_t result = reply_mix(CMD_ACK, FelicaFrame.len, arg0, 0, FelicaFrame.framebytes, FelicaFrame.len); + if(result){ + Dbprintf("Reply to Client Error Code: %i", result); + } } - - if ((param & FELICA_NO_DISCONNECT)) + if ((param & FELICA_NO_DISCONNECT)){ Dbprintf("Disconnect"); - return; - - OUT: - switch_off(); - - //Resetting Frame mode (First set in fpgaloader.c) - AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF | SSC_FRAME_MODE_WORDS_PER_TRANSFER(0); - - if (DBGLEVEL > 3) Dbprintf("FeliCa_sendraw Exit"); + } + if (DBGLEVEL > 3) + Dbprintf("FeliCa_sendraw Exit"); + felica_reset_frame_mode(); + return; } void felica_sniff(uint32_t samplesToSkip, uint32_t triggersToSkip) { diff --git a/armsrc/felica.h b/armsrc/felica.h index f160a0edf..faa52aa66 100644 --- a/armsrc/felica.h +++ b/armsrc/felica.h @@ -23,6 +23,7 @@ void felica_create_read_block_frame(uint16_t blockNr); void felica_create_authentication1_frame(); void felica_create_authentication2_frame(); void felica_send_request_service(uint8_t *request_service); +void felica_reset_frame_mode(); uint8_t * felica_create_request_service_frame(uint8_t nodeNumber, uint8_t *idm); #endif diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 8a8d08ec0..cb93d6269 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -440,17 +440,15 @@ static int CmdHFFelicaDumpLite(const char *Cmd) { static void waitCmdFelica(uint8_t iSelect) { PacketResponseNG resp; - if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { uint16_t len = iSelect ? (resp.oldarg[1] & 0xffff) : (resp.oldarg[0] & 0xffff); - PrintAndLogEx(NORMAL, "received %i octets", len); + PrintAndLogEx(NORMAL, "Client Received %i octets", len); if (!len) return; PrintAndLogEx(NORMAL, "%s", sprint_hex(resp.data.asBytes, len)); } else { - PrintAndLogEx(WARNING, "timeout while waiting for reply."); + PrintAndLogEx(WARNING, "Timeout while waiting for reply."); } - } static int CmdHFFelicaCmdRaw(const char *Cmd) { @@ -532,6 +530,7 @@ static int CmdHFFelicaCmdRaw(const char *Cmd) { if (crc && datalen > 0 && datalen < sizeof(data) - 2) { uint8_t b1, b2; compute_crc(CRC_FELICA, data, datalen, &b1, &b2); + // TODO FIND OUT IF FeliCa Light has another CRC order - Order changed for FeliCa Standard cards data[datalen++] = b2; data[datalen++] = b1; } @@ -555,14 +554,16 @@ static int CmdHFFelicaCmdRaw(const char *Cmd) { datalen = (datalen > PM3_CMD_DATA_SIZE) ? PM3_CMD_DATA_SIZE : datalen; clearCommandBuffer(); - SendCommandOLD(CMD_HF_FELICA_COMMAND, flags, (datalen & 0xFFFF) | (uint32_t)(numbits << 16), 0, data, datalen); + SendCommandMIX(CMD_HF_FELICA_COMMAND, flags, (datalen & 0xFFFF) | (uint32_t)(numbits << 16), 0, data, datalen); if (reply) { - if (active_select) + if (active_select){ PrintAndLogEx(NORMAL, "Active select wait for FeliCa."); waitCmdFelica(1); - if (datalen > 0) + } + if (datalen > 0){ waitCmdFelica(0); + } } return 0; } @@ -617,37 +618,14 @@ int readFelicaUid(bool verbose) { return status; } -// TODO FINISH THIS METHOD + int dump(const char *Cmd){ clearCommandBuffer(); char ctmp = tolower(param_getchar(Cmd, 0)); if (ctmp == 'h') return usage_hf_felica_dumplite(); + // TODO FINISH THIS METHOD + PrintAndLogEx(SUCCESS, "NOT IMPLEMENTED YET!"); - PrintAndLogEx(SUCCESS, "FeliCa - dump started"); - clearCommandBuffer(); - SendCommandNG(CMD_HF_FELICALITE_DUMP, NULL, 0); - PacketResponseNG resp; - - uint8_t timeout = 0; - while (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { - timeout++; - printf("."); - fflush(stdout); - if (kbd_enter_pressed()) { - PrintAndLogEx(WARNING, "\n[!] aborted via keyboard!\n"); - DropField(); - return 1; - } - if (timeout > 100) { - PrintAndLogEx(WARNING, "timeout while waiting for reply."); - DropField(); - return 1; - } - } - if (resp.oldarg[0] == 0) { - PrintAndLogEx(WARNING, "\nButton pressed. Aborted."); - return 1; - } return 0; }