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