FIX: em410xdemod empty tag id

Code cleanup
This commit is contained in:
Chris 2018-09-05 18:56:21 +02:00
commit fb49ca9735

View file

@ -237,14 +237,14 @@ int usage_data_fsktonrz() {
//set the demod buffer with given array of binary (one bit per byte) //set the demod buffer with given array of binary (one bit per byte)
//by marshmellow //by marshmellow
void setDemodBuf(uint8_t *buf, size_t size, size_t startIdx) { void setDemodBuf(uint8_t *buf, size_t size, size_t start_idx) {
if (buf == NULL) return; if (buf == NULL) return;
if ( size > MAX_DEMOD_BUF_LEN - startIdx) if ( size > MAX_DEMOD_BUF_LEN - start_idx)
size = MAX_DEMOD_BUF_LEN - startIdx; size = MAX_DEMOD_BUF_LEN - start_idx;
for (size_t i = 0; i < size; i++) for (size_t i = 0; i < size; i++)
DemodBuffer[i] = buf[startIdx++]; DemodBuffer[i] = buf[start_idx++];
DemodBufferLen = size; DemodBufferLen = size;
} }
@ -346,10 +346,11 @@ void save_restoreDB(uint8_t saveOpt) {
memcpy(SavedDB, DemodBuffer, sizeof(DemodBuffer)); memcpy(SavedDB, DemodBuffer, sizeof(DemodBuffer));
SavedDBlen = DemodBufferLen; SavedDBlen = DemodBufferLen;
DB_Saved=true; DB_Saved = true;
savedDemodStartIdx = g_DemodStartIdx; savedDemodStartIdx = g_DemodStartIdx;
savedDemodClock = g_DemodClock; savedDemodClock = g_DemodClock;
} else if (DB_Saved) { //restore } else if (DB_Saved) { //restore
memcpy(DemodBuffer, SavedDB, sizeof(DemodBuffer)); memcpy(DemodBuffer, SavedDB, sizeof(DemodBuffer));
DemodBufferLen = SavedDBlen; DemodBufferLen = SavedDBlen;
g_DemodClock = savedDemodClock; g_DemodClock = savedDemodClock;
@ -422,10 +423,12 @@ int CmdPrintDemodBuff(const char *Cmd) {
char *buf = (char *) (DemodBuffer + offset); char *buf = (char *) (DemodBuffer + offset);
numBits = (numBits > sizeof(hex)) ? sizeof(hex) : numBits; numBits = (numBits > sizeof(hex)) ? sizeof(hex) : numBits;
numBits = binarraytohex(hex, buf, numBits); numBits = binarraytohex(hex, buf, numBits);
if (numBits==0) return 0; if (numBits == 0) {
PrintAndLogEx(NORMAL, "DemodBuffer: %s",hex); return 0;
}
PrintAndLogEx(NORMAL, "DemodBuffer: %s", hex);
} else { } else {
PrintAndLogEx(NORMAL, "DemodBuffer:\n%s", sprint_bin_break(DemodBuffer+offset,numBits,16)); PrintAndLogEx(NORMAL, "DemodBuffer:\n%s", sprint_bin_break(DemodBuffer+offset, numBits, 16));
} }
return 1; return 1;
} }
@ -544,14 +547,14 @@ int ASKDemod(const char *Cmd, bool verbose, bool emSearch, uint8_t askType) {
//prints binary found and saves in graphbuffer for further commands //prints binary found and saves in graphbuffer for further commands
int Cmdaskmandemod(const char *Cmd) int Cmdaskmandemod(const char *Cmd)
{ {
char cmdp = param_getchar(Cmd, 0); char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 45 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_am(); if (strlen(Cmd) > 45 || cmdp == 'h') return usage_data_rawdemod_am();
bool st = true; bool st = true;
if (Cmd[0]=='s') if (Cmd[0] == 's')
return ASKDemod_ext(Cmd++, true, true, 1, &st); return ASKDemod_ext(Cmd++, true, true, 1, &st);
else if (Cmd[1] == 's') else if (Cmd[1] == 's')
return ASKDemod_ext(Cmd+=2, true, true, 1, &st); return ASKDemod_ext(Cmd += 2, true, true, 1, &st);
return ASKDemod(Cmd, true, true, 1); return ASKDemod(Cmd, true, true, 1);
} }
@ -559,27 +562,26 @@ int Cmdaskmandemod(const char *Cmd)
//by marshmellow //by marshmellow
//manchester decode //manchester decode
//stricktly take 10 and 01 and convert to 0 and 1 //stricktly take 10 and 01 and convert to 0 and 1
int Cmdmandecoderaw(const char *Cmd) int Cmdmandecoderaw(const char *Cmd) {
{
int i = 0;
int errCnt = 0;
size_t size = 0; size_t size = 0;
int invert = 0;
int maxErr = 20;
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 5 || cmdp == 'h' || cmdp == 'H') return usage_data_manrawdecode();
if (DemodBufferLen==0) return 0;
uint8_t BitStream[MAX_DEMOD_BUF_LEN]={0};
int high = 0, low = 0; int high = 0, low = 0;
int i = 0, errCnt = 0, invert = 0, maxErr = 20;
char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 5 || cmdp == 'h') return usage_data_manrawdecode();
if (DemodBufferLen == 0) return 0;
uint8_t bits[MAX_DEMOD_BUF_LEN] = {0};
for (; i < DemodBufferLen; ++i){ for (; i < DemodBufferLen; ++i){
if (DemodBuffer[i] > high) if (DemodBuffer[i] > high)
high=DemodBuffer[i]; high = DemodBuffer[i];
else if(DemodBuffer[i] < low) else if(DemodBuffer[i] < low)
low=DemodBuffer[i]; low = DemodBuffer[i];
BitStream[i] = DemodBuffer[i]; bits[i] = DemodBuffer[i];
} }
if (high>7 || low <0 ){
if (high > 7 || low < 0 ){
PrintAndLogEx(WARNING, "Error: please raw demod the wave first then manchester raw decode"); PrintAndLogEx(WARNING, "Error: please raw demod the wave first then manchester raw decode");
return 0; return 0;
} }
@ -587,20 +589,22 @@ int Cmdmandecoderaw(const char *Cmd)
sscanf(Cmd, "%i %i", &invert, &maxErr); sscanf(Cmd, "%i %i", &invert, &maxErr);
size = i; size = i;
uint8_t alignPos = 0; uint8_t alignPos = 0;
errCnt = manrawdecode(BitStream, &size, invert, &alignPos); errCnt = manrawdecode(bits, &size, invert, &alignPos);
if (errCnt >= maxErr){ if (errCnt >= maxErr){
PrintAndLogEx(WARNING, "Too many errors: %d",errCnt); PrintAndLogEx(WARNING, "Too many errors: %d",errCnt);
return 0; return 0;
} }
PrintAndLogEx(NORMAL, "Manchester Decoded - # errors:%d - data:",errCnt); PrintAndLogEx(NORMAL, "Manchester Decoded - # errors:%d - data:",errCnt);
PrintAndLogEx(NORMAL, "%s", sprint_bin_break(BitStream, size, 16)); PrintAndLogEx(NORMAL, "%s", sprint_bin_break(bits, size, 16));
if (errCnt == 0){ if (errCnt == 0){
uint64_t id = 0; uint64_t id = 0;
uint32_t hi = 0; uint32_t hi = 0;
size_t idx=0; size_t idx = 0;
if (Em410xDecode(BitStream, &size, &idx, &hi, &id)){ if (Em410xDecode(bits, &size, &idx, &hi, &id) == 1){
//need to adjust to set bitstream back to manchester encoded data //need to adjust to set bitstream back to manchester encoded data
//setDemodBuf(BitStream, size, idx); //setDemodBuf(bits, size, idx);
printEM410x(hi, id); printEM410x(hi, id);
} }
} }
@ -613,22 +617,23 @@ int Cmdmandecoderaw(const char *Cmd)
//takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit //takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit
// and "invert" default = 0 if 1 it will invert output // and "invert" default = 0 if 1 it will invert output
// the argument offset allows us to manually shift if the output is incorrect - [EDIT: now auto detects] // the argument offset allows us to manually shift if the output is incorrect - [EDIT: now auto detects]
int CmdBiphaseDecodeRaw(const char *Cmd) int CmdBiphaseDecodeRaw(const char *Cmd) {
{ size_t size = 0;
size_t size=0; int offset = 0, invert = 0, maxErr = 20, errCnt = 0;
int offset=0, invert=0, maxErr=20, errCnt=0; char cmdp = tolower(param_getchar(Cmd, 0));
char cmdp = param_getchar(Cmd, 0); if (strlen(Cmd) > 3 || cmdp == 'h') return usage_data_biphaserawdecode();
if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') return usage_data_biphaserawdecode();
sscanf(Cmd, "%i %i %i", &offset, &invert, &maxErr); sscanf(Cmd, "%i %i %i", &offset, &invert, &maxErr);
if (DemodBufferLen==0){ if (DemodBufferLen == 0){
PrintAndLogEx(NORMAL, "DemodBuffer Empty - run 'data rawdemod ar' first"); PrintAndLogEx(NORMAL, "DemodBuffer Empty - run 'data rawdemod ar' first");
return 0; return 0;
} }
uint8_t BitStream[MAX_DEMOD_BUF_LEN]={0};
size = sizeof(BitStream); uint8_t bits[MAX_DEMOD_BUF_LEN] = {0};
if ( !getDemodBuf(BitStream, &size) ) return 0; size = sizeof(bits);
errCnt=BiphaseRawDecode(BitStream, &size, &offset, invert); if ( !getDemodBuf(bits, &size) ) return 0;
errCnt = BiphaseRawDecode(bits, &size, &offset, invert);
if (errCnt < 0){ if (errCnt < 0){
PrintAndLogEx(WARNING, "Error during decode:%d", errCnt); PrintAndLogEx(WARNING, "Error during decode:%d", errCnt);
return 0; return 0;
@ -642,10 +647,12 @@ int CmdBiphaseDecodeRaw(const char *Cmd)
PrintAndLogEx(WARNING, "# Errors found during Demod (shown as 7 in bit stream): %d",errCnt); PrintAndLogEx(WARNING, "# Errors found during Demod (shown as 7 in bit stream): %d",errCnt);
PrintAndLogEx(NORMAL, "Biphase Decoded using offset: %d - # invert:%d - data:",offset,invert); PrintAndLogEx(NORMAL, "Biphase Decoded using offset: %d - # invert:%d - data:",offset,invert);
PrintAndLogEx(NORMAL, "%s", sprint_bin_break(BitStream, size, 16)); PrintAndLogEx(NORMAL, "%s", sprint_bin_break(bits, size, 16));
//remove first bit from raw demod
if (offset) if (offset)
setDemodBuf(DemodBuffer,DemodBufferLen-offset, offset); //remove first bit from raw demod setDemodBuf(DemodBuffer,DemodBufferLen-offset, offset);
setClockGrid(g_DemodClock, g_DemodStartIdx + g_DemodClock*offset/2); setClockGrid(g_DemodClock, g_DemodStartIdx + g_DemodClock*offset/2);
return 1; return 1;
} }
@ -1040,21 +1047,19 @@ int FSKrawDemod(const char *Cmd, bool verbose)
//fsk raw demod and print binary //fsk raw demod and print binary
//takes 4 arguments - Clock, invert, fchigh, fclow //takes 4 arguments - Clock, invert, fchigh, fclow
//defaults: clock = 50, invert=1, fchigh=10, fclow=8 (RF/10 RF/8 (fsk2a)) //defaults: clock = 50, invert=1, fchigh=10, fclow=8 (RF/10 RF/8 (fsk2a))
int CmdFSKrawdemod(const char *Cmd) int CmdFSKrawdemod(const char *Cmd) {
{ char cmdp = tolower(param_getchar(Cmd, 0));
char cmdp = param_getchar(Cmd, 0); if (strlen(Cmd) > 20 || cmdp == 'h') return usage_data_rawdemod_fs();
if (strlen(Cmd) > 20 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_fs();
return FSKrawDemod(Cmd, true); return FSKrawDemod(Cmd, true);
} }
//by marshmellow //by marshmellow
//attempt to psk1 demod graph buffer //attempt to psk1 demod graph buffer
int PSKDemod(const char *Cmd, bool verbose) int PSKDemod(const char *Cmd, bool verbose) {
{
int invert = 0, clk = 0, maxErr = 100; int invert = 0, clk = 0, maxErr = 100;
sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr); sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
if (clk == 1){ if (clk == 1) {
invert = 1; invert = 1;
clk=0; clk=0;
} }
@ -1072,14 +1077,14 @@ int PSKDemod(const char *Cmd, bool verbose)
if (g_debugMode || verbose) PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt); if (g_debugMode || verbose) PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt);
return 0; return 0;
} }
if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first) if (errCnt < 0|| BitLen < 16){ //throw away static - allow 1 and -1 (in case of threshold command first)
if (g_debugMode || verbose) PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt); if (g_debugMode || verbose) PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt);
return 0; return 0;
} }
if (verbose || g_debugMode){ if (verbose || g_debugMode){
PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) Using Clock:%d, invert:%d, Bits Found:%d",clk,invert,BitLen); PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) Using Clock:%d, invert:%d, Bits Found:%d",clk, invert, BitLen);
if (errCnt > 0){ if (errCnt > 0){
PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) errors during Demoding (shown as 7 in bit stream): %d",errCnt); PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) errors during Demoding (shown as 7 in bit stream): %d", errCnt);
} }
} }
//prime demod buffer for output //prime demod buffer for output
@ -1103,8 +1108,10 @@ int CmdPSKIdteck(const char *Cmd) {
if (idx == -1) if (idx == -1)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: not enough samples"); PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: not enough samples");
else if (idx == -2) else if (idx == -2)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: preamble not found"); PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: just noise");
else if (idx == -3) else if (idx == -3)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: preamble not found");
else if (idx == -4)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: size not correct: %d", size); PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: size not correct: %d", size);
else else
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: idx: %d",idx); PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: idx: %d",idx);
@ -1120,8 +1127,10 @@ int CmdPSKIdteck(const char *Cmd) {
if (idx == -1) if (idx == -1)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: not enough samples"); PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: not enough samples");
else if (idx == -2) else if (idx == -2)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: preamble not found"); PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: just noise");
else if (idx == -3) else if (idx == -3)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: preamble not found");
else if (idx == -4)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: size not correct: %d", size); PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: size not correct: %d", size);
else else
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: idx: %d",idx); PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: idx: %d",idx);
@ -1148,41 +1157,42 @@ int CmdPSKIdteck(const char *Cmd) {
// takes 3 arguments - clock, invert, maxErr as integers // takes 3 arguments - clock, invert, maxErr as integers
// attempts to demodulate nrz only // attempts to demodulate nrz only
// prints binary found and saves in demodbuffer for further commands // prints binary found and saves in demodbuffer for further commands
int NRZrawDemod(const char *Cmd, bool verbose) int NRZrawDemod(const char *Cmd, bool verbose) {
{
int invert=0; int errCnt = 0, clkStartIdx = 0;
int clk=0; int invert = 0, clk = 0, maxErr = 100;
int maxErr=100;
sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr); sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
if (clk==1){ if (clk == 1){
invert=1; invert = 1;
clk=0; clk = 0;
} }
if (invert != 0 && invert != 1) { if (invert != 0 && invert != 1) {
PrintAndLogEx(WARNING, "(NRZrawDemod) Invalid argument: %s", Cmd); PrintAndLogEx(WARNING, "(NRZrawDemod) Invalid argument: %s", Cmd);
return 0; return 0;
} }
uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
size_t BitLen = getFromGraphBuf(BitStream); uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
if (BitLen==0) return 0; size_t BitLen = getFromGraphBuf(bits);
int errCnt=0;
int clkStartIdx = 0; if (BitLen == 0) return 0;
errCnt = nrzRawDemod(BitStream, &BitLen, &clk, &invert, &clkStartIdx);
errCnt = nrzRawDemod(bits, &BitLen, &clk, &invert, &clkStartIdx);
if (errCnt > maxErr){ if (errCnt > maxErr){
PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
return 0; return 0;
} }
if (errCnt<0 || BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first) if (errCnt < 0 || BitLen < 16){ //throw away static - allow 1 and -1 (in case of threshold command first)
PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt);
return 0; return 0;
} }
if (verbose || g_debugMode) PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen); if (verbose || g_debugMode) PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d", clk, invert, BitLen);
//prime demod buffer for output //prime demod buffer for output
setDemodBuf(BitStream,BitLen,0); setDemodBuf(bits, BitLen, 0);
setClockGrid(clk, clkStartIdx); setClockGrid(clk, clkStartIdx);
if (errCnt>0 && (verbose || g_debugMode)) PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Errors during Demoding (shown as 7 in bit stream): %d",errCnt); if (errCnt > 0 && (verbose || g_debugMode)) PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Errors during Demoding (shown as 7 in bit stream): %d", errCnt);
if (verbose || g_debugMode) { if (verbose || g_debugMode) {
PrintAndLogEx(NORMAL, "NRZ demoded bitstream:"); PrintAndLogEx(NORMAL, "NRZ demoded bitstream:");
// Now output the bitstream to the scrollback by line of 16 bits // Now output the bitstream to the scrollback by line of 16 bits
@ -1191,10 +1201,9 @@ int NRZrawDemod(const char *Cmd, bool verbose)
return 1; return 1;
} }
int CmdNRZrawDemod(const char *Cmd) int CmdNRZrawDemod(const char *Cmd) {
{ char cmdp = tolower(param_getchar(Cmd, 0));
char cmdp = param_getchar(Cmd, 0); if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_nr();
if (strlen(Cmd) > 16 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_nr();
return NRZrawDemod(Cmd, true); return NRZrawDemod(Cmd, true);
} }
@ -1203,13 +1212,11 @@ int CmdNRZrawDemod(const char *Cmd)
// takes 3 arguments - clock, invert, maxErr as integers // takes 3 arguments - clock, invert, maxErr as integers
// attempts to demodulate psk only // attempts to demodulate psk only
// prints binary found and saves in demodbuffer for further commands // prints binary found and saves in demodbuffer for further commands
int CmdPSK1rawDemod(const char *Cmd) int CmdPSK1rawDemod(const char *Cmd) {
{ char cmdp = tolower(param_getchar(Cmd, 0));
int ans; if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p1();
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 16 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_p1();
ans = PSKDemod(Cmd, true); int ans = PSKDemod(Cmd, true);
//output //output
if (!ans){ if (!ans){
if (g_debugMode) PrintAndLogEx(WARNING, "Error demoding: %d",ans); if (g_debugMode) PrintAndLogEx(WARNING, "Error demoding: %d",ans);
@ -1223,13 +1230,11 @@ int CmdPSK1rawDemod(const char *Cmd)
// by marshmellow // by marshmellow
// takes same args as cmdpsk1rawdemod // takes same args as cmdpsk1rawdemod
int CmdPSK2rawDemod(const char *Cmd) int CmdPSK2rawDemod(const char *Cmd) {
{ char cmdp = tolower(param_getchar(Cmd, 0));
int ans = 0; if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p2();
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 16 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_p2();
ans = PSKDemod(Cmd, true); int ans = PSKDemod(Cmd, true);
if (!ans){ if (!ans){
if (g_debugMode) PrintAndLogEx(WARNING, "Error demoding: %d",ans); if (g_debugMode) PrintAndLogEx(WARNING, "Error demoding: %d",ans);
return 0; return 0;
@ -1242,31 +1247,22 @@ int CmdPSK2rawDemod(const char *Cmd)
} }
// by marshmellow - combines all raw demod functions into one menu command // by marshmellow - combines all raw demod functions into one menu command
int CmdRawDemod(const char *Cmd) int CmdRawDemod(const char *Cmd) {
{
char cmdp = Cmd[0]; //param_getchar(Cmd, 0);
char cmdp2 = Cmd[1];
int ans = 0; int ans = 0;
if (strlen(Cmd) > 35 || cmdp == 'h' || cmdp == 'H' || strlen(Cmd) < 2) if (strlen(Cmd) > 35 || strlen(Cmd) < 2)
return usage_data_rawdemod(); return usage_data_rawdemod();
if (cmdp == 'f' && cmdp2 == 's') str_lower( (char *)Cmd);
ans = CmdFSKrawdemod(Cmd+2);
else if(cmdp == 'a' && cmdp2 == 'b') if (str_startswith(Cmd, "fs")) ans = CmdFSKrawdemod(Cmd+2);
ans = Cmdaskbiphdemod(Cmd+2); else if(str_startswith(Cmd, "ab")) ans = Cmdaskbiphdemod(Cmd+2);
else if(cmdp == 'a' && cmdp2 == 'm') else if(str_startswith(Cmd, "am")) ans = Cmdaskmandemod(Cmd+2);
ans = Cmdaskmandemod(Cmd+2); else if(str_startswith(Cmd, "ar")) ans = Cmdaskrawdemod(Cmd+2);
else if(cmdp == 'a' && cmdp2 == 'r') else if(str_startswith(Cmd, "nr")) ans = CmdNRZrawDemod(Cmd+2);
ans = Cmdaskrawdemod(Cmd+2); else if(str_startswith(Cmd, "p1")) ans = CmdPSK1rawDemod(Cmd+2);
else if(cmdp == 'n' && cmdp2 == 'r') else if(str_startswith(Cmd, "p2")) ans = CmdPSK2rawDemod(Cmd+2);
ans = CmdNRZrawDemod(Cmd+2); else PrintAndLogEx(WARNING, "Unknown modulation entered - see help ('h') for parameter structure");
else if(cmdp == 'p' && cmdp2 == '1')
ans = CmdPSK1rawDemod(Cmd+2);
else if(cmdp == 'p' && cmdp2 == '2')
ans = CmdPSK2rawDemod(Cmd+2);
else
PrintAndLogEx(WARNING, "Unknown modulation entered - see help ('h') for parameter structure");
return ans; return ans;
} }
@ -1295,8 +1291,7 @@ void setClockGrid(int clk, int offset) {
} }
} }
int CmdGrid(const char *Cmd) int CmdGrid(const char *Cmd) {
{
sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY); sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY);
PlotGridXdefault = PlotGridX; PlotGridXdefault = PlotGridX;
PlotGridYdefault = PlotGridY; PlotGridYdefault = PlotGridY;
@ -1310,11 +1305,8 @@ int CmdSetGraphMarkers(const char *Cmd) {
return 0; return 0;
} }
int CmdHexsamples(const char *Cmd) int CmdHexsamples(const char *Cmd) {
{ int i, j, requested = 0, offset = 0;
int i, j;
int requested = 0;
int offset = 0;
char string_buf[25]; char string_buf[25];
char* string_ptr = string_buf; char* string_ptr = string_buf;
uint8_t got[BIGBUF_SIZE]; uint8_t got[BIGBUF_SIZE];
@ -1322,9 +1314,9 @@ int CmdHexsamples(const char *Cmd)
sscanf(Cmd, "%i %i", &requested, &offset); sscanf(Cmd, "%i %i", &requested, &offset);
/* if no args send something */ /* if no args send something */
if (requested == 0) { if (requested == 0)
requested = 8; requested = 8;
}
if (offset + requested > sizeof(got)) { if (offset + requested > sizeof(got)) {
PrintAndLogEx(NORMAL, "Tried to read past end of buffer, <bytes> + <offset> > %d", BIGBUF_SIZE); PrintAndLogEx(NORMAL, "Tried to read past end of buffer, <bytes> + <offset> > %d", BIGBUF_SIZE);
return 0; return 0;
@ -1355,17 +1347,14 @@ int CmdHexsamples(const char *Cmd)
return 0; return 0;
} }
int CmdHide(const char *Cmd) int CmdHide(const char *Cmd) {
{
HideGraphWindow(); HideGraphWindow();
return 0; return 0;
} }
//zero mean GraphBuffer //zero mean GraphBuffer
int CmdHpf(const char *Cmd) int CmdHpf(const char *Cmd) {
{ int i, accum = 0;
int i;
int accum = 0;
for (i = 10; i < GraphTraceLen; ++i) for (i = 10; i < GraphTraceLen; ++i)
accum += GraphBuffer[i]; accum += GraphBuffer[i];
@ -1379,18 +1368,15 @@ int CmdHpf(const char *Cmd)
return 0; return 0;
} }
bool _headBit( BitstreamOut *stream) bool _headBit( BitstreamOut *stream) {
{
int bytepos = stream->position >> 3; // divide by 8 int bytepos = stream->position >> 3; // divide by 8
int bitpos = (stream->position++) & 7; // mask out 00000111 int bitpos = (stream->position++) & 7; // mask out 00000111
return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1; return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
} }
uint8_t getByte(uint8_t bits_per_sample, BitstreamOut* b) uint8_t getByte(uint8_t bits_per_sample, BitstreamOut* b) {
{
int i;
uint8_t val = 0; uint8_t val = 0;
for(i = 0 ; i < bits_per_sample; i++) for(int i = 0 ; i < bits_per_sample; i++)
val |= (_headBit(b) << (7-i)); val |= (_headBit(b) << (7-i));
return val; return val;
@ -1426,7 +1412,9 @@ int getSamples(int n, bool silent) {
} }
if (bits_per_sample < 8) { if (bits_per_sample < 8) {
if (!silent) PrintAndLogEx(NORMAL, "Unpacking..."); if (!silent) PrintAndLogEx(NORMAL, "Unpacking...");
BitstreamOut bout = { got, bits_per_sample * n, 0}; BitstreamOut bout = { got, bits_per_sample * n, 0};
int j =0; int j =0;
for (j = 0; j * bits_per_sample < n * 8 && j < n; j++) { for (j = 0; j * bits_per_sample < n * 8 && j < n; j++) {
@ -1434,6 +1422,7 @@ int getSamples(int n, bool silent) {
GraphBuffer[j] = ((int) sample )- 128; GraphBuffer[j] = ((int) sample )- 128;
} }
GraphTraceLen = j; GraphTraceLen = j;
if (!silent) PrintAndLogEx(NORMAL, "Unpacked %d samples" , j ); if (!silent) PrintAndLogEx(NORMAL, "Unpacked %d samples" , j );
} else { } else {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
@ -1444,9 +1433,7 @@ int getSamples(int n, bool silent) {
//ICEMAN todo //ICEMAN todo
// set signal properties low/high/mean/amplitude and is_noice detection // set signal properties low/high/mean/amplitude and is_noice detection
justNoise(got, n); justNoise(GraphBuffer, GraphTraceLen);
// set signal properties low/high/mean/amplitude and isnoice detection
//justNoise_int(GraphBuffer, GraphTraceLen);
setClockGrid(0, 0); setClockGrid(0, 0);
DemodBufferLen = 0; DemodBufferLen = 0;
@ -1454,8 +1441,7 @@ int getSamples(int n, bool silent) {
return 0; return 0;
} }
int CmdSamples(const char *Cmd) int CmdSamples(const char *Cmd) {
{
int n = strtol(Cmd, NULL, 0); int n = strtol(Cmd, NULL, 0);
return getSamples(n, false); return getSamples(n, false);
} }
@ -1550,8 +1536,7 @@ int CmdTuneSamples(const char *Cmd) {
return 0; return 0;
} }
int CmdLoad(const char *Cmd) int CmdLoad(const char *Cmd) {
{
char filename[FILE_PATH_SIZE] = {0x00}; char filename[FILE_PATH_SIZE] = {0x00};
int len = 0; int len = 0;
@ -1579,14 +1564,16 @@ int CmdLoad(const char *Cmd)
DemodBufferLen = 0; DemodBufferLen = 0;
RepaintGraphWindow(); RepaintGraphWindow();
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection // set signal properties low/high/mean/amplitude and isnoice detection
justNoise_int(GraphBuffer, GraphTraceLen); justNoise(GraphBuffer, GraphTraceLen);
return 0; return 0;
} }
int CmdLtrim(const char *Cmd) // trim graph from the end
{ int CmdLtrim(const char *Cmd) {
if (GraphTraceLen <= 0) return 0; // sanitycheck
if (GraphTraceLen <= 0) return 1;
int ds = atoi(Cmd); int ds = atoi(Cmd);
for (int i = ds; i < GraphTraceLen; ++i) for (int i = ds; i < GraphTraceLen; ++i)
@ -1597,10 +1584,14 @@ int CmdLtrim(const char *Cmd)
return 0; return 0;
} }
// trim graph to input argument length // trim graph from the beginning
int CmdRtrim(const char *Cmd) int CmdRtrim(const char *Cmd) {
{
int ds = atoi(Cmd); int ds = atoi(Cmd);
// sanitycheck
if (GraphTraceLen <= ds) return 1;
GraphTraceLen = ds; GraphTraceLen = ds;
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
@ -1611,22 +1602,23 @@ int CmdMtrim(const char *Cmd) {
int start = 0, stop = 0; int start = 0, stop = 0;
sscanf(Cmd, "%i %i", &start, &stop); sscanf(Cmd, "%i %i", &start, &stop);
if (start > GraphTraceLen || stop > GraphTraceLen || start > stop) return 0; if (start > GraphTraceLen || stop > GraphTraceLen || start > stop) return 1;
start++; //leave start position sample
// leave start position sample
start++;
GraphTraceLen = stop - start; GraphTraceLen = stop - start;
for (int i = 0; i < GraphTraceLen; i++) { for (int i = 0; i < GraphTraceLen; i++)
GraphBuffer[i] = GraphBuffer[start+i]; GraphBuffer[i] = GraphBuffer[start+i];
}
return 0; return 0;
} }
int CmdNorm(const char *Cmd) {
int CmdNorm(const char *Cmd)
{
int i; int i;
int max = INT_MIN, min = INT_MAX; int max = INT_MIN, min = INT_MAX;
// Find local min, max
for (i = 10; i < GraphTraceLen; ++i) { for (i = 10; i < GraphTraceLen; ++i) {
if (GraphBuffer[i] > max) max = GraphBuffer[i]; if (GraphBuffer[i] > max) max = GraphBuffer[i];
if (GraphBuffer[i] < min) min = GraphBuffer[i]; if (GraphBuffer[i] < min) min = GraphBuffer[i];
@ -1639,19 +1631,22 @@ int CmdNorm(const char *Cmd)
} }
} }
RepaintGraphWindow(); RepaintGraphWindow();
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen);
return 0; return 0;
} }
int CmdPlot(const char *Cmd) int CmdPlot(const char *Cmd) {
{
ShowGraphWindow(); ShowGraphWindow();
return 0; return 0;
} }
int CmdSave(const char *Cmd) int CmdSave(const char *Cmd) {
{
char filename[FILE_PATH_SIZE] = {0x00};
int len = 0; int len = 0;
char filename[FILE_PATH_SIZE] = {0x00};
len = strlen(Cmd); len = strlen(Cmd);
if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
@ -1659,7 +1654,7 @@ int CmdSave(const char *Cmd)
FILE *f = fopen(filename, "w"); FILE *f = fopen(filename, "w");
if(!f) { if(!f) {
PrintAndLogEx(NORMAL, "couldn't open '%s'", filename); PrintAndLogEx(WARNING, "couldn't open '%s'", filename);
return 0; return 0;
} }
@ -1669,12 +1664,11 @@ int CmdSave(const char *Cmd)
if (f) if (f)
fclose(f); fclose(f);
PrintAndLogEx(NORMAL, "saved to '%s'", Cmd); PrintAndLogEx(SUCCESS, "saved to '%s'", Cmd);
return 0; return 0;
} }
int CmdScale(const char *Cmd) int CmdScale(const char *Cmd) {
{
CursorScaleFactor = atoi(Cmd); CursorScaleFactor = atoi(Cmd);
if (CursorScaleFactor == 0) { if (CursorScaleFactor == 0) {
PrintAndLogEx(FAILED, "bad, can't have zero scale"); PrintAndLogEx(FAILED, "bad, can't have zero scale");
@ -1684,10 +1678,13 @@ int CmdScale(const char *Cmd)
return 0; return 0;
} }
int directionalThreshold(const int* in, int *out, size_t len, int8_t up, int8_t down) int directionalThreshold(const int* in, int *out, size_t len, int8_t up, int8_t down) {
{
int lastValue = in[0]; int lastValue = in[0];
out[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in.
// Will be changed at the end, but init 0 as we adjust to last samples
// value if no threshold kicks in.
out[0] = 0;
for (size_t i = 1; i < len; ++i) { for (size_t i = 1; i < len; ++i) {
// Apply first threshold to samples heading up // Apply first threshold to samples heading up
@ -1708,30 +1705,28 @@ int directionalThreshold(const int* in, int *out, size_t len, int8_t up, int8_t
out[i] = out[i-1]; out[i] = out[i-1];
} }
} }
out[0] = out[1]; // Align with first edited sample.
// Align with first edited sample.
out[0] = out[1];
return 0; return 0;
} }
int CmdDirectionalThreshold(const char *Cmd) int CmdDirectionalThreshold(const char *Cmd) {
{ int8_t up = param_get8(Cmd, 0);
int8_t upThres = param_get8(Cmd, 0); int8_t down = param_get8(Cmd, 1);
int8_t downThres = param_get8(Cmd, 1);
PrintAndLogEx(NORMAL, "Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres); PrintAndLogEx(INFO, "Applying Up Threshold: %d, Down Threshold: %d\n", up, down);
directionalThreshold(GraphBuffer, GraphBuffer,GraphTraceLen, upThres, downThres); directionalThreshold(GraphBuffer, GraphBuffer,GraphTraceLen, up, down);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
} }
int CmdZerocrossings(const char *Cmd) int CmdZerocrossings(const char *Cmd) {
{
// Zero-crossings aren't meaningful unless the signal is zero-mean. // Zero-crossings aren't meaningful unless the signal is zero-mean.
CmdHpf(""); CmdHpf("");
int sign = 1; int sign = 1, zc = 0, lastZc = 0;
int zc = 0;
int lastZc = 0;
for (int i = 0; i < GraphTraceLen; ++i) { for (int i = 0; i < GraphTraceLen; ++i) {
if (GraphBuffer[i] * sign >= 0) { if (GraphBuffer[i] * sign >= 0) {
@ -1749,6 +1744,10 @@ int CmdZerocrossings(const char *Cmd)
} }
} }
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
} }
@ -1758,44 +1757,39 @@ int CmdZerocrossings(const char *Cmd)
* @param Cmd * @param Cmd
* @return * @return
*/ */
int Cmdbin2hex(const char *Cmd) int Cmdbin2hex(const char *Cmd) {
{ int bg = 0, en = 0;
int bg =0, en =0; if (param_getptr(Cmd, &bg, &en, 0))
if(param_getptr(Cmd, &bg, &en, 0))
return usage_data_bin2hex(); return usage_data_bin2hex();
//Number of digits supplied as argument //Number of digits supplied as argument
size_t length = en - bg +1; size_t length = en - bg + 1;
size_t bytelen = (length+7) / 8; size_t bytelen = (length+7) / 8;
uint8_t* arr = (uint8_t *) malloc(bytelen); uint8_t* arr = (uint8_t *) malloc(bytelen);
memset(arr, 0, bytelen); memset(arr, 0, bytelen);
BitstreamOut bout = { arr, 0, 0 }; BitstreamOut bout = { arr, 0, 0 };
for (; bg <= en ;bg++) { for (; bg <= en; bg++) {
char c = Cmd[bg]; char c = Cmd[bg];
if( c == '1') pushBit(&bout, 1); if( c == '1')
else if( c == '0') pushBit(&bout, 0); pushBit(&bout, 1);
else PrintAndLogEx(NORMAL, "Ignoring '%c'", c); else if( c == '0')
pushBit(&bout, 0);
else
PrintAndLogEx(NORMAL, "Ignoring '%c'", c);
} }
if (bout.numbits % 8 != 0) if (bout.numbits % 8 != 0)
PrintAndLogEx(NORMAL, "[padded with %d zeroes]\n", 8-(bout.numbits % 8)); PrintAndLogEx(NORMAL, "[padded with %d zeroes]", 8 - (bout.numbits % 8));
//Uses printf instead of PrintAndLog since the latter PrintAndLogEx(NORMAL, "%s", sprint_hex(arr, bytelen));
// adds linebreaks to each printout - this way was more convenient since we don't have to
// allocate a string and write to that first...
for(size_t x = 0; x < bytelen ; x++)
PrintAndLogEx(NORMAL, "%02X", arr[x]);
PrintAndLogEx(NORMAL, "\n");
free(arr); free(arr);
return 0; return 0;
} }
int Cmdhex2bin(const char *Cmd) int Cmdhex2bin(const char *Cmd) {
{ int bg = 0, en = 0;
int bg =0, en =0; if (param_getptr(Cmd, &bg, &en, 0)) return usage_data_hex2bin();
if(param_getptr(Cmd, &bg, &en, 0)) return usage_data_hex2bin();
while (bg <= en ) { while (bg <= en ) {
char x = Cmd[bg++]; char x = Cmd[bg++];
@ -1813,12 +1807,10 @@ int Cmdhex2bin(const char *Cmd)
//Uses printf instead of PrintAndLog since the latter //Uses printf instead of PrintAndLog since the latter
// adds linebreaks to each printout - this way was more convenient since we don't have to // adds linebreaks to each printout - this way was more convenient since we don't have to
// allocate a string and write to that first... // allocate a string and write to that first...
for(int i = 0 ; i < 4 ; ++i)
for(int i= 0 ; i < 4 ; ++i)
PrintAndLogEx(NORMAL, "%d",(x >> (3 - i)) & 1); PrintAndLogEx(NORMAL, "%d",(x >> (3 - i)) & 1);
} }
PrintAndLogEx(NORMAL, "\n"); PrintAndLogEx(NORMAL, "\n");
return 0; return 0;
} }
@ -1845,7 +1837,7 @@ void GetHiLoTone(int *LowTone, int *HighTone, int clk, int LowToneFC, int HighTo
int Right_Modifier = (clk % LowToneFC) / 2; int Right_Modifier = (clk % LowToneFC) / 2;
//int HighToneMod = clk mod HighToneFC; //int HighToneMod = clk mod HighToneFC;
int LeftHalfFCCnt = (LowToneFC % 2) + (LowToneFC/2); //truncate int LeftHalfFCCnt = (LowToneFC % 2) + (LowToneFC/2); //truncate
int FCs_per_clk = clk/LowToneFC; int FCs_per_clk = clk / LowToneFC;
// need to correctly split up the clock to field clocks. // need to correctly split up the clock to field clocks.
// First attempt uses modifiers on each end to make up for when FCs don't evenly divide into Clk // First attempt uses modifiers on each end to make up for when FCs don't evenly divide into Clk
@ -1860,14 +1852,14 @@ void GetHiLoTone(int *LowTone, int *HighTone, int clk, int LowToneFC, int HighTo
for (i = 0; i < (FCs_per_clk); i++) { for (i = 0; i < (FCs_per_clk); i++) {
// loop # of samples per field clock // loop # of samples per field clock
for (j = 0; j < LowToneFC; j++) { for (j = 0; j < LowToneFC; j++) {
LowTone[(i*LowToneFC)+Left_Modifier+j] = ( j < LeftHalfFCCnt ) ? 1 : -1; LowTone[ (i * LowToneFC) + Left_Modifier + j] = ( j < LeftHalfFCCnt ) ? 1 : -1;
} }
} }
int k; int k;
// add last -1 modifiers // add last -1 modifiers
for (k = 0; k < Right_Modifier; k++) { for (k = 0; k < Right_Modifier; k++) {
LowTone[((i-1)*LowToneFC)+Left_Modifier+j+k] = -1; LowTone[ ( (i-1) * LowToneFC) + Left_Modifier + j + k] = -1;
} }
// now do hightone // now do hightone
@ -1884,18 +1876,18 @@ void GetHiLoTone(int *LowTone, int *HighTone, int clk, int LowToneFC, int HighTo
for (i = 0; i < (FCs_per_clk); i++) { for (i = 0; i < (FCs_per_clk); i++) {
// loop # of samples per field clock // loop # of samples per field clock
for (j = 0; j < HighToneFC; j++) { for (j = 0; j < HighToneFC; j++) {
HighTone[(i*HighToneFC)+Left_Modifier+j] = ( j < LeftHalfFCCnt ) ? 1 : -1; HighTone[(i * HighToneFC) + Left_Modifier + j] = ( j < LeftHalfFCCnt ) ? 1 : -1;
} }
} }
// add last -1 modifiers // add last -1 modifiers
for (k = 0; k < Right_Modifier; k++) { for (k = 0; k < Right_Modifier; k++) {
PrintAndLogEx(NORMAL, "(i-1)*HighToneFC+lm+j+k %i",((i-1)*HighToneFC)+Left_Modifier+j+k); PrintAndLogEx(NORMAL, "(i-1)*HighToneFC+lm+j+k %i", ((i-1) * HighToneFC) + Left_Modifier + j + k);
HighTone[((i-1)*HighToneFC)+Left_Modifier+j+k] = -1; HighTone[ ( (i-1) * HighToneFC) + Left_Modifier + j + k] = -1;
} }
if (g_debugMode == 2) { if (g_debugMode == 2) {
for ( i = 0; i < clk; i++) { for ( i = 0; i < clk; i++) {
PrintAndLogEx(NORMAL, "Low: %i, High: %i",LowTone[i],HighTone[i]); PrintAndLogEx(NORMAL, "Low: %i, High: %i", LowTone[i], HighTone[i]);
} }
} }
} }
@ -1903,7 +1895,7 @@ void GetHiLoTone(int *LowTone, int *HighTone, int clk, int LowToneFC, int HighTo
//old CmdFSKdemod adapted by marshmellow //old CmdFSKdemod adapted by marshmellow
//converts FSK to clear NRZ style wave. (or demodulates) //converts FSK to clear NRZ style wave. (or demodulates)
int FSKToNRZ(int *data, int *dataLen, int clk, int LowToneFC, int HighToneFC) { int FSKToNRZ(int *data, int *dataLen, int clk, int LowToneFC, int HighToneFC) {
uint8_t ans=0; uint8_t ans = 0;
if (clk == 0 || LowToneFC == 0 || HighToneFC == 0) { if (clk == 0 || LowToneFC == 0 || HighToneFC == 0) {
int firstClockEdge=0; int firstClockEdge=0;
ans = fskClocks((uint8_t *) &LowToneFC, (uint8_t *) &HighToneFC, (uint8_t *) &clk, &firstClockEdge); ans = fskClocks((uint8_t *) &LowToneFC, (uint8_t *) &HighToneFC, (uint8_t *) &clk, &firstClockEdge);
@ -1912,25 +1904,25 @@ int FSKToNRZ(int *data, int *dataLen, int clk, int LowToneFC, int HighToneFC) {
} }
} }
// currently only know fsk modulations with field clocks < 10 samples and > 4 samples. filter out to remove false positives (and possibly destroying ask/psk modulated waves...) // currently only know fsk modulations with field clocks < 10 samples and > 4 samples. filter out to remove false positives (and possibly destroying ask/psk modulated waves...)
if (ans == 0 || clk == 0 || LowToneFC == 0 || HighToneFC == 0 || LowToneFC > 10 || HighToneFC < 4) { if (ans == 0 || clk == 0 || LowToneFC == 0 || HighToneFC == 0 || LowToneFC > 10 || HighToneFC < 4) {
if (g_debugMode > 1) { if (g_debugMode > 1) {
PrintAndLog ("DEBUG FSKtoNRZ: no fsk clocks found"); PrintAndLog ("DEBUG FSKtoNRZ: no fsk clocks found");
} }
return 0; return 0;
} }
int i, j;
int LowTone[clk]; int LowTone[clk];
int HighTone[clk]; int HighTone[clk];
GetHiLoTone(LowTone, HighTone, clk, LowToneFC, HighToneFC); GetHiLoTone(LowTone, HighTone, clk, LowToneFC, HighToneFC);
int i, j;
// loop through ([all samples] - clk) // loop through ([all samples] - clk)
for (i = 0; i < *dataLen - clk; ++i) { for (i = 0; i < *dataLen - clk; ++i) {
int lowSum = 0, highSum = 0; int lowSum = 0, highSum = 0;
// sum all samples together starting from this sample for [clk] samples for each tone (multiply tone value with sample data) // sum all samples together starting from this sample for [clk] samples for each tone (multiply tone value with sample data)
for (j = 0; j < clk; ++j) { for (j = 0; j < clk; ++j) {
lowSum += LowTone[j] * data[i+j]; lowSum += LowTone[j] * data[i + j];
highSum += HighTone[j] * data[i + j]; highSum += HighTone[j] * data[i + j];
} }
// get abs( [average sample value per clk] * 100 ) (or a rolling average of sorts) // get abs( [average sample value per clk] * 100 ) (or a rolling average of sorts)
@ -1963,18 +1955,14 @@ int FSKToNRZ(int *data, int *dataLen, int clk, int LowToneFC, int HighToneFC) {
return 0; return 0;
} }
int CmdFSKToNRZ(const char *Cmd) { int CmdFSKToNRZ(const char *Cmd) {
// take clk, fc_low, fc_high // take clk, fc_low, fc_high
// blank = auto; // blank = auto;
bool errors = false; bool errors = false;
int clk = 0;
char cmdp = 0; char cmdp = 0;
int fc_low = 10, fc_high = 8; int clk = 0, fc_low = 10, fc_high = 8;
while(param_getchar(Cmd, cmdp) != 0x00) while (param_getchar(Cmd, cmdp) != 0x00) {
{ switch (tolower(param_getchar(Cmd, cmdp))) {
switch (tolower(param_getchar(Cmd, cmdp)))
{
case 'h': case 'h':
return usage_data_fsktonrz(); return usage_data_fsktonrz();
case 'c': case 'c':
@ -1997,9 +1985,9 @@ int CmdFSKToNRZ(const char *Cmd) {
if(errors) break; if(errors) break;
} }
//Validations //Validations
if(errors) return usage_data_fsktonrz(); if (errors) return usage_data_fsktonrz();
setClockGrid(0,0); setClockGrid(0, 0);
DemodBufferLen = 0; DemodBufferLen = 0;
int ans = FSKToNRZ(GraphBuffer, &GraphTraceLen, clk, fc_low, fc_high); int ans = FSKToNRZ(GraphBuffer, &GraphTraceLen, clk, fc_low, fc_high);
CmdNorm(""); CmdNorm("");
@ -2007,17 +1995,15 @@ int CmdFSKToNRZ(const char *Cmd) {
return ans; return ans;
} }
int CmdDataIIR(const char *Cmd){ int CmdDataIIR(const char *Cmd){
uint8_t k = param_get8(Cmd,0); uint8_t k = param_get8(Cmd, 0);
//iceIIR_Butterworth(GraphBuffer, GraphTraceLen); //iceIIR_Butterworth(GraphBuffer, GraphTraceLen);
iceSimple_Filter(GraphBuffer, GraphTraceLen, k); iceSimple_Filter(GraphBuffer, GraphTraceLen, k);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
} }
static command_t CommandTable[] = static command_t CommandTable[] = {
{
{"help", CmdHelp, 1, "This help"}, {"help", CmdHelp, 1, "This help"},
{"askedgedetect", CmdAskEdgeDetect, 1, "[threshold] Adjust Graph for manual ASK demod using the length of sample differences to detect the edge of a wave (use 20-45, def:25)"}, {"askedgedetect", CmdAskEdgeDetect, 1, "[threshold] Adjust Graph for manual ASK demod using the length of sample differences to detect the edge of a wave (use 20-45, def:25)"},
{"autocorr", CmdAutoCorr, 1, "[window length] [g] -- Autocorrelation over window - g to save back to GraphBuffer (overwrite)"}, {"autocorr", CmdAutoCorr, 1, "[window length] [g] -- Autocorrelation over window - g to save back to GraphBuffer (overwrite)"},
@ -2058,14 +2044,13 @@ static command_t CommandTable[] =
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };
int CmdData(const char *Cmd){ int CmdData(const char *Cmd) {
clearCommandBuffer(); clearCommandBuffer();
CmdsParse(CommandTable, Cmd); CmdsParse(CommandTable, Cmd);
return 0; return 0;
} }
int CmdHelp(const char *Cmd) int CmdHelp(const char *Cmd) {
{
CmdsHelp(CommandTable); CmdsHelp(CommandTable);
return 0; return 0;
} }