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