2024-08-16 10:01:09 +08:00
|
|
|
|
#pragma once
|
2024-08-01 11:40:00 +08:00
|
|
|
|
|
2024-08-16 10:01:09 +08:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
extern "C"
|
|
|
|
|
|
{
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
#include <stdio.h>
|
2024-08-16 10:19:36 +08:00
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* The maximum number of seconds that can be stored in the time_t value is 2147483647 –- a little over 68 years.
|
|
|
|
|
|
*
|
|
|
|
|
|
* struct timespec
|
|
|
|
|
|
* {
|
|
|
|
|
|
* time_t tv_sec; // seconds
|
|
|
|
|
|
* long tv_nsec; // nanoseconds
|
|
|
|
|
|
* };
|
|
|
|
|
|
*
|
|
|
|
|
|
* 1 s = 1000 ms
|
|
|
|
|
|
* 1 ms = 1000 us
|
|
|
|
|
|
* 1 us = 1000 ns
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#define TIMESPEC_TO_MSEC(ts) ((ts).tv_sec * 1000 + (ts).tv_nsec / 1000000)
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint64_t clock_get_real_time_ms()
|
|
|
|
|
|
{
|
|
|
|
|
|
struct timespec now;
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME_COARSE, &now);
|
|
|
|
|
|
|
|
|
|
|
|
return TIMESPEC_TO_MSEC(now);
|
|
|
|
|
|
}
|
2024-08-16 10:01:09 +08:00
|
|
|
|
|
|
|
|
|
|
static inline void hexdump_to_fd(int fd, uint32_t idx, const char *data, uint16_t len)
|
2024-08-01 11:40:00 +08:00
|
|
|
|
{
|
|
|
|
|
|
uint16_t i = 0;
|
|
|
|
|
|
uint16_t used = 0;
|
|
|
|
|
|
uint16_t offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
#define LINE_LEN 80
|
|
|
|
|
|
char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
|
|
|
|
|
|
while (offset < len)
|
|
|
|
|
|
{
|
2024-08-13 15:23:48 +08:00
|
|
|
|
used = snprintf(line, LINE_LEN, "%08X ", idx + offset);
|
|
|
|
|
|
|
|
|
|
|
|
// hexdump
|
2024-08-01 11:40:00 +08:00
|
|
|
|
for (i = 0; ((offset + i) < len) && (i < 16); i++)
|
|
|
|
|
|
{
|
2024-08-13 15:23:48 +08:00
|
|
|
|
if (i == 8)
|
|
|
|
|
|
{
|
|
|
|
|
|
used += snprintf(line + used, LINE_LEN - used, " ");
|
|
|
|
|
|
}
|
|
|
|
|
|
used += snprintf(line + used, LINE_LEN - used, " %02x", (data[offset + i] & 0xff));
|
2024-08-01 11:40:00 +08:00
|
|
|
|
}
|
2024-08-13 15:23:48 +08:00
|
|
|
|
|
|
|
|
|
|
// padding
|
2024-08-01 11:40:00 +08:00
|
|
|
|
for (; i <= 16; i++)
|
|
|
|
|
|
{
|
2024-08-13 15:23:48 +08:00
|
|
|
|
if (i == 8)
|
|
|
|
|
|
{
|
|
|
|
|
|
used += snprintf(line + used, LINE_LEN - used, " ");
|
|
|
|
|
|
}
|
|
|
|
|
|
used += snprintf(line + used, LINE_LEN - used, " ");
|
2024-08-01 11:40:00 +08:00
|
|
|
|
}
|
2024-08-13 15:23:48 +08:00
|
|
|
|
|
|
|
|
|
|
// ascii
|
2024-08-01 11:40:00 +08:00
|
|
|
|
for (i = 0; (offset < len) && (i < 16); i++, offset++)
|
|
|
|
|
|
{
|
|
|
|
|
|
unsigned char c = data[offset];
|
|
|
|
|
|
if ((c < ' ') || (c > '~'))
|
|
|
|
|
|
{
|
|
|
|
|
|
c = '.';
|
|
|
|
|
|
}
|
2024-08-13 15:23:48 +08:00
|
|
|
|
if (i == 8)
|
|
|
|
|
|
{
|
|
|
|
|
|
used += snprintf(line + used, LINE_LEN - used, " ");
|
|
|
|
|
|
}
|
2024-08-01 11:40:00 +08:00
|
|
|
|
used += snprintf(line + used, LINE_LEN - used, "%c", c);
|
|
|
|
|
|
}
|
|
|
|
|
|
dprintf(fd, "%s\n", line);
|
|
|
|
|
|
}
|
2024-08-16 10:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|