align clock grid with demods on graph

This commit is contained in:
marshmellow42 2017-04-11 10:34:43 -04:00
parent 68541dddc2
commit 0f321d631a
13 changed files with 73 additions and 49 deletions

View file

@ -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

View file

@ -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, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
{"hexsamples", CmdHexsamples, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},

View file

@ -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

View file

@ -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;

View file

@ -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) &&

View file

@ -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;

View file

@ -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);

View file

@ -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
}

View file

@ -22,8 +22,6 @@
#include "proxguiqt.h"
#include "proxgui.h"
int GridOffset= 0;
bool GridLocked= 0;
int startMax;
int PageWidth;

View file

@ -12,6 +12,7 @@
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <readline/readline.h>
#include <pthread.h>
@ -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";

View file

@ -11,6 +11,8 @@
#ifndef UI_H__
#define UI_H__
#include <stdbool.h>
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

View file

@ -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

View file

@ -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);