summaryrefslogtreecommitdiff
path: root/monitor_nmea.c
blob: fb1cc565473fca29e518ce85fc05b95fe92529eb (plain)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* $Id$ */
/*
 * nmeamon.c - gpsmon support for NMEA devices.
 *
 * To do: Support for GPGLL, GPRME, GPZDA, OHPR, PASHR NMEA sentences.
 * Support for Tripmate, Earthmate, True North and Oceanserver devices.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#ifndef S_SPLINT_S
#include <unistd.h>
#endif /* S_SPLINT_S */
#include <stdarg.h>
#include <stdbool.h>
#include <assert.h>

#include "gpsd_config.h"
#ifdef HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif /* HAVE_NCURSES_H */
#include "gpsd.h"

#include "bits.h"
#include "gpsmon.h"
#include "gpsdclient.h"

#ifdef NMEA_ENABLE
extern const struct gps_type_t nmea;

static WINDOW *cookedwin, *nmeawin, *satwin, *gprmcwin, *gpggawin, *gpgsawin;
static double last_tick, tick_interval;

/*****************************************************************************
 *
 * Generic NMEA support
 *
 *****************************************************************************/

#define SENTENCELINE	1	/* index of sentences line in the NMEA window */
#define MAXSATS 	12	/* max satellites we can display */

static bool nmea_initialize(void)
{
    int i;

    /*@ -onlytrans @*/
    cookedwin = derwin(devicewin, 3,  80, 0, 0);
    (void)wborder(cookedwin, 0, 0, 0, 0, 0, 0, 0, 0);
    (void)syncok(cookedwin, true);
    wattrset(cookedwin, A_BOLD);
    mvwaddstr(cookedwin, 1, 1, "Time: ");
    mvwaddstr(cookedwin, 1, 31, "Lat: ");
    mvwaddstr(cookedwin, 1, 55, "Lon: ");
    mvwaddstr(cookedwin, 2, 60, "Lon: ");
    mvwaddstr(cookedwin, 2, 34, " Cooked PVT ");
    wattrset(cookedwin, A_NORMAL);

    nmeawin = derwin(devicewin, 3,  80, 3, 0);
    (void)wborder(nmeawin, 0, 0, 0, 0, 0, 0, 0, 0);
    (void)syncok(nmeawin, true);
    wattrset(nmeawin, A_BOLD);
    mvwaddstr(nmeawin, 2, 34, " Sentences ");
    wattrset(nmeawin, A_NORMAL);

    satwin  = derwin(devicewin, MAXSATS+3, 20, 6, 0);
    (void)wborder(satwin, 0, 0, 0, 0, 0, 0, 0, 0),
    (void)syncok(satwin, true);
    (void)wattrset(satwin, A_BOLD);
    (void)mvwprintw(satwin, 1, 1, "Ch PRN  Az El S/N");
    for (i = 0; i < MAXSATS; i++)
	(void)mvwprintw(satwin, (int)(i+2), 1, "%2d",i);
    (void)mvwprintw(satwin, 14, 7, " GSV ");
    (void)wattrset(satwin, A_NORMAL);

    gprmcwin  = derwin(devicewin, 9, 30, 6, 20);
    (void)wborder(gprmcwin, 0, 0, 0, 0, 0, 0, 0, 0),
    (void)syncok(gprmcwin, true);
    (void)wattrset(gprmcwin, A_BOLD);
    (void)mvwprintw(gprmcwin, 1, 1, "Time: ");
    (void)mvwprintw(gprmcwin, 2, 1, "Latitude: ");
    (void)mvwprintw(gprmcwin, 3, 1, "Longitude: ");
    (void)mvwprintw(gprmcwin, 4, 1, "Speed: ");
    (void)mvwprintw(gprmcwin, 5, 1, "Course: ");
    (void)mvwprintw(gprmcwin, 6, 1, "Status:            FAA: ");
    (void)mvwprintw(gprmcwin, 7, 1, "MagVar: ");
    (void)mvwprintw(gprmcwin, 8, 12, " RMC ");
    (void)wattrset(gprmcwin, A_NORMAL);

    gpgsawin  = derwin(devicewin, 5, 30, 15, 20);
    (void)wborder(gpgsawin, 0, 0, 0, 0, 0, 0, 0, 0),
    (void)syncok(gpgsawin, true);
    (void)wattrset(gpgsawin, A_BOLD);
    (void)mvwprintw(gpgsawin, 1, 1, "Mode: ");
    (void)mvwprintw(gpgsawin, 2, 1, "Sats: ");
    (void)mvwprintw(gpgsawin, 3, 1, "DOP: H=      V=      P=");
    (void)mvwprintw(gpgsawin, 4, 12, " GSA ");
    (void)wattrset(gpgsawin, A_NORMAL);

    gpggawin  = derwin(devicewin, 9, 30, 6, 50);
    (void)wborder(gpggawin, 0, 0, 0, 0, 0, 0, 0, 0),
    (void)syncok(gpggawin, true);
    (void)wattrset(gpggawin, A_BOLD);
    (void)mvwprintw(gpggawin, 1, 1, "Time: ");
    (void)mvwprintw(gpggawin, 2, 1, "Latitude: ");
    (void)mvwprintw(gpggawin, 3, 1, "Longitude: ");
    (void)mvwprintw(gpggawin, 4, 1, "Altitude: ");
    (void)mvwprintw(gpggawin, 5, 1, "Quality:       Sats: ");
    (void)mvwprintw(gpggawin, 6, 1, "HDOP: ");
    (void)mvwprintw(gpggawin, 7, 1, "Geoid: ");
    (void)mvwprintw(gpggawin, 8, 12, " GGA ");
    (void)wattrset(gpggawin, A_NORMAL);
    /*@ +onlytrans @*/

    last_tick = timestamp();

    return (nmeawin != NULL);
}

