Files
adsb-tools/adsbus.c

95 lines
1.7 KiB
C
Raw Normal View History

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/epoll.h>
#include "common.h"
#include "airspy_adsb.h"
struct opts {
2016-02-15 21:12:26 +00:00
bool dump;
};
2016-02-15 06:47:40 +00:00
struct client {
int placeholder;
};
2016-02-14 20:35:43 +00:00
static int parse_opts(int argc, char *argv[], struct opts *opts) {
int opt;
2016-02-15 21:12:26 +00:00
while ((opt = getopt(argc, argv, "d")) != -1) {
switch (opt) {
2016-02-15 21:12:26 +00:00
case 'd':
opts->dump = true;
break;
default:
return -1;
}
}
return 0;
}
2016-02-15 20:01:48 +00:00
static int loop(int epoll_fd) {
while (1) {
#define MAX_EVENTS 10
struct epoll_event events[MAX_EVENTS];
2016-02-15 20:01:48 +00:00
int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
if (nfds == -1) {
perror("epoll_wait");
return -1;
}
for (int n = 0; n < nfds; n++) {
2016-02-15 06:47:40 +00:00
struct peer *peer = events[n].data.ptr;
switch (peer->type) {
2016-02-15 20:01:48 +00:00
case PEER_BACKEND:
if (!backend_read((struct backend *) peer)) {
2016-02-15 06:47:40 +00:00
return -1;
}
break;
default:
fprintf(stderr, "Unpossible: unknown peer type.\n");
return -1;
}
}
}
}
int main(int argc, char *argv[]) {
2016-02-14 22:26:34 +00:00
hex_init();
2016-02-15 06:47:40 +00:00
airspy_adsb_init();
2016-02-14 22:26:34 +00:00
struct opts opts = {
2016-02-15 21:12:26 +00:00
.dump = false,
};
2016-02-15 21:12:26 +00:00
if (parse_opts(argc, argv, &opts) ||
argc - optind < 2 ||
(argc - optind) % 2 != 0) {
fprintf(stderr, "Usage: %s -d localhost 30006 [ remotehost 30002 ... ]\n", argv[0]);
return EXIT_FAILURE;
}
2016-02-15 20:01:48 +00:00
int epoll_fd = epoll_create1(0);
if (epoll_fd == -1) {
perror("epoll_create1");
return EXIT_FAILURE;
}
2016-02-15 21:12:26 +00:00
int nbackends = (argc - optind) / 2;
struct backend backends[nbackends];
for (int i = 0, j = optind; i < nbackends && j < argc; i++, j += 2) {
backend_init(&backends[i]);
if (!backend_connect(argv[j], argv[j + 1], &backends[i], epoll_fd)) {
return EXIT_FAILURE;
}
}
2016-02-15 20:01:48 +00:00
loop(epoll_fd);
close(epoll_fd);
return EXIT_SUCCESS;
}