]> git.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
Merge remote-tracking branch 'upstream/master'
[proxmark3-svn] / client / graph.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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// Graph utilities
9//-----------------------------------------------------------------------------
10
7fe9b0b7 11#include <stdio.h>
a1557c4c 12#include <stdbool.h>
7fe9b0b7 13#include <string.h>
14#include "ui.h"
15#include "graph.h"
d5a72d2f 16#include "lfdemod.h"
7fe9b0b7 17
18int GraphBuffer[MAX_GRAPH_TRACE_LEN];
19int GraphTraceLen;
20
21/* write a bit to the graph */
22void AppendGraph(int redraw, int clock, int bit)
23{
24 int i;
25
26 for (i = 0; i < (int)(clock / 2); ++i)
27 GraphBuffer[GraphTraceLen++] = bit ^ 1;
7fe9b0b7 28
7fe9b0b7 29 for (i = (int)(clock / 2); i < clock; ++i)
30 GraphBuffer[GraphTraceLen++] = bit;
31
32 if (redraw)
33 RepaintGraphWindow();
34}
35
c12512e9 36// clear out our graph window
7fe9b0b7 37int ClearGraph(int redraw)
38{
39 int gtl = GraphTraceLen;
52ab55ab 40 memset(GraphBuffer, 0x00, GraphTraceLen);
41
7fe9b0b7 42 GraphTraceLen = 0;
43
44 if (redraw)
45 RepaintGraphWindow();
46
47 return gtl;
48}
49
c12512e9 50// DETECT CLOCK NOW IN LFDEMOD.C
7fe9b0b7 51
ba1a299c 52void setGraphBuf(uint8_t *buff, size_t size)
d5a72d2f 53{
a1557c4c 54 if ( buff == NULL ) return;
55
56 uint16_t i = 0;
57 if ( size > MAX_GRAPH_TRACE_LEN )
58 size = MAX_GRAPH_TRACE_LEN;
d5a72d2f 59 ClearGraph(0);
60 for (; i < size; ++i){
ba1a299c 61 GraphBuffer[i]=buff[i]-128;
d5a72d2f 62 }
63 GraphTraceLen=size;
64 RepaintGraphWindow();
65 return;
66}
ba1a299c 67size_t getFromGraphBuf(uint8_t *buff)
d5a72d2f 68{
e770c648 69 if (buff == NULL ) return 0;
d5a72d2f 70 uint32_t i;
f822a063 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
d5a72d2f 74 buff[i]=(uint8_t)(GraphBuffer[i]+128);
f822a063 75 }
d5a72d2f 76 return i;
77}
ec75f5c1 78
79
c12512e9 80// Get or auto-detect clock rate
7fe9b0b7 81int GetClock(const char *str, int peak, int verbose)
82{
c54d1394
MHS
83 int clock;
84 sscanf(str, "%i", &clock);
85 if (!strcmp(str, ""))
86 clock = 0;
7fe9b0b7 87
c12512e9 88 // Auto-detect clock
c54d1394
MHS
89 if (!clock)
90 {
91 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
ba1a299c 92 size_t size = getFromGraphBuf(grph);
e629181f 93 if ( size == 0 ) {
a1557c4c 94 PrintAndLog("Failed to copy from graphbuffer");
95 return -1;
96 }
6de43508 97 DetectASKClock(grph,size,&clock,20);
c12512e9 98 // Only print this message if we're not looping something
c54d1394
MHS
99 if (!verbose){
100 PrintAndLog("Auto-detected clock rate: %d", clock);
101 }
102 }
103 return clock;
7fe9b0b7 104}
ba1a299c 105
a1557c4c 106// A simple test to see if there is any data inside Graphbuffer.
107bool HasGraphData(){
7fe9b0b7 108
a1557c4c 109 if ( GraphTraceLen <= 0) {
110 PrintAndLog("No data available, try reading something first");
111 return false;
112 }
113 return true;
114}
115
116// Detect high and lows in Grapbuffer.
117// Only loops the first 256 values.
118void DetectHighLowInGraph(int *high, int *low, bool addFuzz) {
119
120 uint8_t loopMax = 255;
121 if ( loopMax > GraphTraceLen)
122 loopMax = GraphTraceLen;
123
124 for (uint8_t i = 0; i < loopMax; ++i) {
125 if (GraphBuffer[i] > *high)
126 *high = GraphBuffer[i];
127 else if (GraphBuffer[i] < *low)
128 *low = GraphBuffer[i];
129 }
130
131 //12% fuzz in case highs and lows aren't clipped
132 if (addFuzz) {
133 *high = (int)(*high * .88);
134 *low = (int)(*low * .88);
135 }
7fe9b0b7 136}
ba1a299c 137
e770c648 138int GetPskClock(const char *str, int peak, int verbose)
139{
140 int clock;
141 sscanf(str, "%i", &clock);
142 if (!strcmp(str, ""))
143 clock = 0;
144
145 // Auto-detect clock
146 if (!clock)
147 {
148 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
149 size_t size = getFromGraphBuf(grph);
150 if ( size == 0 ) {
151 PrintAndLog("Failed to copy from graphbuffer");
152 return -1;
153 }
154 clock = DetectPSKClock(grph,size,0);
155 // Only print this message if we're not looping something
156 if (!verbose){
157 PrintAndLog("Auto-detected clock rate: %d", clock);
158 }
159 }
160 return clock;
161}
162
163int GetNrzClock(const char *str, int peak, int verbose)
4118b74d 164{
ba1a299c 165 int clock;
ba1a299c 166 sscanf(str, "%i", &clock);
167 if (!strcmp(str, ""))
168 clock = 0;
169
170 // Auto-detect clock
171 if (!clock)
172 {
173 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
d6d20c54 174 size_t size = getFromGraphBuf(grph);
e629181f
MHS
175 if ( size == 0 ) {
176 PrintAndLog("Failed to copy from graphbuffer");
177 return -1;
178 }
e770c648 179 clock = DetectNRZClock(grph,size,0);
ba1a299c 180 // Only print this message if we're not looping something
181 if (!verbose){
182 PrintAndLog("Auto-detected clock rate: %d", clock);
ba1a299c 183 }
184 }
185 return clock;
4118b74d 186}
Impressum, Datenschutz