1 //-----------------------------------------------------------------------------
2 // Copyright (C) Jonathan Westhues, Mar 2006
3 // Copyright (C) Gerhard de Koning Gans, Sep 2007
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.
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 //-----------------------------------------------------------------------------
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
) {
33 uint8_t buf
[DEBUG_MAX_MSG_SIZE
];
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
);
42 void DbpString(const char *str
) {
44 DbpStringEx(FLAG_LOG
, str
, strlen(str
));
48 void DbprintfEx(uint32_t flags
, const char *fmt
, ...) {
50 // should probably limit size here; oh well, let's just use a big buffer
51 char s
[DEBUG_MAX_MSG_SIZE
] = {0x00};
54 kvsprintf(fmt
, s
, 10, ap
);
57 DbpStringEx(flags
, s
, strlen(s
));
61 void Dbprintf(const char *fmt
, ...) {
63 // should probably limit size here; oh well, let's just use a big buffer
64 char output_string
[DEBUG_MAX_MSG_SIZE
] = {0x00};
68 kvsprintf(fmt
, output_string
, 10, ap
);
71 DbpString(output_string
);
76 void Dbhexdump(int len
, const uint8_t *d
, bool bAsci
) {
80 int l
= (len
> 16) ? 16 : len
;
89 for (int i
= 0; i
< l
; i
++) {
90 if (ascii
[i
] < 32 || ascii
[i
] > 126) {
95 Dbprintf("%-8s %*D", ascii
, l
, d
, " ");
97 Dbprintf("%*D", l
, d
, " ");
105 void print_result(const char *name
, const uint8_t *d
, size_t
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",
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]
124 for (; p
- d
< n
; p
++) {
125 sprintf(sp
, "%02x ", p
[0]);
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);