+//by marshmellow
+//attempt to detect the field clock and bit clock for FSK
+int CmdFSKfcDetect(const char *Cmd)
+{
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ size_t size = getFromGraphBuf(BitStream);
+
+ uint16_t ans = countFC(BitStream, size);
+ if (ans==0) {
+ if (g_debugMode) PrintAndLog("DEBUG: No data found");
+ return 0;
+ }
+ uint8_t fc1, fc2;
+ fc1 = (ans >> 8) & 0xFF;
+ fc2 = ans & 0xFF;
+
+ uint8_t rf1 = detectFSKClk(BitStream, size, fc1, fc2);
+ if (rf1==0) {
+ if (g_debugMode) PrintAndLog("DEBUG: Clock detect error");
+ return 0;
+ }
+ PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
+ return 1;
+}
+
+//by marshmellow
+//attempt to detect the bit clock for PSK or NRZ modulations
+int CmdDetectNRZpskClockRate(const char *Cmd)
+{
+ GetNRZpskClock("",0,0);
+ return 0;
+}
+
+//by marshmellow
+//attempt to psk1 or nrz demod graph buffer
+//NOTE CURRENTLY RELIES ON PEAKS :(
+int PSKnrzDemod(const char *Cmd, uint8_t verbose)
+{
+ int invert=0;
+ int clk=0;
+ sscanf(Cmd, "%i %i", &clk, &invert);
+ if (invert != 0 && invert != 1) {
+ PrintAndLog("Invalid argument: %s", Cmd);
+ return -1;
+ }
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ 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)
+ if (g_debugMode==1) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
+ return -1;
+ }
+ if (verbose) PrintAndLog("Tried PSK/NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
+
+ //prime demod buffer for output
+ setDemodBuf(BitStream,BitLen,0);
+ return errCnt;
+}
+// Indala 26 bit decode
+// by marshmellow
+// optional arguments - same as CmdpskNRZrawDemod (clock & invert)
+int CmdIndalaDecode(const char *Cmd)
+{
+ int ans;
+ if (strlen(Cmd)>0){
+ ans = PSKnrzDemod(Cmd, 0);
+ } else{ //default to RF/32
+ ans = PSKnrzDemod("32", 0);
+ }
+
+ if (ans < 0){
+ if (g_debugMode==1)
+ PrintAndLog("Error1: %d",ans);
+ return 0;
+ }
+ uint8_t invert=0;
+ ans = indala26decode(DemodBuffer,(size_t *) &DemodBufferLen, &invert);
+ if (ans < 1) {
+ if (g_debugMode==1)
+ PrintAndLog("Error2: %d",ans);
+ return -1;
+ }
+ char showbits[251]={0x00};
+ if (invert)
+ if (g_debugMode==1)
+ PrintAndLog("Had to invert bits");
+
+ //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<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){
+ PrintAndLog("DEBUG: printing demodbuffer:");
+ printDemodBuff();
+ }
+ return 1;
+}
+
+//by marshmellow
+//attempt to clean psk wave noise after a peak
+//NOTE RELIES ON PEAKS :(
+int CmdPskClean(const char *Cmd)
+{
+ uint8_t bitStream[MAX_GRAPH_TRACE_LEN]={0};
+ size_t bitLen = getFromGraphBuf(bitStream);
+ pskCleanWave(bitStream, bitLen);
+ setGraphBuf(bitStream, bitLen);
+ return 0;
+}
+
+// by marshmellow
+// takes 2 arguments - clock and invert both as integers
+// attempts to demodulate psk only
+// prints binary found and saves in demodbuffer for further commands
+int CmdpskNRZrawDemod(const char *Cmd)
+{
+ int errCnt;
+
+ errCnt = PSKnrzDemod(Cmd, 1);
+ //output
+ if (errCnt<0){
+ if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);
+ return 0;
+ }
+ if (errCnt>0){
+ if (g_debugMode){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ PrintAndLog("PSK or NRZ demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printDemodBuff();
+ }
+ }else{
+ PrintAndLog("PSK or NRZ demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printDemodBuff();
+ return 1;
+ }
+ return 0;
+}
+
+// by marshmellow
+// takes same args as cmdpsknrzrawdemod
+int CmdPSK2rawDemod(const char *Cmd)
+{
+ int errCnt=0;
+ errCnt=PSKnrzDemod(Cmd, 1);
+ if (errCnt<0){
+ if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);
+ return 0;
+ }
+ psk1TOpsk2(DemodBuffer, DemodBufferLen);
+ if (errCnt>0){
+ if (g_debugMode){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ PrintAndLog("PSK2 demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printDemodBuff();
+ }
+ }else{
+ PrintAndLog("PSK2 demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printDemodBuff();
+ }
+ return 1;
+}
+