view autocal/tsidsock.c @ 63:131abadbd74d

doc/Rx-cal-theory written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 28 May 2017 03:21:34 +0000
parents f67f46e56355
children
line wrap: on
line source

/*
 * TSID local socket interface
 */

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

char *tsid_socket_pathname = "/tmp/fc_rftest_socket";
int tsid_socket;
char tsid_response[4096];

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

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

	local.sun_family = AF_UNIX;
	strncpy(local.sun_path, tsid_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(tsid_socket, (struct sockaddr *) &local, namelen);
	if (rc != 0) {
		perror(tsid_socket_pathname);
		exit(ERROR_RFTEST);
	}
	collect_tsid_response();
	return(0);
}

collect_tsid_response()
{
	char buf[BUFSIZ];
	int cc, pos;

	for (pos = 0; ; ) {
		cc = read(tsid_socket, buf, sizeof buf);
		if (cc <= 0) {
			perror("read from TSID socket");
			exit(ERROR_RFTEST);
		}
		if (pos + cc > sizeof tsid_response) {
			fprintf(stderr,
			"error: response from TSID exceeds our buffer size\n");
			exit(ERROR_RFTEST);
		}
		bcopy(buf, tsid_response + pos, cc);
		pos += cc;
		if (tsid_response[pos-1] == '\n')
			break;
	}
	tsid_response[pos-1] = '\0';
	if (tsid_response[0] != '+') {
		fprintf(stderr, "Error from TSID: %s\n", tsid_response);
		exit(ERROR_RFTEST);
	}
	return(0);
}

tsid_command(cmd)
	char *cmd;
{
	write(tsid_socket, cmd, strlen(cmd));
	collect_tsid_response();
	return(0);
}