e04475c4 |
1 | //----------------------------------------------------------------------------- |
2 | // Authored by Iceman |
3 | // |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, |
5 | // at your option, any later version. See the LICENSE.txt file for the text of |
6 | // the license. |
7 | //----------------------------------------------------------------------------- |
8 | // Low frequency COTAG commands |
9 | //----------------------------------------------------------------------------- |
ad939de5 |
10 | |
11 | #include "cmdlfcotag.h" |
12 | |
7cb8516c |
13 | #include <stdio.h> |
14 | #include <string.h> |
15 | #include <stdint.h> |
ad939de5 |
16 | #include "comms.h" |
7cb8516c |
17 | #include "ui.h" |
18 | #include "cmddata.h" |
7cb8516c |
19 | #include "lfdemod.h" |
20 | #include "usb_cmd.h" |
21 | #include "cmdmain.h" |
e04475c4 |
22 | |
23 | static int CmdHelp(const char *Cmd); |
24 | |
25 | int usage_lf_cotag_read(void){ |
26 | PrintAndLog("Usage: lf COTAG read [h] <signaldata>"); |
27 | PrintAndLog("Options:"); |
28 | PrintAndLog(" h : This help"); |
29 | PrintAndLog(" <0|1|2> : 0 - HIGH/LOW signal; maxlength bigbuff"); |
30 | PrintAndLog(" : 1 - translation of HI/LO into bytes with manchester 0,1"); |
31 | PrintAndLog(" : 2 - raw signal; maxlength bigbuff"); |
32 | PrintAndLog(""); |
33 | PrintAndLog("Sample:"); |
34 | PrintAndLog(" lf cotag read 0"); |
35 | PrintAndLog(" lf cotag read 1"); |
36 | return 0; |
37 | } |
38 | |
39 | // COTAG demod should be able to use GraphBuffer, |
40 | // when data load samples |
41 | int CmdCOTAGDemod(const char *Cmd) { |
42 | |
43 | uint8_t bits[COTAG_BITS] = {0}; |
44 | size_t bitlen = COTAG_BITS; |
45 | memcpy(bits, DemodBuffer, COTAG_BITS); |
46 | |
669959bc |
47 | uint8_t alignPos = 0; |
48 | int err = manrawdecode(bits, &bitlen, 1, &alignPos); |
e04475c4 |
49 | if (err){ |
50 | if (g_debugMode) PrintAndLog("DEBUG: Error - COTAG too many errors: %d", err); |
51 | return -1; |
52 | } |
53 | |
54 | setDemodBuf(bits, bitlen, 0); |
55 | |
56 | //got a good demod |
57 | uint16_t cn = bytebits_to_byteLSBF(bits+1, 16); |
58 | uint32_t fc = bytebits_to_byteLSBF(bits+1+16, 8); |
59 | |
60 | uint32_t raw1 = bytebits_to_byteLSBF(bits, 32); |
61 | uint32_t raw2 = bytebits_to_byteLSBF(bits+32, 32); |
62 | uint32_t raw3 = bytebits_to_byteLSBF(bits+64, 32); |
63 | uint32_t raw4 = bytebits_to_byteLSBF(bits+96, 32); |
64 | |
65 | /* |
66 | fc 161: 1010 0001 -> LSB 1000 0101 |
67 | cn 33593 1000 0011 0011 1001 -> LSB 1001 1100 1100 0001 |
68 | cccc cccc cccc cccc ffffffff |
69 | 0 1001 1100 1100 0001 1000 0101 0000 0000 100001010000000001111011100000011010000010000000000000000000000000000000000000000000000000000000100111001100000110000101000 |
70 | 1001 1100 1100 0001 10000101 |
71 | */ |
72 | PrintAndLog("COTAG Found: FC %u, CN: %u Raw: %08X%08X%08X%08X", fc, cn, raw1 ,raw2, raw3, raw4); |
73 | return 1; |
74 | } |
75 | |
76 | // When reading a COTAG. |
77 | // 0 = HIGH/LOW signal - maxlength bigbuff |
78 | // 1 = translation for HI/LO into bytes with manchester 0,1 - length 300 |
79 | // 2 = raw signal - maxlength bigbuff |
80 | int CmdCOTAGRead(const char *Cmd) { |
81 | |
82 | if (Cmd[0] == 'h' || Cmd[0] == 'H') return usage_lf_cotag_read(); |
83 | |
84 | uint32_t rawsignal = 1; |
85 | sscanf(Cmd, "%u", &rawsignal); |
86 | |
87 | UsbCommand c = {CMD_COTAG, {rawsignal, 0, 0}}; |
88 | clearCommandBuffer(); |
89 | SendCommand(&c); |
90 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 7000) ) { |
91 | PrintAndLog("command execution time out"); |
92 | return -1; |
93 | } |
94 | |
95 | switch ( rawsignal ){ |
96 | case 0: |
97 | case 2: { |
98 | CmdPlot(""); |
99 | CmdGrid("384"); |
b9957414 |
100 | getSamples(0, true); break; |
e04475c4 |
101 | } |
102 | case 1: { |
e04475c4 |
103 | UsbCommand response; |
babca445 |
104 | DemodBufferLen = COTAG_BITS; |
105 | if (!GetFromBigBuf(DemodBuffer, COTAG_BITS, 0, &response, 1000, true)) { |
e04475c4 |
106 | PrintAndLog("timeout while waiting for reply."); |
107 | return -1; |
108 | } |
109 | return CmdCOTAGDemod(""); |
110 | } |
111 | } |
112 | return 0; |
113 | } |
114 | |
115 | static command_t CommandTable[] = { |
116 | {"help", CmdHelp, 1, "This help"}, |
117 | {"demod", CmdCOTAGDemod, 1, "Tries to decode a COTAG signal"}, |
118 | {"read", CmdCOTAGRead, 0, "Attempt to read and extract tag data"}, |
119 | {NULL, NULL, 0, NULL} |
120 | }; |
121 | |
122 | int CmdLFCOTAG(const char *Cmd) { |
123 | clearCommandBuffer(); |
124 | CmdsParse(CommandTable, Cmd); |
125 | return 0; |
126 | } |
127 | |
128 | int CmdHelp(const char *Cmd) { |
129 | CmdsHelp(CommandTable); |
130 | return 0; |
131 | } |