]> git.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
Merge pull request #57 from iceman1001/feature
[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{
e629181f 69 if ( buff == NULL ) return 0;
a1557c4c 70
d5a72d2f 71 uint32_t i;
f822a063 72 for (i=0;i<GraphTraceLen;++i){
73 if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
74 if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
d5a72d2f 75 buff[i]=(uint8_t)(GraphBuffer[i]+128);
f822a063 76 }
d5a72d2f 77 return i;
78}
ec75f5c1 79
80
c12512e9 81// Get or auto-detect clock rate
7fe9b0b7 82int GetClock(const char *str, int peak, int verbose)
83{
c54d1394
MHS
84 int clock;
85 sscanf(str, "%i", &clock);
86 if (!strcmp(str, ""))
87 clock = 0;
7fe9b0b7 88
c12512e9 89 // Auto-detect clock
c54d1394
MHS
90 if (!clock)
91 {
92 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
ba1a299c 93 size_t size = getFromGraphBuf(grph);
e629181f 94 if ( size == 0 ) {
a1557c4c 95 PrintAndLog("Failed to copy from graphbuffer");
96 return -1;
97 }
c54d1394 98 clock = DetectASKClock(grph,size,0);
c12512e9 99 // Only print this message if we're not looping something
c54d1394
MHS
100 if (!verbose){
101 PrintAndLog("Auto-detected clock rate: %d", clock);
102 }
103 }
104 return clock;
7fe9b0b7 105}
ba1a299c 106
a1557c4c 107// A simple test to see if there is any data inside Graphbuffer.
108bool HasGraphData(){
7fe9b0b7 109
a1557c4c 110 if ( GraphTraceLen <= 0) {
111 PrintAndLog("No data available, try reading something first");
112 return false;
113 }
114 return true;
115}
116
117// Detect high and lows in Grapbuffer.
118// Only loops the first 256 values.
119void DetectHighLowInGraph(int *high, int *low, bool addFuzz) {
120
121 uint8_t loopMax = 255;
122 if ( loopMax > GraphTraceLen)
123 loopMax = GraphTraceLen;
124
125 for (uint8_t i = 0; i < loopMax; ++i) {
126 if (GraphBuffer[i] > *high)
127 *high = GraphBuffer[i];
128 else if (GraphBuffer[i] < *low)
129 *low = GraphBuffer[i];
130 }
131
132 //12% fuzz in case highs and lows aren't clipped
133 if (addFuzz) {
134 *high = (int)(*high * .88);
135 *low = (int)(*low * .88);
136 }
7fe9b0b7 137}
ba1a299c 138
4118b74d 139int GetNRZpskClock(const char *str, int peak, int verbose)
140{
ba1a299c 141 int clock;
ba1a299c 142 sscanf(str, "%i", &clock);
143 if (!strcmp(str, ""))
144 clock = 0;
145
146 // Auto-detect clock
147 if (!clock)
148 {
149 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
d6d20c54 150 size_t size = getFromGraphBuf(grph);
e629181f
MHS
151 if ( size == 0 ) {
152 PrintAndLog("Failed to copy from graphbuffer");
153 return -1;
154 }
d6d20c54 155 clock = DetectpskNRZClock(grph,size,0);
ba1a299c 156 // Only print this message if we're not looping something
157 if (!verbose){
158 PrintAndLog("Auto-detected clock rate: %d", clock);
ba1a299c 159 }
160 }
161 return clock;
4118b74d 162}
Impressum, Datenschutz