-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutput_rinex_nav.c
348 lines (273 loc) · 7.97 KB
/
output_rinex_nav.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
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
339
340
341
342
343
344
345
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "gpsd/gps.h"
#include "sirfdump.h"
#include "sirf_msg.h"
#include "sirf_codec_ssb.h"
#include "nav.h"
extern const char const *MonthName[];
struct rinex_nav_ctx_t {
struct {
char pgm[21];
char run_by[21];
char date[21];
} file;
fpos_t opt_header_pos;
int opt_header_pos_valid;
int header_printed;
int opt_header_printed;
unsigned gps_week;
struct nav_data_t navdata;
};
static int print_nav_header(FILE *out_f, struct rinex_nav_ctx_t *ctx, const struct nav_sat_data_t *nav_data);
static int print_nav_optional_header(FILE *out_f, struct rinex_nav_ctx_t *ctx, const struct nav_sat_data_t *sat);
static int print_nav_data(FILE *out_f, struct rinex_nav_ctx_t *ctx, const struct nav_sat_data_t *nav_data);
static int handle_mid8_msg(struct rinex_nav_ctx_t *ctx,
const tSIRF_MSG_SSB_50BPS_DATA *msg,
FILE *out_f);
static double ura2meters(unsigned ura);
void *new_rinex_nav_ctx(int argc, char **argv)
{
struct rinex_nav_ctx_t *ctx;
struct tm *tm;
time_t clock;
if (argc || argv) {};
ctx = malloc(sizeof(*ctx));
if (ctx == NULL)
return NULL;
snprintf(ctx->file.pgm, sizeof(ctx->file.pgm), "sirfdump");
ctx->file.run_by[0]='\0';
time(&clock);
tm = gmtime(&clock);
snprintf(ctx->file.date, sizeof(ctx->file.date), "%i-%3s-%02u %02i:%02i",
tm->tm_mday, MonthName[tm->tm_mon], tm->tm_year % 100,
tm->tm_hour, tm->tm_min);
ctx->opt_header_pos_valid = 0;
ctx->header_printed = ctx->opt_header_printed = 0;
ctx->gps_week = 1024;
init_nav_data(&ctx->navdata);
return ctx;
}
void free_rinex_nav_ctx(void *ctx)
{
struct rinex_nav_ctx_t *c;
c = (struct rinex_nav_ctx_t *)ctx;
free(c);
}
int output_rinex_nav(struct transport_msg_t *msg, FILE *out_f, void *user_ctx)
{
int err;
struct rinex_nav_ctx_t *ctx;
tSIRF_UINT32 msg_id, msg_length;
tSIRF_UINT32 options;
union {
tSIRF_MSG_SSB_50BPS_DATA data_50bps;
tSIRF_MSG_SSB_CLOCK_STATUS data_clock;
uint8_t u8[SIRF_MSG_SSB_MAX_MESSAGE_LEN];
} m;
assert(user_ctx);
if (!msg || msg->payload_length < 1)
return 1;
ctx = (struct rinex_nav_ctx_t *)user_ctx;
options = 0;
err = SIRF_CODEC_SSB_Decode(msg->payload,
msg->payload_length,
&msg_id,
m.u8,
&msg_length,
&options);
if (err)
return err;
if (msg_id == SIRF_MSG_SSB_50BPS_DATA)
handle_mid8_msg(ctx, &m.data_50bps, out_f);
else if (msg_id == SIRF_MSG_SSB_CLOCK_STATUS)
ctx->gps_week = m.data_clock.gps_week;
return err;
}
static int handle_mid8_msg(struct rinex_nav_ctx_t *ctx,
const tSIRF_MSG_SSB_50BPS_DATA *msg,
FILE *out_f)
{
int data_changed;
struct nav_sat_data_t *dst;
assert(ctx);
assert(msg);
assert(out_f);
data_changed = populate_navdata_from_mid8(msg, &ctx->navdata);
if (data_changed <= 0)
return data_changed;
dst = get_navdata_p(&ctx->navdata, msg->svid);
if (!dst)
return -1;
if (!ctx->header_printed)
ctx->header_printed = print_nav_header(out_f, ctx, dst);
else if (!ctx->opt_header_printed && (data_changed & 0x02))
ctx->opt_header_printed = print_nav_optional_header(out_f, ctx, dst);
else if (0 && (data_changed & 0x02)) /* iono data changed */
print_nav_optional_header(out_f, ctx, dst);
if (data_changed & 0x01)
print_nav_data(out_f, ctx, dst);
return 0;
}
static int print_nav_header(FILE *out_f, struct rinex_nav_ctx_t *ctx, const struct nav_sat_data_t *sat)
{
assert(ctx);
assert(out_f);
assert(sat);
fprintf(out_f, "%9.2f%-11s%c%-19s%-20s%-20s\n",
2.10,
"",
'N',
": GPS NAV DATA",
"",
"RINEX VERSION / TYPE");
fprintf(out_f, "%-20s%-20s%-20s%-20s\n", ctx->file.pgm, ctx->file.run_by, ctx->file.date,
"PGM / RUN BY / DATE");
if ((ctx->opt_header_printed = print_nav_optional_header(out_f, ctx, sat)) == 0) {
ctx->opt_header_pos_valid = !fgetpos(out_f, &ctx->opt_header_pos);
fprintf(out_f,
"%-60s%-20s\n"
"%-60s%-20s\n"
"%-60s%-20s\n"
"%-60s%-20s\n",
"", "COMMENT",
"", "COMMENT",
"", "COMMENT",
"", "COMMENT");
}else
ctx->opt_header_printed = 1;
fprintf(out_f, "%-60s%-20s\n", "",
"END OF HEADER");
return 1;
}
static int print_nav_optional_header(FILE *out_f, struct rinex_nav_ctx_t *ctx, const struct nav_sat_data_t *sat)
{
unsigned wn;
unsigned seek_to_header;
if (ctx->navdata.sub4_18.is_active == 0)
return 0;
if (sat->is_sub1_active == 0)
return 0;
seek_to_header = ctx->header_printed
&& (!ctx->opt_header_printed)
&& (ctx->opt_header_pos_valid);
if (seek_to_header) {
if (fsetpos(out_f, &ctx->opt_header_pos) != 0)
seek_to_header = 0; /* not a seekable stream */
}
/* XXX */
wn = (ctx->gps_week & 0xfc00)
| (sat->sub1.sub1.WN & 0x300)
| (ctx->navdata.sub4_18.WNt & 0xff);
fprintf(out_f,
" %12.4E%12.4E%12.4E%12.4E%-10s%-20s\n"
" %12.4E%12.4E%12.4E%12.4E%-10s%-20s\n"
" %19.12E%19.12E%9u%9u %-20s\n"
"%6u%-54s%-20s\n",
ctx->navdata.sub4_18.ion_alpha[0],
ctx->navdata.sub4_18.ion_alpha[1],
ctx->navdata.sub4_18.ion_alpha[2],
ctx->navdata.sub4_18.ion_alpha[3],
"",
"ION ALPHA",
ctx->navdata.sub4_18.ion_beta[0],
ctx->navdata.sub4_18.ion_beta[1],
ctx->navdata.sub4_18.ion_beta[2],
ctx->navdata.sub4_18.ion_beta[3],
"",
"ION BETA",
ctx->navdata.sub4_18.a0,
ctx->navdata.sub4_18.a1,
(unsigned)floor(ctx->navdata.sub4_18.tot),
wn,
"DELTA-UTC: A0,A1,T,W",
ctx->navdata.sub4_18.leap,
"",
"LEAP SECONDS"
);
if (seek_to_header)
fseek(out_f, 0L, SEEK_END);
return 1;
}
static int print_nav_data(FILE *out_f, struct rinex_nav_ctx_t *ctx, const struct nav_sat_data_t *nav_data)
{
unsigned wn;
struct gps_tm toc_tm;
assert(nav_data);
if (!nav_data->is_sub1_active
|| !nav_data->is_sub2_active
|| !nav_data->is_sub3_active)
return 0;
assert((nav_data->sub1.sub1.IODC & 0xff) == nav_data->sub2.sub2.IODE);
assert((nav_data->sub1.sub1.IODC & 0xff) == nav_data->sub3.sub3.IODE);
/* XXX */
wn = (ctx->gps_week & 0xfc00) | (nav_data->sub1.sub1.WN & 0x3ff);
gpstime2tm0(wn, nav_data->sub1.sub1.l_toc, &toc_tm);
fprintf(out_f,
"%2u%3u%3u%3u%3u%3u%5.1f%19.12E%19.12E%19.12E\n",
nav_data->sub1.tSVID,
toc_tm.year % 100,
toc_tm.month,
toc_tm.day,
toc_tm.hour,
toc_tm.min,
toc_tm.sec,
nav_data->sub1.sub1.d_af0,
nav_data->sub1.sub1.d_af1,
nav_data->sub1.sub1.d_af2);
fprintf(out_f,
" %19.12E%19.12E%19.12E%19.12E\n"
" %19.12E%19.12E%19.12E%19.12E\n"
" %19.12E%19.12E%19.12E%19.12E\n"
" %19.12E%19.12E%19.12E%19.12E\n"
" %19.12E%19.12E%19.12E%19.12E\n"
" %19.12E%19.12E%19.12E%19.12E\n"
" %19.12E\n",
(double)nav_data->sub2.sub2.IODE,
nav_data->sub2.sub2.d_Crs,
nav_data->sub2.sub2.d_deltan * (double)GPS_PI,
nav_data->sub2.sub2.d_M0,
nav_data->sub2.sub2.d_Cuc,
nav_data->sub2.sub2.d_eccentricity,
nav_data->sub2.sub2.d_Cus,
nav_data->sub2.sub2.d_sqrtA,
(double)nav_data->sub2.sub2.l_toe,
nav_data->sub3.sub3.d_Cic,
nav_data->sub3.sub3.d_Omega0 * (double)GPS_PI,
nav_data->sub3.sub3.d_Cis,
nav_data->sub3.sub3.d_i0 * (double)GPS_PI,
nav_data->sub3.sub3.d_Crc,
nav_data->sub3.sub3.d_omega * (double)GPS_PI,
nav_data->sub3.sub3.d_Omegad * (double)GPS_PI,
nav_data->sub3.sub3.d_IDOT * (double)GPS_PI,
(double)nav_data->sub1.sub1.l2,
(double)wn,
(double)nav_data->sub1.sub1.l2p,
ura2meters(nav_data->sub1.sub1.ura),
(double)nav_data->sub1.sub1.hlth,
nav_data->sub1.sub1.d_Tgd,
(double)nav_data->sub1.sub1.IODC,
/* XXX */
(double)nav_data->sub1.l_TOW17
);
return 1;
}
static double ura2meters(unsigned ura)
{
switch (ura) {
case 1: return 2.8;
case 3: return 5.7;
case 5: return 11.3;
default:
break;
}
if (ura <= 6)
return pow(2.0, 1.0+(double)ura/2.0);
if (ura < 15)
return pow(2.0, (double)ura - 2.0);
return 6145.0;
}