-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
172 lines (154 loc) · 4.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @file
* Main executable.
*/
#include "config.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_CAPSICUM
#include <sys/capability.h>
#endif
#include "data.h"
#include "main.h"
#include "receiver.h"
#include "rotator.h"
#include "tests.h"
config_s config;
/**
* Print a message stating how to get help.
*/
static void print_quickhelp() {
fprintf(stderr, "Type 'marp -h' for usage.\n");
}
/**
* Print usage information.
*/
static void print_usage() {
printf(
"Usage: marp -m ID [options]\n"
" marp -l FILE [options]\n"
"\n"
"Options:\n"
" -a MIN-MAX Set azimuth sweep range (default 0-359).\n"
" -d DEVICE Set antenna rotator device.\n"
" -h Display this help message.\n"
" -l FILE Load recorded data from a file.\n"
" -m ID Set antenna rotator model.\n"
" -o ORIGIN Set the direction of the source antenna.\n"
" -r DEVICE Set receiver device.\n"
" -w FILE Write data log to a file.\n"
);
}
/**
* Parse command-line arguments and load configuration defaults.
*/
static void parse_args(int argc, char *argv[]) {
int flag;
// Load default settings.
config.rot_file = "/dev/ttyU0";
config.rig_file = "/dev/ttyU1";
config.rig_model = 214;
config.az_min = 0, config.az_max = 359, config.az_origin = 0;
config.el_min = 0, config.el_max = 180;
config.write_file = "data.log";
while ((flag = getopt(argc, argv, "a:d:hl:m:o:r:w:")) != -1) {
switch (flag) {
case 'a':
if (sscanf(optarg, "%d,%d", &config.az_min,
&config.az_max) != 2) {
print_quickhelp();
exit(EXIT_FAILURE);
}
if (config.az_min < 0 || config.az_max > 360) {
fprintf(stderr, "Invalid azimuth sweep range\n");
print_quickhelp();
exit(EXIT_FAILURE);
}
break;
case 'd':
config.rot_file = optarg;
break;
case 'h':
print_usage();
exit(EXIT_SUCCESS);
break;
case 'l':
config.load_file = optarg;
break;
case 'm':
config.rot_model = atoi(optarg);
break;
case 'o':
config.az_origin = atoi(optarg);
break;
case 'r':
config.rig_file = optarg;
break;
case 'w':
config.write_file = optarg;
break;
case '?':
print_quickhelp();
exit(EXIT_FAILURE);
break;
}
}
// Complain about required options that are not specified.
if (config.rot_model == 0 && config.load_file == NULL) {
fprintf(stderr, "You must specify one of '-m' or '-l'.\n");
print_quickhelp();
exit(EXIT_FAILURE);
}
}
/**
* Clean up before exiting.
*/
static void cleanup() {
fprintf(stderr, "Cleaning up...\n");
rotator_close();
receiver_close();
}
/**
* Initialize sandboxing using Capsicum.
*/
static void init_sandbox() {
#ifdef HAVE_CAPSICUM
if (cap_enter() != 0) {
perror("Could not start Capsicum");
exit(EXIT_FAILURE);
}
#endif
}
/**
* Main program entry point.
*/
int main(int argc, char *argv[]) {
parse_args(argc, argv);
// Load data from a file if one is set, otherwise collect new data.
if (config.load_file != NULL) {
FILE *data_file;
// Read data from standard input if file is given as '-'.
if (*config.load_file == '-') {
data_file = stdin;
} else {
data_file = fopen(config.load_file, "r");
if (data_file == NULL) {
perror("Could not load data file");
exit(EXIT_FAILURE);
}
}
init_sandbox();
data_load(data_file);
fclose(data_file);
} else {
rotator_open();
receiver_open();
data_init();
init_sandbox();
atexit(cleanup);
signal(SIGINT, exit);
tests_run();
}
}