Merge pull request #2616 from jmichelp/fix14b
[RRG-proxmark3.git] / armsrc / dbprint.c
blob42d96fc3ec56456bdd72e3dc6bc317b36e203e07
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"
19 #include "string.h"
20 #include "cmd.h"
21 #include "printf.h"
23 #define DEBUG 1
24 #define DEBUG_MAX_MSG_SIZE 200
25 //=============================================================================
26 // Debug print functions, to go out over USB, to the usual PC-side client.
27 //=============================================================================
29 void DbpStringEx(uint32_t flags, const char *src, size_t srclen) {
30 #if DEBUG
31 struct {
32 uint16_t flag;
33 uint8_t buf[DEBUG_MAX_MSG_SIZE];
34 } PACKED data;
35 data.flag = flags;
36 uint16_t len = MIN(srclen, sizeof(data.buf));
37 memcpy(data.buf, src, len);
38 reply_ng(CMD_DEBUG_PRINT_STRING, PM3_SUCCESS, (uint8_t *)&data, sizeof(data.flag) + len);
39 #endif
42 void DbpString(const char *str) {
43 #if DEBUG
44 DbpStringEx(FLAG_LOG, str, strlen(str));
45 #endif
48 void DbprintfEx(uint32_t flags, const char *fmt, ...) {
49 #if DEBUG
50 // should probably limit size here; oh well, let's just use a big buffer
51 char s[DEBUG_MAX_MSG_SIZE] = {0x00};
52 va_list ap;
53 va_start(ap, fmt);
54 kvsprintf(fmt, s, 10, ap);
55 va_end(ap);
57 DbpStringEx(flags, s, strlen(s));
58 #endif
61 void Dbprintf(const char *fmt, ...) {
62 #if DEBUG
63 // should probably limit size here; oh well, let's just use a big buffer
64 char output_string[DEBUG_MAX_MSG_SIZE] = {0x00};
65 va_list ap;
67 va_start(ap, fmt);
68 kvsprintf(fmt, output_string, 10, ap);
69 va_end(ap);
71 DbpString(output_string);
72 #endif
75 // prints HEX & ASCII
76 void Dbhexdump(int len, const uint8_t *d, bool bAsci) {
77 #if DEBUG
78 while (len > 0) {
80 int l = (len > 16) ? 16 : len;
82 if (bAsci) {
83 char ascii[17];
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 Dbprintf("%-8s %*D", ascii, l, d, " ");
96 } else {
97 Dbprintf("%*D", l, d, " ");
100 len -= 16;
101 d += 16;
103 #endif
105 void print_result(const char *name, const uint8_t *d, size_t
107 n) {
109 const uint8_t *p = d;
110 uint16_t tmp = n & 0xFFF0;
112 for (; p - d < tmp; p += 16) {
113 Dbprintf("[%s: %02d/%02d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
114 name,
115 p - d,
117 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]
121 if (n % 16 != 0) {
122 char s[46] = {0};
123 char *sp = s;
124 for (; p - d < n; p++) {
125 sprintf(sp, "%02x ", p[0]);
126 sp += 3;
128 Dbprintf("[%s: %02d/%02d] %s", name, p - d, n, s);
132 // Prints message and hexdump
133 void print_dbg(const char *msg, const uint8_t *d, uint16_t n) {
134 if (g_dbglevel == DBG_DEBUG) {
135 print_result(msg, d, n);
139 /* useful when debugging new protocol implementations like FeliCa
140 void PrintToSendBuffer(void) {
141 DbpString("Printing ToSendBuffer:");
142 Dbhexdump(ToSendMax, ToSend, 0);