From: marshmellow42 <marshmellowrf@gmail.com>
Date: Mon, 26 Jan 2015 22:23:19 +0000 (-0500)
Subject: lf psk demods
X-Git-Tag: v2.0.0-rc1~43^2~1
X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/04d2721b3c7c4113e627d3f953835bde932065db

lf psk demods

clarify existing as psk1
added psk2 demod
---

diff --git a/client/cmddata.c b/client/cmddata.c
index 0a1841bf..0c6cd72c 100644
--- a/client/cmddata.c
+++ b/client/cmddata.c
@@ -721,8 +721,8 @@ int CmdFSKdemodHID(const char *Cmd)
   return 1;
 }
 
-//by marshmellow (based on existing demod + holiman's refactor)
-//Paradox Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
+//by marshmellow
+//Paradox Prox demod - FSK RF/50 with preamble of 00001111 (then manchester encoded)
 //print full Paradox Prox ID and some bit format details if found
 int CmdFSKdemodParadox(const char *Cmd)
 {
@@ -1196,7 +1196,7 @@ int CmdDetectNRZpskClockRate(const char *Cmd)
 	return 0;
 }
 
-int PSKnrzDemod(const char *Cmd)
+int PSKnrzDemod(const char *Cmd, uint8_t verbose)
 {
 	int invert=0;
 	int clk=0;
@@ -1213,7 +1213,7 @@ int PSKnrzDemod(const char *Cmd)
 		if (g_debugMode==1) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
 		return -1;
 	}
-	PrintAndLog("Tried PSK/NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
+  if (verbose) PrintAndLog("Tried PSK/NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
 
 	//prime demod buffer for output
 	setDemodBuf(BitStream,BitLen,0);
@@ -1226,9 +1226,9 @@ int CmdIndalaDecode(const char *Cmd)
 {
   int ans;
   if (strlen(Cmd)>0){
-    ans = PSKnrzDemod(Cmd);
+    ans = PSKnrzDemod(Cmd, 0);
   } else{ //default to RF/32
-    ans = PSKnrzDemod("32");
+    ans = PSKnrzDemod("32", 0);
   }
 
 	if (ans < 0){
@@ -1305,15 +1305,15 @@ int CmdPskClean(const char *Cmd)
 	return 0;
 }
 
-//by marshmellow
-//takes 2 arguments - clock and invert both as integers
-//attempts to demodulate ask only
-//prints binary found and saves in graphbuffer for further commands
+// by marshmellow
+// takes 2 arguments - clock and invert both as integers
+// attempts to demodulate psk only
+// prints binary found and saves in demodbuffer for further commands
 int CmdpskNRZrawDemod(const char *Cmd)
 {
   int errCnt;
  
-  errCnt = PSKnrzDemod(Cmd);
+  errCnt = PSKnrzDemod(Cmd, 1);
 	//output
 	if (errCnt<0){
     if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);  
@@ -1335,6 +1335,32 @@ int CmdpskNRZrawDemod(const char *Cmd)
   return 0;
 }
 
+// by marshmellow
+// takes same args as cmdpsknrzrawdemod
+int CmdPSK2rawDemod(const char *Cmd)
+{
+  int errCnt=0;
+  errCnt=PSKnrzDemod(Cmd, 1);
+  if (errCnt<0){
+    if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);  
+    return 0;
+  } 
+  psk1TOpsk2(DemodBuffer, DemodBufferLen);
+  if (errCnt>0){
+    if (g_debugMode){
+      PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+      PrintAndLog("PSK2 demoded bitstream:");
+      // Now output the bitstream to the scrollback by line of 16 bits
+      printDemodBuff();
+    }
+  }else{
+    PrintAndLog("PSK2 demoded bitstream:");
+    // Now output the bitstream to the scrollback by line of 16 bits
+    printDemodBuff();  
+  }
+  return 1;
+}
+
 int CmdGrid(const char *Cmd)
 {
   sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY);
@@ -1949,8 +1975,9 @@ static command_t CommandTable[] =
   {"plot",          CmdPlot,            1, "Show graph window (hit 'h' in window for keystroke help)"},
 	{"pskclean",      CmdPskClean,        1, "Attempt to clean psk wave"},
 	{"pskdetectclock",CmdDetectNRZpskClockRate, 1, "Detect ASK, PSK, or NRZ clock rate"},
-	{"pskindalademod",CmdIndalaDecode,    1, "[clock] [invert<0|1>] -- Attempt to demodulate psk indala tags and output ID binary & hex (args optional)"},
-	{"psknrzrawdemod",CmdpskNRZrawDemod,  1, "[clock] [invert<0|1>] -- Attempt to demodulate psk or nrz tags and output binary (args optional)"},
+	{"pskindalademod",CmdIndalaDecode,    1, "[clock] [invert<0|1>] -- Attempt to demodulate psk1 indala tags and output ID binary & hex (args optional)"},
+	{"psk1nrzrawdemod",CmdpskNRZrawDemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate psk1 or nrz tags and output binary (args optional)"},
+  {"psk2rawdemod",  CmdPSK2rawDemod,    1, "[clock] [invert<0|1>] -- Attempt to demodulate psk2 tags and output binary (args optional)"},
   {"samples",       CmdSamples,         0, "[512 - 40000] -- Get raw samples for graph window"},
   {"save",          CmdSave,            1, "<filename> -- Save trace (from graph window)"},
   {"scale",         CmdScale,           1, "<int> -- Set cursor display scale"},
diff --git a/common/lfdemod.c b/common/lfdemod.c
index 34194394..810e0357 100644
--- a/common/lfdemod.c
+++ b/common/lfdemod.c
@@ -816,7 +816,6 @@ int PyramiddemodFSK(uint8_t *dest, size_t size)
   return -4;
 }
 
-
 // by marshmellow
 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
 // maybe somehow adjust peak trimming value based on samples to fix?
@@ -885,7 +884,6 @@ int DetectASKClock(uint8_t dest[], size_t size, int clock)
   return clk[best];
 }
 
-
 //by marshmellow
 //detect psk clock by reading #peaks vs no peaks(or errors)
 int DetectpskNRZClock(uint8_t dest[], size_t size, int clock)
@@ -963,7 +961,7 @@ int DetectpskNRZClock(uint8_t dest[], size_t size, int clock)
 	return clk[best];
 }
 
