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