Files
adsb-tools/adsbus.c

170 lines
3.4 KiB
C
Raw Normal View History

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/epoll.h>
2016-02-17 01:16:11 +00:00
#include <string.h>
#include <signal.h>
#include "common.h"
#include "backend.h"
#include "client.h"
#include "incoming.h"
#include "airspy_adsb.h"
2016-02-16 03:42:41 +00:00
#include "beast.h"
#include "json.h"
2016-02-17 08:30:32 +00:00
#include "stats.h"
static bool add_dump(char *format) {
struct serializer *serializer = client_get_serializer(format);
if (!serializer) {
fprintf(stderr, "Unknown dump format: %s\n", format);
return false;
}
client_add(1, serializer);
return true;
}
2016-02-17 01:16:11 +00:00
static void print_usage(char *argv[]) {
fprintf(stderr,
"Usage: %s [OPTION]...\n"
"\n"
"Options:\n"
"\t--help\n"
"\t--backend=HOST/PORT\n"
2016-02-17 01:16:11 +00:00
"\t--dump=FORMAT\n"
"\t--incoming=[HOST/]PORT\n"
"\t--listen=FORMAT=[HOST/]PORT\n"
2016-02-17 01:16:11 +00:00
, argv[0]);
}
static bool parse_opts(int argc, char *argv[], int epoll_fd) {
static struct option long_options[] = {
{"backend", required_argument, 0, 'b'},
{"dump", required_argument, 0, 'd'},
{"incoming", required_argument, 0, 'i'},
{"listen", required_argument, 0, 'l'},
{"help", no_argument, 0, 'h'},
2016-02-17 01:16:11 +00:00
};
int opt;
char *delim1, *delim2;
2016-02-17 01:16:11 +00:00
while ((opt = getopt_long_only(argc, argv, "", long_options, NULL)) != -1) {
switch (opt) {
2016-02-17 01:16:11 +00:00
case 'b':
// It would be really nice if libc had a standard way to split host:port.
delim1 = strrchr(optarg, '/');
if (delim1 == NULL) {
2016-02-17 01:16:11 +00:00
print_usage(argv);
return false;
}
*delim1 = '\0';
delim1++;
2016-02-17 01:16:11 +00:00
backend_new(optarg, delim1, epoll_fd);
2016-02-17 01:16:11 +00:00
break;
2016-02-15 21:12:26 +00:00
case 'd':
if (!add_dump(optarg)) {
return false;
}
break;
2016-02-17 01:16:11 +00:00
case 'h':
print_usage(argv);
return false;
case 'i':
delim1 = strrchr(optarg, '/');
if (delim1 == NULL) {
incoming_new(NULL, optarg, epoll_fd, backend_new_fd, NULL);
} else {
*delim1 = '\0';
delim1++;
incoming_new(optarg, delim1, epoll_fd, backend_new_fd, NULL);
}
break;
case 'l':
delim1 = strchr(optarg, '=');
if (delim1 == NULL) {
print_usage(argv);
return false;
}
*delim1 = '\0';
delim1++;
struct serializer *serializer = client_get_serializer(optarg);
if (!serializer) {
fprintf(stderr, "Unknown format: %s\n", optarg);
return false;
}
delim2 = strrchr(delim1, '/');
if (delim2 == NULL) {
incoming_new(NULL, delim1, epoll_fd, client_new_fd, serializer);
} else {
*delim2 = '\0';
delim2++;
incoming_new(delim1, delim2, epoll_fd, client_new_fd, serializer);
}
break;
default:
2016-02-17 01:16:11 +00:00
print_usage(argv);
return false;
}
}
2016-02-17 01:16:11 +00:00
if (optind != argc) {
fprintf(stderr, "Not a flag: %s\n", argv[optind]);
print_usage(argv);
return false;
}
return true;
}
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;
2016-02-17 08:05:18 +00:00
peer->event_handler(peer, epoll_fd);
}
}
}
int main(int argc, char *argv[]) {
signal(SIGPIPE, SIG_IGN);
server_init();
2016-02-14 22:26:34 +00:00
hex_init();
2016-02-15 06:47:40 +00:00
airspy_adsb_init();
2016-02-16 03:42:41 +00:00
beast_init();
json_init();
2016-02-17 08:30:32 +00:00
stats_init();
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-17 01:16:11 +00:00
if (!parse_opts(argc, argv, epoll_fd)) {
return EXIT_FAILURE;
}
2016-02-15 20:01:48 +00:00
loop(epoll_fd);
close(epoll_fd);
return EXIT_SUCCESS;
}