From bd4d1ec74e965db06d7d32cae09dbb6e5a8b8b96 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 10 Aug 2017 14:19:57 +0200 Subject: [PATCH] Fixes: lf simpsk - make sure Carrier input is ok. fix: 'lf awid sim' - Clk param is used CorreCt fix: 'lf HID sim' - unified way logging fix: 'lf indala sim' - adding a draft simulation Command fix: 'lf io sim' - unified way logging fix: 'lf nedap sim' - getting bits is wrong still.. fix: 'lf paradox sim' - Helptext lfdemod.C got some reworked loops, still some debug messages to be ... --- armsrc/lfops.c | 6 +- client/cmdlf.c | 3 +- client/cmdlfawid.c | 32 ++++++----- client/cmdlfhid.c | 8 ++- client/cmdlfindala.c | 76 +++++++++++++++++------- client/cmdlfindala.h | 2 + client/cmdlfio.c | 4 +- client/cmdlfnedap.c | 12 ++-- client/cmdlfparadox.c | 2 +- client/cmdlfparadox.h | 3 + client/util.c | 1 - common/lfdemod.c | 131 +++++++++++++++++++++--------------------- common/lfdemod.h | 5 +- 13 files changed, 162 insertions(+), 123 deletions(-) diff --git a/armsrc/lfops.c b/armsrc/lfops.c index 70257b322..21f19d9bc 100644 --- a/armsrc/lfops.c +++ b/armsrc/lfops.c @@ -796,10 +796,11 @@ void CmdHIDdemodFSK(int findone, int *high, int *low, int ledcontrol) WDT_HIT(); if (ledcontrol) LED_A_ON(); - DoAcquisition_default(-1,true); + DoAcquisition_default(-1, true); // FSK demodulator size = 50*128*2; //big enough to catch 2 sequences of largest format idx = HIDdemodFSK(dest, &size, &hi2, &hi, &lo, &dummyIdx); + if ( idx < 0 ) continue; if (idx>0 && lo>0 && (size==96 || size==192)){ // go over previously decoded manchester data and decode into usable tag ID @@ -869,7 +870,6 @@ void CmdHIDdemodFSK(int findone, int *high, int *low, int ledcontrol) // reset } hi2 = hi = lo = idx = 0; - WDT_HIT(); } FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); DbpString("Stopped"); @@ -1054,7 +1054,7 @@ void CmdIOdemodFSK(int findone, int *high, int *low, int ledcontrol) //fskdemod and get start index WDT_HIT(); idx = detectIOProx(dest, &size, &dummyIdx); - if (idx<0) continue; + if (idx < 0) continue; //valid tag found //Index map diff --git a/client/cmdlf.c b/client/cmdlf.c index 2f8c8dd8e..72469a462 100644 --- a/client/cmdlf.c +++ b/client/cmdlf.c @@ -702,7 +702,8 @@ int CmdLFpskSim(const char *Cmd) { if (clk <= 0) clk = 32; - if (carrier == 0) carrier = 2; + if (carrier != 2 && carrier != 4 && carrier != 8 ) + carrier = 2; if (pskType != 1){ if (pskType == 2){ diff --git a/client/cmdlfawid.c b/client/cmdlfawid.c index 8fec408a1..5aff24288 100644 --- a/client/cmdlfawid.c +++ b/client/cmdlfawid.c @@ -43,7 +43,7 @@ int usage_lf_awid_sim(void) { PrintAndLog(""); PrintAndLog("Samples:"); PrintAndLog(" lf awid sim 26 224 1337"); - PrintAndLog(" lf awid sim 50 2001 13371337"); + PrintAndLog(" lf awid sim 50 2001 deadc0de"); return 0; } @@ -98,15 +98,16 @@ static int sendPing(void){ } static bool sendTry(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint32_t delay, uint8_t *bs, size_t bs_len){ - PrintAndLog("Trying FC: %u; CN: %u", fc, cn); if ( !getAWIDBits(fmtlen, fc, cn, bs)) { PrintAndLog("Error with tag bitstream generation."); return false; } - uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8 - uint64_t arg2 = 50; // clk RF/50 invert=0 + uint8_t clk = 50, high = 10, low = 8, invert = 1; + uint64_t arg1 = (high << 8) + low; + uint64_t arg2 = (invert << 8) + clk; + UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, bs_len}}; memcpy(c.d.asBytes, bs, bs_len); clearCommandBuffer(); @@ -228,7 +229,7 @@ int CmdAWIDRead_device(const char *Cmd) { } //by marshmellow -//AWID Prox demod - FSK RF/50 with preamble of 00000001 (always a 96 bit data stream) +//AWID Prox demod - FSK2a RF/50 with preamble of 00000001 (always a 96 bit data stream) //print full AWID Prox ID and some bit format details if found int CmdAWIDDemod(const char *Cmd) { uint8_t bits[MAX_GRAPH_TRACE_LEN]={0}; @@ -277,7 +278,8 @@ int CmdAWIDDemod(const char *Cmd) { uint32_t rawHi = bytebits_to_byte(bits + idx + 32, 32); uint32_t rawHi2 = bytebits_to_byte(bits + idx, 32); setDemodBuf(bits, 96, idx); - + setClockGrid(g_DemodClock, g_DemodStartIdx + (idx*g_DemodClock)); + size = removeParity(bits, idx+8, 4, 1, 88); if (size != 66){ if (g_debugMode) PrintAndLog("DEBUG: Error - AWID at parity check-tag size does not match AWID format"); @@ -362,13 +364,9 @@ int CmdAWIDSim(const char *Cmd) { uint32_t fc = 0, cn = 0; uint8_t fmtlen = 0; uint8_t bits[96]; - uint8_t *bs = bits; size_t size = sizeof(bits); - memset(bs, 0x00, size); + memset(bits, 0x00, size); - uint64_t arg1 = ( 10 << 8 ) + 8; // fcHigh = 10, fcLow = 8 - uint64_t arg2 = 50; // clk RF/50 invert=0 - char cmdp = param_getchar(Cmd, 0); if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_awid_sim(); @@ -382,16 +380,22 @@ int CmdAWIDSim(const char *Cmd) { PrintAndLog("Emulating AWID %u -- FC: %u; CN: %u\n", fmtlen, fc, cn); PrintAndLog("Press pm3-button to abort simulation or run another command"); - if (!getAWIDBits(fmtlen, fc, cn, bs)) { + if (!getAWIDBits(fmtlen, fc, cn, bits)) { PrintAndLog("Error with tag bitstream generation."); return 1; } - // AWID uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0 + + uint8_t clk = 50, high = 10, low = 8, invert = 1; + uint64_t arg1 = (high << 8) + low; + uint64_t arg2 = (invert << 8) + clk; + + + // AWID uses: FSK2a fcHigh: 10, fcLow: 8, clk: 50, invert: 1 // arg1 --- fcHigh<<8 + fcLow // arg2 --- Inversion and clk setting // 96 --- Bitstream length: 96-bits == 12 bytes UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}}; - memcpy(c.d.asBytes, bs, size); + memcpy(c.d.asBytes, bits, size); clearCommandBuffer(); SendCommand(&c); return 0; diff --git a/client/cmdlfhid.c b/client/cmdlfhid.c index 3a910d5e9..d2e39823c 100644 --- a/client/cmdlfhid.c +++ b/client/cmdlfhid.c @@ -133,12 +133,14 @@ int CmdHIDDemod(const char *Cmd) { if (idx < 0) { if (g_debugMode){ if (idx==-1){ - PrintAndLog("DEBUG: Error - HID just noise detected"); + PrintAndLog("DEBUG: Error - HID not enough samples"); } else if (idx == -2) { - PrintAndLog("DEBUG: Error - HID problem during FSK demod"); + PrintAndLog("DEBUG: Error - HID just noise detected"); } else if (idx == -3) { - PrintAndLog("DEBUG: Error - HID preamble not found"); + PrintAndLog("DEBUG: Error - HID problem during FSK demod"); } else if (idx == -4) { + PrintAndLog("DEBUG: Error - HID preamble not found"); + } else if (idx == -5) { PrintAndLog("DEBUG: Error - HID error in Manchester data, SIZE: %d", BitLen); } else { PrintAndLog("DEBUG: Error - HID error demoding fsk %d", idx); diff --git a/client/cmdlfindala.c b/client/cmdlfindala.c index 002ae821e..ee1c16685 100644 --- a/client/cmdlfindala.c +++ b/client/cmdlfindala.c @@ -26,29 +26,27 @@ int usage_lf_indala_demod(void) { } int usage_lf_indala_sim(void) { - PrintAndLog("Enables simulation of Indala card with specified facility-code and card number."); + PrintAndLog("Enables simulation of Indala card with specified uid."); PrintAndLog("Simulation runs until the button is pressed or another USB command is issued."); PrintAndLog(""); - PrintAndLog("Usage: lf indala sim [h] "); + PrintAndLog("Usage: lf indala sim [h] "); PrintAndLog("Options :"); - PrintAndLog(" h : This help"); - PrintAndLog(" : 8bit version"); - PrintAndLog(" : 8bit value facility code"); - PrintAndLog(" : 16bit value card number"); + PrintAndLog(" h : This help"); + PrintAndLog(" : 64/224 UID"); PrintAndLog(""); PrintAndLog("Samples"); - PrintAndLog(" lf indala sim 26 101 1337"); + PrintAndLog(" lf indala sim deadc0de"); return 0; } int usage_lf_indala_clone(void) { - PrintAndLog("Enables cloning of Indala card with specified facility-code and card number onto T55x7."); + PrintAndLog("Enables cloning of Indala card with specified uid onto T55x7."); PrintAndLog("The T55x7 must be on the antenna when issuing this command. T55x7 blocks are calculated and printed in the process."); PrintAndLog(""); PrintAndLog("Usage: lf indala clone [h] [Q5]"); PrintAndLog("Options :"); PrintAndLog(" h : This help"); - PrintAndLog(" : 64/221 UID"); + PrintAndLog(" : 64/224 UID"); PrintAndLog(" Q5 : optional - clone to Q5 (T5555) instead of T55x7 chip"); PrintAndLog(""); PrintAndLog("Samples"); @@ -56,6 +54,23 @@ int usage_lf_indala_clone(void) { return 0; } +// redesigned by marshmellow adjusted from existing decode functions +// indala id decoding - only tested on 26 bit tags, but attempted to make it work for more +int detectIndala26(uint8_t *dest, size_t *size, uint8_t *invert) { + //26 bit 40134 format (don't know other formats) + uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; + uint8_t preamble_i[] = {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,0}; + size_t startidx = 0; + if (!preambleSearch(dest, preamble, sizeof(preamble), size, &startidx)){ + // if didn't find preamble try again inverting + if (!preambleSearch(dest, preamble_i, sizeof(preamble_i), size, &startidx)) return -1; + *invert ^= 1; + } + if (*size != 64 && *size != 224) return -2; + + return (int) startidx; +} + // this read is the "normal" read, which download lf signal and tries to demod here. int CmdIndalaRead(const char *Cmd) { lf_read(true, 30000); @@ -124,7 +139,6 @@ int CmdIndalaDemod(const char *Cmd) { // but the other appears to currently be more accurate than this approach most of the time. int CmdIndalaDemodAlt(const char *Cmd) { // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID - int state = -1; int count = 0; int i, j; @@ -319,25 +333,44 @@ int CmdIndalaDemodAlt(const char *Cmd) { } int CmdIndalaSim(const char *Cmd) { - uint16_t cn = 0; - uint8_t bits[64]; - uint8_t *bs = bits; - size_t size = sizeof(bits); - memset(bs, 0x00, size); - uint64_t arg1 = ( 10 << 8 ) + 8; // fcHigh = 10, fcLow = 8 - uint64_t arg2 = (64 << 8)| + 1; // clk RF/64 invert=1 - char cmdp = param_getchar(Cmd, 0); if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_indala_sim(); + uint8_t bits[224]; + size_t size = sizeof(bits); + memset(bits, 0x00, size); + + // uid + uint8_t hexuid[100]; + int len = 0; + param_gethex_ex(Cmd, 0, hexuid, &len); + if ( len > 28 ) + return usage_lf_indala_sim(); + // convert to binarray + uint8_t counter = 224; + for (uint8_t i=0; i< len; i++) { + for(uint8_t j=0; j<8; j++) { + bits[counter--] = hexuid[i] & 1; + hexuid[i] >>= 1; + } + } - PrintAndLog("Emulating Indala UID: %u \n", cn); - PrintAndLog("Press pm3-button to abort simulation or run another command"); + // indala PSK + uint8_t clk = 32, carrier = 2, invert = 0; + uint16_t arg1, arg2; + arg1 = clk << 8 | carrier; + arg2 = invert; + + // It has to send either 64bits (8bytes) or 224bits (28bytes). Zero padding needed if not. + // lf simpsk 1 c 32 r 2 d 0102030405060708 + +// PrintAndLog("Emulating Indala UID: %u \n", cn); +// PrintAndLog("Press pm3-button to abort simulation or run another command"); UsbCommand c = {CMD_PSK_SIM_TAG, {arg1, arg2, size}}; - memcpy(c.d.asBytes, bs, size); + memcpy(c.d.asBytes, bits, size); clearCommandBuffer(); SendCommand(&c); return 0; @@ -347,7 +380,6 @@ int CmdIndalaSim(const char *Cmd) { int CmdIndalaClone(const char *Cmd) { UsbCommand c; uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7; - uid1 = uid2 = uid3 = uid4 = uid5 = uid6 = uid7 = 0; int n = 0, i = 0; diff --git a/client/cmdlfindala.h b/client/cmdlfindala.h index 80ccca22a..063100fab 100644 --- a/client/cmdlfindala.h +++ b/client/cmdlfindala.h @@ -33,6 +33,8 @@ extern int CmdIndalaRead(const char *Cmd); extern int CmdIndalaClone(const char *Cmd); extern int CmdIndalaSim(const char *Cmd); +extern int detectIndala26(uint8_t *bitStream, size_t *size, uint8_t *invert); + extern int usage_lf_indala_demod(void); extern int usage_lf_indala_clone(void); extern int usage_lf_indala_sim(void); diff --git a/client/cmdlfio.c b/client/cmdlfio.c index 072eae6a2..a4943e84c 100644 --- a/client/cmdlfio.c +++ b/client/cmdlfio.c @@ -101,9 +101,9 @@ int CmdIOProxDemod(const char *Cmd) { if (idx < 0){ if (g_debugMode){ if (idx == -1){ - PrintAndLog("DEBUG: Error - IO prox just noise detected"); + PrintAndLog("DEBUG: Error - IO prox not enough samples"); } else if (idx == -2) { - PrintAndLog("DEBUG: Error - IO prox not enough samples"); + PrintAndLog("DEBUG: Error - IO prox just noise detected"); } else if (idx == -3) { PrintAndLog("DEBUG: Error - IO prox error during fskdemod"); } else if (idx == -4) { diff --git a/client/cmdlfnedap.c b/client/cmdlfnedap.c index fa74a9c8a..c0fea028f 100644 --- a/client/cmdlfnedap.c +++ b/client/cmdlfnedap.c @@ -49,14 +49,13 @@ int detectNedap(uint8_t *dest, size_t *size) { return (int) startIdx; } - int GetNedapBits(uint32_t cn, uint8_t *nedapBits) { uint8_t pre[128]; memset(pre, 0x00, sizeof(pre)); - // preamble 1111 1111 10 = 0XF8 - num_to_bytebits(0xF8, 10, pre); + // preamble 1111 1111 10 = 0xFF8 + num_to_bytebits(0xFF8, 12, pre); // fixed tagtype code? 0010 1101 = 0x2D num_to_bytebits(0x2D, 8, pre+10); @@ -64,7 +63,7 @@ int GetNedapBits(uint32_t cn, uint8_t *nedapBits) { // 46 encrypted bits - UNKNOWN ALGO // -- 16 bits checksum. Should be 4x4 checksum, based on UID and 2 constant values. // -- 30 bits undocumented? - num_to_bytebits(cn, 46, pre+18); + //num_to_bytebits(cn, 46, pre+18); //----from this part, the UID in clear text, with a 1bit ZERO as separator between bytes. pre[64] = 0; @@ -95,10 +94,10 @@ int GetNedapBits(uint32_t cn, uint8_t *nedapBits) { pre[63] = GetParity( DemodBuffer, EVEN, 63); pre[127] = GetParity( DemodBuffer+64, EVEN, 63); - memcpy(nedapBits, pre, 128); -//1111111110001011010000010110100011001001000010110101001101011001000110011010010000000000100001110001001000000001000101011100111 + + // 1111111110001011010000010110100011001001000010110101001101011001000110011010010000000000100001110001001000000001000101011100111 return 1; } /* @@ -317,6 +316,7 @@ int CmdLFNedapSim(const char *Cmd) { return 1; } + PrintAndLog("bin %s", sprint_bin_break(bs, 128, 32)); PrintAndLog("Simulating Nedap - CardNumber: %u", cardnumber ); UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}}; diff --git a/client/cmdlfparadox.c b/client/cmdlfparadox.c index 6790a1c3b..b9417626d 100644 --- a/client/cmdlfparadox.c +++ b/client/cmdlfparadox.c @@ -68,7 +68,7 @@ int detectParadox(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint } //by marshmellow -//Paradox Prox demod - FSK RF/50 with preamble of 00001111 (then manchester encoded) +//Paradox Prox demod - FSK2a RF/50 with preamble of 00001111 (then manchester encoded) //print full Paradox Prox ID and some bit format details if found int CmdParadoxDemod(const char *Cmd) { //raw fsk demod no manchester decoding no start bit finding just get binary from wave diff --git a/client/cmdlfparadox.h b/client/cmdlfparadox.h index a136ba54f..44045f114 100644 --- a/client/cmdlfparadox.h +++ b/client/cmdlfparadox.h @@ -12,5 +12,8 @@ extern int CmdLFParadox(const char *Cmd); extern int CmdParadoxDemod(const char *Cmd); extern int CmdParadoxRead(const char *Cmd); +//extern int CmdParadoxClone(const char *Cmd); +extern int CmdParadoxSim(const char *Cmd); + extern int detectParadox(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo, int *waveStartIdx); #endif diff --git a/client/util.c b/client/util.c index e66170fc9..43d63826a 100644 --- a/client/util.c +++ b/client/util.c @@ -491,7 +491,6 @@ int param_getstr(const char *line, int paramnum, char * str) The following methods comes from Rfidler sourcecode. https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/src/ */ - // convert hex to sequence of 0/1 bit values // returns number of bits converted int hextobinarray(char *target, char *source) diff --git a/common/lfdemod.c b/common/lfdemod.c index 8e31ea694..9cc3f029a 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -1488,49 +1488,53 @@ size_t fsk_wave_demod(uint8_t * dest, size_t size, uint8_t fchigh, uint8_t fclow //translate 11111100000 to 10 //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) { +size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t clk, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx) { uint8_t lastval = dest[0]; - size_t idx = 0; + size_t i = 0; size_t numBits = 0; uint32_t n = 1; - for( idx = 1; idx < size; idx++) { + uint8_t hclk = clk/2; + + for( i = 1; i < size; i++) { n++; - if (dest[idx] == lastval) continue; //skip until we hit a transition + if (dest[i] == 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) { - n = (n * fclow + rfLen/2) / rfLen; + if (dest[i-1] == 1) { + n = (n * fclow + hclk) / clk; } else {// 0->1 crossing - n = (n * fchigh + rfLen/2) / rfLen; + n = (n * fchigh + hclk) / clk; } if (n == 0) n = 1; //first transition - save startidx if (numBits == 0) { if (lastval == 1) { //high to low - *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); + *startIdx += (fclow * i) - (n*clk); + if (g_debugMode == 2) prnt("DEBUG (aggregate_bits) FSK startIdx %i, fclow*idx %i, n*clk %u", *startIdx, fclow*i, n*clk); } else { - *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); + *startIdx += (fchigh * i) - (n*clk); + if (g_debugMode == 2) prnt("DEBUG (aggregate_bits) FSK startIdx %i, fchigh*idx %i, n*clk %u", *startIdx, fchigh*i, n*clk); } } //add to our destination the bits we collected - memset(dest+numBits, dest[idx-1] ^ invert , n); + memset(dest+numBits, dest[i-1] ^ invert , n); + if (g_debugMode == 2) prnt("ICCE:: n %u | numbits %u", n, numBits); numBits += n; n = 0; - lastval = dest[idx]; + lastval = dest[i]; + }//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) { - n = (n * fclow + rfLen/2) / rfLen; + if (n > clk/fchigh) { + if (dest[i-2] == 1) { + n = (n * fclow + clk/2) / clk; } else { - n = (n * fchigh + rfLen/2) / rfLen; + n = (n * fchigh + clk/2) / clk; } - memset(dest+numBits, dest[idx-1] ^ invert , n); + memset(dest+numBits, dest[i-1] ^ invert , n); numBits += n; } return numBits; @@ -1579,14 +1583,11 @@ void psk2TOpsk1(uint8_t *bits, size_t size) { //by marshmellow - demodulate PSK1 wave //uses wave lengths (# Samples) -int pskRawDemod_ext(uint8_t dest[], size_t *size, int *clock, int *invert, int *startIdx) { +int pskRawDemod_ext(uint8_t *dest, size_t *size, int *clock, int *invert, int *startIdx) { // sanity check if (*size < 170) return -1; - uint16_t loopCnt = 4096; //don't need to loop through entire array... - if (*size < loopCnt) loopCnt = *size; - uint8_t curPhase = *invert; uint8_t fc=0; size_t i=0, numBits=0, waveStart=1, waveEnd=0, firstFullWave=0, lastClkBit=0; @@ -1598,18 +1599,18 @@ int pskRawDemod_ext(uint8_t dest[], size_t *size, int *clock, int *invert, int * //if clock detect found firstfullwave... uint16_t tol = fc/2; if (firstFullWave == 0) { - //find start of modulating data in trace + //find start of modulating data in trace i = findModStart(dest, *size, fc); - //find first phase shift + //find first phase shift firstFullWave = pskFindFirstPhaseShift(dest, *size, &curPhase, i, fc, &fullWaveLen); - if (firstFullWave == 0) { - // no phase shift detected - could be all 1's or 0's - doesn't matter where we start - // so skip a little to ensure we are past any Start Signal - firstFullWave = 160; - memset(dest, curPhase, firstFullWave / *clock); - } else { - memset(dest, curPhase^1, firstFullWave / *clock); - } + if (firstFullWave == 0) { + // no phase shift detected - could be all 1's or 0's - doesn't matter where we start + // so skip a little to ensure we are past any Start Signal + firstFullWave = 160; + memset(dest, curPhase, firstFullWave / *clock); + } else { + memset(dest, curPhase^1, firstFullWave / *clock); + } } else { memset(dest, curPhase^1, firstFullWave / *clock); } @@ -1664,7 +1665,7 @@ int pskRawDemod_ext(uint8_t dest[], size_t *size, int *clock, int *invert, int * return errCnt; } -int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert) { +int pskRawDemod(uint8_t *dest, size_t *size, int *clock, int *invert) { int startIdx = 0; return pskRawDemod_ext(dest, size, clock, invert, &startIdx); } @@ -1678,14 +1679,16 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert) { // by marshmellow // FSK Demod then try to locate an AWID ID int detectAWID(uint8_t *dest, size_t *size, int *waveStartIdx) { - //make sure buffer has enough data + //make sure buffer has enough data (96bits * 50clock samples) if (*size < 96*50) return -1; if (justNoise(dest, *size)) return -2; - // FSK demodulator - *size = fskdemod(dest, *size, 50, 1, 10, 8, waveStartIdx); // fsk2a RF/50 - if (*size < 96) return -3; //did we get a good demod? + // FSK2a demodulator clock 50, invert 1, fcHigh 10, fcLow 8 + *size = fskdemod(dest, *size, 50, 1, 10, 8, waveStartIdx); + + //did we get a good demod? + if (*size < 96) return -3; uint8_t preamble[] = {0,0,0,0,0,0,0,1}; size_t startIdx = 0; @@ -1737,23 +1740,29 @@ int Em410xDecode(uint8_t *bits, size_t *size, size_t *startIdx, uint32_t *hi, ui // loop to get raw HID waveform then FSK demodulate the TAG ID from it int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo, int *waveStartIdx) { - - if (justNoise(dest, *size)) return -1; - - size_t numStart=0, startIdx=0; + //make sure buffer has data + if (*size < 96*50) return -1; + + if (justNoise(dest, *size)) return -2; + // FSK demodulator fsk2a so invert and fc/10/8 *size = fskdemod(dest, *size, 50, 1, 10, 8, waveStartIdx); - if (*size < 96*2) return -2; + + //did we get a good demod? + if (*size < 96*2) return -3; + // 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1 uint8_t preamble[] = {0,0,0,1,1,1,0,1}; + size_t startIdx = 0; if (!preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx)) - return -3; //preamble not found + return -4; //preamble not found + size_t numStart = 0; numStart = startIdx + sizeof(preamble); // final loop, go over previously decoded FSK data and manchester decode into usable tag ID for (size_t idx = numStart; (idx-numStart) < *size - sizeof(preamble); idx+=2){ if (dest[idx] == dest[idx+1]){ - return -4; //not manchester data + return -5; //not manchester data } *hi2 = (*hi2<<1)|(*hi>>31); *hi = (*hi<<1)|(*lo>>31); @@ -1781,12 +1790,17 @@ int detectIdteck(uint8_t *dest, size_t *size) { } int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx) { - if (justNoise(dest, *size)) return -1; //make sure buffer has data - if (*size < 66*64) return -2; + if (*size < 66*64) return -1; + + if (justNoise(dest, *size)) return -2; + // FSK demodulator RF/64, fsk2a so invert, and fc/10/8 *size = fskdemod(dest, *size, 64, 1, 10, 8, waveStartIdx); - if (*size < 65) return -3; //did we get a good demod? + + //did we get a good demod? + if (*size < 64) return -3; + //Index map //0 10 20 30 40 50 60 //| | | | | | | @@ -1795,9 +1809,9 @@ int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx) { //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11 // //XSF(version)facility:codeone+codetwo - //Handle the data - size_t startIdx = 0; + uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,1}; + size_t startIdx = 0; if (! preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx)) return -4; //preamble not found @@ -1807,21 +1821,4 @@ int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx) { return (int) startIdx; } return -5; -} - -// redesigned by marshmellow adjusted from existing decode functions -// indala id decoding - only tested on 26 bit tags, but attempted to make it work for more -int detectIndala26(uint8_t *dest, size_t *size, uint8_t *invert) { - //26 bit 40134 format (don't know other formats) - uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; - uint8_t preamble_i[] = {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,0}; - size_t startidx = 0; - if (!preambleSearch(dest, preamble, sizeof(preamble), size, &startidx)){ - // if didn't find preamble try again inverting - if (!preambleSearch(dest, preamble_i, sizeof(preamble_i), size, &startidx)) return -1; - *invert ^= 1; - } - if (*size != 64 && *size != 224) return -2; - - return (int) startidx; -} +} \ No newline at end of file diff --git a/common/lfdemod.h b/common/lfdemod.h index 80f03cee7..a9f36a91f 100644 --- a/common/lfdemod.h +++ b/common/lfdemod.h @@ -46,8 +46,8 @@ extern int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, extern bool parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType); extern bool preambleSearch(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx); extern bool preambleSearchEx(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx, bool findone); -extern int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert); -extern int pskRawDemod_ext(uint8_t dest[], size_t *size, int *clock, int *invert, int *startIdx); +extern int pskRawDemod(uint8_t *dest, size_t *size, int *clock, int *invert); +extern int pskRawDemod_ext(uint8_t *dest, size_t *size, int *clock, int *invert, int *startIdx); extern void psk2TOpsk1(uint8_t *BitStream, size_t size); extern void psk1TOpsk2(uint8_t *BitStream, size_t size); extern size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen); @@ -58,5 +58,4 @@ extern int Em410xDecode(uint8_t *dest, size_t *size, size_t *startIdx, uint32_t extern int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo, int *waveStartIdx); extern int detectIdteck(uint8_t *dest, size_t *size); extern int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx); -extern int detectIndala26(uint8_t *bitStream, size_t *size, uint8_t *invert); #endif