From 14331320c4fe733b60bed9e638be0989286a41e4 Mon Sep 17 00:00:00 2001 From: marshmellow42 Date: Tue, 10 Nov 2015 11:59:41 -0500 Subject: [PATCH] Fix viking demod bug - clean my indala demod code NOTE lf indalademod is still available as it uses a different technique to demodulating, which sometimes works better (and sometimes worse). --- client/cmddata.c | 64 ++++++++++++------------------------------ common/lfdemod.c | 72 ++++++++++-------------------------------------- 2 files changed, 32 insertions(+), 104 deletions(-) diff --git a/client/cmddata.c b/client/cmddata.c index 663faf80..58035278 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -649,7 +649,7 @@ int CmdVikingDemod(const char *Cmd) //call lfdemod.c demod for gProxII int ans = VikingDemod_AM(DemodBuffer, &size); if (ans < 0) { - if (g_debugMode) PrintAndLog("Error Viking_Demod"); + if (g_debugMode) PrintAndLog("Error Viking_Demod %d", ans); return 0; } //got a good demod @@ -1615,61 +1615,33 @@ int CmdIndalaDecode(const char *Cmd) return 0; } uint8_t invert=0; - ans = indala26decode(DemodBuffer, &DemodBufferLen, &invert); - if (ans < 1) { + size_t size = DemodBufferLen; + size_t startIdx = indala26decode(DemodBuffer, &size, &invert); + if (startIdx < 1) { if (g_debugMode==1) PrintAndLog("Error2: %d",ans); return -1; } - char showbits[251]={0x00}; + setDemodBuf(DemodBuffer, size, startIdx); if (invert) if (g_debugMode==1) PrintAndLog("Had to invert bits"); + PrintAndLog("BitLen: %d",DemodBufferLen); //convert UID to HEX uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7; - int idx; - uid1=0; - uid2=0; - PrintAndLog("BitLen: %d",DemodBufferLen); - if (DemodBufferLen==64){ - for( idx=0; idx<64; idx++) { - uid1=(uid1<<1)|(uid2>>31); - if (DemodBuffer[idx] == 0) { - uid2=(uid2<<1)|0; - showbits[idx]='0'; - } else { - uid2=(uid2<<1)|1; - showbits[idx]='1'; - } - } - showbits[idx]='\0'; - PrintAndLog("Indala UID=%s (%x%08x)", showbits, uid1, uid2); - } - else { - uid3=0; - uid4=0; - uid5=0; - uid6=0; - uid7=0; - for( idx=0; idx>31); - uid2=(uid2<<1)|(uid3>>31); - uid3=(uid3<<1)|(uid4>>31); - uid4=(uid4<<1)|(uid5>>31); - uid5=(uid5<<1)|(uid6>>31); - uid6=(uid6<<1)|(uid7>>31); - if (DemodBuffer[idx] == 0) { - uid7=(uid7<<1)|0; - showbits[idx]='0'; - } - else { - uid7=(uid7<<1)|1; - showbits[idx]='1'; - } - } - showbits[idx]='\0'; - PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7); + uid1=bytebits_to_byte(DemodBuffer,32); + uid2=bytebits_to_byte(DemodBuffer+32,32); + if (DemodBufferLen==64) { + PrintAndLog("Indala UID=%s (%x%08x)", sprint_bin(DemodBuffer,DemodBufferLen), uid1, uid2); + } else { + uid3=bytebits_to_byte(DemodBuffer+64,32); + uid4=bytebits_to_byte(DemodBuffer+96,32); + uid5=bytebits_to_byte(DemodBuffer+128,32); + uid6=bytebits_to_byte(DemodBuffer+160,32); + uid7=bytebits_to_byte(DemodBuffer+192,32); + PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)", + sprint_bin(DemodBuffer,DemodBufferLen), uid1, uid2, uid3, uid4, uid5, uid6, uid7); } if (g_debugMode){ PrintAndLog("DEBUG: printing demodbuffer:"); diff --git a/common/lfdemod.c b/common/lfdemod.c index c8a2c69a..32feab6a 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -602,7 +602,6 @@ int IOdemodFSK(uint8_t *dest, size_t size) // by marshmellow // find viking preamble 0xF200 in already demoded data int VikingDemod_AM(uint8_t *dest, size_t *size) { - if (justNoise(dest, *size)) return -1; //make sure buffer has data if (*size < 64*2) return -2; @@ -614,7 +613,7 @@ int VikingDemod_AM(uint8_t *dest, size_t *size) { ^ bytebits_to_byte(dest+startIdx+24,8) ^ bytebits_to_byte(dest+startIdx+32,8) ^ bytebits_to_byte(dest+startIdx+40,8) ^ bytebits_to_byte(dest+startIdx+48,8) ^ bytebits_to_byte(dest+startIdx+56,8); if ( checkCalc != 0xA8 ) return -5; - if (*size != 64) return -5; + if (*size != 64) return -6; //return start position return (int) startIdx; } @@ -1074,63 +1073,20 @@ void psk2TOpsk1(uint8_t *BitStream, size_t size) int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert) { //26 bit 40134 format (don't know other formats) - int i; - int long_wait=29;//29 leading zeros in format - int start; - int first = 0; - int first2 = 0; - int bitCnt = 0; - int ii; - // Finding the start of a UID - for (start = 0; start <= *size - 250; start++) { - first = bitStream[start]; - for (i = start; i < start + long_wait; i++) { - if (bitStream[i] != first) { - break; - } - } - if (i == (start + long_wait)) { - break; - } - } - if (start == *size - 250 + 1) { - // did not find start sequence - return -1; - } - // Inverting signal if needed - if (first == 1) { - for (i = start; i < *size; i++) { - bitStream[i] = !bitStream[i]; - } - *invert = 1; - }else *invert=0; - - int iii; - //found start once now test length by finding next one - for (ii=start+29; ii <= *size - 250; ii++) { - first2 = bitStream[ii]; - for (iii = ii; iii < ii + long_wait; iii++) { - if (bitStream[iii] != first2) { - break; - } - } - if (iii == (ii + long_wait)) { - break; - } - } - if (ii== *size - 250 + 1){ - // did not find second start sequence - return -2; - } - bitCnt=ii-start; + 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(bitStream, preamble, sizeof(preamble), size, &startidx)){ + // if didn't find preamble try again inverting + if (!preambleSearch(bitStream, preamble_i, sizeof(preamble_i), size, &startidx)) return -1; + *invert ^= 1; + } + if (*size != 64 && *size != 224) return -2; + if (*invert==1) + for (size_t i = startidx; i < *size; i++) + bitStream[i] ^= 1; - // Dumping UID - i = start; - for (ii = 0; ii < bitCnt; ii++) { - bitStream[ii] = bitStream[i++]; - } - *size=bitCnt; - return 1; + return (int) startidx; } // by marshmellow - demodulate NRZ wave (both similar enough) -- 2.39.2