From 91898babc0a1088e8cd89f32d0b7077d0aed3af8 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 30 Jul 2017 21:21:02 +0200 Subject: [PATCH] FIX: data plot AutoCorrelate slider, window too big, now limited to number of samples. enhanced debugstatements, 'lf em 410x_demod' vs 'lf em 410x_read' now read does the same as all other LF, and demod too... --- armsrc/appmain.c | 12 ++-- client/cmddata.c | 91 ++++++++++++++---------- client/cmdlf.c | 169 ++++++++------------------------------------- client/cmdlfem4x.c | 92 +++++++++++++----------- client/cmdlfem4x.h | 6 +- client/graph.c | 53 ++++++-------- client/graph.h | 4 +- client/util.c | 52 ++++++++++++-- common/lfdemod.c | 69 +++++++++--------- 9 files changed, 250 insertions(+), 298 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 61f7ee496..8ef038a55 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -929,15 +929,19 @@ void UsbPacketReceived(uint8_t *packet, int len) case CMD_SET_LF_SAMPLING_CONFIG: setSamplingConfig((sample_config *) c->d.asBytes); break; - case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K: - cmd_send(CMD_ACK,SampleLF(c->arg[0], c->arg[1]),0,0,0,0); + case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K: { + uint32_t bits = SampleLF(c->arg[0], c->arg[1]); + cmd_send(CMD_ACK, bits, 0, 0, 0, 0); break; + } case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K: ModThenAcquireRawAdcSamples125k(c->arg[0], c->arg[1], c->arg[2], c->d.asBytes); break; - case CMD_LF_SNOOP_RAW_ADC_SAMPLES: - cmd_send(CMD_ACK,SnoopLF(),0,0,0,0); + case CMD_LF_SNOOP_RAW_ADC_SAMPLES: { + uint32_t bits = SnoopLF(); + cmd_send(CMD_ACK, bits, 0, 0, 0, 0); break; + } case CMD_HID_DEMOD_FSK: CmdHIDdemodFSK(c->arg[0], 0, 0, 1); break; diff --git a/client/cmddata.c b/client/cmddata.c index 91aed6fd8..1886c84f9 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -299,15 +299,14 @@ int CmdSetDebugMode(const char *Cmd) { //by marshmellow // max output to 512 bits if we have more - should be plenty void printDemodBuff(void) { - int bitLen = DemodBufferLen; - if (bitLen < 1) { - PrintAndLog("no bits found in demod buffer"); + int len = DemodBufferLen; + if (len < 1) { + PrintAndLog("(printDemodBuff) no bits found in demod buffer"); return; } - if (bitLen > 512) bitLen = 512; + if (len > 512) len = 512; - char *bin = sprint_bin_break(DemodBuffer, bitLen,16); - PrintAndLog("%s",bin); + PrintAndLog("%s", sprint_bin_break(DemodBuffer, len, 16) ); } int CmdPrintDemodBuff(const char *Cmd) { @@ -392,20 +391,29 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType, uint8_t askamp = 0; char amp = param_getchar(Cmd, 0); uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0}; + sscanf(Cmd, "%i %i %i %i %c", &clk, &invert, &maxErr, &maxLen, &); - if (!maxLen) maxLen = BIGBUF_SIZE; + + if (!maxLen) maxLen = MAX_GRAPH_TRACE_LEN; + if (invert != 0 && invert != 1) { PrintAndLog("Invalid argument: %s", Cmd); return 0; } - if (clk==1){ - invert=1; - clk=0; + + if (clk == 1) { + invert = 1; + clk = 0; } + size_t BitLen = getFromGraphBuf(BitStream); - if (g_debugMode) PrintAndLog("DEBUG: Bitlen from grphbuff: %d", BitLen); - if (BitLen<255) return 0; - if (maxLen maxErr){ - if (g_debugMode) PrintAndLog("DEBUG: Too many errors found, errors:%d, bits:%d, clock:%d",errCnt, BitLen, clk); + if (g_debugMode) + PrintAndLog("DEBUG: (ASKDemod_ext) 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); + + if (verbose || g_debugMode) PrintAndLog("DEBUG: (ASKDemod_ext) Using clock:%d, invert:%d, bits found:%d", clk, invert, BitLen); //output setDemodBuf(BitStream,BitLen,0); setClockGrid(clk, startIdx); if (verbose || g_debugMode){ - if (errCnt>0) + if (errCnt > 0) PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt); if (askType) PrintAndLog("ASK/Manchester - Clock: %d - Decoded bitstream:",clk); @@ -636,11 +652,16 @@ int AutoCorrelate(const int *in, int *out, size_t len, int window, bool SaveGrph size_t Correlation = 0; int maxSum = 0; int lastMax = 0; + + // sanity check + if ( window > len ) window = len; + if (verbose) PrintAndLog("performing %d correlations", GraphTraceLen - window); + for (int i = 0; i < len - window; ++i) { int sum = 0; for (int j = 0; j < window; ++j) { - sum += (in[j]*in[i + j]) / 256; + sum += (in[j] * in[i + j]) / 256; } CorrelBuffer[i] = sum; if (sum >= maxSum-100 && sum <= maxSum+100){ @@ -649,7 +670,7 @@ int AutoCorrelate(const int *in, int *out, size_t len, int window, bool SaveGrph lastMax = i; if (sum > maxSum) maxSum = sum; } else if (sum > maxSum){ - maxSum=sum; + maxSum = sum; lastMax = i; } } @@ -789,9 +810,9 @@ int CmdGraphShiftZero(const char *Cmd) { int AskEdgeDetect(const int *in, int *out, int len, int threshold) { int last = 0; for(int i = 1; i= threshold) //large jump up + if (in[i] - in[i-1] >= threshold) //large jump up last = 127; - else if(in[i]-in[i-1] <= -1 * threshold) //large jump down + else if (in[i] - in[i-1] <= -1 * threshold) //large jump down last = -127; out[i-1] = last; } @@ -919,7 +940,7 @@ int FSKrawDemod(const char *Cmd, bool verbose) // Now output the bitstream to the scrollback by line of 16 bits if (verbose || g_debugMode) { - PrintAndLog("\nUsing Clock:%u, invert:%u, fchigh:%u, fclow:%u", rfLen, invert, fchigh, fclow); + PrintAndLog("DEBUG: (FSKrawDemod) Using Clock:%u, invert:%u, fchigh:%u, fclow:%u", rfLen, invert, fchigh, fclow); PrintAndLog("%s decoded bitstream:", GetFSKType(fchigh, fclow, invert)); printDemodBuff(); } @@ -963,17 +984,17 @@ int PSKDemod(const char *Cmd, bool verbose) int startIdx = 0; errCnt = pskRawDemod_ext(BitStream, &BitLen, &clk, &invert, &startIdx); if (errCnt > maxErr){ - if (g_debugMode || verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt); + if (g_debugMode || verbose) PrintAndLog("DEBUG: (PSKdemod) 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 || verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt); + if (g_debugMode || verbose) PrintAndLog("DEBUG: (PSKdemod) no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt); return 0; } if (verbose || g_debugMode){ - PrintAndLog("\nUsing Clock:%d, invert:%d, Bits Found:%d",clk,invert,BitLen); + PrintAndLog("DEBUG: (PSKdemod) Using Clock:%d, invert:%d, Bits Found:%d",clk,invert,BitLen); if (errCnt>0){ - PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt); + PrintAndLog("DEBUG: (PSKdemod) errors during Demoding (shown as 7 in bit stream): %d",errCnt); } } //prime demod buffer for output @@ -1055,7 +1076,7 @@ int NRZrawDemod(const char *Cmd, bool verbose) clk=0; } if (invert != 0 && invert != 1) { - PrintAndLog("Invalid argument: %s", Cmd); + PrintAndLog("(NRZrawDemod) Invalid argument: %s", Cmd); return 0; } uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; @@ -1065,20 +1086,20 @@ int NRZrawDemod(const char *Cmd, bool verbose) int clkStartIdx = 0; errCnt = nrzRawDemod(BitStream, &BitLen, &clk, &invert, &clkStartIdx); if (errCnt > maxErr){ - if (g_debugMode) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); + if (g_debugMode) PrintAndLog("DEBUG: (NRZrawDemod) 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) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); + if (g_debugMode) PrintAndLog("DEBUG: (NRZrawDemod) no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); return 0; } - if (verbose || g_debugMode) PrintAndLog("Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen); + if (verbose || g_debugMode) PrintAndLog("DEBUG: (NRZrawDemod) Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen); //prime demod buffer for output setDemodBuf(BitStream,BitLen,0); setClockGrid(clk, clkStartIdx); - if (errCnt>0 && (verbose || g_debugMode)) PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt); + if (errCnt>0 && (verbose || g_debugMode)) PrintAndLog("DEBUG: (NRZrawDemod) 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 @@ -1170,7 +1191,7 @@ int CmdRawDemod(const char *Cmd) void setClockGrid(int clk, int offset) { g_DemodStartIdx = offset; g_DemodClock = clk; - if (g_debugMode) PrintAndLog("demodoffset %d, clk %d",offset,clk); + if (g_debugMode) PrintAndLog("DBEUG: (setClockGrid) demodoffset %d, clk %d",offset,clk); if (offset > clk) offset %= clk; if (offset < 0) offset += clk; @@ -1338,7 +1359,7 @@ int getSamples(int n, bool silent) GraphTraceLen = n; } - setClockGrid(0,0); + setClockGrid(0, 0); DemodBufferLen = 0; RepaintGraphWindow(); return 0; diff --git a/client/cmdlf.c b/client/cmdlf.c index 7e730b3f0..fb5e96c43 100644 --- a/client/cmdlf.c +++ b/client/cmdlf.c @@ -335,7 +335,8 @@ bool lf_read(bool silent, uint32_t samples) { return false; } } - getSamples(resp.arg[0], silent); + // resp.arg[0] is bits read not bytes read. + getSamples(resp.arg[0]/8, silent); return true; } @@ -816,13 +817,13 @@ int CheckChipType(bool getDeviceData) { if (!getDeviceData) return 0; - uint32_t word = 0; - save_restoreGB(GRAPH_SAVE); - + save_restoreDB(GRAPH_SAVE); + //check for em4x05/em4x69 chips first + uint32_t word = 0; if (EM4x05IsBlock0(&word)) { + PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nTry lf em 4x05... commands\n"); save_restoreGB(GRAPH_RESTORE); - PrintAndLog("\nValid EM4x05/EM4x69 Chipset found\nTry `lf em 4x05` commands\n"); return 1; } @@ -830,7 +831,6 @@ int CheckChipType(bool getDeviceData) { if (tryDetectP1(true)) { PrintAndLog("\nValid T55xx Chip Found\nTry `lf t55xx` commands\n"); save_restoreGB(GRAPH_RESTORE); - return 1; } @@ -866,20 +866,13 @@ int CmdLFfind(const char *Cmd) { if (getDeviceData) { // only run if graphbuffer is just noise as it should be for hitag/cotag - if (graphJustNoise(GraphBuffer, testLen)) { + if (is_justnoise(GraphBuffer, testLen)) { if (CheckChipType(getDeviceData) ) return 1; - ans=CmdLFHitagReader("26"); - if (ans==0) - return 1; - - ans=CmdCOTAGRead(""); - if (ans>0){ - PrintAndLog("\nValid COTAG ID Found!"); - return 1; - } + ans=CmdLFHitagReader("26"); if (ans==0) {PrintAndLog("\nValid Hitag Found!");return 1;} + ans=CmdCOTAGRead(""); if (ans>0) {PrintAndLog("\nValid COTAG ID Found!"); return 1;} PrintAndLog("Signal looks just like noise. Quitting."); return 0; } @@ -888,135 +881,32 @@ int CmdLFfind(const char *Cmd) { // identify chipset CheckChipType(getDeviceData); - ans=CmdIOProxDemod(""); - if (ans>0) { - PrintAndLog("\nValid IO Prox ID Found!"); - return CheckChipType(getDeviceData); - } + ans=CmdAWIDDemod(""); if (ans>0) { PrintAndLog("\nValid AWID ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdEM410xDemod(""); if (ans>0) { PrintAndLog("\nValid EM410x ID Found!"); return CheckChipType(getDeviceData);} + ans=EM4x50Read("", false); if (ans>0) { PrintAndLog("\nValid EM4x50 ID Found!"); return 1;} + ans=CmdFdxDemod(""); if (ans>0) { PrintAndLog("\nValid FDX-B ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdGuardDemod(""); if (ans>0) { PrintAndLog("\nValid Guardall G-Prox II ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdHIDDemod(""); if (ans>0) { PrintAndLog("\nValid HID Prox ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdPSKIdteck(""); if (ans>0) { PrintAndLog("\nValid Idteck ID Found!"); return CheckChipType(getDeviceData);} - ans=CmdPyramidDemod(""); - if (ans>0) { - PrintAndLog("\nValid Pyramid ID Found!"); - return CheckChipType(getDeviceData); - } + ans=CmdIndalaDemod(""); if (ans>0) { PrintAndLog("\nValid Indala ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdIOProxDemod(""); if (ans>0) { PrintAndLog("\nValid IO Prox ID Found!");return CheckChipType(getDeviceData);} + ans=CmdJablotronDemod(""); if (ans>0) { PrintAndLog("\nValid Jablotron ID Found!"); return CheckChipType(getDeviceData);} - ans=CmdParadoxDemod(""); - if (ans>0) { - PrintAndLog("\nValid Paradox ID Found!"); - return CheckChipType(getDeviceData); - } + ans=CmdLFNedapDemod(""); if (ans>0) { PrintAndLog("\nValid NEDAP ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdNexWatchDemod(""); if (ans>0) { PrintAndLog("\nValid NexWatch ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdNoralsyDemod(""); if (ans>0) { PrintAndLog("\nValid Noralsy ID Found!"); return CheckChipType(getDeviceData);} - ans=CmdAWIDDemod(""); - if (ans>0) { - PrintAndLog("\nValid AWID ID Found!"); - return CheckChipType(getDeviceData); - } + ans=CmdPacDemod(""); if (ans>0) { PrintAndLog("\nValid PAC/Stanley ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdParadoxDemod(""); if (ans>0) { PrintAndLog("\nValid Paradox ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdPrescoDemod(""); if (ans>0) { PrintAndLog("\nValid Presco ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdPyramidDemod(""); if (ans>0) { PrintAndLog("\nValid Pyramid ID Found!"); return CheckChipType(getDeviceData);} - ans=CmdHIDDemod(""); - if (ans>0) { - PrintAndLog("\nValid HID Prox ID Found!"); - return CheckChipType(getDeviceData); - } + ans=CmdSecurakeyDemod(""); if (ans>0) { PrintAndLog("\nValid Securakey ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdVikingDemod(""); if (ans>0) { PrintAndLog("\nValid Viking ID Found!"); return CheckChipType(getDeviceData);} + ans=CmdVisa2kDemod(""); if (ans>0) { PrintAndLog("\nValid Visa2000 ID Found!"); return CheckChipType(getDeviceData);} - ans=CmdAskEM410xDemod(""); - if (ans>0) { - PrintAndLog("\nValid EM410x ID Found!"); - return CheckChipType(getDeviceData); - } - - ans=CmdVisa2kDemod(""); - if (ans>0) { - PrintAndLog("\nValid Visa2000 ID Found!"); - return CheckChipType(getDeviceData); - } - ans=CmdGuardDemod(""); - if (ans>0) { - PrintAndLog("\nValid Guardall G-Prox II ID Found!"); - return CheckChipType(getDeviceData); - } - - ans=CmdFdxDemod(""); //biphase - if (ans>0) { - PrintAndLog("\nValid FDX-B ID Found!"); - return CheckChipType(getDeviceData); - } - ans=EM4x50Read("", false); - if (ans>0) { - PrintAndLog("\nValid EM4x50 ID Found!"); - return 1; - } - - ans=CmdJablotronDemod(""); - if (ans>0) { - PrintAndLog("\nValid Jablotron ID Found!"); - return CheckChipType(getDeviceData); - } - - ans=CmdNoralsyDemod(""); - if (ans>0) { - PrintAndLog("\nValid Noralsy ID Found!"); - return CheckChipType(getDeviceData); - } - - ans=CmdSecurakeyDemod(""); - if (ans>0) { - PrintAndLog("\nValid Securakey ID Found!"); - return CheckChipType(getDeviceData); - } - - ans=CmdVikingDemod(""); - if (ans>0) { - PrintAndLog("\nValid Viking ID Found!"); - return CheckChipType(getDeviceData); - } - ans=CmdIndalaDemod(""); - if (ans>0) { - PrintAndLog("\nValid Indala ID Found!"); - return CheckChipType(getDeviceData); - } - - ans=CmdNexWatchDemod(""); - if (ans>0) { - PrintAndLog("\nValid NexWatch ID Found!"); - return CheckChipType(getDeviceData); - } - ans=CmdPSKIdteck(""); - if (ans>0) { - PrintAndLog("\nValid Idteck ID Found!"); - return CheckChipType(getDeviceData); - } - ans=CmdJablotronDemod(""); - if (ans>0) { - PrintAndLog("\nValid Jablotron ID Found!"); - return CheckChipType(getDeviceData); - } - ans=CmdLFNedapDemod(""); - if (ans>0) { - PrintAndLog("\nValid NEDAP ID Found!"); - return CheckChipType(getDeviceData); - } - ans=CmdVisa2kDemod(""); - if (ans>0) { - PrintAndLog("\nValid Visa2000 ID Found!"); - return CheckChipType(getDeviceData); - } - ans=CmdNoralsyDemod(""); - if (ans>0) { - PrintAndLog("\nValid Noralsy ID Found!"); - return CheckChipType(getDeviceData); - } - ans=CmdPrescoDemod(""); - if (ans>0) { - PrintAndLog("\nValid Presco ID Found!"); - return CheckChipType(getDeviceData); - } - ans=CmdPacDemod(""); - if (ans>0) { - PrintAndLog("\nValid PAC/Stanley ID Found!"); - return CheckChipType(getDeviceData); - } - // TIdemod? PrintAndLog("\nNo Known Tags Found!\n"); if (testRaw=='u' || testRaw=='U'){ @@ -1074,6 +964,7 @@ int CmdLFfind(const char *Cmd) { PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod"); return CheckChipType(getDeviceData); } + ans = CheckChipType(getDeviceData); PrintAndLog("\nNo Data Found!\n"); } return 0; diff --git a/client/cmdlfem4x.c b/client/cmdlfem4x.c index e23f06ea5..f1ac2a7a3 100644 --- a/client/cmdlfem4x.c +++ b/client/cmdlfem4x.c @@ -16,16 +16,16 @@ static int CmdHelp(const char *Cmd); //////////////// 410x commands int usage_lf_em410x_demod(void){ - PrintAndLog("Usage: data askem410xdemod [clock] <0|1> [maxError]"); + PrintAndLog("Usage: lf em 410x_demod [clock] <0|1> [maxError]"); PrintAndLog(" [set clock as integer] optional, if not set, autodetect."); PrintAndLog(" , 1 for invert output"); PrintAndLog(" [set maximum allowed errors], default = 100."); PrintAndLog(""); - PrintAndLog(" sample: data askem410xdemod = demod an EM410x Tag ID from GraphBuffer"); - PrintAndLog(" : data askem410xdemod 32 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32"); - PrintAndLog(" : data askem410xdemod 32 1 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32 and inverting data"); - PrintAndLog(" : data askem410xdemod 1 = demod an EM410x Tag ID from GraphBuffer while inverting data"); - 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"); + PrintAndLog(" sample: lf em 410x_demod = demod an EM410x Tag ID from GraphBuffer"); + PrintAndLog(" : lf em 410x_demod 32 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32"); + PrintAndLog(" : lf em 410x_demod 32 1 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32 and inverting data"); + PrintAndLog(" : lf em 410x_demod 1 = demod an EM410x Tag ID from GraphBuffer while inverting data"); + PrintAndLog(" : lf em 410x_demod 64 1 0 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/64 and inverting data and allowing 0 demod errors"); return 0; } int usage_lf_em410x_write(void) { @@ -188,6 +188,14 @@ int usage_lf_em4x05_info(void) { return 0; } +/* Read the ID of an EM410x tag. + * Format: + * 1111 1111 1 <-- standard non-repeatable header + * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID + * .... + * CCCC <-- each bit here is parity for the 10 bits above in corresponding column + * 0 <-- stop bit, end of tag + */ // Construct the graph for emulating an EM410X tag void ConstructEM410xEmulGraph(const char *uid,const uint8_t clock) { @@ -342,19 +350,25 @@ void printEM410x(uint32_t hi, uint64_t id) { */ int AskEm410xDecode(bool verbose, uint32_t *hi, uint64_t *lo ) { size_t idx = 0; - size_t size = DemodBufferLen; - uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0}; - memcpy(BitStream, DemodBuffer, size); - int ans = Em410xDecode(BitStream, &size, &idx, hi, lo); + uint8_t bits[512] = {0}; + size_t size = sizeof(bits); + if ( !getDemodBuf(bits, &size) ) { + PrintAndLog("DEBUG: Error - Em410x problem during copy from ASK demod"); + return 0; + } + + int ans = Em410xDecode(bits, &size, &idx, hi, lo); if ( ans < 0){ if (g_debugMode){ if (ans == -1) PrintAndLog("DEBUG: Error - Em410x not only 0|1 in decoded bitstream"); else if (ans == -2) - PrintAndLog("DEBUG: Error - Em410x preamble not found"); - else if (ans == -3) - PrintAndLog("DEBUG: Error - Em410x Size not correct: %d", size); + PrintAndLog("DEBUG: Error - Em410x not enough samples after demod"); else if (ans == -4) + PrintAndLog("DEBUG: Error - Em410x preamble not found"); + else if (ans == -5) + PrintAndLog("DEBUG: Error - Em410x Size not correct: %d", size); + else if (ans == -6) PrintAndLog("DEBUG: Error - Em410x parity failed"); } return 0; @@ -365,12 +379,14 @@ int AskEm410xDecode(bool verbose, uint32_t *hi, uint64_t *lo ) { } //set GraphBuffer for clone or sim command - setDemodBuf(BitStream, size, idx); + setDemodBuf(DemodBuffer, (size==40) ? 64 : 128, idx+1); + setClockGrid(g_DemodClock, g_DemodStartIdx + ((idx+1)*g_DemodClock)); if (g_debugMode){ PrintAndLog("DEBUG: Em410x idx: %d, Len: %d, Printing Demod Buffer:", idx, size); printDemodBuff(); } + printf("ice B %d \n", verbose); if (verbose) printEM410x(*hi, *lo); @@ -382,21 +398,16 @@ int AskEm410xDemod(const char *Cmd, uint32_t *hi, uint64_t *lo, bool verbose) { return AskEm410xDecode(verbose, hi, lo); } -//by marshmellow -//takes 3 arguments - clock, invert and maxErr as integers -//attempts to demodulate ask while decoding manchester -//prints binary found and saves in graphbuffer for further commands -int CmdAskEM410xDemod(const char *Cmd) { - char cmdp = param_getchar(Cmd, 0); - if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') - return usage_lf_em410x_demod(); - - uint64_t lo = 0; - uint32_t hi = 0; - return AskEm410xDemod(Cmd, &hi, &lo, true); +// this read is the "normal" read, which download lf signal and tries to demod here. +int CmdEM410xRead(const char *Cmd) { + lf_read(true, 8192); + CmdEM410xDemod(Cmd); + return 0; } -int CmdEMdemodASK(const char *Cmd) { +// this read loops on device side. +// uses the demod in lfops.c +int CmdEM410xRead_device(const char *Cmd) { char cmdp = param_getchar(Cmd, 0); uint8_t findone = (cmdp == '1') ? 1 : 0; UsbCommand c = {CMD_EM410X_DEMOD, {findone, 0, 0}}; @@ -404,21 +415,19 @@ int CmdEMdemodASK(const char *Cmd) { return 0; } -/* Read the ID of an EM410x tag. - * Format: - * 1111 1111 1 <-- standard non-repeatable header - * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID - * .... - * CCCC <-- each bit here is parity for the 10 bits above in corresponding column - * 0 <-- stop bit, end of tag - */ -int CmdEM410xRead(const char *Cmd) { + //by marshmellow +//takes 3 arguments - clock, invert and maxErr as integers +//attempts to demodulate ask while decoding manchester +//prints binary found and saves in graphbuffer for further commands +int CmdEM410xDemod(const char *Cmd) { + char cmdp = param_getchar(Cmd, 0); + if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_demod(); + uint32_t hi = 0; uint64_t lo = 0; - if(!AskEm410xDemod("", &hi, &lo, false)) return 0; - - printEM410x(hi, lo); + if(AskEm410xDemod(Cmd, &hi, &lo, true) != 1) return 0; + g_em410xid = lo; return 1; } @@ -1138,7 +1147,7 @@ int EM4x05ReadWord_ext(uint8_t addr, uint32_t pwd, bool usePwd, uint32_t *word) return -1; } int testLen = (GraphTraceLen < 1000) ? GraphTraceLen : 1000; - if (graphJustNoise(GraphBuffer, testLen)) { + if (is_justnoise(GraphBuffer, testLen)) { PrintAndLog("no tag found"); return -1; } @@ -1414,7 +1423,8 @@ int CmdEM4x05Info(const char *Cmd) { static command_t CommandTable[] = { {"help", CmdHelp, 1, "This help"}, - {"410x_demod", CmdEMdemodASK, 0, "Extract ID from EM410x tag on antenna)"}, + //{"410x_demod", CmdEMdemodASK, 0, "Extract ID from EM410x tag on antenna)"}, + {"410x_demod", CmdEM410xDemod, 0, "Extract ID from EM410x tag on antenna)"}, {"410x_read", CmdEM410xRead, 1, "Extract ID from EM410x tag from GraphBuffer"}, {"410x_sim", CmdEM410xSim, 0, "simulate EM410x tag"}, {"410x_brute", CmdEM410xBrute, 0, "Reader bruteforce attack by simulating EM410x tags"}, diff --git a/client/cmdlfem4x.h b/client/cmdlfem4x.h index 464ae365c..aa044b9c7 100644 --- a/client/cmdlfem4x.h +++ b/client/cmdlfem4x.h @@ -28,10 +28,9 @@ extern int CmdLFEM4X(const char *Cmd); -extern int CmdEMdemodASK(const char *Cmd); -extern int CmdAskEM410xDemod(const char *Cmd); -extern int CmdEM410xRead(const char *Cmd); +extern int CmdEM410xDemod(const char *Cmd); +extern int CmdEM410xRead(const char *Cmd); extern int CmdEM410xSim(const char *Cmd); extern int CmdEM410xBrute(const char *Cmd); extern int CmdEM410xWatch(const char *Cmd); @@ -52,7 +51,6 @@ extern void printEM410x(uint32_t hi, uint64_t id); extern int AskEm410xDecode(bool verbose, uint32_t *hi, uint64_t *lo ); extern int AskEm410xDemod(const char *Cmd, uint32_t *hi, uint64_t *lo, bool verbose); - extern int usage_lf_em410x_sim(void); extern int usage_lf_em410x_ws(void); extern int usage_lf_em410x_clone(void); diff --git a/client/graph.c b/client/graph.c index aa22b36e9..8bccb7aff 100644 --- a/client/graph.c +++ b/client/graph.c @@ -18,12 +18,10 @@ int GraphBuffer[MAX_GRAPH_TRACE_LEN]; int GraphTraceLen; - int s_Buff[MAX_GRAPH_TRACE_LEN]; /* write a manchester bit to the graph */ -void AppendGraph(int redraw, int clock, int bit) -{ +void AppendGraph(int redraw, int clock, int bit) { int i; //set first half the clock bit (all 1's or 0's for a 0 or 1 bit) for (i = 0; i < (int)(clock / 2); ++i) @@ -37,8 +35,7 @@ void AppendGraph(int redraw, int clock, int bit) } // clear out our graph window -int ClearGraph(int redraw) -{ +int ClearGraph(int redraw) { int gtl = GraphTraceLen; memset(GraphBuffer, 0x00, GraphTraceLen); GraphTraceLen = 0; @@ -47,17 +44,16 @@ int ClearGraph(int redraw) return gtl; } // option '1' to save GraphBuffer any other to restore -void save_restoreGB(uint8_t saveOpt) -{ +void save_restoreGB(uint8_t saveOpt) { static int SavedGB[MAX_GRAPH_TRACE_LEN]; - static int SavedGBlen=0; + static int SavedGBlen = 0; static bool GB_Saved = false; - static int SavedGridOffsetAdj=0; + static int SavedGridOffsetAdj = 0; if (saveOpt == GRAPH_SAVE) { //save memcpy(SavedGB, GraphBuffer, sizeof(GraphBuffer)); SavedGBlen = GraphTraceLen; - GB_Saved=true; + GB_Saved = true; SavedGridOffsetAdj = GridOffset; } else if (GB_Saved){ //restore memcpy(GraphBuffer, SavedGB, sizeof(GraphBuffer)); @@ -69,9 +65,8 @@ void save_restoreGB(uint8_t saveOpt) } // DETECT CLOCK NOW IN LFDEMOD.C -void setGraphBuf(uint8_t *buff, size_t size) -{ - if ( buff == NULL ) return; +void setGraphBuf(uint8_t *buf, size_t size) { + if ( buf == NULL ) return; ClearGraph(0); @@ -79,27 +74,26 @@ void setGraphBuf(uint8_t *buff, size_t size) size = MAX_GRAPH_TRACE_LEN; for (uint16_t i = 0; i < size; ++i) - GraphBuffer[i] = buff[i] - 128; + GraphBuffer[i] = buf[i] - 128; GraphTraceLen = size; RepaintGraphWindow(); return; } -size_t getFromGraphBuf(uint8_t *buff) -{ - if (buff == NULL ) return 0; +size_t getFromGraphBuf(uint8_t *buf) { + + if (buf == NULL ) return 0; uint32_t i; for (i=0; i < GraphTraceLen; ++i){ if (GraphBuffer[i] > 127) GraphBuffer[i] = 127; //trim if (GraphBuffer[i] < -127) GraphBuffer[i] = -127; //trim - buff[i] = (uint8_t)(GraphBuffer[i]+128); + buf[i] = (uint8_t)(GraphBuffer[i]+128); } return i; } // A simple test to see if there is any data inside Graphbuffer. bool HasGraphData(){ - if ( GraphTraceLen <= 0) { PrintAndLog("No data available, try reading something first"); return false; @@ -109,6 +103,7 @@ bool HasGraphData(){ // Detect high and lows in Grapbuffer. // Only loops the first 256 values. +// Optional: 12% fuzz in case highs and lows aren't clipped void DetectHighLowInGraph(int *high, int *low, bool addFuzz) { uint8_t loopMax = 255; @@ -130,8 +125,7 @@ void DetectHighLowInGraph(int *high, int *low, bool addFuzz) { } // Get or auto-detect ask clock rate -int GetAskClock(const char str[], bool printAns, bool verbose) -{ +int GetAskClock(const char str[], bool printAns, bool verbose) { int clock; sscanf(str, "%i", &clock); if (!strcmp(str, "")) @@ -140,7 +134,7 @@ int GetAskClock(const char str[], bool printAns, bool verbose) if (clock != 0) return clock; // Auto-detect clock - uint8_t grph[MAX_GRAPH_TRACE_LEN]={0}; + uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0}; size_t size = getFromGraphBuf(grph); if (size == 0) { if (verbose) @@ -162,8 +156,7 @@ int GetAskClock(const char str[], bool printAns, bool verbose) return clock; } -uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose) -{ +uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose) { uint8_t carrier = 0; uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0}; size_t size = getFromGraphBuf(grph); @@ -182,8 +175,7 @@ uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose) return carrier; } -int GetPskClock(const char str[], bool printAns, bool verbose) -{ +int GetPskClock(const char str[], bool printAns, bool verbose) { int clock; sscanf(str, "%i", &clock); if (!strcmp(str, "")) @@ -208,8 +200,7 @@ int GetPskClock(const char str[], bool printAns, bool verbose) return clock; } -uint8_t GetNrzClock(const char str[], bool printAns, bool verbose) -{ +uint8_t GetNrzClock(const char str[], bool printAns, bool verbose) { int clock; sscanf(str, "%i", &clock); if (!strcmp(str, "")) @@ -236,8 +227,7 @@ uint8_t GetNrzClock(const char str[], bool printAns, bool verbose) } //by marshmellow //attempt to detect the field clock and bit clock for FSK -uint8_t GetFskClock(const char str[], bool printAns, bool verbose) -{ +uint8_t GetFskClock(const char str[], bool printAns, bool verbose) { int clock; sscanf(str, "%i", &clock); if (!strcmp(str, "")) @@ -282,8 +272,7 @@ uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose, int *f } // test samples are not just noise -bool graphJustNoise(int *bits, int size) -{ +bool is_justnoise(int *bits, int size) { //might not be high enough for noisy environments #define THRESHOLD 15; bool isNoise = true; diff --git a/client/graph.h b/client/graph.h index b20ae4e84..d4043a68a 100644 --- a/client/graph.h +++ b/client/graph.h @@ -14,7 +14,6 @@ void AppendGraph(int redraw, int clock, int bit); int ClearGraph(int redraw); -//int DetectClock(int peak); size_t getFromGraphBuf(uint8_t *buff); int GetAskClock(const char str[], bool printAns, bool verbose); int GetPskClock(const char str[], bool printAns, bool verbose); @@ -22,8 +21,7 @@ uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose); uint8_t GetNrzClock(const char str[], bool printAns, bool verbose); uint8_t GetFskClock(const char str[], bool printAns, bool verbose); uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose, int *firstClockEdge); -//uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose); -bool graphJustNoise(int *BitStream, int size); +bool is_justnoise(int *bits, int size); void setGraphBuf(uint8_t *buff, size_t size); void save_restoreGB(uint8_t saveOpt); diff --git a/client/util.c b/client/util.c index 436b6a060..07742a3e7 100644 --- a/client/util.c +++ b/client/util.c @@ -157,19 +157,21 @@ char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t brea // make sure we don't go beyond our char array memory size_t in_index = 0, out_index = 0; - int rowlen; - if (breaks==0) - rowlen = ( len > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len; - else + + int rowlen = (len > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len; + + if ( len % breaks != 0) rowlen = ( len+(len/breaks) > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len+(len/breaks); - + +// printf("(sprint_bin_break) rowlen %d\n", rowlen); + static char buf[MAX_BIN_BREAK_LENGTH]; // 3072 + end of line characters if broken at 8 bits //clear memory memset(buf, 0x00, sizeof(buf)); char *tmp = buf; // loop through the out_index to make sure we don't go too far - for (out_index=0; out_index < rowlen-1; out_index++) { + for (out_index=0; out_index < rowlen; out_index++) { // set character sprintf(tmp++, "%u", data[in_index]); // check if a line break is needed and we have room to print it in our array @@ -184,6 +186,44 @@ char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t brea sprintf(tmp++, "%u", data[in_index]); return buf; } +/* +void sprint_bin_break_ex(uint8_t *src, size_t srclen, char *dest , uint8_t breaks) { + if ( src == NULL ) return; + if ( srclen < 1 ) return; + + // make sure we don't go beyond our char array memory + size_t in_index = 0, out_index = 0; + int rowlen; + if (breaks==0) + rowlen = ( len > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len; + else + rowlen = ( len+(len/breaks) > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len+(len/breaks); + + printf("(sprint_bin_break) rowlen %d\n", rowlen); + + // 3072 + end of line characters if broken at 8 bits + dest = (char *)malloc(MAX_BIN_BREAK_LENGTH); + if (dest == NULL) return; + + //clear memory + memset(dest, 0x00, sizeof(dest)); + + // loop through the out_index to make sure we don't go too far + for (out_index=0; out_index < rowlen-1; out_index++) { + // set character + sprintf(dest++, "%u", src[in_index]); + // check if a line break is needed and we have room to print it in our array + if ( (breaks > 0) && !((in_index+1) % breaks) && (out_index+1 != rowlen) ) { + // increment and print line break + out_index++; + sprintf(dest++, "%s","\n"); + } + in_index++; + } + // last char. + sprintf(dest++, "%u", src[in_index]); +} +*/ char *sprint_bin(const uint8_t *data, const size_t len) { return sprint_bin_break(data, len, 0); diff --git a/common/lfdemod.c b/common/lfdemod.c index 86f2e0703..df2ed279e 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -176,6 +176,7 @@ bool preambleSearch(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t * //by marshmellow // search for given preamble in given BitStream and return success=1 or fail=0 and startIndex (where it was found) and length if not fineone // fineone does not look for a repeating preamble for em4x05/4x69 sends preamble once, so look for it once in the first pLen bits +//(iceman) FINDONE, only finds start index. NOT SIZE!. I see Em410xDecode (lfdemod.c) uses SIZE to determine success bool preambleSearchEx(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx, bool findone) { // Sanity check. If preamble length is bigger than bitstream length. @@ -184,20 +185,21 @@ bool preambleSearchEx(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t uint8_t foundCnt = 0; for (size_t idx = 0; idx < *size - pLen; idx++) { if (memcmp(BitStream+idx, preamble, pLen) == 0){ - if (g_debugMode) prnt("DEBUG: preamble found at %i", idx); //first index found foundCnt++; if (foundCnt == 1){ + if (g_debugMode) prnt("DEBUG: (preambleSearchEx) preamble found at %i", idx); *startIdx = idx; if (findone) return true; } if (foundCnt == 2){ + if (g_debugMode) prnt("DEBUG: (preambleSearchEx) preamble 2 found at %i", idx); *size = idx - *startIdx; return true; } } } - return false; + return (foundCnt > 0); } // find start of modulating data (for fsk and psk) in case of beginning noise or slow chip startup. @@ -445,7 +447,7 @@ int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr) { if (!clockFnd){ if (DetectCleanAskWave(dest, size, peak, low)==1){ int ans = DetectStrongAskClock(dest, size, peak, low, clock); - if (g_debugMode==2) prnt("DEBUG ASK: detectaskclk Clean Ask Wave Detected: clk %i, ShortestWave: %i", clock ,ans); + if (g_debugMode==2) prnt("DEBUG ASK: detectaskclk Clean Ask Wave Detected: clk %i, ShortestWave: %i", *clock ,ans); if (ans > 0){ return ans; //return shortest wave start position } @@ -1262,7 +1264,7 @@ int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int if (smplCnt > clk-(clk/4)-1) { //full clock if (smplCnt > clk + (clk/4)+1) { //too many samples errCnt++; - if (g_debugMode==2) prnt("DEBUG ASK: Modulation Error at: %u", i); + if (g_debugMode==2) prnt("DEBUG:(cleanAskRawDemod) ASK Modulation Error at: %u", i); BinStream[bitCnt++] = 7; } else if (waveHigh) { BinStream[bitCnt++] = invert; @@ -1320,20 +1322,22 @@ int askdemod_ext(uint8_t *BinStream, size_t *size, int *clk, int *invert, int ma size_t errCnt = 0; // if clean clipped waves detected run alternate demod if (DetectCleanAskWave(BinStream, *size, high, low)) { + if (g_debugMode==2) prnt("DEBUG ASK: Clean Wave Detected - using clean wave demod"); + errCnt = cleanAskRawDemod(BinStream, size, *clk, *invert, high, low, startIdx); - if (askType) { //askman + + if (askType) { //ask/manchester uint8_t alignPos = 0; errCnt = manrawdecode(BinStream, size, 0, &alignPos); *startIdx += *clk/2 * alignPos; - if (g_debugMode) prnt("DEBUG ASK CLEAN: startIdx %i, alignPos %u", *startIdx, alignPos); + if (g_debugMode) + prnt("DEBUG: (askdemod_ext) CLEAN: startIdx %i, alignPos %u", *startIdx, alignPos); + } return errCnt; - } else { //askraw - return errCnt; - } } - if (g_debugMode) prnt("DEBUG ASK WEAK: startIdx %i", *startIdx); - if (g_debugMode==2) prnt("DEBUG ASK: Weak Wave Detected - using weak wave demod"); + if (g_debugMode) prnt("DEBUG: (askdemod_ext) WEAK: startIdx %i", *startIdx); + if (g_debugMode==2) prnt("DEBUG: (askdemod_ext) Weak Wave Detected - using weak wave demod"); int lastBit; //set first clock check - can go negative size_t i, bitnum = 0; //output counter @@ -1351,7 +1355,7 @@ int askdemod_ext(uint8_t *BinStream, size_t *size, int *clk, int *invert, int ma BinStream[bitnum++] = *invert ^ 1; } else if (i-lastBit >= *clk+tol) { if (bitnum > 0) { - if (g_debugMode==2) prnt("DEBUG ASK: Modulation Error at: %u", i); + if (g_debugMode==2) prnt("DEBUG: (askdemod_ext) Modulation Error at: %u", i); BinStream[bitnum++]=7; errCnt++; } @@ -1426,8 +1430,8 @@ int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, int *startId size_t fsk_wave_demod(uint8_t * dest, size_t size, uint8_t fchigh, uint8_t fclow, int *startIdx) { size_t last_transition = 0; size_t idx = 1; - if (fchigh==0) fchigh=10; - if (fclow==0) fclow=8; + if (fchigh == 0) fchigh = 10; + if (fclow == 0) fclow = 8; //set the threshold close to 0 (graph) or 128 std to avoid static size_t preLastSample = 0; size_t LastSample = 0; @@ -1486,16 +1490,16 @@ size_t fsk_wave_demod(uint8_t * dest, size_t size, uint8_t fchigh, uint8_t fclow //rfLen = clock, fchigh = larger field clock, fclow = smaller field clock size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx) { uint8_t lastval = dest[0]; - size_t idx=0; - size_t numBits=0; - uint32_t n=1; - for( idx=1; idx < size; idx++) { + size_t idx = 0; + size_t numBits = 0; + uint32_t n = 1; + for( idx = 1; idx < size; idx++) { n++; - if (dest[idx]==lastval) continue; //skip until we hit a transition + if (dest[idx] == lastval) continue; //skip until we hit a transition //find out how many bits (n) we collected (use 1/2 clk tolerance) //if lastval was 1, we have a 1->0 crossing - if (dest[idx-1]==1) { + if (dest[idx-1] == 1) { n = (n * fclow + rfLen/2) / rfLen; } else {// 0->1 crossing n = (n * fchigh + rfLen/2) / rfLen; @@ -1506,27 +1510,27 @@ size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, if (numBits == 0) { if (lastval == 1) { //high to low *startIdx += (fclow * idx) - (n*rfLen); - if (g_debugMode==2) prnt("DEBUG FSK: startIdx %i, fclow*idx %i, n*rflen %u", *startIdx, fclow*(idx), n*rfLen); + if (g_debugMode == 2) prnt("DEBUG (aggregate_bits) FSK startIdx %i, fclow*idx %i, n*rflen %u", *startIdx, fclow*(idx), n*rfLen); } else { *startIdx += (fchigh * idx) - (n*rfLen); - if (g_debugMode==2) prnt("DEBUG FSK: startIdx %i, fchigh*idx %i, n*rflen %u", *startIdx, fchigh*(idx), n*rfLen); + if (g_debugMode == 2) prnt("DEBUG (aggregate_bits) FSK startIdx %i, fchigh*idx %i, n*rflen %u", *startIdx, fchigh*(idx), n*rfLen); } } //add to our destination the bits we collected - memset(dest+numBits, dest[idx-1]^invert , n); + memset(dest+numBits, dest[idx-1] ^ invert , n); numBits += n; - n=0; - lastval=dest[idx]; + n = 0; + lastval = dest[idx]; }//end for // if valid extra bits at the end were all the same frequency - add them in if (n > rfLen/fchigh) { - if (dest[idx-2]==1) { + if (dest[idx-2] == 1) { n = (n * fclow + rfLen/2) / rfLen; } else { n = (n * fchigh + rfLen/2) / rfLen; } - memset(dest+numBits, dest[idx-1]^invert , n); + memset(dest+numBits, dest[idx-1] ^ invert , n); numBits += n; } return numBits; @@ -1695,9 +1699,9 @@ int detectAWID(uint8_t *dest, size_t *size, int *waveStartIdx) { //takes 1s and 0s and searches for EM410x format - output EM ID int Em410xDecode(uint8_t *bits, size_t *size, size_t *startIdx, uint32_t *hi, uint64_t *lo) { // sanity check - if (*size < 64) return -3; if (bits[1] > 1) return -1; - + if (*size < 64) return -2; + uint8_t fmtlen; *startIdx = 0; @@ -1705,11 +1709,8 @@ int Em410xDecode(uint8_t *bits, size_t *size, size_t *startIdx, uint32_t *hi, ui // include 0 in front to help get start pos uint8_t preamble[] = {0,1,1,1,1,1,1,1,1,1}; if (!preambleSearch(bits, preamble, sizeof(preamble), size, startIdx)) - return -2; + return -4; - //XL and normal size. - if (*size != 64 && *size != 128) return -3; - fmtlen = (*size == 128) ? 22 : 10; //skip last 4bit parity row for simplicity @@ -1728,7 +1729,7 @@ int Em410xDecode(uint8_t *bits, size_t *size, size_t *startIdx, uint32_t *hi, ui *lo = ((uint64_t)(bytebits_to_byte(bits + 24, 32)) << 32) | (bytebits_to_byte(bits + 24 + 32, 32)); break; } - default: return -4; + default: return -6; } return 1; }