static void cooked_pvt(void)
{
    char scr[128];

    if (isnan(session.gpsdata.fix.time)==0) {
	(void)unix_to_iso8601(session.gpsdata.fix.time, scr, sizeof(scr));
    } else
	(void)snprintf(scr, sizeof(scr), "n/a");
    (void)mvwprintw(cookedwin, 1, 7, "%-22s", scr);


    if (session.gpsdata.fix.mode >= MODE_2D && isnan(session.gpsdata.fix.latitude)==0) {
	(void)snprintf(scr, sizeof(scr), "%s %c", 
		       deg_to_str(deg_ddmmss,  fabs(session.gpsdata.fix.latitude)), 
		       (session.gpsdata.fix.latitude < 0) ? 'S' : 'N');
    } else
	(void)snprintf(scr, sizeof(scr), "n/a");
    (void)mvwprintw(cookedwin, 1, 36, "%-17s", scr);

    if (session.gpsdata.fix.mode >= MODE_2D && isnan(session.gpsdata.fix.longitude)==0) {
	(void)snprintf(scr, sizeof(scr), "%s %c", 
		       deg_to_str(deg_ddmmss,  fabs(session.gpsdata.fix.longitude)), 
		       (session.gpsdata.fix.longitude < 0) ? 'W' : 'E');
    } else
	(void)snprintf(scr, sizeof(scr), "n/a");
    (void)mvwprintw(cookedwin, 1, 60, "%-17s", scr);

#if 0
    if (isnan(session.gpsdata.fix.track)==0)
	(void)snprintf(scr, sizeof(scr), "%.1f meters/sec", session.gpsdata.fix.speed);
    else
	(void)snprintf(scr, sizeof(scr), "n/a");
    (void)mvwprintw(cookedwin, 1, 33, "%-17s", scr);

    /* fill in the course */
    if (session.gpsdata.fix.mode >= MODE_2D && isnan(session.gpsdata.fix.track)==0)
	(void)snprintf(scr, sizeof(scr), "%.1f deg", session.gpsdata.fix.track);
    else
	(void)snprintf(scr, sizeof(scr), "n/a");
    (void)mvwprintw(cookedwin, 5, 11, "%-17s", scr);
#endif
}


