summaryrefslogtreecommitdiff
path: root/contrib/ashctl.c
blob: 8917fa5ef373fa18d10b7213a628ebae9f17a7c8 (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
/*
 * This file is Copyright (c) 2010-2018 by the GPSD project
 * SPDX-License-Identifier: BSD-2-clause
 */
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>  /* For nanosleep() */
#include <termios.h>
#include <unistd.h>

#define MODE_RAW 0
#define MODE_NORMAL 1
#define ASHSPD_9600 5
#define ASHSPD_57600 8

static int nmea_send(int , const char *, ... );
static void nmea_add_checksum(char *);
static void serial_speed(int, int);
static void config_raw(int);
static void config_normal(int);

int main(int argc, char **argv) {
	int i, speed, op, fd;
	int rates[] = {57600, 9600, 115200, 4800, 19200, 1200, 0}; /* RTFM */
	char buf[BUFSIZ];

	/* check number of args */
	if (argc != 3){
u:		fprintf(stderr, "usage: ashctl <port> [raw|normal]\n"
				"normal = 9600, GGA+GSA+GSV+RMC+ZDA\n"
				"raw = 57600, normal+XPG+POS+SAT+MCA+PBN+SNV\n"
				);
		return 1;
	}

	/* validate command */
	if (strcmp(argv[2], "raw") == 0)
		op = MODE_RAW;
	else if (strcmp(argv[2], "normal") == 0)
		op = MODE_NORMAL;
	else
		goto u;

	if ((fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY, 0644)) == -1)
		err(1, "open");

	i = 0;
	/* spam receiver w/ config messages */
	while((speed = rates[i++])){
		fprintf(stderr,
			"\010\010\010\010\010\010\010\010"
			"\010\010\010\010\010\010\010\010"
			"\010\010\010\010\010\010\010\010"
			"\010\010\010\010\010\010\010\010"
			"configuring at %d bps...  ", speed);
		serial_speed(fd, speed);
		if (op == MODE_NORMAL) {
			config_normal(fd);
			serial_speed(fd, 9600);
		} else if (op == MODE_RAW) {
			config_raw(fd);
			serial_speed(fd, 57600);
		}

		sleep(1);
		i = read(fd, buf, BUFSIZ-1);
		buf[i] = '\0';
		if (strstr(buf, "$PASH") || strstr(buf, "$GP"))
			goto done;
	}
done:	fprintf(stderr,
		"\010\010\010\010\010\010\010\010"
		"\010\010\010\010\010\010\010\010"
		"\010\010\010\010\010\010\010\010"
		"\010\010\010\010\010\010\010\010"
		"receiver configuration done         \n");
	return 0;
}

static void serial_speed(int fd, int speed){
	struct termios term;

	tcgetattr(fd, &term);
	cfmakeraw(&term);
	cfsetospeed(&term, speed);
	cfsetispeed(&term, speed);
	if (tcsetattr(fd, TCSANOW | TCSAFLUSH, &term) == -1)
		err(1, "tcsetattr");

	tcflush(fd, TCIOFLUSH);
	return;
}

static void config_normal(int fd){
	nmea_send(fd, "$PASHS,NME,ALL,A,OFF"); /* silence outbound chatter */
	nmea_send(fd, "$PASHS,NME,ALL,B,OFF");
	nmea_send(fd, "$PASHS,NME,GGA,A,ON");
	nmea_send(fd, "$PASHS,NME,GSA,A,ON");
	nmea_send(fd, "$PASHS,NME,GSV,A,ON");
	nmea_send(fd, "$PASHS,NME,RMC,A,ON");
	nmea_send(fd, "$PASHS,NME,ZDA,A,ON");

	nmea_send(fd, "$PASHS,INI,%d,%d,,,0,",ASHSPD_9600, ASHSPD_9600);
	sleep(6); /* it takes 4-6 sec for the receiver to reboot */
	nmea_send(fd, "$PASHS,WAS,ON"); /* enable WAAS */
}

static void config_raw(int fd){
	nmea_send(fd, "$PASHS,NME,ALL,A,OFF"); /* silence outbound chatter */
	nmea_send(fd, "$PASHS,NME,ALL,B,OFF");
	nmea_send(fd, "$PASHS,NME,GGA,A,ON");
	nmea_send(fd, "$PASHS,NME,GSA,A,ON");
	nmea_send(fd, "$PASHS,NME,GSV,A,ON");
	nmea_send(fd, "$PASHS,NME,RMC,A,ON");
	nmea_send(fd, "$PASHS,NME,ZDA,A,ON");

	nmea_send(fd, "$PASHS,INI,%d,%d,,,0,",ASHSPD_57600, ASHSPD_9600);
	sleep(6); /* it takes 4-6 sec for the receiver to reboot */
	nmea_send(fd, "$PASHS,WAS,ON"); /* enable WAAS */

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

	nmea_send(fd, "$PASHS,NME,XMG,A,ON"); /* exception messages */
}

static void nmea_add_checksum(char *sentence)
/* add NMEA checksum to a possibly  *-terminated sentence */
{
	char *p = sentence;

	if (*p == '$') {
		unsigned char sum = '\0';
		char c;

		p++;
		while ( ((c = *p) != '*') && (c != '\0')) {
			sum ^= c;
			p++;
		}
		*p++ = '*';
		(void)snprintf(p, 5, "%02X\r\n", (unsigned int)sum);
	}
}

static int nmea_send(int fd, const char *fmt, ... )
/* ship a command to the GPS, adding * and correct checksum */
{
	size_t status;
	char buf[BUFSIZ];
	va_list ap;
	struct timespec delay;

	va_start(ap, fmt) ;
	(void)vsnprintf(buf, sizeof(buf)-5, fmt, ap);
	va_end(ap);
	(void)strncat(buf, "*", sizeof(buf)-1);
	nmea_add_checksum(buf);
	// (void)fputs(buf, stderr); /* debug output */
	tcflush(fd, TCIOFLUSH);
	status = (size_t)write(fd, buf, strlen(buf));
	tcdrain(fd);
        /* wait 100,000 uSec */
	delay.tv_sec = 0;
	delay.tv_nsec = 100000000L;
	nanosleep(&delay, NULL);

	if (status == strlen(buf)) {
		return (int)status;
	} else {
		perror("nmea_send");
		return -1;
	}
}