view autocal/rvinterf.c @ 90:713749548df6

fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 17 Jul 2017 05:51:31 +0000
parents e86779d5445c
children
line wrap: on
line source

/*
 * Interface to the DUT via rvinterf
 */

#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/pktmux.h>
#include <rvinterf/localsock.h>
#include <rvinterf/limits.h>
#include <rvinterf/exitcodes.h>

char *rvif_socket_pathname = "/tmp/rvinterf_socket";
int rvif_socket;

int rx_enable_state;
u_char rvi_msg[LOCALSOCK_MAX_MSG];
int rvi_msg_len;

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

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

	local.sun_family = AF_UNIX;
	strncpy(local.sun_path, rvif_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(rvif_socket, (struct sockaddr *) &local, namelen);
	if (rc != 0) {
		perror(rvif_socket_pathname);
		exit(ERROR_RVINTERF);
	}

	return(0);
}

static void
collect_bytes_from_rvi(buf, nbytes)
	u_char *buf;
{
	int cc;

	while (nbytes) {
		cc = read(rvif_socket, buf, nbytes);
		if (cc <= 0) {
			perror("read from rvinterf socket");
			exit(ERROR_RVINTERF);
		}
		buf += cc;
		nbytes -= cc;
	}
}

collect_rvi_msg()
{
	u_char lenbuf[2];

	collect_bytes_from_rvi(lenbuf, 2);
	rvi_msg_len = lenbuf[0] << 8 | lenbuf[1];
	if (rvi_msg_len < 1 || rvi_msg_len > LOCALSOCK_MAX_MSG) {
		fprintf(stderr, "Invalid length from rvinterf: %02X%02X\n",
			lenbuf[0], lenbuf[1]);
		exit(ERROR_RVINTERF);
	}
	collect_bytes_from_rvi(rvi_msg, rvi_msg_len);
	return(0);
}

send_rvimisc_command(cmdpkt, cmdlen)
	u_char *cmdpkt;
{
	u_char lenbuf[2];

	lenbuf[0] = 0;
	lenbuf[1] = cmdlen;
	write(rvif_socket, lenbuf, 2);
	write(rvif_socket, cmdpkt, cmdlen);
}

rx_control(enable)
{
	u_char cmdpkt[2];
	int cmdlen;

	/* are we already in the desired state? */
	if (rx_enable_state == enable)
		return(0);
	/* no, do the work */
	if (enable) {
		cmdpkt[0] = CLI2RVI_WANT_MUXPROTO;
		cmdpkt[1] = RVT_TM_HEADER;
		cmdlen = 2;
	} else {
		cmdpkt[0] = CLI2RVI_RESET_PACKET_RX;
		cmdlen = 1;
	}
	send_rvimisc_command(cmdpkt, cmdlen);
	collect_rvi_msg();
	if (rvi_msg[0] != RVI2CLI_LOCAL_CMD_RESP || rvi_msg_len < 2) {
		fprintf(stderr,
		"error: unexpected response to rvinterf local command\n");
		exit(ERROR_RVINTERF);
	}
	if (rvi_msg[1] != '+') {
		fprintf(stderr, "Error from rvinterf: %.*s\n", rvi_msg_len - 1,
			rvi_msg + 1);
		exit(ERROR_RVINTERF);
	}
	rx_enable_state = enable;
	return(0);
}

send_pkt_to_target(pkt, pktlen)
	u_char *pkt;
{
	u_char hdrbuf[3];
	int len1;

	len1 = pktlen + 1;
	hdrbuf[0] = len1 >> 8;
	hdrbuf[1] = len1 & 0xFF;
	hdrbuf[2] = CLI2RVI_PKT_TO_TARGET;
	write(rvif_socket, hdrbuf, 3);
	write(rvif_socket, pkt, pktlen);
}

target_pkt_exch(outpkt, outpktlen)
	u_char *outpkt;
{
	rx_control(1);
	send_pkt_to_target(outpkt, outpktlen);
	collect_rvi_msg();
	if (rvi_msg[0] != RVI2CLI_PKT_FROM_TARGET) {
		fprintf(stderr,
			"error: unexpected response type from rvinterf\n");
		exit(ERROR_RVINTERF);
	}
	return(0);
}

collect_extra_pkt_from_target()
{
	collect_rvi_msg();
	if (rvi_msg[0] != RVI2CLI_PKT_FROM_TARGET) {
		fprintf(stderr, "error: unexpected message from rvinterf\n");
		exit(ERROR_RVINTERF);
	}
	return(0);
}