+ char buf[30] = {0x00};\r
+ char *cmdStr = buf;\r
+ int ans = 0;\r
+ uint8_t bitRate[8] = {8,16,32,40,50,64,100,128};\r
+ DemodBufferLen = 0x00;\r
+\r
+ //trim 1/2 a clock from beginning\r
+ snprintf(cmdStr, sizeof(buf),"%d", bitRate[config.bitrate]/2 );\r
+ CmdLtrim(cmdStr);\r
+ switch( config.modulation ){\r
+ case DEMOD_FSK:\r
+ snprintf(cmdStr, sizeof(buf),"%d %d", bitRate[config.bitrate], config.inverted );\r
+ ans = FSKrawDemod(cmdStr, FALSE);\r
+ break;\r
+ case DEMOD_FSK1:\r
+ case DEMOD_FSK1a:\r
+ snprintf(cmdStr, sizeof(buf),"%d %d 8 5", bitRate[config.bitrate], config.inverted );\r
+ ans = FSKrawDemod(cmdStr, FALSE);\r
+ break;\r
+ case DEMOD_FSK2:\r
+ case DEMOD_FSK2a:\r
+ snprintf(cmdStr, sizeof(buf),"%d %d 10 8", bitRate[config.bitrate], config.inverted );\r
+ ans = FSKrawDemod(cmdStr, FALSE);\r
+ break;\r
+ case DEMOD_ASK:\r
+ snprintf(cmdStr, sizeof(buf),"%d %d 0", bitRate[config.bitrate], config.inverted );\r
+ ans = ASKDemod(cmdStr, FALSE, FALSE, 1);\r
+ break;\r
+ case DEMOD_PSK1:\r
+ snprintf(cmdStr, sizeof(buf),"%d %d 0", bitRate[config.bitrate], config.inverted );\r
+ ans = PSKDemod(cmdStr, FALSE);\r
+ break;\r
+ case DEMOD_PSK2: //inverted won't affect this\r
+ case DEMOD_PSK3: //not fully implemented\r
+ snprintf(cmdStr, sizeof(buf),"%d 0 1", bitRate[config.bitrate] );\r
+ ans = PSKDemod(cmdStr, FALSE);\r
+ psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
+ break;\r
+ case DEMOD_NRZ:\r
+ snprintf(cmdStr, sizeof(buf),"%d %d 1", bitRate[config.bitrate], config.inverted );\r
+ ans = NRZrawDemod(cmdStr, FALSE);\r
+ break;\r
+ case DEMOD_BI:\r
+ case DEMOD_BIa:\r
+ snprintf(cmdStr, sizeof(buf),"0 %d %d 0", bitRate[config.bitrate], config.inverted );\r
+ ans = ASKbiphaseDemod(cmdStr, FALSE);\r
+ break;\r
+ default:\r
+ return FALSE;\r
+ }\r
+ return (bool) ans;\r
+}\r
+\r
+int CmdT55xxDetect(const char *Cmd){\r
+\r
+ char cmdp = param_getchar(Cmd, 0);\r
+ if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')\r
+ return usage_t55xx_detect();\r
+ \r
+ if (strlen(Cmd)==0)\r
+ AquireData( CONFIGURATION_BLOCK );\r
+\r
+ if ( !tryDetectModulation() )\r
+ PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");\r
+\r
+ return 0;\r
+}\r
+\r
+// detect configuration?\r
+bool tryDetectModulation(){\r
+ char cmdStr[8] = {0};\r
+ uint8_t hits = 0;\r
+ t55xx_conf_block_t tests[15];\r
+ int bitRate=0;\r
+ uint8_t fc1 = 0, fc2 = 0, clk=0;\r
+ save_restoreGB(1);\r
+ if (GetFskClock("", FALSE, FALSE)){ \r
+ fskClocks(&fc1, &fc2, &clk, FALSE);\r
+ sprintf(cmdStr,"%d", clk/2);\r
+ CmdLtrim(cmdStr);\r
+ if ( FSKrawDemod("0 0", FALSE) && test(DEMOD_FSK, &tests[hits].offset, &bitRate)){\r
+ tests[hits].modulation = DEMOD_FSK;\r
+ if (fc1==8 && fc2 == 5)\r
+ tests[hits].modulation = DEMOD_FSK1a;\r
+ else if (fc1==10 && fc2 == 8)\r
+ tests[hits].modulation = DEMOD_FSK2;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( FSKrawDemod("0 1", FALSE) && test(DEMOD_FSK, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_FSK;\r
+ if (fc1 == 8 && fc2 == 5)\r
+ tests[hits].modulation = DEMOD_FSK1;\r
+ else if (fc1 == 10 && fc2 == 8)\r
+ tests[hits].modulation = DEMOD_FSK2a;\r
+\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ } else {\r
+ clk = GetAskClock("", FALSE, FALSE);\r
+ if (clk>0) {\r
+ sprintf(cmdStr,"%d", clk/2);\r
+ CmdLtrim(cmdStr);\r
+ if ( ASKDemod("0 0 0", FALSE, FALSE, 1) && test(DEMOD_ASK, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_ASK;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( ASKDemod("0 1 0", FALSE, FALSE, 1) && test(DEMOD_ASK, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_ASK;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( ASKbiphaseDemod("0 0 0 0", FALSE) && test(DEMOD_BI, &tests[hits].offset, &bitRate) ) {\r
+ tests[hits].modulation = DEMOD_BI;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( ASKbiphaseDemod("0 0 1 0", FALSE) && test(DEMOD_BIa, &tests[hits].offset, &bitRate) ) {\r
+ tests[hits].modulation = DEMOD_BIa;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ }\r
+ //undo trim from ask\r
+ save_restoreGB(0);\r
+ clk = GetNrzClock("", FALSE, FALSE);\r
+ if (clk>0) {\r
+ sprintf(cmdStr,"%d", clk/2);\r
+ CmdLtrim(cmdStr);\r
+ if ( NRZrawDemod("0 0 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_NRZ;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+\r
+ if ( NRZrawDemod("0 1 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_NRZ;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ }\r
+ \r
+ //undo trim from nrz\r
+ save_restoreGB(0);\r
+ clk = GetPskClock("", FALSE, FALSE);\r
+ if (clk>0) {\r
+ PrintAndLog("clk %d",clk);\r
+ sprintf(cmdStr,"%d", clk/2);\r
+ CmdLtrim(cmdStr); \r
+ if ( PSKDemod("0 0 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_PSK1;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( PSKDemod("0 1 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_PSK1;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ // PSK2 - needs a call to psk1TOpsk2.\r
+ if ( PSKDemod("0 0 1", FALSE)) {\r
+ psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
+ if (test(DEMOD_PSK2, &tests[hits].offset, &bitRate)){\r
+ tests[hits].modulation = DEMOD_PSK2;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ } // inverse waves does not affect this demod\r
+ // PSK3 - needs a call to psk1TOpsk2.\r
+ if ( PSKDemod("0 0 1", FALSE)) {\r
+ psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
+ if (test(DEMOD_PSK3, &tests[hits].offset, &bitRate)){\r
+ tests[hits].modulation = DEMOD_PSK3;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ } // inverse waves does not affect this demod\r
+ }\r
+ } \r
+ if ( hits == 1) {\r
+ config.modulation = tests[0].modulation;\r
+ config.bitrate = tests[0].bitrate;\r
+ config.inverted = tests[0].inverted;\r
+ config.offset = tests[0].offset;\r
+ config.block0 = tests[0].block0;\r
+ printConfiguration( config );\r
+ return TRUE;\r
+ }\r