1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Data and Graph commands
9 //-----------------------------------------------------------------------------
15 #include "proxmark3.h"
19 #include "cmdparser.h"
27 uint8_t DemodBuffer
[MAX_DEMOD_BUF_LEN
];
30 static int CmdHelp(const char *Cmd
);
32 //set the demod buffer with given array of binary (one bit per byte)
34 void setDemodBuf(uint8_t *buff
, size_t size
, size_t startIdx
)
39 if ( size
>= MAX_DEMOD_BUF_LEN
)
40 size
= MAX_DEMOD_BUF_LEN
;
43 for (; i
< size
; i
++){
44 DemodBuffer
[i
]=buff
[startIdx
++];
50 int CmdSetDebugMode(const char *Cmd
)
53 sscanf(Cmd
, "%i", &demod
);
54 g_debugMode
=(uint8_t)demod
;
59 void printDemodBuff(void)
62 int bitLen
= DemodBufferLen
;
64 PrintAndLog("no bits found in demod buffer");
67 if (bitLen
>512) bitLen
=512; //max output to 512 bits if we have more - should be plenty
69 // ensure equally divided by 16
72 for (i
= 0; i
<= (bitLen
-16); i
+=16) {
73 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
94 int CmdPrintDemodBuff(const char *Cmd
)
97 char printBuff
[512]={0x00};
98 uint8_t numBits
= DemodBufferLen
& 0xFFF0;
99 sscanf(Cmd
, "%c", &hex
);
101 PrintAndLog("Usage: data printdemodbuffer [x]");
102 PrintAndLog("Options: ");
103 PrintAndLog(" h This help");
104 PrintAndLog(" x output in hex (omit for binary output)");
108 numBits
= binarraytohex(printBuff
, (char *)DemodBuffer
, numBits
);
109 if (numBits
==0) return 0;
110 PrintAndLog("DemodBuffer: %s",printBuff
);
116 int CmdAmp(const char *Cmd
)
118 int i
, rising
, falling
;
119 int max
= INT_MIN
, min
= INT_MAX
;
121 for (i
= 10; i
< GraphTraceLen
; ++i
) {
122 if (GraphBuffer
[i
] > max
)
123 max
= GraphBuffer
[i
];
124 if (GraphBuffer
[i
] < min
)
125 min
= GraphBuffer
[i
];
130 for (i
= 0; i
< GraphTraceLen
; ++i
) {
131 if (GraphBuffer
[i
+ 1] < GraphBuffer
[i
]) {
133 GraphBuffer
[i
] = max
;
138 if (GraphBuffer
[i
+ 1] > GraphBuffer
[i
]) {
140 GraphBuffer
[i
] = min
;
147 RepaintGraphWindow();
152 * Generic command to demodulate ASK.
154 * Argument is convention: positive or negative (High mod means zero
155 * or high mod means one)
157 * Updates the Graph trace with 0/1 values
160 * c : 0 or 1 (or invert)
162 //this method ignores the clock
164 //this function strictly converts highs and lows to 1s and 0s for each sample in the graphbuffer
165 int Cmdaskdemod(const char *Cmd
)
168 int c
, high
= 0, low
= 0;
170 sscanf(Cmd
, "%i", &c
);
172 /* Detect high and lows */
173 for (i
= 0; i
< GraphTraceLen
; ++i
)
175 if (GraphBuffer
[i
] > high
)
176 high
= GraphBuffer
[i
];
177 else if (GraphBuffer
[i
] < low
)
178 low
= GraphBuffer
[i
];
182 if (c
!= 0 && c
!= 1) {
183 PrintAndLog("Invalid argument: %s", Cmd
);
187 if (GraphBuffer
[0] > 0) {
188 GraphBuffer
[0] = 1-c
;
192 for (i
= 1; i
< GraphTraceLen
; ++i
) {
193 /* Transitions are detected at each peak
194 * Transitions are either:
195 * - we're low: transition if we hit a high
196 * - we're high: transition if we hit a low
197 * (we need to do it this way because some tags keep high or
198 * low for long periods, others just reach the peak and go
201 //[marhsmellow] change == to >= for high and <= for low for fuzz
202 if ((GraphBuffer
[i
] >= high
) && (GraphBuffer
[i
- 1] == c
)) {
203 GraphBuffer
[i
] = 1 - c
;
204 } else if ((GraphBuffer
[i
] <= low
) && (GraphBuffer
[i
- 1] == (1 - c
))){
208 GraphBuffer
[i
] = GraphBuffer
[i
- 1];
211 RepaintGraphWindow();
215 //this function strictly converts >1 to 1 and <1 to 0 for each sample in the graphbuffer
216 int CmdGetBitStream(const char *Cmd
)
220 for (i
= 0; i
< GraphTraceLen
; i
++) {
221 if (GraphBuffer
[i
] >= 1) {
227 RepaintGraphWindow();
233 void printBitStream(uint8_t BitStream
[], uint32_t bitLen
)
237 PrintAndLog("Too few bits found: %d",bitLen
);
240 if (bitLen
>512) bitLen
=512;
242 // ensure equally divided by 16
246 for (i
= 0; i
<= (bitLen
-16); i
+=16) {
247 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
268 //print 64 bit EM410x ID in multiple formats
269 void printEM410x(uint32_t hi
, uint64_t id
)
276 for (ii
=5; ii
>0;ii
--){
278 id2lo
=(id2lo
<<1LL) | ((id
& (iii
<< (i
+((ii
-1)*8)))) >> (i
+((ii
-1)*8)));
282 //output 88 bit em id
283 PrintAndLog("EM TAG ID : %06x%016llx", hi
, id
);
285 //output 40 bit em id
286 PrintAndLog("EM TAG ID : %010llx", id
);
287 PrintAndLog("Unique TAG ID: %010llx", id2lo
);
288 PrintAndLog("DEZ 8 : %08lld",id
& 0xFFFFFF);
289 PrintAndLog("DEZ 10 : %010lld",id
& 0xFFFFFF);
290 PrintAndLog("DEZ 5.5 : %05lld.%05lld",(id
>>16LL) & 0xFFFF,(id
& 0xFFFF));
291 PrintAndLog("DEZ 3.5A : %03lld.%05lld",(id
>>32ll),(id
& 0xFFFF));
292 PrintAndLog("DEZ 14/IK2 : %014lld",id
);
293 PrintAndLog("DEZ 15/IK3 : %015lld",id2lo
);
294 PrintAndLog("Other : %05lld_%03lld_%08lld",(id
&0xFFFF),((id
>>16LL) & 0xFF),(id
& 0xFFFFFF));
301 //takes 3 arguments - clock, invert and maxErr as integers
302 //attempts to demodulate ask while decoding manchester
303 //prints binary found and saves in graphbuffer for further commands
304 int CmdAskEM410xDemod(const char *Cmd
)
306 char cmdp
= param_getchar(Cmd
, 0);
307 if (strlen(Cmd
) > 10 || cmdp
== 'h' || cmdp
== 'H') {
308 PrintAndLog("Usage: data askem410xdemod [clock] <0|1> [maxError]");
309 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
310 PrintAndLog(" <invert>, 1 for invert output");
311 PrintAndLog(" [set maximum allowed errors], default = 100.");
313 PrintAndLog(" sample: data askem410xdemod = demod an EM410x Tag ID from GraphBuffer");
314 PrintAndLog(" : data askem410xdemod 32 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32");
315 PrintAndLog(" : data askem410xdemod 32 1 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32 and inverting data");
316 PrintAndLog(" : data askem410xdemod 1 = demod an EM410x Tag ID from GraphBuffer while inverting data");
317 PrintAndLog(" : data askem410xdemod 64 1 0 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/64 and inverting data and allowing 0 demod errors");
320 int ans
= ASKmanDemod(Cmd
, FALSE
, FALSE
);
326 if (Em410xDecode(DemodBuffer
,(size_t *) &DemodBufferLen
, &idx
, &hi
, &lo
)){
328 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, DemodBufferLen
);
331 PrintAndLog("EM410x pattern found: ");
338 int ASKmanDemod(const char *Cmd
, bool verbose
, bool emSearch
)
344 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
345 sscanf(Cmd
, "%i %i %i", &clk
, &invert
, &maxErr
);
346 if (invert
!= 0 && invert
!= 1) {
347 PrintAndLog("Invalid argument: %s", Cmd
);
354 size_t BitLen
= getFromGraphBuf(BitStream
);
355 if (g_debugMode
==1) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen
);
356 if (BitLen
==0) return 0;
358 errCnt
= askmandemod(BitStream
, &BitLen
, &clk
, &invert
, maxErr
);
359 if (errCnt
<0||BitLen
<16){ //if fatal error (or -1)
360 if (g_debugMode
==1) PrintAndLog("no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt
,invert
,BitLen
,clk
);
363 if (verbose
|| g_debugMode
) PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk
,invert
,BitLen
);
367 if (verbose
|| g_debugMode
) PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt
);
369 if (verbose
|| g_debugMode
) PrintAndLog("ASK/Manchester decoded bitstream:");
370 // Now output the bitstream to the scrollback by line of 16 bits
371 setDemodBuf(BitStream
,BitLen
,0);
372 if (verbose
|| g_debugMode
) printDemodBuff();
377 if (Em410xDecode(BitStream
, &BitLen
, &idx
, &hi
, &lo
)){
378 //set GraphBuffer for clone or sim command
379 setDemodBuf(BitStream
, BitLen
, idx
);
381 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, BitLen
);
384 if (verbose
) PrintAndLog("EM410x pattern found: ");
385 if (verbose
) printEM410x(hi
, lo
);
393 //takes 3 arguments - clock, invert, maxErr as integers
394 //attempts to demodulate ask while decoding manchester
395 //prints binary found and saves in graphbuffer for further commands
396 int Cmdaskmandemod(const char *Cmd
)
398 char cmdp
= param_getchar(Cmd
, 0);
399 if (strlen(Cmd
) > 10 || cmdp
== 'h' || cmdp
== 'H') {
400 PrintAndLog("Usage: data rawdemod am [clock] <0|1> [maxError]");
401 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
402 PrintAndLog(" <invert>, 1 for invert output");
403 PrintAndLog(" [set maximum allowed errors], default = 100.");
405 PrintAndLog(" sample: data rawdemod am = demod an ask/manchester tag from GraphBuffer");
406 PrintAndLog(" : data rawdemod am 32 = demod an ask/manchester tag from GraphBuffer using a clock of RF/32");
407 PrintAndLog(" : data rawdemod am 32 1 = demod an ask/manchester tag from GraphBuffer using a clock of RF/32 and inverting data");
408 PrintAndLog(" : data rawdemod am 1 = demod an ask/manchester tag from GraphBuffer while inverting data");
409 PrintAndLog(" : data rawdemod am 64 1 0 = demod an ask/manchester tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors");
412 return ASKmanDemod(Cmd
, TRUE
, TRUE
);
417 //stricktly take 10 and 01 and convert to 0 and 1
418 int Cmdmandecoderaw(const char *Cmd
)
424 char cmdp
= param_getchar(Cmd
, 0);
425 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
426 PrintAndLog("Usage: data manrawdecode");
427 PrintAndLog(" Takes 10 and 01 and converts to 0 and 1 respectively");
428 PrintAndLog(" --must have binary sequence in demodbuffer (run data askrawdemod first)");
430 PrintAndLog(" sample: data manrawdecode = decode manchester bitstream from the demodbuffer");
433 if (DemodBufferLen
==0) return 0;
434 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
436 for (;i
<DemodBufferLen
;++i
){
437 if (DemodBuffer
[i
]>high
) high
=DemodBuffer
[i
];
438 else if(DemodBuffer
[i
]<low
) low
=DemodBuffer
[i
];
439 BitStream
[i
]=DemodBuffer
[i
];
441 if (high
>1 || low
<0 ){
442 PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
446 errCnt
=manrawdecode(BitStream
, &size
);
448 PrintAndLog("Too many errors: %d",errCnt
);
451 PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt
);
452 printBitStream(BitStream
, size
);
457 if (Em410xDecode(BitStream
, &size
, &idx
, &hi
, &id
)){
458 //need to adjust to set bitstream back to manchester encoded data
459 //setDemodBuf(BitStream, size, idx);
469 //take 01 or 10 = 0 and 11 or 00 = 1
470 //takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit
471 // and "invert" default = 0 if 1 it will invert output
472 // since it is not like manchester and doesn't have an incorrect bit pattern we
473 // cannot determine if our decode is correct or if it should be shifted by one bit
474 // the argument offset allows us to manually shift if the output is incorrect
475 // (better would be to demod and decode at the same time so we can distinguish large
476 // width waves vs small width waves to help the decode positioning) or askbiphdemod
477 int CmdBiphaseDecodeRaw(const char *Cmd
)
480 int offset
=0, invert
=0, maxErr
=20, errCnt
=0;
481 char cmdp
= param_getchar(Cmd
, 0);
482 if (strlen(Cmd
) > 3 || cmdp
== 'h' || cmdp
== 'H') {
483 PrintAndLog("Usage: data biphaserawdecode [offset] [invert] [maxErr]");
484 PrintAndLog(" Converts 10 or 01 to 1 and 11 or 00 to 0");
485 PrintAndLog(" --must have binary sequence in demodbuffer (run data askrawdemod first)");
487 PrintAndLog(" [offset <0|1>], set to 0 not to adjust start position or to 1 to adjust decode start position");
488 PrintAndLog(" [invert <0|1>], set to 1 to invert output");
489 PrintAndLog(" [maxErr int], set max errors tolerated - default=20");
491 PrintAndLog(" sample: data biphaserawdecode = decode biphase bitstream from the demodbuffer");
492 PrintAndLog(" sample: data biphaserawdecode 1 1 = decode biphase bitstream from the demodbuffer, set offset, and invert output");
495 sscanf(Cmd
, "%i %i %i", &offset
, &invert
, &maxErr
);
496 if (DemodBufferLen
==0){
497 PrintAndLog("DemodBuffer Empty - run 'data rawdemod ar' first");
500 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
501 memcpy(BitStream
, DemodBuffer
, DemodBufferLen
);
502 size
= DemodBufferLen
;
503 errCnt
=BiphaseRawDecode(BitStream
, &size
, offset
, invert
);
505 PrintAndLog("Error during decode:%d", errCnt
);
509 PrintAndLog("Too many errors attempting to decode: %d",errCnt
);
514 PrintAndLog("# Errors found during Demod (shown as 77 in bit stream): %d",errCnt
);
516 PrintAndLog("Biphase Decoded using offset: %d - # invert:%d - data:",offset
,invert
);
517 printBitStream(BitStream
, size
);
519 if (offset
) setDemodBuf(DemodBuffer
,DemodBufferLen
-offset
, offset
); //remove first bit from raw demod
523 // set demod buffer back to raw after biphase demod
524 void setBiphasetoRawDemodBuf(uint8_t *BitStream
, size_t size
)
526 uint8_t rawStream
[512]={0x00};
530 PrintAndLog("ERROR - Biphase Demod Buffer overrun");
533 for (size_t idx
=0; idx
<size
; idx
++){
535 rawStream
[i
++] = curPhase
;
536 rawStream
[i
++] = curPhase
;
539 rawStream
[i
++] = curPhase
;
540 rawStream
[i
++] = curPhase
^ 1;
543 setDemodBuf(rawStream
,i
,0);
548 //takes 4 arguments - clock, invert, maxErr as integers and amplify as char
549 //attempts to demodulate ask only
550 //prints binary found and saves in graphbuffer for further commands
551 int ASKrawDemod(const char *Cmd
, bool verbose
)
557 char amp
= param_getchar(Cmd
, 0);
558 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
559 sscanf(Cmd
, "%i %i %i %c", &clk
, &invert
, &maxErr
, &
);
560 if (invert
!= 0 && invert
!= 1) {
561 if (verbose
|| g_debugMode
) PrintAndLog("Invalid argument: %s", Cmd
);
568 if (amp
== 'a' || amp
== 'A') askAmp
=1;
569 size_t BitLen
= getFromGraphBuf(BitStream
);
570 if (BitLen
==0) return 0;
572 errCnt
= askrawdemod(BitStream
, &BitLen
, &clk
, &invert
, maxErr
, askAmp
);
573 if (errCnt
==-1||BitLen
<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
574 if (verbose
|| g_debugMode
) PrintAndLog("no data found");
575 if (g_debugMode
) PrintAndLog("errCnt: %d, BitLen: %d, clk: %d, invert: %d", errCnt
, BitLen
, clk
, invert
);
578 if (verbose
|| g_debugMode
) PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d", clk
, invert
, BitLen
);
580 //move BitStream back to DemodBuffer
581 setDemodBuf(BitStream
,BitLen
,0);
584 if (errCnt
>0 && (verbose
|| g_debugMode
)){
585 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d", errCnt
);
587 if (verbose
|| g_debugMode
){
588 PrintAndLog("ASK demoded bitstream:");
589 // Now output the bitstream to the scrollback by line of 16 bits
590 printBitStream(BitStream
,BitLen
);
596 // - ASK Demod then Biphase decode GraphBuffer samples
597 int ASKbiphaseDemod(const char *Cmd
, bool verbose
)
599 //ask raw demod GraphBuffer first
600 int offset
=0, clk
=0, invert
=0, maxErr
=0, ans
=0;
601 ans
= sscanf(Cmd
, "%i %i %i %i", &offset
, &clk
, &invert
, &maxErr
);
603 ans
= ASKrawDemod(Cmd
+2, FALSE
);
605 ans
= ASKrawDemod(Cmd
, FALSE
);
607 if (g_debugMode
|| verbose
) PrintAndLog("Error AskrawDemod: %d", ans
);
611 //attempt to Biphase decode DemodBuffer
612 size_t size
= DemodBufferLen
;
613 uint8_t BitStream
[MAX_DEMOD_BUF_LEN
];
614 memcpy(BitStream
, DemodBuffer
, DemodBufferLen
);
616 int errCnt
= BiphaseRawDecode(BitStream
, &size
, offset
, invert
);
618 if (g_debugMode
|| verbose
) PrintAndLog("Error BiphaseRawDecode: %d", errCnt
);
621 if (errCnt
> maxErr
) {
622 if (g_debugMode
|| verbose
) PrintAndLog("Error BiphaseRawDecode too many errors: %d", errCnt
);
625 //success set DemodBuffer and return
626 setDemodBuf(BitStream
, size
, 0);
627 if (g_debugMode
|| verbose
){
628 PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:",offset
,errCnt
);
633 //by marshmellow - see ASKbiphaseDemod
634 int Cmdaskbiphdemod(const char *Cmd
)
636 char cmdp
= param_getchar(Cmd
, 0);
637 if (strlen(Cmd
) > 12 || cmdp
== 'h' || cmdp
== 'H') {
638 PrintAndLog("Usage: data rawdemod ab [offset] [clock] <invert> [maxError] <amplify>");
639 PrintAndLog(" [offset], offset to begin biphase, default=0");
640 PrintAndLog(" [set clock as integer] optional, if not set, autodetect");
641 PrintAndLog(" <invert>, 1 to invert output");
642 PrintAndLog(" [set maximum allowed errors], default = 100");
643 PrintAndLog(" <amplify>, 'a' to attempt demod with ask amplification, default = no amp");
644 PrintAndLog(" NOTE: <invert> can be entered as second or third argument");
645 PrintAndLog(" NOTE: <amplify> can be entered as first, second or last argument");
646 PrintAndLog(" NOTE: any other arg must have previous args set to work");
648 PrintAndLog(" sample: data rawdemod ab = demod an ask/biph tag from GraphBuffer");
649 PrintAndLog(" : data rawdemod ab a = demod an ask/biph tag from GraphBuffer, amplified");
650 PrintAndLog(" : data rawdemod ab 1 32 = demod an ask/biph tag from GraphBuffer using an offset of 1 and a clock of RF/32");
651 PrintAndLog(" : data rawdemod ab 0 32 1 = demod an ask/biph tag from GraphBuffer using a clock of RF/32 and inverting data");
652 PrintAndLog(" : data rawdemod ab 0 1 = demod an ask/biph tag from GraphBuffer while inverting data");
653 PrintAndLog(" : data rawdemod ab 0 64 1 0 = demod an ask/biph tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors");
654 PrintAndLog(" : data rawdemod ab 0 64 1 0 a = demod an ask/biph tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors, and amp");
657 return ASKbiphaseDemod(Cmd
, TRUE
);
661 //attempts to demodulate and identify a G_Prox_II verex/chubb card
662 //WARNING: if it fails during some points it will destroy the DemodBuffer data
663 // but will leave the GraphBuffer intact.
664 //if successful it will push askraw data back to demod buffer ready for emulation
665 int CmdG_Prox_II_Demod(const char *Cmd
)
667 if (!ASKbiphaseDemod(Cmd
, FALSE
)){
668 if (g_debugMode
) PrintAndLog("ASKbiphaseDemod failed 1st try");
671 size_t size
= DemodBufferLen
;
672 //call lfdemod.c demod for gProxII
673 int ans
= gProxII_Demod(DemodBuffer
, &size
);
675 if (g_debugMode
) PrintAndLog("Error gProxII_Demod");
679 uint32_t ByteStream
[65] = {0x00};
684 size_t startIdx
= ans
+ 6; //start after preamble
685 for (size_t idx
= 0; idx
<size
-6; idx
++){
686 if ((idx
+1) % 5 == 0){
687 //spacer bit - should be 0
688 if (DemodBuffer
[startIdx
+idx
] != 0) {
689 if (g_debugMode
) PrintAndLog("Error spacer not 0: %d, pos: %d",DemodBuffer
[startIdx
+idx
],startIdx
+idx
);
694 if (keyCnt
<8){ //lsb first
695 xorKey
= xorKey
| (DemodBuffer
[startIdx
+idx
]<<keyCnt
);
697 if (keyCnt
==8 && g_debugMode
) PrintAndLog("xorKey Found: %02x", xorKey
);
701 ByteStream
[ByteCnt
] = ByteStream
[ByteCnt
] | (DemodBuffer
[startIdx
+idx
]<<bitCnt
);
703 if (bitCnt
% 8 == 0){
704 if (g_debugMode
) PrintAndLog("byte %d: %02x",ByteCnt
,ByteStream
[ByteCnt
]);
709 for (uint8_t i
= 0; i
< ByteCnt
; i
++){
710 ByteStream
[i
] ^= xorKey
; //xor
711 if (g_debugMode
) PrintAndLog("byte %d after xor: %02x", i
, ByteStream
[i
]);
713 //now ByteStream contains 64 bytes of decrypted raw tag data
715 uint8_t fmtLen
= ByteStream
[0]>>2;
718 uint32_t raw1
= bytebits_to_byte(DemodBuffer
+ans
,32);
719 uint32_t raw2
= bytebits_to_byte(DemodBuffer
+ans
+32, 32);
720 uint32_t raw3
= bytebits_to_byte(DemodBuffer
+ans
+64, 32);
723 FC
= ((ByteStream
[3] & 0x7F)<<7) | (ByteStream
[4]>>1);
724 Card
= ((ByteStream
[4]&1)<<19) | (ByteStream
[5]<<11) | (ByteStream
[6]<<3) | (ByteStream
[7]>>5);
725 PrintAndLog("G-Prox-II Found: FmtLen %d, FC %d, Card %d",fmtLen
,FC
,Card
);
726 } else if(fmtLen
==26){
727 FC
= ((ByteStream
[3] & 0x7F)<<1) | (ByteStream
[4]>>7);
728 Card
= ((ByteStream
[4]&0x7F)<<9) | (ByteStream
[5]<<1) | (ByteStream
[6]>>7);
729 PrintAndLog("G-Prox-II Found: FmtLen %d, FC %d, Card %d",fmtLen
,FC
,Card
);
731 PrintAndLog("Unknown G-Prox-II Fmt Found: FmtLen %d",fmtLen
);
733 PrintAndLog("Raw: %08x%08x%08x", raw1
,raw2
,raw3
);
734 setDemodBuf(DemodBuffer
+ans
, 96, 0);
738 //by marshmellow - see ASKrawDemod
739 int Cmdaskrawdemod(const char *Cmd
)
741 char cmdp
= param_getchar(Cmd
, 0);
742 if (strlen(Cmd
) > 12 || cmdp
== 'h' || cmdp
== 'H') {
743 PrintAndLog("Usage: data rawdemod ar [clock] <invert> [maxError] [amplify]");
744 PrintAndLog(" [set clock as integer] optional, if not set, autodetect");
745 PrintAndLog(" <invert>, 1 to invert output");
746 PrintAndLog(" [set maximum allowed errors], default = 100");
747 PrintAndLog(" <amplify>, 'a' to attempt demod with ask amplification, default = no amp");
749 PrintAndLog(" sample: data rawdemod ar = demod an ask tag from GraphBuffer");
750 PrintAndLog(" : data rawdemod ar a = demod an ask tag from GraphBuffer, amplified");
751 PrintAndLog(" : data rawdemod ar 32 = demod an ask tag from GraphBuffer using a clock of RF/32");
752 PrintAndLog(" : data rawdemod ar 32 1 = demod an ask tag from GraphBuffer using a clock of RF/32 and inverting data");
753 PrintAndLog(" : data rawdemod ar 1 = demod an ask tag from GraphBuffer while inverting data");
754 PrintAndLog(" : data rawdemod ar 64 1 0 = demod an ask tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors");
755 PrintAndLog(" : data rawdemod ar 64 1 0 a = demod an ask tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors, and amp");
758 return ASKrawDemod(Cmd
, TRUE
);
761 int AutoCorrelate(int window
, bool SaveGrph
, bool verbose
)
763 static int CorrelBuffer
[MAX_GRAPH_TRACE_LEN
];
764 size_t Correlation
= 0;
767 if (verbose
) PrintAndLog("performing %d correlations", GraphTraceLen
- window
);
768 for (int i
= 0; i
< GraphTraceLen
- window
; ++i
) {
770 for (int j
= 0; j
< window
; ++j
) {
771 sum
+= (GraphBuffer
[j
]*GraphBuffer
[i
+ j
]) / 256;
773 CorrelBuffer
[i
] = sum
;
774 if (sum
>= maxSum
-100 && sum
<= maxSum
+100){
776 Correlation
= i
-lastMax
;
778 if (sum
> maxSum
) maxSum
= sum
;
779 } else if (sum
> maxSum
){
785 //try again with wider margin
786 for (int i
= 0; i
< GraphTraceLen
- window
; i
++){
787 if (CorrelBuffer
[i
] >= maxSum
-(maxSum
*0.05) && CorrelBuffer
[i
] <= maxSum
+(maxSum
*0.05)){
789 Correlation
= i
-lastMax
;
791 //if (CorrelBuffer[i] > maxSum) maxSum = sum;
795 if (verbose
&& Correlation
> 0) PrintAndLog("Possible Correlation: %d samples",Correlation
);
798 GraphTraceLen
= GraphTraceLen
- window
;
799 memcpy(GraphBuffer
, CorrelBuffer
, GraphTraceLen
* sizeof (int));
800 RepaintGraphWindow();
805 int usage_data_autocorr(void)
808 PrintAndLog("Usage: data autocorr [window] [g]");
809 PrintAndLog("Options: ");
810 PrintAndLog(" h This help");
811 PrintAndLog(" [window] window length for correlation - default = 4000");
812 PrintAndLog(" g save back to GraphBuffer (overwrite)");
816 int CmdAutoCorr(const char *Cmd
)
818 char cmdp
= param_getchar(Cmd
, 0);
819 if (cmdp
== 'h' || cmdp
== 'H')
820 return usage_data_autocorr();
821 int window
= 4000; //set default
823 bool updateGrph
= FALSE
;
824 sscanf(Cmd
, "%i %c", &window
, &grph
);
826 if (window
>= GraphTraceLen
) {
827 PrintAndLog("window must be smaller than trace (%d samples)",
831 if (grph
== 'g') updateGrph
=TRUE
;
832 return AutoCorrelate(window
, updateGrph
, TRUE
);
835 int CmdBitsamples(const char *Cmd
)
840 GetFromBigBuf(got
,sizeof(got
),0);
841 WaitForResponse(CMD_ACK
,NULL
);
843 for (int j
= 0; j
< sizeof(got
); j
++) {
844 for (int k
= 0; k
< 8; k
++) {
845 if(got
[j
] & (1 << (7 - k
))) {
846 GraphBuffer
[cnt
++] = 1;
848 GraphBuffer
[cnt
++] = 0;
853 RepaintGraphWindow();
858 * Convert to a bitstream
860 int CmdBitstream(const char *Cmd
)
868 int hithigh
, hitlow
, first
;
870 /* Detect high and lows and clock */
871 for (i
= 0; i
< GraphTraceLen
; ++i
)
873 if (GraphBuffer
[i
] > high
)
874 high
= GraphBuffer
[i
];
875 else if (GraphBuffer
[i
] < low
)
876 low
= GraphBuffer
[i
];
880 clock
= GetAskClock(Cmd
, high
, 1);
884 for (i
= 0; i
< (int)(gtl
/ clock
); ++i
)
889 /* Find out if we hit both high and low peaks */
890 for (j
= 0; j
< clock
; ++j
)
892 if (GraphBuffer
[(i
* clock
) + j
] == high
)
894 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
896 /* it doesn't count if it's the first part of our read
897 because it's really just trailing from the last sequence */
898 if (first
&& (hithigh
|| hitlow
))
899 hithigh
= hitlow
= 0;
903 if (hithigh
&& hitlow
)
907 /* If we didn't hit both high and low peaks, we had a bit transition */
908 if (!hithigh
|| !hitlow
)
911 AppendGraph(0, clock
, bit
);
914 RepaintGraphWindow();
918 int CmdBuffClear(const char *Cmd
)
920 UsbCommand c
= {CMD_BUFF_CLEAR
};
926 int CmdDec(const char *Cmd
)
928 for (int i
= 0; i
< (GraphTraceLen
/ 2); ++i
)
929 GraphBuffer
[i
] = GraphBuffer
[i
* 2];
931 PrintAndLog("decimated by 2");
932 RepaintGraphWindow();
936 * Undecimate - I'd call it 'interpolate', but we'll save that
937 * name until someone does an actual interpolation command, not just
938 * blindly repeating samples
942 int CmdUndec(const char *Cmd
)
944 if(param_getchar(Cmd
, 0) == 'h')
946 PrintAndLog("Usage: data undec [factor]");
947 PrintAndLog("This function performs un-decimation, by repeating each sample N times");
948 PrintAndLog("Options: ");
949 PrintAndLog(" h This help");
950 PrintAndLog(" factor The number of times to repeat each sample.[default:2]");
951 PrintAndLog("Example: 'data undec 3'");
955 uint8_t factor
= param_get8ex(Cmd
, 0,2, 10);
956 //We have memory, don't we?
957 int swap
[MAX_GRAPH_TRACE_LEN
] = { 0 };
958 uint32_t g_index
= 0 ,s_index
= 0;
959 while(g_index
< GraphTraceLen
&& s_index
< MAX_GRAPH_TRACE_LEN
)
962 for(count
= 0; count
< factor
&& s_index
+count
< MAX_GRAPH_TRACE_LEN
; count
++)
963 swap
[s_index
+count
] = GraphBuffer
[g_index
];
967 memcpy(GraphBuffer
,swap
, s_index
* sizeof(int));
968 GraphTraceLen
= s_index
;
969 RepaintGraphWindow();
974 //shift graph zero up or down based on input + or -
975 int CmdGraphShiftZero(const char *Cmd
)
979 //set options from parameters entered with the command
980 sscanf(Cmd
, "%i", &shift
);
982 for(int i
= 0; i
<GraphTraceLen
; i
++){
983 shiftedVal
=GraphBuffer
[i
]+shift
;
986 else if (shiftedVal
<-127)
988 GraphBuffer
[i
]= shiftedVal
;
995 //use large jumps in read samples to identify edges of waves and then amplify that wave to max
996 //similar to dirtheshold, threshold, and askdemod commands
997 //takes a threshold length which is the measured length between two samples then determines an edge
998 int CmdAskEdgeDetect(const char *Cmd
)
1001 sscanf(Cmd
, "%i", &thresLen
);
1004 for(int i
= 1; i
<GraphTraceLen
; i
++){
1005 if (GraphBuffer
[i
]-GraphBuffer
[i
-1]>=thresLen
) //large jump up
1007 else if(GraphBuffer
[i
]-GraphBuffer
[i
-1]<=-1*thresLen
) //large jump down
1010 shiftedVal
=GraphBuffer
[i
]+shift
;
1014 else if (shiftedVal
<-127)
1016 GraphBuffer
[i
-1] = shiftedVal
;
1018 RepaintGraphWindow();
1023 /* Print our clock rate */
1024 // uses data from graphbuffer
1025 // adjusted to take char parameter for type of modulation to find the clock - by marshmellow.
1026 int CmdDetectClockRate(const char *Cmd
)
1028 char cmdp
= param_getchar(Cmd
, 0);
1029 if (strlen(Cmd
) > 3 || strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1030 PrintAndLog("Usage: data detectclock [modulation]");
1031 PrintAndLog(" [modulation as char], specify the modulation type you want to detect the clock of");
1032 PrintAndLog(" 'a' = ask, 'f' = fsk, 'n' = nrz/direct, 'p' = psk");
1034 PrintAndLog(" sample: data detectclock a = detect the clock of an ask modulated wave in the GraphBuffer");
1035 PrintAndLog(" data detectclock f = detect the clock of an fsk modulated wave in the GraphBuffer");
1036 PrintAndLog(" data detectclock p = detect the clock of an psk modulated wave in the GraphBuffer");
1037 PrintAndLog(" data detectclock n = detect the clock of an nrz/direct modulated wave in the GraphBuffer");
1041 ans
= GetAskClock("", true, false);
1042 } else if (cmdp
== 'f'){
1043 ans
= GetFskClock("", true, false);
1044 } else if (cmdp
== 'n'){
1045 ans
= GetNrzClock("", true, false);
1046 } else if (cmdp
== 'p'){
1047 ans
= GetPskClock("", true, false);
1049 PrintAndLog ("Please specify a valid modulation to detect the clock of - see option h for help");
1055 //fsk raw demod and print binary
1056 //takes 4 arguments - Clock, invert, fchigh, fclow
1057 //defaults: clock = 50, invert=1, fchigh=10, fclow=8 (RF/10 RF/8 (fsk2a))
1058 int FSKrawDemod(const char *Cmd
, bool verbose
)
1060 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1067 //set options from parameters entered with the command
1068 sscanf(Cmd
, "%i %i %i %i", &rfLen
, &invert
, &fchigh
, &fclow
);
1070 if (strlen(Cmd
)>0 && strlen(Cmd
)<=2) {
1072 invert
=1; //if invert option only is used
1077 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1078 size_t BitLen
= getFromGraphBuf(BitStream
);
1079 if (BitLen
==0) return 0;
1080 //get field clock lengths
1083 if (fchigh
==0 || fclow
== 0){
1084 fcs
= countFC(BitStream
, BitLen
, &dummy
);
1089 fchigh
= (fcs
>> 8) & 0xFF;
1093 //get bit clock length
1095 rfLen
= detectFSKClk(BitStream
, BitLen
, fchigh
, fclow
);
1096 if (rfLen
== 0) rfLen
= 50;
1098 if (verbose
) PrintAndLog("Args invert: %d - Clock:%d - fchigh:%d - fclow: %d",invert
,rfLen
,fchigh
, fclow
);
1099 int size
= fskdemod(BitStream
,BitLen
,(uint8_t)rfLen
,(uint8_t)invert
,(uint8_t)fchigh
,(uint8_t)fclow
);
1101 setDemodBuf(BitStream
,size
,0);
1103 // Now output the bitstream to the scrollback by line of 16 bits
1104 if(size
> (8*32)+2) size
= (8*32)+2; //only output a max of 8 blocks of 32 bits most tags will have full bit stream inside that sample size
1106 PrintAndLog("FSK decoded bitstream:");
1107 printBitStream(BitStream
,size
);
1112 if (verbose
) PrintAndLog("no FSK data found");
1118 //fsk raw demod and print binary
1119 //takes 4 arguments - Clock, invert, fchigh, fclow
1120 //defaults: clock = 50, invert=1, fchigh=10, fclow=8 (RF/10 RF/8 (fsk2a))
1121 int CmdFSKrawdemod(const char *Cmd
)
1123 char cmdp
= param_getchar(Cmd
, 0);
1124 if (strlen(Cmd
) > 10 || cmdp
== 'h' || cmdp
== 'H') {
1125 PrintAndLog("Usage: data rawdemod fs [clock] <invert> [fchigh] [fclow]");
1126 PrintAndLog(" [set clock as integer] optional, omit for autodetect.");
1127 PrintAndLog(" <invert>, 1 for invert output, can be used even if the clock is omitted");
1128 PrintAndLog(" [fchigh], larger field clock length, omit for autodetect");
1129 PrintAndLog(" [fclow], small field clock length, omit for autodetect");
1131 PrintAndLog(" sample: data rawdemod fs = demod an fsk tag from GraphBuffer using autodetect");
1132 PrintAndLog(" : data rawdemod fs 32 = demod an fsk tag from GraphBuffer using a clock of RF/32, autodetect fc");
1133 PrintAndLog(" : data rawdemod fs 1 = demod an fsk tag from GraphBuffer using autodetect, invert output");
1134 PrintAndLog(" : data rawdemod fs 32 1 = demod an fsk tag from GraphBuffer using a clock of RF/32, invert output, autodetect fc");
1135 PrintAndLog(" : data rawdemod fs 64 0 8 5 = demod an fsk1 RF/64 tag from GraphBuffer");
1136 PrintAndLog(" : data rawdemod fs 50 0 10 8 = demod an fsk2 RF/50 tag from GraphBuffer");
1137 PrintAndLog(" : data rawdemod fs 50 1 10 8 = demod an fsk2a RF/50 tag from GraphBuffer");
1140 return FSKrawDemod(Cmd
, TRUE
);
1143 //by marshmellow (based on existing demod + holiman's refactor)
1144 //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
1145 //print full HID Prox ID and some bit format details if found
1146 int CmdFSKdemodHID(const char *Cmd
)
1148 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1149 uint32_t hi2
=0, hi
=0, lo
=0;
1151 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1152 size_t BitLen
= getFromGraphBuf(BitStream
);
1153 if (BitLen
==0) return 0;
1154 //get binary from fsk wave
1155 int idx
= HIDdemodFSK(BitStream
,&BitLen
,&hi2
,&hi
,&lo
);
1159 PrintAndLog("DEBUG: Just Noise Detected");
1160 } else if (idx
== -2) {
1161 PrintAndLog("DEBUG: Error demoding fsk");
1162 } else if (idx
== -3) {
1163 PrintAndLog("DEBUG: Preamble not found");
1164 } else if (idx
== -4) {
1165 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen
);
1167 PrintAndLog("DEBUG: Error demoding fsk %d", idx
);
1172 if (hi2
==0 && hi
==0 && lo
==0) {
1173 if (g_debugMode
) PrintAndLog("DEBUG: Error - no values found");
1176 if (hi2
!= 0){ //extra large HID tags
1177 PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)",
1178 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
1180 else { //standard HID tags <38 bits
1183 uint32_t cardnum
= 0;
1184 if (((hi
>>5)&1)==1){//if bit 38 is set then < 37 bit format is used
1186 lo2
=(((hi
& 31) << 12) | (lo
>>20)); //get bits 21-37 to check for format len bit
1188 while(lo2
>1){ //find last bit set to 1 (format len bit)
1196 cardnum
= (lo
>>1)&0xFFFF;
1200 cardnum
= (lo
>>1)&0xFFFF;
1201 fc
= ((hi
&1)<<15)|(lo
>>17);
1204 cardnum
= (lo
>>1)&0xFFFFF;
1205 fc
= ((hi
&1)<<11)|(lo
>>21);
1208 else { //if bit 38 is not set then 37 bit format is used
1213 cardnum
= (lo
>>1)&0x7FFFF;
1214 fc
= ((hi
&0xF)<<12)|(lo
>>20);
1217 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d",
1218 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF,
1219 (unsigned int) fmtLen
, (unsigned int) fc
, (unsigned int) cardnum
);
1221 setDemodBuf(BitStream
,BitLen
,idx
);
1223 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, BitLen
);
1230 //Paradox Prox demod - FSK RF/50 with preamble of 00001111 (then manchester encoded)
1231 //print full Paradox Prox ID and some bit format details if found
1232 int CmdFSKdemodParadox(const char *Cmd
)
1234 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1235 uint32_t hi2
=0, hi
=0, lo
=0;
1237 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1238 size_t BitLen
= getFromGraphBuf(BitStream
);
1239 if (BitLen
==0) return 0;
1240 //get binary from fsk wave
1241 int idx
= ParadoxdemodFSK(BitStream
,&BitLen
,&hi2
,&hi
,&lo
);
1245 PrintAndLog("DEBUG: Just Noise Detected");
1246 } else if (idx
== -2) {
1247 PrintAndLog("DEBUG: Error demoding fsk");
1248 } else if (idx
== -3) {
1249 PrintAndLog("DEBUG: Preamble not found");
1250 } else if (idx
== -4) {
1251 PrintAndLog("DEBUG: Error in Manchester data");
1253 PrintAndLog("DEBUG: Error demoding fsk %d", idx
);
1258 if (hi2
==0 && hi
==0 && lo
==0){
1259 if (g_debugMode
) PrintAndLog("DEBUG: Error - no value found");
1262 uint32_t fc
= ((hi
& 0x3)<<6) | (lo
>>26);
1263 uint32_t cardnum
= (lo
>>10)&0xFFFF;
1264 uint32_t rawLo
= bytebits_to_byte(BitStream
+idx
+64,32);
1265 uint32_t rawHi
= bytebits_to_byte(BitStream
+idx
+32,32);
1266 uint32_t rawHi2
= bytebits_to_byte(BitStream
+idx
,32);
1268 PrintAndLog("Paradox TAG ID: %x%08x - FC: %d - Card: %d - Checksum: %02x - RAW: %08x%08x%08x",
1269 hi
>>10, (hi
& 0x3)<<26 | (lo
>>10), fc
, cardnum
, (lo
>>2) & 0xFF, rawHi2
, rawHi
, rawLo
);
1270 setDemodBuf(BitStream
,BitLen
,idx
);
1272 PrintAndLog("DEBUG: idx: %d, len: %d, Printing Demod Buffer:", idx
, BitLen
);
1279 //IO-Prox demod - FSK RF/64 with preamble of 000000001
1280 //print ioprox ID and some format details
1281 int CmdFSKdemodIO(const char *Cmd
)
1283 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1286 //something in graphbuffer?
1287 if (GraphTraceLen
< 65) {
1288 if (g_debugMode
)PrintAndLog("DEBUG: not enough samples in GraphBuffer");
1291 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1292 size_t BitLen
= getFromGraphBuf(BitStream
);
1293 if (BitLen
==0) return 0;
1295 //get binary from fsk wave
1296 idx
= IOdemodFSK(BitStream
,BitLen
);
1300 PrintAndLog("DEBUG: Just Noise Detected");
1301 } else if (idx
== -2) {
1302 PrintAndLog("DEBUG: not enough samples");
1303 } else if (idx
== -3) {
1304 PrintAndLog("DEBUG: error during fskdemod");
1305 } else if (idx
== -4) {
1306 PrintAndLog("DEBUG: Preamble not found");
1307 } else if (idx
== -5) {
1308 PrintAndLog("DEBUG: Separator bits not found");
1310 PrintAndLog("DEBUG: Error demoding fsk %d", idx
);
1316 if (g_debugMode
==1){
1317 PrintAndLog("DEBUG: IO Prox Data not found - FSK Bits: %d",BitLen
);
1318 if (BitLen
> 92) printBitStream(BitStream
,92);
1323 //0 10 20 30 40 50 60
1325 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
1326 //-----------------------------------------------------------------------------
1327 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
1329 //XSF(version)facility:codeone+codetwo (raw)
1331 if (idx
+64>BitLen
) {
1332 if (g_debugMode
==1) PrintAndLog("not enough bits found - bitlen: %d",BitLen
);
1335 PrintAndLog("%d%d%d%d%d%d%d%d %d",BitStream
[idx
], BitStream
[idx
+1], BitStream
[idx
+2], BitStream
[idx
+3], BitStream
[idx
+4], BitStream
[idx
+5], BitStream
[idx
+6], BitStream
[idx
+7], BitStream
[idx
+8]);
1336 PrintAndLog("%d%d%d%d%d%d%d%d %d",BitStream
[idx
+9], BitStream
[idx
+10], BitStream
[idx
+11],BitStream
[idx
+12],BitStream
[idx
+13],BitStream
[idx
+14],BitStream
[idx
+15],BitStream
[idx
+16],BitStream
[idx
+17]);
1337 PrintAndLog("%d%d%d%d%d%d%d%d %d facility",BitStream
[idx
+18], BitStream
[idx
+19], BitStream
[idx
+20],BitStream
[idx
+21],BitStream
[idx
+22],BitStream
[idx
+23],BitStream
[idx
+24],BitStream
[idx
+25],BitStream
[idx
+26]);
1338 PrintAndLog("%d%d%d%d%d%d%d%d %d version",BitStream
[idx
+27], BitStream
[idx
+28], BitStream
[idx
+29],BitStream
[idx
+30],BitStream
[idx
+31],BitStream
[idx
+32],BitStream
[idx
+33],BitStream
[idx
+34],BitStream
[idx
+35]);
1339 PrintAndLog("%d%d%d%d%d%d%d%d %d code1",BitStream
[idx
+36], BitStream
[idx
+37], BitStream
[idx
+38],BitStream
[idx
+39],BitStream
[idx
+40],BitStream
[idx
+41],BitStream
[idx
+42],BitStream
[idx
+43],BitStream
[idx
+44]);
1340 PrintAndLog("%d%d%d%d%d%d%d%d %d code2",BitStream
[idx
+45], BitStream
[idx
+46], BitStream
[idx
+47],BitStream
[idx
+48],BitStream
[idx
+49],BitStream
[idx
+50],BitStream
[idx
+51],BitStream
[idx
+52],BitStream
[idx
+53]);
1341 PrintAndLog("%d%d%d%d%d%d%d%d %d%d checksum",BitStream
[idx
+54],BitStream
[idx
+55],BitStream
[idx
+56],BitStream
[idx
+57],BitStream
[idx
+58],BitStream
[idx
+59],BitStream
[idx
+60],BitStream
[idx
+61],BitStream
[idx
+62],BitStream
[idx
+63]);
1343 uint32_t code
= bytebits_to_byte(BitStream
+idx
,32);
1344 uint32_t code2
= bytebits_to_byte(BitStream
+idx
+32,32);
1345 uint8_t version
= bytebits_to_byte(BitStream
+idx
+27,8); //14,4
1346 uint8_t facilitycode
= bytebits_to_byte(BitStream
+idx
+18,8) ;
1347 uint16_t number
= (bytebits_to_byte(BitStream
+idx
+36,8)<<8)|(bytebits_to_byte(BitStream
+idx
+45,8)); //36,9
1348 PrintAndLog("IO Prox XSF(%02d)%02x:%05d (%08x%08x)",version
,facilitycode
,number
,code
,code2
);
1349 setDemodBuf(BitStream
,64,idx
);
1351 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing demod buffer:",idx
,64);
1358 //AWID Prox demod - FSK RF/50 with preamble of 00000001 (always a 96 bit data stream)
1359 //print full AWID Prox ID and some bit format details if found
1360 int CmdFSKdemodAWID(const char *Cmd
)
1364 //sscanf(Cmd, "%i", &verbose);
1366 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1367 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1368 size_t size
= getFromGraphBuf(BitStream
);
1369 if (size
==0) return 0;
1371 //get binary from fsk wave
1372 int idx
= AWIDdemodFSK(BitStream
, &size
);
1374 if (g_debugMode
==1){
1376 PrintAndLog("DEBUG: Error - not enough samples");
1378 PrintAndLog("DEBUG: Error - only noise found");
1380 PrintAndLog("DEBUG: Error - problem during FSK demod");
1382 PrintAndLog("DEBUG: Error - AWID preamble not found");
1384 PrintAndLog("DEBUG: Error - Size not correct: %d", size
);
1386 PrintAndLog("DEBUG: Error %d",idx
);
1392 // 0 10 20 30 40 50 60
1394 // 01234567 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 - to 96
1395 // -----------------------------------------------------------------------------
1396 // 00000001 000 1 110 1 101 1 011 1 101 1 010 0 000 1 000 1 010 0 001 0 110 1 100 0 000 1 000 1
1397 // premable bbb o bbb o bbw o fff o fff o ffc o ccc o ccc o ccc o ccc o ccc o wxx o xxx o xxx o - to 96
1398 // |---26 bit---| |-----117----||-------------142-------------|
1399 // b = format bit len, o = odd parity of last 3 bits
1400 // f = facility code, c = card number
1401 // w = wiegand parity
1402 // (26 bit format shown)
1404 //get raw ID before removing parities
1405 uint32_t rawLo
= bytebits_to_byte(BitStream
+idx
+64,32);
1406 uint32_t rawHi
= bytebits_to_byte(BitStream
+idx
+32,32);
1407 uint32_t rawHi2
= bytebits_to_byte(BitStream
+idx
,32);
1408 setDemodBuf(BitStream
,96,idx
);
1410 size
= removeParity(BitStream
, idx
+8, 4, 1, 88);
1412 if (g_debugMode
==1) PrintAndLog("DEBUG: Error - at parity check-tag size does not match AWID format");
1415 // ok valid card found!
1418 // 0 10 20 30 40 50 60
1420 // 01234567 8 90123456 7890123456789012 3 456789012345678901234567890123456
1421 // -----------------------------------------------------------------------------
1422 // 00011010 1 01110101 0000000010001110 1 000000000000000000000000000000000
1423 // bbbbbbbb w ffffffff cccccccccccccccc w xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1424 // |26 bit| |-117--| |-----142------|
1425 // b = format bit len, o = odd parity of last 3 bits
1426 // f = facility code, c = card number
1427 // w = wiegand parity
1428 // (26 bit format shown)
1431 uint32_t cardnum
= 0;
1434 uint8_t fmtLen
= bytebits_to_byte(BitStream
,8);
1436 fc
= bytebits_to_byte(BitStream
+9, 8);
1437 cardnum
= bytebits_to_byte(BitStream
+17, 16);
1438 code1
= bytebits_to_byte(BitStream
+8,fmtLen
);
1439 PrintAndLog("AWID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %08x%08x%08x", fmtLen
, fc
, cardnum
, code1
, rawHi2
, rawHi
, rawLo
);
1441 cardnum
= bytebits_to_byte(BitStream
+8+(fmtLen
-17), 16);
1443 code1
= bytebits_to_byte(BitStream
+8,fmtLen
-32);
1444 code2
= bytebits_to_byte(BitStream
+8+(fmtLen
-32),32);
1445 PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x%08x, Raw: %08x%08x%08x", fmtLen
, cardnum
, code1
, code2
, rawHi2
, rawHi
, rawLo
);
1447 code1
= bytebits_to_byte(BitStream
+8,fmtLen
);
1448 PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x, Raw: %08x%08x%08x", fmtLen
, cardnum
, code1
, rawHi2
, rawHi
, rawLo
);
1452 PrintAndLog("DEBUG: idx: %d, Len: %d Printing Demod Buffer:", idx
, 96);
1455 //todo - convert hi2, hi, lo to demodbuffer for future sim/clone commands
1460 //Pyramid Prox demod - FSK RF/50 with preamble of 0000000000000001 (always a 128 bit data stream)
1461 //print full Farpointe Data/Pyramid Prox ID and some bit format details if found
1462 int CmdFSKdemodPyramid(const char *Cmd
)
1464 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1465 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1466 size_t size
= getFromGraphBuf(BitStream
);
1467 if (size
==0) return 0;
1469 //get binary from fsk wave
1470 int idx
= PyramiddemodFSK(BitStream
, &size
);
1472 if (g_debugMode
==1){
1474 PrintAndLog("DEBUG: Error - not enough samples");
1476 PrintAndLog("DEBUG: Error - only noise found");
1478 PrintAndLog("DEBUG: Error - problem during FSK demod");
1480 PrintAndLog("DEBUG: Error - Size not correct: %d", size
);
1482 PrintAndLog("DEBUG: Error - Pyramid preamble not found");
1484 PrintAndLog("DEBUG: Error - idx: %d",idx
);
1489 // 0 10 20 30 40 50 60
1491 // 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3
1492 // -----------------------------------------------------------------------------
1493 // 0000000 0 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1
1494 // premable xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o
1496 // 64 70 80 90 100 110 120
1498 // 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7
1499 // -----------------------------------------------------------------------------
1500 // 0000000 1 0000000 1 0000000 1 0110111 0 0011000 1 0000001 0 0001100 1 1001010 0
1501 // xxxxxxx o xxxxxxx o xxxxxxx o xswffff o ffffccc o ccccccc o ccccccw o ppppppp o
1502 // |---115---||---------71---------|
1503 // s = format start bit, o = odd parity of last 7 bits
1504 // f = facility code, c = card number
1505 // w = wiegand parity, x = extra space for other formats
1506 // p = unknown checksum
1507 // (26 bit format shown)
1509 //get bytes for checksum calc
1510 uint8_t checksum
= bytebits_to_byte(BitStream
+ idx
+ 120, 8);
1511 uint8_t csBuff
[14] = {0x00};
1512 for (uint8_t i
= 0; i
< 13; i
++){
1513 csBuff
[i
] = bytebits_to_byte(BitStream
+ idx
+ 16 + (i
*8), 8);
1515 //check checksum calc
1516 //checksum calc thanks to ICEMAN!!
1517 uint32_t checkCS
= CRC8Maxim(csBuff
,13);
1519 //get raw ID before removing parities
1520 uint32_t rawLo
= bytebits_to_byte(BitStream
+idx
+96,32);
1521 uint32_t rawHi
= bytebits_to_byte(BitStream
+idx
+64,32);
1522 uint32_t rawHi2
= bytebits_to_byte(BitStream
+idx
+32,32);
1523 uint32_t rawHi3
= bytebits_to_byte(BitStream
+idx
,32);
1524 setDemodBuf(BitStream
,128,idx
);
1526 size
= removeParity(BitStream
, idx
+8, 8, 1, 120);
1529 PrintAndLog("DEBUG: Error at parity check - tag size does not match Pyramid format, SIZE: %d, IDX: %d, hi3: %x",size
, idx
, rawHi3
);
1533 // ok valid card found!
1536 // 0 10 20 30 40 50 60 70
1538 // 01234567890123456789012345678901234567890123456789012345678901234567890
1539 // -----------------------------------------------------------------------
1540 // 00000000000000000000000000000000000000000000000000000000000000000000000
1541 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1545 // 1 2 34567890 1234567890123456 7 8901234
1546 // ---------------------------------------
1547 // 1 1 01110011 0000000001000110 0 1001010
1548 // s w ffffffff cccccccccccccccc w ppppppp
1549 // |--115-| |------71------|
1550 // s = format start bit, o = odd parity of last 7 bits
1551 // f = facility code, c = card number
1552 // w = wiegand parity, x = extra space for other formats
1553 // p = unknown checksum
1554 // (26 bit format shown)
1556 //find start bit to get fmtLen
1558 for (j
=0; j
<size
; j
++){
1559 if(BitStream
[j
]) break;
1561 uint8_t fmtLen
= size
-j
-8;
1563 uint32_t cardnum
= 0;
1565 //uint32_t code2 = 0;
1567 fc
= bytebits_to_byte(BitStream
+73, 8);
1568 cardnum
= bytebits_to_byte(BitStream
+81, 16);
1569 code1
= bytebits_to_byte(BitStream
+72,fmtLen
);
1570 PrintAndLog("Pyramid ID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %08x%08x%08x%08x", fmtLen
, fc
, cardnum
, code1
, rawHi3
, rawHi2
, rawHi
, rawLo
);
1571 } else if (fmtLen
==45){
1572 fmtLen
=42; //end = 10 bits not 7 like 26 bit fmt
1573 fc
= bytebits_to_byte(BitStream
+53, 10);
1574 cardnum
= bytebits_to_byte(BitStream
+63, 32);
1575 PrintAndLog("Pyramid ID Found - BitLength: %d, FC: %d, Card: %d - Raw: %08x%08x%08x%08x", fmtLen
, fc
, cardnum
, rawHi3
, rawHi2
, rawHi
, rawLo
);
1577 cardnum
= bytebits_to_byte(BitStream
+81, 16);
1579 //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen-32);
1580 //code2 = bytebits_to_byte(BitStream+(size-32),32);
1581 PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %08x%08x%08x%08x", fmtLen
, cardnum
, rawHi3
, rawHi2
, rawHi
, rawLo
);
1583 //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen);
1584 PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %08x%08x%08x%08x", fmtLen
, cardnum
, rawHi3
, rawHi2
, rawHi
, rawLo
);
1587 if (checksum
== checkCS
)
1588 PrintAndLog("Checksum %02x passed", checksum
);
1590 PrintAndLog("Checksum %02x failed - should have been %02x", checksum
, checkCS
);
1593 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, 128);
1599 int CmdFSKdemod(const char *Cmd
) //old CmdFSKdemod needs updating
1601 static const int LowTone
[] = {
1602 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1603 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1604 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1605 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1606 1, 1, 1, 1, 1, -1, -1, -1, -1, -1
1608 static const int HighTone
[] = {
1609 1, 1, 1, 1, 1, -1, -1, -1, -1,
1610 1, 1, 1, 1, -1, -1, -1, -1,
1611 1, 1, 1, 1, -1, -1, -1, -1,
1612 1, 1, 1, 1, -1, -1, -1, -1,
1613 1, 1, 1, 1, -1, -1, -1, -1,
1614 1, 1, 1, 1, -1, -1, -1, -1, -1,
1617 int lowLen
= sizeof (LowTone
) / sizeof (int);
1618 int highLen
= sizeof (HighTone
) / sizeof (int);
1619 int convLen
= (highLen
> lowLen
) ? highLen
: lowLen
;
1620 uint32_t hi
= 0, lo
= 0;
1623 int minMark
= 0, maxMark
= 0;
1625 for (i
= 0; i
< GraphTraceLen
- convLen
; ++i
) {
1626 int lowSum
= 0, highSum
= 0;
1628 for (j
= 0; j
< lowLen
; ++j
) {
1629 lowSum
+= LowTone
[j
]*GraphBuffer
[i
+j
];
1631 for (j
= 0; j
< highLen
; ++j
) {
1632 highSum
+= HighTone
[j
] * GraphBuffer
[i
+ j
];
1634 lowSum
= abs(100 * lowSum
/ lowLen
);
1635 highSum
= abs(100 * highSum
/ highLen
);
1636 GraphBuffer
[i
] = (highSum
<< 16) | lowSum
;
1639 for(i
= 0; i
< GraphTraceLen
- convLen
- 16; ++i
) {
1640 int lowTot
= 0, highTot
= 0;
1641 // 10 and 8 are f_s divided by f_l and f_h, rounded
1642 for (j
= 0; j
< 10; ++j
) {
1643 lowTot
+= (GraphBuffer
[i
+j
] & 0xffff);
1645 for (j
= 0; j
< 8; j
++) {
1646 highTot
+= (GraphBuffer
[i
+ j
] >> 16);
1648 GraphBuffer
[i
] = lowTot
- highTot
;
1649 if (GraphBuffer
[i
] > maxMark
) maxMark
= GraphBuffer
[i
];
1650 if (GraphBuffer
[i
] < minMark
) minMark
= GraphBuffer
[i
];
1653 GraphTraceLen
-= (convLen
+ 16);
1654 RepaintGraphWindow();
1656 // Find bit-sync (3 lo followed by 3 high) (HID ONLY)
1657 int max
= 0, maxPos
= 0;
1658 for (i
= 0; i
< 6000; ++i
) {
1660 for (j
= 0; j
< 3 * lowLen
; ++j
) {
1661 dec
-= GraphBuffer
[i
+ j
];
1663 for (; j
< 3 * (lowLen
+ highLen
); ++j
) {
1664 dec
+= GraphBuffer
[i
+ j
];
1672 // place start of bit sync marker in graph
1673 GraphBuffer
[maxPos
] = maxMark
;
1674 GraphBuffer
[maxPos
+ 1] = minMark
;
1678 // place end of bit sync marker in graph
1679 GraphBuffer
[maxPos
] = maxMark
;
1680 GraphBuffer
[maxPos
+1] = minMark
;
1682 PrintAndLog("actual data bits start at sample %d", maxPos
);
1683 PrintAndLog("length %d/%d", highLen
, lowLen
);
1685 uint8_t bits
[46] = {0x00};
1687 // find bit pairs and manchester decode them
1688 for (i
= 0; i
< arraylen(bits
) - 1; ++i
) {
1690 for (j
= 0; j
< lowLen
; ++j
) {
1691 dec
-= GraphBuffer
[maxPos
+ j
];
1693 for (; j
< lowLen
+ highLen
; ++j
) {
1694 dec
+= GraphBuffer
[maxPos
+ j
];
1697 // place inter bit marker in graph
1698 GraphBuffer
[maxPos
] = maxMark
;
1699 GraphBuffer
[maxPos
+ 1] = minMark
;
1701 // hi and lo form a 64 bit pair
1702 hi
= (hi
<< 1) | (lo
>> 31);
1704 // store decoded bit as binary (in hi/lo) and text (in bits[])
1712 PrintAndLog("bits: '%s'", bits
);
1713 PrintAndLog("hex: %08x %08x", hi
, lo
);
1718 //attempt to psk1 demod graph buffer
1719 int PSKDemod(const char *Cmd
, bool verbose
)
1724 sscanf(Cmd
, "%i %i %i", &clk
, &invert
, &maxErr
);
1729 if (invert
!= 0 && invert
!= 1) {
1730 if (verbose
) PrintAndLog("Invalid argument: %s", Cmd
);
1733 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1734 size_t BitLen
= getFromGraphBuf(BitStream
);
1735 if (BitLen
==0) return -1;
1736 uint8_t carrier
=countPSK_FC(BitStream
, BitLen
);
1737 if (carrier
!=2 && carrier
!=4 && carrier
!=8){
1742 errCnt
= pskRawDemod(BitStream
, &BitLen
, &clk
, &invert
);
1743 if (errCnt
> maxErr
){
1744 if (g_debugMode
==1 && verbose
) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk
,invert
,BitLen
,errCnt
);
1747 if (errCnt
<0|| BitLen
<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
1748 if (g_debugMode
==1 && verbose
) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk
,invert
,BitLen
,errCnt
);
1752 PrintAndLog("Tried PSK Demod using Clock: %d - invert: %d - Bits Found: %d",clk
,invert
,BitLen
);
1754 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt
);
1757 //prime demod buffer for output
1758 setDemodBuf(BitStream
,BitLen
,0);
1762 // Indala 26 bit decode
1764 // optional arguments - same as CmdpskNRZrawDemod (clock & invert)
1765 int CmdIndalaDecode(const char *Cmd
)
1769 ans
= PSKDemod(Cmd
, 0);
1770 } else{ //default to RF/32
1771 ans
= PSKDemod("32", 0);
1776 PrintAndLog("Error1: %d",ans
);
1780 ans
= indala26decode(DemodBuffer
,(size_t *) &DemodBufferLen
, &invert
);
1783 PrintAndLog("Error2: %d",ans
);
1786 char showbits
[251]={0x00};
1789 PrintAndLog("Had to invert bits");
1791 //convert UID to HEX
1792 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
1796 PrintAndLog("BitLen: %d",DemodBufferLen
);
1797 if (DemodBufferLen
==64){
1798 for( idx
=0; idx
<64; idx
++) {
1799 uid1
=(uid1
<<1)|(uid2
>>31);
1800 if (DemodBuffer
[idx
] == 0) {
1809 PrintAndLog("Indala UID=%s (%x%08x)", showbits
, uid1
, uid2
);
1817 for( idx
=0; idx
<DemodBufferLen
; idx
++) {
1818 uid1
=(uid1
<<1)|(uid2
>>31);
1819 uid2
=(uid2
<<1)|(uid3
>>31);
1820 uid3
=(uid3
<<1)|(uid4
>>31);
1821 uid4
=(uid4
<<1)|(uid5
>>31);
1822 uid5
=(uid5
<<1)|(uid6
>>31);
1823 uid6
=(uid6
<<1)|(uid7
>>31);
1824 if (DemodBuffer
[idx
] == 0) {
1834 PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
1837 PrintAndLog("DEBUG: printing demodbuffer:");
1844 // takes 3 arguments - clock, invert, maxErr as integers
1845 // attempts to demodulate nrz only
1846 // prints binary found and saves in demodbuffer for further commands
1848 int NRZrawDemod(const char *Cmd
, bool verbose
)
1853 sscanf(Cmd
, "%i %i %i", &clk
, &invert
, &maxErr
);
1858 if (invert
!= 0 && invert
!= 1) {
1859 PrintAndLog("Invalid argument: %s", Cmd
);
1862 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1863 size_t BitLen
= getFromGraphBuf(BitStream
);
1864 if (BitLen
==0) return 0;
1866 errCnt
= nrzRawDemod(BitStream
, &BitLen
, &clk
, &invert
, maxErr
);
1867 if (errCnt
> maxErr
){
1868 if (g_debugMode
==1 && verbose
) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk
,invert
,BitLen
,errCnt
);
1871 if (errCnt
<0|| BitLen
<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
1872 if (g_debugMode
==1 && verbose
) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk
,invert
,BitLen
,errCnt
);
1876 PrintAndLog("Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk
,invert
,BitLen
);
1877 //prime demod buffer for output
1878 setDemodBuf(BitStream
,BitLen
,0);
1880 if (errCnt
>0 && verbose
){
1881 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt
);
1884 PrintAndLog("NRZ demoded bitstream:");
1885 // Now output the bitstream to the scrollback by line of 16 bits
1891 int CmdNRZrawDemod(const char *Cmd
)
1893 char cmdp
= param_getchar(Cmd
, 0);
1894 if (strlen(Cmd
) > 10 || cmdp
== 'h' || cmdp
== 'H') {
1895 PrintAndLog("Usage: data rawdemod nr [clock] <0|1> [maxError]");
1896 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
1897 PrintAndLog(" <invert>, 1 for invert output");
1898 PrintAndLog(" [set maximum allowed errors], default = 100.");
1900 PrintAndLog(" sample: data rawdemod nr = demod a nrz/direct tag from GraphBuffer");
1901 PrintAndLog(" : data rawdemod nr 32 = demod a nrz/direct tag from GraphBuffer using a clock of RF/32");
1902 PrintAndLog(" : data rawdemod nr 32 1 = demod a nrz/direct tag from GraphBuffer using a clock of RF/32 and inverting data");
1903 PrintAndLog(" : data rawdemod nr 1 = demod a nrz/direct tag from GraphBuffer while inverting data");
1904 PrintAndLog(" : data rawdemod nr 64 1 0 = demod a nrz/direct tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors");
1907 return NRZrawDemod(Cmd
, TRUE
);
1911 // takes 3 arguments - clock, invert, maxErr as integers
1912 // attempts to demodulate psk only
1913 // prints binary found and saves in demodbuffer for further commands
1914 int CmdPSK1rawDemod(const char *Cmd
)
1917 char cmdp
= param_getchar(Cmd
, 0);
1918 if (strlen(Cmd
) > 10 || cmdp
== 'h' || cmdp
== 'H') {
1919 PrintAndLog("Usage: data rawdemod p1 [clock] <0|1> [maxError]");
1920 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
1921 PrintAndLog(" <invert>, 1 for invert output");
1922 PrintAndLog(" [set maximum allowed errors], default = 100.");
1924 PrintAndLog(" sample: data rawdemod p1 = demod a psk1 tag from GraphBuffer");
1925 PrintAndLog(" : data rawdemod p1 32 = demod a psk1 tag from GraphBuffer using a clock of RF/32");
1926 PrintAndLog(" : data rawdemod p1 32 1 = demod a psk1 tag from GraphBuffer using a clock of RF/32 and inverting data");
1927 PrintAndLog(" : data rawdemod p1 1 = demod a psk1 tag from GraphBuffer while inverting data");
1928 PrintAndLog(" : data rawdemod p1 64 1 0 = demod a psk1 tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors");
1931 ans
= PSKDemod(Cmd
, TRUE
);
1934 if (g_debugMode
) PrintAndLog("Error demoding: %d",ans
);
1938 PrintAndLog("PSK demoded bitstream:");
1939 // Now output the bitstream to the scrollback by line of 16 bits
1945 // takes same args as cmdpsk1rawdemod
1946 int CmdPSK2rawDemod(const char *Cmd
)
1949 char cmdp
= param_getchar(Cmd
, 0);
1950 if (strlen(Cmd
) > 10 || cmdp
== 'h' || cmdp
== 'H') {
1951 PrintAndLog("Usage: data rawdemod p2 [clock] <0|1> [maxError]");
1952 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
1953 PrintAndLog(" <invert>, 1 for invert output");
1954 PrintAndLog(" [set maximum allowed errors], default = 100.");
1956 PrintAndLog(" sample: data rawdemod p2 = demod a psk2 tag from GraphBuffer, autodetect clock");
1957 PrintAndLog(" : data rawdemod p2 32 = demod a psk2 tag from GraphBuffer using a clock of RF/32");
1958 PrintAndLog(" : data rawdemod p2 32 1 = demod a psk2 tag from GraphBuffer using a clock of RF/32 and inverting output");
1959 PrintAndLog(" : data rawdemod p2 1 = demod a psk2 tag from GraphBuffer, autodetect clock and invert output");
1960 PrintAndLog(" : data rawdemod p2 64 1 0 = demod a psk2 tag from GraphBuffer using a clock of RF/64, inverting output and allowing 0 demod errors");
1963 ans
=PSKDemod(Cmd
, TRUE
);
1965 if (g_debugMode
) PrintAndLog("Error demoding: %d",ans
);
1968 psk1TOpsk2(DemodBuffer
, DemodBufferLen
);
1969 PrintAndLog("PSK2 demoded bitstream:");
1970 // Now output the bitstream to the scrollback by line of 16 bits
1975 // by marshmellow - combines all raw demod functions into one menu command
1976 int CmdRawDemod(const char *Cmd
)
1978 char cmdp
= Cmd
[0]; //param_getchar(Cmd, 0);
1980 if (strlen(Cmd
) > 14 || cmdp
== 'h' || cmdp
== 'H' || strlen(Cmd
)<2) {
1981 PrintAndLog("Usage: data rawdemod [modulation] <help>|<options>");
1982 PrintAndLog(" [modulation] as 2 char, 'ab' for ask/biphase, 'am' for ask/manchester, 'ar' for ask/raw, 'fs' for fsk, ...");
1983 PrintAndLog(" 'nr' for nrz/direct, 'p1' for psk1, 'p2' for psk2");
1984 PrintAndLog(" <help> as 'h', prints the help for the specific modulation");
1985 PrintAndLog(" <options> see specific modulation help for optional parameters");
1987 PrintAndLog(" sample: data rawdemod fs h = print help for ask/raw demod");
1988 PrintAndLog(" : data rawdemod fs = demod GraphBuffer using: fsk - autodetect");
1989 PrintAndLog(" : data rawdemod ab = demod GraphBuffer using: ask/biphase - autodetect");
1990 PrintAndLog(" : data rawdemod am = demod GraphBuffer using: ask/manchester - autodetect");
1991 PrintAndLog(" : data rawdemod ar = demod GraphBuffer using: ask/raw - autodetect");
1992 PrintAndLog(" : data rawdemod nr = demod GraphBuffer using: nrz/direct - autodetect");
1993 PrintAndLog(" : data rawdemod p1 = demod GraphBuffer using: psk1 - autodetect");
1994 PrintAndLog(" : data rawdemod p2 = demod GraphBuffer using: psk2 - autodetect");
1997 char cmdp2
= Cmd
[1];
1999 if (cmdp
== 'f' && cmdp2
== 's'){
2000 ans
= CmdFSKrawdemod(Cmd
+3);
2001 } else if(cmdp
== 'a' && cmdp2
== 'b'){
2002 ans
= Cmdaskbiphdemod(Cmd
+3);
2003 } else if(cmdp
== 'a' && cmdp2
== 'm'){
2004 ans
= Cmdaskmandemod(Cmd
+3);
2005 } else if(cmdp
== 'a' && cmdp2
== 'r'){
2006 ans
= Cmdaskrawdemod(Cmd
+3);
2007 } else if(cmdp
== 'n' && cmdp2
== 'r'){
2008 ans
= CmdNRZrawDemod(Cmd
+3);
2009 } else if(cmdp
== 'p' && cmdp2
== '1'){
2010 ans
= CmdPSK1rawDemod(Cmd
+3);
2011 } else if(cmdp
== 'p' && cmdp2
== '2'){
2012 ans
= CmdPSK2rawDemod(Cmd
+3);
2014 PrintAndLog("unknown modulation entered - see help ('h') for parameter structure");
2019 int CmdGrid(const char *Cmd
)
2021 sscanf(Cmd
, "%i %i", &PlotGridX
, &PlotGridY
);
2022 PlotGridXdefault
= PlotGridX
;
2023 PlotGridYdefault
= PlotGridY
;
2024 RepaintGraphWindow();
2028 int CmdHexsamples(const char *Cmd
)
2033 char string_buf
[25];
2034 char* string_ptr
= string_buf
;
2035 uint8_t got
[BIGBUF_SIZE
];
2037 sscanf(Cmd
, "%i %i", &requested
, &offset
);
2039 /* if no args send something */
2040 if (requested
== 0) {
2043 if (offset
+ requested
> sizeof(got
)) {
2044 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > %d", BIGBUF_SIZE
);
2048 GetFromBigBuf(got
,requested
,offset
);
2049 WaitForResponse(CMD_ACK
,NULL
);
2052 for (j
= 0; j
< requested
; j
++) {
2054 string_ptr
+= sprintf(string_ptr
, "%02x ", got
[j
]);
2056 *(string_ptr
- 1) = '\0'; // remove the trailing space
2057 PrintAndLog("%s", string_buf
);
2058 string_buf
[0] = '\0';
2059 string_ptr
= string_buf
;
2062 if (j
== requested
- 1 && string_buf
[0] != '\0') { // print any remaining bytes
2063 *(string_ptr
- 1) = '\0';
2064 PrintAndLog("%s", string_buf
);
2065 string_buf
[0] = '\0';
2071 int CmdHide(const char *Cmd
)
2077 //zero mean GraphBuffer
2078 int CmdHpf(const char *Cmd
)
2083 for (i
= 10; i
< GraphTraceLen
; ++i
)
2084 accum
+= GraphBuffer
[i
];
2085 accum
/= (GraphTraceLen
- 10);
2086 for (i
= 0; i
< GraphTraceLen
; ++i
)
2087 GraphBuffer
[i
] -= accum
;
2089 RepaintGraphWindow();
2098 bool _headBit( BitstreamOut
*stream
)
2100 int bytepos
= stream
->position
>> 3; // divide by 8
2101 int bitpos
= (stream
->position
++) & 7; // mask out 00000111
2102 return (*(stream
->buffer
+ bytepos
) >> (7-bitpos
)) & 1;
2105 uint8_t getByte(uint8_t bits_per_sample
, BitstreamOut
* b
)
2109 for(i
=0 ; i
< bits_per_sample
; i
++)
2111 val
|= (_headBit(b
) << (7-i
));
2116 int CmdSamples(const char *Cmd
)
2118 //If we get all but the last byte in bigbuf,
2119 // we don't have to worry about remaining trash
2120 // in the last byte in case the bits-per-sample
2121 // does not line up on byte boundaries
2122 uint8_t got
[BIGBUF_SIZE
-1] = { 0 };
2124 int n
= strtol(Cmd
, NULL
, 0);
2128 if (n
> sizeof(got
))
2131 PrintAndLog("Reading %d bytes from device memory\n", n
);
2132 GetFromBigBuf(got
,n
,0);
2133 PrintAndLog("Data fetched");
2134 UsbCommand response
;
2135 WaitForResponse(CMD_ACK
, &response
);
2136 uint8_t bits_per_sample
= 8;
2138 //Old devices without this feature would send 0 at arg[0]
2139 if(response
.arg
[0] > 0)
2141 sample_config
*sc
= (sample_config
*) response
.d
.asBytes
;
2142 PrintAndLog("Samples @ %d bits/smpl, decimation 1:%d ", sc
->bits_per_sample
2144 bits_per_sample
= sc
->bits_per_sample
;
2146 if(bits_per_sample
< 8)
2148 PrintAndLog("Unpacking...");
2149 BitstreamOut bout
= { got
, bits_per_sample
* n
, 0};
2151 for (j
= 0; j
* bits_per_sample
< n
* 8 && j
< sizeof(GraphBuffer
); j
++) {
2152 uint8_t sample
= getByte(bits_per_sample
, &bout
);
2153 GraphBuffer
[j
] = ((int) sample
)- 128;
2156 PrintAndLog("Unpacked %d samples" , j
);
2159 for (int j
= 0; j
< n
; j
++) {
2160 GraphBuffer
[j
] = ((int)got
[j
]) - 128;
2165 RepaintGraphWindow();
2169 int CmdTuneSamples(const char *Cmd
)
2172 printf("\nMeasuring antenna characteristics, please wait...");
2174 UsbCommand c
= {CMD_MEASURE_ANTENNA_TUNING
};
2178 while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING
,&resp
,1000)) {
2182 PrintAndLog("\nNo response from Proxmark. Aborting...");
2188 int vLf125
, vLf134
, vHf
;
2189 vLf125
= resp
.arg
[0] & 0xffff;
2190 vLf134
= resp
.arg
[0] >> 16;
2191 vHf
= resp
.arg
[1] & 0xffff;;
2192 peakf
= resp
.arg
[2] & 0xffff;
2193 peakv
= resp
.arg
[2] >> 16;
2195 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125
/1000.0);
2196 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134
/1000.0);
2197 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv
/1000.0, 12000.0/(peakf
+1));
2198 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf
/1000.0);
2200 #define LF_UNUSABLE_V 2948 // was 2000. Changed due to bugfix in voltage measurements. LF results are now 47% higher.
2201 #define LF_MARGINAL_V 14739 // was 10000. Changed due to bugfix bug in voltage measurements. LF results are now 47% higher.
2202 #define HF_UNUSABLE_V 3167 // was 2000. Changed due to bugfix in voltage measurements. HF results are now 58% higher.
2203 #define HF_MARGINAL_V 7917 // was 5000. Changed due to bugfix in voltage measurements. HF results are now 58% higher.
2205 if (peakv
< LF_UNUSABLE_V
)
2206 PrintAndLog("# Your LF antenna is unusable.");
2207 else if (peakv
< LF_MARGINAL_V
)
2208 PrintAndLog("# Your LF antenna is marginal.");
2209 if (vHf
< HF_UNUSABLE_V
)
2210 PrintAndLog("# Your HF antenna is unusable.");
2211 else if (vHf
< HF_MARGINAL_V
)
2212 PrintAndLog("# Your HF antenna is marginal.");
2214 if (peakv
>= LF_UNUSABLE_V
) {
2215 for (int i
= 0; i
< 256; i
++) {
2216 GraphBuffer
[i
] = resp
.d
.asBytes
[i
] - 128;
2218 PrintAndLog("Displaying LF tuning graph. Divisor 89 is 134khz, 95 is 125khz.\n");
2220 GraphTraceLen
= 256;
2222 RepaintGraphWindow();
2229 int CmdLoad(const char *Cmd
)
2231 char filename
[FILE_PATH_SIZE
] = {0x00};
2235 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
2236 memcpy(filename
, Cmd
, len
);
2238 FILE *f
= fopen(filename
, "r");
2240 PrintAndLog("couldn't open '%s'", filename
);
2246 while (fgets(line
, sizeof (line
), f
)) {
2247 GraphBuffer
[GraphTraceLen
] = atoi(line
);
2251 PrintAndLog("loaded %d samples", GraphTraceLen
);
2252 RepaintGraphWindow();
2256 int CmdLtrim(const char *Cmd
)
2260 for (int i
= ds
; i
< GraphTraceLen
; ++i
)
2261 GraphBuffer
[i
-ds
] = GraphBuffer
[i
];
2262 GraphTraceLen
-= ds
;
2264 RepaintGraphWindow();
2268 // trim graph to input argument length
2269 int CmdRtrim(const char *Cmd
)
2275 RepaintGraphWindow();
2280 * Manchester demodulate a bitstream. The bitstream needs to be already in
2281 * the GraphBuffer as 0 and 1 values
2283 * Give the clock rate as argument in order to help the sync - the algorithm
2284 * resyncs at each pulse anyway.
2286 * Not optimized by any means, this is the 1st time I'm writing this type of
2287 * routine, feel free to improve...
2289 * 1st argument: clock rate (as number of samples per clock rate)
2290 * Typical values can be 64, 32, 128...
2292 int CmdManchesterDemod(const char *Cmd
)
2294 int i
, j
, invert
= 0;
2300 int hithigh
, hitlow
, first
;
2306 /* check if we're inverting output */
2309 PrintAndLog("Inverting output");
2314 while(*Cmd
== ' '); // in case a 2nd argument was given
2317 /* Holds the decoded bitstream: each clock period contains 2 bits */
2318 /* later simplified to 1 bit after manchester decoding. */
2319 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
2320 /* int BitStream[GraphTraceLen*2/clock+10]; */
2322 /* But it does not work if compiling on WIndows: therefore we just allocate a */
2324 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0};
2326 /* Detect high and lows */
2327 for (i
= 0; i
< GraphTraceLen
; i
++)
2329 if (GraphBuffer
[i
] > high
)
2330 high
= GraphBuffer
[i
];
2331 else if (GraphBuffer
[i
] < low
)
2332 low
= GraphBuffer
[i
];
2336 clock
= GetAskClock(Cmd
, high
, 1);
2338 int tolerance
= clock
/4;
2340 /* Detect first transition */
2341 /* Lo-Hi (arbitrary) */
2342 /* skip to the first high */
2343 for (i
= 0; i
< GraphTraceLen
; i
++)
2344 if (GraphBuffer
[i
] == high
)
2346 /* now look for the first low */
2347 for (; i
< GraphTraceLen
; i
++)
2349 if (GraphBuffer
[i
] == low
)
2356 /* If we're not working with 1/0s, demod based off clock */
2359 bit
= 0; /* We assume the 1st bit is zero, it may not be
2360 * the case: this routine (I think) has an init problem.
2363 for (; i
< (int)(GraphTraceLen
/ clock
); i
++)
2369 /* Find out if we hit both high and low peaks */
2370 for (j
= 0; j
< clock
; j
++)
2372 if (GraphBuffer
[(i
* clock
) + j
] == high
)
2374 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
2377 /* it doesn't count if it's the first part of our read
2378 because it's really just trailing from the last sequence */
2379 if (first
&& (hithigh
|| hitlow
))
2380 hithigh
= hitlow
= 0;
2384 if (hithigh
&& hitlow
)
2388 /* If we didn't hit both high and low peaks, we had a bit transition */
2389 if (!hithigh
|| !hitlow
)
2392 BitStream
[bit2idx
++] = bit
^ invert
;
2396 /* standard 1/0 bitstream */
2400 /* Then detect duration between 2 successive transitions */
2401 for (bitidx
= 1; i
< GraphTraceLen
; i
++)
2403 if (GraphBuffer
[i
-1] != GraphBuffer
[i
])
2408 // Error check: if bitidx becomes too large, we do not
2409 // have a Manchester encoded bitstream or the clock is really
2411 if (bitidx
> (GraphTraceLen
*2/clock
+8) ) {
2412 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
2415 // Then switch depending on lc length:
2416 // Tolerance is 1/4 of clock rate (arbitrary)
2417 if (abs(lc
-clock
/2) < tolerance
) {
2418 // Short pulse : either "1" or "0"
2419 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
2420 } else if (abs(lc
-clock
) < tolerance
) {
2421 // Long pulse: either "11" or "00"
2422 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
2423 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
2427 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
2428 PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
2432 PrintAndLog("Error: too many detection errors, aborting.");
2439 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
2440 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
2441 // to stop output at the final bitidx2 value, not bitidx
2442 for (i
= 0; i
< bitidx
; i
+= 2) {
2443 if ((BitStream
[i
] == 0) && (BitStream
[i
+1] == 1)) {
2444 BitStream
[bit2idx
++] = 1 ^ invert
;
2445 } else if ((BitStream
[i
] == 1) && (BitStream
[i
+1] == 0)) {
2446 BitStream
[bit2idx
++] = 0 ^ invert
;
2448 // We cannot end up in this state, this means we are unsynchronized,
2452 PrintAndLog("Unsynchronized, resync...");
2453 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
2457 PrintAndLog("Error: too many decode errors, aborting.");
2464 PrintAndLog("Manchester decoded bitstream");
2465 // Now output the bitstream to the scrollback by line of 16 bits
2466 for (i
= 0; i
< (bit2idx
-16); i
+=16) {
2467 PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
2488 /* Modulate our data into manchester */
2489 int CmdManchesterMod(const char *Cmd
)
2493 int bit
, lastbit
, wave
;
2496 clock
= GetAskClock(Cmd
, 0, 1);
2500 for (i
= 0; i
< (int)(GraphTraceLen
/ clock
); i
++)
2502 bit
= GraphBuffer
[i
* clock
] ^ 1;
2504 for (j
= 0; j
< (int)(clock
/2); j
++)
2505 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
;
2506 for (j
= (int)(clock
/2); j
< clock
; j
++)
2507 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
^ 1;
2509 /* Keep track of how we start our wave and if we changed or not this time */
2510 wave
^= bit
^ lastbit
;
2514 RepaintGraphWindow();
2518 int CmdNorm(const char *Cmd
)
2521 int max
= INT_MIN
, min
= INT_MAX
;
2523 for (i
= 10; i
< GraphTraceLen
; ++i
) {
2524 if (GraphBuffer
[i
] > max
)
2525 max
= GraphBuffer
[i
];
2526 if (GraphBuffer
[i
] < min
)
2527 min
= GraphBuffer
[i
];
2531 for (i
= 0; i
< GraphTraceLen
; ++i
) {
2532 GraphBuffer
[i
] = (GraphBuffer
[i
] - ((max
+ min
) / 2)) * 256 /
2534 //marshmelow: adjusted *1000 to *256 to make +/- 128 so demod commands still work
2537 RepaintGraphWindow();
2541 int CmdPlot(const char *Cmd
)
2547 int CmdSave(const char *Cmd
)
2549 char filename
[FILE_PATH_SIZE
] = {0x00};
2553 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
2554 memcpy(filename
, Cmd
, len
);
2557 FILE *f
= fopen(filename
, "w");
2559 PrintAndLog("couldn't open '%s'", filename
);
2563 for (i
= 0; i
< GraphTraceLen
; i
++) {
2564 fprintf(f
, "%d\n", GraphBuffer
[i
]);
2567 PrintAndLog("saved to '%s'", Cmd
);
2571 int CmdScale(const char *Cmd
)
2573 CursorScaleFactor
= atoi(Cmd
);
2574 if (CursorScaleFactor
== 0) {
2575 PrintAndLog("bad, can't have zero scale");
2576 CursorScaleFactor
= 1;
2578 RepaintGraphWindow();
2582 int CmdThreshold(const char *Cmd
)
2584 int threshold
= atoi(Cmd
);
2586 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
2587 if (GraphBuffer
[i
] >= threshold
)
2590 GraphBuffer
[i
] = -1;
2592 RepaintGraphWindow();
2596 int CmdDirectionalThreshold(const char *Cmd
)
2598 int8_t upThres
= param_get8(Cmd
, 0);
2599 int8_t downThres
= param_get8(Cmd
, 1);
2601 printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres
, downThres
);
2603 int lastValue
= GraphBuffer
[0];
2604 GraphBuffer
[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in.
2606 for (int i
= 1; i
< GraphTraceLen
; ++i
) {
2607 // Apply first threshold to samples heading up
2608 if (GraphBuffer
[i
] >= upThres
&& GraphBuffer
[i
] > lastValue
)
2610 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
2613 // Apply second threshold to samples heading down
2614 else if (GraphBuffer
[i
] <= downThres
&& GraphBuffer
[i
] < lastValue
)
2616 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
2617 GraphBuffer
[i
] = -1;
2621 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
2622 GraphBuffer
[i
] = GraphBuffer
[i
-1];
2626 GraphBuffer
[0] = GraphBuffer
[1]; // Aline with first edited sample.
2627 RepaintGraphWindow();
2631 int CmdZerocrossings(const char *Cmd
)
2633 // Zero-crossings aren't meaningful unless the signal is zero-mean.
2640 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
2641 if (GraphBuffer
[i
] * sign
>= 0) {
2642 // No change in sign, reproduce the previous sample count.
2644 GraphBuffer
[i
] = lastZc
;
2646 // Change in sign, reset the sample count.
2648 GraphBuffer
[i
] = lastZc
;
2656 RepaintGraphWindow();
2660 static command_t CommandTable
[] =
2662 {"help", CmdHelp
, 1, "This help"},
2663 {"amp", CmdAmp
, 1, "Amplify peaks"},
2664 //{"askdemod", Cmdaskdemod, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"},
2665 {"askedgedetect", CmdAskEdgeDetect
, 1, "[threshold] Adjust Graph for manual ask demod using length of sample differences to detect the edge of a wave (default = 25)"},
2666 {"askem410xdemod",CmdAskEM410xDemod
, 1, "[clock] [invert<0|1>] [maxErr] -- Demodulate an EM410x tag from GraphBuffer (args optional)"},
2667 {"askgproxiidemod",CmdG_Prox_II_Demod
,1, "Demodulate a G Prox II tag from GraphBuffer"},
2668 //{"askmandemod", Cmdaskmandemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate ASK/Manchester tags and output binary (args optional)"},
2669 //{"askrawdemod", Cmdaskrawdemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate ASK tags and output bin (args optional)"},
2670 {"autocorr", CmdAutoCorr
, 1, "[window length] [g] -- Autocorrelation over window - g to save back to GraphBuffer (overwrite)"},
2671 {"biphaserawdecode",CmdBiphaseDecodeRaw
,1,"[offset] [invert<0|1>] Biphase decode bin stream in DemodBuffer (offset = 0|1 bits to shift the decode start)"},
2672 {"bitsamples", CmdBitsamples
, 0, "Get raw samples as bitstring"},
2673 //{"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"},
2674 {"buffclear", CmdBuffClear
, 1, "Clear sample buffer and graph window"},
2675 {"dec", CmdDec
, 1, "Decimate samples"},
2676 {"detectclock", CmdDetectClockRate
, 1, "[modulation] Detect clock rate of wave in GraphBuffer (options: 'a','f','n','p' for ask, fsk, nrz, psk respectively)"},
2677 //{"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"},
2678 {"fskawiddemod", CmdFSKdemodAWID
, 1, "Demodulate an AWID FSK tag from GraphBuffer"},
2679 //{"fskfcdetect", CmdFSKfcDetect, 1, "Try to detect the Field Clock of an FSK wave"},
2680 {"fskhiddemod", CmdFSKdemodHID
, 1, "Demodulate a HID FSK tag from GraphBuffer"},
2681 {"fskiodemod", CmdFSKdemodIO
, 1, "Demodulate an IO Prox FSK tag from GraphBuffer"},
2682 {"fskpyramiddemod",CmdFSKdemodPyramid
,1, "Demodulate a Pyramid FSK tag from GraphBuffer"},
2683 {"fskparadoxdemod",CmdFSKdemodParadox
,1, "Demodulate a Paradox FSK tag from GraphBuffer"},
2684 //{"fskrawdemod", CmdFSKrawdemod, 1, "[clock rate] [invert] [rchigh] [rclow] Demodulate graph window from FSK to bin (clock = 50)(invert = 1|0)(rchigh = 10)(rclow=8)"},
2685 {"getbitstream", CmdGetBitStream
, 1, "Convert GraphBuffer's >=1 values to 1 and <1 to 0"},
2686 {"grid", CmdGrid
, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
2687 {"hexsamples", CmdHexsamples
, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},
2688 {"hide", CmdHide
, 1, "Hide graph window"},
2689 {"hpf", CmdHpf
, 1, "Remove DC offset from trace"},
2690 {"load", CmdLoad
, 1, "<filename> -- Load trace (to graph window"},
2691 {"ltrim", CmdLtrim
, 1, "<samples> -- Trim samples from left of trace"},
2692 {"rtrim", CmdRtrim
, 1, "<location to end trace> -- Trim samples from right of trace"},
2693 //{"mandemod", CmdManchesterDemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
2694 {"manrawdecode", Cmdmandecoderaw
, 1, "Manchester decode binary stream in DemodBuffer"},
2695 {"manmod", CmdManchesterMod
, 1, "[clock rate] -- Manchester modulate a binary stream"},
2696 {"norm", CmdNorm
, 1, "Normalize max/min to +/-128"},
2697 //{"nrzdetectclock",CmdDetectNRZClockRate, 1, "Detect ASK, PSK, or NRZ clock rate"},
2698 //{"nrzrawdemod", CmdNRZrawDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate nrz tags and output binary (args optional)"},
2699 {"plot", CmdPlot
, 1, "Show graph window (hit 'h' in window for keystroke help)"},
2700 //{"pskdetectclock",CmdDetectPSKClockRate, 1, "Detect ASK, PSK, or NRZ clock rate"},
2701 {"printdemodbuffer",CmdPrintDemodBuff
,1, "[x] -- print the data in the DemodBuffer - 'x' for hex output"},
2702 {"pskindalademod",CmdIndalaDecode
, 1, "[clock] [invert<0|1>] -- Demodulate an indala tag (PSK1) from GraphBuffer (args optional)"},
2703 //{"psk1rawdemod", CmdPSK1rawDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate psk1 tags and output binary (args optional)"},
2704 //{"psk2rawdemod", CmdPSK2rawDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate psk2 tags and output binary (args optional)"},
2705 {"rawdemod", CmdRawDemod
, 1, "[modulation] ... <options> -see help (h option) -- Demodulate the data in the GraphBuffer and output binary"},
2706 {"samples", CmdSamples
, 0, "[512 - 40000] -- Get raw samples for graph window (GraphBuffer)"},
2707 {"save", CmdSave
, 1, "<filename> -- Save trace (from graph window)"},
2708 {"scale", CmdScale
, 1, "<int> -- Set cursor display scale"},
2709 {"setdebugmode", CmdSetDebugMode
, 1, "<0|1> -- Turn on or off Debugging Mode for demods"},
2710 {"shiftgraphzero",CmdGraphShiftZero
, 1, "<shift> -- Shift 0 for Graphed wave + or - shift value"},
2711 //{"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"},
2712 {"dirthreshold", CmdDirectionalThreshold
, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."},
2713 {"tune", CmdTuneSamples
, 0, "Get hw tune samples for graph window"},
2714 {"undec", CmdUndec
, 1, "Un-decimate samples by 2"},
2715 {"zerocrossings", CmdZerocrossings
, 1, "Count time between zero-crossings"},
2716 {NULL
, NULL
, 0, NULL
}
2719 int CmdData(const char *Cmd
)
2721 CmdsParse(CommandTable
, Cmd
);
2725 int CmdHelp(const char *Cmd
)
2727 CmdsHelp(CommandTable
);