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