]> git.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
ADD: midnitesnakes desfire, ultralight changes from Unstable branch.
[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>
c6be64da 12#include <stdbool.h>
7fe9b0b7 13#include <string.h>
14#include "ui.h"
15#include "graph.h"
6ff6ade2 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;
a61b4976 25 int half = (int)(clock/2);
26 int firstbit = bit ^ 1;
27
28 for (i = 0; i < half; ++i)
29 GraphBuffer[GraphTraceLen++] = firstbit;
7fe9b0b7 30
a61b4976 31 for (i = 0; i <= half; ++i)
7fe9b0b7 32 GraphBuffer[GraphTraceLen++] = bit;
33
34 if (redraw)
35 RepaintGraphWindow();
36}
37
38/* clear out our graph window */
39int ClearGraph(int redraw)
40{
41 int gtl = GraphTraceLen;
2ae8a312 42 memset(GraphBuffer, 0x00, GraphTraceLen);
7fe9b0b7 43
2ae8a312 44 GraphTraceLen = 0;
45
7fe9b0b7 46 if (redraw)
47 RepaintGraphWindow();
48
49 return gtl;
50}
51
52/*
53 * Detect clock rate
54 */
6ff6ade2 55 //decommissioned - has difficulty detecting rf/32
1b492a97 56/*
6ff6ade2 57int DetectClockOld(int peak)
7fe9b0b7 58{
59 int i;
60 int clock = 0xFFFF;
61 int lastpeak = 0;
62
1b492a97 63 // Detect peak if we don't have one
7fe9b0b7 64 if (!peak)
65 for (i = 0; i < GraphTraceLen; ++i)
66 if (GraphBuffer[i] > peak)
67 peak = GraphBuffer[i];
68
6ff6ade2 69 // peak=(int)(peak*.75);
7fe9b0b7 70 for (i = 1; i < GraphTraceLen; ++i)
71 {
1b492a97 72 // If this is the beginning of a peak
73 if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak)
7fe9b0b7 74 {
1b492a97 75 // Find lowest difference between peaks
7fe9b0b7 76 if (lastpeak && i - lastpeak < clock)
77 clock = i - lastpeak;
78 lastpeak = i;
79 }
80 }
a61b4976 81
a61b4976 82 return clock;
1b492a97 83}
84*/
6ff6ade2 85/*
86NOW IN LFDEMOD.C
a61b4976 87
1b492a97 88// by marshmellow
89// not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
90// maybe somehow adjust peak trimming value based on samples to fix?
6ff6ade2 91int DetectASKClock(int peak)
a61b4976 92 {
1b492a97 93 int i=0;
94 int low=0;
95 int clk[]={16,32,40,50,64,100,128,256};
6ff6ade2 96 int loopCnt = 256;
97 if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen;
1b492a97 98 if (!peak){
6ff6ade2 99 for (i=0;i<loopCnt;++i){
1b492a97 100 if(GraphBuffer[i]>peak){
101 peak = GraphBuffer[i];
102 }
103 if(GraphBuffer[i]<low){
104 low = GraphBuffer[i];
105 }
106 }
107 peak=(int)(peak*.75);
108 low= (int)(low*.75);
109 }
1b492a97 110 int ii;
1b492a97 111 int clkCnt;
112 int tol = 0;
113 int bestErr=1000;
114 int errCnt[]={0,0,0,0,0,0,0,0};
1b492a97 115 for(clkCnt=0; clkCnt<6;++clkCnt){
116 if (clk[clkCnt]==32){
117 tol=1;
118 }else{
119 tol=0;
120 }
121 bestErr=1000;
122 for (ii=0; ii<loopCnt; ++ii){
123 if ((GraphBuffer[ii]>=peak) || (GraphBuffer[ii]<=low)){
1b492a97 124 errCnt[clkCnt]=0;
125 for (i=0; i<((int)(GraphTraceLen/clk[clkCnt])-1); ++i){
126 if (GraphBuffer[ii+(i*clk[clkCnt])]>=peak || GraphBuffer[ii+(i*clk[clkCnt])]<=low){
1b492a97 127 }else if(GraphBuffer[ii+(i*clk[clkCnt])-tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])-tol]<=low){
128 }else if(GraphBuffer[ii+(i*clk[clkCnt])+tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])+tol]<=low){
129 }else{ //error no peak detected
1b492a97 130 errCnt[clkCnt]++;
1b492a97 131 }
132 }
133 if(errCnt[clkCnt]==0) return clk[clkCnt];
134 if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
135 }
136 }
1b492a97 137 }
138 int iii=0;
139 int best=0;
140 for (iii=0; iii<6;++iii){
141 if (errCnt[iii]<errCnt[best]){
142 best = iii;
143 }
144 }
6ff6ade2 145 // PrintAndLog("DEBUG: clkCnt: %d, ii: %d, i: %d peak: %d, low: %d, errcnt: %d, errCnt64: %d",clkCnt,ii,i,peak,low,errCnt[best],errCnt[4]);
1b492a97 146 return clk[best];
7fe9b0b7 147}
6ff6ade2 148*/
149void setGraphBuf(uint8_t *buff,int size)
150{
151 int i=0;
152 ClearGraph(0);
153 for (; i < size; ++i){
154 GraphBuffer[i]=buff[i];
155 }
156 GraphTraceLen=size;
157 RepaintGraphWindow();
158 return;
159}
160int getFromGraphBuf(uint8_t *buff)
161{
162 uint32_t i;
163 for (i=0;i<GraphTraceLen;++i){
164 if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
165 if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
166 buff[i]=(uint8_t)(GraphBuffer[i]+128);
167 }
168 return i;
169}
7fe9b0b7 170/* Get or auto-detect clock rate */
171int GetClock(const char *str, int peak, int verbose)
172{
173 int clock;
174
175 sscanf(str, "%i", &clock);
176 if (!strcmp(str, ""))
177 clock = 0;
178
179 /* Auto-detect clock */
180 if (!clock)
181 {
6ff6ade2 182 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
183 int size = getFromGraphBuf(grph);
184 clock = DetectASKClock(grph,size,0);
1b492a97 185 //clock2 = DetectClock2(peak);
7fe9b0b7 186 /* Only print this message if we're not looping something */
187 if (!verbose)
188 PrintAndLog("Auto-detected clock rate: %d", clock);
189 }
190
191 return clock;
192}
c6be64da 193
194
195/* A simple test to see if there is any data inside Graphbuffer.
196*/
197bool HasGraphData(){
198
199 if ( GraphTraceLen <= 0) {
200 PrintAndLog("No data available, try reading something first");
201 return false;
202 }
203 return true;
204}
Impressum, Datenschutz