Files
adsb-tools/adsbus/common.h

97 lines
1.9 KiB
C
Raw Normal View History

2016-02-15 20:01:48 +00:00
#pragma once
#include <stdint.h>
#include <sys/epoll.h>
2016-02-15 20:01:48 +00:00
//////// peer
// All specific peer structs must be castable to this.
2016-02-17 08:05:18 +00:00
struct peer;
typedef void (*peer_event_handler)(struct peer *);
2016-02-15 20:01:48 +00:00
struct peer {
2016-02-17 08:05:18 +00:00
int fd;
peer_event_handler event_handler;
2016-02-15 20:01:48 +00:00
};
void peer_init();
void peer_epoll_add(struct peer *, uint32_t);
void peer_epoll_del(struct peer *);
void peer_loop();
2016-02-15 20:01:48 +00:00
2016-02-14 20:35:43 +00:00
//////// buf
2016-02-14 22:26:34 +00:00
#define BUF_LEN_MAX 256
struct buf {
2016-02-14 22:26:34 +00:00
char buf[BUF_LEN_MAX];
size_t start;
size_t length;
};
2016-02-16 19:26:30 +00:00
#define BUF_INIT { \
.start = 0, \
.length = 0, \
}
2016-02-14 20:35:43 +00:00
#define buf_chr(buff, at) ((buff)->buf[(buff)->start + (at)])
#define buf_at(buff, at) (&buf_chr(buff, at))
2016-02-15 21:12:26 +00:00
void buf_init(struct buf *);
2016-02-14 20:35:43 +00:00
ssize_t buf_fill(struct buf *, int);
void buf_consume(struct buf *, size_t);
//////// packet
2016-02-14 22:26:34 +00:00
#define DATA_LEN_MAX 14
struct packet {
enum {
MODE_S_SHORT,
MODE_S_LONG,
} type;
2016-02-17 18:01:39 -08:00
#define NUM_TYPES 2
2016-02-16 19:26:30 +00:00
uint8_t payload[DATA_LEN_MAX];
uint64_t mlat_timestamp;
uint32_t rssi;
};
2016-02-17 08:30:32 +00:00
extern char *packet_type_names[];
//////// mlat
#define MLAT_MHZ 120
// Use the signed max to avoid problems with some consumers; it's large enough to not matter.
#define MLAT_MAX INT64_MAX
#define RSSI_MAX UINT32_MAX
struct mlat_state {
uint64_t timestamp_last;
uint64_t timestamp_generation;
};
uint64_t mlat_timestamp_scale_in(uint64_t, uint64_t, uint16_t, struct mlat_state *);
2016-02-17 18:01:39 -08:00
uint64_t mlat_timestamp_scale_out(uint64_t, uint64_t, uint16_t);
//////// rssi
uint32_t rssi_scale_in(uint32_t, uint32_t);
2016-02-17 18:01:39 -08:00
uint32_t rssi_scale_out(uint32_t, uint32_t);
2016-02-14 20:35:43 +00:00
//////// hex
2016-02-15 20:01:48 +00:00
2016-02-14 22:26:34 +00:00
void hex_init();
2016-02-17 16:40:09 -08:00
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);
///////// retry timing
void retry_init();
void retry_cleanup();
uint32_t retry_get_delay_ms(uint32_t);