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
|
#include <u.h>
#include <libc.h>
#include "dat.h"
char *serial = "/dev/eia0";
int ttyfd, ctlfd, debug;
int baud = Baud;
char *baudstr = "b%dd1r1pns1l8i1w5";
Place where = {-(74.0 + 23.9191/60.0), 40.0 + 41.1346/60.0};
void setline(void);
void evermore80(Place, int);
void evermore89(int);
void evermore8e(void);
void
setline(void){
char *serialctl;
serialctl = smprint("%sctl", serial);
if((ttyfd = open(serial, ORDWR)) < 0)
sysfatal("%s: %r", serial);
if((ctlfd = open(serialctl, OWRITE)) >= 0){
if(fprint(ctlfd, baudstr, baud) < 0)
sysfatal("%s: %r", serialctl);
}
free(serialctl);
}
enum {
GGAon = 0x01,
GLLon = 0x02,
GSAon = 0x04,
GSVon = 0x08,
RMCon = 0x10,
VTGon = 0x20,
CRCon = 0x40,
EMTon = 0x80
};
char*
putbyte(char *s, int v)
{
*s++ = v;
if((v & 0xff) == 0x10)
*s++ = v;
return s;
}
char*
putshort(char *s, int v)
{
s = putbyte(s, v);
s = putbyte(s, v >> 8);
return s;
}
char*
putlong(char *s, long v)
{
s = putbyte(s, v);
s = putbyte(s, v >> 8);
s = putbyte(s, v >> 16);
s = putbyte(s, v >> 24);
return s;
}
void
evermoresend(char *body, int l)
{
char buf[8], *s;
int crc, i;
s = buf;
*s++ = 0x10; /* DCE */
*s++ = 0x02; /* STX */
s = putbyte(s, l); /* length */
write(ttyfd, buf, s-buf); /* write header */
write(ttyfd, body, l); /* write body */
crc = 0;
for(i = 0; i < l; i++)
crc += body[i]; /* calculate crc */
s = buf;
s = putbyte(s, crc); /* checksum */
*s++ = 0x10; /* DCE */
*s++ = 0x03; /* ETX */
write(ttyfd, buf, s-buf); /* write trailer */
}
void
evermore80(Place pl, int baud)
{
char buf[32], *s;
long now, seconds, week;
fprint(2, "Evermore80");
time(&now);
seconds = now - 315964800;
week = (seconds / (7*24*3600));
seconds = seconds % (7*24*3600);
s = buf;
s = putbyte(s, 0x80); /* message ID */
s = putshort(s, week); /* week number */
s = putlong(s, seconds*100); /* seconds */
s = putshort(s, pl.lat*10.0); /* latitude tenths degree */
s = putshort(s, pl.lon*10.0); /* longitude tenths degree */
s = putshort(s, 100); /* altitude meters */
s = putshort(s, 0); /* datumn ID */
s = putbyte(s, 2); /* warm start */
s = putbyte(s, GGAon|GSAon|GSVon|RMCon|CRCon);
switch(baud){
case 4800: s = putbyte(s, 0); break;
case 9600: s = putbyte(s, 1); break;
case 19200: s = putbyte(s, 2); break;
case 38400: s = putbyte(s, 3); break;
default:
sysfatal("Illegal baud rate");
}
evermoresend(buf, s - buf);
fprint(2, "\n");
}
void
evermore89(int baud)
{
char buf[32], *s;
fprint(2, "Evermore89");
s = buf;
s = putbyte(s, 0x89); /* message ID */
s = putbyte(s, 0x01); /* set main serial port */
switch(baud){
case 4800: s = putbyte(s, 0x00); break;
case 9600: s = putbyte(s, 0x01); break;
case 19200: s = putbyte(s, 0x02); break;
case 38400: s = putbyte(s, 0x03); break;
default:
sysfatal("illegal baud rate %d", baud);
}
evermoresend(buf, s - buf);
fprint(2, "\n");
}
void
evermore8e(void)
{
char buf[32], *s;
fprint(2, "Evermore8e");
s = buf;
s = putbyte(s, 0x8e); /* message ID */
s = putbyte(s, GGAon|GSAon|GSVon|RMCon); /* all messages except GLL and VTG */
s = putbyte(s, 0x01); /* checksum on */
s = putbyte(s, 0x01); /* GGA update rate */
s = putbyte(s, 0x0b); /* GLL update rate */
s = putbyte(s, 0x0a); /* GSA update rate */
s = putbyte(s, 0x14); /* GSV update rate */
s = putbyte(s, 0x08); /* RMC update rate */
s = putbyte(s, 0x0d); /* VTG update rate */
evermoresend(buf, s - buf);
fprint(2, "\n");
}
void
main(int argc, char*argv[])
{
char *p;
Place pl;
int newbaud;
newbaud = -1;
pl = nowhere;
ARGBEGIN {
default:
fprint(2, "usage: %s [-b baud] [-d device] [-l longitude latitude] [-n newbaud]\n", argv0);
exits("usage");
case 'D':
debug++;
break;
case 'b':
baud = strtol(ARGF(), nil, 0);
break;
case 'd':
serial = ARGF();
break;
case 'l':
p = ARGF();
if(strtolatlon(p, &p, &pl) < 0)
sysfatal("bad position");
while(*p == ' ' || *p == '\t' || *p == '\n')
p++;
if(*p == '\0')
p = ARGF();
if (strtolatlon(p, &p, &pl) < 0)
sysfatal("bad position");
while(*p == ' ' || *p == '\t' || *p == '\n')
p++;
if(*p != '\0')
sysfatal("trailing gunk in position");
where = pl;
break;
case 'n':
newbaud = strtol(ARGF(), nil, 0);
break;
} ARGEND
if(newbaud < 0)
newbaud = baud;
fmtinstall('L', placeconv);
print("Initializing GPS to %d baud, at %L, time %s\n",
newbaud, where, ctime(time(nil)));
setline();
evermore80(where, newbaud);
}
|