]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmddata.c
rename askrawdemod to askmandemod
[proxmark3-svn] / client / cmddata.c
index 4cd06e86ffa90dacd9998ce66ae112d8f2f42316..be6e35d58f49b0afdc4408835df6ebc22afe3c05 100644 (file)
@@ -11,6 +11,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <inttypes.h>
+
 #include <limits.h>
 #include "proxmark3.h"
 #include "data.h"
@@ -183,7 +185,7 @@ int Em410xDecode(const char *Cmd)
   //  otherwise could be a void with no arguments
   //set defaults
   int high=0, low=0;
-  uint32_t hi=0, lo=0;
+  uint64_t lo=0; //hi=0,
 
   uint32_t i = 0;
   uint32_t initLoopMax = 1000;
@@ -219,8 +221,8 @@ restart:
         if (parityTest== ((parityTest>>1)<<1)){
           parityTest=0;
           for (ii=0; ii<4;++ii){
-            hi = (hi<<1)|(lo>>31);
-            lo=(lo<<1)|(GraphBuffer[(i*5)+ii+idx]);
+            //hi = (hi<<1)|(lo>>31);
+            lo=(lo<<1LL)|(GraphBuffer[(i*5)+ii+idx]);
           }
           //PrintAndLog("DEBUG: EM parity passed parity val: %d, i:%d, ii:%d,idx:%d, Buffer: %d%d%d%d%d,lo: %d",parityTest,i,ii,idx,GraphBuffer[idx+ii+(i*5)-5],GraphBuffer[idx+ii+(i*5)-4],GraphBuffer[idx+ii+(i*5)-3],GraphBuffer[idx+ii+(i*5)-2],GraphBuffer[idx+ii+(i*5)-1],lo);          
         }else {//parity failed
@@ -234,24 +236,27 @@ restart:
       }
       //skip last 5 bit parity test for simplicity.
 
-      //output em id
-      PrintAndLog("EM TAG ID    : %02x%08x", hi, lo);
       //get Unique ID
