+// detect configuration?\r
+bool tryDetectModulation(){\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
+ \r
+ if (GetFskClock("", FALSE, FALSE)){ \r
+ fskClocks(&fc1, &fc2, &clk, FALSE);\r
+ if ( FSKrawDemod("0 0", FALSE) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){\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
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+ if ( FSKrawDemod("0 1", FALSE) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\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
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+ } else {\r
+ clk = GetAskClock("", FALSE, FALSE);\r
+ if (clk>0) {\r
+ tests[hits].ST = TRUE;\r
+ // "0 0 1 " == clock auto, invert false, maxError 1.\r
+ // false = no verbose\r
+ // false = no emSearch\r
+ // 1 = Ask/Man\r
+ // st = true\r
+ if ( ASKDemod_ext("0 0 1", FALSE, FALSE, 1, &tests[hits].ST) && test(DEMOD_ASK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\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
+ tests[hits].ST = TRUE;\r
+ // "0 0 1 " == clock auto, invert true, maxError 1.\r
+ // false = no verbose\r
+ // false = no emSearch\r
+ // 1 = Ask/Man\r
+ // st = true\r
+ if ( ASKDemod_ext("0 1 1", FALSE, FALSE, 1, &tests[hits].ST) && test(DEMOD_ASK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\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 2", FALSE) && test(DEMOD_BI, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5) ) {\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
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+ if ( ASKbiphaseDemod("0 0 1 2", FALSE) && test(DEMOD_BIa, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5) ) {\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
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+ }\r
+ //undo trim from ask\r
+ //save_restoreGB(0);\r
+ clk = GetNrzClock("", FALSE, FALSE);\r
+ if (clk>0) {\r
+ if ( NRZrawDemod("0 0 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\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
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+\r
+ if ( NRZrawDemod("0 1 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\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
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+ }\r
+ \r
+ // allow undo\r
+ // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)\r
+ save_restoreGB(1);\r
+ CmdLtrim("160");\r
+ clk = GetPskClock("", FALSE, FALSE);\r
+ if (clk>0) {\r
+ if ( PSKDemod("0 0 6", FALSE) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\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
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+ if ( PSKDemod("0 1 6", FALSE) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\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
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+ // PSK2 - needs a call to psk1TOpsk2.\r
+ if ( PSKDemod("0 0 6", FALSE)) {\r
+ psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
+ if (test(DEMOD_PSK2, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){\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
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+ } // inverse waves does not affect this demod\r
+ // PSK3 - needs a call to psk1TOpsk2.\r
+ if ( PSKDemod("0 0 6", FALSE)) {\r
+ psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
+ if (test(DEMOD_PSK3, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){\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
+ tests[hits].ST = FALSE;\r
+ ++hits;\r
+ }\r
+ } // inverse waves does not affect this demod\r
+ }\r
+ //undo trim samples\r
+ save_restoreGB(0);\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
+ config.Q5 = tests[0].Q5;\r
+ config.ST = tests[0].ST;\r
+ printConfiguration( config );\r
+ return TRUE;\r
+ }\r
+ \r
+ bool retval = FALSE;\r
+ if ( hits > 1) {\r
+ PrintAndLog("Found [%d] possible matches for modulation.",hits);\r
+ for(int i=0; i<hits; ++i){\r
+ retval = testKnownConfigBlock(tests[i].block0);\r
+ if ( retval ) { \r
+ PrintAndLog("--[%d]--------------- << selected this", i+1);\r
+ config.modulation = tests[i].modulation;\r
+ config.bitrate = tests[i].bitrate;\r
+ config.inverted = tests[i].inverted;\r
+ config.offset = tests[i].offset;\r
+ config.block0 = tests[i].block0;\r
+ config.Q5 = tests[i].Q5;\r
+ config.ST = tests[i].ST;\r
+ } else {\r
+ PrintAndLog("--[%d]---------------", i+1);\r
+ }\r
+ printConfiguration( tests[i] );\r
+ }\r
+ }\r
+ return retval;\r
+}\r
+\r
+bool testKnownConfigBlock(uint32_t block0) {\r
+ switch(block0){\r
+ case T55X7_DEFAULT_CONFIG_BLOCK:\r
+ case T55X7_RAW_CONFIG_BLOCK:\r
+ case T55X7_EM_UNIQUE_CONFIG_BLOCK:\r
+ case T55X7_FDXB_CONFIG_BLOCK:\r
+ case T55X7_HID_26_CONFIG_BLOCK:\r
+ case T55X7_PYRAMID_CONFIG_BLOCK:\r
+ case T55X7_INDALA_64_CONFIG_BLOCK:\r
+ case T55X7_INDALA_224_CONFIG_BLOCK:\r
+ case T55X7_GUARDPROXII_CONFIG_BLOCK:\r
+ case T55X7_VIKING_CONFIG_BLOCK:\r
+ case T55X7_NORALYS_CONFIG_BLOCK:\r
+ case T55X7_IOPROX_CONFIG_BLOCK:\r
+ case T55X7_PRESCO_CONFIG_BLOCK:\r
+ return TRUE;\r
+ }\r
+ return FALSE;\r
+}\r