/*@ -globstate -nullpass (splint is confused) */
static void nmea_update(void)
{
    static char sentences[NMEA_MAX];
    char **fields;

    assert(cookedwin!=NULL);
    assert(nmeawin!=NULL);
    assert(gpgsawin!=NULL);
    assert(gpggawin!=NULL);
    assert(gprmcwin!=NULL);

    fields = session.driver.nmea.field;

    if (session.packet.outbuffer[0] == (unsigned char)'$') {
	int ymax, xmax;
	double now;
	getmaxyx(nmeawin, ymax, xmax);
	if (strstr(sentences, fields[0]) == NULL) {
	    char *s_end = sentences + strlen(sentences);
	    if ((int)(strlen(sentences) + strlen(fields[0])) < xmax-2) {
		*s_end++ = ' '; 
		(void)strlcpy(s_end, fields[0], NMEA_MAX);
	    } else {
		*--s_end = '.';
		*--s_end = '.';
		*--s_end = '.';
	    }
	    mvwaddstr(nmeawin, SENTENCELINE, 1, sentences);
	}

	/* 
	 * If the interval between this and last update is 
	 * the longest we've seen yet, boldify the corresponding
	 * tag.
	 */
	now = timestamp();
	if (now > last_tick && (now - last_tick) > tick_interval)
	{
	    char *findme = strstr(sentences, fields[0]);

	    tick_interval = now - last_tick;
	    if (findme != NULL) {
		mvwchgat(nmeawin, SENTENCELINE, 1, xmax-13, A_NORMAL, 0, NULL);
		mvwchgat(nmeawin, 
			 SENTENCELINE, 1+(findme-sentences), 
			 (int)strlen(fields[0]),
			 A_BOLD, 0, NULL);
	    }
	}
	last_tick = now;

	if (strcmp(fields[0], "GPGSV") == 0) {
	    int i;
	    int nsats = (session.gpsdata.satellites < MAXSATS) ? session.gpsdata.satellites : MAXSATS;

	    for (i = 0; i < nsats; i++) {
		(void)wmove(satwin, i+2, 3);
		(void)wprintw(satwin, " %3d %3d%3d %3d", 
			      session.gpsdata.PRN[i],
			      session.gpsdata.azimuth[i],
			      session.gpsdata.elevation[i],
			      session.gpsdata.ss[i]);
	    }
	    /* add overflow mark to the display */
	    if (nsats <= MAXSATS)
		(void)mvwaddch(satwin, MAXSATS+2, 18, ACS_HLINE);
	    else
		(void)mvwaddch(satwin, MAXSATS+2, 18, ACS_DARROW);
	}

	if (strcmp(fields[0], "GPRMC") == 0) {
	    /* time, lat, lon, course, speed */
	    (void)mvwaddstr(gprmcwin, 1, 12, fields[1]);
	    (void)mvwprintw(gprmcwin, 2, 12, "%12s %s", fields[3], fields[4]);
	    (void)mvwprintw(gprmcwin, 3, 12, "%12s %s", fields[5], fields[6]);
	    (void)mvwaddstr(gprmcwin, 4, 12, fields[7]);
	    (void)mvwaddstr(gprmcwin, 5, 12, fields[8]);
	    /* the status field, FAA code, and magnetic variation */
	    (void)mvwaddstr(gprmcwin, 6, 12, fields[2]);
	    (void)mvwaddstr(gprmcwin, 6, 25, fields[12]);
	    (void)mvwprintw(gprmcwin, 7, 12, "%-5s%s", fields[10],fields[11]);

	    cooked_pvt();	/* cooked version of PVT */
	}

	if (strcmp(fields[0], "GPGSA") == 0) {
	    char scr[128];
	    int i;
	    (void)mvwprintw(gpgsawin, 1,7, "%1s %s", fields[1], fields[2]);
	    (void)wmove(gpgsawin, 2, 7);
	    (void)wclrtoeol(gpgsawin);
	    scr[0] = '\0';
	    for (i = 0; i < session.gpsdata.satellites_used; i++) {
		(void)snprintf(scr + strlen(scr), sizeof(scr)-strlen(scr), 
			       "%d ", session.gpsdata.used[i]);
	    }
	    getmaxyx(gpgsawin, ymax, xmax);
	    (void)mvwaddnstr(gpgsawin, 2, 7, scr, xmax-2-7);
	    if (strlen(scr) >= (size_t)(xmax-2)) {
		mvwaddch(gpgsawin, 2, xmax-2-7, (chtype)'.');
		mvwaddch(gpgsawin, 2, xmax-3-7, (chtype)'.');
		mvwaddch(gpgsawin, 2, xmax-4-7, (chtype)'.');
	    }
	    monitor_fixframe(gpgsawin);
	    (void)mvwprintw(gpgsawin, 3, 8, "%-5s", fields[16]); 
	    (void)mvwprintw(gpgsawin, 3, 16, "%-5s", fields[17]); 
	    (void)mvwprintw(gpgsawin, 3, 24, "%-5s", fields[15]); 
	    monitor_fixframe(gpgsawin);
	}
	if (strcmp(fields[0], "GPGGA") == 0) {
	    (void)mvwprintw(gpggawin, 1, 12, "%-17s", fields[1]);
	    (void)mvwprintw(gpggawin, 2, 12, "%-17s", fields[2]);
	    (void)mvwprintw(gpggawin, 3, 12, "%-17s", fields[4]);
	    (void)mvwprintw(gpggawin, 4, 12, "%-17s", fields[9]);
	    (void)mvwprintw(gpggawin, 5, 12, "%1.1s", fields[6]);
	    (void)mvwprintw(gpggawin, 5, 22, "%2.2s", fields[7]);
	    (void)mvwprintw(gpggawin, 6, 12, "%-5.5s", fields[8]);
	    (void)mvwprintw(gpggawin, 7, 12, "%-5.5s", fields[11]);
	}
    }
}
/*@ +globstate +nullpass */

