view rfcal/tsid-test/fc-tsid-shell.c @ 206:70bb11ba7485

fc-tsid-shell (fc-cmu200d development aid) written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 May 2017 00:34:23 +0000
parents
children
line wrap: on
line source

/*
 * This program connects to the RF calibration Test System Interface Daemon
 * (TSID) over the local socket interface and allows manual human interaction
 * with the TSID for development.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/errno.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>

extern int errno;

static char default_socket_pathname[] = "/tmp/fc_rftest_socket";

char *socket_pathname;
int sock;

connect_local_socket()
{
	/* local socket binding voodoo copied from osmocon */
	struct sockaddr_un local;
	unsigned int namelen;
	int rc;

	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sock < 0) {
		perror("socket(AF_UNIX, SOCK_STREAM, 0)");
		exit(1);
	}

	local.sun_family = AF_UNIX;
	strncpy(local.sun_path, socket_pathname, sizeof(local.sun_path));
	local.sun_path[sizeof(local.sun_path) - 1] = '\0';

	/* we use the same magic that X11 uses in Xtranssock.c for
	 * calculating the proper length of the sockaddr */
#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
	local.sun_len = strlen(local.sun_path);
#endif
#if defined(BSD44SOCKETS) || defined(SUN_LEN)
	namelen = SUN_LEN(&local);
#else
	namelen = strlen(local.sun_path) +
		  offsetof(struct sockaddr_un, sun_path) + 1;
#endif

	rc = connect(sock, (struct sockaddr *) &local, namelen);
	if (rc != 0) {
		perror(socket_pathname);
		exit(1);
	}

	return(0);
}

main(argc, argv)
	char **argv;
{
	char buf[BUFSIZ];
	fd_set fds, fds1;
	register int i, cc, max;

	switch (argc) {
	case 1:
		socket_pathname = default_socket_pathname;
		break;
	case 2:
		socket_pathname = argv[1];
		break;
	default:
		fprintf(stderr, "usage: %s [socket-pathname]\n", argv[0]);
		exit(1);
	}
	connect_local_socket();
	FD_ZERO(&fds);
	FD_SET(0, &fds);
	FD_SET(sock, &fds);
	max = sock + 1;
	for (;;) {
		bcopy(&fds, &fds1, sizeof(fd_set));
		i = select(max, &fds1, NULL, NULL, NULL);
		if (i < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			exit(1);
		}
		if (FD_ISSET(0, &fds1)) {
			cc = read(0, buf, sizeof buf);
			if (cc <= 0)
				exit(0);
			write(sock, buf, cc);
		}
		if (FD_ISSET(sock, &fds1)) {
			cc = read(sock, buf, sizeof buf);
			if (cc <= 0) {
				fprintf(stderr, "EOF/error on socket read\n");
				exit(1);
			}
			write(1, buf, cc);
		}
	}
}