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).
This commit is contained in:
marshmellow42 2015-11-10 11:59:41 -05:00
parent 3ea7254a57
commit 14331320c4
2 changed files with 32 additions and 104 deletions

View file

@ -649,7 +649,7 @@ int CmdVikingDemod(const char *Cmd)
//call lfdemod.c demod for gProxII //call lfdemod.c demod for gProxII
int ans = VikingDemod_AM(DemodBuffer, &size); int ans = VikingDemod_AM(DemodBuffer, &size);
if (ans < 0) { if (ans < 0) {
if (g_debugMode) PrintAndLog("Error Viking_Demod"); if (g_debugMode) PrintAndLog("Error Viking_Demod %d", ans);
return 0; return 0;
} }
//got a good demod //got a good demod
@ -1615,61 +1615,33 @@ int CmdIndalaDecode(const char *Cmd)
return 0; return 0;
} }
uint8_t invert=0; uint8_t invert=0;
ans = indala26decode(DemodBuffer, &DemodBufferLen, &invert); size_t size = DemodBufferLen;
if (ans < 1) { size_t startIdx = indala26decode(DemodBuffer, &size, &invert);
if (startIdx < 1) {
if (g_debugMode==1) if (g_debugMode==1)
PrintAndLog("Error2: %d",ans); PrintAndLog("Error2: %d",ans);
return -1; return -1;
} }
char showbits[251]={0x00}; setDemodBuf(DemodBuffer, size, startIdx);
if (invert) if (invert)
if (g_debugMode==1) if (g_debugMode==1)
PrintAndLog("Had to invert bits"); PrintAndLog("Had to invert bits");
PrintAndLog("BitLen: %d",DemodBufferLen);
//convert UID to HEX //convert UID to HEX
uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7; uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
int idx; uid1=bytebits_to_byte(DemodBuffer,32);
uid1=0; uid2=bytebits_to_byte(DemodBuffer+32,32);
uid2=0; if (DemodBufferLen==64) {
PrintAndLog("BitLen: %d",DemodBufferLen); PrintAndLog("Indala UID=%s (%x%08x)", sprint_bin(DemodBuffer,DemodBufferLen), uid1, uid2);
if (DemodBufferLen==64){ } else {
for( idx=0; idx<64; idx++) { uid3=bytebits_to_byte(DemodBuffer+64,32);
uid1=(uid1<<1)|(uid2>>31); uid4=bytebits_to_byte(DemodBuffer+96,32);
if (DemodBuffer[idx] == 0) { uid5=bytebits_to_byte(DemodBuffer+128,32);
uid2=(uid2<<1)|0; uid6=bytebits_to_byte(DemodBuffer+160,32);
showbits[idx]='0'; uid7=bytebits_to_byte(DemodBuffer+192,32);
} else { PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)",
uid2=(uid2<<1)|1; sprint_bin(DemodBuffer,DemodBufferLen), uid1, uid2, uid3, uid4, uid5, uid6, uid7);
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<DemodBufferLen; idx++) {
uid1=(uid1<<1)|(uid2>>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);
} }
if (g_debugMode){ if (g_debugMode){
PrintAndLog("DEBUG: printing demodbuffer:"); PrintAndLog("DEBUG: printing demodbuffer:");

View file

@ -602,7 +602,6 @@ int IOdemodFSK(uint8_t *dest, size_t size)
// by marshmellow // by marshmellow
// find viking preamble 0xF200 in already demoded data // find viking preamble 0xF200 in already demoded data
int VikingDemod_AM(uint8_t *dest, size_t *size) { int VikingDemod_AM(uint8_t *dest, size_t *size) {
if (justNoise(dest, *size)) return -1;
//make sure buffer has data //make sure buffer has data
if (*size < 64*2) return -2; 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+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); ^ bytebits_to_byte(dest+startIdx+48,8) ^ bytebits_to_byte(dest+startIdx+56,8);
if ( checkCalc != 0xA8 ) return -5; if ( checkCalc != 0xA8 ) return -5;
if (*size != 64) return -5; if (*size != 64) return -6;
//return start position //return start position
return (int) startIdx; 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) int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert)
{ {
//26 bit 40134 format (don't know other formats) //26 bit 40134 format (don't know other formats)
int i; 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};
int long_wait=29;//29 leading zeros in format 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};
int start; size_t startidx = 0;
int first = 0; if (!preambleSearch(bitStream, preamble, sizeof(preamble), size, &startidx)){
int first2 = 0; // if didn't find preamble try again inverting
int bitCnt = 0; if (!preambleSearch(bitStream, preamble_i, sizeof(preamble_i), size, &startidx)) return -1;
int ii; *invert ^= 1;
// Finding the start of a UID }
for (start = 0; start <= *size - 250; start++) { if (*size != 64 && *size != 224) return -2;
first = bitStream[start]; if (*invert==1)
for (i = start; i < start + long_wait; i++) { for (size_t i = startidx; i < *size; i++)
if (bitStream[i] != first) { bitStream[i] ^= 1;
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; return (int) startidx;
//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;
// Dumping UID
i = start;
for (ii = 0; ii < bitCnt; ii++) {
bitStream[ii] = bitStream[i++];
}
*size=bitCnt;
return 1;
} }
// by marshmellow - demodulate NRZ wave (both similar enough) // by marshmellow - demodulate NRZ wave (both similar enough)