lf cleaning

remove unneeded code/functions
fix lfdemod askmandemod bug with maxErr=0
silence output for getting samples in lf search
This commit is contained in:
marshmellow42 2015-04-05 00:58:57 -04:00
commit 2767fc0291
13 changed files with 169 additions and 853 deletions

View file

@ -58,37 +58,16 @@ int CmdSetDebugMode(const char *Cmd)
//by marshmellow
void printDemodBuff(void)
{
uint32_t i = 0;
int bitLen = DemodBufferLen;
if (bitLen<16) {
if (bitLen<1) {
PrintAndLog("no bits found in demod buffer");
return;
}
if (bitLen>512) bitLen=512; //max output to 512 bits if we have more - should be plenty
// ensure equally divided by 16
bitLen &= 0xfff0;
char *bin = sprint_bin_break(DemodBuffer,bitLen,16);
PrintAndLog("%s",bin);
for (i = 0; i <= (bitLen-16); i+=16) {
PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
DemodBuffer[i],
DemodBuffer[i+1],
DemodBuffer[i+2],
DemodBuffer[i+3],
DemodBuffer[i+4],
DemodBuffer[i+5],
DemodBuffer[i+6],
DemodBuffer[i+7],
DemodBuffer[i+8],
DemodBuffer[i+9],
DemodBuffer[i+10],
DemodBuffer[i+11],
DemodBuffer[i+12],
DemodBuffer[i+13],
DemodBuffer[i+14],
DemodBuffer[i+15]
);
}
return;
}
@ -114,105 +93,8 @@ int CmdPrintDemodBuff(const char *Cmd)
}
return 1;
}
int CmdAmp(const char *Cmd)
{
int i, rising, falling;
int max = INT_MIN, min = INT_MAX;
for (i = 10; i < GraphTraceLen; ++i) {
if (GraphBuffer[i] > max)
max = GraphBuffer[i];
if (GraphBuffer[i] < min)
min = GraphBuffer[i];
}
if (max != min) {
rising = falling= 0;
for (i = 0; i < GraphTraceLen; ++i) {
if (GraphBuffer[i + 1] < GraphBuffer[i]) {
if (rising) {
GraphBuffer[i] = max;
rising = 0;
}
falling = 1;
}
if (GraphBuffer[i + 1] > GraphBuffer[i]) {
if (falling) {
GraphBuffer[i] = min;
falling = 0;
}
rising= 1;
}
}
}
RepaintGraphWindow();
return 0;
}
/*
* Generic command to demodulate ASK.
*
* Argument is convention: positive or negative (High mod means zero
* or high mod means one)
*
* Updates the Graph trace with 0/1 values
*
* Arguments:
* c : 0 or 1 (or invert)
*/
//this method ignores the clock
//this function strictly converts highs and lows to 1s and 0s for each sample in the graphbuffer
int Cmdaskdemod(const char *Cmd)
{
int i;
int c, high = 0, low = 0;
sscanf(Cmd, "%i", &c);
/* Detect high and lows */
for (i = 0; i < GraphTraceLen; ++i)
{
if (GraphBuffer[i] > high)
high = GraphBuffer[i];
else if (GraphBuffer[i] < low)
low = GraphBuffer[i];
}
high=abs(high*.75);
low=abs(low*.75);
if (c != 0 && c != 1) {
PrintAndLog("Invalid argument: %s", Cmd);
return 0;
}
//prime loop
if (GraphBuffer[0] > 0) {
GraphBuffer[0] = 1-c;
} else {
GraphBuffer[0] = c;
}
for (i = 1; i < GraphTraceLen; ++i) {
/* Transitions are detected at each peak
* Transitions are either:
* - we're low: transition if we hit a high
* - we're high: transition if we hit a low
* (we need to do it this way because some tags keep high or
* low for long periods, others just reach the peak and go
* down)
*/
//[marhsmellow] change == to >= for high and <= for low for fuzz
if ((GraphBuffer[i] >= high) && (GraphBuffer[i - 1] == c)) {
GraphBuffer[i] = 1 - c;
} else if ((GraphBuffer[i] <= low) && (GraphBuffer[i - 1] == (1 - c))){
GraphBuffer[i] = c;
} else {
/* No transition */
GraphBuffer[i] = GraphBuffer[i - 1];
}
}
RepaintGraphWindow();
return 0;
}
//by marshmellow
//this function strictly converts >1 to 1 and <1 to 0 for each sample in the graphbuffer
int CmdGetBitStream(const char *Cmd)
{
@ -229,43 +111,6 @@ int CmdGetBitStream(const char *Cmd)
return 0;
}
//by marshmellow
void printBitStream(uint8_t BitStream[], uint32_t bitLen)
{
uint32_t i = 0;
if (bitLen<16) {
PrintAndLog("Too few bits found: %d",bitLen);
return;
}
if (bitLen>512) bitLen=512;
// ensure equally divided by 16
bitLen &= 0xfff0;
for (i = 0; i <= (bitLen-16); i+=16) {
PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
BitStream[i],
BitStream[i+1],
BitStream[i+2],
BitStream[i+3],
BitStream[i+4],
BitStream[i+5],
BitStream[i+6],
BitStream[i+7],
BitStream[i+8],
BitStream[i+9],
BitStream[i+10],
BitStream[i+11],
BitStream[i+12],
BitStream[i+13],
BitStream[i+14],
BitStream[i+15]
);
}
return;
}
//by marshmellow
//print 64 bit EM410x ID in multiple formats
void printEM410x(uint32_t hi, uint64_t id)
@ -282,11 +127,11 @@ void printEM410x(uint32_t hi, uint64_t id)
}
if (hi){
//output 88 bit em id
PrintAndLog("\nEM TAG ID : %06x%016llx", hi, id);
PrintAndLog("\nEM TAG ID : %06X%016llX", hi, id);
} else{
//output 40 bit em id
PrintAndLog("\nEM TAG ID : %010llx", id);
PrintAndLog("Unique TAG ID : %010llx", id2lo);
PrintAndLog("\nEM TAG ID : %010llX", id);
PrintAndLog("Unique TAG ID : %010llX", id2lo);
PrintAndLog("\nPossible de-scramble patterns");
PrintAndLog("HoneyWell IdentKey {");
PrintAndLog("DEZ 8 : %08lld",id & 0xFFFFFF);
@ -311,7 +156,7 @@ void printEM410x(uint32_t hi, uint64_t id)
);
uint64_t paxton = (((id>>32) << 24) | (id & 0xffffff)) + 0x143e00;
PrintAndLog("}\nOther : %05lld_%03lld_%08lld",(id&0xFFFF),((id>>16LL) & 0xFF),(id & 0xFFFFFF));
PrintAndLog("Pattern Paxton : %0d", paxton);
PrintAndLog("Pattern Paxton : %lld [0x%llX]", paxton, paxton);
uint32_t p1id = (id & 0xFFFFFF);
uint8_t arr[32] = {0x00};
@ -352,12 +197,12 @@ void printEM410x(uint32_t hi, uint64_t id)
p1 |= arr[2] << 4;
p1 |= arr[1] << 5;
p1 |= arr[0] << 9;
PrintAndLog("Pattern 1 : 0x%X - %d", p1, p1);
PrintAndLog("Pattern 1 : %d [0x%X]", p1, p1);
uint16_t sebury1 = id & 0xFFFF;
uint8_t sebury2 = (id >> 16) & 0x7F;
uint32_t sebury3 = id & 0x7FFFFF;
PrintAndLog("Pattern Sebury : %d %d %d (hex: %X %X %X)", sebury1, sebury2, sebury3, sebury1, sebury2, sebury3);
PrintAndLog("Pattern Sebury : %d %d %d [0x%X 0x%X 0x%X]", sebury1, sebury2, sebury3, sebury1, sebury2, sebury3);
}
}
return;
@ -430,20 +275,23 @@ int ASKmanDemod(const char *Cmd, bool verbose, bool emSearch)
clk=0;
}
size_t BitLen = getFromGraphBuf(BitStream);
if (g_debugMode==1) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen);
if (BitLen==0) return 0;
int errCnt=0;
if (g_debugMode) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen);
if (!BitLen) return 0;
if (maxLen<BitLen && maxLen != 0) BitLen = maxLen;
errCnt = askmandemod(BitStream, &BitLen, &clk, &invert, maxErr);
int errCnt = askmandemod(BitStream, &BitLen, &clk, &invert, maxErr);
if (errCnt<0||BitLen<16){ //if fatal error (or -1)
if (g_debugMode==1) PrintAndLog("no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk);
if (g_debugMode) PrintAndLog("DEBUG: no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk);
return 0;
}
if (verbose || g_debugMode) PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,BitLen);
if (errCnt>maxErr){
if (g_debugMode) PrintAndLog("DEBUG: Too many errors found, errors:%d, bits:%d, clock:%d",errCnt, BitLen, clk);
return 0;
}
if (verbose || g_debugMode) PrintAndLog("\nUsing Clock:%d, Invert:%d, Bits Found:%d",clk,invert,BitLen);
//output
if (errCnt>0){
if (verbose || g_debugMode) PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
if (verbose || g_debugMode) PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt);
}
if (verbose || g_debugMode) PrintAndLog("ASK/Manchester decoded bitstream:");
// Now output the bitstream to the scrollback by line of 16 bits
@ -519,7 +367,7 @@ int Cmdmandecoderaw(const char *Cmd)
BitStream[i]=DemodBuffer[i];
}
if (high>1 || low <0 ){
PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
PrintAndLog("Error: please raw demod the wave first then manchester raw decode");
return 0;
}
size=i;
@ -529,7 +377,7 @@ int Cmdmandecoderaw(const char *Cmd)
return 0;
}
PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt);
printBitStream(BitStream, size);
PrintAndLog("%s", sprint_bin_break(BitStream, size, 16));
if (errCnt==0){
uint64_t id = 0;
uint32_t hi = 0;
@ -549,11 +397,7 @@ int Cmdmandecoderaw(const char *Cmd)
//take 01 or 10 = 0 and 11 or 00 = 1
//takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit
// and "invert" default = 0 if 1 it will invert output
// since it is not like manchester and doesn't have an incorrect bit pattern we
// cannot determine if our decode is correct or if it should be shifted by one bit
// the argument offset allows us to manually shift if the output is incorrect
// (better would be to demod and decode at the same time so we can distinguish large
// width waves vs small width waves to help the decode positioning) or askbiphdemod
// the argument offset allows us to manually shift if the output is incorrect - [EDIT: now auto detects]
int CmdBiphaseDecodeRaw(const char *Cmd)
{
size_t size=0;
@ -592,39 +436,15 @@ int CmdBiphaseDecodeRaw(const char *Cmd)
}
if (errCnt>0){
PrintAndLog("# Errors found during Demod (shown as 77 in bit stream): %d",errCnt);
PrintAndLog("# Errors found during Demod (shown as 7 in bit stream): %d",errCnt);
}
PrintAndLog("Biphase Decoded using offset: %d - # invert:%d - data:",offset,invert);
printBitStream(BitStream, size);
PrintAndLog("%s", sprint_bin_break(BitStream, size, 16));
if (offset) setDemodBuf(DemodBuffer,DemodBufferLen-offset, offset); //remove first bit from raw demod
return 1;
}
// set demod buffer back to raw after biphase demod
void setBiphasetoRawDemodBuf(uint8_t *BitStream, size_t size)
{
uint8_t rawStream[512]={0x00};
size_t i=0;
uint8_t curPhase=0;
if (size > 256) {
PrintAndLog("ERROR - Biphase Demod Buffer overrun");
return;
}
for (size_t idx=0; idx<size; idx++){
if(!BitStream[idx]){
rawStream[i++] = curPhase;
rawStream[i++] = curPhase;
curPhase ^= 1;
} else {
rawStream[i++] = curPhase;
rawStream[i++] = curPhase ^ 1;
}
}
setDemodBuf(rawStream,i,0);
return;
}
//by marshmellow
//takes 4 arguments - clock, invert, maxErr as integers and amplify as char
//attempts to demodulate ask only
@ -649,26 +469,31 @@ int ASKrawDemod(const char *Cmd, bool verbose)
if (amp == 'a' || amp == 'A') askAmp=1;
size_t BitLen = getFromGraphBuf(BitStream);
if (BitLen==0) return 0;
int errCnt=0;
errCnt = askrawdemod(BitStream, &BitLen, &clk, &invert, maxErr, askAmp);
int errCnt = askrawdemod(BitStream, &BitLen, &clk, &invert, maxErr, askAmp);
if (errCnt==-1||BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
if (verbose || g_debugMode) PrintAndLog("no data found");
if (g_debugMode) PrintAndLog("errCnt: %d, BitLen: %d, clk: %d, invert: %d", errCnt, BitLen, clk, invert);
return 0;
}
if (verbose || g_debugMode) PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d", clk, invert, BitLen);
if (errCnt>maxErr) {
if (g_debugMode)
PrintAndLog("Too many errors found, errCnt: %d, BitLen: %d, clk: %d, invert: %d", errCnt, BitLen, clk, invert);
return 0;
}
if (verbose || g_debugMode)
PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d", clk, invert, BitLen);
//move BitStream back to DemodBuffer
setDemodBuf(BitStream,BitLen,0);
//output
if (errCnt>0 && (verbose || g_debugMode)){
PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d", errCnt);
PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d", errCnt);
}
if (verbose || g_debugMode){
PrintAndLog("ASK demoded bitstream:");
// Now output the bitstream to the scrollback by line of 16 bits
printBitStream(BitStream,BitLen);
printDemodBuff();
}
return 1;
}
@ -937,67 +762,6 @@ int CmdBitsamples(const char *Cmd)
return 0;
}
/*
* Convert to a bitstream
*/
int CmdBitstream(const char *Cmd)
{
int i, j;
int bit;
int gtl;
int clock;
int low = 0;
int high = 0;
int hithigh, hitlow, first;
/* Detect high and lows and clock */
for (i = 0; i < GraphTraceLen; ++i)
{
if (GraphBuffer[i] > high)
high = GraphBuffer[i];
else if (GraphBuffer[i] < low)
low = GraphBuffer[i];
}
/* Get our clock */
clock = GetAskClock(Cmd, high, 1);
gtl = ClearGraph(0);
bit = 0;
for (i = 0; i < (int)(gtl / clock); ++i)
{
hithigh = 0;
hitlow = 0;
first = 1;
/* Find out if we hit both high and low peaks */
for (j = 0; j < clock; ++j)
{
if (GraphBuffer[(i * clock) + j] == high)
hithigh = 1;
else if (GraphBuffer[(i * clock) + j] == low)
hitlow = 1;
/* it doesn't count if it's the first part of our read
because it's really just trailing from the last sequence */
if (first && (hithigh || hitlow))
hithigh = hitlow = 0;
else
first = 0;
if (hithigh && hitlow)
break;
}
/* If we didn't hit both high and low peaks, we had a bit transition */
if (!hithigh || !hitlow)
bit ^= 1;
AppendGraph(0, clock, bit);
}
RepaintGraphWindow();
return 0;
}
int CmdBuffClear(const char *Cmd)
{
UsbCommand c = {CMD_BUFF_CLEAR};
@ -1076,7 +840,7 @@ int CmdGraphShiftZero(const char *Cmd)
//by marshmellow
//use large jumps in read samples to identify edges of waves and then amplify that wave to max
//similar to dirtheshold, threshold, and askdemod commands
//similar to dirtheshold, threshold commands
//takes a threshold length which is the measured length between two samples then determines an edge
int CmdAskEdgeDetect(const char *Cmd)
{
@ -1134,6 +898,25 @@ int CmdDetectClockRate(const char *Cmd)
return ans;
}
char *GetFSKType(uint8_t fchigh, uint8_t fclow, uint8_t invert)
{
char *fskType;
if (fchigh==10 && fclow==8){
if (invert) //fsk2a
fskType = "FSK2a";
else //fsk2
fskType = "FSK2";
} else if (fchigh == 8 && fclow == 5) {
if (invert)
fskType = "FSK1";
else
fskType = "FSK1a";
} else {
fskType = "FSK??";
}
return fskType;
}
//by marshmellow
//fsk raw demod and print binary
//takes 4 arguments - Clock, invert, fchigh, fclow
@ -1177,21 +960,20 @@ int FSKrawDemod(const char *Cmd, bool verbose)
rfLen = detectFSKClk(BitStream, BitLen, fchigh, fclow);
if (rfLen == 0) rfLen = 50;
}
if (verbose) PrintAndLog("Args invert: %d - Clock:%d - fchigh:%d - fclow: %d",invert,rfLen,fchigh, fclow);
int size = fskdemod(BitStream,BitLen,(uint8_t)rfLen,(uint8_t)invert,(uint8_t)fchigh,(uint8_t)fclow);
if (size>0){
setDemodBuf(BitStream,size,0);
// Now output the bitstream to the scrollback by line of 16 bits
if(size > (8*32)+2) size = (8*32)+2; //only output a max of 8 blocks of 32 bits most tags will have full bit stream inside that sample size
if (verbose) {
PrintAndLog("FSK decoded bitstream:");
printBitStream(BitStream,size);
if (verbose || g_debugMode) {
PrintAndLog("\nUsing Clock:%d, invert:%d, fchigh:%d, fclow:%d", rfLen, invert, fchigh, fclow);
PrintAndLog("%s decoded bitstream:",GetFSKType(fchigh,fclow,invert));
printDemodBuff();
}
return 1;
} else{
if (verbose) PrintAndLog("no FSK data found");
if (g_debugMode) PrintAndLog("no FSK data found");
}
return 0;
}
@ -1395,9 +1177,9 @@ int CmdFSKdemodIO(const char *Cmd)
return 0;
}
if (idx==0){
if (g_debugMode==1){
if (g_debugMode){
PrintAndLog("DEBUG: IO Prox Data not found - FSK Bits: %d",BitLen);
if (BitLen > 92) printBitStream(BitStream,92);
if (BitLen > 92) PrintAndLog("%s", sprint_bin_break(BitStream,92,16));
}
return 0;
}
@ -1411,7 +1193,7 @@ int CmdFSKdemodIO(const char *Cmd)
//XSF(version)facility:codeone+codetwo (raw)
//Handle the data
if (idx+64>BitLen) {
if (g_debugMode==1) PrintAndLog("not enough bits found - bitlen: %d",BitLen);
if (g_debugMode) PrintAndLog("not enough bits found - bitlen: %d",BitLen);
return 0;
}
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]);
@ -1432,7 +1214,6 @@ int CmdFSKdemodIO(const char *Cmd)
for (uint8_t i=1; i<6; ++i){
calccrc += bytebits_to_byte(BitStream+idx+9*i,8);
//PrintAndLog("%d", calccrc);
}
calccrc &= 0xff;
calccrc = 0xff - calccrc;
@ -1690,124 +1471,6 @@ int CmdFSKdemodPyramid(const char *Cmd)
return 1;
}
int CmdFSKdemod(const char *Cmd) //old CmdFSKdemod needs updating
{
static const int LowTone[] = {
1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1, 1, 1, 1, 1, -1, -1, -1, -1, -1
};
static const int HighTone[] = {
1, 1, 1, 1, 1, -1, -1, -1, -1,
1, 1, 1, 1, -1, -1, -1, -1,
1, 1, 1, 1, -1, -1, -1, -1,
1, 1, 1, 1, -1, -1, -1, -1,
1, 1, 1, 1, -1, -1, -1, -1,
1, 1, 1, 1, -1, -1, -1, -1, -1,
};
int lowLen = sizeof (LowTone) / sizeof (int);
int highLen = sizeof (HighTone) / sizeof (int);
int convLen = (highLen > lowLen) ? highLen : lowLen;
uint32_t hi = 0, lo = 0;
int i, j;
int minMark = 0, maxMark = 0;
for (i = 0; i < GraphTraceLen - convLen; ++i) {
int lowSum = 0, highSum = 0;
for (j = 0; j < lowLen; ++j) {
lowSum += LowTone[j]*GraphBuffer[i+j];
}
for (j = 0; j < highLen; ++j) {
highSum += HighTone[j] * GraphBuffer[i + j];
}
lowSum = abs(100 * lowSum / lowLen);
highSum = abs(100 * highSum / highLen);
GraphBuffer[i] = (highSum << 16) | lowSum;
}
for(i = 0; i < GraphTraceLen - convLen - 16; ++i) {
int lowTot = 0, highTot = 0;
// 10 and 8 are f_s divided by f_l and f_h, rounded
for (j = 0; j < 10; ++j) {
lowTot += (GraphBuffer[i+j] & 0xffff);
}
for (j = 0; j < 8; j++) {
highTot += (GraphBuffer[i + j] >> 16);
}
GraphBuffer[i] = lowTot - highTot;
if (GraphBuffer[i] > maxMark) maxMark = GraphBuffer[i];
if (GraphBuffer[i] < minMark) minMark = GraphBuffer[i];
}
GraphTraceLen -= (convLen + 16);
RepaintGraphWindow();
// Find bit-sync (3 lo followed by 3 high) (HID ONLY)
int max = 0, maxPos = 0;
for (i = 0; i < 6000; ++i) {
int dec = 0;
for (j = 0; j < 3 * lowLen; ++j) {
dec -= GraphBuffer[i + j];
}
for (; j < 3 * (lowLen + highLen ); ++j) {
dec += GraphBuffer[i + j];
}
if (dec > max) {
max = dec;
maxPos = i;
}
}
// place start of bit sync marker in graph
GraphBuffer[maxPos] = maxMark;
GraphBuffer[maxPos + 1] = minMark;
maxPos += j;
// place end of bit sync marker in graph
GraphBuffer[maxPos] = maxMark;
GraphBuffer[maxPos+1] = minMark;
PrintAndLog("actual data bits start at sample %d", maxPos);
PrintAndLog("length %d/%d", highLen, lowLen);
uint8_t bits[46] = {0x00};
// find bit pairs and manchester decode them
for (i = 0; i < arraylen(bits) - 1; ++i) {
int dec = 0;
for (j = 0; j < lowLen; ++j) {
dec -= GraphBuffer[maxPos + j];
}
for (; j < lowLen + highLen; ++j) {
dec += GraphBuffer[maxPos + j];
}
maxPos += j;
// place inter bit marker in graph
GraphBuffer[maxPos] = maxMark;
GraphBuffer[maxPos + 1] = minMark;
// hi and lo form a 64 bit pair
hi = (hi << 1) | (lo >> 31);
lo = (lo << 1);
// store decoded bit as binary (in hi/lo) and text (in bits[])
if(dec < 0) {
bits[i] = '1';
lo |= 1;
} else {
bits[i] = '0';
}
}
PrintAndLog("bits: '%s'", bits);
PrintAndLog("hex: %08x %08x", hi, lo);
return 0;
}
//by marshmellow
//attempt to psk1 demod graph buffer
int PSKDemod(const char *Cmd, bool verbose)
@ -1835,17 +1498,17 @@ int PSKDemod(const char *Cmd, bool verbose)
int errCnt=0;
errCnt = pskRawDemod(BitStream, &BitLen, &clk, &invert);
if (errCnt > maxErr){
if (g_debugMode==1 && verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
if (g_debugMode || verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
return 0;
}
if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
if (g_debugMode==1 && verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
if (g_debugMode || verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
return 0;
}
if (verbose){
PrintAndLog("Tried PSK Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
if (verbose || g_debugMode){
PrintAndLog("\nUsing Clock:%d, invert:%d, Bits Found:%d",clk,invert,BitLen);
if (errCnt>0){
PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt);
}
}
//prime demod buffer for output
@ -1970,7 +1633,7 @@ int NRZrawDemod(const char *Cmd, bool verbose)
//prime demod buffer for output
setDemodBuf(BitStream,BitLen,0);
if (errCnt>0 && (verbose || g_debugMode)) PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
if (errCnt>0 && (verbose || g_debugMode)) PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt);
if (verbose || g_debugMode) {
PrintAndLog("NRZ demoded bitstream:");
// Now output the bitstream to the scrollback by line of 16 bits
@ -2026,7 +1689,7 @@ int CmdPSK1rawDemod(const char *Cmd)
return 0;
}
PrintAndLog("PSK demoded bitstream:");
PrintAndLog("PSK1 demoded bitstream:");
// Now output the bitstream to the scrollback by line of 16 bits
printDemodBuff();
return 1;
@ -2295,10 +1958,10 @@ int CmdTuneSamples(const char *Cmd)
PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1));
PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0);
#define LF_UNUSABLE_V 2948 // was 2000. Changed due to bugfix in voltage measurements. LF results are now 47% higher.
#define LF_MARGINAL_V 14739 // was 10000. Changed due to bugfix bug in voltage measurements. LF results are now 47% higher.
#define HF_UNUSABLE_V 3167 // was 2000. Changed due to bugfix in voltage measurements. HF results are now 58% higher.
#define HF_MARGINAL_V 7917 // was 5000. Changed due to bugfix in voltage measurements. HF results are now 58% higher.
#define LF_UNUSABLE_V 2948 // was 2000. Changed due to bugfix in voltage measurements. LF results are now 47% higher.
#define LF_MARGINAL_V 14739 // was 10000. Changed due to bugfix bug in voltage measurements. LF results are now 47% higher.
#define HF_UNUSABLE_V 3167 // was 2000. Changed due to bugfix in voltage measurements. HF results are now 58% higher.
#define HF_MARGINAL_V 7917 // was 5000. Changed due to bugfix in voltage measurements. HF results are now 58% higher.
if (peakv < LF_UNUSABLE_V)
PrintAndLog("# Your LF antenna is unusable.");
@ -2374,245 +2037,6 @@ int CmdRtrim(const char *Cmd)
return 0;
}
/*
* Manchester demodulate a bitstream. The bitstream needs to be already in
* the GraphBuffer as 0 and 1 values
*
* Give the clock rate as argument in order to help the sync - the algorithm
* resyncs at each pulse anyway.
*
* Not optimized by any means, this is the 1st time I'm writing this type of
* routine, feel free to improve...
*
* 1st argument: clock rate (as number of samples per clock rate)
* Typical values can be 64, 32, 128...
*/
int CmdManchesterDemod(const char *Cmd)
{
int i, j, invert= 0;
int bit;
int clock;
int lastval = 0;
int low = 0;
int high = 0;
int hithigh, hitlow, first;
int lc = 0;
int bitidx = 0;
int bit2idx = 0;
int warnings = 0;
/* check if we're inverting output */
if (*Cmd == 'i')
{
PrintAndLog("Inverting output");
invert = 1;
++Cmd;
do
++Cmd;
while(*Cmd == ' '); // in case a 2nd argument was given
}
/* Holds the decoded bitstream: each clock period contains 2 bits */
/* later simplified to 1 bit after manchester decoding. */
/* Add 10 bits to allow for noisy / uncertain traces without aborting */
/* int BitStream[GraphTraceLen*2/clock+10]; */
/* But it does not work if compiling on WIndows: therefore we just allocate a */
/* large array */
uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
/* Detect high and lows */
for (i = 0; i < GraphTraceLen; i++)
{
if (GraphBuffer[i] > high)
high = GraphBuffer[i];
else if (GraphBuffer[i] < low)
low = GraphBuffer[i];
}
/* Get our clock */
clock = GetAskClock(Cmd, high, 1);
int tolerance = clock/4;
/* Detect first transition */
/* Lo-Hi (arbitrary) */
/* skip to the first high */
for (i= 0; i < GraphTraceLen; i++)
if (GraphBuffer[i] == high)
break;
/* now look for the first low */
for (; i < GraphTraceLen; i++)
{
if (GraphBuffer[i] == low)
{
lastval = i;
break;
}
}
/* If we're not working with 1/0s, demod based off clock */
if (high != 1)
{
bit = 0; /* We assume the 1st bit is zero, it may not be
* the case: this routine (I think) has an init problem.
* Ed.
*/
for (; i < (int)(GraphTraceLen / clock); i++)
{
hithigh = 0;
hitlow = 0;
first = 1;
/* Find out if we hit both high and low peaks */
for (j = 0; j < clock; j++)
{
if (GraphBuffer[(i * clock) + j] == high)
hithigh = 1;
else if (GraphBuffer[(i * clock) + j] == low)
hitlow = 1;
/* it doesn't count if it's the first part of our read
because it's really just trailing from the last sequence */
if (first && (hithigh || hitlow))
hithigh = hitlow = 0;
else
first = 0;
if (hithigh && hitlow)
break;
}
/* If we didn't hit both high and low peaks, we had a bit transition */
if (!hithigh || !hitlow)
bit ^= 1;
BitStream[bit2idx++] = bit ^ invert;
}
}
/* standard 1/0 bitstream */
else
{
/* Then detect duration between 2 successive transitions */
for (bitidx = 1; i < GraphTraceLen; i++)
{
if (GraphBuffer[i-1] != GraphBuffer[i])
{
lc = i-lastval;
lastval = i;
// Error check: if bitidx becomes too large, we do not
// have a Manchester encoded bitstream or the clock is really
// wrong!
if (bitidx > (GraphTraceLen*2/clock+8) ) {
PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
return 0;
}
// Then switch depending on lc length:
// Tolerance is 1/4 of clock rate (arbitrary)
if (abs(lc-clock/2) < tolerance) {
// Short pulse : either "1" or "0"
BitStream[bitidx++]=GraphBuffer[i-1];
} else if (abs(lc-clock) < tolerance) {
// Long pulse: either "11" or "00"
BitStream[bitidx++]=GraphBuffer[i-1];
BitStream[bitidx++]=GraphBuffer[i-1];
} else {
// Error
warnings++;
PrintAndLog("Warning: Manchester decode error for pulse width detection.");
PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
if (warnings > 10)
{
PrintAndLog("Error: too many detection errors, aborting.");
return 0;
}
}
}
}
// At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
// Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
// to stop output at the final bitidx2 value, not bitidx
for (i = 0; i < bitidx; i += 2) {
if ((BitStream[i] == 0) && (BitStream[i+1] == 1)) {
BitStream[bit2idx++] = 1 ^ invert;
} else if ((BitStream[i] == 1) && (BitStream[i+1] == 0)) {
BitStream[bit2idx++] = 0 ^ invert;
} else {
// We cannot end up in this state, this means we are unsynchronized,
// move up 1 bit:
i++;
warnings++;
PrintAndLog("Unsynchronized, resync...");
PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
if (warnings > 10)
{
PrintAndLog("Error: too many decode errors, aborting.");
return 0;
}
}
}
}
PrintAndLog("Manchester decoded bitstream");
// Now output the bitstream to the scrollback by line of 16 bits
for (i = 0; i < (bit2idx-16); i+=16) {
PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
BitStream[i],
BitStream[i+1],
BitStream[i+2],
BitStream[i+3],
BitStream[i+4],
BitStream[i+5],
BitStream[i+6],
BitStream[i+7],
BitStream[i+8],
BitStream[i+9],
BitStream[i+10],
BitStream[i+11],
BitStream[i+12],
BitStream[i+13],
BitStream[i+14],
BitStream[i+15]);
}
return 0;
}
/* Modulate our data into manchester */
int CmdManchesterMod(const char *Cmd)
{
int i, j;
int clock;
int bit, lastbit, wave;
/* Get our clock */
clock = GetAskClock(Cmd, 0, 1);
wave = 0;
lastbit = 1;
for (i = 0; i < (int)(GraphTraceLen / clock); i++)
{
bit = GraphBuffer[i * clock] ^ 1;
for (j = 0; j < (int)(clock/2); j++)
GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave;
for (j = (int)(clock/2); j < clock; j++)
GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave ^ 1;
/* Keep track of how we start our wave and if we changed or not this time */
wave ^= bit ^ lastbit;
lastbit = bit;
}
RepaintGraphWindow();
return 0;
}
int CmdNorm(const char *Cmd)
{
int i;
@ -2677,20 +2101,6 @@ int CmdScale(const char *Cmd)
return 0;
}
int CmdThreshold(const char *Cmd)
{
int threshold = atoi(Cmd);
for (int i = 0; i < GraphTraceLen; ++i) {
if (GraphBuffer[i] >= threshold)
GraphBuffer[i] = 1;
else
GraphBuffer[i] = -1;
}
RepaintGraphWindow();
return 0;
}
int CmdDirectionalThreshold(const char *Cmd)
{
int8_t upThres = param_get8(Cmd, 0);
@ -2758,19 +2168,15 @@ int CmdZerocrossings(const char *Cmd)
static command_t CommandTable[] =
{
{"help", CmdHelp, 1, "This help"},
{"amp", CmdAmp, 1, "Amplify peaks"},
//{"askdemod", Cmdaskdemod, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"},
{"askedgedetect", CmdAskEdgeDetect, 1, "[threshold] Adjust Graph for manual ask demod using length of sample differences to detect the edge of a wave (default = 25)"},
{"askem410xdemod", CmdAskEM410xDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Demodulate an EM410x tag from GraphBuffer (args optional)"},
{"askgproxiidemod", CmdG_Prox_II_Demod, 1, "Demodulate a G Prox II tag from GraphBuffer"},
{"autocorr", CmdAutoCorr, 1, "[window length] [g] -- Autocorrelation over window - g to save back to GraphBuffer (overwrite)"},
{"biphaserawdecode",CmdBiphaseDecodeRaw,1,"[offset] [invert<0|1>] Biphase decode bin stream in DemodBuffer (offset = 0|1 bits to shift the decode start)"},
{"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"},
//{"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"},
{"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"},
{"dec", CmdDec, 1, "Decimate samples"},
{"detectclock", CmdDetectClockRate, 1, "[modulation] Detect clock rate of wave in GraphBuffer (options: 'a','f','n','p' for ask, fsk, nrz, psk respectively)"},
//{"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"},
{"fskawiddemod", CmdFSKdemodAWID, 1, "Demodulate an AWID FSK tag from GraphBuffer"},
//{"fskfcdetect", CmdFSKfcDetect, 1, "Try to detect the Field Clock of an FSK wave"},
{"fskhiddemod", CmdFSKdemodHID, 1, "Demodulate a HID FSK tag from GraphBuffer"},
@ -2785,9 +2191,7 @@ static command_t CommandTable[] =
{"load", CmdLoad, 1, "<filename> -- Load trace (to graph window"},
{"ltrim", CmdLtrim, 1, "<samples> -- Trim samples from left of trace"},
{"rtrim", CmdRtrim, 1, "<location to end trace> -- Trim samples from right of trace"},
//{"mandemod", CmdManchesterDemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
{"manrawdecode", Cmdmandecoderaw, 1, "Manchester decode binary stream in DemodBuffer"},
{"manmod", CmdManchesterMod, 1, "[clock rate] -- Manchester modulate a binary stream"},
{"norm", CmdNorm, 1, "Normalize max/min to +/-128"},
{"plot", CmdPlot, 1, "Show graph window (hit 'h' in window for keystroke help)"},
{"printdemodbuffer",CmdPrintDemodBuff, 1, "[x] -- print the data in the DemodBuffer - 'x' for hex output"},
@ -2798,7 +2202,6 @@ static command_t CommandTable[] =
{"scale", CmdScale, 1, "<int> -- Set cursor display scale"},
{"setdebugmode", CmdSetDebugMode, 1, "<0|1> -- Turn on or off Debugging Mode for demods"},
{"shiftgraphzero", CmdGraphShiftZero, 1, "<shift> -- Shift 0 for Graphed wave + or - shift value"},
//{"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"},
{"dirthreshold", CmdDirectionalThreshold, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."},
{"tune", CmdTuneSamples, 0, "Get hw tune samples for graph window"},
{"undec", CmdUndec, 1, "Un-decimate samples by 2"},