diff --git a/adsbus/Makefile b/adsbus/Makefile index c4f0968..06545bd 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -5,7 +5,7 @@ LIBS ?= -ljansson OBJ_NETWORK = receive.o send.o incoming.o outgoing.o OBJ_PROTOCOL = airspy_adsb.o beast.o json.o raw.o stats.o -OBJ_UTIL = rand.o uuid.o wakeup.o opts.o common.o +OBJ_UTIL = rand.o uuid.o wakeup.o opts.o hex.o common.o all: adsbus diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index de03452..6c0193b 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -15,9 +15,10 @@ #include "json.h" #include "stats.h" -#include "rand.h" -#include "opts.h" #include "common.h" +#include "hex.h" +#include "opts.h" +#include "rand.h" #include "wakeup.h" static void print_usage(const char *name) { diff --git a/adsbus/airspy_adsb.c b/adsbus/airspy_adsb.c index a82ae18..46885bf 100644 --- a/adsbus/airspy_adsb.c +++ b/adsbus/airspy_adsb.c @@ -3,8 +3,10 @@ #include #include "common.h" -#include "uuid.h" +#include "hex.h" #include "receive.h" +#include "uuid.h" + #include "airspy_adsb.h" #define SEND_MHZ 20 diff --git a/adsbus/common.c b/adsbus/common.c index 874e004..5a29105 100644 --- a/adsbus/common.c +++ b/adsbus/common.c @@ -162,69 +162,3 @@ uint32_t rssi_scale_in(uint32_t value, uint32_t max) { uint32_t rssi_scale_out(uint32_t value, uint32_t max) { return value / (RSSI_MAX / max); } - - -static uint8_t hex_table[256] = {0}; -static char hex_upper_table[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', }; -static char hex_lower_table[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; - -void hex_init() { - for (int i = '0'; i <= '9'; i++) { - hex_table[i] = i - '0'; - } - for (int i = 'a'; i <= 'f'; i++) { - hex_table[i] = 10 + i - 'a'; - } - for (int i = 'A'; i <= 'F'; i++) { - hex_table[i] = 10 + i - 'A'; - } -} - -void hex_to_bin(uint8_t *out, const char *in, size_t bytes) { - const uint8_t *in2 = (uint8_t *) in; - for (size_t i = 0, j = 0; i < bytes; i++, j += 2) { - out[i] = (hex_table[in2[j]] << 4) | hex_table[in2[j + 1]]; - } -} - -uint64_t hex_to_int(const char *in, size_t bytes) { - const uint8_t *in2 = (uint8_t *) in; - uint64_t ret = 0; - bytes *= 2; - for (size_t i = 0; i < bytes; i++) { - ret <<= 4; - ret |= hex_table[in2[i]]; - } - return ret; -} - -static void hex_from_bin(char *out, const uint8_t *in, size_t bytes, char table[]) { - for (size_t i = 0, j = 0; i < bytes; i++, j += 2) { - out[j] = table[in[i] >> 4]; - out[j + 1] = table[in[i] & 0xf]; - } -} - -static void hex_from_int(char *out, uint64_t in, size_t bytes, char table[]) { - bytes *= 2; - for (int o = bytes - 1; o >= 0; o--) { - out[o] = table[in & 0xf]; - in >>= 4; - } -} - -void hex_from_bin_upper(char *out, const uint8_t *in, size_t bytes) { - hex_from_bin(out, in, bytes, hex_upper_table); -} - -void hex_from_bin_lower(char *out, const uint8_t *in, size_t bytes) { - hex_from_bin(out, in, bytes, hex_lower_table); -} - -void hex_from_int_upper(char *out, uint64_t in, size_t bytes) { - hex_from_int(out, in, bytes, hex_upper_table); -} - -void hex_from_int_lower(char *out, uint64_t in, size_t bytes) { - hex_from_int(out, in, bytes, hex_lower_table); -} diff --git a/adsbus/common.h b/adsbus/common.h index f99704c..f761531 100644 --- a/adsbus/common.h +++ b/adsbus/common.h @@ -76,14 +76,3 @@ uint64_t mlat_timestamp_scale_out(uint64_t, uint64_t, uint16_t); uint32_t rssi_scale_in(uint32_t, uint32_t); uint32_t rssi_scale_out(uint32_t, uint32_t); - - -//////// hex - -void hex_init(); -void hex_to_bin(uint8_t *, const char *, size_t); -uint64_t hex_to_int(const char *, size_t); -void hex_from_bin_upper(char *, const uint8_t *, size_t); -void hex_from_bin_lower(char *, const uint8_t *, size_t); -void hex_from_int_upper(char *, uint64_t, size_t); -void hex_from_int_lower(char *, uint64_t, size_t); diff --git a/adsbus/hex.c b/adsbus/hex.c new file mode 100644 index 0000000..8d01f95 --- /dev/null +++ b/adsbus/hex.c @@ -0,0 +1,66 @@ +#include "hex.h" + +static uint8_t hex_table[256] = {0}; +static char hex_upper_table[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', }; +static char hex_lower_table[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; + +void hex_init() { + for (int i = '0'; i <= '9'; i++) { + hex_table[i] = i - '0'; + } + for (int i = 'a'; i <= 'f'; i++) { + hex_table[i] = 10 + i - 'a'; + } + for (int i = 'A'; i <= 'F'; i++) { + hex_table[i] = 10 + i - 'A'; + } +} + +void hex_to_bin(uint8_t *out, const char *in, size_t bytes) { + const uint8_t *in2 = (uint8_t *) in; + for (size_t i = 0, j = 0; i < bytes; i++, j += 2) { + out[i] = (hex_table[in2[j]] << 4) | hex_table[in2[j + 1]]; + } +} + +uint64_t hex_to_int(const char *in, size_t bytes) { + const uint8_t *in2 = (uint8_t *) in; + uint64_t ret = 0; + bytes *= 2; + for (size_t i = 0; i < bytes; i++) { + ret <<= 4; + ret |= hex_table[in2[i]]; + } + return ret; +} + +static void hex_from_bin(char *out, const uint8_t *in, size_t bytes, char table[]) { + for (size_t i = 0, j = 0; i < bytes; i++, j += 2) { + out[j] = table[in[i] >> 4]; + out[j + 1] = table[in[i] & 0xf]; + } +} + +static void hex_from_int(char *out, uint64_t in, size_t bytes, char table[]) { + bytes *= 2; + for (int o = bytes - 1; o >= 0; o--) { + out[o] = table[in & 0xf]; + in >>= 4; + } +} + +void hex_from_bin_upper(char *out, const uint8_t *in, size_t bytes) { + hex_from_bin(out, in, bytes, hex_upper_table); +} + +void hex_from_bin_lower(char *out, const uint8_t *in, size_t bytes) { + hex_from_bin(out, in, bytes, hex_lower_table); +} + +void hex_from_int_upper(char *out, uint64_t in, size_t bytes) { + hex_from_int(out, in, bytes, hex_upper_table); +} + +void hex_from_int_lower(char *out, uint64_t in, size_t bytes) { + hex_from_int(out, in, bytes, hex_lower_table); +} diff --git a/adsbus/hex.h b/adsbus/hex.h new file mode 100644 index 0000000..7993d86 --- /dev/null +++ b/adsbus/hex.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +void hex_init(); +void hex_to_bin(uint8_t *, const char *, size_t); +uint64_t hex_to_int(const char *, size_t); +void hex_from_bin_upper(char *, const uint8_t *, size_t); +void hex_from_bin_lower(char *, const uint8_t *, size_t); +void hex_from_int_upper(char *, uint64_t, size_t); +void hex_from_int_lower(char *, uint64_t, size_t); diff --git a/adsbus/json.c b/adsbus/json.c index 7595f30..13a3623 100644 --- a/adsbus/json.c +++ b/adsbus/json.c @@ -3,10 +3,12 @@ #include #include +#include "hex.h" +#include "rand.h" #include "receive.h" #include "send.h" -#include "rand.h" #include "uuid.h" + #include "json.h" static void json_serialize_to_buf(json_t *obj, struct buf *buf) { diff --git a/adsbus/raw.c b/adsbus/raw.c index a0ed0d3..c735cd2 100644 --- a/adsbus/raw.c +++ b/adsbus/raw.c @@ -3,7 +3,9 @@ #include #include "common.h" +#include "hex.h" #include "uuid.h" + #include "raw.h" struct __attribute__((packed)) raw_mode_s_short_overlay { diff --git a/adsbus/uuid.c b/adsbus/uuid.c index 505c292..10cb5ea 100644 --- a/adsbus/uuid.c +++ b/adsbus/uuid.c @@ -1,7 +1,8 @@ #include -#include "rand.h" #include "common.h" +#include "hex.h" +#include "rand.h" #include "uuid.h"