2019-08-08 22:57:33 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-01-06 09:19:46 +08:00
|
|
|
// Copyright (C) Jonathan Westhues, Mar 2006
|
|
|
|
// Copyright (C) Gerhard de Koning Gans, Sep 2007
|
2019-08-08 22:57:33 +08:00
|
|
|
//
|
2022-01-06 09:19:46 +08:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// See LICENSE.txt for the text of the license.
|
2019-08-08 22:57:33 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "dbprint.h"
|
|
|
|
|
|
|
|
#include "string.h"
|
|
|
|
#include "cmd.h"
|
|
|
|
#include "printf.h"
|
|
|
|
|
|
|
|
#define DEBUG 1
|
2023-12-15 06:29:43 +08:00
|
|
|
#define DEBUG_MAX_MSG_SIZE 200
|
2019-08-08 22:57:33 +08:00
|
|
|
//=============================================================================
|
|
|
|
// Debug print functions, to go out over USB, to the usual PC-side client.
|
|
|
|
//=============================================================================
|
|
|
|
|
2020-05-10 23:28:55 +08:00
|
|
|
void DbpStringEx(uint32_t flags, const char *src, size_t srclen) {
|
2019-08-08 22:57:33 +08:00
|
|
|
#if DEBUG
|
|
|
|
struct {
|
|
|
|
uint16_t flag;
|
2023-12-15 06:29:43 +08:00
|
|
|
uint8_t buf[DEBUG_MAX_MSG_SIZE];
|
2019-08-08 22:57:33 +08:00
|
|
|
} PACKED data;
|
|
|
|
data.flag = flags;
|
2019-10-10 17:36:28 +08:00
|
|
|
uint16_t len = MIN(srclen, sizeof(data.buf));
|
|
|
|
memcpy(data.buf, src, len);
|
2019-08-08 22:57:33 +08:00
|
|
|
reply_ng(CMD_DEBUG_PRINT_STRING, PM3_SUCCESS, (uint8_t *)&data, sizeof(data.flag) + len);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-10 23:28:55 +08:00
|
|
|
void DbpString(const char *str) {
|
2019-08-08 22:57:33 +08:00
|
|
|
#if DEBUG
|
2019-10-10 17:36:28 +08:00
|
|
|
DbpStringEx(FLAG_LOG, str, strlen(str));
|
2019-08-08 22:57:33 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbprintfEx(uint32_t flags, const char *fmt, ...) {
|
|
|
|
#if DEBUG
|
|
|
|
// should probably limit size here; oh well, let's just use a big buffer
|
2023-12-15 06:29:43 +08:00
|
|
|
char s[DEBUG_MAX_MSG_SIZE] = {0x00};
|
2019-08-08 22:57:33 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2019-10-10 17:36:28 +08:00
|
|
|
kvsprintf(fmt, s, 10, ap);
|
2019-08-08 22:57:33 +08:00
|
|
|
va_end(ap);
|
|
|
|
|
2019-10-10 17:36:28 +08:00
|
|
|
DbpStringEx(flags, s, strlen(s));
|
2019-08-08 22:57:33 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dbprintf(const char *fmt, ...) {
|
|
|
|
#if DEBUG
|
|
|
|
// should probably limit size here; oh well, let's just use a big buffer
|
2023-12-15 06:29:43 +08:00
|
|
|
char output_string[DEBUG_MAX_MSG_SIZE] = {0x00};
|
2019-08-08 22:57:33 +08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
kvsprintf(fmt, output_string, 10, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
DbpString(output_string);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// prints HEX & ASCII
|
2023-08-09 12:09:31 +08:00
|
|
|
void Dbhexdump(int len, const uint8_t *d, bool bAsci) {
|
2019-08-08 22:57:33 +08:00
|
|
|
#if DEBUG
|
2023-10-01 19:05:08 +08:00
|
|
|
char ascii[17];
|
2019-08-08 22:57:33 +08:00
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
|
2023-10-01 19:05:08 +08:00
|
|
|
int l = (len > 16) ? 16 : len;
|
2019-08-08 22:57:33 +08:00
|
|
|
|
|
|
|
memcpy(ascii, d, l);
|
|
|
|
ascii[l] = 0;
|
|
|
|
|
|
|
|
// filter safe ascii
|
|
|
|
for (int i = 0; i < l; i++) {
|
|
|
|
if (ascii[i] < 32 || ascii[i] > 126) {
|
|
|
|
ascii[i] = '.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bAsci)
|
|
|
|
Dbprintf("%-8s %*D", ascii, l, d, " ");
|
|
|
|
else
|
|
|
|
Dbprintf("%*D", l, d, " ");
|
|
|
|
|
2023-10-01 19:05:08 +08:00
|
|
|
len -= 16;
|
|
|
|
d += 16;
|
2019-08-08 22:57:33 +08:00
|
|
|
}
|
|
|
|
#endif
|
2024-05-28 02:29:02 +08:00
|
|
|
}
|
|
|
|
void print_result(const char *name, const uint8_t *d, size_t
|
2019-08-08 22:57:33 +08:00
|
|
|
|
2024-05-28 02:29:02 +08:00
|
|
|
n) {
|
2019-08-08 22:57:33 +08:00
|
|
|
|
2023-10-01 19:05:08 +08:00
|
|
|
const uint8_t *p = d;
|
|
|
|
uint16_t tmp = n & 0xFFF0;
|
2019-08-08 22:57:33 +08:00
|
|
|
|
2023-10-01 19:05:08 +08:00
|
|
|
for (; p - d < tmp; p += 16) {
|
2019-08-08 22:57:33 +08:00
|
|
|
Dbprintf("[%s: %02d/%02d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
|
|
|
|
name,
|
2023-10-01 19:05:08 +08:00
|
|
|
p - d,
|
|
|
|
n,
|
2019-08-08 22:57:33 +08:00
|
|
|
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]
|
|
|
|
);
|
|
|
|
}
|
2023-10-01 19:05:08 +08:00
|
|
|
|
|
|
|
if (n % 16 != 0) {
|
2019-08-08 22:57:33 +08:00
|
|
|
char s[46] = {0};
|
|
|
|
char *sp = s;
|
2023-10-01 19:05:08 +08:00
|
|
|
for (; p - d < n; p++) {
|
2019-08-08 22:57:33 +08:00
|
|
|
sprintf(sp, "%02x ", p[0]);
|
|
|
|
sp += 3;
|
|
|
|
}
|
2023-10-01 19:05:08 +08:00
|
|
|
Dbprintf("[%s: %02d/%02d] %s", name, p - d, n, s);
|
2019-08-08 22:57:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-01 19:05:08 +08:00
|
|
|
// Prints message and hexdump
|
2024-05-14 18:32:57 +08:00
|
|
|
void print_dbg(const char *msg, const uint8_t *d, uint16_t n) {
|
2023-10-01 19:05:08 +08:00
|
|
|
if (g_dbglevel == DBG_DEBUG) {
|
|
|
|
print_result(msg, d, n);
|
2023-10-03 23:10:39 +08:00
|
|
|
}
|
2023-10-01 19:05:08 +08:00
|
|
|
}
|
|
|
|
|
2019-08-08 22:57:33 +08:00
|
|
|
/* useful when debugging new protocol implementations like FeliCa
|
|
|
|
void PrintToSendBuffer(void) {
|
|
|
|
DbpString("Printing ToSendBuffer:");
|
|
|
|
Dbhexdump(ToSendMax, ToSend, 0);
|
|
|
|
}
|
|
|
|
*/
|