Skip to content

Commit

Permalink
Merge pull request RfidResearchGroup#2347 from jlitewski/plot_refactor
Browse files Browse the repository at this point in the history
Refactored Plot Annotations and some cleanup
  • Loading branch information
iceman1001 authored Apr 14, 2024
2 parents 3c8b131 + f2afbca commit 197baa4
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 115 deletions.
32 changes: 16 additions & 16 deletions client/src/cmddata.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ int ASKDemod_ext(int clk, int invert, int maxErr, size_t maxlen, bool amplify, b
return PM3_EMALLOC;
}

size_t bitlen = getFromGraphBuf(bits);
size_t bitlen = getFromGraphBuffer(bits);

PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) #samples from graphbuff: %zu", bitlen);

Expand Down Expand Up @@ -760,7 +760,7 @@ int ASKbiphaseDemod(int offset, int clk, int invert, int maxErr, bool verbose) {
return PM3_EMALLOC;
}

size_t size = getFromGraphBufEx(bs, MAX_DEMOD_BUF_LEN);
size_t size = getFromGraphBufferEx(bs, MAX_DEMOD_BUF_LEN);
if (size == 0) {
PrintAndLogEx(DEBUG, "DEBUG: no data in graphbuf");
free(bs);
Expand Down Expand Up @@ -1298,7 +1298,7 @@ int FSKrawDemod(uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, bo
return PM3_EMALLOC;
}

size_t bitlen = getFromGraphBuf(bits);
size_t bitlen = getFromGraphBuffer(bits);
if (bitlen == 0) {
PrintAndLogEx(DEBUG, "DEBUG: no data in graphbuf");
free(bits);
Expand Down Expand Up @@ -1400,7 +1400,7 @@ int PSKDemod(int clk, int invert, int maxErr, bool verbose) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t bitlen = getFromGraphBuf(bits);
size_t bitlen = getFromGraphBuffer(bits);
if (bitlen == 0) {
free(bits);
return PM3_ESOFT;
Expand Down Expand Up @@ -1451,7 +1451,7 @@ int NRZrawDemod(int clk, int invert, int maxErr, bool verbose) {
return PM3_EMALLOC;
}

size_t bitlen = getFromGraphBuf(bits);
size_t bitlen = getFromGraphBuffer(bits);

if (bitlen == 0) {
free(bits);
Expand Down Expand Up @@ -1821,10 +1821,10 @@ int CmdHpf(const char *Cmd) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
removeSignalOffset(bits, size);
// push it back to graph
setGraphBuf(bits, size);
setGraphBuffer(bits, size);
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);

Expand Down Expand Up @@ -1929,7 +1929,7 @@ int getSamplesFromBufEx(uint8_t *data, size_t sample_num, uint8_t bits_per_sampl
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
free(bits);
Expand Down Expand Up @@ -2039,10 +2039,10 @@ static int CmdLoad(const char *Cmd) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);

removeSignalOffset(bits, size);
setGraphBuf(bits, size);
setGraphBuffer(bits, size);
computeSignalProperties(bits, size);
free(bits);
}
Expand Down Expand Up @@ -2186,7 +2186,7 @@ int CmdNorm(const char *Cmd) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);

Expand Down Expand Up @@ -2337,7 +2337,7 @@ static int CmdDirectionalThreshold(const char *Cmd) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
// set signal properties low/high/mean/amplitude and is_noice detection
computeSignalProperties(bits, size);

Expand Down Expand Up @@ -2385,7 +2385,7 @@ static int CmdZerocrossings(const char *Cmd) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
RepaintGraphWindow();
Expand Down Expand Up @@ -2600,7 +2600,7 @@ static int CmdDataIIR(const char *Cmd) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
RepaintGraphWindow();
Expand Down Expand Up @@ -3336,7 +3336,7 @@ static int CmdCenterThreshold(const char *Cmd) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
// set signal properties low/high/mean/amplitude and is_noice detection
computeSignalProperties(bits, size);
RepaintGraphWindow();
Expand Down Expand Up @@ -3386,7 +3386,7 @@ static int CmdEnvelope(const char *Cmd) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
// set signal properties low/high/mean/amplitude and is_noice detection
computeSignalProperties(bits, size);
RepaintGraphWindow();
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmdlfawid.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int demodAWID(bool verbose) {
return PM3_EMALLOC;
}

size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Error - AWID not enough samples");
free(bits);
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmdlfem4x05.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static bool em4x05_download_samples(void) {
return false;
}

setGraphBuf(got, sizeof(got));
setGraphBuffer(got, sizeof(got));
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(got, sizeof(got));
RepaintGraphWindow();
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmdlfhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ int demodHID(bool verbose) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Error - " _RED_("HID not enough samples"));
free(bits);
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmdlfindala.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ static int CmdIndalaDemodAlt(const char *Cmd) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t datasize = getFromGraphBuf(data);
size_t datasize = getFromGraphBuffer(data);

uint8_t rawbits[4096] = {0};
int rawbit = 0;
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmdlfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int demodIOProx(bool verbose) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size < 65) {
PrintAndLogEx(DEBUG, "DEBUG: Error - IO prox not enough samples in GraphBuffer");
free(bits);
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmdlfparadox.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ int demodParadox(bool verbose, bool oldChksum) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Error - Paradox not enough samples");
free(bits);
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmdlfpyramid.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int demodPyramid(bool verbose) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Error - Pyramid not enough samples");
free(bits);
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmdlft55xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -2900,7 +2900,7 @@ static int CmdResetRead(const char *Cmd) {
free(got);
return PM3_ETIMEOUT;
}
setGraphBuf(got, gotsize);
setGraphBuffer(got, gotsize);
free(got);
}

