code cleanup. re-added psk commands.

also fixed a bug in detect clock functions.
sync with master prep for pull request
This commit is contained in:
marshmellow42 2015-01-06 23:29:45 -05:00
parent c07b79fcbf
commit ba1a299ce6
7 changed files with 4174 additions and 3841 deletions

View file

@ -173,15 +173,12 @@ void ReadTItag(void)
// when we read a TI tag we sample the zerocross line at 2Mhz
// TI tags modulate a 1 as 16 cycles of 123.2Khz
// TI tags modulate a 0 as 16 cycles of 134.2Khz
#define FSAMPLE 2000000
#define FREQLO 123200
#define FREQHI 134200
#define FSAMPLE 2000000
#define FREQLO 123200
#define FREQHI 134200
signed char *dest = (signed char *)BigBuf;
int n = sizeof(BigBuf);
// int *dest = GraphBuffer;
// int n = GraphTraceLen;
// 128 bit shift register [shift3:shift2:shift1:shift0]
uint32_t shift3 = 0, shift2 = 0, shift1 = 0, shift0 = 0;
@ -263,10 +260,10 @@ void ReadTItag(void)
shift2 = ((shift2>>24) | (shift3 << 8)) & 0x0ffff;
// if r/w tag, check ident match
if ( shift3&(1<<15) ) {
if (shift3 & (1<<15) ) {
DbpString("Info: TI tag is rewriteable");
// only 15 bits compare, last bit of ident is not valid
if ( ((shift3>>16)^shift0)&0x7fff ) {
if (((shift3 >> 16) ^ shift0) & 0x7fff ) {
DbpString("Error: Ident mismatch!");
} else {
DbpString("Info: TI tag ident is valid");
@ -330,7 +327,7 @@ void AcquireTiType(void)
int i, j, n;
// tag transmission is <20ms, sampling at 2M gives us 40K samples max
// each sample is 1 bit stuffed into a uint32_t so we need 1250 uint32_t
#define TIBUFLEN 1250
#define TIBUFLEN 1250
// clear buffer
memset(BigBuf,0,sizeof(BigBuf));
@ -648,15 +645,12 @@ void CmdHIDdemodFSK(int findone, int *high, int *low, int ledcontrol)
if (ledcontrol) LED_A_ON();
DoAcquisition125k_internal(-1,true);
size = sizeof(BigBuf);
if (size < 2000) continue;
// FSK demodulator
int bitLen = HIDdemodFSK(dest,size,&hi2,&hi,&lo);
size = HIDdemodFSK(dest, sizeof(BigBuf), &hi2, &hi, &lo);
WDT_HIT();
if (bitLen>0 && lo>0){
if (size>0 && lo>0){
// final loop, go over previously decoded manchester data and decode into usable tag ID
// 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
if (hi2 != 0){ //extra large HID tags
@ -667,30 +661,30 @@ void CmdHIDdemodFSK(int findone, int *high, int *low, int ledcontrol)
uint8_t bitlen = 0;
uint32_t fc = 0;
uint32_t cardnum = 0;
if (((hi>>5)&1)==1){//if bit 38 is set then < 37 bit format is used
if (((hi>>5)&1) == 1){//if bit 38 is set then < 37 bit format is used
uint32_t lo2=0;
lo2=(((hi & 31) << 12) | (lo>>20)); //get bits 21-37 to check for format len bit
uint8_t idx3 = 1;
while(lo2>1){ //find last bit set to 1 (format len bit)
lo2=lo2>>1;
while(lo2 > 1){ //find last bit set to 1 (format len bit)
lo2=lo2 >> 1;
idx3++;
}
bitlen =idx3+19;
bitlen = idx3+19;
fc =0;
cardnum=0;
if(bitlen==26){
if(bitlen == 26){
cardnum = (lo>>1)&0xFFFF;
fc = (lo>>17)&0xFF;
}
if(bitlen==37){
if(bitlen == 37){
cardnum = (lo>>1)&0x7FFFF;
fc = ((hi&0xF)<<12)|(lo>>20);
}
if(bitlen==34){
if(bitlen == 34){
cardnum = (lo>>1)&0xFFFF;
fc= ((hi&1)<<15)|(lo>>17);
}
if(bitlen==35){
if(bitlen == 35){
cardnum = (lo>>1)&0xFFFFF;
fc = ((hi&1)<<11)|(lo>>21);
}
@ -718,7 +712,6 @@ void CmdHIDdemodFSK(int findone, int *high, int *low, int ledcontrol)
hi2 = hi = lo = 0;
}
WDT_HIT();
//SpinDelay(50);
}
DbpString("Stopped");
if (ledcontrol) LED_A_OFF();
@ -728,8 +721,7 @@ void CmdEM410xdemod(int findone, int *high, int *low, int ledcontrol)
{
uint8_t *dest = (uint8_t *)BigBuf;
size_t size=0; //, found=0;
uint32_t bitLen=0;
size_t size=0;
int clk=0, invert=0, errCnt=0;
uint64_t lo=0;
// Configure to go in 125Khz listen mode
@ -742,21 +734,22 @@ void CmdEM410xdemod(int findone, int *high, int *low, int ledcontrol)
DoAcquisition125k_internal(-1,true);
size = sizeof(BigBuf);
if (size < 2000) continue;
// FSK demodulator
//int askmandemod(uint8_t *BinStream,uint32_t *BitLen,int *clk, int *invert);
bitLen=size;
//Dbprintf("DEBUG: Buffer got");
errCnt = askmandemod(dest,&bitLen,&clk,&invert); //HIDdemodFSK(dest,size,&hi2,&hi,&lo);
//askdemod and manchester decode
errCnt = askmandemod(dest, &size, &clk, &invert);
//Dbprintf("DEBUG: ASK Got");
WDT_HIT();
if (errCnt>=0){
lo = Em410xDecode(dest,bitLen);
lo = Em410xDecode(dest,size);
//Dbprintf("DEBUG: EM GOT");
//printEM410x(lo);
if (lo>0){
Dbprintf("EM TAG ID: %02x%08x - (%05d_%03d_%08d)",(uint32_t)(lo>>32),(uint32_t)lo,(uint32_t)(lo&0xFFFF),(uint32_t)((lo>>16LL) & 0xFF),(uint32_t)(lo & 0xFFFFFF));
Dbprintf("EM TAG ID: %02x%08x - (%05d_%03d_%08d)",
(uint32_t)(lo>>32),
(uint32_t)lo,
(uint32_t)(lo&0xFFFF),
(uint32_t)((lo>>16LL) & 0xFF),
(uint32_t)(lo & 0xFFFFFF));
}
if (findone){
if (ledcontrol) LED_A_OFF();
@ -771,7 +764,6 @@ void CmdEM410xdemod(int findone, int *high, int *low, int ledcontrol)
invert=0;
errCnt=0;
size=0;
//SpinDelay(50);
}
DbpString("Stopped");
if (ledcontrol) LED_A_OFF();

View file

@ -201,6 +201,7 @@ void printBitStream(uint8_t BitStream[], uint32_t bitLen)
return;
}
//by marshmellow
//print EM410x ID in multiple formats
void printEM410x(uint64_t id)
{
if (id !=0){
@ -210,7 +211,7 @@ void printEM410x(uint64_t id)
uint32_t i=0;
for (ii=5; ii>0;ii--){
for (i=0;i<8;i++){
id2lo=(id2lo<<1LL)|((id & (iii<<(i+((ii-1)*8))))>>(i+((ii-1)*8)));
id2lo=(id2lo<<1LL) | ((id & (iii << (i+((ii-1)*8)))) >> (i+((ii-1)*8)));
}
}
//output em id
@ -228,6 +229,7 @@ void printEM410x(uint64_t id)
}
//by marshmellow
//take binary from demod buffer and see if we can find an EM410x ID
int CmdEm410xDecode(const char *Cmd)
{
uint64_t id=0;
@ -256,7 +258,7 @@ int Cmdaskmandemod(const char *Cmd)
return 0;
}
int BitLen = getFromGraphBuf(BitStream);
size_t BitLen = getFromGraphBuf(BitStream);
// PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen);
int errCnt=0;
errCnt = askmandemod(BitStream, &BitLen,&clk,&invert);
@ -293,7 +295,7 @@ int Cmdmandecoderaw(const char *Cmd)
{
int i =0;
int errCnt=0;
int bitnum=0;
size_t size=0;
uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
int high=0,low=0;
for (;i<DemodBufferLen;++i){
@ -305,18 +307,18 @@ int Cmdmandecoderaw(const char *Cmd)
PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
return 0;
}
bitnum=i;
errCnt=manrawdecode(BitStream,&bitnum);
size=i;
errCnt=manrawdecode(BitStream, &size);
if (errCnt>=20){
PrintAndLog("Too many errors: %d",errCnt);
return 0;
}
PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt);
printBitStream(BitStream,bitnum);
printBitStream(BitStream, size);
if (errCnt==0){
uint64_t id = 0;
id = Em410xDecode(BitStream,bitnum);
if (id>0) setDemodBuf(BitStream,bitnum);
id = Em410xDecode(BitStream, size);
if (id>0) setDemodBuf(BitStream, size);
printEM410x(id);
}
return 1;
@ -335,7 +337,7 @@ int CmdBiphaseDecodeRaw(const char *Cmd)
{
int i = 0;
int errCnt=0;
int bitnum=0;
size_t size=0;
int offset=0;
int high=0, low=0;
sscanf(Cmd, "%i", &offset);
@ -350,14 +352,14 @@ int CmdBiphaseDecodeRaw(const char *Cmd)
PrintAndLog("Error: please raw demod the wave first then decode");
return 0;
}
bitnum=i;
errCnt=BiphaseRawDecode(BitStream,&bitnum, offset);
size=i;
errCnt=BiphaseRawDecode(BitStream, &size, offset);
if (errCnt>=20){
PrintAndLog("Too many errors attempting to decode: %d",errCnt);
return 0;
}
PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:",offset,errCnt);
printBitStream(BitStream,bitnum);
printBitStream(BitStream, size);
PrintAndLog("\nif bitstream does not look right try offset=1");
return 1;
}
@ -377,7 +379,7 @@ int Cmdaskrawdemod(const char *Cmd)
PrintAndLog("Invalid argument: %s", Cmd);
return 0;
}
int BitLen = getFromGraphBuf(BitStream);
size_t BitLen = getFromGraphBuf(BitStream);
int errCnt=0;
errCnt = askrawdemod(BitStream, &BitLen,&clk,&invert);
if (errCnt==-1||BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
@ -571,7 +573,7 @@ int CmdFSKrawdemod(const char *Cmd)
}
PrintAndLog("Args invert: %d - Clock:%d - fchigh:%d - fclow: %d",invert,rfLen,fchigh, fclow);
uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
uint32_t BitLen = getFromGraphBuf(BitStream);
size_t BitLen = getFromGraphBuf(BitStream);
int size = fskdemod(BitStream,BitLen,(uint8_t)rfLen,(uint8_t)invert,(uint8_t)fchigh,(uint8_t)fclow);
if (size>0){
PrintAndLog("FSK decoded bitstream:");
@ -595,7 +597,7 @@ int CmdFSKdemodHID(const char *Cmd)
uint32_t hi2=0, hi=0, lo=0;
uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
uint32_t BitLen = getFromGraphBuf(BitStream);
size_t BitLen = getFromGraphBuf(BitStream);
//get binary from fsk wave
size_t size = HIDdemodFSK(BitStream,BitLen,&hi2,&hi,&lo);
if (size<0){
@ -671,7 +673,7 @@ int CmdFSKdemodIO(const char *Cmd)
//something in graphbuffer
if (GraphTraceLen < 65) return 0;
uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
uint32_t BitLen = getFromGraphBuf(BitStream);
size_t BitLen = getFromGraphBuf(BitStream);
//get binary from fsk wave
// PrintAndLog("DEBUG: got buff");
idx = IOdemodFSK(BitStream,BitLen);
@ -850,7 +852,7 @@ int PSKnrzDemod(const char *Cmd){
return -1;
}
uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
int BitLen = getFromGraphBuf(BitStream);
size_t BitLen = getFromGraphBuf(BitStream);
int errCnt=0;
errCnt = pskNRZrawDemod(BitStream, &BitLen,&clk,&invert);
if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
@ -875,7 +877,7 @@ int CmdIndalaDecode(const char *Cmd)
return 0;
}
uint8_t invert=0;
ans = indala26decode(DemodBuffer, &DemodBufferLen, &invert);
ans = indala26decode(DemodBuffer,(size_t *) &DemodBufferLen, &invert);
if (ans < 1) {
PrintAndLog("Error2: %d",ans);
return -1;
@ -962,7 +964,7 @@ void pskCleanWave2(uint8_t *bitStream, int bitLen)
int CmdPskClean(const char *Cmd)
{
uint8_t bitStream[MAX_GRAPH_TRACE_LEN]={0};
int bitLen = getFromGraphBuf(bitStream);
size_t bitLen = getFromGraphBuf(bitStream);
pskCleanWave(bitStream, bitLen);
setGraphBuf(bitStream, bitLen);
return 0;
@ -1532,7 +1534,7 @@ static command_t CommandTable[] =
{"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, "Detect ASK, PSK, or NRZ clock rate"},
{"detectclock", CmdDetectClockRate, 1, "Detect ASK clock rate"},
{"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"},
{"fskhiddemod", CmdFSKdemodHID, 1, "Demodulate graph window as a HID FSK using raw"},
{"fskiodemod", CmdFSKdemodIO, 1, "Demodulate graph window as an IO Prox FSK using raw"},

View file

@ -12,6 +12,7 @@
#include <stdlib.h>
#include <string.h>
#include <limits.h>
//#include "proxusb.h"
#include "proxmark3.h"
#include "data.h"
#include "graph.h"
@ -76,18 +77,22 @@ int CmdFlexdemod(const char *Cmd)
GraphBuffer[start] = 2;
GraphBuffer[start+1] = -2;
uint8_t bits[64] = {0x00};
int bit, sum;
uint8_t bits[64];
int bit;
i = start;
for (bit = 0; bit < 64; bit++) {
sum = 0;
for (int j = 0; j < 16; j++) {
int j;
int sum = 0;
for (j = 0; j < 16; j++) {
sum += GraphBuffer[i++];
}
bits[bit] = (sum > 0) ? 1 : 0;
if (sum > 0) {
bits[bit] = 1;
} else {
bits[bit] = 0;
}
PrintAndLog("bit %d sum %d", bit, sum);
}
@ -105,14 +110,15 @@ int CmdFlexdemod(const char *Cmd)
}
}
// HACK writing back to graphbuffer.
GraphTraceLen = 32*64;
i = 0;
int phase = 0;
for (bit = 0; bit < 64; bit++) {
phase = (bits[bit] == 0) ? 0 : 1;
if (bits[bit] == 0) {
phase = 0;
} else {
phase = 1;
}
int j;
for (j = 0; j < 32; j++) {
GraphBuffer[i++] = phase;
@ -131,10 +137,8 @@ int CmdIndalaDemod(const char *Cmd)
int state = -1;
int count = 0;
int i, j;
// worst case with GraphTraceLen=64000 is < 4096
// under normal conditions it's < 2048
uint8_t rawbits[4096];
int rawbit = 0;
int worst = 0, worstPos = 0;
@ -167,14 +171,10 @@ int CmdIndalaDemod(const char *Cmd)
count = 0;
}
}
if (rawbit>0){
PrintAndLog("Recovered %d raw bits, expected: %d", rawbit, GraphTraceLen/32);
PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
} else {
return 0;
}
} else return 0;
// Finding the start of a UID
int uidlen, long_wait;
if (strcmp(Cmd, "224") == 0) {
@ -184,7 +184,6 @@ int CmdIndalaDemod(const char *Cmd)
uidlen = 64;
long_wait = 29;
}
int start;
int first = 0;
for (start = 0; start <= rawbit - uidlen; start++) {
@ -198,7 +197,6 @@ int CmdIndalaDemod(const char *Cmd)
break;
}
}
if (start == rawbit - uidlen + 1) {
PrintAndLog("nothing to wait for");
return 0;
@ -212,12 +210,12 @@ int CmdIndalaDemod(const char *Cmd)
}
// Dumping UID
uint8_t bits[224] = {0x00};
char showbits[225] = {0x00};
uint8_t bits[224];
char showbits[225];
showbits[uidlen]='\0';
int bit;
i = start;
int times = 0;
if (uidlen > rawbit) {
PrintAndLog("Warning: not enough raw bits to get a full UID");
for (bit = 0; bit < rawbit; bit++) {
@ -239,8 +237,8 @@ int CmdIndalaDemod(const char *Cmd)
//convert UID to HEX
uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
int idx;
uid1 = uid2 = 0;
uid1=0;
uid2=0;
if (uidlen==64){
for( idx=0; idx<64; idx++) {
if (showbits[idx] == '0') {
@ -254,8 +252,11 @@ int CmdIndalaDemod(const char *Cmd)
PrintAndLog("UID=%s (%x%08x)", showbits, uid1, uid2);
}
else {
uid3 = uid4 = uid5 = uid6 = uid7 = 0;
uid3=0;
uid4=0;
uid5=0;
uid6=0;
uid7=0;
for( idx=0; idx<224; idx++) {
uid1=(uid1<<1)|(uid2>>31);
uid2=(uid2<<1)|(uid3>>31);
@ -263,19 +264,15 @@ int CmdIndalaDemod(const char *Cmd)
uid4=(uid4<<1)|(uid5>>31);
uid5=(uid5<<1)|(uid6>>31);
uid6=(uid6<<1)|(uid7>>31);
if (showbits[idx] == '0')
uid7 = (uid7<<1) | 0;
else
uid7 = (uid7<<1) | 1;
if (showbits[idx] == '0') uid7=(uid7<<1)|0;
else uid7=(uid7<<1)|1;
}
PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
}
// Checking UID against next occurrences
int failed = 0;
for (; i + uidlen <= rawbit;) {
failed = 0;
int failed = 0;
for (bit = 0; bit < uidlen; bit++) {
if (bits[bit] != rawbits[i++]) {
failed = 1;
@ -287,12 +284,9 @@ int CmdIndalaDemod(const char *Cmd)
}
times += 1;
}
PrintAndLog("Occurrences: %d (expected %d)", times, (rawbit - start) / uidlen);
// Remodulating for tag cloning
// HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
// since this changes graphbuffer data.
GraphTraceLen = 32*uidlen;
i = 0;
int phase = 0;
@ -315,10 +309,15 @@ int CmdIndalaDemod(const char *Cmd)
int CmdIndalaClone(const char *Cmd)
{
UsbCommand c;
unsigned int uid1, uid2, uid3, uid4, uid5, uid6, uid7;
uid1 = uid2 = uid3 = uid4 = uid5 = uid6 = uid7 = 0;
UsbCommand c;
uid1=0;
uid2=0;
uid3=0;
uid4=0;
uid5=0;
uid6=0;
uid7=0;
int n = 0, i = 0;
if (strchr(Cmd,'l') != 0) {
@ -340,7 +339,9 @@ int CmdIndalaClone(const char *Cmd)
c.d.asDwords[4] = uid5;
c.d.asDwords[5] = uid6;
c.d.asDwords[6] = uid7;
} else {
}
else
{
while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
uid1 = (uid1 << 4) | (uid2 >> 28);
uid2 = (uid2 << 4) | (n & 0xf);
@ -358,16 +359,13 @@ int CmdIndalaClone(const char *Cmd)
int CmdLFRead(const char *Cmd)
{
UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K};
// 'h' means higher-low-frequency, 134 kHz
if(*Cmd == 'h') {
c.arg[0] = 1;
} else if (*Cmd == '\0') {
c.arg[0] = 0;
} else if (sscanf(Cmd, "%"lli, &c.arg[0]) != 1) {
PrintAndLog("Samples 1: 'lf read'");
PrintAndLog(" 2: 'lf read h'");
PrintAndLog(" 3: 'lf read <divisor>'");
PrintAndLog("use 'read' or 'read h', or 'read <divisor>'");
return 0;
}
SendCommand(&c);
@ -419,9 +417,7 @@ int CmdLFSim(const char *Cmd)
int CmdLFSimBidir(const char *Cmd)
{
// Set ADC to twice the carrier for a slight supersampling
// HACK: not implemented in ARMSRC.
PrintAndLog("Not implemented yet.");
/* Set ADC to twice the carrier for a slight supersampling */
UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
SendCommand(&c);
return 0;
@ -433,17 +429,23 @@ int CmdLFSimManchester(const char *Cmd)
static int clock, gap;
static char data[1024], gapstring[8];
/* get settings/bits */
sscanf(Cmd, "%i %s %i", &clock, &data[0], &gap);
/* clear our graph */
ClearGraph(0);
/* fill it with our bitstream */
for (int i = 0; i < strlen(data) ; ++i)
AppendGraph(0, clock, data[i]- '0');
/* modulate */
CmdManchesterMod("");
/* show what we've done */
RepaintGraphWindow();
/* simulate */
sprintf(&gapstring[0], "%i", gap);
CmdLFSim(gapstring);
return 0;
@ -452,23 +454,20 @@ int CmdLFSimManchester(const char *Cmd)
int CmdLFSnoop(const char *Cmd)
{
UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES};
// 'h' means higher-low-frequency, 134 kHz
c.arg[0] = 0;
c.arg[1] = -1;
if (*Cmd == 'l') {
if (*Cmd == 0) {
// empty
} else if (*Cmd == 'l') {
sscanf(Cmd, "l %"lli, &c.arg[1]);
} else if(*Cmd == 'h') {
c.arg[0] = 1;
sscanf(Cmd, "h %"lli, &c.arg[1]);
} else if (sscanf(Cmd, "%"lli" %"lli, &c.arg[0], &c.arg[1]) < 1) {
PrintAndLog("usage 1: snoop");
PrintAndLog(" 2: snoop {l,h} [trigger threshold]");
PrintAndLog(" 3: snoop <divisor> [trigger threshold]");
PrintAndLog("use 'snoop' or 'snoop {l,h} [trigger threshold]', or 'snoop <divisor> [trigger threshold]'");
return 0;
}
SendCommand(&c);
WaitForResponse(CMD_ACK,NULL);
return 0;

View file

@ -141,7 +141,7 @@ int DetectASKClock(int peak)
return clk[best];
}
*/
void setGraphBuf(uint8_t *buff,int size)
void setGraphBuf(uint8_t *buff, size_t size)
{
int i=0;
ClearGraph(0);
@ -152,7 +152,7 @@ void setGraphBuf(uint8_t *buff,int size)
RepaintGraphWindow();
return;
}
int getFromGraphBuf(uint8_t *buff)
size_t getFromGraphBuf(uint8_t *buff)
{
uint32_t i;
for (i=0;i<GraphTraceLen;++i){
@ -175,7 +175,7 @@ int GetClock(const char *str, int peak, int verbose)
if (!clock)
{
uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
int size = getFromGraphBuf(grph);
size_t size = getFromGraphBuf(grph);
clock = DetectASKClock(grph,size,0);
//clock2 = DetectClock2(peak);
/* Only print this message if we're not looping something */
@ -200,7 +200,7 @@ int GetNRZpskClock(const char *str, int peak, int verbose)
if (!clock)
{
uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
int size = getFromGraphBuf(grph);
size_t size = getFromGraphBuf(grph);
clock = DetectpskNRZClock(grph,size,0);
//clock2 = DetectClock2(peak);
/* Only print this message if we're not looping something */

View file

@ -15,10 +15,10 @@
void AppendGraph(int redraw, int clock, int bit);
int ClearGraph(int redraw);
//int DetectClock(int peak);
int getFromGraphBuf(uint8_t *buff);
size_t getFromGraphBuf(uint8_t *buff);
int GetClock(const char *str, int peak, int verbose);
int GetNRZpskClock(const char *str, int peak, int verbose);
void setGraphBuf(uint8_t *buff,int size);
void setGraphBuf(uint8_t *buff, size_t size);
#define MAX_GRAPH_TRACE_LEN (1024*128)
extern int GraphBuffer[MAX_GRAPH_TRACE_LEN];

View file

@ -14,7 +14,7 @@
//by marshmellow
//takes 1s and 0s and searches for EM410x format - output EM ID
uint64_t Em410xDecode(uint8_t *BitStream,uint32_t BitLen)
uint64_t Em410xDecode(uint8_t *BitStream, size_t size)
{
//no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
// otherwise could be a void with no arguments
@ -24,7 +24,7 @@ uint64_t Em410xDecode(uint8_t *BitStream,uint32_t BitLen)
uint32_t i = 0;
uint32_t initLoopMax = 65;
if (initLoopMax>BitLen) initLoopMax=BitLen;
if (initLoopMax>size) initLoopMax=size;
for (;i < initLoopMax; ++i) //65 samples should be plenty to find high and low values
{
@ -43,8 +43,8 @@ uint64_t Em410xDecode(uint8_t *BitStream,uint32_t BitLen)
uint32_t idx = 0;
uint32_t ii=0;
uint8_t resetCnt = 0;
while( (idx + 64) < BitLen) {
restart:
while( (idx + 64) < size) {
restart:
// search for a start of frame marker
if ( memcmp(BitStream+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0)
{ // frame marker found
@ -82,17 +82,17 @@ restart:
//takes 2 arguments - clock and invert both as integers
//attempts to demodulate ask while decoding manchester
//prints binary found and saves in graphbuffer for further commands
int askmandemod(uint8_t * BinStream,uint32_t *BitLen,int *clk, int *invert)
int askmandemod(uint8_t *BinStream, size_t *size, int *clk, int *invert)
{
int i;
int high = 0, low = 128;
*clk=DetectASKClock(BinStream,(size_t)*BitLen,*clk); //clock default
*clk=DetectASKClock(BinStream, *size, *clk); //clock default
if (*clk<8) *clk =64;
if (*clk<32) *clk=32;
if (*invert != 0 && *invert != 1) *invert=0;
uint32_t initLoopMax = 200;
if (initLoopMax>*BitLen) initLoopMax=*BitLen;
if (initLoopMax > *size) initLoopMax=*size;
// Detect high and lows
for (i = 0; i < initLoopMax; ++i) //200 samples should be enough to find high and low values
{
@ -115,23 +115,23 @@ int askmandemod(uint8_t * BinStream,uint32_t *BitLen,int *clk, int *invert)
int tol = 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
if (*clk==32)tol=1; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
int iii = 0;
uint32_t gLen = *BitLen;
uint32_t gLen = *size;
if (gLen > 3000) gLen=3000;
uint8_t errCnt =0;
uint32_t bestStart = *BitLen;
uint32_t bestErrCnt = (*BitLen/1000);
uint32_t maxErr = (*BitLen/1000);
uint32_t bestStart = *size;
uint32_t bestErrCnt = (*size/1000);
uint32_t maxErr = (*size/1000);
//PrintAndLog("DEBUG - lastbit - %d",lastBit);
//loop to find first wave that works
for (iii=0; iii < gLen; ++iii){
if ((BinStream[iii]>=high)||(BinStream[iii]<=low)){
if ((BinStream[iii] >= high) || (BinStream[iii] <= low)){
lastBit=iii-*clk;
errCnt=0;
//loop through to see if this start location works
for (i = iii; i < *BitLen; ++i) {
if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
for (i = iii; i < *size; ++i) {
if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){
lastBit+=*clk;
} else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
} else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
//low found and we are expecting a bar
lastBit+=*clk;
} else {
@ -167,14 +167,14 @@ int askmandemod(uint8_t * BinStream,uint32_t *BitLen,int *clk, int *invert)
if (bestErrCnt<maxErr){
//best run is good enough set to best run and set overwrite BinStream
iii=bestStart;
lastBit=bestStart-*clk;
lastBit = bestStart - *clk;
bitnum=0;
for (i = iii; i < *BitLen; ++i) {
if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
lastBit+=*clk;
for (i = iii; i < *size; ++i) {
if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){
lastBit += *clk;
BinStream[bitnum] = *invert;
bitnum++;
} else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
} else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
//low found and we are expecting a bar
lastBit+=*clk;
BinStream[bitnum] = 1-*invert;
@ -196,7 +196,7 @@ int askmandemod(uint8_t * BinStream,uint32_t *BitLen,int *clk, int *invert)
}
if (bitnum >=400) break;
}
*BitLen=bitnum;
*size=bitnum;
} else{
*invert=bestStart;
*clk=iii;
@ -208,7 +208,7 @@ int askmandemod(uint8_t * BinStream,uint32_t *BitLen,int *clk, int *invert)
//by marshmellow
//take 10 and 01 and manchester decode
//run through 2 times and take least errCnt
int manrawdecode(uint8_t * BitStream, int *bitLen)
int manrawdecode(uint8_t * BitStream, size_t *size)
{
int bitnum=0;
int errCnt =0;
@ -218,7 +218,7 @@ int manrawdecode(uint8_t * BitStream, int *bitLen)
int ii=1;
for (ii=1;ii<3;++ii){
i=1;
for (i=i+ii;i<*bitLen-2;i+=2){
for (i=i+ii;i<*size-2;i+=2){
if(BitStream[i]==1 && (BitStream[i+1]==0)){
} else if((BitStream[i]==0)&& BitStream[i+1]==1){
} else {
@ -236,10 +236,10 @@ int manrawdecode(uint8_t * BitStream, int *bitLen)
if (errCnt<20){
ii=bestRun;
i=1;
for (i=i+ii;i<*bitLen-2;i+=2){
if(BitStream[i]==1 && (BitStream[i+1]==0)){
for (i=i+ii;i < *size-2;i+=2){
if(BitStream[i] == 1 && (BitStream[i+1] == 0)){
BitStream[bitnum++]=0;
} else if((BitStream[i]==0)&& BitStream[i+1]==1){
} else if((BitStream[i] == 0) && BitStream[i+1] == 1){
BitStream[bitnum++]=1;
} else {
BitStream[bitnum++]=77;
@ -247,7 +247,7 @@ int manrawdecode(uint8_t * BitStream, int *bitLen)
}
if(bitnum>300) break;
}
*bitLen=bitnum;
*size=bitnum;
}
return errCnt;
}
@ -255,16 +255,16 @@ int manrawdecode(uint8_t * BitStream, int *bitLen)
//by marshmellow
//take 01 or 10 = 0 and 11 or 00 = 1
int BiphaseRawDecode(uint8_t * BitStream, int *bitLen, int offset)
int BiphaseRawDecode(uint8_t *BitStream, size_t *size, int offset)
{
uint8_t bitnum=0;
uint32_t errCnt =0;
uint32_t i=1;
i=offset;
for (;i<*bitLen-2;i+=2){
if((BitStream[i]==1 && BitStream[i+1]==0)||(BitStream[i]==0 && BitStream[i+1]==1)){
for (;i<*size-2;i+=2){
if((BitStream[i]==1 && BitStream[i+1]==0) || (BitStream[i]==0 && BitStream[i+1]==1)){
BitStream[bitnum++]=1;
} else if((BitStream[i]==0 && BitStream[i+1]==0)||(BitStream[i]==1 && BitStream[i+1]==1)){
} else if((BitStream[i]==0 && BitStream[i+1]==0) || (BitStream[i]==1 && BitStream[i+1]==1)){
BitStream[bitnum++]=0;
} else {
BitStream[bitnum++]=77;
@ -272,7 +272,7 @@ int BiphaseRawDecode(uint8_t * BitStream, int *bitLen, int offset)
}
if(bitnum>250) break;
}
*bitLen=bitnum;
*size=bitnum;
return errCnt;
}
@ -280,19 +280,19 @@ int BiphaseRawDecode(uint8_t * BitStream, int *bitLen, int offset)
//takes 2 arguments - clock and invert both as integers
//attempts to demodulate ask only
//prints binary found and saves in graphbuffer for further commands
int askrawdemod(uint8_t *BinStream, int *bitLen,int *clk, int *invert)
int askrawdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert)
{
uint32_t i;
// int invert=0; //invert default
int high = 0, low = 128;
*clk=DetectASKClock(BinStream,*bitLen,*clk); //clock default
*clk=DetectASKClock(BinStream, *size, *clk); //clock default
uint8_t BitStream[502] = {0};
if (*clk<8) *clk =64;
if (*clk<32) *clk=32;
if (*invert != 0 && *invert != 1) *invert =0;
uint32_t initLoopMax = 200;
if (initLoopMax>*bitLen) initLoopMax=*bitLen;
if (initLoopMax>*size) initLoopMax=*size;
// Detect high and lows
for (i = 0; i < initLoopMax; ++i) //200 samples should be plenty to find high and low values
{
@ -315,19 +315,19 @@ int askrawdemod(uint8_t *BinStream, int *bitLen,int *clk, int *invert)
uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
if (*clk==32)tol=1; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
uint32_t iii = 0;
uint32_t gLen = *bitLen;
uint32_t gLen = *size;
if (gLen > 500) gLen=500;
uint8_t errCnt =0;
uint32_t bestStart = *bitLen;
uint32_t bestErrCnt = (*bitLen/1000);
uint32_t bestStart = *size;
uint32_t bestErrCnt = (*size/1000);
uint8_t midBit=0;
//PrintAndLog("DEBUG - lastbit - %d",lastBit);
//loop to find first wave that works
for (iii=0; iii < gLen; ++iii){
if ((BinStream[iii]>=high)||(BinStream[iii]<=low)){
if ((BinStream[iii]>=high) || (BinStream[iii]<=low)){
lastBit=iii-*clk;
//loop through to see if this start location works
for (i = iii; i < *bitLen; ++i) {
for (i = iii; i < *size; ++i) {
if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
lastBit+=*clk;
BitStream[bitnum] = *invert;
@ -336,20 +336,20 @@ int askrawdemod(uint8_t *BinStream, int *bitLen,int *clk, int *invert)
} else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
//low found and we are expecting a bar
lastBit+=*clk;
BitStream[bitnum] = 1-*invert;
BitStream[bitnum] = 1- *invert;
bitnum++;
midBit=0;
} else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
//mid bar?
midBit=1;
BitStream[bitnum]= 1-*invert;
BitStream[bitnum]= 1- *invert;
bitnum++;
} else if ((BinStream[i]>=high)&&(midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
} else if ((BinStream[i]>=high) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
//mid bar?
midBit=1;
BitStream[bitnum]= *invert;
bitnum++;
} else if ((i-lastBit)>((*clk/2)+tol)&&(midBit==0)){
} else if ((i-lastBit)>((*clk/2)+tol) && (midBit==0)){
//no mid bar found
midBit=1;
BitStream[bitnum]= BitStream[bitnum-1];
@ -369,7 +369,7 @@ int askrawdemod(uint8_t *BinStream, int *bitLen,int *clk, int *invert)
errCnt++;
lastBit+=*clk;//skip over until hit too many errors
if (errCnt>((*bitLen/1000))){ //allow 1 error for every 1000 samples else start over
if (errCnt > ((*size/1000))){ //allow 1 error for every 1000 samples else start over
errCnt=0;
bitnum=0;//start over
break;
@ -379,7 +379,7 @@ int askrawdemod(uint8_t *BinStream, int *bitLen,int *clk, int *invert)
if (bitnum>500) break;
}
//we got more than 64 good bits and not all errors
if ((bitnum > (64+errCnt)) && (errCnt<(*bitLen/1000))) {
if ((bitnum > (64+errCnt)) && (errCnt<(*size/1000))) {
//possible good read
if (errCnt==0) break; //great read - finish
if (bestStart == iii) break; //if current run == bestErrCnt run (after exhausted testing) then finish
@ -391,7 +391,7 @@ int askrawdemod(uint8_t *BinStream, int *bitLen,int *clk, int *invert)
}
if (iii>=gLen){ //exhausted test
//if there was a ok test go back to that one and re-run the best run (then dump after that run)
if (bestErrCnt < (*bitLen/1000)) iii=bestStart;
if (bestErrCnt < (*size/1000)) iii=bestStart;
}
}
if (bitnum>16){
@ -402,7 +402,7 @@ int askrawdemod(uint8_t *BinStream, int *bitLen,int *clk, int *invert)
for (i=0; i < bitnum; ++i){
BinStream[i]=BitStream[i];
}
*bitLen=bitnum;
*size=bitnum;
// RepaintGraphWindow();
//output
// if (errCnt>0){
@ -438,8 +438,6 @@ size_t fsk_wave_demod(uint8_t * dest, size_t size, uint8_t fchigh, uint8_t fclow
// less likely to get a false transition up there.
// (but have to be careful not to go too high and miss some short waves)
uint8_t threshold_value = (uint8_t)(((maxVal-128)*.75)+128);
// idx=1;
//uint8_t threshold_value = 127;
// sync to first lo-hi transition, and threshold
@ -481,7 +479,8 @@ uint32_t myround2(float f)
}
//translate 11111100000 to 10
size_t aggregate_bits(uint8_t *dest,size_t size, uint8_t rfLen, uint8_t maxConsequtiveBits, uint8_t invert,uint8_t fchigh,uint8_t fclow )// uint8_t h2l_crossing_value,uint8_t l2h_crossing_value,
size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t maxConsequtiveBits,
uint8_t invert, uint8_t fchigh, uint8_t fclow)
{
uint8_t lastval=dest[0];
uint32_t idx=0;
@ -524,7 +523,7 @@ int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t
{
// FSK demodulator
size = fsk_wave_demod(dest, size, fchigh, fclow);
size = aggregate_bits(dest, size,rfLen,192,invert,fchigh,fclow);
size = aggregate_bits(dest, size, rfLen, 192, invert, fchigh, fclow);
return size;
}
// loop to get raw HID waveform then FSK demodulate the TAG ID from it
@ -579,7 +578,7 @@ int HIDdemodFSK(uint8_t *dest, size_t size, uint32_t *hi2, uint32_t *hi, uint32_
return -1;
}
uint32_t bytebits_to_byte(uint8_t* src, int numbits)
uint32_t bytebits_to_byte(uint8_t* src, size_t numbits)
{
uint32_t num = 0;
for(int i = 0 ; i < numbits ; i++)
@ -592,19 +591,19 @@ uint32_t bytebits_to_byte(uint8_t* src, int numbits)
int IOdemodFSK(uint8_t *dest, size_t size)
{
static const uint8_t THRESHOLD = 140;
uint32_t idx=0;
//make sure buffer has data
if (size < 66) return -1;
//test samples are not just noise
uint8_t testMax=0;
for(idx=0;idx<65;idx++){
if (testMax<dest[idx]) testMax=dest[idx];
uint8_t justNoise = 1;
for(idx=0;idx< size && justNoise ;idx++){
justNoise = dest[idx] < THRESHOLD;
}
idx=0;
//if not just noise
if (testMax>20){
if(justNoise) return 0;
// FSK demodulator
size = fskdemod(dest, size,64,1,10,8); // RF/64 and invert
size = fskdemod(dest, size, 64, 1, 10, 8); // RF/64 and invert
if (size < 65) return -1; //did we get a good demod?
//Index map
//0 10 20 30 40 50 60
@ -626,7 +625,6 @@ int IOdemodFSK(uint8_t *dest, size_t size)
}
}
}
}
return 0;
}
@ -655,13 +653,13 @@ int DetectASKClock(uint8_t dest[], size_t size, int clock)
low = dest[i];
}
}
peak=(int)((peak-128)*.75)+128;
low= (int)((low-128)*.75)+128;
peak=(int)(((peak-128)*.75)+128);
low= (int)(((low-128)*.75)+128);
int ii;
int clkCnt;
int tol = 0;
int bestErr=1000;
int errCnt[]={0,0,0,0,0,0,0,0};
int bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000};
int errCnt=0;
//test each valid clock from smallest to greatest to see which lines up
for(clkCnt=0; clkCnt<6;++clkCnt){
if (clk[clkCnt]==32){
@ -669,33 +667,371 @@ int DetectASKClock(uint8_t dest[], size_t size, int clock)
}else{
tol=0;
}
bestErr=1000;
bestErr[clkCnt]=1000;
//try lining up the peaks by moving starting point (try first 256)
for (ii=0; ii<loopCnt; ++ii){
if ((dest[ii]>=peak) || (dest[ii]<=low)){
errCnt[clkCnt]=0;
errCnt=0;
// now that we have the first one lined up test rest of wave array
for (i=0; i<((int)(size/clk[clkCnt])-1); ++i){
if (dest[ii+(i*clk[clkCnt])]>=peak || dest[ii+(i*clk[clkCnt])]<=low){
}else if(dest[ii+(i*clk[clkCnt])-tol]>=peak || dest[ii+(i*clk[clkCnt])-tol]<=low){
}else if(dest[ii+(i*clk[clkCnt])+tol]>=peak || dest[ii+(i*clk[clkCnt])+tol]<=low){
}else{ //error no peak detected
errCnt[clkCnt]++;
errCnt++;
}
}
//if we found no errors this is correct one - return this clock
if(errCnt[clkCnt]==0) return clk[clkCnt];
if(errCnt==0) return clk[clkCnt];
//if we found errors see if it is lowest so far and save it as best run
if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
if(errCnt<bestErr[clkCnt]) bestErr[clkCnt]=errCnt;
}
}
}
int iii=0;
int best=0;
for (iii=0; iii<6;++iii){
if (errCnt[iii]<errCnt[best]){
for (iii=0; iii<7;++iii){
if (bestErr[iii]<bestErr[best]){
// current best bit to error ratio vs new bit to error ratio
if (((size/clk[best])/bestErr[best]<(size/clk[iii])/bestErr[iii]) ){
best = iii;
}
}
}
return clk[best];
}
//by marshmellow
//detect psk clock by reading #peaks vs no peaks(or errors)
int DetectpskNRZClock(uint8_t dest[], size_t size, int clock)
{
int i=0;
int peak=0;
int low=128;
int clk[]={16,32,40,50,64,100,128,256};
int loopCnt = 2048; //don't need to loop through entire array...
if (size<loopCnt) loopCnt = size;
//if we already have a valid clock quit
for (;i<8;++i)
if (clk[i]==clock) return clock;
//get high and low peak
for (i=0;i<loopCnt;++i){
if(dest[i]>peak){
peak = dest[i];
}
if(dest[i]<low){
low = dest[i];
}
}
peak=(int)(((peak-128)*.90)+128);
low= (int)(((low-128)*.90)+128);
//PrintAndLog("DEBUG: peak: %d, low: %d",peak,low);
int ii;
uint8_t clkCnt;
uint8_t tol = 0;
int peakcnt=0;
int errCnt=0;
int bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
int peaksdet[]={0,0,0,0,0,0,0,0,0};
//test each valid clock from smallest to greatest to see which lines up
for(clkCnt=0; clkCnt<6;++clkCnt){
if (clk[clkCnt]==32){
tol=0;
}else{
tol=0;
}
//try lining up the peaks by moving starting point (try first 256)
for (ii=0; ii<loopCnt; ++ii){
if ((dest[ii]>=peak) || (dest[ii]<=low)){
errCnt=0;
peakcnt=0;
// now that we have the first one lined up test rest of wave array
for (i=0; i<((int)(size/clk[clkCnt])-1); ++i){
if (dest[ii+(i*clk[clkCnt])]>=peak || dest[ii+(i*clk[clkCnt])]<=low){
peakcnt++;
}else if(dest[ii+(i*clk[clkCnt])-tol]>=peak || dest[ii+(i*clk[clkCnt])-tol]<=low){
peakcnt++;
}else if(dest[ii+(i*clk[clkCnt])+tol]>=peak || dest[ii+(i*clk[clkCnt])+tol]<=low){
peakcnt++;
}else{ //error no peak detected
errCnt++;
}
}
if(peakcnt>peaksdet[clkCnt]) {
peaksdet[clkCnt]=peakcnt;
bestErr[clkCnt]=errCnt;
}
}
}
}
int iii=0;
int best=0;
//int ratio2; //debug
int ratio;
//int bits;
for (iii=0; iii<7;++iii){
ratio=1000;
//ratio2=1000; //debug
//bits=size/clk[iii]; //debug
if (peaksdet[iii]>0){
ratio=bestErr[iii]/peaksdet[iii];
if (((bestErr[best]/peaksdet[best])>(ratio)+1)){
best = iii;
}
//ratio2=bits/peaksdet[iii]; //debug
}
//PrintAndLog("DEBUG: Clk: %d, peaks: %d, errs: %d, bestClk: %d, ratio: %d, bits: %d, peakbitr: %d",clk[iii],peaksdet[iii],bestErr[iii],clk[best],ratio, bits,ratio2);
}
return clk[best];
}
//by marshmellow (attempt to get rid of high immediately after a low)
void pskCleanWave(uint8_t *bitStream, size_t size)
{
int i;
int low=128;
int high=0;
int gap = 4;
// int loopMax = 2048;
int newLow=0;
int newHigh=0;
for (i=0; i<size; ++i){
if (bitStream[i]<low) low=bitStream[i];
if (bitStream[i]>high) high=bitStream[i];
}
high = (int)(((high-128)*.80)+128);
low = (int)(((low-128)*.90)+128);
//low = (uint8_t)(((int)(low)-128)*.80)+128;
for (i=0; i<size; ++i){
if (newLow==1){
bitStream[i]=low+8;
gap--;
if (gap==0){
newLow=0;
gap=4;
}
}else if (newHigh==1){
bitStream[i]=high-8;
gap--;
if (gap==0){
newHigh=0;
gap=4;
}
}
if (bitStream[i]<=low) newLow=1;
if (bitStream[i]>=high) newHigh=1;
}
return;
}
//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 indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert)
{
//26 bit 40134 format (don't know other formats)
int i;
int long_wait;
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;
}
//found start once now test length by finding next one
// Inverting signal if needed
if (first == 1) {
for (i = start; i < *size; i++) {
bitStream[i] = !bitStream[i];
}
*invert = 1;
}else *invert=0;
int iii;
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 PSK wave or NRZ wave (both similar enough)
//peaks switch bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
int pskNRZrawDemod(uint8_t *dest, size_t *size, int *clk, int *invert)
{
pskCleanWave(dest,*size);
int clk2 = DetectpskNRZClock(dest, *size, *clk);
*clk=clk2;
uint32_t i;
uint8_t high=0, low=128;
uint32_t gLen = *size;
if (gLen > 1280) gLen=1280;
// get high
for (i=0; i<gLen; ++i){
if (dest[i]>high) high = dest[i];
if (dest[i]<low) low=dest[i];
}
//fudge high/low bars by 25%
high = (uint8_t)((((int)(high)-128)*.75)+128);
low = (uint8_t)((((int)(low)-128)*.80)+128);
//PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
int lastBit = 0; //set first clock check
uint32_t bitnum = 0; //output counter
uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
if (*clk==32)tol=2; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
uint32_t iii = 0;
uint8_t errCnt =0;
uint32_t bestStart = *size;
uint32_t maxErr = (*size/1000);
uint32_t bestErrCnt = maxErr;
//uint8_t midBit=0;
uint8_t curBit=0;
uint8_t bitHigh=0;
uint8_t ignorewin=*clk/8;
//PrintAndLog("DEBUG - lastbit - %d",lastBit);
//loop to find first wave that works - align to clock
for (iii=0; iii < gLen; ++iii){
if ((dest[iii]>=high)||(dest[iii]<=low)){
lastBit=iii-*clk;
//loop through to see if this start location works
for (i = iii; i < *size; ++i) {
//if we found a high bar and we are at a clock bit
if ((dest[i]>=high ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){
bitHigh=1;
lastBit+=*clk;
ignorewin=*clk/8;
bitnum++;
//else if low bar found and we are at a clock point
}else if ((dest[i]<=low ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){
bitHigh=1;
lastBit+=*clk;
ignorewin=*clk/8;
bitnum++;
//else if no bars found
}else if(dest[i]<high && dest[i]>low) {
if (ignorewin==0){
bitHigh=0;
}else ignorewin--;
//if we are past a clock point
if (i>=lastBit+*clk+tol){ //clock val
lastBit+=*clk;
bitnum++;
}
//else if bar found but we are not at a clock bit and we did not just have a clock bit
}else if ((dest[i]>=high || dest[i]<=low) && (i<lastBit+*clk-tol || i>lastBit+*clk+tol) && (bitHigh==0)){
//error bar found no clock...
errCnt++;
}
if (bitnum>=1000) break;
}
//we got more than 64 good bits and not all errors
if ((bitnum > (64+errCnt)) && (errCnt<(maxErr))) {
//possible good read
if (errCnt==0){
bestStart = iii;
bestErrCnt=errCnt;
break; //great read - finish
}
if (bestStart == iii) break; //if current run == bestErrCnt run (after exhausted testing) then finish
if (errCnt<bestErrCnt){ //set this as new best run
bestErrCnt=errCnt;
bestStart = iii;
}
}
}
}
if (bestErrCnt<maxErr){
//best run is good enough set to best run and set overwrite BinStream
iii=bestStart;
lastBit=bestStart-*clk;
bitnum=0;
for (i = iii; i < *size; ++i) {
//if we found a high bar and we are at a clock bit
if ((dest[i]>=high ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){
bitHigh=1;
lastBit+=*clk;
curBit=1-*invert;
dest[bitnum]=curBit;
ignorewin=*clk/8;
bitnum++;
//else if low bar found and we are at a clock point
}else if ((dest[i]<=low ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){
bitHigh=1;
lastBit+=*clk;
curBit=*invert;
dest[bitnum]=curBit;
ignorewin=*clk/8;
bitnum++;
//else if no bars found
}else if(dest[i]<high && dest[i]>low) {
if (ignorewin==0){
bitHigh=0;
}else ignorewin--;
//if we are past a clock point
if (i>=lastBit+*clk+tol){ //clock val
lastBit+=*clk;
dest[bitnum]=curBit;
bitnum++;
}
//else if bar found but we are not at a clock bit and we did not just have a clock bit
}else if ((dest[i]>=high || dest[i]<=low) && ((i<lastBit+*clk-tol) || (i>lastBit+*clk+tol)) && (bitHigh==0)){
//error bar found no clock...
bitHigh=1;
dest[bitnum]=77;
bitnum++;
errCnt++;
}
if (bitnum >=1000) break;
}
*size=bitnum;
} else{
*size=bitnum;
*clk=bestStart;
return -1;
}
if (bitnum>16){
*size=bitnum;
} else return -1;
return errCnt;
}

View file

@ -12,14 +12,18 @@
#include <stdint.h>
int DetectASKClock(uint8_t dest[], size_t size, int clock);
int askmandemod(uint8_t *BinStream,uint32_t *BitLen,int *clk, int *invert);
uint64_t Em410xDecode(uint8_t *BitStream,uint32_t BitLen);
int manrawdecode(uint8_t *BitStream, int *bitLen);
int BiphaseRawDecode(uint8_t * BitStream, int *bitLen, int offset);
int askrawdemod(uint8_t *BinStream, int *bitLen,int *clk, int *invert);
int askmandemod(uint8_t *BinStream, size_t *size, int *clk, int *invert);
uint64_t Em410xDecode(uint8_t *BitStream,size_t size);
int manrawdecode(uint8_t *BitStream, size_t *size);
int BiphaseRawDecode(uint8_t * BitStream, size_t *size, int offset);
int askrawdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert);
int HIDdemodFSK(uint8_t *dest, size_t size, uint32_t *hi2, uint32_t *hi, uint32_t *lo);
int IOdemodFSK(uint8_t *dest, size_t size);
int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow);
uint32_t bytebits_to_byte(uint8_t* src, int numbits);
uint32_t bytebits_to_byte(uint8_t* src, size_t numbits);
int pskNRZrawDemod(uint8_t *dest, size_t *size, int *clk, int *invert);
int DetectpskNRZClock(uint8_t dest[], size_t size, int clock);
int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert);
void pskCleanWave(uint8_t *bitStream, size_t size);
#endif