- if (g_debugMode) \r
- PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d", clk, invert, bitlen);\r
-\r
- //move bits back to DemodBuffer\r
- setDemodBuf(bits, bitlen, 0);\r
- printBitStream(bits,bitlen);\r
- \r
- // bits has the manchester encoded data.\r
- errCnt = manrawdecode(bits, &bitlen); \r
- if ( errCnt == -1 || bitlen < 16 ){ \r
- PrintAndLog("no data found");\r
- if (g_debugMode) \r
- PrintAndLog("errCnt: %d, bitlen: %d, clk: %d, invert: %d", errCnt, bitlen, clk, invert);\r
- return 4;\r
+}\r
+\r
+int CmdT55xxDetect(const char *Cmd){\r
+ char cmdp = param_getchar(Cmd, 0);\r
+ if (cmdp == 'h' || cmdp == 'H')\r
+ return usage_t55xx_detect();\r
+ \r
+ // read block 0, Page 0. Configuration.\r
+ UsbCommand c = {CMD_T55XX_READ_BLOCK, {0, 0, 0}};\r
+ c.d.asBytes[0] = 0x0; \r
+\r
+ //Password mode\r
+ // if ( res == 2 ) {\r
+ // c.arg[2] = password;\r
+ // c.d.asBytes[0] = 0x1; \r
+ // }\r
+\r
+ SendCommand(&c);\r
+ if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {\r
+ PrintAndLog("command execution time out");\r
+ return FALSE;\r
+ }\r
+ \r
+ uint8_t got[12000];\r
+ GetFromBigBuf(got,sizeof(got),0);\r
+ WaitForResponse(CMD_ACK,NULL);\r
+ setGraphBuf(got, 12000);\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
+ \r
+ uint8_t hits = 0;\r
+ t55xx_conf_block_t tests[10];\r
+ \r
+ if (GetFskClock("", FALSE, FALSE)){ \r
+ if ( FSKrawDemod("0 0", FALSE) && test()){\r
+ tests[hits].modulation = DEMOD_FSK;\r
+ tests[hits].inversed = FALSE;\r
+ ++hits;\r
+ }\r
+ if ( FSKrawDemod("0 1", FALSE) && test()) {\r
+ tests[hits].modulation = DEMOD_FSK;\r
+ tests[hits].inversed = TRUE;\r
+ ++hits;\r
+ }\r
+ } else {\r
+ if ( ASKmanDemod("0 0 1", FALSE, FALSE) && test()) {\r
+ tests[hits].modulation = DEMOD_ASK;\r
+ tests[hits].inversed = FALSE;\r
+ ++hits;\r
+ }\r
+\r
+ if ( ASKmanDemod("0 1 1", FALSE, FALSE) && test()) {\r
+ tests[hits].modulation = DEMOD_ASK;\r
+ tests[hits].inversed = TRUE;\r
+ ++hits;\r
+ }\r
+ \r
+ if ( NRZrawDemod("0 0 1", FALSE) && test()) {\r
+ tests[hits].modulation = DEMOD_NZR;\r
+ tests[hits].inversed = FALSE;\r
+ ++hits;\r
+ }\r
+\r
+ if ( NRZrawDemod("0 1 1", FALSE) && test()) {\r
+ tests[hits].modulation = DEMOD_NZR;\r
+ tests[hits].inversed = TRUE;\r
+ ++hits;\r
+ }\r
+ \r
+ if ( PSKDemod("0 0 1", FALSE) && test()) {\r
+ tests[hits].modulation = DEMOD_PSK;\r
+ tests[hits].inversed = FALSE;\r
+ ++hits;\r
+ }\r
+ \r
+ if ( PSKDemod("0 1 1", FALSE) && test()) {\r
+ tests[hits].modulation = DEMOD_PSK;\r
+ tests[hits].inversed = TRUE;\r
+ ++hits;\r
+ }\r
+ //PSK2?\r
+ // if (!BiphaseRawDecode("0",FALSE) && test()) {\r
+ // tests[++hits].modulation = DEMOD_BI;\r
+ // tests[hits].inversed = FALSE;\r
+ //}\r
+ // if (!BiphaseRawDecode("1",FALSE) && test()) {\r
+ // tests[++hits].modulation = DEMOD_BI;\r
+ // tests[hits].inversed = TRUE;\r
+ // }\r
+ } \r
+ if ( hits == 1) {\r
+ config.modulation = tests[0].modulation;\r
+ config.inversed = tests[0].inversed;\r
+ printConfiguration( config );\r
+ return TRUE;\r
+ }\r
+ \r
+ if ( hits > 1) {\r
+ PrintAndLog("Found [%d] possible matches for modulation.",hits);\r
+ for(int i=0; i<hits; ++i){\r
+ PrintAndLog("--[%d]---------------", i+1);\r
+ printConfiguration( tests[i] );\r
+ }\r
+ }\r
+ return FALSE;\r
+}\r
+\r
+bool test(){\r
+\r
+ if ( !DemodBufferLen) \r
+ return false;\r
+ \r
+ if ( PackBits(0, 32, DemodBuffer) == 0x00 )\r
+ return FALSE;\r
+ \r
+ uint8_t si = 0;\r
+ \r
+ uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; \r
+ uint8_t resv = PackBits(si, 7, DemodBuffer); si += 7+3;\r
+ uint8_t extend = PackBits(si, 1, DemodBuffer); si += 1;\r
+\r
+ //PrintAndLog("test: %X %X %X ", safer, resv, extend);\r
+ \r
+ // 2nibble must be zeroed.\r
+ if ( resv > 0x00) return FALSE;\r
+\r
+ if ( safer == 0x6 || safer == 0x9){\r
+ if ( extend == 0x00)\r
+ return TRUE;\r