Expand Down
95 changes: 64 additions & 31 deletions client/src/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
#include "cmddata.h" //for g_debugmode


int g_GraphBuffer[MAX_GRAPH_TRACE_LEN];
int g_OperationBuffer[MAX_GRAPH_TRACE_LEN];
size_t g_GraphTraceLen;
int32_t g_GraphBuffer[MAX_GRAPH_TRACE_LEN];
int32_t g_OperationBuffer[MAX_GRAPH_TRACE_LEN];
int32_t g_OverlayBuffer[MAX_GRAPH_TRACE_LEN];
bool g_useOverlays = false;
size_t g_GraphTraceLen;

/* write a manchester bit to the graph
*/
Expand Down Expand Up @@ -65,44 +67,49 @@ void AppendGraph(bool redraw, uint16_t clock, int bit) {
}
}

// clear out our graph window
// clear out our graph window and all the buffers associated with it
size_t ClearGraph(bool redraw) {
size_t gtl = g_GraphTraceLen;

memset(g_GraphBuffer, 0x00, g_GraphTraceLen);
memset(g_OperationBuffer, 0x00, g_GraphTraceLen);
memset(g_OverlayBuffer, 0x00, g_GraphTraceLen);

g_GraphTraceLen = 0;
g_GraphStart = 0;
g_GraphStop = 0;

g_DemodBufferLen = 0;
if (redraw)
g_useOverlays = false;

if (redraw) {
RepaintGraphWindow();
}

return gtl;
}

// option '1' to save g_GraphBuffer any other to restore
void save_restoreGB(uint8_t saveOpt) {
static int SavedGB[MAX_GRAPH_TRACE_LEN];
static size_t SavedGBlen = 0;
static bool GB_Saved = false;
static int Savedg_GridOffsetAdj = 0;
static int savedBuffer[MAX_GRAPH_TRACE_LEN];
static size_t savedBufferLen = 0;
static bool bufferSaved = false;
static int savedOffset = 0;

if (saveOpt == GRAPH_SAVE) { //save
memcpy(SavedGB, g_GraphBuffer, sizeof(g_GraphBuffer));
SavedGBlen = g_GraphTraceLen;
GB_Saved = true;
Savedg_GridOffsetAdj = g_GridOffset;
} else if (GB_Saved) { //restore
memcpy(g_GraphBuffer, SavedGB, sizeof(g_GraphBuffer));
memcpy(g_OperationBuffer, SavedGB, sizeof(g_OperationBuffer));
g_GraphTraceLen = SavedGBlen;
g_GridOffset = Savedg_GridOffsetAdj;
memcpy(savedBuffer, g_GraphBuffer, sizeof(g_GraphBuffer));
savedBufferLen = g_GraphTraceLen;
bufferSaved = true;
savedOffset = g_GridOffset;
} else if (bufferSaved) { //restore
memcpy(g_GraphBuffer, savedBuffer, sizeof(g_GraphBuffer));
memcpy(g_OperationBuffer, savedBuffer, sizeof(g_OperationBuffer));
g_GraphTraceLen = savedBufferLen;
g_GridOffset = savedOffset;
RepaintGraphWindow();
}
}

