Files
adsb-tools/adsbus/adsbus.c

129 lines
2.3 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
2016-02-17 01:16:11 +00:00
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "incoming.h"
#include "outgoing.h"
#include "receive.h"
#include "send.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"
2016-02-22 14:45:18 -08:00
#include "common.h"
2016-02-22 16:36:27 -08:00
#include "hex.h"
#include "opts.h"
#include "rand.h"
2016-02-22 14:45:18 -08:00
#include "wakeup.h"
2016-02-17 16:40:09 -08:00
static void print_usage(const char *name) {
2016-02-17 01:16:11 +00:00
fprintf(stderr,
2016-02-17 16:04:11 -08:00
"\n"
2016-02-17 01:16:11 +00:00
"Usage: %s [OPTION]...\n"
"\n"
"Options:\n"
"\t--help\n"
"\t--dump=FORMAT\n"
"\t--connect-receive=HOST/PORT\n"
"\t--connect-send=FORMAT=HOST/PORT\n"
"\t--listen-receive=[HOST/]PORT\n"
"\t--listen-send=FORMAT=[HOST/]PORT\n"
2016-02-17 16:40:09 -08:00
, name);
receive_print_usage();
send_print_usage();
2016-02-17 16:04:11 -08:00
}
static bool parse_opts(int argc, char *argv[]) {
2016-02-17 01:16:11 +00:00
static struct option long_options[] = {
{"dump", required_argument, 0, 'd'},
{"connect-receive", required_argument, 0, 'c'},
{"connect-send", required_argument, 0, 's'},
{"listen-receive", required_argument, 0, 'l'},
{"listen-send", required_argument, 0, 'm'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0 },
2016-02-17 01:16:11 +00:00
};
int opt;
2016-02-17 01:16:11 +00:00
while ((opt = getopt_long_only(argc, argv, "", long_options, NULL)) != -1) {
2016-02-17 16:04:11 -08:00
bool (*handler)(char *) = NULL;
switch (opt) {
2016-02-15 21:12:26 +00:00
case 'd':
handler = opts_add_dump;
break;
case 'c':
handler = opts_add_connect_receive;
break;
2016-02-17 01:16:11 +00:00
case 's':
handler = opts_add_connect_send;
break;
case 'l':
handler = opts_add_listen_receive;
break;
case 'm':
handler = opts_add_listen_send;
break;
case 'h':
default:
2016-02-17 16:40:09 -08:00
print_usage(argv[0]);
return false;
}
2016-02-17 16:04:11 -08:00
if (handler) {
if (!handler(optarg)) {
fprintf(stderr, "Invalid flag value: %s\n", optarg);
2016-02-17 16:40:09 -08:00
print_usage(argv[0]);
2016-02-17 16:04:11 -08:00
return false;
}
}
}
2016-02-17 01:16:11 +00:00
if (optind != argc) {
fprintf(stderr, "Not a flag: %s\n", argv[optind]);
2016-02-17 16:40:09 -08:00
print_usage(argv[0]);
2016-02-17 01:16:11 +00:00
return false;
}
return true;
}
int main(int argc, char *argv[]) {
assert(!close(0));
2016-02-14 22:26:34 +00:00
hex_init();
2016-02-21 15:56:07 -08:00
rand_init();
2016-02-18 09:33:32 -08:00
wakeup_init();
peer_init();
2016-02-18 09:33:32 -08:00
send_init();
2016-02-18 09:33:32 -08:00
2016-02-16 03:42:41 +00:00
beast_init();
json_init();
2016-02-17 08:30:32 +00:00
stats_init();
if (!parse_opts(argc, argv)) {
return EXIT_FAILURE;
}
peer_loop();
2016-02-21 15:56:07 -08:00
rand_cleanup();
2016-02-20 23:56:40 -08:00
wakeup_cleanup();
send_cleanup();
2016-02-20 23:56:40 -08:00
incoming_cleanup();
outgoing_cleanup();
return EXIT_SUCCESS;
}