-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
338 lines (301 loc) · 10 KB
/
main.cc
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// satnow: main.cc
//
// Copyright 2019 Matt Davis (https://github.com/enferex)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "main.hh"
#include "db.hh"
#include "display.hh"
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <curl/curl.h>
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <string>
#include <vector>
// Resources:
// https://www.celestrak.com/NORAD/documentation/tle-fmt.php
// https://en.wikipedia.org/wiki/Two-line_element_set
// Command line options.
static const struct option opts[] = {
{"alt", required_argument, nullptr, 'a'},
{"lat", required_argument, nullptr, 'x'},
{"lon", required_argument, nullptr, 'y'},
{"update", required_argument, nullptr, 'u'},
{"db", required_argument, nullptr, 'd'},
{"verbose", no_argument, nullptr, 'v'},
#if HAVE_GUI
{"gui", no_argument, nullptr, 'g'},
{"refresh", required_argument, nullptr, 'r'},
#endif
{"help", no_argument, nullptr, 'h'}};
[[noreturn]] static void usage(const char *execname) {
std::cout << "satnow v" << VER << std::endl
<< "Usage: " << execname << " --lat=val --lon=val "
<< "[-h -v --alt=val --update=file --db=file]" << std::endl;
#if HAVE_GUI
std::cout << " [--gui --refresh=msec] " << std::endl;
#endif
std::cout << " --lat=<latitude in degrees>" << std::endl
<< " --lon=<longitude in degrees>" << std::endl
<< " --alt=<altitude in meters>" << std::endl
<< " --db=<path to database> (default: " << DEFAULT_DB_PATH << ')'
<< std::endl
<< " --help/-h: This help message." << std::endl
<< " --verbose/-v: Output additional data (for debugging)."
<< std::endl
#if HAVE_GUI
<< " --gui: Enable curses/gui mode." << std::endl
<< " --refresh/-r: Number of milliseconds to refresh gui."
<< std::endl
#endif
<< " --update=<sources> " << std::endl
<< " Where 'sources' is a text file containing a " << std::endl
<< " list of URLs that point to a TLE .txt file. " << std::endl
<< " Each source must be on its own line in the file."
<< std::endl;
exit(EXIT_SUCCESS);
}
bool readLine(FILE *fp, std::string &str) {
if (feof(fp) || ferror(fp))
return false;
char *line = nullptr;
size_t n = 0;
if (!getline(&line, &n, fp) || n == 0)
return false;
str = std::string(line);
free(line);
if (str.size() == 0 && feof(fp))
return false;
else
return true;
}
// Return a vector of TLE instances for each TLE entry.
// This supports both forms of TLE where each line of data (two of them) are 69
// bytes each, and the optional name line is 24 bytes.
static std::vector<Tle> readTLEs(const std::string &fname, FILE *fp) {
std::vector<Tle> tles;
std::string line1, line2, name;
size_t lineNo = 0;
while (readLine(fp, line1)) {
++lineNo;
if (std::isalpha(line1[0]) || line1.size() <= 24)
name = line1;
// Trim trailing whitespace from name.
auto en = name.find_last_not_of(" \t\r\n");
if (en != std::string::npos)
name = name.substr(0, en + 1);
// Celestrak and wikipedia say that names are 24 bytes, libsgp4 says 22.
if (name.size() > 22)
name = name.substr(0, 22);
++lineNo;
if (!readLine(fp, line1)) {
std::cerr << "Unexpected error reading TLE line 1 at line " << lineNo
<< " in " << fname << std::endl;
return tles;
}
++lineNo;
if (!readLine(fp, line2)) {
std::cerr << "Unexpected error reading TLE line 2 at line " << lineNo
<< " in " << fname << std::endl;
return tles;
}
tles.emplace_back(name, line1.substr(0, 69), line2.substr(0, 69));
name.clear();
}
return tles;
}
static bool tryParseFile(const std::string &fname, std::vector<Tle> &tles) {
// If it looks like a URL, don't try to open it.
if (fname.find("://") != std::string::npos)
return false;
FILE *fp; // We use c-style file i/o so we can use tmpfile in tryParseURL.
if (!(fp = fopen(fname.c_str(), "r")))
return false;
// Read the new TLEs and add them to 'tles'.
auto newTLEs = readTLEs(fname, fp);
tles.insert(tles.end(), std::make_move_iterator(newTLEs.begin()),
std::make_move_iterator(newTLEs.end()));
fclose(fp);
return true;
}
static bool tryParseURL(const std::string &fname, std::vector<Tle> &tles) {
CURL *crl = curl_easy_init();
if (!crl)
return false;
if (curl_easy_setopt(crl, CURLOPT_URL, fname.c_str()))
return false;
// Ignore any "URLs" that do not have a protocol delimiter.
if (fname.find("://") == std::string::npos)
return false;
// Prepare to download the TLE data into a temp file.
FILE *fp = tmpfile();
if (!fp)
return false;
if (curl_easy_setopt(crl, CURLOPT_WRITEDATA, fp)) {
fclose(fp);
curl_easy_cleanup(crl);
return false;
}
// Download the data.
std::cout << "[+] Downloading contents from " << fname << std::endl;
bool ok = curl_easy_perform(crl) == CURLE_OK;
curl_easy_cleanup(crl);
// Read the new TLEs and add them to 'tles'.
rewind(fp);
auto newTLEs = readTLEs(fname, fp);
tles.insert(tles.end(), std::make_move_iterator(newTLEs.begin()),
std::make_move_iterator(newTLEs.end()));
fclose(fp);
return ok;
}
// Update the database of TLEs.
static void update(const char *sourceFile, DB &db, bool verbose) {
assert(sourceFile && db.ok() && "Invalid input to update.");
// Parse the source file (one entry per line).
std::string line;
std::ifstream fh(sourceFile);
size_t lineNumber = 0;
std::vector<Tle> results;
while (std::getline(fh, line)) {
++lineNumber;
// Trim white space from both ends.
size_t st, en;
if ((st = line.find_first_not_of(" \t\r\n")) == std::string::npos ||
(en = line.find_last_not_of("# \t\r\n")) == std::string::npos)
continue;
++en;
// If a comment line, ignore.
if (line[st] == '#')
continue;
// Reject empty lines.
if (en - st == 0)
continue;
// Trim comments and any spaces from the end.
const auto comments = line.find_first_of("# \t\r\n", st);
if (comments != std::string::npos)
en = comments;
// Get the trimmed string.
const auto str = line.substr(st, en - st);
// Parse the contents at the url or in the file.
std::cerr << "[+] Loading TLEs from '" << str << '\'' << std::endl;
if (!tryParseFile(str, results) && !tryParseURL(str, results)) {
std::cerr << "[-] Unknown entry in " << sourceFile << " Line "
<< lineNumber << std::endl;
continue;
}
}
// Update the database.
size_t count = 0;
for (const auto &tle : results) {
db.update(tle);
if (verbose)
std::cout << "[+] Refreshing [" << (++count) << '/' << results.size()
<< "]: " << std::to_string(tle.NoradNumber()) << " ("
<< tle.Name() << ") [" << (db.ok() ? "Good" : "Failed") << ']'
<< std::endl;
}
}
SatLookAngles getSatellitesAndLookAngles(double lat, double lon, double alt,
DB &db) {
SatLookAngles sats(lat, lon, alt);
// Get the TLEs.
std::vector<Tle> tles = db.fetchTLEs();
// Add the TLEs (this will automatically generate look angles.).
for (const auto &tle : tles)
sats.add(tle);
// Sort by increasing range.
sats.sort();
return sats;
}
int main(int argc, char **argv) {
double alt = 0.0, lat = 0.0, lon = 0.0;
bool verbose = false, gui = false;
const char *sourceFile = nullptr, *dbFile = DEFAULT_DB_PATH;
int opt, refreshRate = -1;
const char *optStr = "ghva:d:r:u:x:y:";
while ((opt = getopt_long(argc, argv, optStr, opts, nullptr)) > 0) {
switch (opt) {
#if HAVE_GUI
case 'g':
gui = true;
break;
case 'r':
refreshRate = std::stoi(optarg);
break;
#endif
case 'h':
usage(argv[0]);
break;
case 'v':
verbose = true;
break;
case 'a':
alt = std::stod(optarg);
break;
case 'd':
dbFile = optarg;
break;
case 'u':
sourceFile = optarg;
break;
case 'x':
lat = std::stod(optarg);
break;
case 'y':
lon = std::stod(optarg);
break;
default:
std::cerr << "[-] Unknown command line option." << std::endl;
exit(EXIT_FAILURE);
break;
}
}
// Ensure we have valid coords.
if (lat > 90.0 || lat < -90.0 || lon > 180.0 || lon < -180.0) {
std::cerr << "[-] Invalid coordinates (latitude: " << lat
<< ", longitude: " << lon << ')' << std::endl;
return EXIT_FAILURE;
}
std::cout << "[+] Using viewer position (latitude: " << lat
<< ", longitude: " << lon << ", "
<< ", altitude: " << alt << ')' << std::endl;
// Open the database that contains the TLE data.
if (!dbFile) {
std::cerr << "[-] The database path must not be empty (see --help)."
<< std::endl;
return EXIT_FAILURE;
}
std::cout << "[+] Using database: " << dbFile << std::endl;
DBSQLite db(dbFile);
if (!db.ok()) {
std::cerr << "[-] Error opening database '" << dbFile
<< "': " << db.getErrorString() << std::endl;
return EXIT_FAILURE;
}
// If a source file is specified, then update the existing database.
if (sourceFile)
update(sourceFile, db, verbose);
// Calculate and display.
auto TLEsAndLAs = getSatellitesAndLookAngles(lat, lon, alt, db);
if (gui) {
DisplayNCurses disp(refreshRate);
disp.render(TLEsAndLAs);
} else {
DisplayConsole disp;
disp.render(TLEsAndLAs);
}
return 0;
}