From 7ed7a9de40a4868e34c5702ebcef472f8ec6264d Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 14 May 2019 00:36:58 +0200 Subject: [PATCH] Rework hf tune to make it synchronous (needed for rdv4) --- armsrc/appmain.c | 47 ++++++++++++++++++++++++++--------------------- client/cmdhf.c | 43 ++++++++++++++++++++++++++++++++++++------- client/ui.c | 12 ++++++++---- client/ui.h | 2 +- include/pm3_cmd.h | 4 +++- 5 files changed, 74 insertions(+), 34 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 60f1631c3..6c820b4b3 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -55,6 +55,7 @@ uint8_t ToSend[TOSEND_BUFFER_SIZE]; int ToSendMax = -1; static int ToSendBit; struct common_area common_area __attribute__((section(".commonarea"))); +int button_status = BUTTON_NO_CLICK; void ToSendReset(void) { ToSendMax = -1; @@ -300,28 +301,17 @@ void MeasureAntennaTuning(void) { LEDsoff(); } -void MeasureAntennaTuningHf(void) { +uint16_t MeasureAntennaTuningHfData(void) { uint16_t volt = 0; // in mV - // Let the FPGA drive the high-frequency antenna around 13.56 MHz. - FpgaDownloadAndGo(FPGA_BITSTREAM_HF); - FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); - SpinDelay(50); volt = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10; bool use_high = (volt > MAX_ADC_HF_VOLTAGE - 300); - while (!BUTTON_PRESS()) { - SpinDelay(20); - if (!use_high) { - volt = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10; - } else { - volt = (MAX_ADC_HF_VOLTAGE_RDV40 * AvgAdc(ADC_CHAN_HF_RDV40)) >> 10; - } - DbprintfEx(FLAG_INPLACE, "%u mV / %5u V", volt, (uint16_t)(volt / 1000)); + if (!use_high) { + volt = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10; + } else { + volt = (MAX_ADC_HF_VOLTAGE_RDV40 * AvgAdc(ADC_CHAN_HF_RDV40)) >> 10; } - FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); - DbprintfEx(FLAG_NEWLINE, ""); - Dbprintf("[+] cancelled", 1); - reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_EOPABORTED, NULL, 0); + return volt; } void ReadMem(int addr) { @@ -1239,8 +1229,23 @@ static void PacketReceived(PacketCommandNG *packet) { MeasureAntennaTuning(); break; - case CMD_MEASURE_ANTENNA_TUNING_HF: - MeasureAntennaTuningHf(); + case CMD_MEASURE_ANTENNA_TUNING_HF_START: + // Let the FPGA drive the high-frequency antenna around 13.56 MHz. + FpgaDownloadAndGo(FPGA_BITSTREAM_HF); + FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); + reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF_START, PM3_SUCCESS, NULL, 0); + break; + + case CMD_MEASURE_ANTENNA_TUNING_HF_SAMPLE: + if (button_status == BUTTON_SINGLE_CLICK) + reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF_SAMPLE, PM3_EOPABORTED, NULL, 0); + uint16_t volt = MeasureAntennaTuningHfData(); + reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF_SAMPLE, PM3_SUCCESS, (uint8_t *)&volt, sizeof(volt)); + break; + + case CMD_MEASURE_ANTENNA_TUNING_HF_STOP: + FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF_STOP, PM3_SUCCESS, NULL, 0); break; case CMD_LISTEN_READER_FIELD: @@ -1646,8 +1651,8 @@ void __attribute__((noreturn)) AppMain(void) { } // Press button for one second to enter a possible standalone mode - if (BUTTON_HELD(1000) > 0) { - + button_status = BUTTON_HELD(1000); + if (button_status == BUTTON_HOLD) { /* * So this is the trigger to execute a standalone mod. Generic entrypoint by following the standalone/standalone.h headerfile * All standalone mod "main loop" should be the RunMod() function. diff --git a/client/cmdhf.c b/client/cmdhf.c index 65dbaaef1..b1bb6fa25 100644 --- a/client/cmdhf.c +++ b/client/cmdhf.c @@ -20,6 +20,7 @@ static int usage_hf_search() { PrintAndLogEx(NORMAL, ""); return 0; } + static int usage_hf_sniff() { PrintAndLogEx(NORMAL, "The high frequence sniffer will assign all available memory on device for sniffed data"); PrintAndLogEx(NORMAL, "Use " _YELLOW_("'data samples'")" command to download from device, and " _YELLOW_("'data plot'")" to look at it"); @@ -36,6 +37,16 @@ static int usage_hf_sniff() { return 0; } +static int usage_hf_tune() { + PrintAndLogEx(NORMAL, "Usage: hf tune []"); + PrintAndLogEx(NORMAL, "Continuously measure HF antenna tuning."); + PrintAndLogEx(NORMAL, "Press button to interrupt."); + PrintAndLogEx(NORMAL, "Options:"); + PrintAndLogEx(NORMAL, " - number of iterations (default: infinite)"); + PrintAndLogEx(NORMAL, ""); + return 0; +} + int CmdHFSearch(const char *Cmd) { char cmdp = tolower(param_getchar(Cmd, 0)); @@ -82,17 +93,35 @@ int CmdHFSearch(const char *Cmd) { } int CmdHFTune(const char *Cmd) { - (void)Cmd; // Cmd is not used so far - PrintAndLogEx(SUCCESS, "Measuring HF antenna, press button to exit"); - clearCommandBuffer(); - SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_HF, NULL, 0); + char cmdp = tolower(param_getchar(Cmd, 0)); + if (cmdp == 'h') return usage_hf_tune(); + int iter = param_get32ex(Cmd, 0, 0, 10); + PacketResponseNG resp; - if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_HF, &resp, 1000)) { + PrintAndLogEx(SUCCESS, "Measuring HF antenna, click button to exit"); + clearCommandBuffer(); + SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_HF_START, NULL, 0); + if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_HF_START, &resp, 1000)) { PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark HF measure, aborting"); return PM3_ETIMEOUT; } - if (resp.status != PM3_EOPABORTED) - return resp.status; + for (uint8_t i=0; iter == 0 || i< iter; i++) { // loop forever (till button pressed) if iter = 0 (default) + SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_HF_SAMPLE, NULL, 0); + if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_HF_SAMPLE, &resp, 1000)) { + PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark HF measure, aborting"); + return PM3_ETIMEOUT; + } + if (resp.status == PM3_EOPABORTED) + break; + uint16_t volt = resp.data.asDwords[0]; + PrintAndLogEx(INPLACE, "%u mV / %5u V", volt, (uint16_t)(volt / 1000)); + } + SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_HF_STOP, NULL, 0); + if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_HF_STOP, &resp, 1000)) { + PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark HF measure, aborting"); + return PM3_ETIMEOUT; + } + PrintAndLogEx(SUCCESS, "Done."); return PM3_SUCCESS; } diff --git a/client/ui.c b/client/ui.c index a2a364536..023f5b444 100644 --- a/client/ui.c +++ b/client/ui.c @@ -63,8 +63,6 @@ void PrintAndLogEx(logLevel_t level, const char *fmt, ...) { char buffer2[MAX_PRINT_BUFFER + 20] = {0}; char *token = NULL; char *tmp_ptr = NULL; - // {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG} - static const char *prefixes[7] = { "", "[+] ", "[=] ", "[-] ", "[!] ", "[!!] ", "[#] "}; FILE *stream = stdout; switch (level) { @@ -88,7 +86,8 @@ void PrintAndLogEx(logLevel_t level, const char *fmt, ...) { strncpy(prefix, _YELLOW_("[=]"), sizeof(prefix) - 1); break; case NORMAL: - strncpy(prefix, prefixes[level], sizeof(prefix) - 1); + case INPLACE: + // no prefixes for normal & inplace break; } @@ -97,11 +96,16 @@ void PrintAndLogEx(logLevel_t level, const char *fmt, ...) { vsnprintf(buffer, sizeof(buffer), fmt, args); va_end(args); - // no prefixes for normal + // no prefixes for normal & inplace if (level == NORMAL) { fPrintAndLog(stream, "%s", buffer); return; } + if (level == INPLACE) { + printf("%s\r", buffer); + fflush(stdout); + return; + } if (strchr(buffer, '\n')) { diff --git a/client/ui.h b/client/ui.h index 4f70b54cd..6e70b053d 100644 --- a/client/ui.h +++ b/client/ui.h @@ -38,7 +38,7 @@ extern session_arg_t session; #define M_PI 3.14159265358979323846264338327 #endif #define MAX_PRINT_BUFFER 2048 -typedef enum logLevel {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG} logLevel_t; +typedef enum logLevel {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG, INPLACE} logLevel_t; void ShowGui(void); void HideGraphWindow(void); diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index de66f312f..a725ccab9 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -335,7 +335,9 @@ typedef struct { // For measurements of the antenna tuning #define CMD_MEASURE_ANTENNA_TUNING 0x0400 -#define CMD_MEASURE_ANTENNA_TUNING_HF 0x0401 +#define CMD_MEASURE_ANTENNA_TUNING_HF_START 0x0401 +#define CMD_MEASURE_ANTENNA_TUNING_HF_SAMPLE 0x0402 +#define CMD_MEASURE_ANTENNA_TUNING_HF_STOP 0x0403 #define CMD_MEASURED_ANTENNA_TUNING 0x0410 #define CMD_LISTEN_READER_FIELD 0x0420