xref: /freebsd/contrib/ntp/libntp/icom.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Program to control ICOM radios
3  *
4  * This is a ripoff of the utility routines in the ICOM software
5  * distribution. The only function provided is to load the radio
6  * frequency. All other parameters must be manually set before use.
7  */
8 #include "icom.h"
9 #include <unistd.h>
10 #include <stdio.h>
11 
12 #ifdef HAVE_TERMIOS_H
13 # include <termios.h>
14 #endif /* HAVE_TERMIOS_H */
15 #ifdef HAVE_SYS_TERMIOS_H
16 # include <sys/termios.h>
17 #endif /* HAVE_SYS_TERMIOS_H */
18 
19 #include <fcntl.h>
20 #include <errno.h>
21 
22 /*
23  * Scraps
24  */
25 #define BMAX 50			/* max command length */
26 #define DICOM /dev/icom/	/* ICOM port link */
27 
28 /*
29  * FSA definitions
30  */
31 #define S_IDLE	0		/* idle */
32 #define S_HDR	1		/* header */
33 #define S_TX	2		/* address */
34 #define S_DATA	3		/* data */
35 #define S_ERROR	4		/* error */
36 
37 /*
38  * Local function prototypes
39  */
40 static void doublefreq		P((double, u_char *, int));
41 static int sndpkt		P((int, int, u_char *, u_char *));
42 static int sndoctet		P((int, int));
43 static int rcvoctet		P((int));
44 
45 /*
46  * Local variables
47  */
48 static int flags;		/* trace flags */
49 static int state;		/* fsa state */
50 
51 
52 /*
53  * icom_freq(fd, ident, freq) - load radio frequency
54  */
55 int
56 icom_freq(			/* returns 0 (ok), EIO (error) */
57 	int fd,			/* file descriptor */
58 	int ident,		/* ICOM radio identifier */
59 	double freq		/* frequency (MHz) */
60 	)
61 {
62 	u_char cmd[BMAX], rsp[BMAX];
63 	int temp;
64 	cmd[0] = V_SFREQ;
65 	if (ident == IC735)
66 		temp = 4;
67 	else
68 		temp = 5;
69 	doublefreq(freq * 1e6, &cmd[1], temp);
70 	temp = sndpkt(fd, ident, cmd, rsp);
71 	if (temp < 1 || rsp[0] != ACK)
72 		return (EIO);
73 	return (0);
74 }
75 
76 
77 /*
78  * doublefreq(freq, y, len) - double to ICOM frequency with padding
79  */
80 static void
81 doublefreq(			/* returns void */
82 	double freq,		/* frequency */
83 	u_char *x,		/* radio frequency */
84 	int len			/* length (octets) */
85 	)
86 {
87 	int i;
88 	char s1[11];
89 	char *y;
90 
91 	sprintf(s1, " %10.0f", freq);
92 	y = s1 + 10;
93 	i = 0;
94 	while (*y != ' ') {
95 		x[i] = *y-- & 0x0f;
96 		x[i] = x[i] | ((*y-- & 0x0f) << 4);
97 		i++;
98 	}
99 	for (; i < len; i++)
100 		x[i] = 0;
101 	x[i] = FI;
102 }
103 
104 
105 /*
106  * Packet routines
107  *
108  * These routines send a packet and receive the response. If an error
109  * (collision) occurs on transmit, the packet is resent. If an error
110  * occurs on receive (timeout), all input to the terminating FI is
111  * discarded and the packet is resent. If the maximum number of retries
112  * is not exceeded, the program returns the number of octets in the user
113  * buffer; otherwise, it returns zero.
114  *
115  * ICOM frame format
116  *
117  * Frames begin with a two-octet preamble PR-PR followyd by the
118  * transceiver address RE, controller address TX, control code CN, zero
119  * or more data octets DA (depending on command), and terminator FI.
120  * Since the bus is bidirectional, every octet output is echoed on
121  * input. Every valid frame sent is answered with a frame in the same
122  * format, but with the RE and TX fields interchanged. The CN field is
123  * set to NAK if an error has occurred. Otherwise, the data are returned
124  * in this and following DA octets. If no data are returned, the CN
125  * octet is set to ACK.
126  *
127  *	+------+------+------+------+------+--//--+------+
128  *	|  PR  |  PR  |  RE  |  TX  |  CN  |  DA  |  FI  |
129  *	+------+------+------+------+------+--//--+------+
130  */
131 /*
132  * icom_open() - open and initialize serial interface
133  *
134  * This routine opens the serial interface for raw transmission; that
135  * is, character-at-a-time, no stripping, checking or monkeying with the
136  * bits. For Unix, an input operation ends either with the receipt of a
137  * character or a 0.5-s timeout.
138  */
139 int
140 icom_init(
141 	char *device,		/* device name/link */
142 	int speed,		/* line speed */
143 	int trace		/* trace flags */	)
144 {
145 	struct termios ttyb;
146 	int fd;
147 
148 	flags = trace;
149 	fd = open(device, O_RDWR, 0777);
150 	if (fd < 0)
151 		return (fd);
152 	tcgetattr(fd, &ttyb);
153 	ttyb.c_iflag = 0;	/* input modes */
154 	ttyb.c_oflag = 0;	/* output modes */
155 	ttyb.c_cflag = IBAUD|CS8|CREAD|CLOCAL;	/* control modes */
156 	ttyb.c_lflag = 0;	/* local modes */
157 	ttyb.c_cc[VMIN] = 0;	/* min chars */
158 	ttyb.c_cc[VTIME] = 5;	/* receive timeout */
159 	cfsetispeed(&ttyb, (u_int)speed);
160 	cfsetospeed(&ttyb, (u_int)speed);
161 	tcsetattr(fd, TCSANOW, &ttyb);
162 	return (fd);
163 }
164 
165 
166 /*
167  * sndpkt(r, x, y) - send packet and receive response
168  *
169  * This routine sends a command frame, which consists of all except the
170  * preamble octets PR-PR. It then listens for the response frame and
171  * returns the payload to the caller. The routine checks for correct
172  * response header format; that is, the length of the response vector
173  * returned to the caller must be at least 2 and the RE and TX octets
174  * must be interchanged; otherwise, the operation is retried up to
175  * the number of times specified in a global variable.
176  *
177  * The trace function, which is enabled by the P_TRACE bit of the global
178  * flags variable, prints all characters received or echoed on the bus
179  * preceded by a T (transmit) or R (receive). The P_ERMSG bit of the
180  * flags variable enables printing of bus error messages.
181  *
182  * Note that the first octet sent is a PAD in order to allow time for
183  * the radio to flush its receive buffer after sending the previous
184  * response. Even with this precaution, some of the older radios
185  * occasionally fail to receive a command and it has to be sent again.
186  */
187 static int
188 sndpkt(				/* returns octet count */
189 	int fd,			/* file descriptor */
190 	int r,			/* radio address */
191 	u_char *cmd,		/* command vector */
192 	u_char *rsp		/* response vector */
193 	)
194 {
195 	int i, j, temp;
196 
197 	(void)tcflush(fd, TCIOFLUSH);
198 	for (i = 0; i < RETRY; i++) {
199 		state = S_IDLE;
200 
201 		/*
202 		 * Transmit packet.
203 		 */
204 		if (flags & P_TRACE)
205 			printf("icom T:");
206 		sndoctet(fd, PAD);	/* send header */
207 		sndoctet(fd, PR);
208 		sndoctet(fd, PR);
209 		sndoctet(fd, r);
210 		sndoctet(fd, TX);
211 		for (j = 0; j < BMAX; j++) { /* send body */
212 			if (sndoctet(fd, cmd[j]) == FI)
213 				break;
214 		}
215 		while (rcvoctet(fd) != FI); /* purge echos */
216 		if (cmd[0] == V_FREQT || cmd[0] == V_MODET)
217 			return (0);	/* shortcut for broadcast */
218 
219 		/*
220 		 * Receive packet. First, delete all characters
221 		 * preceeding a PR, then discard all PRs. Check that the
222 		 * RE and TX fields are correctly interchanged, then
223 		 * copy the remaining data and FI to the user buffer.
224 		 */
225 		if (flags & P_TRACE)
226 			printf("\nicom R:");
227 		j = 0;
228 		while ((temp = rcvoctet(fd)) != FI) {
229 			switch (state) {
230 
231 			case S_IDLE:
232 				if (temp != PR)
233 					continue;
234 				state = S_HDR;
235 				break;
236 
237 			case S_HDR:
238 				if (temp == PR) {
239 					continue;
240 				} else if (temp != TX) {
241 					if (flags & P_ERMSG)
242 						printf(
243 						    "icom: TX error\n");
244 					state = S_ERROR;
245 				}
246 				state = S_TX;
247 				break;
248 
249 			case S_TX:
250 				if (temp != r) {
251 					if (flags & P_ERMSG)
252 						printf(
253 						    "icom: RE error\n");
254 					state = S_ERROR;
255 				}
256 				state = S_DATA;
257 				break;
258 
259 			case S_DATA:
260 				if (j >= BMAX ) {
261 					if (flags & P_ERMSG)
262 						printf(
263 					    "icom: buffer overrun\n");
264 					state = S_ERROR;
265 					j = 0;
266 				}
267 				rsp[j++] = (u_char)temp;
268 				break;
269 
270 			case S_ERROR:
271 				break;
272 			}
273 		}
274 		if (flags & P_TRACE)
275 			printf("\n");
276 		if (j > 0) {
277 			rsp[j++] = FI;
278 			return (j);
279 		}
280 	}
281 	if (flags & P_ERMSG)
282 		printf("icom: retries exceeded\n");
283 	return (0);
284 }
285 
286 
287 /*
288  * Interface routines
289  *
290  * These routines read and write octets on the bus. In case of receive
291  * timeout a FI code is returned. In case of output collision (echo
292  * does not match octet sent), the remainder of the collision frame
293  * (including the trailing FI) is discarded.
294  */
295 /*
296  * sndoctet(fd, x) - send octet
297  */
298 static int
299 sndoctet(			/* returns octet */
300 	int fd,			/* file descriptor */
301 	int x			/* octet */
302 	)
303 {
304 	u_char y;
305 
306 	y = (u_char)x;
307 	write(fd, &y, 1);
308 	return (x);
309 }
310 
311 
312 /*
313  * rcvoctet(fd) - receive octet
314  */
315 static int
316 rcvoctet(			/* returns octet */
317 	int fd			/* file descriptor */
318 	)
319 {
320 	u_char y;
321 
322 	if (read(fd, &y, 1) < 1)
323 		y = FI;		/* come here if timeout */
324 	if (flags & P_TRACE && y != PAD)
325 		printf(" %02x", y);
326 	return (y);
327 }
328 
329 /* end program */
330