Merge pull request #1331 from Guilhem7/master
[RRG-proxmark3.git] / armsrc / dbprint.c
blobbfeb0ded2b4471551f34797a1f5db21873733c06
1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, Mar 2006
3 // Edits by Gerhard de Koning Gans, Sep 2007 (##)
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // The main application code. This is the first thing called after start.c
10 // executes.
11 //-----------------------------------------------------------------------------
13 #include "dbprint.h"
15 #include "string.h"
16 #include "cmd.h"
17 #include "printf.h"
19 #define DEBUG 1
21 //=============================================================================
22 // Debug print functions, to go out over USB, to the usual PC-side client.
23 //=============================================================================
25 void DbpStringEx(uint32_t flags, const char *src, size_t srclen) {
26 #if DEBUG
27 struct {
28 uint16_t flag;
29 uint8_t buf[PM3_CMD_DATA_SIZE - sizeof(uint16_t)];
30 } PACKED data;
31 data.flag = flags;
32 uint16_t len = MIN(srclen, sizeof(data.buf));
33 memcpy(data.buf, src, len);
34 reply_ng(CMD_DEBUG_PRINT_STRING, PM3_SUCCESS, (uint8_t *)&data, sizeof(data.flag) + len);
35 #endif
38 void DbpString(const char *str) {
39 #if DEBUG
40 DbpStringEx(FLAG_LOG, str, strlen(str));
41 #endif
44 void DbprintfEx(uint32_t flags, const char *fmt, ...) {
45 #if DEBUG
46 // should probably limit size here; oh well, let's just use a big buffer
47 char s[PM3_CMD_DATA_SIZE] = {0x00};
48 va_list ap;
49 va_start(ap, fmt);
50 kvsprintf(fmt, s, 10, ap);
51 va_end(ap);
53 DbpStringEx(flags, s, strlen(s));
54 #endif
57 void Dbprintf(const char *fmt, ...) {
58 #if DEBUG
59 // should probably limit size here; oh well, let's just use a big buffer
60 char output_string[PM3_CMD_DATA_SIZE] = {0x00};
61 va_list ap;
63 va_start(ap, fmt);
64 kvsprintf(fmt, output_string, 10, ap);
65 va_end(ap);
67 DbpString(output_string);
68 #endif
71 // prints HEX & ASCII
72 void Dbhexdump(int len, uint8_t *d, bool bAsci) {
73 #if DEBUG
74 char ascii[9];
76 while (len > 0) {
78 int l = (len > 8) ? 8 : len;
80 memcpy(ascii, d, l);
81 ascii[l] = 0;
83 // filter safe ascii
84 for (int i = 0; i < l; i++) {
85 if (ascii[i] < 32 || ascii[i] > 126) {
86 ascii[i] = '.';
90 if (bAsci)
91 Dbprintf("%-8s %*D", ascii, l, d, " ");
92 else
93 Dbprintf("%*D", l, d, " ");
95 len -= 8;
96 d += 8;
98 #endif
101 void print_result(const char *name, uint8_t *buf, size_t len) {
103 uint8_t *p = buf;
104 uint16_t tmp = len & 0xFFF0;
106 for (; p - buf < tmp; p += 16) {
107 Dbprintf("[%s: %02d/%02d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
108 name,
109 p - buf,
110 len,
111 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]
114 if (len % 16 != 0) {
115 char s[46] = {0};
116 char *sp = s;
117 for (; p - buf < len; p++) {
118 sprintf(sp, "%02x ", p[0]);
119 sp += 3;
121 Dbprintf("[%s: %02d/%02d] %s", name, p - buf, len, s);
125 /* useful when debugging new protocol implementations like FeliCa
126 void PrintToSendBuffer(void) {
127 DbpString("Printing ToSendBuffer:");
128 Dbhexdump(ToSendMax, ToSend, 0);