***SPAM*** Re: [ntp:hackers] Patches to reinstate rank variable and add end keyword.

Poul-Henning Kamp phk at phk.freebsd.dk
Sat Aug 6 21:07:37 UTC 2005


Here is a somewhat trival example of how using the ISC event stuff
looks.  This is a small program that allows me to record/monitor
RS-232 traffic using two serial ports and a special cable.

---------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <eventlib.h>
#include <errno.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define VOR	1

static FILE *logfd;

static int last_out;

static void
doit(FILE *f, int fd, unsigned char *p, int len)
{
	int i;
	double a;
	static u_char cmd[BUFSIZ];

	fprintf(f, "%d ", fd);

	for (i = 0; i < len; i++)
		fprintf(f,"%02x", p[i]);
	if (fd == 5) {
		memcpy(cmd, p, len);
		fprintf(f,"\t");
		return;
	}
	fprintf(f, " C(%02x %d)", cmd[0], cmd[1]);
	a = p[2] * 256 + p[1];
	if (cmd[0] == 0x10 || cmd[0] == 0x50) {
		switch(cmd[1]) {
		case 32:
			fprintf(f, " Az		%f", a / 182.0);
			break;
		case 33:
			fprintf(f, " RfPower	%f", a / 163.84);
			break;
		case 35:
			fprintf(f, " 30Hz mod	%f %%", a / 327.7);
			break;
		case 36:
			fprintf(f, " 9960Hz	%f %%", a / 327.7);
			break;
		case 37:
			fprintf(f, " 30HzDev	%f", a / 327.7);
			break;
		}
	}
	if (cmd[0] == 0x08 || cmd[0] == 0x28) {
		fprintf(f, "\n");

		if (p[3] & 0x80)	fprintf(f, " RF-PHASE"); 
		if (p[3] & 0x40)	fprintf(f, " 30Hz FM");
		if (p[3] & 0x20)	fprintf(f, " RF-LEVEL");
		if (p[3] & 0x10)	fprintf(f, " IDENT");
		if (p[3] & 0x08)	fprintf(f, " FREQ");
		if (p[3] & 0x04)	fprintf(f, " 9960 Mz MOD");
		if (p[3] & 0x02)	fprintf(f, " 30 Hz MOD");
		if (p[3] & 0x01)	fprintf(f, " AZIMUTH");
		fprintf(f, "\n");

		if (p[4] & 0x80)	fprintf(f, " ALARM");
		if (p[4] & 0x40)	fprintf(f, " RES.");
		if (p[4] & 0x20)	fprintf(f, " Rem/Loc");
		if (p[4] & 0x10)	fprintf(f, " Ack EGB");
		if (p[4] & 0x08)	fprintf(f, " Mon Bypass");
		if (p[4] & 0x04)	fprintf(f, " BCPS fault");
		if (p[4] & 0x02)	fprintf(f, " +5vTx2");
		if (p[4] & 0x01)	fprintf(f, " +5vTx1");

	}
	fprintf(f,"\n");
}

static void
AddEvent(int fd, u_int detail)
{
	struct timespec ts;
	static unsigned char buf[BUFSIZ];
	static int bufp;

	ts = evNowTime();

	fprintf(logfd, "%d.%09ld chr %d %u\n",
	    ts.tv_sec, ts.tv_nsec, fd, detail);

	if (fd != last_out) {
		doit(stdout, last_out, buf, bufp);
		bufp = 0;
	}
	buf[bufp++] = detail;
	last_out = fd;
	return;

	if (fd == 0)
		printf("\033[m");
	else if (fd == 3)
		printf("\033[32m");
	else
		printf("\033[34m");

#ifdef VOR
	printf("%02x", detail & 0xff);
#else
	if (detail == '\n')
		printf("\\n\n");
	else if (detail == '\r')
		printf("\\r");
	else if (detail < ' ' || detail > '~')
		printf("\\x%02x", detail & 0xff);
	else
		printf("%c", detail);
#endif
}

static int fser[2];

