]>
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
];
20 /* write a manchester bit to the graph */
21 void AppendGraph(int redraw
, int clock
, int bit
)
24 //set first half the clock bit (all 1's or 0's for a 0 or 1 bit)
25 for (i
= 0; i
< (int)(clock
/ 2); ++i
)
26 GraphBuffer
[GraphTraceLen
++] = bit
;
27 //set second half of the clock bit (all 0's or 1's for a 0 or 1 bit)
28 for (i
= (int)(clock
/ 2); i
< clock
; ++i
)
29 GraphBuffer
[GraphTraceLen
++] = bit
^ 1;
35 // clear out our graph window
36 int ClearGraph(int redraw
)
38 int gtl
= GraphTraceLen
;
39 memset(GraphBuffer
, 0x00, GraphTraceLen
);
48 // option '1' to save GraphBuffer any other to restore
49 void save_restoreGB(uint8_t saveOpt
)
51 static int SavedGB
[MAX_GRAPH_TRACE_LEN
];
52 static int SavedGBlen
;
53 static bool GB_Saved
= false;
55 if (saveOpt
==1) { //save
56 memcpy(SavedGB
,GraphBuffer
, sizeof(GraphBuffer
));
57 SavedGBlen
= GraphTraceLen
;
60 memcpy(GraphBuffer
,SavedGB
, sizeof(GraphBuffer
));
61 GraphTraceLen
= SavedGBlen
;
66 // DETECT CLOCK NOW IN LFDEMOD.C
68 void setGraphBuf(uint8_t *buff
, size_t size
)
70 if ( buff
== NULL
) return;
73 if ( size
> MAX_GRAPH_TRACE_LEN
)
74 size
= MAX_GRAPH_TRACE_LEN
;
76 for (; i
< size
; ++i
){
77 GraphBuffer
[i
]=buff
[i
]-128;
83 size_t getFromGraphBuf(uint8_t *buff
)
85 if (buff
== NULL
) return 0;
87 for (i
=0;i
<GraphTraceLen
;++i
){
88 if (GraphBuffer
[i
]>127) GraphBuffer
[i
]=127; //trim
89 if (GraphBuffer
[i
]<-127) GraphBuffer
[i
]=-127; //trim
90 buff
[i
]=(uint8_t)(GraphBuffer
[i
]+128);
95 // A simple test to see if there is any data inside Graphbuffer.
98 if ( GraphTraceLen
<= 0) {
99 PrintAndLog("No data available, try reading something first");
105 // Detect high and lows in Grapbuffer.
106 // Only loops the first 256 values.
107 void DetectHighLowInGraph(int *high
, int *low
, bool addFuzz
) {
109 uint8_t loopMax
= 255;
110 if ( loopMax
> GraphTraceLen
)
111 loopMax
= GraphTraceLen
;
113 for (uint8_t i
= 0; i
< loopMax
; ++i
) {
114 if (GraphBuffer
[i
] > *high
)
115 *high
= GraphBuffer
[i
];
116 else if (GraphBuffer
[i
] < *low
)
117 *low
= GraphBuffer
[i
];
120 //12% fuzz in case highs and lows aren't clipped
122 *high
= (int)(*high
* .88);
123 *low
= (int)(*low
* .88);
127 // Get or auto-detect ask clock rate
128 int GetAskClock(const char str
[], bool printAns
, bool verbose
)
131 sscanf(str
, "%i", &clock
);
132 if (!strcmp(str
, ""))
138 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
139 size_t size
= getFromGraphBuf(grph
);
142 PrintAndLog("Failed to copy from graphbuffer");
145 DetectASKClock(grph
, size
, &clock
, 20);
146 // Only print this message if we're not looping something
148 PrintAndLog("Auto-detected clock rate: %d", clock
);
153 uint8_t GetPskCarrier(const char str
[], bool printAns
, bool verbose
)
156 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
157 size_t size
= getFromGraphBuf(grph
);
160 PrintAndLog("Failed to copy from graphbuffer");
163 //uint8_t countPSK_FC(uint8_t *BitStream, size_t size)
165 carrier
= countFC(grph
,size
,0);
166 // Only print this message if we're not looping something
168 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier
);
173 int GetPskClock(const char str
[], bool printAns
, bool verbose
)
176 sscanf(str
, "%i", &clock
);
177 if (!strcmp(str
, ""))
183 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
184 size_t size
= getFromGraphBuf(grph
);
187 PrintAndLog("Failed to copy from graphbuffer");
190 clock
= DetectPSKClock(grph
,size
,0);
191 // Only print this message if we're not looping something
193 PrintAndLog("Auto-detected clock rate: %d", clock
);
198 uint8_t GetNrzClock(const char str
[], bool printAns
, bool verbose
)
201 sscanf(str
, "%i", &clock
);
202 if (!strcmp(str
, ""))
208 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
209 size_t size
= getFromGraphBuf(grph
);
212 PrintAndLog("Failed to copy from graphbuffer");
215 clock
= DetectNRZClock(grph
, size
, 0);
216 // Only print this message if we're not looping something
218 PrintAndLog("Auto-detected clock rate: %d", clock
);
223 //attempt to detect the field clock and bit clock for FSK
224 uint8_t GetFskClock(const char str
[], bool printAns
, bool verbose
)
227 sscanf(str
, "%i", &clock
);
228 if (!strcmp(str
, ""))
230 if (clock
!= 0) return (uint8_t)clock
;
233 uint8_t fc1
=0, fc2
=0, rf1
=0;
234 uint8_t ans
= fskClocks(&fc1
, &fc2
, &rf1
, verbose
);
235 if (ans
== 0) return 0;
236 if ((fc1
==10 && fc2
==8) || (fc1
==8 && fc2
==5)){
237 if (printAns
) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
241 PrintAndLog("DEBUG: unknown fsk field clock detected");
242 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
246 uint8_t fskClocks(uint8_t *fc1
, uint8_t *fc2
, uint8_t *rf1
, bool verbose
)
248 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
249 size_t size
= getFromGraphBuf(BitStream
);
250 if (size
==0) return 0;
251 uint16_t ans
= countFC(BitStream
, size
, 1);
253 if (verbose
) PrintAndLog("DEBUG: No data found");
256 *fc1
= (ans
>> 8) & 0xFF;
259 *rf1
= detectFSKClk(BitStream
, size
, *fc1
, *fc2
);
261 if (verbose
) PrintAndLog("DEBUG: Clock detect error");