Release v4.15864 - Radium
[RRG-proxmark3.git] / armsrc / dbprint.c
blob7be7bba4c7ee2da2e79a73927306c039a831c1c7
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Jonathan Westhues, Mar 2006
3 // Copyright (C) Gerhard de Koning Gans, Sep 2007
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // See LICENSE.txt for the text of the license.
16 //-----------------------------------------------------------------------------
18 #include "dbprint.h"
20 #include "string.h"
21 #include "cmd.h"
22 #include "printf.h"
24 #define DEBUG 1
26 //=============================================================================
27 // Debug print functions, to go out over USB, to the usual PC-side client.
28 //=============================================================================
30 void DbpStringEx(uint32_t flags, const char *src, size_t srclen) {
31 #if DEBUG
32 struct {
33 uint16_t flag;
34 uint8_t buf[PM3_CMD_DATA_SIZE - sizeof(uint16_t)];
35 } PACKED data;
36 data.flag = flags;
37 uint16_t len = MIN(srclen, sizeof(data.buf));
38 memcpy(data.buf, src, len);
39 reply_ng(CMD_DEBUG_PRINT_STRING, PM3_SUCCESS, (uint8_t *)&data, sizeof(data.flag) + len);
40 #endif
43 void DbpString(const char *str) {
44 #if DEBUG
45 DbpStringEx(FLAG_LOG, str, strlen(str));
46 #endif
49 void DbprintfEx(uint32_t flags, const char *fmt, ...) {
50 #if DEBUG
51 // should probably limit size here; oh well, let's just use a big buffer
52 char s[PM3_CMD_DATA_SIZE] = {0x00};
53 va_list ap;
54 va_start(ap, fmt);
55 kvsprintf(fmt, s, 10, ap);
56 va_end(ap);
58 DbpStringEx(flags, s, strlen(s));
59 #endif
62 void Dbprintf(const char *fmt, ...) {
63 #if DEBUG
64 // should probably limit size here; oh well, let's just use a big buffer
65 char output_string[PM3_CMD_DATA_SIZE] = {0x00};
66 va_list ap;
68 va_start(ap, fmt);
69 kvsprintf(fmt, output_string, 10, ap);
70 va_end(ap);
72 DbpString(output_string);
73 #endif
76 // prints HEX & ASCII
77 void Dbhexdump(int len, uint8_t *d, bool bAsci) {
78 #if DEBUG
79 char ascii[9];
81 while (len > 0) {
83 int l = (len > 8) ? 8 : len;
85 memcpy(ascii, d, l);
86 ascii[l] = 0;
88 // filter safe ascii
89 for (int i = 0; i < l; i++) {
90 if (ascii[i] < 32 || ascii[i] > 126) {
91 ascii[i] = '.';
95 if (bAsci)
96 Dbprintf("%-8s %*D", ascii, l, d, " ");
97 else
98 Dbprintf("%*D", l, d, " ");
100 len -= 8;
101 d += 8;
103 #endif
106 void print_result(const char *name, uint8_t *buf, size_t len) {
108 uint8_t *p = buf;
109 uint16_t tmp = len & 0xFFF0;
111 for (; p - buf < tmp; p += 16) {
112 Dbprintf("[%s: %02d/%02d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
113 name,
114 p - buf,
115 len,
116 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]
119 if (len % 16 != 0) {
120 char s[46] = {0};
121 char *sp = s;
122 for (; p - buf < len; p++) {
123 sprintf(sp, "%02x ", p[0]);
124 sp += 3;
126 Dbprintf("[%s: %02d/%02d] %s", name, p - buf, len, s);
130 /* useful when debugging new protocol implementations like FeliCa
131 void PrintToSendBuffer(void) {
132 DbpString("Printing ToSendBuffer:");
133 Dbhexdump(ToSendMax, ToSend, 0);