Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / icom.c
blob3bb38f8f41761af7c5b76d99da023433868547b8
1 /* $NetBSD$ */
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 * Packet routines
22 * These routines send a packet and receive the response. If an error
23 * (collision) occurs on transmit, the packet is resent. If an error
24 * occurs on receive (timeout), all input to the terminating FI is
25 * discarded and the packet is resent. If the maximum number of retries
26 * is not exceeded, the program returns the number of octets in the user
27 * buffer; otherwise, it returns zero.
29 * ICOM frame format
31 * Frames begin with a two-octet preamble PR-PR followyd by the
32 * transceiver address RE, controller address TX, control code CN, zero
33 * or more data octets DA (depending on command), and terminator FI.
34 * Since the bus is bidirectional, every octet output is echoed on
35 * input. Every valid frame sent is answered with a frame in the same
36 * format, but with the RE and TX fields interchanged. The CN field is
37 * set to NAK if an error has occurred. Otherwise, the data are returned
38 * in this and following DA octets. If no data are returned, the CN
39 * octet is set to ACK.
41 * +------+------+------+------+------+--//--+------+
42 * | PR | PR | RE | TX | CN | DA | FI |
43 * +------+------+------+------+------+--//--+------+
46 * Scraps
48 #define DICOM /dev/icom/ /* ICOM port link */
51 * Local function prototypes
53 static void doublefreq (double, u_char *, int);
57 * icom_freq(fd, ident, freq) - load radio frequency
59 int
60 icom_freq( /* returns 0 (ok), EIO (error) */
61 int fd, /* file descriptor */
62 int ident, /* ICOM radio identifier */
63 double freq /* frequency (MHz) */
66 u_char cmd[] = {PAD, PR, PR, 0, TX, V_SFREQ, 0, 0, 0, 0, FI,
67 FI};
68 int temp;
69 cmd[3] = ident;
70 if (ident == IC735)
71 temp = 4;
72 else
73 temp = 5;
74 doublefreq(freq * 1e6, &cmd[6], temp);
75 temp = write(fd, cmd, temp + 7);
76 return (0);
81 * doublefreq(freq, y, len) - double to ICOM frequency with padding
83 static void
84 doublefreq( /* returns void */
85 double freq, /* frequency */
86 u_char *x, /* radio frequency */
87 int len /* length (octets) */
90 int i;
91 char s1[11];
92 char *y;
94 sprintf(s1, " %10.0f", freq);
95 y = s1 + 10;
96 i = 0;
97 while (*y != ' ') {
98 x[i] = *y-- & 0x0f;
99 x[i] = x[i] | ((*y-- & 0x0f) << 4);
100 i++;
102 for (; i < len; i++)
103 x[i] = 0;
104 x[i] = FI;
108 * icom_open() - open and initialize serial interface
110 * This routine opens the serial interface for raw transmission; that
111 * is, character-at-a-time, no stripping, checking or monkeying with the
112 * bits. For Unix, an input operation ends either with the receipt of a
113 * character or a 0.5-s timeout.
116 icom_init(
117 char *device, /* device name/link */
118 int speed, /* line speed */
119 int trace /* trace flags */ )
121 TTY ttyb;
122 int fd, flags;
124 flags = trace;
125 fd = open(device, O_RDWR, 0777);
126 if (fd < 0)
127 return (fd);
129 tcgetattr(fd, &ttyb);
130 ttyb.c_iflag = 0; /* input modes */
131 ttyb.c_oflag = 0; /* output modes */
132 ttyb.c_cflag = IBAUD|CS8|CLOCAL; /* control modes (no read) */
133 ttyb.c_lflag = 0; /* local modes */
134 ttyb.c_cc[VMIN] = 0; /* min chars */
135 ttyb.c_cc[VTIME] = 5; /* receive timeout */
136 cfsetispeed(&ttyb, (u_int)speed);
137 cfsetospeed(&ttyb, (u_int)speed);
138 tcsetattr(fd, TCSANOW, &ttyb);
139 return (fd);
142 /* end program */