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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
/*
* Copyright (c) 2005 Jeff Francis <jeff@gritch.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
Jeff Francis
jeff@gritch.org
A client that passes gpsd data to lcdproc, turning your car computer
into a very expensive feature-free GPS receiver ;^). Currently
assumes a 4x40 LCD and writes data formatted to fit that size
screen. Also displays 4- or 6-character Maidenhead grid square
output for the hams among us.
This program assumes that LCDd (lcdproc) is running locally on the
default (13666) port. The #defines LCDDHOST and LCDDPORT can be
changed to talk to a different host and TCP port.
*/
#define LCDDHOST "localhost"
#define LCDDPORT 13666
#define CLIMB 3
#include <sys/types.h>
#include <sys/stat.h>
#ifndef S_SPLINT_S
#include <netdb.h>
#ifndef AF_UNSPEC
#include <sys/socket.h>
#endif /* AF_UNSPEC */
#endif /* S_SPLINT_S */
#ifndef INADDR_ANY
#include <netinet/in.h>
#endif /* INADDR_ANY */
#include <sys/time.h> /* for select() */
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <stdio.h>
#ifndef S_SPLINT_S
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#endif /* S_SPLINT_S */
#include "gps.h"
#include "gpsdclient.h"
#include "revision.h"
/* Prototypes. */
void latlon2maidenhead(char *st,float n,float e);
ssize_t sockreadline(int sockd,void *vptr,size_t maxlen);
ssize_t sockwriteline(int sockd,const void *vptr,size_t n);
int send_lcd(char *buf);
static struct fixsource_t source;
static struct gps_data_t gpsdata;
static float altfactor = METERS_TO_FEET;
static float speedfactor = MPS_TO_MPH;
static char *altunits = "ft";
static char *speedunits = "mph";
#ifdef CLIMB
double avgclimb, climb[CLIMB];
#endif
/* Global socket descriptor for LCDd. */
int sd;
/* Convert lat/lon to Maidenhead. Lifted from QGrid -
http://users.pandora.be/on4qz/qgrid/ */
void latlon2maidenhead(char *st,float n,float e)
{
int t1;
e=e+180.0;
t1=(int)(e/20);
st[0]=t1+'A';
e-=(float)t1*20.0;
t1=(int)e/2;
st[2]=t1+'0';
e-=(float)t1*2;
#ifndef CLIMB
st[4]=(int)(e*12.0+0.5)+'A';
#endif
n=n+90.0;
t1=(int)(n/10.0);
st[1]=t1+'A';
n-=(float)t1*10.0;
st[3]=(int)n+'0';
n-=(int)n;
n*=24; // convert to 24 division
#ifndef CLIMB
st[5]=(int)(n+0.5)+'A';
#endif
}
/* Read a line from a socket */
ssize_t sockreadline(int sockd,void *vptr,size_t maxlen) {
ssize_t n,rc;
char c,*buffer;
buffer=vptr;
for (n = 1; n < (ssize_t)maxlen; n++) {
if((rc=read(sockd,&c,1))==1) {
*buffer++=c;
if(c=='\n')
break;
}
else if(rc==0) {
if(n==1)
return(0);
else
break;
}
else {
if(errno==EINTR)
continue;
return(-1);
}
}
*buffer=0;
return(n);
}
/* Write a line to a socket */
ssize_t sockwriteline(int sockd,const void *vptr,size_t n) {
size_t nleft;
ssize_t nwritten;
const char *buffer;
buffer=vptr;
nleft=n;
while(nleft>0) {
if((nwritten= write(sockd,buffer,nleft))<=0) {
if(errno==EINTR)
nwritten=0;
else
return(-1);
}
nleft-=nwritten;
buffer+=nwritten;
}
return(n);
}
/* send a command to the LCD */
int send_lcd(char *buf) {
int res;
char rcvbuf[256];
size_t outlen;
/* Limit the size of outgoing strings. */
outlen = strlen(buf);
if(outlen > 255) {
outlen = 256;
}
/* send the command */
res=sockwriteline(sd,buf,outlen);
/* TODO: check return status */
/* read the data */
res=sockreadline(sd,rcvbuf,255);
/* null-terminate the string before printing */
/* rcvbuf[res-1]=0; FIX-ME: not using this at the moment... */
/* return the result */
return(res);
}
/* reset the LCD */
static void reset_lcd(void) {
/* Initialize. In theory, we should look at what's returned, as it
tells us info on the attached LCD module. TODO. */
send_lcd("hello\n");
/* Set up the screen */
send_lcd("client_set name {GPSD test}\n");
send_lcd("screen_add gpsd\n");
send_lcd("widget_add gpsd one string\n");
send_lcd("widget_add gpsd two string\n");
send_lcd("widget_add gpsd three string\n");
send_lcd("widget_add gpsd four string\n");
}
static enum deg_str_type deg_type = deg_dd;
/* This gets called once for each new sentence. */
static void update_lcd(struct gps_data_t *gpsdata)
{
char tmpbuf[255];
#ifdef CLIMB
char maidenhead[5];
maidenhead[4]=0;
int n;
#else
char maidenhead[7];
maidenhead[6]=0;
#endif
char *s;
int track;
/* Get our location in Maidenhead. */
latlon2maidenhead(maidenhead,gpsdata->fix.latitude,gpsdata->fix.longitude);
/* Fill in the latitude and longitude. */
if (gpsdata->fix.mode >= MODE_2D) {
s = deg_to_str(deg_type, fabs(gpsdata->fix.latitude));
snprintf(tmpbuf, 254, "widget_set gpsd one 1 1 {Lat: %s %c}\n", s, (gpsdata->fix.latitude < 0) ? 'S' : 'N');
send_lcd(tmpbuf);
s = deg_to_str(deg_type, fabs(gpsdata->fix.longitude));
snprintf(tmpbuf, 254, "widget_set gpsd two 1 2 {Lon: %s %c}\n", s, (gpsdata->fix.longitude < 0) ? 'W' : 'E');
send_lcd(tmpbuf);
/* As a pilot, a heading of "0" gives me the heebie-jeebies (ie, 0
== "invalid heading data", 360 == "North"). */
track=(int)(gpsdata->fix.track);
if(track == 0) track = 360;
snprintf(tmpbuf, 254, "widget_set gpsd three 1 3 {%.1f %s %d deg}\n",
gpsdata->fix.speed*speedfactor, speedunits,
track);
send_lcd(tmpbuf);
} else {
send_lcd("widget_set gpsd one 1 1 {Lat: n/a}\n");
send_lcd("widget_set gpsd two 1 2 {Lon: n/a}\n");
send_lcd("widget_set gpsd three 1 3 {n/a}\n");
}
/* Fill in the altitude and fix status. */
if (gpsdata->fix.mode == MODE_3D) {
#ifdef CLIMB
for(n=0;n<CLIMB-2;n++) climb[n]=climb[n+1];
climb[CLIMB-1]=gpsdata->fix.climb;
avgclimb=0.0;
for(n=0;n<CLIMB;n++) avgclimb+=climb[n];
avgclimb/=CLIMB;
snprintf(tmpbuf, 254, "widget_set gpsd four 1 4 {%d %s %s %d fpm }\n",
(int)(gpsdata->fix.altitude*altfactor), altunits, maidenhead, (int)(avgclimb * METERS_TO_FEET * 60));
#else
snprintf(tmpbuf, 254, "widget_set gpsd four 1 4 {%.1f %s %s}\n",
gpsdata->fix.altitude*altfactor, altunits, maidenhead);
#endif
} else {
snprintf(tmpbuf, 254, "widget_set gpsd four 1 4 {n/a}\n");
}
send_lcd(tmpbuf);
}
static void usage( char *prog)
{
(void)fprintf(stderr,
"Usage: %s [-h] [-v] [-V] [-s] [-l {d|m|s}] [-u {i|m|n}] [server[:port:[device]]]\n\n"
" -h Show this help, then exit\n"
" -V Show version, then exit\n"
" -s Sleep for 10 seconds before starting\n"
" -j Turn on anti-jitter buffering\n"
" -l {d|m|s} Select lat/lon format\n"
" d = DD.dddddd (default)\n"
" m = DD MM.mmmm'\n"
" s = DD MM' SS.sss\"\n"
" -u {i|m|n} Select Units\n"
" i = Imperial (default)\n"
" m = Metric'\n"
" n = Nautical\"\n"
, prog);
exit(1);
}
int main(int argc, char *argv[])
{
int option, rc;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
struct timeval timeout;
fd_set rfds;
int data;
#ifdef CLIMB
int n;
for(n=0;n<CLIMB;n++) climb[n]=0.0;
#endif
/*@ -observertrans @*/
switch (gpsd_units())
{
case imperial:
altfactor = METERS_TO_FEET;
altunits = "ft";
speedfactor = MPS_TO_MPH;
speedunits = "mph";
break;
case nautical:
altfactor = METERS_TO_FEET;
altunits = "ft";
speedfactor = MPS_TO_KNOTS;
speedunits = "knots";
break;
case metric:
altfactor = 1;
altunits = "m";
speedfactor = MPS_TO_KPH;
speedunits = "kph";
break;
default:
/* leave the default alone */
break;
}
/*@ +observertrans @*/
/* Process the options. Print help if requested. */
while ((option = getopt(argc, argv, "Vhl:su:")) != -1) {
switch (option) {
case 'V':
(void)fprintf(stderr, "lcdgs revision " REVISION "\n");
exit(0);
case 'h':
default:
usage(argv[0]);
break;
case 'l':
switch ( optarg[0] ) {
case 'd':
deg_type = deg_dd;
continue;
case 'm':
deg_type = deg_ddmm;
continue;
case 's':
deg_type = deg_ddmmss;
continue;
default:
(void)fprintf(stderr, "Unknown -l argument: %s\n", optarg);
/*@ -casebreak @*/
}
case 's':
sleep(10);
continue;
case 'u':
switch ( optarg[0] ) {
case 'i':
altfactor = METERS_TO_FEET;
altunits = "ft";
speedfactor = MPS_TO_MPH;
speedunits = "mph";
continue;
case 'n':
altfactor = METERS_TO_FEET;
altunits = "ft";
speedfactor = MPS_TO_KNOTS;
speedunits = "knots";
continue;
case 'm':
altfactor = 1;
altunits = "m";
speedfactor = MPS_TO_KPH;
speedunits = "kph";
continue;
default:
(void)fprintf(stderr, "Unknown -u argument: %s\n", optarg);
/*@ -casebreak @*/
}
}
}
/* Grok the server, port, and device. */
if (optind < argc) {
gpsd_source_spec(argv[optind], &source);
} else
gpsd_source_spec(NULL, &source);
/* Daemonize... */
if (daemon(0, 0) != 0)
(void)fprintf(stderr,
"lcdgps: demonization failed: %s\n",
strerror(errno));
/* Open the stream to gpsd. */
if (gps_open(source.server, source.port, &gpsdata) != 0) {
(void)fprintf( stderr,
"lcdgps: no gpsd running or network error: %d, %s\n",
errno, gps_errstr(errno));
exit(2);
}
/* Connect to LCDd */
h = gethostbyname(LCDDHOST);
if(h==NULL) {
printf("%s: unknown host '%s'\n",argv[0],LCDDHOST);
exit(1);
}
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(LCDDPORT);
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd == -1) {
perror("cannot open socket ");
exit(1);
}
/* bind any port number */
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
if(rc == -1) {
printf("%s: cannot bind port TCP %u\n",argv[0],LCDDPORT);
perror("error ");
exit(1);
}
/* connect to server */
rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc == -1) {
perror("cannot connect ");
exit(1);
}
/* Do the initial field label setup. */
reset_lcd();
/* Here's where updates go. */
unsigned int flags = WATCH_ENABLE;
if (source.device != NULL)
flags |= WATCH_DEVICE;
(void)gps_stream(&gpsdata, flags, source.device);
for (;;) { /* heart of the client */
/* watch to see when it has input */
FD_ZERO(&rfds);
FD_SET(gpsdata.gps_fd, &rfds);
/* wait up to five seconds. */
timeout.tv_sec = 5;
timeout.tv_usec = 0;
/* check if we have new information */
data = select(gpsdata.gps_fd + 1, &rfds, NULL, NULL, &timeout);
if (data == -1) {
fprintf( stderr, "lcdgps: socket error\n");
exit(2);
}
else if (data) {
(void)gps_read(&gpsdata);
update_lcd(&gpsdata);
}
}
}
|