freebsd-dev/contrib/ntp/libntp/icom.c
Gleb Smirnoff 9034852c84 MFV ntp-4.2.8p4 (r289715)
Security:       VuXML: c4a18a12-77fc-11e5-a687-206a8a720317
Security:	CVE-2015-7871
Security:	CVE-2015-7855
Security:	CVE-2015-7854
Security:	CVE-2015-7853
Security:	CVE-2015-7852
Security:	CVE-2015-7851
Security:	CVE-2015-7850
Security:	CVE-2015-7849
Security:	CVE-2015-7848
Security:	CVE-2015-7701
Security:	CVE-2015-7703
Security:	CVE-2015-7704, CVE-2015-7705
Security:	CVE-2015-7691, CVE-2015-7692, CVE-2015-7702
Security:	http://support.ntp.org/bin/view/Main/SecurityNotice#October_2015_NTP_Security_Vulner
Sponsored by:	Nginx, Inc.
2015-10-22 19:42:57 +00:00

179 lines
4.2 KiB
C

/*
* Program to control ICOM radios
*
* This is a ripoff of the utility routines in the ICOM software
* distribution. The only function provided is to load the radio
* frequency. All other parameters must be manually set before use.
*/
#include <config.h>
#include <ntp_stdlib.h>
#include <ntp_tty.h>
#include <l_stdlib.h>
#include <icom.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#ifdef SYS_WINNT
#undef write /* ports/winnt/include/config.h: #define write _write */
extern int async_write(int, const void *, unsigned int);
#define write(fd, data, octets) async_write(fd, data, octets)
#endif
/*
* Packet routines
*
* These routines send a packet and receive the response. If an error
* (collision) occurs on transmit, the packet is resent. If an error
* occurs on receive (timeout), all input to the terminating FI is
* discarded and the packet is resent. If the maximum number of retries
* is not exceeded, the program returns the number of octets in the user
* buffer; otherwise, it returns zero.
*
* ICOM frame format
*
* Frames begin with a two-octet preamble PR-PR followyd by the
* transceiver address RE, controller address TX, control code CN, zero
* or more data octets DA (depending on command), and terminator FI.
* Since the bus is bidirectional, every octet output is echoed on
* input. Every valid frame sent is answered with a frame in the same
* format, but with the RE and TX fields interchanged. The CN field is
* set to NAK if an error has occurred. Otherwise, the data are returned
* in this and following DA octets. If no data are returned, the CN
* octet is set to ACK.
*
* +------+------+------+------+------+--//--+------+
* | PR | PR | RE | TX | CN | DA | FI |
* +------+------+------+------+------+--//--+------+
*/
/*
* Scraps
*/
#define DICOM /dev/icom/ /* ICOM port link */
/*
* Local function prototypes
*/
static void doublefreq (double, u_char *, int);
/*
* icom_freq(fd, ident, freq) - load radio frequency
*
* returns:
* 0 (ok)
* -1 (error)
* 1 (short write to device)
*/
int
icom_freq(
int fd, /* file descriptor */
int ident, /* ICOM radio identifier */
double freq /* frequency (MHz) */
)
{
u_char cmd[] = {PAD, PR, PR, 0, TX, V_SFREQ, 0, 0, 0, 0, FI,
FI};
int temp;
int rc;
cmd[3] = (char)ident;
if (ident == IC735)
temp = 4;
else
temp = 5;
doublefreq(freq * 1e6, &cmd[6], temp);
rc = write(fd, cmd, temp + 7);
if (rc == -1) {
msyslog(LOG_ERR, "icom_freq: write() failed: %m");
return -1;
} else if (rc != temp + 7) {
msyslog(LOG_ERR, "icom_freq: only wrote %d of %d bytes.",
rc, temp+7);
return 1;
}
return 0;
}
/*
* doublefreq(freq, y, len) - double to ICOM frequency with padding
*/
static void
doublefreq( /* returns void */
double freq, /* frequency */
u_char *x, /* radio frequency */
int len /* length (octets) */
)
{
int i;
char s1[16];
char *y;
snprintf(s1, sizeof(s1), " %10.0f", freq);
y = s1 + 10;
i = 0;
while (*y != ' ') {
x[i] = *y-- & 0x0f;
x[i] = x[i] | ((*y-- & 0x0f) << 4);
i++;
}
for ( ; i < len; i++)
x[i] = 0;
x[i] = FI;
}
/*
* icom_init() - open and initialize serial interface
*
* This routine opens the serial interface for raw transmission; that
* is, character-at-a-time, no stripping, checking or monkeying with the
* bits. For Unix, an input operation ends either with the receipt of a
* character or a 0.5-s timeout.
*/
int
icom_init(
const char *device, /* device name/link */
int speed, /* line speed */
int trace /* trace flags */ )
{
TTY ttyb;
int fd;
int rc;
int saved_errno;
fd = tty_open(device, O_RDWR, 0777);
if (fd < 0)
return -1;
rc = tcgetattr(fd, &ttyb);
if (rc < 0) {
saved_errno = errno;
close(fd);
errno = saved_errno;
return -1;
}
ttyb.c_iflag = 0; /* input modes */
ttyb.c_oflag = 0; /* output modes */
ttyb.c_cflag = IBAUD|CS8|CLOCAL; /* control modes (no read) */
ttyb.c_lflag = 0; /* local modes */
ttyb.c_cc[VMIN] = 0; /* min chars */
ttyb.c_cc[VTIME] = 5; /* receive timeout */
cfsetispeed(&ttyb, (u_int)speed);
cfsetospeed(&ttyb, (u_int)speed);
rc = tcsetattr(fd, TCSANOW, &ttyb);
if (rc < 0) {
saved_errno = errno;
close(fd);
errno = saved_errno;
return -1;
}
return (fd);
}
/* end program */