Move hexdump() function to utils

This commit is contained in:
Thomas Kolb 2024-06-01 16:04:17 +02:00
parent 692780b313
commit de0f3620e6
5 changed files with 44 additions and 82 deletions

View file

@ -69,47 +69,6 @@ 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, const struct layer1_rx_s *rx, uint8_t *packet_data, size_t packet_len) void cb_rx(rx_evt_t evt, const struct layer1_rx_s *rx, uint8_t *packet_data, size_t packet_len)
{ {
int ret; int ret;

View file

@ -97,3 +97,44 @@ void sleep_until(double hires_time)
} while(ret == EINTR); } while(ret == EINTR);
} }
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;
}
}

View file

@ -28,4 +28,6 @@ void sleep_until(double hires_time);
void fsleep(double d); void fsleep(double d);
double get_hires_time(void); double get_hires_time(void);
void hexdump(const uint8_t *data, size_t len);
#endif // UTILS_H #endif // UTILS_H

View file

@ -33,47 +33,6 @@ 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, const layer1_rx_t *rx, uint8_t *packet_data, size_t packet_len) void cb_rx(rx_evt_t evt, const layer1_rx_t *rx, uint8_t *packet_data, size_t packet_len)
{ {
(void)rx; (void)rx;

View file

@ -5,6 +5,7 @@
#include <liquid/liquid.h> #include <liquid/liquid.h>
#include "logger.h" #include "logger.h"
#include "utils.h"
#include "layer1/rx.h" #include "layer1/rx.h"
#include "config.h" #include "config.h"