Show packets as hexdump instead of text

This commit is contained in:
Thomas Kolb 2022-02-27 22:01:51 +01:00
parent 9817896914
commit 45156e64a0

View file

@ -3,6 +3,8 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <ctype.h>
#include <liquid/liquid.h> #include <liquid/liquid.h>
#include <unistd.h> #include <unistd.h>
@ -34,6 +36,47 @@ void print_complex_array(const char *varname, float complex const *array, size_t
} }
void hexdump(const uint8_t *data, size_t len)
{
static const char lut[16] = "0123456789ABCDEF";
static const size_t BYTES_PER_LINE = 16;
size_t pos = 0;
while(pos < len) {
size_t bytes_in_line = len - pos;
if(bytes_in_line > BYTES_PER_LINE) {
bytes_in_line = BYTES_PER_LINE;
}
for(size_t i = 0; i < BYTES_PER_LINE; i++) {
if(i >= bytes_in_line) {
fputs(" ", stderr);
} else {
uint8_t byte = data[pos+i];
fprintf(stderr, "%c%c ", lut[byte >> 4], lut[byte & 0x0F]);
}
}
fputs(" ", stderr);
for(size_t i = 0; i < bytes_in_line; i++) {
uint8_t byte = data[pos+i];
if(isprint(byte)) {
putc(byte, stderr);
} else {
putc('.', stderr);
}
}
putc('\n', stderr);
pos += bytes_in_line;
}
}
void cb_rx(rx_evt_t evt, uint8_t *packet_data, size_t packet_len) void cb_rx(rx_evt_t evt, uint8_t *packet_data, size_t packet_len)
{ {
int ret; int ret;
@ -43,13 +86,13 @@ void cb_rx(rx_evt_t evt, uint8_t *packet_data, size_t packet_len)
case RX_EVT_CHECKSUM_ERROR: case RX_EVT_CHECKSUM_ERROR:
fprintf(stderr, "Received a message of %zu bytes, but decoding failed.\n", packet_len); fprintf(stderr, "Received a message of %zu bytes, but decoding failed.\n", packet_len);
//fprintf(stderr, "=== FAILED PAYLOAD ===\n"); //fprintf(stderr, "=== FAILED PAYLOAD ===\n");
//fprintf(stderr, "%s\n", packet_data); //hexdump(packet_data, packet_len);
//fprintf(stderr, "=======================\n"); //fprintf(stderr, "=======================\n");
break; break;
case RX_EVT_PACKET_RECEIVED: case RX_EVT_PACKET_RECEIVED:
fprintf(stderr, "=== DECODED PAYLOAD (%4zu bytes) ===\n", packet_len); fprintf(stderr, "=== DECODED PAYLOAD (%4zu bytes) ===\n", packet_len);
fprintf(stderr, "%s\n", packet_data); hexdump(packet_data, packet_len);
fprintf(stderr, "====================================\n"); fprintf(stderr, "====================================\n");
ret = write(m_tunfd, packet_data, packet_len); ret = write(m_tunfd, packet_data, packet_len);