]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmddata.c
sim buffer max settings tests
[proxmark3-svn] / client / cmddata.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// Data and Graph commands
9//-----------------------------------------------------------------------------
10
7fe9b0b7 11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <limits.h>
902cb3c0 15#include "proxmark3.h"
7fe9b0b7 16#include "data.h"
17#include "ui.h"
18#include "graph.h"
19#include "cmdparser.h"
d51b2eda 20#include "util.h"
7fe9b0b7 21#include "cmdmain.h"
22#include "cmddata.h"
7db5f1ca 23#include "lfdemod.h"
31abe49f
MHS
24#include "usb_cmd.h"
25
4118b74d 26uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN];
ec75f5c1 27uint8_t g_debugMode;
4118b74d 28int DemodBufferLen;
7fe9b0b7 29static int CmdHelp(const char *Cmd);
30
4118b74d 31//set the demod buffer with given array of binary (one bit per byte)
32//by marshmellow
ec75f5c1 33void setDemodBuf(uint8_t *buff, size_t size, size_t startIdx)
4118b74d 34{
ec75f5c1 35 size_t i = 0;
36 for (; i < size; i++){
37 DemodBuffer[i]=buff[startIdx++];
ba1a299c 38 }
39 DemodBufferLen=size;
40 return;
4118b74d 41}
42
ec75f5c1 43int CmdSetDebugMode(const char *Cmd)
44{
45 int demod=0;
46 sscanf(Cmd, "%i", &demod);
47 g_debugMode=(uint8_t)demod;
48 return 1;
49}
50
4118b74d 51//by marshmellow
abd6112f 52void printDemodBuff(void)
4118b74d 53{
ba1a299c 54 uint32_t i = 0;
55 int bitLen = DemodBufferLen;
56 if (bitLen<16) {
57 PrintAndLog("no bits found in demod buffer");
58 return;
59 }
60 if (bitLen>512) bitLen=512; //max output to 512 bits if we have more - should be plenty
33c7e2f3 61
0c5998c0 62 // ensure equally divided by 16
b4fb11ba 63 bitLen &= 0xfff0;
33c7e2f3 64
ba1a299c 65 for (i = 0; i <= (bitLen-16); i+=16) {
66 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
67 DemodBuffer[i],
68 DemodBuffer[i+1],
69 DemodBuffer[i+2],
70 DemodBuffer[i+3],
71 DemodBuffer[i+4],
72 DemodBuffer[i+5],
73 DemodBuffer[i+6],
74 DemodBuffer[i+7],
75 DemodBuffer[i+8],
76 DemodBuffer[i+9],
77 DemodBuffer[i+10],
78 DemodBuffer[i+11],
79 DemodBuffer[i+12],
80 DemodBuffer[i+13],
81 DemodBuffer[i+14],
82 DemodBuffer[i+15]);
83 }
84 return;
4118b74d 85}
86
8d960002 87int CmdPrintDemodBuff(const char *Cmd)
88{
89 printDemodBuff();
90 return 1;
91}
7fe9b0b7 92int CmdAmp(const char *Cmd)
93{
94 int i, rising, falling;
95 int max = INT_MIN, min = INT_MAX;
96
97 for (i = 10; i < GraphTraceLen; ++i) {
98 if (GraphBuffer[i] > max)
99 max = GraphBuffer[i];
100 if (GraphBuffer[i] < min)
101 min = GraphBuffer[i];
102 }
103
104 if (max != min) {
105 rising = falling= 0;
106 for (i = 0; i < GraphTraceLen; ++i) {
107 if (GraphBuffer[i + 1] < GraphBuffer[i]) {
108 if (rising) {
109 GraphBuffer[i] = max;
110 rising = 0;
111 }
112 falling = 1;
113 }
114 if (GraphBuffer[i + 1] > GraphBuffer[i]) {
115 if (falling) {
116 GraphBuffer[i] = min;
117 falling = 0;
118 }
119 rising= 1;
120 }
121 }
122 }
123 RepaintGraphWindow();
124 return 0;
125}
126
127/*
128 * Generic command to demodulate ASK.
129 *
130 * Argument is convention: positive or negative (High mod means zero
131 * or high mod means one)
132 *
133 * Updates the Graph trace with 0/1 values
134 *
135 * Arguments:
78f5b1a7 136 * c : 0 or 1 (or invert)
7fe9b0b7 137 */
78f5b1a7 138 //this method ignores the clock
139
140 //this function strictly converts highs and lows to 1s and 0s for each sample in the graphbuffer
7fe9b0b7 141int Cmdaskdemod(const char *Cmd)
142{
143 int i;
144 int c, high = 0, low = 0;
ba1a299c 145
7fe9b0b7 146 sscanf(Cmd, "%i", &c);
ba1a299c 147
78f5b1a7 148 /* Detect high and lows */
7fe9b0b7 149 for (i = 0; i < GraphTraceLen; ++i)
150 {
151 if (GraphBuffer[i] > high)
152 high = GraphBuffer[i];
153 else if (GraphBuffer[i] < low)
154 low = GraphBuffer[i];
155 }
eb191de6 156 high=abs(high*.75);
157 low=abs(low*.75);
7fe9b0b7 158 if (c != 0 && c != 1) {
159 PrintAndLog("Invalid argument: %s", Cmd);
160 return 0;
161 }
b3b70669 162 //prime loop
7fe9b0b7 163 if (GraphBuffer[0] > 0) {
164 GraphBuffer[0] = 1-c;
165 } else {
166 GraphBuffer[0] = c;
167 }
168 for (i = 1; i < GraphTraceLen; ++i) {
169 /* Transitions are detected at each peak
170 * Transitions are either:
171 * - we're low: transition if we hit a high
172 * - we're high: transition if we hit a low
173 * (we need to do it this way because some tags keep high or
174 * low for long periods, others just reach the peak and go
175 * down)
176 */
ae2f73c1 177 //[marhsmellow] change == to >= for high and <= for low for fuzz
78f5b1a7 178 if ((GraphBuffer[i] >= high) && (GraphBuffer[i - 1] == c)) {
7fe9b0b7 179 GraphBuffer[i] = 1 - c;
78f5b1a7 180 } else if ((GraphBuffer[i] <= low) && (GraphBuffer[i - 1] == (1 - c))){
7fe9b0b7 181 GraphBuffer[i] = c;
182 } else {
183 /* No transition */
184 GraphBuffer[i] = GraphBuffer[i - 1];
185 }
186 }
187 RepaintGraphWindow();
188 return 0;
189}
190
78f5b1a7 191//this function strictly converts >1 to 1 and <1 to 0 for each sample in the graphbuffer
192int CmdGetBitStream(const char *Cmd)
193{
194 int i;
195 CmdHpf(Cmd);
196 for (i = 0; i < GraphTraceLen; i++) {
197 if (GraphBuffer[i] >= 1) {
198 GraphBuffer[i] = 1;
199 } else {
200 GraphBuffer[i] = 0;
201 }
202 }
203 RepaintGraphWindow();
204 return 0;
205}
206
207
d5a72d2f 208//by marshmellow
209void printBitStream(uint8_t BitStream[], uint32_t bitLen)
210{
b4fb11ba 211 uint32_t i = 0;
212 if (bitLen<16) {
213 PrintAndLog("Too few bits found: %d",bitLen);
214 return;
215 }
216 if (bitLen>512) bitLen=512;
0c5998c0 217
b4fb11ba 218 // ensure equally divided by 16
219 bitLen &= 0xfff0;
0c5998c0 220
b4fb11ba 221
222 for (i = 0; i <= (bitLen-16); i+=16) {
223 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
224 BitStream[i],
225 BitStream[i+1],
226 BitStream[i+2],
227 BitStream[i+3],
228 BitStream[i+4],
229 BitStream[i+5],
230 BitStream[i+6],
231 BitStream[i+7],
232 BitStream[i+8],
233 BitStream[i+9],
234 BitStream[i+10],
235 BitStream[i+11],
236 BitStream[i+12],
237 BitStream[i+13],
238 BitStream[i+14],
239 BitStream[i+15]);
240 }
ba1a299c 241 return;
2fc2150e 242}
d5a72d2f 243//by marshmellow
abd6112f 244//print 64 bit EM410x ID in multiple formats
eb191de6 245void printEM410x(uint64_t id)
2fc2150e 246{
eb191de6 247 if (id !=0){
b4fb11ba 248 uint64_t iii=1;
249 uint64_t id2lo=0;
250 uint32_t ii=0;
251 uint32_t i=0;
252 for (ii=5; ii>0;ii--){
253 for (i=0;i<8;i++){
254 id2lo=(id2lo<<1LL) | ((id & (iii << (i+((ii-1)*8)))) >> (i+((ii-1)*8)));
2fc2150e 255 }
b4fb11ba 256 }
257 //output em id
258 PrintAndLog("EM TAG ID : %010llx", id);
259 PrintAndLog("Unique TAG ID: %010llx", id2lo);
260 PrintAndLog("DEZ 8 : %08lld",id & 0xFFFFFF);
261 PrintAndLog("DEZ 10 : %010lld",id & 0xFFFFFF);
262 PrintAndLog("DEZ 5.5 : %05lld.%05lld",(id>>16LL) & 0xFFFF,(id & 0xFFFF));
263 PrintAndLog("DEZ 3.5A : %03lld.%05lld",(id>>32ll),(id & 0xFFFF));
264 PrintAndLog("DEZ 14/IK2 : %014lld",id);
265 PrintAndLog("DEZ 15/IK3 : %015lld",id2lo);
266 PrintAndLog("Other : %05lld_%03lld_%08lld",(id&0xFFFF),((id>>16LL) & 0xFF),(id & 0xFFFFFF));
267 }
268 return;
eb191de6 269}
270
e888ed8e 271//by marshmellow
e770c648 272//takes 3 arguments - clock, invert and maxErr as integers
273//attempts to demodulate ask while decoding manchester
274//prints binary found and saves in graphbuffer for further commands
275int CmdAskEM410xDemod(const char *Cmd)
276{
277 int invert=0;
278 int clk=0;
279 int maxErr=100;
280 char cmdp = param_getchar(Cmd, 0);
6de43508 281 if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
e770c648 282 PrintAndLog("Usage: data askem410xdemod [clock] <0|1> [maxError]");
283 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
284 PrintAndLog(" <invert>, 1 for invert output");
285 PrintAndLog(" [set maximum allowed errors], default = 100.");
286 PrintAndLog("");
287 PrintAndLog(" sample: data askem410xdemod = demod an EM410x Tag ID from GraphBuffer");
288 PrintAndLog(" : data askem410xdemod 32 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32");
289 PrintAndLog(" : data askem410xdemod 32 1 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32 and inverting data");
290 PrintAndLog(" : data askem410xdemod 1 = demod an EM410x Tag ID from GraphBuffer while inverting data");
291 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");
292
293 return 0;
294 }
295
296
297 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
298 sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
299 if (invert != 0 && invert != 1) {
300 PrintAndLog("Invalid argument: %s", Cmd);
301 return 0;
302 }
303 size_t BitLen = getFromGraphBuf(BitStream);
304
305 if (g_debugMode==1) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen);
306 if (BitLen==0) return 0;
307 int errCnt=0;
308 errCnt = askmandemod(BitStream, &BitLen, &clk, &invert, maxErr);
309 if (errCnt<0||BitLen<16){ //if fatal error (or -1)
310 if (g_debugMode==1) PrintAndLog("no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk);
311 return 0;
312 }
313 PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,BitLen);
314
315 //output
316 if (errCnt>0){
317 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
318 }
319 //PrintAndLog("ASK/Manchester decoded bitstream:");
320 // Now output the bitstream to the scrollback by line of 16 bits
321 setDemodBuf(BitStream,BitLen,0);
322 //printDemodBuff();
323 uint64_t lo =0;
324 size_t idx=0;
325 lo = Em410xDecode(BitStream, &BitLen, &idx);
326 if (lo>0){
327 //set GraphBuffer for clone or sim command
328 setDemodBuf(BitStream, BitLen, idx);
329 if (g_debugMode){
330 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
331 printDemodBuff();
332 }
333 PrintAndLog("EM410x pattern found: ");
abd6112f 334 if (BitLen > 64) PrintAndLog("\nWarning! Length not what is expected - Length: %d bits\n",BitLen);
e770c648 335 printEM410x(lo);
336 return 1;
337 }
338 return 0;
339}
340
4ac906d1 341int ASKmanDemod(const char *Cmd, bool verbose, bool emSearch)
e888ed8e 342{
ec75f5c1 343 int invert=0;
b4fb11ba 344 int clk=0;
e770c648 345 int maxErr=100;
4ac906d1 346
eb191de6 347 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
b4fb11ba 348 sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
e888ed8e 349 if (invert != 0 && invert != 1) {
350 PrintAndLog("Invalid argument: %s", Cmd);
351 return 0;
6de43508 352 }
353 if (clk==1){
354 invert=1;
355 clk=0;
e888ed8e 356 }
b4fb11ba 357 size_t BitLen = getFromGraphBuf(BitStream);
ec75f5c1 358 if (g_debugMode==1) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen);
e770c648 359 if (BitLen==0) return 0;
eb191de6 360 int errCnt=0;
e770c648 361 errCnt = askmandemod(BitStream, &BitLen, &clk, &invert, maxErr);
b4fb11ba 362 if (errCnt<0||BitLen<16){ //if fatal error (or -1)
363 if (g_debugMode==1) PrintAndLog("no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk);
e888ed8e 364 return 0;
b4fb11ba 365 }
4ac906d1 366 if (verbose) PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,BitLen);
ba1a299c 367
d5a72d2f 368 //output
eb191de6 369 if (errCnt>0){
4ac906d1 370 if (verbose) PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
e888ed8e 371 }
4ac906d1 372 if (verbose) PrintAndLog("ASK/Manchester decoded bitstream:");
eb191de6 373 // Now output the bitstream to the scrollback by line of 16 bits
b4fb11ba 374 setDemodBuf(BitStream,BitLen,0);
4ac906d1 375 if (verbose) printDemodBuff();
eb191de6 376 uint64_t lo =0;
ec75f5c1 377 size_t idx=0;
4ac906d1 378 if (emSearch){
379 lo = Em410xDecode(BitStream, &BitLen, &idx);
380 if (lo>0){
381 //set GraphBuffer for clone or sim command
382 setDemodBuf(BitStream, BitLen, idx);
383 if (g_debugMode){
384 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
385 printDemodBuff();
386 }
387 if (verbose) PrintAndLog("EM410x pattern found: ");
388 if (verbose) printEM410x(lo);
389 return 1;
ec75f5c1 390 }
d5a72d2f 391 }
e770c648 392 return 1;
eb191de6 393}
394
4ac906d1 395//by marshmellow
396//takes 3 arguments - clock, invert, maxErr as integers
397//attempts to demodulate ask while decoding manchester
398//prints binary found and saves in graphbuffer for further commands
399int Cmdaskmandemod(const char *Cmd)
400{
401 char cmdp = param_getchar(Cmd, 0);
402 if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
403 PrintAndLog("Usage: data rawdemod am [clock] <0|1> [maxError]");
404 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
405 PrintAndLog(" <invert>, 1 for invert output");
406 PrintAndLog(" [set maximum allowed errors], default = 100.");
407 PrintAndLog("");
408 PrintAndLog(" sample: data rawdemod am = demod an ask/manchester tag from GraphBuffer");
409 PrintAndLog(" : data rawdemod am 32 = demod an ask/manchester tag from GraphBuffer using a clock of RF/32");
410 PrintAndLog(" : data rawdemod am 32 1 = demod an ask/manchester tag from GraphBuffer using a clock of RF/32 and inverting data");
411 PrintAndLog(" : data rawdemod am 1 = demod an ask/manchester tag from GraphBuffer while inverting data");
412 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");
413 return 0;
414 }
415 return ASKmanDemod(Cmd, TRUE, TRUE);
416}
417
eb191de6 418//by marshmellow
d5a72d2f 419//manchester decode
eb191de6 420//stricktly take 10 and 01 and convert to 0 and 1
421int Cmdmandecoderaw(const char *Cmd)
422{
423 int i =0;
424 int errCnt=0;
b4fb11ba 425 size_t size=0;
6de43508 426 size_t maxErr = 20;
e770c648 427 char cmdp = param_getchar(Cmd, 0);
428 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
429 PrintAndLog("Usage: data manrawdecode");
430 PrintAndLog(" Takes 10 and 01 and converts to 0 and 1 respectively");
431 PrintAndLog(" --must have binary sequence in demodbuffer (run data askrawdemod first)");
432 PrintAndLog("");
433 PrintAndLog(" sample: data manrawdecode = decode manchester bitstream from the demodbuffer");
434 return 0;
435 }
436 if (DemodBufferLen==0) return 0;
eb191de6 437 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
438 int high=0,low=0;
b4fb11ba 439 for (;i<DemodBufferLen;++i){
440 if (DemodBuffer[i]>high) high=DemodBuffer[i];
441 else if(DemodBuffer[i]<low) low=DemodBuffer[i];
442 BitStream[i]=DemodBuffer[i];
eb191de6 443 }
444 if (high>1 || low <0 ){
445 PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
446 return 0;
447 }
b4fb11ba 448 size=i;
449 errCnt=manrawdecode(BitStream, &size);
6de43508 450 if (errCnt>=maxErr){
2df8c079 451 PrintAndLog("Too many errors: %d",errCnt);
452 return 0;
453 }
eb191de6 454 PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt);
b4fb11ba 455 printBitStream(BitStream, size);
eb191de6 456 if (errCnt==0){
b4fb11ba 457 uint64_t id = 0;
ec75f5c1 458 size_t idx=0;
b4fb11ba 459 id = Em410xDecode(BitStream, &size, &idx);
460 if (id>0){
ec75f5c1 461 //need to adjust to set bitstream back to manchester encoded data
462 //setDemodBuf(BitStream, size, idx);
463
464 printEM410x(id);
465 }
eb191de6 466 }
d5a72d2f 467 return 1;
468}
469
470//by marshmellow
471//biphase decode
472//take 01 or 10 = 0 and 11 or 00 = 1
1e090a61 473//takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit
474// and "invert" default = 0 if 1 it will invert output
ba1a299c 475// since it is not like manchester and doesn't have an incorrect bit pattern we
d5a72d2f 476// cannot determine if our decode is correct or if it should be shifted by one bit
477// the argument offset allows us to manually shift if the output is incorrect
478// (better would be to demod and decode at the same time so we can distinguish large
479// width waves vs small width waves to help the decode positioning) or askbiphdemod
480int CmdBiphaseDecodeRaw(const char *Cmd)
481{
b4fb11ba 482 int i = 0;
483 int errCnt=0;
ba1a299c 484 size_t size=0;
b4fb11ba 485 int offset=0;
486 int invert=0;
487 int high=0, low=0;
488 char cmdp = param_getchar(Cmd, 0);
489 if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') {
490 PrintAndLog("Usage: data biphaserawdecode [offset] <invert>");
491 PrintAndLog(" Converts 10 or 01 to 0 and 11 or 00 to 1");
492 PrintAndLog(" --must have binary sequence in demodbuffer (run data askrawdemod first)");
493 PrintAndLog("");
494 PrintAndLog(" [offset <0|1>], set to 0 not to adjust start position or to 1 to adjust decode start position");
495 PrintAndLog(" [invert <0|1>], set to 1 to invert output");
496 PrintAndLog("");
497 PrintAndLog(" sample: data biphaserawdecode = decode biphase bitstream from the demodbuffer");
498 PrintAndLog(" sample: data biphaserawdecode 1 1 = decode biphase bitstream from the demodbuffer, set offset, and invert output");
499 return 0;
500 }
1e090a61 501 sscanf(Cmd, "%i %i", &offset, &invert);
b4fb11ba 502 if (DemodBufferLen==0) return 0;
503 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
504 //get graphbuffer & high and low
ba1a299c 505 for (;i<DemodBufferLen;++i){
506 if(DemodBuffer[i]>high)high=DemodBuffer[i];
507 else if(DemodBuffer[i]<low)low=DemodBuffer[i];
508 BitStream[i]=DemodBuffer[i];
b4fb11ba 509 }
510 if (high>1 || low <0){
511 PrintAndLog("Error: please raw demod the wave first then decode");
512 return 0;
513 }
ba1a299c 514 size=i;
1e090a61 515 errCnt=BiphaseRawDecode(BitStream, &size, offset, invert);
b4fb11ba 516 if (errCnt>=20){
517 PrintAndLog("Too many errors attempting to decode: %d",errCnt);
518 return 0;
519 }
520 PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:",offset,errCnt);
ba1a299c 521 printBitStream(BitStream, size);
b4fb11ba 522 PrintAndLog("\nif bitstream does not look right try offset=1");
ada339a1 523 if (offset == 1) setDemodBuf(DemodBuffer,DemodBufferLen-1,1); //remove first bit from raw demod
b4fb11ba 524 return 1;
eb191de6 525}
526
8d960002 527// set demod buffer back to raw after biphase demod
528void setBiphaseDemodBuf(uint8_t *BitStream, size_t size)
529{
530 uint8_t rawStream[512]={0x00};
531 size_t i=0;
532 uint8_t curPhase=0;
533 if (size > 256) {
534 PrintAndLog("ERROR - Biphase Demod Buffer overrun");
535 return;
536 }
537 for (size_t idx=0; idx<size; idx++){
ada339a1 538 if(!BitStream[idx]){
8d960002 539 rawStream[i++] = curPhase;
540 rawStream[i++] = curPhase;
541 curPhase ^= 1;
542 } else {
543 rawStream[i++] = curPhase;
544 rawStream[i++] = curPhase ^ 1;
545 }
546 }
547 setDemodBuf(rawStream,i,0);
548 return;
549}
eb191de6 550//by marshmellow
6de43508 551//takes 4 arguments - clock, invert, maxErr as integers and amplify as char
eb191de6 552//attempts to demodulate ask only
553//prints binary found and saves in graphbuffer for further commands
4ac906d1 554int ASKrawDemod(const char *Cmd, bool verbose)
eb191de6 555{
b4fb11ba 556 int invert=0;
557 int clk=0;
e770c648 558 int maxErr=100;
6de43508 559 uint8_t askAmp = 0;
560 char amp = param_getchar(Cmd, 0);
eb191de6 561 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
b4fb11ba 562 sscanf(Cmd, "%i %i %i %c", &clk, &invert, &maxErr, &amp);
eb191de6 563 if (invert != 0 && invert != 1) {
4ac906d1 564 if (verbose) PrintAndLog("Invalid argument: %s", Cmd);
eb191de6 565 return 0;
566 }
6de43508 567 if (clk==1){
568 invert=1;
569 clk=0;
570 }
571 if (amp == 'a' || amp == 'A') askAmp=1;
b4fb11ba 572 size_t BitLen = getFromGraphBuf(BitStream);
e770c648 573 if (BitLen==0) return 0;
eb191de6 574 int errCnt=0;
b4fb11ba 575 errCnt = askrawdemod(BitStream, &BitLen, &clk, &invert, maxErr, askAmp);
576 if (errCnt==-1||BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
4ac906d1 577 if (verbose) PrintAndLog("no data found");
578 if (g_debugMode==1 && verbose) PrintAndLog("errCnt: %d, BitLen: %d, clk: %d, invert: %d", errCnt, BitLen, clk, invert);
eb191de6 579 return 0;
b4fb11ba 580 }
4ac906d1 581 if (verbose) PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d", clk, invert, BitLen);
ec75f5c1 582
583 //move BitStream back to DemodBuffer
b4fb11ba 584 setDemodBuf(BitStream,BitLen,0);
ba1a299c 585
b4fb11ba 586 //output
4ac906d1 587 if (errCnt>0 && verbose){
e770c648 588 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d", errCnt);
eb191de6 589 }
4ac906d1 590 if (verbose){
591 PrintAndLog("ASK demoded bitstream:");
592 // Now output the bitstream to the scrollback by line of 16 bits
593 printBitStream(BitStream,BitLen);
594 }
f822a063 595 return 1;
e888ed8e 596}
597
8d960002 598//by marshmellow
599//attempts to demodulate and identify a G_Prox_II verex/chubb card
600//WARNING: if it fails during some points it will destroy the DemodBuffer data
601// but will leave the GraphBuffer intact.
602//if successful it will push askraw data back to demod buffer ready for emulation
603int CmdG_Prox_II_Demod(const char *Cmd)
604{
605 int ans = ASKrawDemod(Cmd, FALSE);
606 if (ans <= 0) {
607 if (g_debugMode) PrintAndLog("Error AskrawDemod: %d",ans);
608 return ans;
609 }
610 size_t size = DemodBufferLen;
611 ans = BiphaseRawDecode(DemodBuffer, &size, 0, 0);
612 if (ans !=0) {
613 if (g_debugMode) PrintAndLog("Error BiphaseRawDecode: %d",ans);
614 return ans;
615 }
616 //call lfdemod.c demod for gProxII
617 ans = gProxII_Demod(DemodBuffer, &size);
618 if (ans < 0){
619 if (g_debugMode) PrintAndLog("Error gProxII_Demod 1st Try: %d",ans);
620 //try biphase again
621 ans = BiphaseRawDecode(DemodBuffer, &size, 1, 0);
622 if (ans != 0) {
623 if (g_debugMode) PrintAndLog("Error BiphaseRawDecode: %d",ans);
624 return ans;
625 }
626 ans = gProxII_Demod(DemodBuffer, &size);
627 if (ans < 0) {
628 if (g_debugMode) PrintAndLog("Error gProxII_Demod 1st Try: %d",ans);
629 return ans;
630 }
631 }
632 //got a good demod
633 uint32_t ByteStream[65] = {0x00};
634 uint8_t xorKey=0;
635 uint8_t keyCnt=0;
636 uint8_t bitCnt=0;
637 uint8_t ByteCnt=0;
638 size_t startIdx = ans + 6; //start after preamble
639 for (size_t idx = 0; idx<size-6; idx++){
640 if ((idx+1) % 5 == 0){
641 //spacer bit - should be 0
642 if (DemodBuffer[startIdx+idx] != 0) {
643 if (g_debugMode) PrintAndLog("Error spacer not 0: %d, pos: %d",DemodBuffer[startIdx+idx],startIdx+idx);
644 return -1;
645 }
646 continue;
647 }
648 if (keyCnt<8){ //lsb first
649 xorKey = xorKey | (DemodBuffer[startIdx+idx]<<keyCnt);
650 keyCnt++;
651 if (keyCnt==8 && g_debugMode) PrintAndLog("xorKey Found: %02x", xorKey);
652 continue;
653 }
654 //lsb first
655 ByteStream[ByteCnt] = ByteStream[ByteCnt] | (DemodBuffer[startIdx+idx]<<bitCnt);
656 bitCnt++;
657 if (bitCnt % 8 == 0){
658 if (g_debugMode) PrintAndLog("byte %d: %02x",ByteCnt,ByteStream[ByteCnt]);
659 bitCnt=0;
660 ByteCnt++;
661 }
662 }
663 for (uint8_t i = 0; i < ByteCnt; i++){
664 ByteStream[i] ^= xorKey; //xor
665 if (g_debugMode) PrintAndLog("byte %d after xor: %02x", i, ByteStream[i]);
666 }
667 //now ByteStream contains 64 bytes of decrypted raw tag data
668 //
669 uint8_t fmtLen = ByteStream[0]>>2;
670 uint32_t FC = 0;
671 uint32_t Card = 0;
672 uint32_t raw1 = bytebits_to_byte(DemodBuffer+ans,32);
673 uint32_t raw2 = bytebits_to_byte(DemodBuffer+ans+32, 32);
674 uint32_t raw3 = bytebits_to_byte(DemodBuffer+ans+64, 32);
675
676 if (fmtLen==36){
677 FC = ((ByteStream[3] & 0x7F)<<7) | (ByteStream[4]>>1);
678 Card = ((ByteStream[4]&1)<<19) | (ByteStream[5]<<11) | (ByteStream[6]<<3) | (ByteStream[7]>>5);
679 PrintAndLog("G-Prox-II Found: FmtLen %d, FC %d, Card %d",fmtLen,FC,Card);
680 } else if(fmtLen==26){
681 FC = ((ByteStream[3] & 0x7F)<<1) | (ByteStream[4]>>7);
682 Card = ((ByteStream[4]&0x7F)<<9) | (ByteStream[5]<<1) | (ByteStream[6]>>7);
683 PrintAndLog("G-Prox-II Found: FmtLen %d, FC %d, Card %d",fmtLen,FC,Card);
684 } else {
685 PrintAndLog("Unknown G-Prox-II Fmt Found: FmtLen %d",fmtLen);
686 }
687 PrintAndLog("Raw: %08x%08x%08x", raw1,raw2,raw3);
688 setBiphaseDemodBuf(DemodBuffer+ans, 96);
689 return 1;
690}
691
4ac906d1 692//by marshmellow - see ASKrawDemod
693int Cmdaskrawdemod(const char *Cmd)
694{
695 char cmdp = param_getchar(Cmd, 0);
696 if (strlen(Cmd) > 12 || cmdp == 'h' || cmdp == 'H') {
697 PrintAndLog("Usage: data rawdemod ar [clock] <invert> [maxError] [amplify]");
698 PrintAndLog(" [set clock as integer] optional, if not set, autodetect");
699 PrintAndLog(" <invert>, 1 to invert output");
700 PrintAndLog(" [set maximum allowed errors], default = 100");
701 PrintAndLog(" <amplify>, 'a' to attempt demod with ask amplification, default = no amp");
702 PrintAndLog("");
703 PrintAndLog(" sample: data rawdemod ar = demod an ask tag from GraphBuffer");
704 PrintAndLog(" : data rawdemod ar a = demod an ask tag from GraphBuffer, amplified");
705 PrintAndLog(" : data rawdemod ar 32 = demod an ask tag from GraphBuffer using a clock of RF/32");
706 PrintAndLog(" : data rawdemod ar 32 1 = demod an ask tag from GraphBuffer using a clock of RF/32 and inverting data");
707 PrintAndLog(" : data rawdemod ar 1 = demod an ask tag from GraphBuffer while inverting data");
708 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");
709 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");
710 return 0;
711 }
712 return ASKrawDemod(Cmd, TRUE);
713}
714
7fe9b0b7 715int CmdAutoCorr(const char *Cmd)
716{
717 static int CorrelBuffer[MAX_GRAPH_TRACE_LEN];
718
719 int window = atoi(Cmd);
720
721 if (window == 0) {
722 PrintAndLog("needs a window");
723 return 0;
724 }
725 if (window >= GraphTraceLen) {
726 PrintAndLog("window must be smaller than trace (%d samples)",
727 GraphTraceLen);
728 return 0;
729 }
730
731 PrintAndLog("performing %d correlations", GraphTraceLen - window);
732
733 for (int i = 0; i < GraphTraceLen - window; ++i) {
734 int sum = 0;
735 for (int j = 0; j < window; ++j) {
736 sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256;
737 }
738 CorrelBuffer[i] = sum;
739 }
740 GraphTraceLen = GraphTraceLen - window;
741 memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int));
742
743 RepaintGraphWindow();
744 return 0;
745}
746
747int CmdBitsamples(const char *Cmd)
748{
749 int cnt = 0;
90d74dc2 750 uint8_t got[12288];
952a8bb5 751
90d74dc2 752 GetFromBigBuf(got,sizeof(got),0);
753 WaitForResponse(CMD_ACK,NULL);
7fe9b0b7 754
90d74dc2 755 for (int j = 0; j < sizeof(got); j++) {
7fe9b0b7 756 for (int k = 0; k < 8; k++) {
90d74dc2 757 if(got[j] & (1 << (7 - k))) {
7fe9b0b7 758 GraphBuffer[cnt++] = 1;
759 } else {
760 GraphBuffer[cnt++] = 0;
761 }
762 }
7fe9b0b7 763 }
764 GraphTraceLen = cnt;
765 RepaintGraphWindow();
766 return 0;
767}
768
769/*
770 * Convert to a bitstream
771 */
772int CmdBitstream(const char *Cmd)
773{
774 int i, j;
775 int bit;
776 int gtl;
777 int clock;
778 int low = 0;
779 int high = 0;
780 int hithigh, hitlow, first;
781
782 /* Detect high and lows and clock */
783 for (i = 0; i < GraphTraceLen; ++i)
784 {
785 if (GraphBuffer[i] > high)
786 high = GraphBuffer[i];
787 else if (GraphBuffer[i] < low)
788 low = GraphBuffer[i];
789 }
790
791 /* Get our clock */
f3bf15e4 792 clock = GetAskClock(Cmd, high, 1);
7fe9b0b7 793 gtl = ClearGraph(0);
794
795 bit = 0;
796 for (i = 0; i < (int)(gtl / clock); ++i)
797 {
798 hithigh = 0;
799 hitlow = 0;
800 first = 1;
801 /* Find out if we hit both high and low peaks */
802 for (j = 0; j < clock; ++j)
803 {
804 if (GraphBuffer[(i * clock) + j] == high)
805 hithigh = 1;
806 else if (GraphBuffer[(i * clock) + j] == low)
807 hitlow = 1;
808 /* it doesn't count if it's the first part of our read
809 because it's really just trailing from the last sequence */
810 if (first && (hithigh || hitlow))
811 hithigh = hitlow = 0;
812 else
813 first = 0;
814
815 if (hithigh && hitlow)
816 break;
817 }
818
819 /* If we didn't hit both high and low peaks, we had a bit transition */
820 if (!hithigh || !hitlow)
821 bit ^= 1;
822
823 AppendGraph(0, clock, bit);
7fe9b0b7 824 }
ba1a299c 825
7fe9b0b7 826 RepaintGraphWindow();
827 return 0;
828}
829
830int CmdBuffClear(const char *Cmd)
831{
832 UsbCommand c = {CMD_BUFF_CLEAR};
833 SendCommand(&c);
834 ClearGraph(true);
835 return 0;
836}
837
838int CmdDec(const char *Cmd)
839{
840 for (int i = 0; i < (GraphTraceLen / 2); ++i)
841 GraphBuffer[i] = GraphBuffer[i * 2];
842 GraphTraceLen /= 2;
843 PrintAndLog("decimated by 2");
844 RepaintGraphWindow();
845 return 0;
846}
698b649e
MHS
847/**
848 * Undecimate - I'd call it 'interpolate', but we'll save that
849 * name until someone does an actual interpolation command, not just
850 * blindly repeating samples
851 * @param Cmd
852 * @return
853 */
854int CmdUndec(const char *Cmd)
855{
c856ceae
MHS
856 if(param_getchar(Cmd, 0) == 'h')
857 {
858 PrintAndLog("Usage: data undec [factor]");
859 PrintAndLog("This function performs un-decimation, by repeating each sample N times");
860 PrintAndLog("Options: ");
861 PrintAndLog(" h This help");
862 PrintAndLog(" factor The number of times to repeat each sample.[default:2]");
863 PrintAndLog("Example: 'data undec 3'");
864 return 0;
865 }
866
867 uint8_t factor = param_get8ex(Cmd, 0,2, 10);
698b649e
MHS
868 //We have memory, don't we?
869 int swap[MAX_GRAPH_TRACE_LEN] = { 0 };
c856ceae
MHS
870 uint32_t g_index = 0 ,s_index = 0;
871 while(g_index < GraphTraceLen && s_index < MAX_GRAPH_TRACE_LEN)
698b649e 872 {
c856ceae
MHS
873 int count = 0;
874 for(count = 0; count < factor && s_index+count < MAX_GRAPH_TRACE_LEN; count ++)
875 swap[s_index+count] = GraphBuffer[g_index];
876 s_index+=count;
698b649e 877 }
698b649e 878
c856ceae
MHS
879 memcpy(GraphBuffer,swap, s_index * sizeof(int));
880 GraphTraceLen = s_index;
881 RepaintGraphWindow();
698b649e
MHS
882 return 0;
883}
7fe9b0b7 884
ec75f5c1 885//by marshmellow
886//shift graph zero up or down based on input + or -
887int CmdGraphShiftZero(const char *Cmd)
888{
889
890 int shift=0;
891 //set options from parameters entered with the command
892 sscanf(Cmd, "%i", &shift);
893 int shiftedVal=0;
894 for(int i = 0; i<GraphTraceLen; i++){
895 shiftedVal=GraphBuffer[i]+shift;
896 if (shiftedVal>127)
897 shiftedVal=127;
898 else if (shiftedVal<-127)
899 shiftedVal=-127;
900 GraphBuffer[i]= shiftedVal;
901 }
902 CmdNorm("");
903 return 0;
904}
905
6de43508 906//by marshmellow
907//use large jumps in read samples to identify edges of waves and then amplify that wave to max
908//similar to dirtheshold, threshold, and askdemod commands
909//takes a threshold length which is the measured length between two samples then determines an edge
910int CmdAskEdgeDetect(const char *Cmd)
911{
912 int thresLen = 25;
913 sscanf(Cmd, "%i", &thresLen);
914 int shift = 127;
915 int shiftedVal=0;
916 for(int i = 1; i<GraphTraceLen; i++){
917 if (GraphBuffer[i]-GraphBuffer[i-1]>=thresLen) //large jump up
918 shift=127;
919 else if(GraphBuffer[i]-GraphBuffer[i-1]<=-1*thresLen) //large jump down
920 shift=-127;
921
922 shiftedVal=GraphBuffer[i]+shift;
923
924 if (shiftedVal>127)
925 shiftedVal=127;
926 else if (shiftedVal<-127)
927 shiftedVal=-127;
928 GraphBuffer[i-1] = shiftedVal;
929 }
930 RepaintGraphWindow();
931 //CmdNorm("");
932 return 0;
933}
934
7fe9b0b7 935/* Print our clock rate */
ba1a299c 936// uses data from graphbuffer
f3bf15e4 937// adjusted to take char parameter for type of modulation to find the clock - by marshmellow.
7fe9b0b7 938int CmdDetectClockRate(const char *Cmd)
939{
f3bf15e4 940 char cmdp = param_getchar(Cmd, 0);
941 if (strlen(Cmd) > 3 || strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
942 PrintAndLog("Usage: data detectclock [modulation]");
943 PrintAndLog(" [modulation as char], specify the modulation type you want to detect the clock of");
944 PrintAndLog(" 'a' = ask, 'f' = fsk, 'n' = nrz/direct, 'p' = psk");
945 PrintAndLog("");
946 PrintAndLog(" sample: data detectclock a = detect the clock of an ask modulated wave in the GraphBuffer");
947 PrintAndLog(" data detectclock f = detect the clock of an fsk modulated wave in the GraphBuffer");
948 PrintAndLog(" data detectclock p = detect the clock of an psk modulated wave in the GraphBuffer");
949 PrintAndLog(" data detectclock n = detect the clock of an nrz/direct modulated wave in the GraphBuffer");
950 }
951 int ans=0;
952 if (cmdp == 'a'){
953 ans = GetAskClock("", true, false);
954 } else if (cmdp == 'f'){
955 ans = GetFskClock("", true, false);
956 } else if (cmdp == 'n'){
957 ans = GetNrzClock("", true, false);
958 } else if (cmdp == 'p'){
959 ans = GetPskClock("", true, false);
960 } else {
961 PrintAndLog ("Please specify a valid modulation to detect the clock of - see option h for help");
962 }
963 return ans;
7fe9b0b7 964}
66707a3b 965
2fc2150e 966//by marshmellow
eb191de6 967//fsk raw demod and print binary
6de43508 968//takes 4 arguments - Clock, invert, fchigh, fclow
969//defaults: clock = 50, invert=1, fchigh=10, fclow=8 (RF/10 RF/8 (fsk2a))
4ac906d1 970int FSKrawDemod(const char *Cmd, bool verbose)
b3b70669 971{
eb191de6 972 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
b3b70669 973 //set defaults
03e6bb4a 974 int rfLen = 0;
4ac906d1 975 int invert = 0;
976 int fchigh = 0;
977 int fclow = 0;
978
b3b70669 979 //set options from parameters entered with the command
b4fb11ba 980 sscanf(Cmd, "%i %i %i %i", &rfLen, &invert, &fchigh, &fclow);
ba1a299c 981
b3b70669 982 if (strlen(Cmd)>0 && strlen(Cmd)<=2) {
b3b70669 983 if (rfLen==1){
984 invert=1; //if invert option only is used
03e6bb4a 985 rfLen = 0;
986 }
b4fb11ba 987 }
03e6bb4a 988
eb191de6 989 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
b4fb11ba 990 size_t BitLen = getFromGraphBuf(BitStream);
e770c648 991 if (BitLen==0) return 0;
03e6bb4a 992 //get field clock lengths
993 uint16_t fcs=0;
e770c648 994 uint8_t dummy=0;
03e6bb4a 995 if (fchigh==0 || fclow == 0){
4ac906d1 996 fcs = countFC(BitStream, BitLen, &dummy);
03e6bb4a 997 if (fcs==0){
998 fchigh=10;
999 fclow=8;
1000 }else{
1001 fchigh = (fcs >> 8) & 0xFF;
1002 fclow = fcs & 0xFF;
1003 }
1004 }
1005 //get bit clock length
1006 if (rfLen==0){
1007 rfLen = detectFSKClk(BitStream, BitLen, fchigh, fclow);
1008 if (rfLen == 0) rfLen = 50;
1009 }
4ac906d1 1010 if (verbose) PrintAndLog("Args invert: %d - Clock:%d - fchigh:%d - fclow: %d",invert,rfLen,fchigh, fclow);
b4fb11ba 1011 int size = fskdemod(BitStream,BitLen,(uint8_t)rfLen,(uint8_t)invert,(uint8_t)fchigh,(uint8_t)fclow);
d5a72d2f 1012 if (size>0){
b4fb11ba 1013 setDemodBuf(BitStream,size,0);
ba1a299c 1014
d5a72d2f 1015 // Now output the bitstream to the scrollback by line of 16 bits
1016 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
4ac906d1 1017 if (verbose) {
1018 PrintAndLog("FSK decoded bitstream:");
1019 printBitStream(BitStream,size);
1020 }
3f41e73c 1021
e770c648 1022 return 1;
d5a72d2f 1023 } else{
4ac906d1 1024 if (verbose) PrintAndLog("no FSK data found");
eb191de6 1025 }
b3b70669 1026 return 0;
1027}
1028
4ac906d1 1029//by marshmellow
1030//fsk raw demod and print binary
1031//takes 4 arguments - Clock, invert, fchigh, fclow
1032//defaults: clock = 50, invert=1, fchigh=10, fclow=8 (RF/10 RF/8 (fsk2a))
1033int CmdFSKrawdemod(const char *Cmd)
1034{
1035 char cmdp = param_getchar(Cmd, 0);
1036 if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
1037 PrintAndLog("Usage: data rawdemod fs [clock] <invert> [fchigh] [fclow]");
1038 PrintAndLog(" [set clock as integer] optional, omit for autodetect.");
1039 PrintAndLog(" <invert>, 1 for invert output, can be used even if the clock is omitted");
1040 PrintAndLog(" [fchigh], larger field clock length, omit for autodetect");
1041 PrintAndLog(" [fclow], small field clock length, omit for autodetect");
1042 PrintAndLog("");
1043 PrintAndLog(" sample: data rawdemod fs = demod an fsk tag from GraphBuffer using autodetect");
1044 PrintAndLog(" : data rawdemod fs 32 = demod an fsk tag from GraphBuffer using a clock of RF/32, autodetect fc");
1045 PrintAndLog(" : data rawdemod fs 1 = demod an fsk tag from GraphBuffer using autodetect, invert output");
1046 PrintAndLog(" : data rawdemod fs 32 1 = demod an fsk tag from GraphBuffer using a clock of RF/32, invert output, autodetect fc");
1047 PrintAndLog(" : data rawdemod fs 64 0 8 5 = demod an fsk1 RF/64 tag from GraphBuffer");
1048 PrintAndLog(" : data rawdemod fs 50 0 10 8 = demod an fsk2 RF/50 tag from GraphBuffer");
1049 PrintAndLog(" : data rawdemod fs 50 1 10 8 = demod an fsk2a RF/50 tag from GraphBuffer");
1050 return 0;
1051 }
1052 return FSKrawDemod(Cmd, TRUE);
1053}
1054
eb191de6 1055//by marshmellow (based on existing demod + holiman's refactor)
1056//HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
1057//print full HID Prox ID and some bit format details if found
b3b70669 1058int CmdFSKdemodHID(const char *Cmd)
1059{
1060 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
b3b70669 1061 uint32_t hi2=0, hi=0, lo=0;
ba1a299c 1062
eb191de6 1063 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
b4fb11ba 1064 size_t BitLen = getFromGraphBuf(BitStream);
1065 if (BitLen==0) return 0;
b3b70669 1066 //get binary from fsk wave
b4fb11ba 1067 int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo);
ec75f5c1 1068 if (idx<0){
a1d17964 1069 if (g_debugMode){
1070 if (idx==-1){
b4fb11ba 1071 PrintAndLog("DEBUG: Just Noise Detected");
a1d17964 1072 } else if (idx == -2) {
1073 PrintAndLog("DEBUG: Error demoding fsk");
1074 } else if (idx == -3) {
1075 PrintAndLog("DEBUG: Preamble not found");
1076 } else if (idx == -4) {
1077 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen);
1078 } else {
1079 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
1080 }
1081 }
ec75f5c1 1082 return 0;
1083 }
1084 if (hi2==0 && hi==0 && lo==0) {
1085 if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
eb191de6 1086 return 0;
1087 }
1088 if (hi2 != 0){ //extra large HID tags
b4fb11ba 1089 PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)",
eb191de6 1090 (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF);
1091 }
1092 else { //standard HID tags <38 bits
d5a72d2f 1093 uint8_t fmtLen = 0;
eb191de6 1094 uint32_t fc = 0;
1095 uint32_t cardnum = 0;
1096 if (((hi>>5)&1)==1){//if bit 38 is set then < 37 bit format is used
1097 uint32_t lo2=0;
84871873 1098 lo2=(((hi & 31) << 12) | (lo>>20)); //get bits 21-37 to check for format len bit
eb191de6 1099 uint8_t idx3 = 1;
1100 while(lo2>1){ //find last bit set to 1 (format len bit)
1101 lo2=lo2>>1;
1102 idx3++;
b3b70669 1103 }
b4fb11ba 1104 fmtLen =idx3+19;
eb191de6 1105 fc =0;
1106 cardnum=0;
d5a72d2f 1107 if(fmtLen==26){
eb191de6 1108 cardnum = (lo>>1)&0xFFFF;
1109 fc = (lo>>17)&0xFF;
1110 }
d5a72d2f 1111 if(fmtLen==34){
eb191de6 1112 cardnum = (lo>>1)&0xFFFF;
1113 fc= ((hi&1)<<15)|(lo>>17);
1114 }
d5a72d2f 1115 if(fmtLen==35){
eb191de6 1116 cardnum = (lo>>1)&0xFFFFF;
1117 fc = ((hi&1)<<11)|(lo>>21);
b3b70669 1118 }
b3b70669 1119 }
eb191de6 1120 else { //if bit 38 is not set then 37 bit format is used
84871873 1121 fmtLen = 37;
1122 fc = 0;
1123 cardnum = 0;
1124 if(fmtLen == 37){
eb191de6 1125 cardnum = (lo>>1)&0x7FFFF;
1126 fc = ((hi&0xF)<<12)|(lo>>20);
1127 }
b4fb11ba 1128 }
1129 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d",
eb191de6 1130 (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
d5a72d2f 1131 (unsigned int) fmtLen, (unsigned int) fc, (unsigned int) cardnum);
b3b70669 1132 }
ec75f5c1 1133 setDemodBuf(BitStream,BitLen,idx);
1134 if (g_debugMode){
1135 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
1136 printDemodBuff();
1137 }
1138 return 1;
1139}
1140
04d2721b 1141//by marshmellow
1142//Paradox Prox demod - FSK RF/50 with preamble of 00001111 (then manchester encoded)
ec75f5c1 1143//print full Paradox Prox ID and some bit format details if found
1144int CmdFSKdemodParadox(const char *Cmd)
1145{
1146 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1147 uint32_t hi2=0, hi=0, lo=0;
1148
1149 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
1150 size_t BitLen = getFromGraphBuf(BitStream);
e770c648 1151 if (BitLen==0) return 0;
ec75f5c1 1152 //get binary from fsk wave
a1d17964 1153 int idx = ParadoxdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo);
ec75f5c1 1154 if (idx<0){
a1d17964 1155 if (g_debugMode){
1156 if (idx==-1){
1157 PrintAndLog("DEBUG: Just Noise Detected");
1158 } else if (idx == -2) {
1159 PrintAndLog("DEBUG: Error demoding fsk");
1160 } else if (idx == -3) {
1161 PrintAndLog("DEBUG: Preamble not found");
1162 } else if (idx == -4) {
1163 PrintAndLog("DEBUG: Error in Manchester data");
1164 } else {
1165 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
1166 }
1167 }
ec75f5c1 1168 return 0;
1169 }
1170 if (hi2==0 && hi==0 && lo==0){
1171 if (g_debugMode) PrintAndLog("DEBUG: Error - no value found");
1172 return 0;
1173 }
1174 uint32_t fc = ((hi & 0x3)<<6) | (lo>>26);
1175 uint32_t cardnum = (lo>>10)&0xFFFF;
abd6112f 1176 uint32_t rawLo = bytebits_to_byte(BitStream+idx+64,32);
1177 uint32_t rawHi = bytebits_to_byte(BitStream+idx+32,32);
1178 uint32_t rawHi2 = bytebits_to_byte(BitStream+idx,32);
1179
1180 PrintAndLog("Paradox TAG ID: %x%08x - FC: %d - Card: %d - Checksum: %02x - RAW: %08x%08x%08x",
1181 hi>>10, (hi & 0x3)<<26 | (lo>>10), fc, cardnum, (lo>>2) & 0xFF, rawHi2, rawHi, rawLo);
ec75f5c1 1182 setDemodBuf(BitStream,BitLen,idx);
1183 if (g_debugMode){
1184 PrintAndLog("DEBUG: idx: %d, len: %d, Printing Demod Buffer:", idx, BitLen);
1185 printDemodBuff();
1186 }
1187 return 1;
b3b70669 1188}
1189
2fc2150e 1190//by marshmellow
eb191de6 1191//IO-Prox demod - FSK RF/64 with preamble of 000000001
1192//print ioprox ID and some format details
b3b70669 1193int CmdFSKdemodIO(const char *Cmd)
1194{
1195 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1196 //set defaults
b4fb11ba 1197 int idx=0;
ec75f5c1 1198 //something in graphbuffer?
1199 if (GraphTraceLen < 65) {
1200 if (g_debugMode)PrintAndLog("DEBUG: not enough samples in GraphBuffer");
1201 return 0;
1202 }
eb191de6 1203 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
b4fb11ba 1204 size_t BitLen = getFromGraphBuf(BitStream);
e770c648 1205 if (BitLen==0) return 0;
ec75f5c1 1206
eb191de6 1207 //get binary from fsk wave
b4fb11ba 1208 idx = IOdemodFSK(BitStream,BitLen);
eb191de6 1209 if (idx<0){
a1d17964 1210 if (g_debugMode){
1211 if (idx==-1){
1212 PrintAndLog("DEBUG: Just Noise Detected");
1213 } else if (idx == -2) {
1214 PrintAndLog("DEBUG: not enough samples");
1215 } else if (idx == -3) {
1216 PrintAndLog("DEBUG: error during fskdemod");
1217 } else if (idx == -4) {
1218 PrintAndLog("DEBUG: Preamble not found");
1219 } else if (idx == -5) {
1220 PrintAndLog("DEBUG: Separator bits not found");
1221 } else {
1222 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
1223 }
1224 }
eb191de6 1225 return 0;
1226 }
1227 if (idx==0){
ec75f5c1 1228 if (g_debugMode==1){
1229 PrintAndLog("DEBUG: IO Prox Data not found - FSK Bits: %d",BitLen);
1230 if (BitLen > 92) printBitStream(BitStream,92);
1231 }
d5a72d2f 1232 return 0;
b3b70669 1233 }
b3b70669 1234 //Index map
1235 //0 10 20 30 40 50 60
1236 //| | | | | | |
1237 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
1238 //-----------------------------------------------------------------------------
1239 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
1240 //
1241 //XSF(version)facility:codeone+codetwo (raw)
1242 //Handle the data
ec75f5c1 1243 if (idx+64>BitLen) {
1244 if (g_debugMode==1) PrintAndLog("not enough bits found - bitlen: %d",BitLen);
1245 return 0;
1246 }
eb191de6 1247 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]);
b4fb11ba 1248 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]);
f822a063 1249 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]);
1250 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]);
1251 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]);
1252 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]);
1253 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]);
ba1a299c 1254
eb191de6 1255 uint32_t code = bytebits_to_byte(BitStream+idx,32);
b4fb11ba 1256 uint32_t code2 = bytebits_to_byte(BitStream+idx+32,32);
f822a063 1257 uint8_t version = bytebits_to_byte(BitStream+idx+27,8); //14,4
1258 uint8_t facilitycode = bytebits_to_byte(BitStream+idx+18,8) ;
eb191de6 1259 uint16_t number = (bytebits_to_byte(BitStream+idx+36,8)<<8)|(bytebits_to_byte(BitStream+idx+45,8)); //36,9
b4fb11ba 1260 PrintAndLog("IO Prox XSF(%02d)%02x:%05d (%08x%08x)",version,facilitycode,number,code,code2);
ec75f5c1 1261 setDemodBuf(BitStream,64,idx);
b4fb11ba 1262 if (g_debugMode){
ec75f5c1 1263 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing demod buffer:",idx,64);
1264 printDemodBuff();
1265 }
b4fb11ba 1266 return 1;
b3b70669 1267}
1e090a61 1268
1e090a61 1269//by marshmellow
1270//AWID Prox demod - FSK RF/50 with preamble of 00000001 (always a 96 bit data stream)
1271//print full AWID Prox ID and some bit format details if found
1272int CmdFSKdemodAWID(const char *Cmd)
1273{
1274
ec75f5c1 1275 //int verbose=1;
1276 //sscanf(Cmd, "%i", &verbose);
1e090a61 1277
1278 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1279 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
1280 size_t size = getFromGraphBuf(BitStream);
e770c648 1281 if (size==0) return 0;
1e090a61 1282
1283 //get binary from fsk wave
a1d17964 1284 int idx = AWIDdemodFSK(BitStream, &size);
1e090a61 1285 if (idx<=0){
ec75f5c1 1286 if (g_debugMode==1){
1e090a61 1287 if (idx == -1)
ec75f5c1 1288 PrintAndLog("DEBUG: Error - not enough samples");
1e090a61 1289 else if (idx == -2)
a1d17964 1290 PrintAndLog("DEBUG: Error - only noise found");
1e090a61 1291 else if (idx == -3)
ec75f5c1 1292 PrintAndLog("DEBUG: Error - problem during FSK demod");
1e090a61 1293 else if (idx == -4)
ec75f5c1 1294 PrintAndLog("DEBUG: Error - AWID preamble not found");
1295 else if (idx == -5)
a1d17964 1296 PrintAndLog("DEBUG: Error - Size not correct: %d", size);
ec75f5c1 1297 else
1298 PrintAndLog("DEBUG: Error %d",idx);
1e090a61 1299 }
1300 return 0;
1301 }
1302
1303 // Index map
1304 // 0 10 20 30 40 50 60
1305 // | | | | | | |
1306 // 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
1307 // -----------------------------------------------------------------------------
1308 // 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
1309 // 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
1310 // |---26 bit---| |-----117----||-------------142-------------|
1311 // b = format bit len, o = odd parity of last 3 bits
1312 // f = facility code, c = card number
1313 // w = wiegand parity
1314 // (26 bit format shown)
1315
1316 //get raw ID before removing parities
1317 uint32_t rawLo = bytebits_to_byte(BitStream+idx+64,32);
1318 uint32_t rawHi = bytebits_to_byte(BitStream+idx+32,32);
1319 uint32_t rawHi2 = bytebits_to_byte(BitStream+idx,32);
ec75f5c1 1320 setDemodBuf(BitStream,96,idx);
1321
1e090a61 1322 size = removeParity(BitStream, idx+8, 4, 1, 88);
1323 if (size != 66){
ec75f5c1 1324 if (g_debugMode==1) PrintAndLog("DEBUG: Error - at parity check-tag size does not match AWID format");
1e090a61 1325 return 0;
1326 }
1327 // ok valid card found!
1328
1329 // Index map
1330 // 0 10 20 30 40 50 60
1331 // | | | | | | |
1332 // 01234567 8 90123456 7890123456789012 3 456789012345678901234567890123456
1333 // -----------------------------------------------------------------------------
1334 // 00011010 1 01110101 0000000010001110 1 000000000000000000000000000000000
1335 // bbbbbbbb w ffffffff cccccccccccccccc w xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1336 // |26 bit| |-117--| |-----142------|
1337 // b = format bit len, o = odd parity of last 3 bits
1338 // f = facility code, c = card number
1339 // w = wiegand parity
1340 // (26 bit format shown)
1341
1342 uint32_t fc = 0;
1343 uint32_t cardnum = 0;
1344 uint32_t code1 = 0;
1345 uint32_t code2 = 0;
1346 uint8_t fmtLen = bytebits_to_byte(BitStream,8);
1347 if (fmtLen==26){
1348 fc = bytebits_to_byte(BitStream+9, 8);
1349 cardnum = bytebits_to_byte(BitStream+17, 16);
1350 code1 = bytebits_to_byte(BitStream+8,fmtLen);
abd6112f 1351 PrintAndLog("AWID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %08x%08x%08x", fmtLen, fc, cardnum, code1, rawHi2, rawHi, rawLo);
1e090a61 1352 } else {
1353 cardnum = bytebits_to_byte(BitStream+8+(fmtLen-17), 16);
1354 if (fmtLen>32){
1355 code1 = bytebits_to_byte(BitStream+8,fmtLen-32);
1356 code2 = bytebits_to_byte(BitStream+8+(fmtLen-32),32);
abd6112f 1357 PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x%08x, Raw: %08x%08x%08x", fmtLen, cardnum, code1, code2, rawHi2, rawHi, rawLo);
1e090a61 1358 } else{
1359 code1 = bytebits_to_byte(BitStream+8,fmtLen);
abd6112f 1360 PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x, Raw: %08x%08x%08x", fmtLen, cardnum, code1, rawHi2, rawHi, rawLo);
1e090a61 1361 }
1362 }
ec75f5c1 1363 if (g_debugMode){
1364 PrintAndLog("DEBUG: idx: %d, Len: %d Printing Demod Buffer:", idx, 96);
1365 printDemodBuff();
1366 }
1e090a61 1367 //todo - convert hi2, hi, lo to demodbuffer for future sim/clone commands
1e090a61 1368 return 1;
1369}
1370
1371//by marshmellow
1372//Pyramid Prox demod - FSK RF/50 with preamble of 0000000000000001 (always a 128 bit data stream)
1373//print full Farpointe Data/Pyramid Prox ID and some bit format details if found
1374int CmdFSKdemodPyramid(const char *Cmd)
1375{
1e090a61 1376 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
1377 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
1378 size_t size = getFromGraphBuf(BitStream);
e770c648 1379 if (size==0) return 0;
1e090a61 1380
1381 //get binary from fsk wave
a1d17964 1382 int idx = PyramiddemodFSK(BitStream, &size);
1e090a61 1383 if (idx < 0){
ec75f5c1 1384 if (g_debugMode==1){
1e090a61 1385 if (idx == -5)
ec75f5c1 1386 PrintAndLog("DEBUG: Error - not enough samples");
1e090a61 1387 else if (idx == -1)
a1d17964 1388 PrintAndLog("DEBUG: Error - only noise found");
1e090a61 1389 else if (idx == -2)
ec75f5c1 1390 PrintAndLog("DEBUG: Error - problem during FSK demod");
1391 else if (idx == -3)
a1d17964 1392 PrintAndLog("DEBUG: Error - Size not correct: %d", size);
1e090a61 1393 else if (idx == -4)
ec75f5c1 1394 PrintAndLog("DEBUG: Error - Pyramid preamble not found");
1395 else
1396 PrintAndLog("DEBUG: Error - idx: %d",idx);
1e090a61 1397 }
1e090a61 1398 return 0;
1399 }
1e090a61 1400 // Index map
1401 // 0 10 20 30 40 50 60
1402 // | | | | | | |
1403 // 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3
1404 // -----------------------------------------------------------------------------
1405 // 0000000 0 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1
1406 // premable xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o
1407
1408 // 64 70 80 90 100 110 120
1409 // | | | | | | |
1410 // 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7
1411 // -----------------------------------------------------------------------------
1412 // 0000000 1 0000000 1 0000000 1 0110111 0 0011000 1 0000001 0 0001100 1 1001010 0
1413 // xxxxxxx o xxxxxxx o xxxxxxx o xswffff o ffffccc o ccccccc o ccccccw o ppppppp o
1414 // |---115---||---------71---------|
1415 // s = format start bit, o = odd parity of last 7 bits
1416 // f = facility code, c = card number
1417 // w = wiegand parity, x = extra space for other formats
1418 // p = unknown checksum
1419 // (26 bit format shown)
1420
1421 //get raw ID before removing parities
1422 uint32_t rawLo = bytebits_to_byte(BitStream+idx+96,32);
1423 uint32_t rawHi = bytebits_to_byte(BitStream+idx+64,32);
1424 uint32_t rawHi2 = bytebits_to_byte(BitStream+idx+32,32);
1425 uint32_t rawHi3 = bytebits_to_byte(BitStream+idx,32);
ec75f5c1 1426 setDemodBuf(BitStream,128,idx);
1427
1e090a61 1428 size = removeParity(BitStream, idx+8, 8, 1, 120);
1429 if (size != 105){
ec75f5c1 1430 if (g_debugMode==1) PrintAndLog("DEBUG: Error at parity check-tag size does not match Pyramid format, SIZE: %d, IDX: %d, hi3: %x",size, idx, rawHi3);
1e090a61 1431 return 0;
1432 }
1433
1434 // ok valid card found!
1435
1436 // Index map
1437 // 0 10 20 30 40 50 60 70
1438 // | | | | | | | |
1439 // 01234567890123456789012345678901234567890123456789012345678901234567890
1440 // -----------------------------------------------------------------------
1441 // 00000000000000000000000000000000000000000000000000000000000000000000000
1442 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1443
1444 // 71 80 90 100
1445 // | | | |
1446 // 1 2 34567890 1234567890123456 7 8901234
1447 // ---------------------------------------
1448 // 1 1 01110011 0000000001000110 0 1001010
1449 // s w ffffffff cccccccccccccccc w ppppppp
1450 // |--115-| |------71------|
1451 // s = format start bit, o = odd parity of last 7 bits
1452 // f = facility code, c = card number
1453 // w = wiegand parity, x = extra space for other formats
1454 // p = unknown checksum
1455 // (26 bit format shown)
1456
1457 //find start bit to get fmtLen
1e090a61 1458 int j;
1459 for (j=0; j<size; j++){
ec75f5c1 1460 if(BitStream[j]) break;
1e090a61 1461 }
1462 uint8_t fmtLen = size-j-8;
1463 uint32_t fc = 0;
1464 uint32_t cardnum = 0;
1465 uint32_t code1 = 0;
1466 //uint32_t code2 = 0;
1467 if (fmtLen==26){
1468 fc = bytebits_to_byte(BitStream+73, 8);
1469 cardnum = bytebits_to_byte(BitStream+81, 16);
1470 code1 = bytebits_to_byte(BitStream+72,fmtLen);
abd6112f 1471 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);
1e090a61 1472 } else if (fmtLen==45){
1473 fmtLen=42; //end = 10 bits not 7 like 26 bit fmt
1474 fc = bytebits_to_byte(BitStream+53, 10);
1475 cardnum = bytebits_to_byte(BitStream+63, 32);
abd6112f 1476 PrintAndLog("Pyramid ID Found - BitLength: %d, FC: %d, Card: %d - Raw: %08x%08x%08x%08x", fmtLen, fc, cardnum, rawHi3, rawHi2, rawHi, rawLo);
1e090a61 1477 } else {
1478 cardnum = bytebits_to_byte(BitStream+81, 16);
1479 if (fmtLen>32){
1480 //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen-32);
1481 //code2 = bytebits_to_byte(BitStream+(size-32),32);
abd6112f 1482 PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %08x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo);
1e090a61 1483 } else{
1484 //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen);
abd6112f 1485 PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %08x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo);
1e090a61 1486 }
1487 }
ec75f5c1 1488 if (g_debugMode){
1489 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128);
1490 printDemodBuff();
1491 }
1e090a61 1492 return 1;
1493}
1494
e888ed8e 1495int CmdFSKdemod(const char *Cmd) //old CmdFSKdemod needs updating
7fe9b0b7 1496{
1497 static const int LowTone[] = {
1498 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1499 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1500 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1501 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1502 1, 1, 1, 1, 1, -1, -1, -1, -1, -1
1503 };
1504 static const int HighTone[] = {
1505 1, 1, 1, 1, 1, -1, -1, -1, -1,
1506 1, 1, 1, 1, -1, -1, -1, -1,
1507 1, 1, 1, 1, -1, -1, -1, -1,
1508 1, 1, 1, 1, -1, -1, -1, -1,
1509 1, 1, 1, 1, -1, -1, -1, -1,
1510 1, 1, 1, 1, -1, -1, -1, -1, -1,
1511 };
1512
1513 int lowLen = sizeof (LowTone) / sizeof (int);
1514 int highLen = sizeof (HighTone) / sizeof (int);
b915fda3 1515 int convLen = (highLen > lowLen) ? highLen : lowLen;
7fe9b0b7 1516 uint32_t hi = 0, lo = 0;
1517
1518 int i, j;
1519 int minMark = 0, maxMark = 0;
952a8bb5 1520
7fe9b0b7 1521 for (i = 0; i < GraphTraceLen - convLen; ++i) {
1522 int lowSum = 0, highSum = 0;
1523
1524 for (j = 0; j < lowLen; ++j) {
1525 lowSum += LowTone[j]*GraphBuffer[i+j];
1526 }
1527 for (j = 0; j < highLen; ++j) {
1528 highSum += HighTone[j] * GraphBuffer[i + j];
1529 }
1530 lowSum = abs(100 * lowSum / lowLen);
1531 highSum = abs(100 * highSum / highLen);
1532 GraphBuffer[i] = (highSum << 16) | lowSum;
1533 }
1534
1535 for(i = 0; i < GraphTraceLen - convLen - 16; ++i) {
1536 int lowTot = 0, highTot = 0;
1537 // 10 and 8 are f_s divided by f_l and f_h, rounded
1538 for (j = 0; j < 10; ++j) {
1539 lowTot += (GraphBuffer[i+j] & 0xffff);
1540 }
1541 for (j = 0; j < 8; j++) {
1542 highTot += (GraphBuffer[i + j] >> 16);
1543 }
1544 GraphBuffer[i] = lowTot - highTot;
1545 if (GraphBuffer[i] > maxMark) maxMark = GraphBuffer[i];
1546 if (GraphBuffer[i] < minMark) minMark = GraphBuffer[i];
1547 }
1548
1549 GraphTraceLen -= (convLen + 16);
1550 RepaintGraphWindow();
1551
b3b70669 1552 // Find bit-sync (3 lo followed by 3 high) (HID ONLY)
7fe9b0b7 1553 int max = 0, maxPos = 0;
1554 for (i = 0; i < 6000; ++i) {
1555 int dec = 0;
1556 for (j = 0; j < 3 * lowLen; ++j) {
1557 dec -= GraphBuffer[i + j];
1558 }
1559 for (; j < 3 * (lowLen + highLen ); ++j) {
1560 dec += GraphBuffer[i + j];
1561 }
1562 if (dec > max) {
1563 max = dec;
1564 maxPos = i;
1565 }
1566 }
1567
1568 // place start of bit sync marker in graph
1569 GraphBuffer[maxPos] = maxMark;
1570 GraphBuffer[maxPos + 1] = minMark;
1571
1572 maxPos += j;
1573
1574 // place end of bit sync marker in graph
1575 GraphBuffer[maxPos] = maxMark;
1576 GraphBuffer[maxPos+1] = minMark;
1577
1578 PrintAndLog("actual data bits start at sample %d", maxPos);
1579 PrintAndLog("length %d/%d", highLen, lowLen);
ba1a299c 1580
a1557c4c 1581 uint8_t bits[46] = {0x00};
ba1a299c 1582
7fe9b0b7 1583 // find bit pairs and manchester decode them
1584 for (i = 0; i < arraylen(bits) - 1; ++i) {
1585 int dec = 0;
1586 for (j = 0; j < lowLen; ++j) {
1587 dec -= GraphBuffer[maxPos + j];
1588 }
1589 for (; j < lowLen + highLen; ++j) {
1590 dec += GraphBuffer[maxPos + j];
1591 }
1592 maxPos += j;
1593 // place inter bit marker in graph
1594 GraphBuffer[maxPos] = maxMark;
1595 GraphBuffer[maxPos + 1] = minMark;
1596
1597 // hi and lo form a 64 bit pair
1598 hi = (hi << 1) | (lo >> 31);
1599 lo = (lo << 1);
1600 // store decoded bit as binary (in hi/lo) and text (in bits[])
1601 if(dec < 0) {
1602 bits[i] = '1';
1603 lo |= 1;
1604 } else {
1605 bits[i] = '0';
1606 }
1607 }
1608 PrintAndLog("bits: '%s'", bits);
1609 PrintAndLog("hex: %08x %08x", hi, lo);
1610 return 0;
1611}
e888ed8e 1612
e770c648 1613//by marshmellow
1614//attempt to psk1 demod graph buffer
4ac906d1 1615int PSKDemod(const char *Cmd, bool verbose)
e770c648 1616{
1617 int invert=0;
1618 int clk=0;
1619 int maxErr=100;
1620 sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
1621 if (clk==1){
1622 invert=1;
1623 clk=0;
1624 }
1625 if (invert != 0 && invert != 1) {
4ac906d1 1626 if (verbose) PrintAndLog("Invalid argument: %s", Cmd);
e770c648 1627 return -1;
1628 }
1629 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
1630 size_t BitLen = getFromGraphBuf(BitStream);
3f41e73c 1631 if (BitLen==0) return -1;
1632 uint8_t carrier=countPSK_FC(BitStream, BitLen);
ec9882b1 1633 if (carrier!=2 && carrier!=4 && carrier!=8){
3f41e73c 1634 //invalid carrier
ec9882b1 1635 return -1;
3f41e73c 1636 }
e770c648 1637 int errCnt=0;
3f41e73c 1638 errCnt = pskRawDemod(BitStream, &BitLen, &clk, &invert);
e770c648 1639 if (errCnt > maxErr){
4ac906d1 1640 if (g_debugMode==1 && verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
e770c648 1641 return -1;
1642 }
1643 if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
4ac906d1 1644 if (g_debugMode==1 && verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
e770c648 1645 return -1;
1646 }
1647 if (verbose) PrintAndLog("Tried PSK Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
1648 //prime demod buffer for output
1649 setDemodBuf(BitStream,BitLen,0);
1650 return errCnt;
4118b74d 1651}
e770c648 1652
4118b74d 1653// Indala 26 bit decode
1654// by marshmellow
1655// optional arguments - same as CmdpskNRZrawDemod (clock & invert)
1656int CmdIndalaDecode(const char *Cmd)
1657{
b4fb11ba 1658 int ans;
1659 if (strlen(Cmd)>0){
1660 ans = PSKDemod(Cmd, 0);
1661 } else{ //default to RF/32
1662 ans = PSKDemod("32", 0);
1663 }
4118b74d 1664
ba1a299c 1665 if (ans < 0){
ec75f5c1 1666 if (g_debugMode==1)
b4fb11ba 1667 PrintAndLog("Error1: %d",ans);
ba1a299c 1668 return 0;
1669 }
1670 uint8_t invert=0;
1671 ans = indala26decode(DemodBuffer,(size_t *) &DemodBufferLen, &invert);
1672 if (ans < 1) {
ec75f5c1 1673 if (g_debugMode==1)
b4fb11ba 1674 PrintAndLog("Error2: %d",ans);
ba1a299c 1675 return -1;
1676 }
a1d17964 1677 char showbits[251]={0x00};
84871873 1678 if (invert)
b4fb11ba 1679 if (g_debugMode==1)
1680 PrintAndLog("Had to invert bits");
ec75f5c1 1681
ba1a299c 1682 //convert UID to HEX
1683 uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
1684 int idx;
1685 uid1=0;
1686 uid2=0;
1687 PrintAndLog("BitLen: %d",DemodBufferLen);
1688 if (DemodBufferLen==64){
1689 for( idx=0; idx<64; idx++) {
1690 uid1=(uid1<<1)|(uid2>>31);
1691 if (DemodBuffer[idx] == 0) {
1692 uid2=(uid2<<1)|0;
1693 showbits[idx]='0';
1694 } else {
1695 uid2=(uid2<<1)|1;
1696 showbits[idx]='1';
1697 }
1698 }
1699 showbits[idx]='\0';
1700 PrintAndLog("Indala UID=%s (%x%08x)", showbits, uid1, uid2);
1701 }
1702 else {
1703 uid3=0;
1704 uid4=0;
1705 uid5=0;
1706 uid6=0;
1707 uid7=0;
1708 for( idx=0; idx<DemodBufferLen; idx++) {
1709 uid1=(uid1<<1)|(uid2>>31);
1710 uid2=(uid2<<1)|(uid3>>31);
1711 uid3=(uid3<<1)|(uid4>>31);
1712 uid4=(uid4<<1)|(uid5>>31);
1713 uid5=(uid5<<1)|(uid6>>31);
1714 uid6=(uid6<<1)|(uid7>>31);
1715 if (DemodBuffer[idx] == 0) {
1716 uid7=(uid7<<1)|0;
1717 showbits[idx]='0';
1718 }
1719 else {
1720 uid7=(uid7<<1)|1;
1721 showbits[idx]='1';
1722 }
1723 }
1724 showbits[idx]='\0';
1725 PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
1726 }
b4fb11ba 1727 if (g_debugMode){
1728 PrintAndLog("DEBUG: printing demodbuffer:");
1729 printDemodBuff();
1730 }
ba1a299c 1731 return 1;
4118b74d 1732}
1733
04d2721b 1734// by marshmellow
e770c648 1735// takes 3 arguments - clock, invert, maxErr as integers
1736// attempts to demodulate nrz only
1737// prints binary found and saves in demodbuffer for further commands
b4a2fcf6 1738
1739int NRZrawDemod(const char *Cmd, bool verbose)
e770c648 1740{
1741 int invert=0;
1742 int clk=0;
1743 int maxErr=100;
e770c648 1744 sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
1745 if (clk==1){
1746 invert=1;
1747 clk=0;
1748 }
1749 if (invert != 0 && invert != 1) {
1750 PrintAndLog("Invalid argument: %s", Cmd);
1751 return 0;
1752 }
1753 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
1754 size_t BitLen = getFromGraphBuf(BitStream);
1755 if (BitLen==0) return 0;
1756 int errCnt=0;
1757 errCnt = nrzRawDemod(BitStream, &BitLen, &clk, &invert, maxErr);
1758 if (errCnt > maxErr){
b4a2fcf6 1759 if (g_debugMode==1 && verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
e770c648 1760 return 0;
1761 }
1762 if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
b4a2fcf6 1763 if (g_debugMode==1 && verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
e770c648 1764 return 0;
1765 }
1766 PrintAndLog("Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
1767 //prime demod buffer for output
1768 setDemodBuf(BitStream,BitLen,0);
1769
b4a2fcf6 1770 if (errCnt>0 && verbose){
e770c648 1771 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
e770c648 1772 }
b4a2fcf6 1773 if (verbose) {
1774 PrintAndLog("NRZ demoded bitstream:");
1775 // Now output the bitstream to the scrollback by line of 16 bits
1776 printDemodBuff();
1777 }
1778 return 1;
1779}
1780
1781int CmdNRZrawDemod(const char *Cmd)
1782{
1783 char cmdp = param_getchar(Cmd, 0);
1784 if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
1785 PrintAndLog("Usage: data rawdemod nr [clock] <0|1> [maxError]");
1786 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
1787 PrintAndLog(" <invert>, 1 for invert output");
1788 PrintAndLog(" [set maximum allowed errors], default = 100.");
1789 PrintAndLog("");
d66d4843 1790 PrintAndLog(" sample: data rawdemod nr = demod a nrz/direct tag from GraphBuffer");
1791 PrintAndLog(" : data rawdemod nr 32 = demod a nrz/direct tag from GraphBuffer using a clock of RF/32");
1792 PrintAndLog(" : data rawdemod nr 32 1 = demod a nrz/direct tag from GraphBuffer using a clock of RF/32 and inverting data");
1793 PrintAndLog(" : data rawdemod nr 1 = demod a nrz/direct tag from GraphBuffer while inverting data");
1794 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");
b4a2fcf6 1795 return 0;
1796 }
1797 return NRZrawDemod(Cmd, TRUE);
e770c648 1798}
1799
1800// by marshmellow
1801// takes 3 arguments - clock, invert, maxErr as integers
04d2721b 1802// attempts to demodulate psk only
1803// prints binary found and saves in demodbuffer for further commands
e770c648 1804int CmdPSK1rawDemod(const char *Cmd)
4118b74d 1805{
84871873 1806 int errCnt;
e770c648 1807 char cmdp = param_getchar(Cmd, 0);
6de43508 1808 if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
19ff0210 1809 PrintAndLog("Usage: data rawdemod p1 [clock] <0|1> [maxError]");
e770c648 1810 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
1811 PrintAndLog(" <invert>, 1 for invert output");
1812 PrintAndLog(" [set maximum allowed errors], default = 100.");
1813 PrintAndLog("");
d66d4843 1814 PrintAndLog(" sample: data rawdemod p1 = demod a psk1 tag from GraphBuffer");
1815 PrintAndLog(" : data rawdemod p1 32 = demod a psk1 tag from GraphBuffer using a clock of RF/32");
1816 PrintAndLog(" : data rawdemod p1 32 1 = demod a psk1 tag from GraphBuffer using a clock of RF/32 and inverting data");
1817 PrintAndLog(" : data rawdemod p1 1 = demod a psk1 tag from GraphBuffer while inverting data");
1818 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");
e770c648 1819 return 0;
1820 }
4ac906d1 1821 errCnt = PSKDemod(Cmd, TRUE);
b4fb11ba 1822 //output
1823 if (errCnt<0){
e770c648 1824 if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);
ec75f5c1 1825 return 0;
e770c648 1826 }
b4fb11ba 1827 if (errCnt>0){
e770c648 1828 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
ec75f5c1 1829 }
e770c648 1830 PrintAndLog("PSK demoded bitstream:");
1831 // Now output the bitstream to the scrollback by line of 16 bits
1832 printDemodBuff();
1833 return 1;
4118b74d 1834}
1835
04d2721b 1836// by marshmellow
19ff0210 1837// takes same args as cmdpsk1rawdemod
04d2721b 1838int CmdPSK2rawDemod(const char *Cmd)
1839{
1840 int errCnt=0;
e770c648 1841 char cmdp = param_getchar(Cmd, 0);
6de43508 1842 if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
19ff0210 1843 PrintAndLog("Usage: data rawdemod p2 [clock] <0|1> [maxError]");
e770c648 1844 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
1845 PrintAndLog(" <invert>, 1 for invert output");
1846 PrintAndLog(" [set maximum allowed errors], default = 100.");
1847 PrintAndLog("");
d66d4843 1848 PrintAndLog(" sample: data rawdemod p2 = demod a psk2 tag from GraphBuffer, autodetect clock");
1849 PrintAndLog(" : data rawdemod p2 32 = demod a psk2 tag from GraphBuffer using a clock of RF/32");
1850 PrintAndLog(" : data rawdemod p2 32 1 = demod a psk2 tag from GraphBuffer using a clock of RF/32 and inverting output");
1851 PrintAndLog(" : data rawdemod p2 1 = demod a psk2 tag from GraphBuffer, autodetect clock and invert output");
1852 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");
e770c648 1853 return 0;
1854 }
3f41e73c 1855 errCnt=PSKDemod(Cmd, TRUE);
04d2721b 1856 if (errCnt<0){
1857 if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);
1858 return 0;
1859 }
1860 psk1TOpsk2(DemodBuffer, DemodBufferLen);
1861 if (errCnt>0){
1862 if (g_debugMode){
1863 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
1864 PrintAndLog("PSK2 demoded bitstream:");
1865 // Now output the bitstream to the scrollback by line of 16 bits
1866 printDemodBuff();
1867 }
1868 }else{
1869 PrintAndLog("PSK2 demoded bitstream:");
1870 // Now output the bitstream to the scrollback by line of 16 bits
1871 printDemodBuff();
1872 }
1873 return 1;
1874}
1875
19ff0210 1876// by marshmellow - combines all raw demod functions into one menu command
1877int CmdRawDemod(const char *Cmd)
1878{
1879 char cmdp = Cmd[0]; //param_getchar(Cmd, 0);
1880
1881 if (strlen(Cmd) > 14 || cmdp == 'h' || cmdp == 'H' || strlen(Cmd)<2) {
1882 PrintAndLog("Usage: data rawdemod [modulation] <help>|<options>");
1883 PrintAndLog(" [modulation] as 2 char, 'am' for ask/manchester, 'ar' for ask/raw, 'fs' for fsk, 'nr' for nrz/direct, 'p1' for psk1, 'p2' for psk2");
1884 PrintAndLog(" <help> as 'h', prints the help for the specific modulation");
1885 PrintAndLog(" <options> see specific modulation help for optional parameters");
1886 PrintAndLog("");
1887 PrintAndLog(" sample: data rawdemod fs h = print help for ask/raw demod");
1888 PrintAndLog(" : data rawdemod fs = demod GraphBuffer using: fsk - autodetect");
1889 PrintAndLog(" : data rawdemod am = demod GraphBuffer using: ask/manchester - autodetect");
1890 PrintAndLog(" : data rawdemod ar = demod GraphBuffer using: ask/raw - autodetect");
1891 PrintAndLog(" : data rawdemod nr = demod GraphBuffer using: nrz/direct - autodetect");
1892 PrintAndLog(" : data rawdemod p1 = demod GraphBuffer using: psk1 - autodetect");
1893 PrintAndLog(" : data rawdemod p2 = demod GraphBuffer using: psk2 - autodetect");
1894 return 0;
1895 }
1896 char cmdp2 = Cmd[1];
1897 int ans = 0;
1898 if (cmdp == 'f' && cmdp2 == 's'){
1899 ans = CmdFSKrawdemod(Cmd+3);
1900 } else if(cmdp == 'a' && cmdp2 == 'm'){
1901 ans = Cmdaskmandemod(Cmd+3);
1902 } else if(cmdp == 'a' && cmdp2 == 'r'){
1903 ans = Cmdaskrawdemod(Cmd+3);
1904 } else if(cmdp == 'n' && cmdp2 == 'r'){
1905 ans = CmdNRZrawDemod(Cmd+3);
1906 } else if(cmdp == 'p' && cmdp2 == '1'){
1907 ans = CmdPSK1rawDemod(Cmd+3);
1908 } else if(cmdp == 'p' && cmdp2 == '2'){
1909 ans = CmdPSK2rawDemod(Cmd+3);
1910 } else {
1911 PrintAndLog("unknown modulation entered - see help ('h') for parameter structure");
1912 }
1913 return ans;
1914}
1915
7fe9b0b7 1916int CmdGrid(const char *Cmd)
1917{
1918 sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY);
7ddb9900 1919 PlotGridXdefault= PlotGridX;
1920 PlotGridYdefault= PlotGridY;
7fe9b0b7 1921 RepaintGraphWindow();
1922 return 0;
1923}
1924
1925int CmdHexsamples(const char *Cmd)
1926{
4961e292 1927 int i, j;
7fe9b0b7 1928 int requested = 0;
1929 int offset = 0;
4961e292 1930 char string_buf[25];
1931 char* string_ptr = string_buf;
f71f4deb 1932 uint8_t got[BIGBUF_SIZE];
952a8bb5 1933
4961e292 1934 sscanf(Cmd, "%i %i", &requested, &offset);
90d74dc2 1935
4961e292 1936 /* if no args send something */
1937 if (requested == 0) {
90d74dc2 1938 requested = 8;
1939 }
90d74dc2 1940 if (offset + requested > sizeof(got)) {
f71f4deb 1941 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > %d", BIGBUF_SIZE);
4961e292 1942 return 0;
952a8bb5 1943 }
90d74dc2 1944
4961e292 1945 GetFromBigBuf(got,requested,offset);
90d74dc2 1946 WaitForResponse(CMD_ACK,NULL);
1947
4961e292 1948 i = 0;
1949 for (j = 0; j < requested; j++) {
1950 i++;
1951 string_ptr += sprintf(string_ptr, "%02x ", got[j]);
1952 if (i == 8) {
1953 *(string_ptr - 1) = '\0'; // remove the trailing space
1954 PrintAndLog("%s", string_buf);
1955 string_buf[0] = '\0';
1956 string_ptr = string_buf;
1957 i = 0;
1958 }
1959 if (j == requested - 1 && string_buf[0] != '\0') { // print any remaining bytes
1960 *(string_ptr - 1) = '\0';
1961 PrintAndLog("%s", string_buf);
1962 string_buf[0] = '\0';
b4fb11ba 1963 }
7fe9b0b7 1964 }
1965 return 0;
1966}
1967
7fe9b0b7 1968int CmdHide(const char *Cmd)
1969{
1970 HideGraphWindow();
1971 return 0;
1972}
1973
78f5b1a7 1974//zero mean GraphBuffer
7fe9b0b7 1975int CmdHpf(const char *Cmd)
1976{
1977 int i;
1978 int accum = 0;
1979
1980 for (i = 10; i < GraphTraceLen; ++i)
1981 accum += GraphBuffer[i];
1982 accum /= (GraphTraceLen - 10);
1983 for (i = 0; i < GraphTraceLen; ++i)
1984 GraphBuffer[i] -= accum;
1985
1986 RepaintGraphWindow();
1987 return 0;
1988}
f6d9fb17
MHS
1989typedef struct {
1990 uint8_t * buffer;
1991 uint32_t numbits;
1992 uint32_t position;
1993}BitstreamOut;
1994
1995bool _headBit( BitstreamOut *stream)
1996{
1997 int bytepos = stream->position >> 3; // divide by 8
1998 int bitpos = (stream->position++) & 7; // mask out 00000111
1999 return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
2000}
2001
2002uint8_t getByte(uint8_t bits_per_sample, BitstreamOut* b)
2003{
2004 int i;
2005 uint8_t val = 0;
2006 for(i =0 ; i < bits_per_sample; i++)
2007 {
2008 val |= (_headBit(b) << (7-i));
2009 }
2010 return val;
2011}
7fe9b0b7 2012
8d183c53 2013int CmdSamples(const char *Cmd)
7fe9b0b7 2014{
31abe49f
MHS
2015 //If we get all but the last byte in bigbuf,
2016 // we don't have to worry about remaining trash
2017 // in the last byte in case the bits-per-sample
2018 // does not line up on byte boundaries
0644d5e3 2019 uint8_t got[BIGBUF_SIZE-1] = { 0 };
ba1a299c 2020
d6d20c54 2021 int n = strtol(Cmd, NULL, 0);
2022 if (n == 0)
31abe49f 2023 n = sizeof(got);
d6d20c54 2024
2025 if (n > sizeof(got))
2026 n = sizeof(got);
ba1a299c 2027
f6d9fb17 2028 PrintAndLog("Reading %d bytes from device memory\n", n);
f71f4deb 2029 GetFromBigBuf(got,n,0);
f6d9fb17
MHS
2030 PrintAndLog("Data fetched");
2031 UsbCommand response;
2032 WaitForResponse(CMD_ACK, &response);
31abe49f
MHS
2033 uint8_t bits_per_sample = 8;
2034
2035 //Old devices without this feature would send 0 at arg[0]
2036 if(response.arg[0] > 0)
2037 {
2038 sample_config *sc = (sample_config *) response.d.asBytes;
2039 PrintAndLog("Samples @ %d bits/smpl, decimation 1:%d ", sc->bits_per_sample
2040 , sc->decimation);
2041 bits_per_sample = sc->bits_per_sample;
f71f4deb 2042 }
f6d9fb17
MHS
2043 if(bits_per_sample < 8)
2044 {
2045 PrintAndLog("Unpacking...");
2046 BitstreamOut bout = { got, bits_per_sample * n, 0};
2047 int j =0;
31abe49f 2048 for (j = 0; j * bits_per_sample < n * 8 && j < sizeof(GraphBuffer); j++) {
f6d9fb17
MHS
2049 uint8_t sample = getByte(bits_per_sample, &bout);
2050 GraphBuffer[j] = ((int) sample )- 128;
2051 }
2052 GraphTraceLen = j;
2053 PrintAndLog("Unpacked %d samples" , j );
2054 }else
2055 {
2056 for (int j = 0; j < n; j++) {
2057 GraphBuffer[j] = ((int)got[j]) - 128;
2058 }
2059 GraphTraceLen = n;
f6d9fb17
MHS
2060 }
2061
f71f4deb 2062 RepaintGraphWindow();
2063 return 0;
7fe9b0b7 2064}
2065
d6a120a2
MHS
2066int CmdTuneSamples(const char *Cmd)
2067{
3aa4014b 2068 int timeout = 0;
2069 printf("\nMeasuring antenna characteristics, please wait...");
ba1a299c 2070
3aa4014b 2071 UsbCommand c = {CMD_MEASURE_ANTENNA_TUNING};
2072 SendCommand(&c);
2073
2074 UsbCommand resp;
2075 while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING,&resp,1000)) {
2076 timeout++;
2077 printf(".");
2078 if (timeout > 7) {
2079 PrintAndLog("\nNo response from Proxmark. Aborting...");
2080 return 1;
2081 }
2082 }
2083
2084 int peakv, peakf;
2085 int vLf125, vLf134, vHf;
2086 vLf125 = resp.arg[0] & 0xffff;
2087 vLf134 = resp.arg[0] >> 16;
2088 vHf = resp.arg[1] & 0xffff;;
2089 peakf = resp.arg[2] & 0xffff;
2090 peakv = resp.arg[2] >> 16;
2091 PrintAndLog("");
2092 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0);
2093 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0);
2094 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1));
2095 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0);
0d0d0499 2096
2097#define LF_UNUSABLE_V 2948 // was 2000. Changed due to bugfix in voltage measurements. LF results are now 47% higher.
2098#define LF_MARGINAL_V 14739 // was 10000. Changed due to bugfix bug in voltage measurements. LF results are now 47% higher.
2099#define HF_UNUSABLE_V 3167 // was 2000. Changed due to bugfix in voltage measurements. HF results are now 58% higher.
2100#define HF_MARGINAL_V 7917 // was 5000. Changed due to bugfix in voltage measurements. HF results are now 58% higher.
2101
2102 if (peakv < LF_UNUSABLE_V)
3aa4014b 2103 PrintAndLog("# Your LF antenna is unusable.");
0d0d0499 2104 else if (peakv < LF_MARGINAL_V)
3aa4014b 2105 PrintAndLog("# Your LF antenna is marginal.");
0d0d0499 2106 if (vHf < HF_UNUSABLE_V)
3aa4014b 2107 PrintAndLog("# Your HF antenna is unusable.");
0d0d0499 2108 else if (vHf < HF_MARGINAL_V)
3aa4014b 2109 PrintAndLog("# Your HF antenna is marginal.");
2110
0d0d0499 2111 if (peakv >= LF_UNUSABLE_V) {
2112 for (int i = 0; i < 256; i++) {
2113 GraphBuffer[i] = resp.d.asBytes[i] - 128;
2114 }
2115 PrintAndLog("Displaying LF tuning graph. Divisor 89 is 134khz, 95 is 125khz.\n");
2116 PrintAndLog("\n");
2117 GraphTraceLen = 256;
2118 ShowGraphWindow();
abd16c45 2119 RepaintGraphWindow();
ba1a299c 2120 }
2121
b4fb11ba 2122 return 0;
d6a120a2
MHS
2123}
2124
3aa4014b 2125
7fe9b0b7 2126int CmdLoad(const char *Cmd)
2127{
ec75f5c1 2128 char filename[FILE_PATH_SIZE] = {0x00};
2129 int len = 0;
b915fda3 2130
ec75f5c1 2131 len = strlen(Cmd);
2132 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
2133 memcpy(filename, Cmd, len);
b915fda3 2134
ec75f5c1 2135 FILE *f = fopen(filename, "r");
7fe9b0b7 2136 if (!f) {
b915fda3 2137 PrintAndLog("couldn't open '%s'", filename);
7fe9b0b7 2138 return 0;
2139 }
2140
2141 GraphTraceLen = 0;
2142 char line[80];
2143 while (fgets(line, sizeof (line), f)) {
2144 GraphBuffer[GraphTraceLen] = atoi(line);
2145 GraphTraceLen++;
2146 }
2147 fclose(f);
2148 PrintAndLog("loaded %d samples", GraphTraceLen);
2149 RepaintGraphWindow();
2150 return 0;
2151}
2152
2153int CmdLtrim(const char *Cmd)
2154{
2155 int ds = atoi(Cmd);
2156
2157 for (int i = ds; i < GraphTraceLen; ++i)
2158 GraphBuffer[i-ds] = GraphBuffer[i];
2159 GraphTraceLen -= ds;
2160
2161 RepaintGraphWindow();
2162 return 0;
2163}
ec75f5c1 2164
2165// trim graph to input argument length
9ec1416a 2166int CmdRtrim(const char *Cmd)
2167{
2168 int ds = atoi(Cmd);
2169
2170 GraphTraceLen = ds;
2171
2172 RepaintGraphWindow();
2173 return 0;
2174}
7fe9b0b7 2175
2176/*
2177 * Manchester demodulate a bitstream. The bitstream needs to be already in
2178 * the GraphBuffer as 0 and 1 values
2179 *
2180 * Give the clock rate as argument in order to help the sync - the algorithm
2181 * resyncs at each pulse anyway.
2182 *
2183 * Not optimized by any means, this is the 1st time I'm writing this type of
2184 * routine, feel free to improve...
2185 *
2186 * 1st argument: clock rate (as number of samples per clock rate)
2187 * Typical values can be 64, 32, 128...
2188 */
2189int CmdManchesterDemod(const char *Cmd)
2190{
2191 int i, j, invert= 0;
2192 int bit;
2193 int clock;
fddf220a 2194 int lastval = 0;
7fe9b0b7 2195 int low = 0;
2196 int high = 0;
2197 int hithigh, hitlow, first;
2198 int lc = 0;
2199 int bitidx = 0;
2200 int bit2idx = 0;
2201 int warnings = 0;
2202
2203 /* check if we're inverting output */
c6f1fb9d 2204 if (*Cmd == 'i')
7fe9b0b7 2205 {
2206 PrintAndLog("Inverting output");
2207 invert = 1;
fffad860 2208 ++Cmd;
7fe9b0b7 2209 do
2210 ++Cmd;
2211 while(*Cmd == ' '); // in case a 2nd argument was given
2212 }
2213
2214 /* Holds the decoded bitstream: each clock period contains 2 bits */
2215 /* later simplified to 1 bit after manchester decoding. */
2216 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
2217 /* int BitStream[GraphTraceLen*2/clock+10]; */
2218
2219 /* But it does not work if compiling on WIndows: therefore we just allocate a */
2220 /* large array */
90e278d3 2221 uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
7fe9b0b7 2222
2223 /* Detect high and lows */
2224 for (i = 0; i < GraphTraceLen; i++)
2225 {
2226 if (GraphBuffer[i] > high)
2227 high = GraphBuffer[i];
2228 else if (GraphBuffer[i] < low)
2229 low = GraphBuffer[i];
2230 }
2231
2232 /* Get our clock */
f3bf15e4 2233 clock = GetAskClock(Cmd, high, 1);
7fe9b0b7 2234
2235 int tolerance = clock/4;
2236
2237 /* Detect first transition */
2238 /* Lo-Hi (arbitrary) */
2239 /* skip to the first high */
2240 for (i= 0; i < GraphTraceLen; i++)
2241 if (GraphBuffer[i] == high)
2242 break;
2243 /* now look for the first low */
2244 for (; i < GraphTraceLen; i++)
2245 {
2246 if (GraphBuffer[i] == low)
2247 {
2248 lastval = i;
2249 break;
2250 }
2251 }
2252
2253 /* If we're not working with 1/0s, demod based off clock */
2254 if (high != 1)
2255 {
2256 bit = 0; /* We assume the 1st bit is zero, it may not be
2257 * the case: this routine (I think) has an init problem.
2258 * Ed.
2259 */
2260 for (; i < (int)(GraphTraceLen / clock); i++)
2261 {
2262 hithigh = 0;
2263 hitlow = 0;
2264 first = 1;
2265
2266 /* Find out if we hit both high and low peaks */
2267 for (j = 0; j < clock; j++)
2268 {
2269 if (GraphBuffer[(i * clock) + j] == high)
2270 hithigh = 1;
2271 else if (GraphBuffer[(i * clock) + j] == low)
2272 hitlow = 1;
2273
2274 /* it doesn't count if it's the first part of our read
2275 because it's really just trailing from the last sequence */
2276 if (first && (hithigh || hitlow))
2277 hithigh = hitlow = 0;
2278 else
2279 first = 0;
2280
2281 if (hithigh && hitlow)
2282 break;
2283 }
2284
2285 /* If we didn't hit both high and low peaks, we had a bit transition */
2286 if (!hithigh || !hitlow)
2287 bit ^= 1;
2288
2289 BitStream[bit2idx++] = bit ^ invert;
2290 }
2291 }
2292
2293 /* standard 1/0 bitstream */
2294 else
2295 {
2296
2297 /* Then detect duration between 2 successive transitions */
2298 for (bitidx = 1; i < GraphTraceLen; i++)
2299 {
2300 if (GraphBuffer[i-1] != GraphBuffer[i])
2301 {
b3b70669 2302 lc = i-lastval;
2303 lastval = i;
2304
2305 // Error check: if bitidx becomes too large, we do not
2306 // have a Manchester encoded bitstream or the clock is really
2307 // wrong!
2308 if (bitidx > (GraphTraceLen*2/clock+8) ) {
2309 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
2310 return 0;
2311 }
2312 // Then switch depending on lc length:
2313 // Tolerance is 1/4 of clock rate (arbitrary)
2314 if (abs(lc-clock/2) < tolerance) {
2315 // Short pulse : either "1" or "0"
2316 BitStream[bitidx++]=GraphBuffer[i-1];
2317 } else if (abs(lc-clock) < tolerance) {
2318 // Long pulse: either "11" or "00"
2319 BitStream[bitidx++]=GraphBuffer[i-1];
2320 BitStream[bitidx++]=GraphBuffer[i-1];
2321 } else {
7fe9b0b7 2322 // Error
2323 warnings++;
b3b70669 2324 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
2325 PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
7fe9b0b7 2326
2327 if (warnings > 10)
2328 {
2329 PrintAndLog("Error: too many detection errors, aborting.");
2330 return 0;
2331 }
2332 }
2333 }
2334 }
2335
2336 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
2337 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
2338 // to stop output at the final bitidx2 value, not bitidx
2339 for (i = 0; i < bitidx; i += 2) {
2340 if ((BitStream[i] == 0) && (BitStream[i+1] == 1)) {
2341 BitStream[bit2idx++] = 1 ^ invert;
b3b70669 2342 } else if ((BitStream[i] == 1) && (BitStream[i+1] == 0)) {
2343 BitStream[bit2idx++] = 0 ^ invert;
2344 } else {
2345 // We cannot end up in this state, this means we are unsynchronized,
2346 // move up 1 bit:
2347 i++;
7fe9b0b7 2348 warnings++;
b3b70669 2349 PrintAndLog("Unsynchronized, resync...");
2350 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
7fe9b0b7 2351
2352 if (warnings > 10)
2353 {
2354 PrintAndLog("Error: too many decode errors, aborting.");
2355 return 0;
2356 }
2357 }
2358 }
2359 }
2360
2361 PrintAndLog("Manchester decoded bitstream");
2362 // Now output the bitstream to the scrollback by line of 16 bits
2363 for (i = 0; i < (bit2idx-16); i+=16) {
2364 PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
2365 BitStream[i],
2366 BitStream[i+1],
2367 BitStream[i+2],
2368 BitStream[i+3],
2369 BitStream[i+4],
2370 BitStream[i+5],
2371 BitStream[i+6],
2372 BitStream[i+7],
2373 BitStream[i+8],
2374 BitStream[i+9],
2375 BitStream[i+10],
2376 BitStream[i+11],
2377 BitStream[i+12],
2378 BitStream[i+13],
2379 BitStream[i+14],
2380 BitStream[i+15]);
2381 }
2382 return 0;
2383}
2384
2385/* Modulate our data into manchester */
2386int CmdManchesterMod(const char *Cmd)
2387{
2388 int i, j;
2389 int clock;
2390 int bit, lastbit, wave;
2391
2392 /* Get our clock */
f3bf15e4 2393 clock = GetAskClock(Cmd, 0, 1);
7fe9b0b7 2394
2395 wave = 0;
2396 lastbit = 1;
2397 for (i = 0; i < (int)(GraphTraceLen / clock); i++)
2398 {
2399 bit = GraphBuffer[i * clock] ^ 1;
2400
2401 for (j = 0; j < (int)(clock/2); j++)
2402 GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave;
2403 for (j = (int)(clock/2); j < clock; j++)
2404 GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave ^ 1;
2405
2406 /* Keep track of how we start our wave and if we changed or not this time */
2407 wave ^= bit ^ lastbit;
2408 lastbit = bit;
2409 }
2410
2411 RepaintGraphWindow();
2412 return 0;
2413}
2414
2415int CmdNorm(const char *Cmd)
2416{
2417 int i;
2418 int max = INT_MIN, min = INT_MAX;
2419
2420 for (i = 10; i < GraphTraceLen; ++i) {
2421 if (GraphBuffer[i] > max)
2422 max = GraphBuffer[i];
2423 if (GraphBuffer[i] < min)
2424 min = GraphBuffer[i];
2425 }
2426
2427 if (max != min) {
2428 for (i = 0; i < GraphTraceLen; ++i) {
ba1a299c 2429 GraphBuffer[i] = (GraphBuffer[i] - ((max + min) / 2)) * 256 /
7fe9b0b7 2430 (max - min);
ba1a299c 2431 //marshmelow: adjusted *1000 to *256 to make +/- 128 so demod commands still work
7fe9b0b7 2432 }
2433 }
2434 RepaintGraphWindow();
2435 return 0;
2436}
2437
2438int CmdPlot(const char *Cmd)
2439{
2440 ShowGraphWindow();
2441 return 0;
2442}
2443
2444int CmdSave(const char *Cmd)
2445{
ec75f5c1 2446 char filename[FILE_PATH_SIZE] = {0x00};
2447 int len = 0;
b915fda3 2448
ec75f5c1 2449 len = strlen(Cmd);
2450 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
2451 memcpy(filename, Cmd, len);
b915fda3 2452
2453
2454 FILE *f = fopen(filename, "w");
7fe9b0b7 2455 if(!f) {
b915fda3 2456 PrintAndLog("couldn't open '%s'", filename);
7fe9b0b7 2457 return 0;
2458 }
2459 int i;
2460 for (i = 0; i < GraphTraceLen; i++) {
2461 fprintf(f, "%d\n", GraphBuffer[i]);
2462 }
2463 fclose(f);
2464 PrintAndLog("saved to '%s'", Cmd);
2465 return 0;
2466}
2467
2468int CmdScale(const char *Cmd)
2469{
2470 CursorScaleFactor = atoi(Cmd);
2471 if (CursorScaleFactor == 0) {
2472 PrintAndLog("bad, can't have zero scale");
2473 CursorScaleFactor = 1;
2474 }
2475 RepaintGraphWindow();
2476 return 0;
2477}
2478
2479int CmdThreshold(const char *Cmd)
2480{
2481 int threshold = atoi(Cmd);
2482
2483 for (int i = 0; i < GraphTraceLen; ++i) {
2484 if (GraphBuffer[i] >= threshold)
2485 GraphBuffer[i] = 1;
2486 else
7bb9d33e 2487 GraphBuffer[i] = -1;
7fe9b0b7 2488 }
2489 RepaintGraphWindow();
2490 return 0;
2491}
2492
d51b2eda
MHS
2493int CmdDirectionalThreshold(const char *Cmd)
2494{
b4fb11ba 2495 int8_t upThres = param_get8(Cmd, 0);
2496 int8_t downThres = param_get8(Cmd, 1);
ba1a299c 2497
d51b2eda 2498 printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres);
952a8bb5 2499
d51b2eda
MHS
2500 int lastValue = GraphBuffer[0];
2501 GraphBuffer[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in.
952a8bb5 2502
d51b2eda
MHS
2503 for (int i = 1; i < GraphTraceLen; ++i) {
2504 // Apply first threshold to samples heading up
2505 if (GraphBuffer[i] >= upThres && GraphBuffer[i] > lastValue)
2506 {
2507 lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it.
2508 GraphBuffer[i] = 1;
2509 }
2510 // Apply second threshold to samples heading down
2511 else if (GraphBuffer[i] <= downThres && GraphBuffer[i] < lastValue)
2512 {
2513 lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it.
2514 GraphBuffer[i] = -1;
2515 }
2516 else
2517 {
2518 lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it.
2519 GraphBuffer[i] = GraphBuffer[i-1];
2520
2521 }
2522 }
2523 GraphBuffer[0] = GraphBuffer[1]; // Aline with first edited sample.
2524 RepaintGraphWindow();
2525 return 0;
2526}
2527
7fe9b0b7 2528int CmdZerocrossings(const char *Cmd)
2529{
2530 // Zero-crossings aren't meaningful unless the signal is zero-mean.
2531 CmdHpf("");
2532
2533 int sign = 1;
2534 int zc = 0;
2535 int lastZc = 0;
2536
2537 for (int i = 0; i < GraphTraceLen; ++i) {
2538 if (GraphBuffer[i] * sign >= 0) {
2539 // No change in sign, reproduce the previous sample count.
2540 zc++;
2541 GraphBuffer[i] = lastZc;
2542 } else {
2543 // Change in sign, reset the sample count.
2544 sign = -sign;
2545 GraphBuffer[i] = lastZc;
2546 if (sign > 0) {
2547 lastZc = zc;
2548 zc = 0;
2549 }
2550 }
2551 }
2552
2553 RepaintGraphWindow();
2554 return 0;
2555}
2556
ba1a299c 2557static command_t CommandTable[] =
7fe9b0b7 2558{
2559 {"help", CmdHelp, 1, "This help"},
2560 {"amp", CmdAmp, 1, "Amplify peaks"},
19ff0210 2561 //{"askdemod", Cmdaskdemod, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"},
cc2ea5ed 2562 {"askedgedetect", CmdAskEdgeDetect, 1, "[threshold] Adjust Graph for manual ask demod using length of sample differences to detect the edge of a wave (default = 25)"},
2563 {"askem410xdemod",CmdAskEM410xDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Demodulate an EM410x tag from GraphBuffer (args optional)"},
8d960002 2564 {"askgproxiidemod",CmdG_Prox_II_Demod,1, "Demodulate a G Prox II tag from GraphBuffer"},
19ff0210 2565 //{"askmandemod", Cmdaskmandemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate ASK/Manchester tags and output binary (args optional)"},
2566 //{"askrawdemod", Cmdaskrawdemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate ASK tags and output bin (args optional)"},
7fe9b0b7 2567 {"autocorr", CmdAutoCorr, 1, "<window length> -- Autocorrelation over window"},
cc2ea5ed 2568 {"biphaserawdecode",CmdBiphaseDecodeRaw,1,"[offset] [invert<0|1>] Biphase decode bin stream in DemodBuffer (offset = 0|1 bits to shift the decode start)"},
7fe9b0b7 2569 {"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"},
19ff0210 2570 //{"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"},
7fe9b0b7 2571 {"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"},
2572 {"dec", CmdDec, 1, "Decimate samples"},
cc2ea5ed 2573 {"detectclock", CmdDetectClockRate, 1, "[modulation] Detect clock rate of wave in GraphBuffer (options: 'a','f','n','p' for ask, fsk, nrz, psk respectively)"},
19ff0210 2574 //{"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"},
cc2ea5ed 2575 {"fskawiddemod", CmdFSKdemodAWID, 1, "Demodulate an AWID FSK tag from GraphBuffer"},
f3bf15e4 2576 //{"fskfcdetect", CmdFSKfcDetect, 1, "Try to detect the Field Clock of an FSK wave"},
cc2ea5ed 2577 {"fskhiddemod", CmdFSKdemodHID, 1, "Demodulate a HID FSK tag from GraphBuffer"},
2578 {"fskiodemod", CmdFSKdemodIO, 1, "Demodulate an IO Prox FSK tag from GraphBuffer"},
2579 {"fskpyramiddemod",CmdFSKdemodPyramid,1, "Demodulate a Pyramid FSK tag from GraphBuffer"},
2580 {"fskparadoxdemod",CmdFSKdemodParadox,1, "Demodulate a Paradox FSK tag from GraphBuffer"},
19ff0210 2581 //{"fskrawdemod", CmdFSKrawdemod, 1, "[clock rate] [invert] [rchigh] [rclow] Demodulate graph window from FSK to bin (clock = 50)(invert = 1|0)(rchigh = 10)(rclow=8)"},
78f5b1a7 2582 {"getbitstream", CmdGetBitStream, 1, "Convert GraphBuffer's >=1 values to 1 and <1 to 0"},
7fe9b0b7 2583 {"grid", CmdGrid, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
b4fb11ba 2584 {"hexsamples", CmdHexsamples, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},
7fe9b0b7 2585 {"hide", CmdHide, 1, "Hide graph window"},
2586 {"hpf", CmdHpf, 1, "Remove DC offset from trace"},
7fe9b0b7 2587 {"load", CmdLoad, 1, "<filename> -- Load trace (to graph window"},
2588 {"ltrim", CmdLtrim, 1, "<samples> -- Trim samples from left of trace"},
9ec1416a 2589 {"rtrim", CmdRtrim, 1, "<location to end trace> -- Trim samples from right of trace"},
19ff0210 2590 //{"mandemod", CmdManchesterDemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
cc2ea5ed 2591 {"manrawdecode", Cmdmandecoderaw, 1, "Manchester decode binary stream in DemodBuffer"},
7fe9b0b7 2592 {"manmod", CmdManchesterMod, 1, "[clock rate] -- Manchester modulate a binary stream"},
b4fb11ba 2593 {"norm", CmdNorm, 1, "Normalize max/min to +/-128"},
f3bf15e4 2594 //{"nrzdetectclock",CmdDetectNRZClockRate, 1, "Detect ASK, PSK, or NRZ clock rate"},
19ff0210 2595 //{"nrzrawdemod", CmdNRZrawDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate nrz tags and output binary (args optional)"},
7ddb9900 2596 {"plot", CmdPlot, 1, "Show graph window (hit 'h' in window for keystroke help)"},
f3bf15e4 2597 //{"pskdetectclock",CmdDetectPSKClockRate, 1, "Detect ASK, PSK, or NRZ clock rate"},
8d960002 2598 {"printdemodbuffer",CmdPrintDemodBuff,1, "[clock] [invert<0|1>] -- Demodulate an indala tag (PSK1) from GraphBuffer (args optional)"},
cc2ea5ed 2599 {"pskindalademod",CmdIndalaDecode, 1, "[clock] [invert<0|1>] -- Demodulate an indala tag (PSK1) from GraphBuffer (args optional)"},
19ff0210 2600 //{"psk1rawdemod", CmdPSK1rawDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate psk1 tags and output binary (args optional)"},
2601 //{"psk2rawdemod", CmdPSK2rawDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate psk2 tags and output binary (args optional)"},
cc2ea5ed 2602 {"rawdemod", CmdRawDemod, 1, "[modulation] ... <options> -see help (h option) -- Demodulate the data in the GraphBuffer and output binary"},
2603 {"samples", CmdSamples, 0, "[512 - 40000] -- Get raw samples for graph window (GraphBuffer)"},
7fe9b0b7 2604 {"save", CmdSave, 1, "<filename> -- Save trace (from graph window)"},
2605 {"scale", CmdScale, 1, "<int> -- Set cursor display scale"},
ec75f5c1 2606 {"setdebugmode", CmdSetDebugMode, 1, "<0|1> -- Turn on or off Debugging Mode for demods"},
2607 {"shiftgraphzero",CmdGraphShiftZero, 1, "<shift> -- Shift 0 for Graphed wave + or - shift value"},
19ff0210 2608 //{"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"},
b4fb11ba 2609 {"dirthreshold", CmdDirectionalThreshold, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."},
2610 {"tune", CmdTuneSamples, 0, "Get hw tune samples for graph window"},
19ff0210 2611 {"undec", CmdUndec, 1, "Un-decimate samples by 2"},
b4fb11ba 2612 {"zerocrossings", CmdZerocrossings, 1, "Count time between zero-crossings"},
7fe9b0b7 2613 {NULL, NULL, 0, NULL}
2614};
2615
2616int CmdData(const char *Cmd)
2617{
2618 CmdsParse(CommandTable, Cmd);
2619 return 0;
2620}
2621
2622int CmdHelp(const char *Cmd)
2623{
2624 CmdsHelp(CommandTable);
2625 return 0;
2626}
Impressum, Datenschutz