void setGraphBuf(const uint8_t *src, size_t size) {
void setGraphBuffer(const uint8_t *src, size_t size) {
if (src == NULL) return;

ClearGraph(false);
Expand All @@ -121,12 +128,12 @@ void setGraphBuf(const uint8_t *src, size_t size) {
}

// This function assumes that the length of dest array >= g_GraphTraceLen.
// If the length of dest array is less than g_GraphTraceLen, use getFromGraphBufEx(dest, maxLen) instead.
size_t getFromGraphBuf(uint8_t *dest) {
return getFromGraphBufEx(dest, g_GraphTraceLen);
// If the length of dest array is less than g_GraphTraceLen, use getFromGraphBufferEx(dest, maxLen) instead.
size_t getFromGraphBuffer(uint8_t *dest) {
return getFromGraphBufferEx(dest, g_GraphTraceLen);
}

size_t getFromGraphBufEx(uint8_t *dest, size_t maxLen) {
size_t getFromGraphBufferEx(uint8_t *dest, size_t maxLen) {
if (dest == NULL) return 0;
if (g_GraphTraceLen == 0) return 0;

Expand All @@ -141,12 +148,38 @@ size_t getFromGraphBufEx(uint8_t *dest, size_t maxLen) {
return i;
}

// A simple test to see if there is any data inside Graphbuffer.
//TODO: In progress function to get chunks of data from the GB w/o modifying the GB
//Currently seems like it doesn't work correctly?
size_t getGraphBufferChunk(uint8_t *dest, size_t start, size_t end) {
if (dest == NULL) return 0;
if (g_GraphTraceLen == 0) return 0;
if (start >= end) return 0;

size_t i, value;
end = (end < g_GraphTraceLen) ? end : g_GraphTraceLen;
for (i = 0; i < (end - start); i++) {
value = g_GraphBuffer[start + i];

//Trim the data to fit into an uint8_t
if (value > 127) {
value = 127;
} else if (value < -127) {
value = -127;
}

dest[i] = ((uint8_t)(value + 128));
}

return i;
}

// A simple test to see if there is any data inside the Graph Buffer.
bool HasGraphData(void) {
if (g_GraphTraceLen == 0) {
PrintAndLogEx(NORMAL, "No data available, try reading something first");
return false;
}

return true;
}

Expand Down Expand Up @@ -180,7 +213,7 @@ void convertGraphFromBitstreamEx(int hi, int low) {
return;
}

size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
free(bits);
Expand Down Expand Up @@ -210,7 +243,7 @@ int GetAskClock(const char *str, bool verbose) {
return -1;
}

size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
free(bits);
Expand Down Expand Up @@ -245,7 +278,7 @@ int GetPskCarrier(bool verbose) {
return -1;
}

size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
free(bits);
Expand Down Expand Up @@ -281,7 +314,7 @@ int GetPskClock(const char *str, bool verbose) {
return -1;
}

size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
free(bits);
Expand Down Expand Up @@ -319,7 +352,7 @@ int GetNrzClock(const char *str, bool verbose) {
return -1;
}

size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
free(bits);
Expand Down Expand Up @@ -375,7 +408,7 @@ bool fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, int *firstClockEdge) {
return false;
}

size_t size = getFromGraphBuf(bits);
size_t size = getFromGraphBuffer(bits);
if (size == 0) {
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
free(bits);
Expand Down
Loading

0 comments on commit 197baa4

Please sign in to comment.