-//by marshmellow (attempt to get rid of high immediately after a low)
+// by marshmellow (attempt to get rid of high immediately after a low)
 void pskCleanWave(uint8_t *BitStream, size_t size)
 {
 	int i;
@@ -999,9 +997,26 @@ void pskCleanWave(uint8_t *BitStream, size_t size)
 	return;
 }
 
+// by marshmellow
+// convert psk1 demod to psk2 demod
+// only transition waves are 1s
+void psk1TOpsk2(uint8_t *BitStream, size_t size)
+{
+	size_t i=1;
+	uint8_t lastBit=BitStream[0];
+	for (; i<size; i++){
+		if (lastBit!=BitStream[i]){
+			lastBit=BitStream[i];
+			BitStream[i]=1;
+		} else {
+			BitStream[i]=0;
+		}
+	}
+	return;
+}
 
-//redesigned by marshmellow adjusted from existing decode functions
-//indala id decoding - only tested on 26 bit tags, but attempted to make it work for more
+// redesigned by marshmellow adjusted from existing decode functions
+// indala id decoding - only tested on 26 bit tags, but attempted to make it work for more
 int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert)
 {
 	//26 bit 40134 format  (don't know other formats)
@@ -1064,9 +1079,8 @@ int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert)
 	return 1;
 }
 
-
-//by marshmellow - demodulate PSK1 wave or NRZ wave (both similar enough)
-//peaks switch bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
+// by marshmellow - demodulate PSK1 wave or NRZ wave (both similar enough)
+// peaks invert bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
 int pskNRZrawDemod(uint8_t *dest, size_t *size, int *clk, int *invert)
 {
 	pskCleanWave(dest,*size);
@@ -1087,7 +1101,6 @@ int pskNRZrawDemod(uint8_t *dest, size_t *size, int *clk, int *invert)
 	uint32_t bestStart = *size;
 	uint32_t maxErr = (*size/1000);
 	uint32_t bestErrCnt = maxErr;
-	//uint8_t midBit=0;
 	uint8_t curBit=0;
 	uint8_t bitHigh=0;
 	uint8_t ignorewin=*clk/8;
@@ -1198,7 +1211,6 @@ int pskNRZrawDemod(uint8_t *dest, size_t *size, int *clk, int *invert)
 	return errCnt;
 }
 
-
 //by marshmellow
 //detects the bit clock for FSK given the high and low Field Clocks
 uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow)
diff --git a/common/lfdemod.h b/common/lfdemod.h
index ca50d4f2..a2eb2c78 100644
--- a/common/lfdemod.h
+++ b/common/lfdemod.h
@@ -27,6 +27,7 @@ int IOdemodFSK(uint8_t *dest, size_t size);
 int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow);
 uint32_t bytebits_to_byte(uint8_t* src, size_t numbits);
 int pskNRZrawDemod(uint8_t *dest, size_t *size, int *clk, int *invert);
+void psk1TOpsk2(uint8_t *BitStream, size_t size);
 int DetectpskNRZClock(uint8_t dest[], size_t size, int clock);
 int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert);
 void pskCleanWave(uint8_t *bitStream, size_t size);