/*
* MIT License
*
* Copyright (c) 2018-2025 ustccw
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <stdio.h>
#include <stdint.h>
/**
* @brief print the data in hexdump or charset dump
*
* print input data, print from data[0] to data[len-1], additionally add notes string
*
* @param[in] data: input data pointer to print
* @param[in] len: data length
* @param[in] note: notes for read easily
* @param[in] mode: 0x00, 0x01, 0x10, 0x11 to decide the HEXDUMP_SHOW && TWO_COLUMN_SHOW
*
* @return noreturn
*
*/
void print_debug(uint8_t *data, int len, const char *note, int mode)
{
#define HEXDUMP_SHOW 0x10 // 0x10 and 0x11: hexdump; 0x00 and 0x01: charset dump
#define TWO_COLUMN_SHOW 0x01 // At the end of each line, counting the total bytes of characters
#define EACH_LINE_BYTES 32 // The maximum number of bytes for each line
#define debug_print printf
debug_print("
********** %s (len:%u) start addr: %p **********
", note, len, data);
for (int i = 0; i < len; ++i) {
if (HEXDUMP_SHOW & mode) {
debug_print("%02x ",data[i]);
} else {
if (data[i] < 32 || data[i] > 126) {
// control || invisible charset
if (i > 0 && data[i-1] >= 33 && data[i-1] <= 126) {
// skip (control || invisible charset || backspace)
debug_print(" ");
}
debug_print("%02x ",data[i]);
} else {
debug_print("%c", data[i]);
}
}
if ((TWO_COLUMN_SHOW & mode) && ((i + 1) % EACH_LINE_BYTES == 0)) {
debug_print(" | %d Bytes
",i + 1);
}
} // end for
if ((TWO_COLUMN_SHOW & mode) && (len % EACH_LINE_BYTES == 0)) {
debug_print("------------------------- %s end -------------------------
", note);
} else {
debug_print("
------------------------- %s end -------------------------
", note);
}
}