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