]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
995a32da6a451cba3e57eb34ec8503f98c15c213
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 //-----------------------------------------------------------------------------
17 #include "cmddata.h" //for g_debugmode
19 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
);
49 // option '1' to save GraphBuffer any other to restore
50 void save_restoreGB(uint8_t saveOpt
)
52 static int SavedGB
[MAX_GRAPH_TRACE_LEN
];
53 static int SavedGBlen
;
54 static bool GB_Saved
= false;
56 if (saveOpt
==1) { //save
57 memcpy(SavedGB
, GraphBuffer
, sizeof(GraphBuffer
));
58 SavedGBlen
= GraphTraceLen
;
60 } else if (GB_Saved
){ //restore
61 memcpy(GraphBuffer
, SavedGB
, sizeof(GraphBuffer
));
62 GraphTraceLen
= SavedGBlen
;
68 // DETECT CLOCK NOW IN LFDEMOD.C
70 void setGraphBuf(uint8_t *buff
, size_t size
)
72 if ( buff
== NULL
) return;
75 if ( size
> MAX_GRAPH_TRACE_LEN
)
76 size
= MAX_GRAPH_TRACE_LEN
;
78 for (; i
< size
; ++i
){
79 GraphBuffer
[i
]=buff
[i
]-128;
85 size_t getFromGraphBuf(uint8_t *buff
)
87 if (buff
== NULL
) return 0;
89 for (i
=0;i
<GraphTraceLen
;++i
){
90 if (GraphBuffer
[i
]>127) GraphBuffer
[i
]=127; //trim
91 if (GraphBuffer
[i
]<-127) GraphBuffer
[i
]=-127; //trim
92 buff
[i
]=(uint8_t)(GraphBuffer
[i
]+128);
97 // A simple test to see if there is any data inside Graphbuffer.
100 if ( GraphTraceLen
<= 0) {
101 PrintAndLog("No data available, try reading something first");
107 // Detect high and lows in Grapbuffer.
108 // Only loops the first 256 values.
109 void DetectHighLowInGraph(int *high
, int *low
, bool addFuzz
) {
111 uint8_t loopMax
= 255;
112 if ( loopMax
> GraphTraceLen
)
113 loopMax
= GraphTraceLen
;
115 for (uint8_t i
= 0; i
< loopMax
; ++i
) {
116 if (GraphBuffer
[i
] > *high
)
117 *high
= GraphBuffer
[i
];
118 else if (GraphBuffer
[i
] < *low
)
119 *low
= GraphBuffer
[i
];
122 //12% fuzz in case highs and lows aren't clipped
124 *high
= (int)(*high
* .88);
125 *low
= (int)(*low
* .88);
129 // Get or auto-detect ask clock rate
130 int GetAskClock(const char str
[], bool printAns
, bool verbose
)
133 sscanf(str
, "%i", &clock
);
134 if (!strcmp(str
, ""))
140 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
141 size_t size
= getFromGraphBuf(grph
);
144 PrintAndLog("Failed to copy from graphbuffer");
147 bool st
= DetectST(grph
, &size
, &clock
);
150 start
= DetectASKClock(grph
, size
, &clock
, 20);
152 // Only print this message if we're not looping something
154 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock
, start
);
159 uint8_t GetPskCarrier(const char str
[], bool printAns
, bool verbose
)
162 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
163 size_t size
= getFromGraphBuf(grph
);
166 PrintAndLog("Failed to copy from graphbuffer");
169 //uint8_t countPSK_FC(uint8_t *BitStream, size_t size)
171 carrier
= countFC(grph
,size
,0);
172 // Only print this message if we're not looping something
174 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier
);
179 int GetPskClock(const char str
[], bool printAns
, bool verbose
)
182 sscanf(str
, "%i", &clock
);
183 if (!strcmp(str
, ""))
189 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
190 size_t size
= getFromGraphBuf(grph
);
193 PrintAndLog("Failed to copy from graphbuffer");
196 clock
= DetectPSKClock(grph
,size
,0);
197 // Only print this message if we're not looping something
199 PrintAndLog("Auto-detected clock rate: %d", clock
);
204 uint8_t GetNrzClock(const char str
[], bool printAns
, bool verbose
)
207 sscanf(str
, "%i", &clock
);
208 if (!strcmp(str
, ""))
214 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
215 size_t size
= getFromGraphBuf(grph
);
218 PrintAndLog("Failed to copy from graphbuffer");
221 size_t clkStartIdx
= 0;
222 clock
= DetectNRZClock(grph
, size
, 0, &clkStartIdx
);
223 // Only print this message if we're not looping something
225 PrintAndLog("Auto-detected clock rate: %d", clock
);
230 //attempt to detect the field clock and bit clock for FSK
231 uint8_t GetFskClock(const char str
[], bool printAns
, bool verbose
)
234 sscanf(str
, "%i", &clock
);
235 if (!strcmp(str
, ""))
237 if (clock
!= 0) return (uint8_t)clock
;
240 uint8_t fc1
=0, fc2
=0, rf1
=0;
241 uint8_t ans
= fskClocks(&fc1
, &fc2
, &rf1
, verbose
);
242 if (ans
== 0) return 0;
243 if ((fc1
==10 && fc2
==8) || (fc1
==8 && fc2
==5)){
244 if (printAns
) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
248 PrintAndLog("DEBUG: unknown fsk field clock detected");
249 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
253 uint8_t fskClocks(uint8_t *fc1
, uint8_t *fc2
, uint8_t *rf1
, bool verbose
)
255 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
256 size_t size
= getFromGraphBuf(BitStream
);
257 if (size
==0) return 0;
258 uint16_t ans
= countFC(BitStream
, size
, 1);
260 if (verbose
|| g_debugMode
) PrintAndLog("DEBUG: No data found");
263 *fc1
= (ans
>> 8) & 0xFF;
266 *rf1
= detectFSKClk(BitStream
, size
, *fc1
, *fc2
);
268 if (verbose
|| g_debugMode
) PrintAndLog("DEBUG: Clock detect error");
273 bool graphJustNoise(int *BitStream
, int size
)
275 static const uint8_t THRESHOLD
= 15; //might not be high enough for noisy environments
276 //test samples are not just noise
278 for(int idx
=0; idx
< size
&& justNoise1
;idx
++){
279 justNoise1
= BitStream
[idx
] < THRESHOLD
;