From 0f321d631adb37c7cc6c6567f66eef8e2a795760 Mon Sep 17 00:00:00 2001 From: marshmellow42 Date: Tue, 11 Apr 2017 10:34:43 -0400 Subject: [PATCH] align clock grid with demods on graph --- CHANGELOG.md | 1 + client/cmddata.c | 44 ++++++++++++++++++++++++++++++++++++-------- client/cmddata.h | 4 +--- client/cmdlf.c | 4 ++-- client/cmdlft55xx.c | 8 ++++---- client/graph.c | 19 +++++++++++++------ client/graph.h | 3 ++- client/proxgui.h | 3 ++- client/proxguiqt.cpp | 2 -- client/ui.c | 4 ++++ client/ui.h | 5 ++++- common/lfdemod.c | 19 ++----------------- common/lfdemod.h | 6 ++---- 13 files changed, 73 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1552e171..37470077 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Added option c to 'hf list' (mark CRC bytes) (piwi) ### Changed +- Adjusted the lf demods to auto align and set the grid for the graph plot. - `lf snoop` now automatically gets samples from the device - `lf read` now accepts [#samples] as arg. && now automatically gets samples from the device - adjusted lf t5 chip timings to use WaitUS. and adjusted the readblock timings diff --git a/client/cmddata.c b/client/cmddata.c index 823da9ce..664ef850 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -28,8 +28,6 @@ uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN]; uint8_t g_debugMode=0; size_t DemodBufferLen=0; -//size_t g_demodStartIdx=0; -//uint8_t g_demodClock=0; static int CmdHelp(const char *Cmd); @@ -223,7 +221,7 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType, } bool st = false; size_t ststart = 0, stend = 0; - if (*stCheck) st = DetectST_ext(BitStream, &BitLen, &foundclk, &ststart, &stend); + if (*stCheck) st = DetectST(BitStream, &BitLen, &foundclk, &ststart, &stend); *stCheck = st; if (st) { clk = (clk == 0) ? foundclk : clk; @@ -236,7 +234,8 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType, //} //RepaintGraphWindow(); } - int errCnt = askdemod(BitStream, &BitLen, &clk, &invert, maxErr, askamp, askType); + int startIdx = 0; + int errCnt = askdemod_ext(BitStream, &BitLen, &clk, &invert, maxErr, askamp, askType, &startIdx); if (errCnt<0 || BitLen<16){ //if fatal error (or -1) if (g_debugMode) PrintAndLog("DEBUG: no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk); return 0; @@ -249,6 +248,8 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType, //output setDemodBuf(BitStream,BitLen,0); + setClockGrid(clk, startIdx); + if (verbose || g_debugMode){ if (errCnt>0) PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt); if (askType) PrintAndLog("ASK/Manchester - Clock: %d - Decoded bitstream:",clk); @@ -787,12 +788,15 @@ int FSKrawDemod(const char *Cmd, bool verbose) } //get bit clock length if (!rfLen) { - rfLen = detectFSKClk(BitStream, BitLen, fchigh, fclow); + int firstClockEdge = 0; //todo - align grid on graph with this... + rfLen = detectFSKClk(BitStream, BitLen, fchigh, fclow, &firstClockEdge); if (!rfLen) rfLen = 50; } - int size = fskdemod(BitStream, BitLen, rfLen, invert, fchigh, fclow); + int startIdx = 0; + int size = fskdemod_ext(BitStream, BitLen, rfLen, invert, fchigh, fclow, &startIdx); if (size > 0) { setDemodBuf(BitStream,size,0); + setClockGrid(rfLen, startIdx); // Now output the bitstream to the scrollback by line of 16 bits if (verbose || g_debugMode) { @@ -854,7 +858,8 @@ int PSKDemod(const char *Cmd, bool verbose) size_t BitLen = getFromGraphBuf(BitStream); if (BitLen==0) return 0; int errCnt=0; - errCnt = pskRawDemod(BitStream, &BitLen, &clk, &invert); + int startIdx = 0; + errCnt = pskRawDemod_ext(BitStream, &BitLen, &clk, &invert, &startIdx); if (errCnt > maxErr){ if (g_debugMode || verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); return 0; @@ -871,6 +876,8 @@ int PSKDemod(const char *Cmd, bool verbose) } //prime demod buffer for output setDemodBuf(BitStream,BitLen,0); + setClockGrid(clk, startIdx); + return 1; } @@ -909,6 +916,8 @@ int NRZrawDemod(const char *Cmd, bool verbose) if (verbose || g_debugMode) PrintAndLog("Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen); //prime demod buffer for output setDemodBuf(BitStream,BitLen,0); + setClockGrid(clk, clkStartIdx); + if (errCnt>0 && (verbose || g_debugMode)) PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt); if (verbose || g_debugMode) { @@ -1047,6 +1056,26 @@ int CmdRawDemod(const char *Cmd) return ans; } +void setClockGrid(int clk, int offset) { + if (offset > clk) offset %= clk; + if (offset < 0) offset += clk; + + if (offset > GraphTraceLen || offset < 0) return; + if (clk < 8 || clk > GraphTraceLen) { + GridLocked = false; + GridOffset = 0; + PlotGridX = 0; + PlotGridXdefault = 0; + RepaintGraphWindow(); + } else { + GridLocked = true; + GridOffset = offset; + PlotGridX = clk; + PlotGridXdefault = clk; + RepaintGraphWindow(); + } +} + int CmdGrid(const char *Cmd) { sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY); @@ -1561,7 +1590,6 @@ static command_t CommandTable[] = {"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"}, {"dec", CmdDec, 1, "Decimate samples"}, {"detectclock", CmdDetectClockRate, 1, "[modulation] Detect clock rate of wave in GraphBuffer (options: 'a','f','n','p' for ask, fsk, nrz, psk respectively)"}, - //{"fskfcdetect", CmdFSKfcDetect, 1, "Try to detect the Field Clock of an FSK wave"}, {"getbitstream", CmdGetBitStream, 1, "Convert GraphBuffer's >=1 values to 1 and <1 to 0"}, {"grid", CmdGrid, 1, " -- overlay grid on graph window, use zero value to turn off either"}, {"hexsamples", CmdHexsamples, 0, " [] -- Dump big buffer as hex bytes"}, diff --git a/client/cmddata.h b/client/cmddata.h index f9c45ae2..09467bd8 100644 --- a/client/cmddata.h +++ b/client/cmddata.h @@ -64,14 +64,12 @@ int FSKrawDemod(const char *Cmd, bool verbose); int PSKDemod(const char *Cmd, bool verbose); int NRZrawDemod(const char *Cmd, bool verbose); int getSamples(int n, bool silent); - +void setClockGrid(int clk, int offset); #define MAX_DEMOD_BUF_LEN (1024*128) extern uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN]; extern size_t DemodBufferLen; extern uint8_t g_debugMode; -//extern size_t g_demodStartIdx; -//extern uint8_t g_demodClock; #define BIGBUF_SIZE 40000 #endif diff --git a/client/cmdlf.c b/client/cmdlf.c index 7670136f..5825b339 100644 --- a/client/cmdlf.c +++ b/client/cmdlf.c @@ -548,10 +548,10 @@ int CmdLFfskSim(const char *Cmd) { return usage_lf_simfsk(); } - + int firstClockEdge = 0; if (dataLen == 0){ //using DemodBuffer if (clk==0 || fcHigh==0 || fcLow==0){ //manual settings must set them all - uint8_t ans = fskClocks(&fcHigh, &fcLow, &clk, 0); + uint8_t ans = fskClocks(&fcHigh, &fcLow, &clk, 0, &firstClockEdge); if (ans==0){ if (!fcHigh) fcHigh=10; if (!fcLow) fcLow=8; diff --git a/client/cmdlft55xx.c b/client/cmdlft55xx.c index 8ab1892d..ed8e3152 100644 --- a/client/cmdlft55xx.c +++ b/client/cmdlft55xx.c @@ -507,8 +507,8 @@ bool tryDetectModulation(){ t55xx_conf_block_t tests[15]; int bitRate=0; uint8_t fc1 = 0, fc2 = 0, ans = 0; - int clk=0; - ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false); + int clk = 0, firstClockEdge = 0; + ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge); if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) { if ( FSKrawDemod("0 0", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) { tests[hits].modulation = DEMOD_FSK; @@ -1555,7 +1555,7 @@ bool tryDetectP1(bool getData) { uint8_t preamble[] = {1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,1}; size_t startIdx = 0; uint8_t fc1 = 0, fc2 = 0, ans = 0; - int clk = 0; + int clk = 0, firstClockEdge = 0; bool st = true; if ( getData ) { @@ -1564,7 +1564,7 @@ bool tryDetectP1(bool getData) { } // try fsk clock detect. if successful it cannot be any other type of modulation... (in theory...) - ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false); + ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge); if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) { if ( FSKrawDemod("0 0", false) && preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && diff --git a/client/graph.c b/client/graph.c index 12f5ff71..a96425b9 100644 --- a/client/graph.c +++ b/client/graph.c @@ -144,11 +144,14 @@ int GetAskClock(const char str[], bool printAns, bool verbose) PrintAndLog("Failed to copy from graphbuffer"); return -1; } - bool st = DetectST(grph, &size, &clock); - int start = 0; + //, size_t *ststart, size_t *stend + size_t ststart = 0, stend = 0; + bool st = DetectST(grph, &size, &clock, &ststart, &stend); + int start = stend; if (st == false) { start = DetectASKClock(grph, size, &clock, 20); } + setClockGrid(clock, start); // Only print this message if we're not looping something if (printAns || g_debugMode) { PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start); @@ -197,6 +200,7 @@ int GetPskClock(const char str[], bool printAns, bool verbose) size_t firstPhaseShiftLoc = 0; uint8_t curPhase = 0, fc = 0; clock = DetectPSKClock(grph, size, 0, &firstPhaseShiftLoc, &curPhase, &fc); + setClockGrid(clock, firstPhaseShiftLoc); // Only print this message if we're not looping something if (printAns){ PrintAndLog("Auto-detected clock rate: %d", clock); @@ -223,6 +227,7 @@ uint8_t GetNrzClock(const char str[], bool printAns, bool verbose) } size_t clkStartIdx = 0; clock = DetectNRZClock(grph, size, 0, &clkStartIdx); + setClockGrid(clock, clkStartIdx); // Only print this message if we're not looping something if (printAns){ PrintAndLog("Auto-detected clock rate: %d", clock); @@ -241,10 +246,12 @@ uint8_t GetFskClock(const char str[], bool printAns, bool verbose) uint8_t fc1=0, fc2=0, rf1=0; - uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose); + int firstClockEdge = 0; + uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose, &firstClockEdge); if (ans == 0) return 0; if ((fc1==10 && fc2==8) || (fc1==8 && fc2==5)){ if (printAns) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1); + setClockGrid(rf1, firstClockEdge); return rf1; } if (verbose){ @@ -253,7 +260,7 @@ uint8_t GetFskClock(const char str[], bool printAns, bool verbose) } return 0; } -uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose) +uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose, int *firstClockEdge) { uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; size_t size = getFromGraphBuf(BitStream); @@ -265,8 +272,8 @@ uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose) } *fc1 = (ans >> 8) & 0xFF; *fc2 = ans & 0xFF; - - *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2); + //int firstClockEdge = 0; + *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2, firstClockEdge); if (*rf1==0) { if (verbose || g_debugMode) PrintAndLog("DEBUG: Clock detect error"); return 0; diff --git a/client/graph.h b/client/graph.h index 6f3f600d..8747bf28 100644 --- a/client/graph.h +++ b/client/graph.h @@ -21,7 +21,8 @@ int GetPskClock(const char str[], bool printAns, bool verbose); uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose); uint8_t GetNrzClock(const char str[], bool printAns, bool verbose); uint8_t GetFskClock(const char str[], bool printAns, bool verbose); -uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose); +uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose, int *firstClockEdge); +//uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose); bool graphJustNoise(int *BitStream, int size); void setGraphBuf(uint8_t *buff, size_t size); void save_restoreGB(uint8_t saveOpt); diff --git a/client/proxgui.h b/client/proxgui.h index ef9ac36a..ee0aa565 100644 --- a/client/proxgui.h +++ b/client/proxgui.h @@ -23,9 +23,10 @@ void ExitGraphics(void); extern int GraphBuffer[MAX_GRAPH_TRACE_LEN]; extern int GraphTraceLen; extern double CursorScaleFactor; -extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos; +extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos, GridOffset; extern int CommandFinished; extern int offline; +extern bool GridLocked; #ifdef __cplusplus } diff --git a/client/proxguiqt.cpp b/client/proxguiqt.cpp index cd8297ef..82f0e60f 100644 --- a/client/proxguiqt.cpp +++ b/client/proxguiqt.cpp @@ -22,8 +22,6 @@ #include "proxguiqt.h" #include "proxgui.h" -int GridOffset= 0; -bool GridLocked= 0; int startMax; int PageWidth; diff --git a/client/ui.c b/client/ui.c index 5902cb89..b438521e 100644 --- a/client/ui.c +++ b/client/ui.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -21,6 +22,9 @@ double CursorScaleFactor; int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0; int offline; int flushAfterWrite = 0; //buzzy +int GridOffset = 0; +bool GridLocked = 0; + extern pthread_mutex_t print_lock; static char *logfilename = "proxmark3.log"; diff --git a/client/ui.h b/client/ui.h index 4e03bfab..929d3921 100644 --- a/client/ui.h +++ b/client/ui.h @@ -11,6 +11,8 @@ #ifndef UI_H__ #define UI_H__ +#include + void ShowGui(void); void HideGraphWindow(void); void ShowGraphWindow(void); @@ -19,8 +21,9 @@ void PrintAndLog(char *fmt, ...); void SetLogFilename(char *fn); extern double CursorScaleFactor; -extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos; +extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos, GridOffset; extern int offline; extern int flushAfterWrite; //buzzy +extern bool GridLocked; #endif diff --git a/common/lfdemod.c b/common/lfdemod.c index f81ac236..d6ef88a4 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -833,15 +833,9 @@ int DetectPSKClock(uint8_t dest[], size_t size, int clock, size_t *firstPhaseShi return clk[best]; } -//int DetectPSKClock(uint8_t dest[], size_t size, int clock) { -// size_t firstPhaseShift = 0; -// uint8_t curPhase = 0; -// return DetectPSKClock_ext(dest, size, clock, &firstPhaseShift, &curPhase); -//} - //by marshmellow //detects the bit clock for FSK given the high and low Field Clocks -uint8_t detectFSKClk_ext(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge) { +uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge) { uint8_t clk[] = {8,16,32,40,50,64,100,128,0}; uint16_t rfLens[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; uint8_t rfCnts[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; @@ -947,11 +941,6 @@ uint8_t detectFSKClk_ext(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_ return clk[ii]; } -uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow) { - int firstClockEdge = 0; - return detectFSKClk_ext(BitStream, size, fcHigh, fcLow, &firstClockEdge); -} - //********************************************************************************************** //--------------------Modulation Demods &/or Decoding Section----------------------------------- //********************************************************************************************** @@ -977,7 +966,7 @@ bool findST(int *stStopLoc, int *stStartIdx, int lowToLowWaveLen[], int highToLo } //by marshmellow //attempt to identify a Sequence Terminator in ASK modulated raw wave -bool DetectST_ext(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststart, size_t *stend) { +bool DetectST(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststart, size_t *stend) { size_t bufsize = *size; //need to loop through all samples and identify our clock, look for the ST pattern int clk = 0; @@ -1098,10 +1087,6 @@ bool DetectST_ext(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststa *size = newloc; return true; } -bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) { - size_t ststart = 0, stend = 0; - return DetectST_ext(buffer, size, foundclock, &ststart, &stend); -} //by marshmellow //take 11 10 01 11 00 and make 01100 ... miller decoding diff --git a/common/lfdemod.h b/common/lfdemod.h index 697b78cb..9f37a969 100644 --- a/common/lfdemod.h +++ b/common/lfdemod.h @@ -27,13 +27,11 @@ extern uint32_t bytebits_to_byteLSBF(uint8_t* src, size_t numbits); extern uint16_t countFC(uint8_t *BitStream, size_t size, uint8_t fskAdj); extern int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr); extern uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, uint8_t high, uint8_t low); -extern uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow); -extern uint8_t detectFSKClk_ext(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge); +extern uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge); extern int DetectNRZClock(uint8_t dest[], size_t size, int clock, size_t *clockStartIdx); extern int DetectPSKClock(uint8_t dest[], size_t size, int clock, size_t *firstPhaseShift, uint8_t *curPhase, uint8_t *fc); extern int DetectStrongAskClock(uint8_t dest[], size_t size, int high, int low, int *clock); -extern bool DetectST(uint8_t buffer[], size_t *size, int *foundclock); -extern bool DetectST_ext(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststart, size_t *stend); +extern bool DetectST(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststart, size_t *stend); extern int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow); extern int fskdemod_ext(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx); extern int getHiLo(uint8_t *BitStream, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo); -- 2.39.5