]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
18 int GraphBuffer
[MAX_GRAPH_TRACE_LEN
];
21 /* write a manchester bit to the graph */
22 void AppendGraph(int redraw
, int clock
, int bit
)
25 //set first half the clock bit (all 1's or 0's for a 0 or 1 bit)
26 for (i
= 0; i
< (int)(clock
/ 2); ++i
)
27 GraphBuffer
[GraphTraceLen
++] = bit
;
28 //set second half of the clock bit (all 0's or 1's for a 0 or 1 bit)
29 for (i
= (int)(clock
/ 2); i
< clock
; ++i
)
30 GraphBuffer
[GraphTraceLen
++] = bit
^ 1;
36 // clear out our graph window
37 int ClearGraph(int redraw
)
39 int gtl
= GraphTraceLen
;
40 memset(GraphBuffer
, 0x00, GraphTraceLen
);
50 // DETECT CLOCK NOW IN LFDEMOD.C
52 void setGraphBuf(uint8_t *buff
, size_t size
)
54 if ( buff
== NULL
) return;
57 if ( size
> MAX_GRAPH_TRACE_LEN
)
58 size
= MAX_GRAPH_TRACE_LEN
;
60 for (; i
< size
; ++i
){
61 GraphBuffer
[i
]=buff
[i
]-128;
67 size_t getFromGraphBuf(uint8_t *buff
)
69 if (buff
== NULL
) return 0;
71 for (i
=0;i
<GraphTraceLen
;++i
){
72 if (GraphBuffer
[i
]>127) GraphBuffer
[i
]=127; //trim
73 if (GraphBuffer
[i
]<-127) GraphBuffer
[i
]=-127; //trim
74 buff
[i
]=(uint8_t)(GraphBuffer
[i
]+128);
79 // A simple test to see if there is any data inside Graphbuffer.
82 if ( GraphTraceLen
<= 0) {
83 PrintAndLog("No data available, try reading something first");
89 // Detect high and lows in Grapbuffer.
90 // Only loops the first 256 values.
91 void DetectHighLowInGraph(int *high
, int *low
, bool addFuzz
) {
93 uint8_t loopMax
= 255;
94 if ( loopMax
> GraphTraceLen
)
95 loopMax
= GraphTraceLen
;
97 for (uint8_t i
= 0; i
< loopMax
; ++i
) {
98 if (GraphBuffer
[i
] > *high
)
99 *high
= GraphBuffer
[i
];
100 else if (GraphBuffer
[i
] < *low
)
101 *low
= GraphBuffer
[i
];
104 //12% fuzz in case highs and lows aren't clipped
106 *high
= (int)(*high
* .88);
107 *low
= (int)(*low
* .88);
111 // Get or auto-detect ask clock rate
112 int GetAskClock(const char str
[], bool printAns
, bool verbose
)
115 sscanf(str
, "%i", &clock
);
116 if (!strcmp(str
, ""))
122 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
123 size_t size
= getFromGraphBuf(grph
);
126 PrintAndLog("Failed to copy from graphbuffer");
129 DetectASKClock(grph
, size
, &clock
, 20);
130 // Only print this message if we're not looping something
132 PrintAndLog("Auto-detected clock rate: %d", clock
);
137 uint8_t GetPskCarrier(const char str
[], bool printAns
, bool verbose
)
140 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
141 size_t size
= getFromGraphBuf(grph
);
144 PrintAndLog("Failed to copy from graphbuffer");
147 //uint8_t countPSK_FC(uint8_t *BitStream, size_t size)
149 carrier
= countPSK_FC(grph
,size
);
150 // Only print this message if we're not looping something
152 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier
);
157 int GetPskClock(const char str
[], bool printAns
, bool verbose
)
160 sscanf(str
, "%i", &clock
);
161 if (!strcmp(str
, ""))
167 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
168 size_t size
= getFromGraphBuf(grph
);
171 PrintAndLog("Failed to copy from graphbuffer");
174 clock
= DetectPSKClock(grph
,size
,0);
175 // Only print this message if we're not looping something
177 PrintAndLog("Auto-detected clock rate: %d", clock
);
182 uint8_t GetNrzClock(const char str
[], bool printAns
, bool verbose
)
185 sscanf(str
, "%i", &clock
);
186 if (!strcmp(str
, ""))
192 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
193 size_t size
= getFromGraphBuf(grph
);
196 PrintAndLog("Failed to copy from graphbuffer");
199 clock
= DetectNRZClock(grph
, size
, 0);
200 // Only print this message if we're not looping something
202 PrintAndLog("Auto-detected clock rate: %d", clock
);
207 //attempt to detect the field clock and bit clock for FSK
208 uint8_t GetFskClock(const char str
[], bool printAns
, bool verbose
)
211 sscanf(str
, "%i", &clock
);
212 if (!strcmp(str
, ""))
214 if (clock
!= 0) return (uint8_t)clock
;
217 uint8_t fc1
=0, fc2
=0, rf1
=0;
218 uint8_t ans
= fskClocks(&fc1
, &fc2
, &rf1
, verbose
);
219 if (ans
== 0) return 0;
220 if ((fc1
==10 && fc2
==8) || (fc1
==8 && fc2
==5)){
221 if (printAns
) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
225 PrintAndLog("DEBUG: unknown fsk field clock detected");
226 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
230 uint8_t fskClocks(uint8_t *fc1
, uint8_t *fc2
, uint8_t *rf1
, bool verbose
)
232 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
233 size_t size
= getFromGraphBuf(BitStream
);
234 if (size
==0) return 0;
236 uint16_t ans
= countFC(BitStream
, size
, &dummy
);
238 if (verbose
) PrintAndLog("DEBUG: No data found");
241 *fc1
= (ans
>> 8) & 0xFF;
244 *rf1
= detectFSKClk(BitStream
, size
, *fc1
, *fc2
);
246 if (verbose
) PrintAndLog("DEBUG: Clock detect error");