#undef SENTENCELINE

static void nmea_wrap(void)
{
    (void)delwin(nmeawin);
    (void)delwin(gpgsawin);
    (void)delwin(gpggawin);
    (void)delwin(gprmcwin);
}

const struct monitor_object_t nmea_mmt = {
    .initialize = nmea_initialize,
    .update = nmea_update,
    .command = NULL,
    .wrap = nmea_wrap,
    .min_y = 21, .min_x = 80,
    .driver = &nmea,
};

/*****************************************************************************
 *
 * Extended NMEA support
 *
 *****************************************************************************/

#ifdef ALLOW_CONTROLSEND
static void monitor_nmea_send(const char *fmt, ... )
{
    char buf[BUFSIZ];
    va_list ap;

    va_start(ap, fmt) ;
    (void)vsnprintf(buf, sizeof(buf)-5, fmt, ap);
    va_end(ap);
    (void)monitor_control_send((unsigned char *)buf, strlen(buf));
}
#endif /* ALLOW_CONTROLSEND */

/*
 * Yes, it's OK for most of these to be clones of the generic NMEA monitor
 * object except for the pointer to the GPSD driver.  That pointer makes
 * a difference, as it will automatically enable stuff like speed-switcher
 * and mode-switcher commands.  It's really only necessary to write a 
 * separate monitor object if you want to change the device-window
 * display or implement device-specific commands.
 */

#if defined(GARMIN_ENABLE) && defined(NMEA_ENABLE)
extern const struct gps_type_t garmin;

const struct monitor_object_t garmin_mmt = {
    .initialize = nmea_initialize,
    .update = nmea_update,
    .command = NULL,
    .wrap = nmea_wrap,
    .min_y = 21, .min_x = 80,
    .driver = &garmin,
};
#endif /* GARMIN_ENABLE && NMEA_ENABLE */

#ifdef ASHTECH_ENABLE
extern const struct gps_type_t ashtech;

#define ASHTECH_SPEED_9600 5
#define ASHTECH_SPEED_57600 8

