12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148 |
- #include "gpsd_config.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <math.h>
- #include <time.h>
- #include <errno.h>
- #include <libgen.h>
- #include <signal.h>
- #include <assert.h>
- #include <unistd.h>
- #include "gps.h"
- #include "gpsdclient.h"
- #include "revision.h"
- #include "os_compat.h"
- static char *progname;
- static struct fixsource_t source;
- static double ecefx = 0.0;
- static double ecefy = 0.0;
- static double ecefz = 0.0;
- static timespec_t start_time = {0};
- static timespec_t first_mtime = {0};
- static timespec_t last_mtime = {0};
- typedef enum {C1C = 0, D1C, L1C,
- C2C, D2C, L2C,
- C2L, D2L, L2L,
- C5I, D5I, L5I,
- C7I, D7I, L7I,
- C7Q, D7Q, L7Q, CODEMAX} obs_codes;
- #define MAXCNT (MAXCHANNELS + 1)
- static struct obs_cnt_t {
- unsigned char gnssid;
- unsigned char svid;
- unsigned int obs_cnts[CODEMAX+1];
- } obs_cnt[MAXCNT] = {{0}};
- static FILE * tmp_file;
- static int sample_count = 20;
- static unsigned int sample_interval = 30;
- #define DEBUG_QUIET 0
- #define DEBUG_INFO 1
- #define DEBUG_PROG 2
- #define DEBUG_RAW 3
- static int debug = DEBUG_INFO;
- static struct gps_data_t gpsdata;
- static FILE *log_file;
- static char gnssid2rinex(int gnssid)
- {
- switch (gnssid) {
- case GNSSID_GPS:
- return 'G';
- case GNSSID_SBAS:
- return 'S';
- case GNSSID_GAL:
- return 'E';
- case GNSSID_BD:
- return 'C';
- case GNSSID_IMES:
- return 'X';
- case GNSSID_QZSS:
- return 'J';
- case GNSSID_GLO:
- return 'R';
- case GNSSID_IRNSS:
- return 'I';
- default:
- return 'x';
- }
- }
- static void obs_cnt_inc(unsigned char gnssid, unsigned char svid,
- obs_codes obs_code)
- {
- int i;
- if (CODEMAX <= obs_code) {
-
- fprintf(stderr, "ERROR: obs_code_inc() obs_code %d out of range\n",
- obs_code);
- exit(1);
- }
-
- for (i = 0; i < MAXCNT; i++) {
- if (0 == obs_cnt[i].svid) {
-
- obs_cnt[i].gnssid = gnssid;
- obs_cnt[i].svid = svid;
- obs_cnt[i].obs_cnts[obs_code] = 1;
- break;
- }
- if (obs_cnt[i].gnssid != gnssid) {
- continue;
- }
- if (obs_cnt[i].svid != svid) {
- continue;
- }
-
- obs_cnt[i].obs_cnts[obs_code]++;
- if (99999 < obs_cnt[i].obs_cnts[obs_code]) {
-
- obs_cnt[i].obs_cnts[obs_code] = 99999;
- }
- break;
- }
-
- return;
- }
- static int compare_obs_cnt(const void *A, const void *B)
- {
- const struct obs_cnt_t *a = (const struct obs_cnt_t *)A;
- const struct obs_cnt_t *b = (const struct obs_cnt_t *)B;
- unsigned char a_gnssid = a->gnssid;
- unsigned char b_gnssid = b->gnssid;
-
- if (0 == a->svid) {
- a_gnssid = 255;
- }
- if (0 == b->svid) {
- b_gnssid = 255;
- }
- if (a_gnssid != b_gnssid) {
- return a_gnssid - b_gnssid;
- }
-
- if (a->svid != b->svid) {
- return a->svid - b->svid;
- }
-
- return 0;
- }
- static int obs_cnt_prns(unsigned char gnssid)
- {
- int i;
- int prn_cnt = 0;
- for (i = 0; i < MAXCNT; i++) {
- if (0 == obs_cnt[i].svid) {
-
- break;
- }
- if ((255 != gnssid) && (gnssid != obs_cnt[i].gnssid)) {
-
- continue;
- }
- prn_cnt++;
- }
-
- return prn_cnt;
- }
- static void print_rinex_header(void)
- {
- int i, j;
- char tmstr[40];
- struct tm *report_time;
- struct tm *first_time;
- struct tm *last_time;
- int cnt;
- int prn_count[GNSSID_CNT] = {0};
- if (DEBUG_PROG <= debug) {
- (void)fprintf(stderr, "doing header\n");
- }
- report_time = gmtime(&(start_time.tv_sec));
- (void)strftime(tmstr, sizeof(tmstr), "%Y%m%d %H%M%S UTC", report_time);
- (void)fprintf(log_file,
- "%9s%11s%-20s%-20s%-20s\n",
- "3.03", "", "OBSERVATION DATA", "M: Mixed", "RINEX VERSION / TYPE");
- (void)fprintf(log_file,
- "%-20s%-20s%-20s%-20s\n",
- "gpsrinex 3.19.1~dev", "", tmstr,
- "PGM / RUN BY / DATE");
- (void)fprintf(log_file, "%-60s%-20s\n",
- "Source: gpsd live data", "COMMENT");
- (void)fprintf(log_file, "%-60s%-20s\n", "XXXX", "MARKER NAME");
- (void)fprintf(log_file, "%-60s%-20s\n", "NON_PHYSICAL", "MARKER TYPE");
- (void)fprintf(log_file, "%-20s%-20s%-20s%-20s\n",
- "Unknown", "Unknown", "", "OBSERVER / AGENCY");
- (void)fprintf(log_file, "%-20s%-20s%-20s%-20s\n",
- "0", "UNKNOWN", "0", "REC # / TYPE / VERS");
- (void)fprintf(log_file, "%-20s%-20s%-20s%-20s\n",
- "0", "UNKNOWN EXT NONE", "" , "ANT # / TYPE");
- if (isfinite(ecefx) &&
- isfinite(ecefy) &&
- isfinite(ecefz)) {
- (void)fprintf(log_file, "%14.4f%14.4f%14.4f%18s%-20s\n",
- ecefx, ecefy, ecefz, "", "APPROX POSITION XYZ");
- } else if (DEBUG_INFO <= debug) {
- (void)fprintf(stderr, "INFO: missing ECEF\n");
- }
- (void)fprintf(log_file, "%14.4f%14.4f%14.4f%18s%-20s\n",
- 0.0, 0.0, 0.0, "", "ANTENNA: DELTA H/E/N");
- (void)fprintf(log_file, "%6d%6d%48s%-20s\n", 1, 1,
- "", "WAVELENGTH FACT L1/2");
-
- qsort(obs_cnt, MAXCNT, sizeof(struct obs_cnt_t), compare_obs_cnt);
- for (i = 0; i < GNSSID_CNT; i++ ) {
- prn_count[i] = obs_cnt_prns(i);
- }
-
- if (0 < prn_count[GNSSID_GPS]) {
-
- (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
- gnssid2rinex(GNSSID_GPS), 5, "C1C", "L1C", "D1C", "C2C", "L2C",
- "D2C", "", "", "", "SYS / # / OBS TYPES");
- }
- if (0 < prn_count[GNSSID_SBAS]) {
-
- (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
- gnssid2rinex(GNSSID_SBAS), 3, "C1C", "L1C", "D1C", "", "", "",
- "", "", "", "SYS / # / OBS TYPES");
- }
- if (0 < prn_count[GNSSID_GAL]) {
-
- (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
- gnssid2rinex(GNSSID_GAL), 3, "C1C", "L1C", "D1C", "C7Q",
- "L7Q", "D7Q", "", "", "", "SYS / # / OBS TYPES");
- }
- if (0 < prn_count[GNSSID_BD]) {
-
- (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
- gnssid2rinex(GNSSID_BD), 5, "C1C", "L1C", "D1C", "C7I", "L7I",
- "D7I", "", "", "", "SYS / # / OBS TYPES");
- }
- if (0 < prn_count[GNSSID_QZSS]) {
-
- (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
- gnssid2rinex(GNSSID_QZSS), 5, "C1C", "L1C", "D1C", "C2L",
- "L2L", "D2L", "", "", "", "SYS / # / OBS TYPES");
- }
- if (0 < prn_count[GNSSID_GLO]) {
-
- (void)fprintf(log_file, "%c%5d%4s%4s%4s%4s%4s%4s%4s%4s%22s%-20s\n",
- gnssid2rinex(GNSSID_GLO), 5, "C1C", "L1C", "D1C", "C2C", "L2C",
- "D2C", "", "", "", "SYS / # / OBS TYPES");
- }
- (void)fprintf(log_file, "%6d%54s%-20s\n", obs_cnt_prns(255),
- "", "# OF SATELLITES");
-
- for (i = 0; i < MAXCNT; i++) {
- cnt = 0;
- if (0 == obs_cnt[i].svid) {
-
- break;
- }
- for (j = 0; j < CODEMAX; j++) {
- cnt += obs_cnt[i].obs_cnts[j];
- }
- if (0 > cnt) {
-
- continue;
- }
- switch (obs_cnt[i].gnssid) {
- case GNSSID_GPS:
-
- (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
- gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
- obs_cnt[i].obs_cnts[C1C],
- obs_cnt[i].obs_cnts[L1C],
- obs_cnt[i].obs_cnts[D1C],
- obs_cnt[i].obs_cnts[C2C],
- obs_cnt[i].obs_cnts[L2C],
- obs_cnt[i].obs_cnts[D2C],
- "", "PRN / # OF OBS");
- break;
- case GNSSID_SBAS:
-
- (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
- gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
- obs_cnt[i].obs_cnts[C1C],
- obs_cnt[i].obs_cnts[L1C],
- obs_cnt[i].obs_cnts[D1C],
- obs_cnt[i].obs_cnts[C5I],
- obs_cnt[i].obs_cnts[L5I],
- obs_cnt[i].obs_cnts[D5I],
- "", "PRN / # OF OBS");
- break;
- case GNSSID_GAL:
-
- (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
- gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
- obs_cnt[i].obs_cnts[C1C],
- obs_cnt[i].obs_cnts[L1C],
- obs_cnt[i].obs_cnts[D1C],
- obs_cnt[i].obs_cnts[C7Q],
- obs_cnt[i].obs_cnts[L7Q],
- obs_cnt[i].obs_cnts[D7Q],
- "", "PRN / # OF OBS");
- break;
- case GNSSID_BD:
-
- (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
- gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
- obs_cnt[i].obs_cnts[C1C],
- obs_cnt[i].obs_cnts[L1C],
- obs_cnt[i].obs_cnts[D1C],
- obs_cnt[i].obs_cnts[C7I],
- obs_cnt[i].obs_cnts[L7I],
- obs_cnt[i].obs_cnts[D7I],
- "", "PRN / # OF OBS");
- break;
- case GNSSID_QZSS:
-
- (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
- gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
- obs_cnt[i].obs_cnts[C1C],
- obs_cnt[i].obs_cnts[L1C],
- obs_cnt[i].obs_cnts[D1C],
- obs_cnt[i].obs_cnts[C2L],
- obs_cnt[i].obs_cnts[L2L],
- obs_cnt[i].obs_cnts[D2L],
- "", "PRN / # OF OBS");
- break;
- case GNSSID_GLO:
-
- (void)fprintf(log_file," %c%02d%6u%6u%6u%6u%6u%6u%18s%-20s\n",
- gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
- obs_cnt[i].obs_cnts[C1C],
- obs_cnt[i].obs_cnts[L1C],
- obs_cnt[i].obs_cnts[D1C],
- obs_cnt[i].obs_cnts[C2C],
- obs_cnt[i].obs_cnts[L2C],
- obs_cnt[i].obs_cnts[D2C],
- "", "PRN / # OF OBS");
- break;
- default:
- (void)fprintf(log_file," %c%02d%6u%6u%6u%6s%6s%24s%-20s\n",
- gnssid2rinex(obs_cnt[i].gnssid), obs_cnt[i].svid,
- obs_cnt[i].obs_cnts[C1C],
- obs_cnt[i].obs_cnts[L1C],
- obs_cnt[i].obs_cnts[D1C],
- "", "",
- "", "PRN / # OF OBS");
- }
- }
- (void)fprintf(log_file, "%10.3f%50s%-20s\n",
- (double)sample_interval, "", "INTERVAL");
-
- first_time = gmtime(&(first_mtime.tv_sec));
- (void)fprintf(log_file, "%6d%6d%6d%6d%6d%5d.%07ld%8s%9s%-20s\n",
- first_time->tm_year + 1900,
- first_time->tm_mon + 1,
- first_time->tm_mday,
- first_time->tm_hour,
- first_time->tm_min,
- first_time->tm_sec,
- (long)(first_mtime.tv_nsec / 100),
- "GPS", "",
- "TIME OF FIRST OBS");
-
- last_time = gmtime(&(last_mtime.tv_sec));
- (void)fprintf(log_file, "%6d%6d%6d%6d%6d%5d.%07ld%8s%9s%-20s\n",
- last_time->tm_year + 1900,
- last_time->tm_mon + 1,
- last_time->tm_mday,
- last_time->tm_hour,
- last_time->tm_min,
- last_time->tm_sec,
- (long)(last_mtime.tv_nsec / 100),
- "GPS", "",
- "TIME OF LAST OBS");
- if (0 < prn_count[GNSSID_GPS]) {
-
- (void)fprintf(log_file, "%-60s%-20s\n",
- "G L1C", "SYS / PHASE SHIFT");
- (void)fprintf(log_file, "%-60s%-20s\n",
- "G L2C", "SYS / PHASE SHIFT");
- }
- if (0 < prn_count[GNSSID_SBAS]) {
-
- (void)fprintf(log_file, "%-60s%-20s\n",
- "S L1C", "SYS / PHASE SHIFT");
- (void)fprintf(log_file, "%-60s%-20s\n",
- "E L5Q", "SYS / PHASE SHIFT");
- }
- if (0 < prn_count[GNSSID_GAL]) {
-
- (void)fprintf(log_file, "%-60s%-20s\n",
- "E L1C", "SYS / PHASE SHIFT");
- (void)fprintf(log_file, "%-60s%-20s\n",
- "E L7Q", "SYS / PHASE SHIFT");
- }
- if (0 < prn_count[GNSSID_BD]) {
-
- (void)fprintf(log_file, "%-60s%-20s\n",
- "B L1C", "SYS / PHASE SHIFT");
- (void)fprintf(log_file, "%-60s%-20s\n",
- "B L7I", "SYS / PHASE SHIFT");
- }
- if (0 < prn_count[GNSSID_QZSS]) {
-
- (void)fprintf(log_file, "%-60s%-20s\n",
- "J L1C", "SYS / PHASE SHIFT");
- (void)fprintf(log_file, "%-60s%-20s\n",
- "J L2L", "SYS / PHASE SHIFT");
- }
- if (0 < prn_count[GNSSID_GLO]) {
-
- (void)fprintf(log_file, "%-60s%-20s\n",
- "R L1C", "SYS / PHASE SHIFT");
- (void)fprintf(log_file, "%-60s%-20s\n",
- "R L2C", "SYS / PHASE SHIFT");
- }
- (void)fprintf(log_file, "%-60s%-20s\n",
- "", "END OF HEADER");
- if (DEBUG_PROG <= debug) {
- (void)fprintf(stderr,"done header\n");
- }
- return;
- }
- static void print_rinex_footer(void)
- {
- char buffer[4096];
-
- print_rinex_header();
-
- (void)fflush(tmp_file);
- rewind(tmp_file);
- while (true) {
- size_t count;
- count = fread(buffer, 1, sizeof(buffer), tmp_file);
- if (0 >= count ) {
- break;
- }
- (void)fwrite(buffer, 1, count, log_file);
- }
- (void)fclose(tmp_file);
- (void)fclose(log_file);
- (void)gps_close(&gpsdata);
- }
- static int compare_meas(const void *A, const void *B)
- {
- const struct meas_t *a = (const struct meas_t*)A;
- const struct meas_t *b = (const struct meas_t*)B;
- if (a->gnssid != b->gnssid) {
- return a->gnssid - b->gnssid;
- }
- if (a->svid != b->svid) {
- return a->svid - b->svid;
- }
- if (a->sigid != b->sigid) {
- return a->sigid - b->sigid;
- }
-
- return 0;
- }
- static const char * fmt_obs(double val, unsigned char lli, unsigned char snr)
- {
- static char buf[20];
- char lli_c;
- char snr_c;
- if (!isfinite(val)) {
-
- return " ";
- }
- switch (lli) {
- case 0:
- default:
- lli_c = ' ';
- break;
- case 1:
- lli_c = '1';
- break;
- case 2:
- lli_c = '2';
- break;
- case 3:
- lli_c = '3';
- break;
- }
- if ((1 > snr) || (9 < snr)) {
- snr_c = ' ';
- } else {
- snr_c = 48 + snr;
- }
- (void)snprintf(buf, sizeof(buf), "%14.3f%c%1c", val, lli_c, snr_c);
- return buf;
- }
- static void one_sig(struct meas_t *meas)
- {
- unsigned char snr;
- unsigned gnssid = meas->gnssid;
- unsigned svid = meas->svid;
- unsigned sigid = meas->sigid;
- obs_codes cxx = C1C;
- obs_codes lxx = L1C;
- obs_codes dxx = D1C;
- if (DEBUG_PROG <= debug) {
- (void)fprintf(stderr, "INFO: one_sig() %c %u:%u:%u\n",
- gnssid2rinex(gnssid),
- gnssid, svid, sigid);
- }
- switch (sigid) {
- default:
- (void)fprintf(stderr, "ERROR: one_sig() gnmssid %u unknown sigid %u\n",
- gnssid, sigid);
-
- case 0:
-
- cxx = C1C;
- lxx = L1C;
- dxx = D1C;
- break;
- case 2:
-
- if (GNSSID_BD == gnssid) {
-
- cxx = C7I;
- lxx = L7I;
- dxx = D7I;
- } else {
- cxx = C2C;
- lxx = L2C;
- dxx = D2C;
- }
- break;
- case 3:
-
- cxx = C2C;
- lxx = L2C;
- dxx = D2C;
- break;
- case 5:
-
- cxx = C2L;
- lxx = L2L;
- dxx = D2L;
- break;
- case 6:
-
- cxx = C7Q;
- lxx = L7Q;
- dxx = D7Q;
- break;
- }
-
- if (0 == meas->snr) {
- snr = 0;
- } else if (12 > meas->snr) {
- snr = 1;
- } else if (18 >= meas->snr) {
- snr = 2;
- } else if (23 >= meas->snr) {
- snr = 3;
- } else if (29 >= meas->snr) {
- snr = 4;
- } else if (35 >= meas->snr) {
- snr = 5;
- } else if (41 >= meas->snr) {
- snr = 6;
- } else if (47 >= meas->snr) {
- snr = 7;
- } else if (53 >= meas->snr) {
- snr = 8;
- } else {
-
- snr = 9;
- }
-
-
- if (meas->locktime < (sample_interval * 1000)) {
- meas->lli |= 2;
- }
- if (0 != isfinite(meas->pseudorange)) {
- obs_cnt_inc(gnssid, svid, cxx);
- }
- if (0 != isfinite(meas->carrierphase)) {
- obs_cnt_inc(gnssid, svid, lxx);
- }
- if (0 != isfinite(meas->doppler)) {
- obs_cnt_inc(gnssid, svid, dxx);
- }
- (void)fputs(fmt_obs(meas->pseudorange, 0, snr), tmp_file);
- (void)fputs(fmt_obs(meas->carrierphase, meas->lli, 0), tmp_file);
- (void)fputs(fmt_obs(meas->doppler, 0, 0), tmp_file);
- }
- static void print_raw(struct gps_data_t *gpsdata)
- {
- struct tm *tmp_now;
- unsigned nrec = 0;
- unsigned nsat = 0;
- unsigned i;
- unsigned char last_gnssid = 0;
- unsigned char last_svid = 0;
- int need_nl = 0;
- int got_l1 = 0;
- if ((last_mtime.tv_sec + (time_t)sample_interval) >
- gpsdata->raw.mtime.tv_sec) {
-
- return;
- }
-
- if (0 != (gpsdata->raw.mtime.tv_sec % sample_interval)) {
- return;
- }
-
-
- for (i = 0; i < MAXCHANNELS; i++) {
- if (0 == gpsdata->raw.meas[i].svid) {
-
- break;
- }
- nrec++;
- }
- if (0 >= nrec) {
-
- return;
- }
- qsort(gpsdata->raw.meas, nrec, sizeof(gpsdata->raw.meas[0]),
- compare_meas);
-
- for (i = 0; i < nrec; i++) {
- if (0 == gpsdata->raw.meas[i].svid) {
-
- continue;
- }
- if (4 == gpsdata->raw.meas[i].gnssid) {
-
- continue;
- }
- if (GNSSID_CNT <= gpsdata->raw.meas[i].gnssid) {
-
- continue;
- }
-
- if ((last_gnssid == gpsdata->raw.meas[i].gnssid) &&
- (last_svid == gpsdata->raw.meas[i].svid)) {
-
- continue;
- }
- last_gnssid = gpsdata->raw.meas[i].gnssid;
- last_svid = gpsdata->raw.meas[i].svid;
- nsat++;
- }
- if (0 >= nsat) {
-
- return;
- }
-
- last_mtime = gpsdata->raw.mtime;
- if (0 == first_mtime.tv_sec) {
-
- first_mtime = last_mtime;
- }
-
- tmp_now = gmtime(&(last_mtime.tv_sec));
- (void)fprintf(tmp_file,"> %4d %02d %02d %02d %02d %02d.%07ld 0%3u\n",
- tmp_now->tm_year + 1900,
- tmp_now->tm_mon + 1,
- tmp_now->tm_mday,
- tmp_now->tm_hour,
- tmp_now->tm_min,
- tmp_now->tm_sec,
- (long)(last_mtime.tv_nsec / 100), nsat);
- last_gnssid = 0;
- last_svid = 0;
- need_nl = 0;
- got_l1 = 0;
-
- for (i = 0; i < nrec; i++) {
- char rinex_gnssid;
- unsigned char gnssid;
- unsigned char svid;
- unsigned char sigid;
- gnssid = gpsdata->raw.meas[i].gnssid;
- rinex_gnssid = gnssid2rinex(gnssid);
- svid = gpsdata->raw.meas[i].svid;
- sigid = gpsdata->raw.meas[i].sigid;
- if (DEBUG_RAW <= debug) {
- (void)fprintf(stderr,"record: %u:%u:%u %s\n",
- gnssid, svid, sigid,
- gpsdata->raw.meas[i].obs_code);
- }
- if (0 == gpsdata->raw.meas[i].svid) {
-
- continue;
- }
-
- if ((last_gnssid != gpsdata->raw.meas[i].gnssid) ||
- (last_svid != gpsdata->raw.meas[i].svid)) {
- if (0 != need_nl) {
- (void)fputs("\n", tmp_file);
- }
- got_l1 = 0;
-
- (void)fprintf(tmp_file,"%c%02d", rinex_gnssid, svid);
- }
- last_gnssid = gpsdata->raw.meas[i].gnssid;
- last_svid = gpsdata->raw.meas[i].svid;
-
- switch (gpsdata->raw.meas[i].sigid) {
- case 0:
-
- one_sig(&gpsdata->raw.meas[i]);
- got_l1 = 1;
- break;
- case 2:
-
- if (0 == got_l1) {
-
- (void)fprintf(tmp_file, "%48s", "");
- }
- one_sig(&gpsdata->raw.meas[i]);
- break;
- case 3:
-
- if (0 == got_l1) {
-
- (void)fprintf(tmp_file, "%48s", "");
- }
- one_sig(&gpsdata->raw.meas[i]);
- break;
- case 5:
-
- if (0 == got_l1) {
-
- (void)fprintf(tmp_file, "%48s", "");
- }
- one_sig(&gpsdata->raw.meas[i]);
- break;
- case 6:
-
- if (0 == got_l1) {
-
- (void)fprintf(tmp_file, "%48s", "");
- }
- one_sig(&gpsdata->raw.meas[i]);
- break;
- default:
- (void)fprintf(stderr,
- "ERROR: print_raw() gnssid %u unknown sigid %u\n",
- gnssid, sigid);
- break;
- }
- need_nl = 1;
- }
- if (0 != need_nl) {
- (void)fputs("\n", tmp_file);
- }
- sample_count--;
- }
- static void quit_handler(int signum)
- {
-
- if (signum != SIGINT)
- syslog(LOG_INFO, "exiting, signal %d received", signum);
- print_rinex_footer();
- (void)gps_close(&gpsdata);
- exit(EXIT_SUCCESS);
- }
- static void conditionally_log_fix(struct gps_data_t *gpsdata)
- {
- if (DEBUG_PROG <= debug) {
-
- (void)fprintf(stderr, "mode %d set %llx\n", gpsdata->fix.mode,
- (long long unsigned)gpsdata->set);
- }
-
- if (MODE_2D < gpsdata->fix.mode) {
-
- if (1.0 > ecefx &&
- isfinite(gpsdata->fix.ecef.x) &&
- isfinite(gpsdata->fix.ecef.y) &&
- isfinite(gpsdata->fix.ecef.z)) {
-
- ecefx = gpsdata->fix.ecef.x;
- ecefy = gpsdata->fix.ecef.y;
- ecefz = gpsdata->fix.ecef.z;
- if (DEBUG_PROG <= debug) {
- (void)fprintf(stderr,"got ECEF\n");
- }
- }
- }
- if (RAW_SET & gpsdata->set) {
- if (DEBUG_RAW <= debug) {
- (void)fprintf(stderr,"got RAW\n");
- }
- print_raw(gpsdata);
- }
- return;
- }
- static void usage(void)
- {
- (void)fprintf(stderr,
- "Usage: %s [OPTIONS] [server[:port:[device]]]\n"
- " [-D debuglevel] Set debug level, default 0\n"
- " [-f filename] out to filename\n"
- " gpsrinexYYYYDDDDHHMM.obs\n"
- " [-h] print this usage and exit\n"
- " [-i interval] time between samples, default: %d\n"
- " [-n count] number samples to collect, default: %d\n"
- " [-V] print version and exit\n"
- "\n"
- "defaults to '%s -n %d -i %d localhost:2947'\n",
- progname, sample_interval, sample_count, progname, sample_count,
- sample_interval);
- exit(EXIT_FAILURE);
- }
- int main(int argc, char **argv)
- {
- char tmstr[40];
- struct tm *report_time;
- int ch;
- unsigned int flags = WATCH_ENABLE;
- char *fname = NULL;
- int timeout = 10;
- progname = argv[0];
- log_file = stdout;
- while ((ch = getopt(argc, argv, "D:f:hi:n:V")) != -1) {
- switch (ch) {
- case 'D':
- debug = atoi(optarg);
- gps_enable_debug(debug, log_file);
- break;
- case 'f':
- fname = strdup(optarg);
- break;
- case 'i':
- sample_interval = (time_t) atoi(optarg);
- if (sample_interval < 1)
- sample_interval = 1;
- if (sample_interval >= 3600)
- (void)fprintf(stderr,
- "WARNING: saample interval is an hour or more!\n");
- break;
- case 'n':
- sample_count = atoi(optarg);
- break;
- case 'V':
- (void)fprintf(stderr, "%s: version %s (revision %s)\n",
- progname, VERSION, REVISION);
- exit(EXIT_SUCCESS);
- default:
- usage();
-
- }
- }
-
- source.server = (char *)"localhost";
- source.port = (char *)DEFAULT_GPSD_PORT;
- source.device = NULL;
- if (optind < argc) {
-
- gpsd_source_spec(argv[optind], &source);
- }
- if (DEBUG_INFO <= debug) {
- (void)fprintf(stderr, "INFO: server: %s port: %s device: %s\n",
- source.server, source.port, source.device);
- }
-
- (void)clock_gettime(CLOCK_REALTIME, &start_time);
- report_time = gmtime(&(start_time.tv_sec));
-
- if (NULL == fname) {
- (void)strftime(tmstr, sizeof(tmstr), "gpsrinex%Y%j%H%M%S.obs",
- report_time);
- fname = tmstr;
- }
- log_file = fopen(fname, "w");
- if (log_file == NULL) {
- syslog(LOG_ERR, "ERROR: Failed to open %s: %s",
- fname, strerror(errno));
- exit(3);
- }
-
- memset(obs_cnt, 0, sizeof(obs_cnt));
-
- (void)signal(SIGTERM, quit_handler);
- (void)signal(SIGQUIT, quit_handler);
- (void)signal(SIGINT, quit_handler);
- if (gps_open(source.server, source.port, &gpsdata) != 0) {
- (void)fprintf(stderr, "%s: no gpsd running or network error: %d, %s\n",
- progname, errno, gps_errstr(errno));
- exit(EXIT_FAILURE);
- }
- if (source.device != NULL)
- flags |= WATCH_DEVICE;
- (void)gps_stream(&gpsdata, flags, source.device);
- tmp_file = tmpfile();
- if (NULL == tmp_file) {
- (void)fprintf(stderr, "ERROR: could not open temp file: %s\n",
- strerror(errno));
- exit(2);
- }
- for (;;) {
-
- if (!gps_waiting(&gpsdata, timeout * 1000000)) {
- (void)fprintf(stderr, "gpsrinex: timeout\n");
- syslog(LOG_INFO, "timeout;");
- break;
- }
- (void)gps_read(&gpsdata, NULL, 0);
- if (ERROR_SET & gpsdata.set) {
- fprintf(stderr, "gps_read() error '%s'\n", gpsdata.error);
- exit(6);
- }
- conditionally_log_fix(&gpsdata);
- if (0 >= sample_count) {
-
- break;
- }
- }
- print_rinex_footer();
- exit(EXIT_SUCCESS);
- }
|