gpsrinex.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /*
  2. * gpsrinex: read "RAW" messages from a gpsd and output a RINEX 3 obs file.
  3. *
  4. * gpsrinex will read live data from gpsd and create a file of RINEX 3
  5. * observations. Currently this only works if the GPS is a u-blox
  6. * GPS and is sending UBX-RXM-RAWX messages.
  7. *
  8. * The u-blox must be configured for u-blox binary messages. GLONASS,
  9. * GALILEO, and BEIDOU must be off. Optionally SBAS on, but can be
  10. * flakey.
  11. *
  12. * Too much data for 9600!
  13. *
  14. * To configure a u-blox to output the proper data:
  15. * # gpsctl -s 115200
  16. * # sleep 2
  17. * # ubxtool -d NMEA
  18. * # ubxtool -e BINARY
  19. * # ubxtool -d GLONASS
  20. * # ubxtool -d BEIDOU
  21. * # ubxtool -d GALILEO
  22. * # ubxtool -d SBAS
  23. * # ubxtool -e RAWX
  24. *
  25. * If you have a u-blox 9 then enable GLONASS as well.
  26. *
  27. * After collecting the default number of observations, gpsrinex will
  28. * create the RINEX .obs file and exit. Upload this file to an
  29. * offline processing service to get cm accuracy.
  30. *
  31. * One service known to work with obsrinex output is [CSRS-PPP]:
  32. * https://webapp.geod.nrcan.gc.ca/geod/tools-outils/ppp.php
  33. *
  34. * Examples:
  35. * To collect 4 hours of samples as 30 second intervals:
  36. * # gpsrinex -i 30 -n 480
  37. *
  38. * To generate RINEX 3 from a u-blox capture file:
  39. * Grab 4 hours of raw live data:
  40. * # gpspipe -x 14400 -R > 4h-raw.ubx
  41. * Feed that data to gpsfake:
  42. * # gpsfake -1 -P 3000 4h-raw.ubx
  43. * In another window, convert that raw to RINEX 3:
  44. * # gpsrinex -i 1 -n 1000000
  45. *
  46. * See also:
  47. * [1] RINEX: The Receiver Independent Exchange Format, Version 3.04
  48. * ftp://igs.org/pub/data/format/rinex304.pdf
  49. *
  50. * [2] GPSTk, http://www.gpstk.org/
  51. *
  52. * [3] Nischan, Thomas (2016):
  53. * GFZRNX - RINEX GNSS Data Conversion and Manipulation Toolbox.
  54. * GFZ Data Services. http://dx.doi.org/10.5880/GFZ.1.1.2016.002
  55. *
  56. * [4] RTKLIB: An Open Source Program Package for GNSS Positioning
  57. * http://www.rtklib.com/
  58. *
  59. * This file is Copyright 2018 by the GPSD project
  60. * SPDX-License-Identifier: BSD-2-clause
  61. *
  62. */
  63. #include "../include/gpsd_config.h" /* must be before all includes */
  64. #include <assert.h>
  65. #include <errno.h>
  66. #include <libgen.h>
  67. #include <math.h>
  68. #include <signal.h>
  69. #include <stdbool.h>
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <string.h>
  73. #include <sys/types.h> // for umask()
  74. #include <sys/stat.h> // for umask()
  75. #include <time.h>
  76. #include <unistd.h>
  77. #ifdef HAVE_GETOPT_LONG
  78. #include <getopt.h>
  79. #endif
  80. #include "../include/compiler.h"
  81. #include "../include/gps.h"
  82. #include "../include/gpsdclient.h"
  83. #include "../include/os_compat.h"
  84. #include "../include/timespec.h"
  85. static char *progname;
  86. static struct fixsource_t source;
  87. static double ecefx = 0.0;
  88. static double ecefy = 0.0;
  89. static double ecefz = 0.0;
  90. static timespec_t start_time = {0}; /* report gen time, UTC */
  91. static timespec_t first_mtime = {0}; /* GPS time, not UTC */
  92. static timespec_t last_mtime = {0}; /* GPS time, not UTC */
  93. static int leap_seconds = 0; // set if non-zero
  94. // strings for the RINEX file
  95. static char agency[41] = "Unknown";
  96. static char ant_num[21] = "0";
  97. static char ant_type[21] = "UNKNOWN EXT NONE";
  98. static double ant_e = 0.0;
  99. static double ant_h = 0.0;
  100. static double ant_n = 0.0;
  101. static char marker_name[61] = "XXXX";
  102. static char marker_type[61] = "NON_PHYSICAL";
  103. static char observer[21] = "Unknown";
  104. static char rec_num[21] = "0";
  105. static char rec_type[21] = "Unknown";
  106. static char rec_vers[21] = "0";
  107. /* total count of observations by u-blox gnssid [0-7]
  108. * 0 = GPS RINEX G
  109. * 1 = SBAS RINEX S
  110. * 2 = Galileo RINEX E
  111. * 3 - BeiDou RINEX C
  112. * 4 = IMES not supported by RINEX
  113. * 5 = QZSS RINEX J
  114. * 6 = GLONASS RINEX R
  115. * 7 = IRNSS RINEX I
  116. *
  117. * RINEX 3 observation codes [1]:
  118. * C1C L1 C/A Pseudorange
  119. * C1P L1 P Pseudorange
  120. * C1W L1 Z-tracking Pseudorange
  121. * D1C L1 C/A Doppler
  122. * L1C L1 C/A Carrier Phase
  123. * L1P L1 P Carrier Phase
  124. * L1W L1 Z-tracking Carrier Phase
  125. * C2C L2 C/A Pseudorange
  126. * C2P L2 P Pseudorange
  127. * C2W L2 Z-tracking Pseudorange
  128. * D2C L2 C/A Doppler
  129. * L2C L2 C/A Carrier phase
  130. * L2P L1 P Carrier Phase
  131. * L2W L2 Z-tracking Carrier Phase
  132. *
  133. * C2L L2C (L), Pseudo Range, BeiDou
  134. * D2L L2C (L), Doppler, BeiDou
  135. * L2L L2C (L), Carrier Phase, BeiDou
  136. *
  137. * L5I L5 I Pseudo Range
  138. * C5I L5 I Carrier Phase
  139. * D5I L5 I Doppler
  140. *
  141. * CSRS-PPP supports:
  142. * GPS: C1C L1C C2C L2C C1W L1W C2W L2W
  143. * GLONASS : C1C L1C C2C L2C C1P L1P C2P L2P
  144. *
  145. */
  146. typedef enum {C1C = 0, D1C, L1C,
  147. C2C, D2C, L2C,
  148. C2L, D2L, L2L,
  149. C5I, D5I, L5I,
  150. C7I, D7I, L7I,
  151. C7Q, D7Q, L7Q, CODEMAX} obs_codes;
  152. /* structure to hold count of observations by gnssid:svid
  153. * MAXCHANNEL+1 is just a WAG of max size */
  154. #define MAXCNT (MAXCHANNELS + 1)
  155. static struct obs_cnt_t {
  156. unsigned char gnssid;
  157. unsigned char svid; /* svid of 0 means unused slot */
  158. unsigned int obs_cnts[CODEMAX+1]; /* count of obscode */
  159. } obs_cnt[MAXCNT] = {{0}};
  160. static FILE * tmp_file; /* file handle for temp file */
  161. static int sample_count = 20; /* number of measurement sets to get */
  162. /* timespec_t between measurement sets */
  163. static timespec_t sample_interval_ts = {30, 0};
  164. /* milli-seconds between measurement sets */
  165. static unsigned sample_interval_ms = 30000;
  166. #define DEBUG_QUIET 0
  167. #define DEBUG_INFO 1
  168. #define DEBUG_PROG 2
  169. #define DEBUG_RAW 3
  170. static int debug = DEBUG_INFO; /* debug level */
  171. static struct gps_data_t gpsdata;
  172. static FILE *log_file;
  173. /* convert a u-blox/gpsd gnssid to the RINEX 3 constellation code
  174. * see [1] Section 3.5
  175. */
  176. static char gnssid2rinex(int gnssid)
  177. {
  178. switch (gnssid) {
  179. case GNSSID_GPS: /* 0 = GPS */
  180. return 'G';
  181. case GNSSID_SBAS: /* 1 = SBAS */
  182. return 'S';
  183. case GNSSID_GAL: /* 2 = Galileo */
  184. return 'E';
  185. case GNSSID_BD: /* 3 = BeiDou */
  186. return 'C';
  187. case GNSSID_IMES: /* 4 = IMES - unsupported */
  188. return 'X';
  189. case GNSSID_QZSS: /* 5 = QZSS */
  190. return 'J';
  191. case GNSSID_GLO: /* 6 = GLONASS */
  192. return 'R';
  193. case GNSSID_IRNSS: /* 7 = IRNSS */
  194. return 'I';
  195. default: /* Huh? */
  196. return 'x';
  197. }
  198. }
  199. /* obs_cnt_inc()
  200. *
  201. * increment an observation count
  202. */
  203. static void obs_cnt_inc(unsigned char gnssid, unsigned char svid,
  204. obs_codes obs_code)
  205. {
  206. int i;
  207. if (CODEMAX <= obs_code) {
  208. /* should never happen... */
  209. fprintf(stderr, "ERROR: obs_code_inc() obs_code %d out of range\n",
  210. obs_code);
  211. exit(1);
  212. }
  213. /* yeah, slow and ugly, linear search. */
  214. for (i = 0; i < MAXCNT; i++) {
  215. if (0 == obs_cnt[i].svid) {
  216. /* end of list, not found, so add this gnssid:svid */
  217. obs_cnt[i].gnssid = gnssid;
  218. obs_cnt[i].svid = svid;
  219. obs_cnt[i].obs_cnts[obs_code] = 1;
  220. break;
  221. }
  222. if (obs_cnt[i].gnssid != gnssid) {
  223. continue;
  224. }
  225. if (obs_cnt[i].svid != svid) {
  226. continue;
  227. }
  228. /* found it, increment it */
  229. obs_cnt[i].obs_cnts[obs_code]++;
  230. if (99999 < obs_cnt[i].obs_cnts[obs_code]) {
  231. /* RINEX 3 max is 99999 */
  232. obs_cnt[i].obs_cnts[obs_code] = 99999;
  233. }
  234. break;
  235. }
  236. /* fell out because table full, item added, or item incremented */
  237. return;
  238. }
  239. /* compare two obs_cnt, for sorting by gnssid, and svid */
  240. static int compare_obs_cnt(const void *A, const void *B)
  241. {
  242. const struct obs_cnt_t *a = (const struct obs_cnt_t *)A;
  243. const struct obs_cnt_t *b = (const struct obs_cnt_t *)B;
  244. unsigned char a_gnssid = a->gnssid;
  245. unsigned char b_gnssid = b->gnssid;
  246. /* 0 = svid means unused, make those last */
  247. if (0 == a->svid) {
  248. a_gnssid = 255;
  249. }
  250. if (0 == b->svid) {
  251. b_gnssid = 255;
  252. }
  253. if (a_gnssid != b_gnssid) {
  254. return a_gnssid - b_gnssid;
  255. }
  256. /* put unused last */
  257. if (a->svid != b->svid) {
  258. return a->svid - b->svid;
  259. }
  260. /* two blank records */
  261. return 0;
  262. }
  263. /* return number of unique PRN in a gnssid from obs_cnt.
  264. * return all PRNs if 255 == gnssid */
  265. static int obs_cnt_prns(unsigned char gnssid)
  266. {
  267. int i;
  268. int prn_cnt = 0;
  269. for (i = 0; i < MAXCNT; i++) {
  270. if (0 == obs_cnt[i].svid) {
  271. /* end of list, done */
  272. break;
  273. }
  274. if ((255 != gnssid) && (gnssid != obs_cnt[i].gnssid)) {
  275. /* wrong gnssid */
  276. continue;
  277. }
  278. prn_cnt++;
  279. }
  280. /* fell out because table full, item added, or item incremented */
  281. return prn_cnt;
  282. }
  283. /* print_rinex_header()
  284. * Print a RINEX 3 header to the file "log_file".
  285. * Some of the data in the header is only known after processing all
  286. * the raw data.
  287. */
  288. static void print_rinex_header(void)
  289. {
  290. int i, j;
  291. char tmstr[40]; /* time: yyyymmdd hhmmss UTC */
  292. struct tm *report_time;
  293. struct tm *first_time;
  294. struct tm *last_time;
  295. struct tm tm_buf; // temp buffer for gmtime_r()
  296. int prn_count[GNSSID_CNT] = {0}; /* count of PRN per gnssid */
  297. if (DEBUG_PROG <= debug) {
  298. (void)fprintf(stderr, "doing header\n");
  299. }
  300. report_time = gmtime_r(&(start_time.tv_sec), &tm_buf);
  301. (void)strftime(tmstr, sizeof(tmstr), "%Y%m%d %H%M%S UTC", report_time);
  302. (void)fprintf(log_file,
  303. "%9s%11s%-20s%-20s%-20s\n",
  304. "3.03", "", "OBSERVATION DATA", "M: Mixed", "RINEX VERSION / TYPE");
  305. (void)fprintf(log_file,
  306. "%-20s%-20s%-20s%-20s\n",
  307. "gpsrinex " VERSION, "", tmstr,
  308. "PGM / RUN BY / DATE");
  309. (void)fprintf(log_file, "%-60s%-20s\n",
  310. "Source: gpsd live data", "COMMENT");
  311. (void)fprintf(log_file, "%-60s%-20s\n", marker_name, "MARKER NAME");
  312. (void)fprintf(log_file, "%-60s%-20s\n", marker_type, "MARKER TYPE");
  313. (void)fprintf(log_file, "%-20s%-40s%-20s\n",
  314. observer, agency, "OBSERVER / AGENCY");
  315. (void)fprintf(log_file, "%-20s%-20s%-20s%-20s\n",
  316. rec_num, rec_type, rec_vers, "REC # / TYPE / VERS");
  317. (void)fprintf(log_file, "%-20s%-20s%-20s%-20s\n",
  318. ant_num, ant_type, "" , "ANT # / TYPE");
  319. if (isfinite(ecefx) &&
  320. isfinite(ecefy) &&
  321. isfinite(ecefz)) {
  322. (void)fprintf(log_file, "%14.4f%14.4f%14.4f%18s%-20s\n",
  323. ecefx, ecefy, ecefz, "", "APPROX POSITION XYZ");
  324. } else if (DEBUG_INFO <= debug) {
  325. (void)fprintf(stderr, "INFO: missing ECEF\n");
  326. }
  327. (void)fprintf(log_file, "%14.4f%14.4f%14.4f%18s%-20s\n",
  328. ant_h, ant_e, ant_n, "", "ANTENNA: DELTA H/E/N");
  329. (void)fprintf(log_file, "%6d%6d%48s%-20s\n", 1, 1,
  330. "", "WAVELENGTH FACT L1/2");
  331. /* get PRN stats */
  332. qsort(obs_cnt, MAXCNT, sizeof(struct obs_cnt_t), compare_obs_cnt);
  333. for (i = 0; i < GNSSID_CNT; i++ ) {
  334. prn_count[i] = obs_cnt_prns(i);
  335. }
  336. /* CSRS-PPP needs C1C, L1C or C1C, L1C, D1C
  337. * CSRS-PPP refuses files with L1C first
  338. * convbin wants C1C, L1C, D1C
  339. * for some reason gfzrnx_lx wants C1C, D1C, L1C, not C1C, L1C, D1C */
  340. if (0 < prn_count[GNSSID_GPS]) {
  341. /* GPS, code G */
  342. (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
  343. gnssid2rinex(GNSSID_GPS), 6, "C1C", "L1C", "D1C", "C2C", "L2C",
  344. "D2C", "", "", "", "SYS / # / OBS TYPES");
  345. }
  346. if (0 < prn_count[GNSSID_SBAS]) {
  347. /* SBAS, L1 and L5 only, code S */
  348. (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
  349. gnssid2rinex(GNSSID_SBAS), 3, "C1C", "L1C", "D1C", "", "", "",
  350. "", "", "", "SYS / # / OBS TYPES");
  351. }
  352. if (0 < prn_count[GNSSID_GAL]) {
  353. /* Galileo, E1, E5 aand E6 only, code E */
  354. (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
  355. gnssid2rinex(GNSSID_GAL), 6, "C1C", "L1C", "D1C", "C7Q",
  356. "L7Q", "D7Q", "", "", "", "SYS / # / OBS TYPES");
  357. }
  358. if (0 < prn_count[GNSSID_BD]) {
  359. /* BeiDou, BDS, code C */
  360. (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
  361. gnssid2rinex(GNSSID_BD), 6, "C1C", "L1C", "D1C", "C7I", "L7I",
  362. "D7I", "", "", "", "SYS / # / OBS TYPES");
  363. }
  364. if (0 < prn_count[GNSSID_QZSS]) {
  365. /* QZSS, code J */
  366. (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
  367. gnssid2rinex(GNSSID_QZSS), 6, "C1C", "L1C", "D1C", "C2L",
  368. "L2L", "D2L", "", "", "", "SYS / # / OBS TYPES");
  369. }
  370. if (0 < prn_count[GNSSID_GLO]) {
  371. /* GLONASS, R */
  372. (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
  373. gnssid2rinex(GNSSID_GLO), 6, "C1C", "L1C", "D1C", "C2C", "L2C",
  374. "D2C", "", "", "", "SYS / # / OBS TYPES");
  375. }
  376. // FIXME: Add IRNSS...
  377. (void)fprintf(log_file, "%6d%54s%-20s\n", obs_cnt_prns(255),
  378. "", "# OF SATELLITES");
  379. /* get all the PRN / # OF OBS */
  380. for (i = 0; i < MAXCNT; i++) {
  381. int cnt = 0; /* number of obs for one sat */
  382. if (0 == obs_cnt[i].svid) {
  383. /* done */
  384. break;
  385. }
  386. for (j = 0; j < CODEMAX; j++) {
  387. cnt += obs_cnt[i].obs_cnts[j];
  388. }
  389. if (0 > cnt) {
  390. /* no counts for this sat */
  391. continue;
  392. }
  393. switch (obs_cnt[i].gnssid) {
  394. case GNSSID_GPS:
  395. /* GPS, code G */
  396. (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
  397. gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
  398. obs_cnt[i].obs_cnts[C1C],
  399. obs_cnt[i].obs_cnts[L1C],
  400. obs_cnt[i].obs_cnts[D1C],
  401. obs_cnt[i].obs_cnts[C2C],
  402. obs_cnt[i].obs_cnts[L2C],
  403. obs_cnt[i].obs_cnts[D2C],
  404. "", "PRN / # OF OBS");
  405. break;
  406. case GNSSID_SBAS:
  407. /* SBAS, L1C and L5C, code S */
  408. (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
  409. gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
  410. obs_cnt[i].obs_cnts[C1C],
  411. obs_cnt[i].obs_cnts[L1C],
  412. obs_cnt[i].obs_cnts[D1C],
  413. obs_cnt[i].obs_cnts[C5I],
  414. obs_cnt[i].obs_cnts[L5I],
  415. obs_cnt[i].obs_cnts[D5I],
  416. "", "PRN / # OF OBS");
  417. break;
  418. case GNSSID_GAL:
  419. /* Galileo, code E */
  420. (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
  421. gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
  422. obs_cnt[i].obs_cnts[C1C],
  423. obs_cnt[i].obs_cnts[L1C],
  424. obs_cnt[i].obs_cnts[D1C],
  425. obs_cnt[i].obs_cnts[C7Q],
  426. obs_cnt[i].obs_cnts[L7Q],
  427. obs_cnt[i].obs_cnts[D7Q],
  428. "", "PRN / # OF OBS");
  429. break;
  430. case GNSSID_BD:
  431. /* BeiDou, code C */
  432. (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
  433. gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
  434. obs_cnt[i].obs_cnts[C1C],
  435. obs_cnt[i].obs_cnts[L1C],
  436. obs_cnt[i].obs_cnts[D1C],
  437. obs_cnt[i].obs_cnts[C7I],
  438. obs_cnt[i].obs_cnts[L7I],
  439. obs_cnt[i].obs_cnts[D7I],
  440. "", "PRN / # OF OBS");
  441. break;
  442. case GNSSID_QZSS:
  443. /* QZSS, code J */
  444. (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
  445. gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
  446. obs_cnt[i].obs_cnts[C1C],
  447. obs_cnt[i].obs_cnts[L1C],
  448. obs_cnt[i].obs_cnts[D1C],
  449. obs_cnt[i].obs_cnts[C2L],
  450. obs_cnt[i].obs_cnts[L2L],
  451. obs_cnt[i].obs_cnts[D2L],
  452. "", "PRN / # OF OBS");
  453. break;
  454. case GNSSID_GLO:
  455. /* GLONASS, code R */
  456. (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
  457. gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
  458. obs_cnt[i].obs_cnts[C1C],
  459. obs_cnt[i].obs_cnts[L1C],
  460. obs_cnt[i].obs_cnts[D1C],
  461. obs_cnt[i].obs_cnts[C2C],
  462. obs_cnt[i].obs_cnts[L2C],
  463. obs_cnt[i].obs_cnts[D2C],
  464. "", "PRN / # OF OBS");
  465. break;
  466. // FIXME: Add GNSSID_IRNSS, L5A
  467. default:
  468. (void)fprintf(log_file," %c%02d%6u%6u%6u%6s%6s%24s%-20s\n",
  469. gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
  470. obs_cnt[i].obs_cnts[C1C],
  471. obs_cnt[i].obs_cnts[L1C],
  472. obs_cnt[i].obs_cnts[D1C],
  473. "", "",
  474. "", "PRN / # OF OBS");
  475. }
  476. }
  477. (void)fprintf(log_file, "%-10s%50s%-20s\n",
  478. "DBHZ", "", "SIGNAL STRENGTH UNIT");
  479. (void)fprintf(log_file, "%10.3f%50s%-20s\n",
  480. (double)sample_interval_ms / 1000.0, "", "INTERVAL");
  481. /* GPS time not UTC */
  482. first_time = gmtime_r(&(first_mtime.tv_sec), &tm_buf);
  483. (void)fprintf(log_file, "%6d%6d%6d%6d%6d%5d.%07ld%8s%9s%-20s\n",
  484. first_time->tm_year + 1900,
  485. first_time->tm_mon + 1,
  486. first_time->tm_mday,
  487. first_time->tm_hour,
  488. first_time->tm_min,
  489. first_time->tm_sec,
  490. (long)(first_mtime.tv_nsec / 100),
  491. "GPS", "",
  492. "TIME OF FIRST OBS");
  493. /* GPS time not UTC */
  494. last_time = gmtime_r(&(last_mtime.tv_sec), &tm_buf);
  495. (void)fprintf(log_file, "%6d%6d%6d%6d%6d%5d.%07ld%8s%9s%-20s\n",
  496. last_time->tm_year + 1900,
  497. last_time->tm_mon + 1,
  498. last_time->tm_mday,
  499. last_time->tm_hour,
  500. last_time->tm_min,
  501. last_time->tm_sec,
  502. (long)(last_mtime.tv_nsec / 100),
  503. "GPS", "",
  504. "TIME OF LAST OBS");
  505. if (0 < prn_count[GNSSID_GPS]) {
  506. /* GPS, code G */
  507. (void)fprintf(log_file, "%-60s%-20s\n",
  508. "G L1C", "SYS / PHASE SHIFT");
  509. (void)fprintf(log_file, "%-60s%-20s\n",
  510. "G L2C", "SYS / PHASE SHIFT");
  511. }
  512. if (0 < prn_count[GNSSID_SBAS]) {
  513. /* SBAS, L1 and L5 only, code S */
  514. (void)fprintf(log_file, "%-60s%-20s\n",
  515. "S L1C", "SYS / PHASE SHIFT");
  516. (void)fprintf(log_file, "%-60s%-20s\n",
  517. "E L5Q", "SYS / PHASE SHIFT");
  518. }
  519. if (0 < prn_count[GNSSID_GAL]) {
  520. /* GALILEO, E1, E5 and E6, code E */
  521. (void)fprintf(log_file, "%-60s%-20s\n",
  522. "E L1C", "SYS / PHASE SHIFT");
  523. (void)fprintf(log_file, "%-60s%-20s\n",
  524. "E L7Q", "SYS / PHASE SHIFT");
  525. }
  526. if (0 < prn_count[GNSSID_BD]) {
  527. /* BeiDou, code C */
  528. (void)fprintf(log_file, "%-60s%-20s\n",
  529. "B L1C", "SYS / PHASE SHIFT");
  530. (void)fprintf(log_file, "%-60s%-20s\n",
  531. "B L7I", "SYS / PHASE SHIFT");
  532. }
  533. if (0 < prn_count[GNSSID_QZSS]) {
  534. /* QZSS, code J */
  535. (void)fprintf(log_file, "%-60s%-20s\n",
  536. "J L1C", "SYS / PHASE SHIFT");
  537. (void)fprintf(log_file, "%-60s%-20s\n",
  538. "J L2L", "SYS / PHASE SHIFT");
  539. }
  540. if (0 < prn_count[GNSSID_GLO]) {
  541. /* GLONASS, code R */
  542. (void)fprintf(log_file, "%-60s%-20s\n",
  543. "R L1C", "SYS / PHASE SHIFT");
  544. (void)fprintf(log_file, "%-60s%-20s\n",
  545. "R L2C", "SYS / PHASE SHIFT");
  546. }
  547. (void)fprintf(log_file, "%-60s%-20s\n",
  548. "", "END OF HEADER");
  549. if (DEBUG_PROG <= debug) {
  550. (void)fprintf(stderr,"done header\n");
  551. }
  552. return;
  553. }
  554. /* print_rinex_footer()
  555. * print a RINEX 3 footer to the file "log_file".
  556. * Except RINEX 3 has no footer. So what this really does is
  557. * call the header function, then move the processed observations from
  558. * "tmp_file" to "log_file".
  559. */
  560. static void print_rinex_footer(void)
  561. {
  562. char buffer[4096];
  563. /* print the header */
  564. print_rinex_header();
  565. /* now replay the data in the tmp_file into the output */
  566. (void)fflush(tmp_file);
  567. rewind(tmp_file);
  568. while (true) {
  569. size_t count;
  570. count = fread(buffer, 1, sizeof(buffer), tmp_file);
  571. if (0 == count ) {
  572. // nothing read, or read error
  573. break;
  574. }
  575. (void)fwrite(buffer, 1, count, log_file);
  576. }
  577. (void)fclose(tmp_file);
  578. (void)fclose(log_file);
  579. (void)gps_close(&gpsdata);
  580. }
  581. /* compare two meas_t, for sorting by gnssid, svid, and sigid */
  582. static int compare_meas(const void *A, const void *B)
  583. {
  584. const struct meas_t *a = (const struct meas_t*)A;
  585. const struct meas_t *b = (const struct meas_t*)B;
  586. if (a->gnssid != b->gnssid) {
  587. return a->gnssid - b->gnssid;
  588. }
  589. if (a->svid != b->svid) {
  590. return a->svid - b->svid;
  591. }
  592. if (a->sigid != b->sigid) {
  593. return a->sigid - b->sigid;
  594. }
  595. /* two blank records */
  596. return 0;
  597. }
  598. /* convert an observation item and return it as a (F14,3,I1,I1)
  599. * in a static buffer */
  600. static const char * fmt_obs(double val, unsigned char lli, unsigned char snr)
  601. {
  602. static char buf[20];
  603. char lli_c; /* set zero lli to blank */
  604. char snr_c; /* set zero snr to blank */
  605. if (!isfinite(val)) {
  606. /* bad value, return 16 blanks */
  607. return " ";
  608. }
  609. switch (lli) {
  610. case 0:
  611. default:
  612. lli_c = ' ';
  613. break;
  614. case 1:
  615. lli_c = '1';
  616. break;
  617. case 2:
  618. lli_c = '2';
  619. break;
  620. case 3:
  621. lli_c = '3';
  622. break;
  623. }
  624. if ((1 > snr) || (9 < snr)) {
  625. snr_c = ' ';
  626. } else {
  627. snr_c = 48 + snr;
  628. }
  629. (void)snprintf(buf, sizeof(buf), "%14.3f%c%1c", val, lli_c, snr_c);
  630. return buf;
  631. }
  632. /* one_sig() - print one signal
  633. *
  634. * one CxC s LxC DxC
  635. */
  636. static void one_sig(struct meas_t *meas)
  637. {
  638. unsigned char snr;
  639. unsigned gnssid = meas->gnssid;
  640. unsigned svid = meas->svid;
  641. unsigned sigid = meas->sigid;
  642. obs_codes cxx = C1C;
  643. obs_codes lxx = L1C;
  644. obs_codes dxx = D1C;
  645. if (DEBUG_PROG <= debug) {
  646. (void)fprintf(stderr, "INFO: one_sig() %c %u:%u:%u\n",
  647. gnssid2rinex(gnssid),
  648. gnssid, svid, sigid);
  649. }
  650. switch (sigid) {
  651. default:
  652. (void)fprintf(stderr, "ERROR: one_sig() gnmssid %u unknown sigid %u\n",
  653. gnssid, sigid);
  654. FALLTHROUGH
  655. case 0:
  656. /* L1C */
  657. cxx = C1C;
  658. lxx = L1C;
  659. dxx = D1C;
  660. break;
  661. case 2:
  662. /* GLONASS L2 OF or BeiDou B2I D1 */
  663. if (GNSSID_BD == gnssid) {
  664. /* WAG */
  665. cxx = C7I;
  666. lxx = L7I;
  667. dxx = D7I;
  668. } else {
  669. cxx = C2C;
  670. lxx = L2C;
  671. dxx = D2C;
  672. }
  673. break;
  674. case 3:
  675. /* GPS L2 or BD B2I D2 */
  676. cxx = C2C;
  677. lxx = L2C;
  678. dxx = D2C;
  679. break;
  680. case 5:
  681. /* QZSS L2C (L) */
  682. cxx = C2L;
  683. lxx = L2L;
  684. dxx = D2L;
  685. break;
  686. case 6:
  687. /* Galileo E5 bQ */
  688. cxx = C7Q;
  689. lxx = L7Q;
  690. dxx = D7Q;
  691. break;
  692. }
  693. /* map snr to RINEX snr flag [1-9] */
  694. if (0 == meas->snr) {
  695. snr = 0;
  696. } else if (12 > meas->snr) {
  697. snr = 1;
  698. } else if (18 >= meas->snr) {
  699. snr = 2;
  700. } else if (23 >= meas->snr) {
  701. snr = 3;
  702. } else if (29 >= meas->snr) {
  703. snr = 4;
  704. } else if (35 >= meas->snr) {
  705. snr = 5;
  706. } else if (41 >= meas->snr) {
  707. snr = 6;
  708. } else if (47 >= meas->snr) {
  709. snr = 7;
  710. } else if (53 >= meas->snr) {
  711. snr = 8;
  712. } else {
  713. /* snr >= 54 */
  714. snr = 9;
  715. }
  716. /* check for slip
  717. * FIXME: use actual interval
  718. * locktime is in milliseconds
  719. * sample_interval_ms is milli seconds */
  720. if (meas->locktime < sample_interval_ms) {
  721. meas->lli |= 2;
  722. }
  723. if (0 != isfinite(meas->pseudorange)) {
  724. obs_cnt_inc(gnssid, svid, cxx);
  725. }
  726. if (0 != isfinite(meas->carrierphase)) {
  727. obs_cnt_inc(gnssid, svid, lxx);
  728. }
  729. if (0 != isfinite(meas->doppler)) {
  730. obs_cnt_inc(gnssid, svid, dxx);
  731. }
  732. (void)fputs(fmt_obs(meas->pseudorange, 0, 0), tmp_file);
  733. // putting snr here, with phase, is deprecated.
  734. // it should be an S observation.
  735. (void)fputs(fmt_obs(meas->carrierphase, meas->lli, snr), tmp_file);
  736. (void)fputs(fmt_obs(meas->doppler, 0, 0), tmp_file);
  737. }
  738. /* print_raw()
  739. * print one epoch of observations into "tmp_file"
  740. */
  741. static void print_raw(struct gps_data_t *gpsdata)
  742. {
  743. struct tm *now_time;
  744. struct tm tm_buf; // temp buffer for gmtime_r()
  745. unsigned nrec = 0;
  746. unsigned nsat = 0;
  747. unsigned i;
  748. unsigned char last_gnssid = 0;
  749. unsigned char last_svid = 0;
  750. int need_nl = 0;
  751. int got_l1 = 0;
  752. timespec_t interval_ts;
  753. TS_SUB(&interval_ts, &gpsdata->raw.mtime, &last_mtime);
  754. if (!TS_GE(&interval_ts, &sample_interval_ts)) {
  755. /* not time yet */
  756. return;
  757. }
  758. #ifdef __UNUSED
  759. fprintf(stderr, "sample: %ld %ld\n", (long)sample_interval_ts.tv_sec,
  760. (long)sample_interval_ts.tv_nsec);
  761. fprintf(stderr, "epoch: %ld %ld\n", (long)gpsdata->raw.mtime.tv_sec,
  762. (long)gpsdata->raw.mtime.tv_nsec);
  763. #endif // __UNUSED
  764. // do modulo only for sample_interval of even seconds
  765. if (0 == sample_interval_ts.tv_nsec &&
  766. 0 < sample_interval_ts.tv_sec) {
  767. time_t epoch_sec = gpsdata->raw.mtime.tv_sec;
  768. if (500000000 < gpsdata->raw.mtime.tv_nsec) {
  769. // round it up. To match convbin.
  770. // does this break opus?
  771. epoch_sec++;
  772. }
  773. /* opus insists (time % interval) = 0 */
  774. if (0 != (epoch_sec % sample_interval_ts.tv_sec)) {
  775. return;
  776. }
  777. }
  778. /* RINEX 3 wants records in each epoch sorted by gnssid.
  779. * To look nice: sort by gnssid and svid
  780. * To work nice, sort by gnssid, svid and sigid.
  781. * Each sigid is one record in RAW, but all sigid is one
  782. * record in RINEX
  783. */
  784. /* go through list three times, first just to get a count for sort */
  785. for (i = 0; i < MAXCHANNELS; i++) {
  786. if (0 == gpsdata->raw.meas[i].svid) {
  787. /* bad svid, end of list */
  788. break;
  789. }
  790. nrec++;
  791. }
  792. if (0 == nrec) {
  793. /* nothing to do */
  794. return;
  795. }
  796. qsort(gpsdata->raw.meas, nrec, sizeof(gpsdata->raw.meas[0]),
  797. compare_meas);
  798. /* second just to get a count, needed for epoch header */
  799. for (i = 0; i < nrec; i++) {
  800. if (0 == gpsdata->raw.meas[i].svid) {
  801. /* bad svid */
  802. continue;
  803. }
  804. if (4 == gpsdata->raw.meas[i].gnssid) {
  805. /* skip IMES */
  806. continue;
  807. }
  808. if (GNSSID_CNT <= gpsdata->raw.meas[i].gnssid) {
  809. /* invalid gnssid */
  810. continue;
  811. }
  812. /* prevent separate sigid from double counting gnssid:svid */
  813. if ((last_gnssid == gpsdata->raw.meas[i].gnssid) &&
  814. (last_svid == gpsdata->raw.meas[i].svid)) {
  815. /* duplicate sat */
  816. continue;
  817. }
  818. last_gnssid = gpsdata->raw.meas[i].gnssid;
  819. last_svid = gpsdata->raw.meas[i].svid;
  820. nsat++;
  821. }
  822. if (0 == nsat) {
  823. /* nothing to do */
  824. return;
  825. }
  826. /* save time of last measurement, GPS time, not UTC */
  827. last_mtime = gpsdata->raw.mtime; /* structure copy */
  828. if (0 == first_mtime.tv_sec) {
  829. /* save time of first measurement */
  830. first_mtime = last_mtime; /* structure copy */
  831. }
  832. /* print epoch header line */
  833. now_time = gmtime_r(&(last_mtime.tv_sec), &tm_buf);
  834. (void)fprintf(tmp_file,"> %4d %02d %02d %02d %02d %02d.%07ld 0%3u\n",
  835. now_time->tm_year + 1900,
  836. now_time->tm_mon + 1,
  837. now_time->tm_mday,
  838. now_time->tm_hour,
  839. now_time->tm_min,
  840. now_time->tm_sec,
  841. (long)(last_mtime.tv_nsec / 100), nsat);
  842. last_gnssid = 0;
  843. last_svid = 0;
  844. need_nl = 0;
  845. got_l1 = 0;
  846. /* Print the observations, one gnssid:svid per line.
  847. * The fun is merging consecutive records (new sigid) of
  848. * same gnssid:svid */
  849. for (i = 0; i < nrec; i++) {
  850. char rinex_gnssid;
  851. unsigned char gnssid;
  852. unsigned char svid;
  853. unsigned char sigid;
  854. gnssid = gpsdata->raw.meas[i].gnssid;
  855. rinex_gnssid = gnssid2rinex(gnssid);
  856. svid = gpsdata->raw.meas[i].svid;
  857. sigid = gpsdata->raw.meas[i].sigid;
  858. if (DEBUG_RAW <= debug) {
  859. (void)fprintf(stderr,"record: %u:%u:%u %s\n",
  860. gnssid, svid, sigid,
  861. gpsdata->raw.meas[i].obs_code);
  862. }
  863. if (0 == gpsdata->raw.meas[i].svid) {
  864. /* should not happen... */
  865. continue;
  866. }
  867. /* line can be longer than 80 chars in RINEX 3 */
  868. if ((last_gnssid != gpsdata->raw.meas[i].gnssid) ||
  869. (last_svid != gpsdata->raw.meas[i].svid)) {
  870. if (0 != need_nl) {
  871. (void)fputs("\n", tmp_file);
  872. }
  873. got_l1 = 0;
  874. /* new record line gnssid:svid preamble */
  875. (void)fprintf(tmp_file,"%c%02d", rinex_gnssid, svid);
  876. }
  877. last_gnssid = gpsdata->raw.meas[i].gnssid;
  878. last_svid = gpsdata->raw.meas[i].svid;
  879. /* L1x */
  880. switch (gpsdata->raw.meas[i].sigid) {
  881. case 0:
  882. /* L1 */
  883. one_sig(&gpsdata->raw.meas[i]);
  884. got_l1 = 1;
  885. break;
  886. case 2:
  887. /* GLONASS L2 OF or BD B2I D1 */
  888. if (0 == got_l1) {
  889. /* space to start of L2 */
  890. (void)fprintf(tmp_file, "%48s", "");
  891. }
  892. one_sig(&gpsdata->raw.meas[i]);
  893. break;
  894. case 3:
  895. /* GPS L2 or BD B2I D2 */
  896. if (0 == got_l1) {
  897. /* space to start of L2 */
  898. (void)fprintf(tmp_file, "%48s", "");
  899. }
  900. one_sig(&gpsdata->raw.meas[i]);
  901. break;
  902. case 5:
  903. /* QZSS L2C (L) */
  904. if (0 == got_l1) {
  905. /* space to start of L2 */
  906. (void)fprintf(tmp_file, "%48s", "");
  907. }
  908. one_sig(&gpsdata->raw.meas[i]);
  909. break;
  910. case 6:
  911. /* Galileo E5 bQ */
  912. if (0 == got_l1) {
  913. /* space to start of L2 */
  914. (void)fprintf(tmp_file, "%48s", "");
  915. }
  916. one_sig(&gpsdata->raw.meas[i]);
  917. break;
  918. default:
  919. (void)fprintf(stderr,
  920. "ERROR: print_raw() gnssid %u unknown sigid %u\n",
  921. gnssid, sigid);
  922. break;
  923. }
  924. need_nl = 1;
  925. }
  926. if (0 != need_nl) {
  927. (void)fputs("\n", tmp_file);
  928. }
  929. sample_count--;
  930. }
  931. static int sig_flag = 0;
  932. static void quit_handler(int signum)
  933. {
  934. // CWE-479: Signal Handler Use of a Non-reentrant Function
  935. // See: The C Standard, 7.14.1.1, paragraph 5 [ISO/IEC 9899:2011]
  936. // Can't log in a signal handler. Can't even call exit().
  937. sig_flag = signum;
  938. return;
  939. }
  940. /* conditionally_log_fix()
  941. * take the new gpsdata and decide what to do with it.
  942. */
  943. static void conditionally_log_fix(struct gps_data_t *gpsdata)
  944. {
  945. if (0 == leap_seconds && 0 < gpsdata->leap_seconds) {
  946. // grab a static copy of the current leap second.
  947. leap_seconds = gpsdata->leap_seconds;
  948. }
  949. if (DEBUG_PROG <= debug) {
  950. /* The (long long unsigned) is for 32/64-bit compatibility */
  951. (void)fprintf(stderr, "mode %d set %llx leap %d\n",
  952. gpsdata->fix.mode,
  953. (long long unsigned)gpsdata->set,
  954. leap_seconds);
  955. }
  956. if (0 == leap_seconds) {
  957. // Can't do anything until we know the current leap second
  958. return;
  959. }
  960. /* mostly we don't care if 2D or 3D fix, let the post processor
  961. * decide */
  962. if (MODE_2D < gpsdata->fix.mode) {
  963. /* got a good 3D fix */
  964. if (1.0 > ecefx &&
  965. isfinite(gpsdata->fix.ecef.x) &&
  966. isfinite(gpsdata->fix.ecef.y) &&
  967. isfinite(gpsdata->fix.ecef.z)) {
  968. /* save ecef for "APPROX POS" */
  969. ecefx = gpsdata->fix.ecef.x;
  970. ecefy = gpsdata->fix.ecef.y;
  971. ecefz = gpsdata->fix.ecef.z;
  972. if (DEBUG_PROG <= debug) {
  973. (void)fprintf(stderr,"got ECEF\n");
  974. }
  975. }
  976. }
  977. if (RAW_SET & gpsdata->set) {
  978. if (DEBUG_RAW <= debug) {
  979. (void)fprintf(stderr,"got RAW\n");
  980. }
  981. /* RINEX 3 prefers GPS time. Accepts GLO (UTC) time.
  982. * NRCan does not accept GLO time
  983. * Remove the leap second to get GPS from UTC.
  984. */
  985. gpsdata->raw.mtime.tv_sec += leap_seconds;
  986. print_raw(gpsdata);
  987. }
  988. return;
  989. }
  990. /* usage()
  991. * print usages, and exit
  992. */
  993. static void usage(void)
  994. {
  995. (void)fprintf(stderr,
  996. "Usage: %s [OPTIONS] [server[:port:[device]]]\n"
  997. "\n"
  998. "Mandatory arguments to long options are mandatory for "
  999. "short options too.\n"
  1000. " -D, --debug LVL Set debug level, default 0\n"
  1001. " -f FILE, --fileout FILE Output to filename\n"
  1002. " default: gpsrinexYYYYDDDDHHMM.obs\n"
  1003. " -h, --help print this usage and exit\n"
  1004. " -i SEC, --interval SEC Time between samples in seconds\n"
  1005. " default: %0.3f\n"
  1006. " -n COUNT, --count COUNT Number samples to collect\n"
  1007. " default: %d\n"
  1008. " -V, --version print version and exit\n"
  1009. "\nThese strings get placed in the generated RINEX 3 obs file\n"
  1010. " --agency AGENCY agency\n"
  1011. " --ant_e EASTING antenna easting in meters\n"
  1012. " --ant_h HEIGHT antenna height in meters\n"
  1013. " --ant_n NORTHING antenna northing in meters\n"
  1014. " --ant_num NUM antenna number\n"
  1015. " --ant_type TYPE antenna type\n"
  1016. " --marker_name NAME marker name\n"
  1017. " --marker_type TYPE marker type\n"
  1018. " --observer OBSERVER observer\n"
  1019. " --rec_num NUM receiver number\n"
  1020. " --rec_type TYPE receiver type\n"
  1021. " --rec_vers VERS receiver vers\n"
  1022. "\n"
  1023. "defaults to '%s -n %d -i %0.3f localhost:2947'\n",
  1024. progname, (double)sample_interval_ms / 1000.0, sample_count, progname,
  1025. sample_count, (double)sample_interval_ms / 1000.0);
  1026. exit(EXIT_FAILURE);
  1027. }
  1028. // defines for getopt_long()
  1029. #define AGENCY 301
  1030. #define ANT_E 302
  1031. #define ANT_H 303
  1032. #define ANT_N 304
  1033. #define ANT_NUM 305
  1034. #define ANT_TYPE 306
  1035. #define MARKER_NAME 307
  1036. #define MARKER_TYPE 308
  1037. #define OBSERVER 309
  1038. #define REC_NUM 310
  1039. #define REC_TYPE 311
  1040. #define REC_VERS 312
  1041. /*
  1042. *
  1043. * Main
  1044. *
  1045. */
  1046. int main(int argc, char **argv)
  1047. {
  1048. char tmstr[40]; // time: YYYYDDDMMHH
  1049. char tmp_fname[32]; // temp file name, for mkstemp
  1050. int tmp_file_desc; // temp file descriptor
  1051. struct tm *report_time;
  1052. struct tm tm_buf; // temp buffer for gmtime_r()
  1053. unsigned int flags = WATCH_ENABLE;
  1054. char *fname = NULL;
  1055. int timeout = 10;
  1056. double f;
  1057. progname = argv[0];
  1058. log_file = stdout;
  1059. while (1) {
  1060. int ch;
  1061. const char *optstring = "?D:f:hi:n:V";
  1062. #ifdef HAVE_GETOPT_LONG
  1063. int option_index = 0;
  1064. static struct option long_options[] = {
  1065. {"agency", required_argument, NULL, AGENCY},
  1066. {"ant_num", required_argument, NULL, ANT_NUM},
  1067. {"ant_type", required_argument, NULL, ANT_TYPE},
  1068. {"ant_e", required_argument, NULL, ANT_E},
  1069. {"ant_h", required_argument, NULL, ANT_H},
  1070. {"ant_n", required_argument, NULL, ANT_N},
  1071. {"count", required_argument, NULL, 'n' },
  1072. {"debug", required_argument, NULL, 'D' },
  1073. {"fileout", required_argument, NULL, 'f' },
  1074. {"help", no_argument, NULL, 'h' },
  1075. {"interval", required_argument, NULL, 'i' },
  1076. {"marker_name", required_argument, NULL, MARKER_NAME},
  1077. {"marker_type", required_argument, NULL, MARKER_TYPE},
  1078. {"observer", required_argument, NULL, OBSERVER},
  1079. {"rec_num", required_argument, NULL, REC_NUM},
  1080. {"rec_type", required_argument, NULL, REC_TYPE},
  1081. {"rec_vers", required_argument, NULL, REC_VERS},
  1082. {"version", no_argument, NULL, 'V' },
  1083. {NULL, 0, NULL, 0},
  1084. };
  1085. ch = getopt_long(argc, argv, optstring, long_options, &option_index);
  1086. #else
  1087. ch = getopt(argc, argv, optstring);
  1088. #endif
  1089. if (ch == -1) {
  1090. break;
  1091. }
  1092. switch (ch) {
  1093. case 'D':
  1094. debug = atoi(optarg);
  1095. gps_enable_debug(debug, log_file);
  1096. break;
  1097. case 'f': /* Output file name. */
  1098. fname = strdup(optarg);
  1099. break;
  1100. case 'i': /* set sampling interval */
  1101. f = safe_atof(optarg); // still in seconds
  1102. if (3600.0 <= f) {
  1103. (void)fprintf(stderr,
  1104. "WARNING: sample interval is an hour or more!\n");
  1105. }
  1106. sample_interval_ms = (unsigned)(1000 * f); // now in ms
  1107. if (0 == sample_interval_ms) {
  1108. // underflow
  1109. sample_interval_ms = 1;
  1110. }
  1111. MSTOTS(&sample_interval_ts, sample_interval_ms);
  1112. break;
  1113. case 'n':
  1114. sample_count = atoi(optarg);
  1115. break;
  1116. case 'V':
  1117. (void)fprintf(stderr, "%s: version %s (revision %s)\n",
  1118. progname, VERSION, REVISION);
  1119. exit(EXIT_SUCCESS);
  1120. case AGENCY:
  1121. strlcpy(agency, optarg, sizeof(agency));
  1122. break;
  1123. case ANT_E:
  1124. ant_e = safe_atof(optarg);
  1125. break;
  1126. case ANT_H:
  1127. ant_h = safe_atof(optarg);
  1128. break;
  1129. case ANT_N:
  1130. ant_n = safe_atof(optarg);
  1131. break;
  1132. case ANT_NUM:
  1133. strlcpy(ant_num, optarg, sizeof(ant_num));
  1134. break;
  1135. case ANT_TYPE:
  1136. strlcpy(ant_type, optarg, sizeof(ant_type));
  1137. break;
  1138. case MARKER_NAME:
  1139. strlcpy(marker_name, optarg, sizeof(marker_name));
  1140. break;
  1141. case MARKER_TYPE:
  1142. strlcpy(marker_type, optarg, sizeof(marker_type));
  1143. break;
  1144. case OBSERVER:
  1145. strlcpy(observer, optarg, sizeof(observer));
  1146. break;
  1147. case REC_NUM:
  1148. strlcpy(rec_num, optarg, sizeof(rec_num));
  1149. break;
  1150. case REC_TYPE:
  1151. strlcpy(rec_type, optarg, sizeof(rec_type));
  1152. break;
  1153. case REC_VERS:
  1154. strlcpy(rec_vers, optarg, sizeof(rec_vers));
  1155. break;
  1156. case '?':
  1157. FALLTHROUGH
  1158. case 'h':
  1159. FALLTHROUGH
  1160. default:
  1161. usage();
  1162. /* NOTREACHED */
  1163. }
  1164. }
  1165. /* init source defaults */
  1166. source.server = (char *)"localhost";
  1167. source.port = (char *)DEFAULT_GPSD_PORT;
  1168. source.device = NULL;
  1169. if (optind < argc) {
  1170. /* in this case, switch to the method "socket" always */
  1171. gpsd_source_spec(argv[optind], &source);
  1172. }
  1173. if (DEBUG_INFO <= debug) {
  1174. char *device;
  1175. if (NULL == source.device) {
  1176. device = "Default";
  1177. } else {
  1178. device = source.device;
  1179. }
  1180. (void)fprintf(stderr, "INFO: server: %s port: %s device: %s\n",
  1181. source.server, source.port, device);
  1182. }
  1183. /* save start time of report */
  1184. (void)clock_gettime(CLOCK_REALTIME, &start_time);
  1185. report_time = gmtime_r(&(start_time.tv_sec), &tm_buf);
  1186. /* open the output file */
  1187. if (NULL == fname) {
  1188. (void)strftime(tmstr, sizeof(tmstr), "gpsrinex%Y%j%H%M%S.obs",
  1189. report_time);
  1190. fname = tmstr;
  1191. }
  1192. log_file = fopen(fname, "w");
  1193. if (log_file == NULL) {
  1194. syslog(LOG_ERR, "ERROR: Failed to open %s: %s",
  1195. fname, strerror(errno));
  1196. exit(3);
  1197. }
  1198. /* clear the counts */
  1199. memset(obs_cnt, 0, sizeof(obs_cnt));
  1200. /* catch all interesting signals */
  1201. (void)signal(SIGTERM, quit_handler);
  1202. (void)signal(SIGQUIT, quit_handler);
  1203. (void)signal(SIGINT, quit_handler);
  1204. if (gps_open(source.server, source.port, &gpsdata) != 0) {
  1205. (void)fprintf(stderr, "%s: no gpsd running or network error: %d, %s\n",
  1206. progname, errno, gps_errstr(errno));
  1207. exit(EXIT_FAILURE);
  1208. }
  1209. if (source.device != NULL)
  1210. flags |= WATCH_DEVICE;
  1211. (void)gps_stream(&gpsdata, flags, source.device);
  1212. // create temp file, coverity does not like tmpfile()
  1213. // covarfity wants a umask
  1214. (void)umask(0177); // force rw-r--r--
  1215. strlcpy(tmp_fname, "/tmp/gpsrinexXXXXXX", sizeof(tmp_fname));
  1216. tmp_file_desc = mkstemp(tmp_fname);
  1217. if (0 > tmp_file_desc) {
  1218. (void)fprintf(stderr, "ERROR: mkstemp(%s) failed: %s\n",
  1219. tmp_fname, strerror(errno));
  1220. exit(2);
  1221. }
  1222. tmp_file = fdopen(tmp_file_desc, "w+");
  1223. if (NULL == tmp_file) {
  1224. (void)fprintf(stderr, "ERROR: fdopen() failed: %s\n",
  1225. strerror(errno));
  1226. exit(2);
  1227. }
  1228. for (;;) {
  1229. if (0 != sig_flag) {
  1230. break;
  1231. }
  1232. /* wait for gpsd */
  1233. if (!gps_waiting(&gpsdata, timeout * 1000000)) {
  1234. (void)fprintf(stderr, "gpsrinex: timeout\n");
  1235. syslog(LOG_INFO, "timeout;");
  1236. break;
  1237. }
  1238. if (0 != sig_flag) {
  1239. break;
  1240. }
  1241. (void)gps_read(&gpsdata, NULL, 0);
  1242. if (ERROR_SET & gpsdata.set) {
  1243. fprintf(stderr, "gps_read() error '%s'\n", gpsdata.error);
  1244. exit(6);
  1245. }
  1246. if (0 != sig_flag) {
  1247. break;
  1248. }
  1249. conditionally_log_fix(&gpsdata);
  1250. if (0 >= sample_count) {
  1251. /* done */
  1252. break;
  1253. }
  1254. }
  1255. print_rinex_footer();
  1256. // remove the temp file
  1257. (void)unlink(tmp_fname);
  1258. if (0 != sig_flag && SIGINT != sig_flag) {
  1259. syslog(LOG_INFO, "exiting, signal %d received", sig_flag);
  1260. }
  1261. exit(EXIT_SUCCESS);
  1262. }
  1263. // vim: set expandtab shiftwidth=4