#ifdef ALLOW_CONTROLSEND
static int ashtech_command(char line[])
{
    switch (line[0]) 
    {
    case 'N':		/* normal = 9600, GGA+GSA+GSV+RMC+ZDA */
	monitor_nmea_send("$PASHS,NME,ALL,A,OFF"); /* silence outbound chatter */
	monitor_nmea_send("$PASHS,NME,ALL,B,OFF");
	monitor_nmea_send("$PASHS,NME,GGA,A,ON");
	monitor_nmea_send("$PASHS,NME,GSA,A,ON");
	monitor_nmea_send("$PASHS,NME,GSV,A,ON");
	monitor_nmea_send("$PASHS,NME,RMC,A,ON");
	monitor_nmea_send("$PASHS,NME,ZDA,A,ON");

	monitor_nmea_send("$PASHS,INI,%d,%d,,,0,",
		  ASHTECH_SPEED_9600, ASHTECH_SPEED_9600);
	(void)sleep(6); /* it takes 4-6 sec for the receiver to reboot */
	monitor_nmea_send("$PASHS,WAS,ON"); /* enable WAAS */
	break;

    case 'R':		/* raw = 57600, normal+XPG+POS+SAT+MCA+PBN+SNV */
	monitor_nmea_send("$PASHS,NME,ALL,A,OFF"); /* silence outbound chatter */
	monitor_nmea_send("$PASHS,NME,ALL,B,OFF");
	monitor_nmea_send("$PASHS,NME,GGA,A,ON");
	monitor_nmea_send("$PASHS,NME,GSA,A,ON");
	monitor_nmea_send("$PASHS,NME,GSV,A,ON");
	monitor_nmea_send("$PASHS,NME,RMC,A,ON");
	monitor_nmea_send("$PASHS,NME,ZDA,A,ON");

	monitor_nmea_send("$PASHS,INI,%d,%d,,,0,",
		  ASHTECH_SPEED_57600, ASHTECH_SPEED_9600);
	(void)sleep(6); /* it takes 4-6 sec for the receiver to reboot */
	monitor_nmea_send("$PASHS,WAS,ON"); /* enable WAAS */

	monitor_nmea_send("$PASHS,NME,POS,A,ON"); /* Ashtech PVT solution */
	monitor_nmea_send("$PASHS,NME,SAT,A,ON"); /* Ashtech Satellite status */
	monitor_nmea_send("$PASHS,NME,MCA,A,ON"); /* MCA measurements */
	monitor_nmea_send("$PASHS,NME,PBN,A,ON"); /* ECEF PVT solution */
	monitor_nmea_send("$PASHS,NME,SNV,A,ON,10"); /* Almanac data */

	monitor_nmea_send("$PASHS,NME,XMG,A,ON"); /* exception messages */
	break;

    default:
	return COMMAND_UNKNOWN;
    }

    return COMMAND_UNKNOWN;
}
#endif /* ALLOW_CONTROLSEND */

const struct monitor_object_t ashtech_mmt = {
    .initialize = nmea_initialize,
    .update = nmea_update,
#ifdef ALLOW_CONTROLSEND
    .command = ashtech_command,
#else
    .command = NULL,
#endif /* ALLOW_CONTROLSEND */
    .wrap = nmea_wrap,
    .min_y = 21, .min_x = 80,
    .driver = &ashtech,
};
#endif /* ASHTECH_ENABLE */

#ifdef FV18_ENABLE
extern const struct gps_type_t fv18;

const struct monitor_object_t fv18_mmt = {
    .initialize = nmea_initialize,
    .update = nmea_update,
    .command = NULL,
    .wrap = nmea_wrap,
    .min_y = 21, .min_x = 80,
    .driver = &fv18,
};
#endif /* FV18_ENABLE */

#ifdef GPSCLOCK_ENABLE
extern const struct gps_type_t gpsclock;

const struct monitor_object_t gpsclock_mmt = {
    .initialize = nmea_initialize,
    .update = nmea_update,
    .command = NULL,
    .wrap = nmea_wrap,
    .min_y = 21, .min_x = 80,
    .driver = &gpsclock,
};
#endif /* GPSCLOCK_ENABLE */

#ifdef MKT3301_ENABLE
extern const struct gps_type_t mkt3301;

const struct monitor_object_t mkt3301_mmt = {
    .initialize = nmea_initialize,
    .update = nmea_update,
    .command = NULL,
    .wrap = nmea_wrap,
    .min_y = 21, .min_x = 80,
    .driver = &mkt3301,
};
#endif /* MKT3301_ENABLE */

#endif /* NMEA_ENABLE */