-      uint32_t iii=1;
-      uint32_t id2hi=0,id2lo=0;
-      for (i=0;i<8;i++){
-        id2hi=(id2hi<<1)|((hi & (iii<<(i)))>>i);
-      }
-      for (ii=4; ii>0;ii--){
+      uint64_t iii=1;
+      uint64_t id2lo=0; //id2hi=0,
+      //for (i=0;i<8;i++){ //for uint32 instead of uint64
+      //  id2hi=(id2hi<<1)|((hi & (iii<<(i)))>>i);
+     //}
+      for (ii=5; ii>0;ii--){
         for (i=0;i<8;i++){
-          id2lo=(id2lo<<1)|((lo & (iii<<(i+((ii-1)*8))))>>(i+((ii-1)*8)));
+          id2lo=(id2lo<<1LL)|((lo & (iii<<(i+((ii-1)*8))))>>(i+((ii-1)*8)));
         }
       }
-      PrintAndLog("Unique TAG ID: %02x%08x", id2hi, id2lo);
-      PrintAndLog("DEZ 8        : %08d",lo & 0xFFFFFF);
-      PrintAndLog("DEZ 10       : %010d",lo & 0xFFFFFF);
-      PrintAndLog("DEZ 5.5      : %05d.%05d",(lo>>16) & 0xFFFF,lo & 0xFFFF);
-      PrintAndLog("DEZ 3.5A     : %03d.%05d",hi,lo &0xFFFF);      
+      //output em id
+      PrintAndLog("EM TAG ID    : %010llx", lo);
+      PrintAndLog("Unique TAG ID: %010llx",  id2lo); //id2hi,
+      PrintAndLog("DEZ 8        : %08lld",lo & 0xFFFFFF);
+      PrintAndLog("DEZ 10       : %010lld",lo & 0xFFFFFF);
+      PrintAndLog("DEZ 5.5      : %05lld.%05lld",(lo>>16LL) & 0xFFFF,(lo & 0xFFFF));
+      PrintAndLog("DEZ 3.5A     : %03lld.%05lld",(lo>>32ll),(lo & 0xFFFF));
+      PrintAndLog("DEZ 14/IK2   : %014lld",lo);
+      PrintAndLog("DEZ 15/IK3   : %015lld",id2lo);
+      PrintAndLog("Other        : %05lld_%03lld_%08lld",(lo&0xFFFF),((lo>>16LL) & 0xFF),(lo & 0xFFFFFF));
       return 0;
     }else{
       idx++;
@@ -264,18 +269,17 @@ restart:
 //by marshmellow
 //takes 2 arguments - clock and invert both as integers 
 //prints binary found and saves in graphbuffer for further commands
-int Cmdaskrawdemod(const char *Cmd)
+int Cmdaskmandemod(const char *Cmd)
 {
   uint32_t i;
   int invert=0;  //invert default
   int high = 0, low = 0;
-  int clk=64; //clock default
+  int clk=DetectClock(0); //clock default
   uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
-  sscanf(Cmd, "%i %i", &clk, &invert);
-  if (!(clk>8)){
-    PrintAndLog("Invalid argument: %s",Cmd);
-    return 0;
-  }
+
+  sscanf(Cmd, "%i %i", &clk, &invert);    
+  if (clk<8) clk =64;
+  if (clk<32) clk=32;
   if (invert != 0 && invert != 1) {
     PrintAndLog("Invalid argument: %s", Cmd);
     return 0;
@@ -302,47 +306,47 @@ int Cmdaskrawdemod(const char *Cmd)
   //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
   int lastBit = 0;  //set first clock check
   uint32_t bitnum = 0;     //output counter
-  uint8_t tol = clk;  //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
-                      //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely 
+  uint8_t tol = 0;  //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
+  if (clk==32)tol=1;    //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely 
   uint32_t iii = 0;
   uint32_t gLen = GraphTraceLen;
   if (gLen > 500) gLen=500;
   uint8_t errCnt =0;
-
+  uint32_t bestStart = GraphTraceLen;
+  uint32_t bestErrCnt = (GraphTraceLen/1000);
   //PrintAndLog("DEBUG - lastbit - %d",lastBit);
-
   //loop to find first wave that works
   for (iii=0; iii < gLen; ++iii){
     if ((GraphBuffer[iii]>=high)||(GraphBuffer[iii]<=low)){
       lastBit=iii-clk;    
       //loop through to see if this start location works
       for (i = iii; i < GraphTraceLen; ++i) {   
-        if ((GraphBuffer[i] >= high) && ((i-lastBit)>(clk-((int)clk/tol)))) { // && GraphBuffer[i-1] < high   
+        if ((GraphBuffer[i] >= high) && ((i-lastBit)>(clk-tol))){
           lastBit+=clk;
           BitStream[bitnum] =  invert;
           bitnum++;
-        } else if ((GraphBuffer[i] <= low) && ((i-lastBit)>(clk-((int)clk/tol)))){
+        } else if ((GraphBuffer[i] <= low) && ((i-lastBit)>(clk-tol))){
           //low found and we are expecting a bar
           lastBit+=clk;
           BitStream[bitnum] = 1-invert; 
           bitnum++;
         } else {
           //mid value found or no bar supposed to be here
-          if ((i-lastBit)>(clk+((int)(clk/tol)))){
+          if ((i-lastBit)>(clk+tol)){
             //should have hit a high or low based on clock!!
 
-            /* 
+             
             //debug
-            PrintAndLog("DEBUG - no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(clk/tol)))),(lastBit+(clk+((int)(clk/tol)))),lastBit);
+            //PrintAndLog("DEBUG - no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(tol)))),(lastBit+(clk+((int)(tol)))),lastBit);
             if (bitnum > 0){
               BitStream[bitnum]=77;
               bitnum++;
             }
-            */
+            
 
             errCnt++;
             lastBit+=clk;//skip over until hit too many errors
-            if (errCnt>((GraphTraceLen/1000)*2)){  //allow 2 errors for every 1000 samples else start over
+            if (errCnt>((GraphTraceLen/1000))){  //allow 1 error for every 1000 samples else start over
               errCnt=0;
               bitnum=0;//start over
               break;
@@ -350,13 +354,24 @@ int Cmdaskrawdemod(const char *Cmd)
           }
         }
       }
-      
-      //debug
-      if ((bitnum>64) && (BitStream[bitnum-1]!=77)) break; 
-      
-    }    
+      //we got more than 64 good bits and not all errors
+      if ((bitnum > (64+errCnt)) && (errCnt<(GraphTraceLen/1000))) {
+        //possible good read
+        if (errCnt==0) break;  //great read - finish
+        if (bestStart == iii) break;  //if current run == bestErrCnt run (after exhausted testing) then finish 
+        if (errCnt<bestErrCnt){  //set this as new best run
+          bestErrCnt=errCnt;
+          bestStart = iii;
+        }
+      }
+    }
+    if (iii>=gLen){ //exhausted test
+      //if there was a ok test go back to that one and re-run the best run (then dump after that run)
+      if (bestErrCnt < (GraphTraceLen/1000)) iii=bestStart;
+    }
   }
   if (bitnum>16){
+    
     PrintAndLog("Data start pos:%d, lastBit:%d, stop pos:%d, numBits:%d",iii,lastBit,i,bitnum);
     //move BitStream back to GraphBuffer
     ClearGraph(0);
@@ -367,7 +382,7 @@ int Cmdaskrawdemod(const char *Cmd)
     RepaintGraphWindow();
     //output
     if (errCnt>0){
-      PrintAndLog("# Errors during Demoding: %d",errCnt);
+      PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
     }
     PrintAndLog("ASK decoded bitstream:");
     // Now output the bitstream to the scrollback by line of 16 bits
@@ -1529,7 +1544,7 @@ static command_t CommandTable[] =
   {"help",          CmdHelp,            1, "This help"},
   {"amp",           CmdAmp,             1, "Amplify peaks"},
   {"askdemod",      Cmdaskdemod,        1, "<0 or 1> -- Attempt to demodulate simple ASK tags"},
-  {"askrawdemod",   Cmdaskrawdemod,     1, "[clock] [invert<0 or 1>] -- Attempt to demodulate simple ASK tags and output binary (args optional-defaults='64 0')"},
+  {"askmandemod",   Cmdaskmandemod,     1, "[clock] [invert<0 or 1>] -- Attempt to demodulate ASK/Manchester tags and output binary (args optional[clock will try Auto-detect])"},
   {"autocorr",      CmdAutoCorr,        1, "<window length> -- Autocorrelation over window"},
   {"bitsamples",    CmdBitsamples,      0, "Get raw samples as bitstring"},
   {"bitstream",     CmdBitstream,       1, "[clock rate] -- Convert waveform into a bitstream"},
Impressum, Datenschutz