static void
Modem(void)
{
	int i, m1, m2;
	static u_int thism, lastm;
	char buf[40];
	struct timespec ts;

	return;
	ts = evNowTime();

	i = ioctl(fser[0], TIOCMGET, &m1);
	i = ioctl(fser[1], TIOCMGET, &m2);

	thism = (m1 << 16) | m2;
	thism &= ~0x00070007;
	if (thism == lastm)
		return;
	buf[0] = '\0';
	if (thism & 0x00000020)
		strcat(buf, " 1 RTS");
	else
		strcat(buf, " 0 rts");
	if (thism & 0x00200000)
		strcat(buf, " 1 CTS");
	else
		strcat(buf, " 0 cts");
	if (thism & 0x00000100)
		strcat(buf, " 1 DSR");
	else
		strcat(buf, " 0 dsr");
	if (thism & 0x01000000)
		strcat(buf, " 1 DTR");
	else
		strcat(buf, " 0 drt");
	if (thism & 0x00000040)
		strcat(buf, " 1 DCD");
	else
		strcat(buf, " 0 dcd");
	if (thism & 0x00400000)
		strcat(buf, " 1 RI");
	else
		strcat(buf, " 0 ri");

	if (last_out)
		printf("\n");
	last_out = 0;
	printf("\033[31mM: %08x %s\n", thism, buf);

	fprintf(logfd, "%d.%09ld mdm 0x%08x %s\n",
	    ts.tv_sec, ts.tv_nsec, thism, buf);
	lastm = thism;
}

static void
TmrModem(evContext ctx __unused, void *uap __unused, struct timespec due __unused, struct timespec inter __unused)
{
	Modem();
}

/*
 * Event on terminal port
 */
static void
EvTerm(evContext ctx __unused, void *uap __unused, int fd, int eventmask)
{
	u_char c;
	int i;

	/* Read the character */
	i = read(fd, &c, 1);
	if (i != 1) {
		/* Bad... */
		printf("[%d/%x]", i, eventmask);
	} else 
		AddEvent(fd, c);
}

/*
 * Event on serial port
 */
static void
EvSer(evContext ctx __unused, void *uap __unused, int fd, int eventmask)
{
	u_char c;
	int i;

	Modem();
	/* Read the character */
	i = read(fd, &c, 1);
	if (i != 1) {
		/* Bad... */
		printf("[%d/%x]", i, eventmask);
	} else 
		AddEvent(fd, c);
	Modem();
}

static void
Usage(void)
{

	errx(1, "Usage: serd [-p port] [-s serial] [-v]");
}

int
main(int argc, char **argv)
{
	struct termios t;
	evContext ctx;
	evFileID ser_fid[2], term_fid;
	evTimerID ser_tid[2];
	int ch, port, verbose;
	char *serial[2];

	printf("\033[m");
	port = 666;
	verbose = 0;
	serial[0] = NULL;
	serial[1] = NULL;
	while ((ch = getopt(argc, argv, "p:s:v")) != -1) {
		switch (ch) {
		case 'p':
			port = atoi(optarg);
			break;
		case 's':
			if (serial[0] == NULL)
				serial[0] = optarg;
			else if (serial[1] == NULL)
				serial[1] = optarg;
			break;
		case 'v':
			verbose = 1;
			break;
		default:
			Usage();
		}
	}
	if (argc != optind)
		Usage();

	if (serial[0] == NULL)
		serial[0] = strdup("/dev/cuaU0");
	if (serial[1] == NULL)
		serial[1] = strdup("/dev/cuaU1");

	logfd = popen("cat -u >> _.sermon.log", "w");
	if (logfd == NULL)
		err(1, "could not open _.sermon.log");
	setlinebuf(logfd);
	setbuf(stdout, NULL);
	evCreate(&ctx);

	for (ch = 0; ch < 2; ch++) {
		fser[ch] = open(serial[ch], O_RDWR);
		if (fser[ch] < 0)
			err(1, "Could not open serial port %s", serial[ch]);

		tcgetattr(fser[ch], &t);
		cfmakeraw(&t);
		cfsetspeed(&t, B600);
		t.c_cc[VMIN] = 1;
		t.c_cc[VTIME] = 0;
		t.c_cflag |= CLOCAL;
		t.c_cflag &= ~CRTSCTS;
		t.c_cflag &= ~MDMBUF;
		t.c_cflag &= ~HUPCL;
		t.c_cflag |= CREAD;
		t.c_cflag |= CS8;
		t.c_cflag |= CSTOPB;
		t.c_cflag |= PARENB;

		tcsetattr(fser[ch], TCSANOW, &t);

		evSetTimer(ctx,
		    TmrModem, &fser[ch], evNowTime(), evConsTime(0, 100000000), &ser_tid[ch]);

		evSelectFD(ctx,
		     fser[ch], EV_READ | EV_EXCEPT, EvSer, NULL, &ser_fid[ch]);
	}
	evSelectFD(ctx,
	     0, EV_READ | EV_EXCEPT, EvTerm, NULL, &term_fid);

	evMainLoop(ctx);

	return (0);
}
---------------------------------------------------------------------------------


-- 
Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
phk at FreeBSD.ORG         | TCP/IP since RFC 956
FreeBSD committer       | BSD since 4.3-tahoe    
Never attribute to malice what can adequately be explained by incompetence.


More information about the hackers mailing list