changeset 228:fec90990f613

pirchgdbg started
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 19 Dec 2017 02:58:38 +0000
parents bb86424f78e6
children 84a4f6ef2d28
files .hgignore pircharge/Makefile pircharge/init.c pircharge/interf.c pircharge/main.c pircharge/pktsort.c
diffstat 6 files changed, 349 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Dec 17 19:30:22 2017 +0000
+++ b/.hgignore	Tue Dec 19 02:58:38 2017 +0000
@@ -57,6 +57,8 @@
 ^objgrep/objgrep$
 ^objgrep/objgrep-fe$
 
+^pircharge/pirchgdbg$
+
 ^pirollback/analyze$
 ^pirollback/catino$
 ^pirollback/dumpjournal$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pircharge/Makefile	Tue Dec 19 02:58:38 2017 +0000
@@ -0,0 +1,12 @@
+CC=	gcc
+CFLAGS=	-O2 -I/opt/freecalypso/include
+PROG=	pirchgdbg
+OBJS=	init.o interf.o main.o pktsort.o
+
+all:	${PROG}
+
+${PROG}:	${OBJS}
+	${CC} -o $@ ${OBJS}
+
+clean:
+	rm -f *.o *.out *errs ${PROG}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pircharge/init.c	Tue Dec 19 02:58:38 2017 +0000
@@ -0,0 +1,83 @@
+#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/exitcodes.h>
+
+extern char *socket_pathname;
+extern 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(ERROR_UNIX);
+	}
+
+	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(ERROR_RVINTERF);
+	}
+
+	return(0);
+}
+
+send_init_command(cmdpkt, cmdlen)
+	u_char *cmdpkt;
+{
+	u_char lenbuf[2];
+
+	lenbuf[0] = 0;
+	lenbuf[1] = cmdlen;
+	write(sock, lenbuf, 2);
+	write(sock, cmdpkt, cmdlen);
+}
+
+init()
+{
+	static u_char want_rvt_lost[9] = {CLI2RVI_WANT_RVTRACE,
+					  0xFF, 0xFF, 0xFF, 0xFF,
+					  0x00, 0x00, 0x00, 0x00};
+	static u_char want_rvt_drv[9]  = {CLI2RVI_WANT_RVTRACE,
+					  0xFF, 0xFF, 0xFF, 0xC7,
+					  0x00, 0x0A, 0x00, 0x00};
+	static u_char want_rvt_lls[9]  = {CLI2RVI_WANT_RVTRACE,
+					  0xFF, 0xFF, 0xFF, 0xFF,
+					  0x00, 0x1E, 0x00, 0x40};
+	static u_char want_etm_mux[2] = {CLI2RVI_WANT_MUXPROTO, RVT_TM_HEADER};
+
+	localsock_prep_for_length_rx();
+	send_init_command(want_rvt_lost, 9);
+	send_init_command(want_rvt_drv, 9);
+	send_init_command(want_rvt_lls, 9);
+	send_init_command(want_etm_mux, 2);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pircharge/interf.c	Tue Dec 19 02:58:38 2017 +0000
@@ -0,0 +1,103 @@
+/*
+ * This module implements the link to rvinterf.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <rvinterf/localsock.h>
+#include <rvinterf/exitcodes.h>
+
+extern int sock;
+
+u_char rvi_msg[LOCALSOCK_MAX_MSG];
+int rvi_msg_len;
+
+static int rx_state, rx_left;
+static u_char *rx_ptr;
+
+void
+localsock_prep_for_length_rx()
+{
+	rx_state = 0;
+	rx_ptr = rvi_msg;
+	rx_left = 2;
+}
+
+static void
+prep_for_message_rx()
+{
+	rx_state = 1;
+	rx_ptr = rvi_msg;
+	rx_left = rvi_msg_len;
+}
+
+void
+process_msg_from_rvinterf()
+{
+	switch (rvi_msg[0]) {
+	case RVI2CLI_PKT_FROM_TARGET:
+		process_pkt_from_target();
+		return;
+	case RVI2CLI_LOCAL_CMD_RESP:
+		if (rvi_msg_len < 2)
+			goto bad;
+		if (rvi_msg[1] == '+')
+			return;
+		fprintf(stderr, "Error from rvinterf: %.*s\n", rvi_msg_len - 1,
+			rvi_msg + 1);
+		exit(ERROR_RVINTERF);
+	default:
+	bad:
+		fprintf(stderr,
+			"Error: unexpected message type %02X from rvinterf\n",
+			rvi_msg[0]);
+		exit(ERROR_RVINTERF);
+	}
+}
+
+void
+handle_rvinterf_input()
+{
+	int cc;
+
+	cc = read(sock, rx_ptr, rx_left);
+	if (cc <= 0) {
+		perror("read from rvinterf socket");
+		exit(ERROR_RVINTERF);
+	}
+	rx_ptr += cc;
+	rx_left -= cc;
+	if (rx_left)
+		return;
+	/* got the thing, process it */
+	if (rx_state) {
+		process_msg_from_rvinterf();
+		localsock_prep_for_length_rx();
+	} else {
+		rvi_msg_len = rvi_msg[0] << 8 | rvi_msg[1];
+		if (rvi_msg_len < 1 || rvi_msg_len > LOCALSOCK_MAX_MSG) {
+			fprintf(stderr,
+				"Invalid length from rvinterf: %02X%02X\n",
+				rvi_msg[0], rvi_msg[1]);
+			exit(ERROR_RVINTERF);
+		}
+		prep_for_message_rx();
+	}
+}
+
+void
+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(sock, hdrbuf, 3);
+	write(sock, pkt, pktlen);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pircharge/main.c	Tue Dec 19 02:58:38 2017 +0000
@@ -0,0 +1,55 @@
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <rvinterf/exitcodes.h>
+
+char *socket_pathname = "/tmp/rvinterf_socket";
+int sock;
+
+int adccal_a, adccal_b;
+
+main(argc, argv)
+	char **argv;
+{
+	extern int optind;
+	extern char *optarg;
+	int c, sopt = 0;
+	fd_set fds;
+
+	while ((c = getopt(argc, argv, "s:")) != EOF)
+		switch (c) {
+		case 's':
+			socket_pathname = optarg;
+			sopt++;
+			continue;
+		case '?':
+		default:
+			/* error msg already printed */
+			exit(ERROR_USAGE);
+		}
+	if (argc != optind + 2) {
+		fprintf(stderr, "usage: %s adccal-a adccal-b\n", argv[0]);
+		exit(ERROR_USAGE);
+	}
+	adccal_a = atoi(argv[optind]);
+	adccal_b = atoi(argv[optind+1]);
+
+	connect_local_socket();
+	init();
+	for (;;) {
+		FD_ZERO(&fds);
+		FD_SET(sock, &fds);
+		c = select(sock+1, &fds, 0, 0, 0);
+		if (c < 0) {
+			if (errno == EINTR)
+				continue;
+			perror("select");
+			exit(ERROR_UNIX);
+		}
+		if (FD_ISSET(sock, &fds))
+			handle_rvinterf_input();
+		fflush(stdout);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pircharge/pktsort.c	Tue Dec 19 02:58:38 2017 +0000
@@ -0,0 +1,94 @@
+/*
+ * Here we sort out incoming packets from the target relayed via rvinterf.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <rvinterf/pktmux.h>
+#include <rvinterf/limits.h>
+#include <rvinterf/localsock.h>
+#include <rvinterf/localtypes.h>
+#include <rvinterf/etm.h>
+#include <rvinterf/exitcodes.h>
+
+extern u_char rvi_msg[];
+extern int rvi_msg_len;
+
+void
+print_rv_trace()
+{
+	char fmtbuf[MAX_PKT_FROM_TARGET*8];	/* size it generously */
+	int i, c;
+	char *dp;
+
+	dp = fmtbuf;
+	strcpy(dp, "RV ");
+	dp += 3;
+	/* the SWE static ID is sent MSB first */
+	for (i = 1; i <= 4; i++) {
+		sprintf(dp, "%02X", rvi_msg[i+1]);
+		dp += 2;
+	}
+	/* severity level */
+	sprintf(dp, " %d ", rvi_msg[6]);
+	dp = index(dp, '\0');
+	for (i = 7; i < rvi_msg_len; i++) {
+		c = rvi_msg[i];
+		if (c & 0x80) {
+			*dp++ = 'M';
+			*dp++ = '-';
+			c &= 0x7F;
+		}
+		if (c < 0x20) {
+			*dp++ = '^';
+			*dp++ = c + '@';
+		} else if (c == 0x7F) {
+			*dp++ = '^';
+			*dp++ = '?';
+		} else
+			*dp++ = c;
+	}
+	*dp = '\0';
+	printf("%s\n", fmtbuf);
+}
+
+void
+rvt_packet_rx()
+{
+	u32 useid;
+
+	if (rvi_msg_len < 7) {
+		fprintf(stderr, "Error: rvinterf sent us an invalid RVT msg\n");
+		exit(ERROR_RVINTERF);
+	}
+	print_rv_trace();
+	useid = rvi_msg[2] << 24 | rvi_msg[3] << 16 | rvi_msg[4] << 8
+		| rvi_msg[5];
+	if (useid != 0x000A0010)
+		return;
+	if (rvi_msg_len != 22)
+		return;
+	if (strncmp(rvi_msg + 7, "IQ EXT: ADC End", 15))
+		return;
+	/* ADC End handling will go here */
+}
+
+void
+process_pkt_from_target()
+{
+	switch (rvi_msg[1]) {
+	case RVT_RV_HEADER:
+		rvt_packet_rx();
+		return;
+	case RVT_TM_HEADER:
+		/* etm_packet_rx(); */
+		return;
+	default:
+		fprintf(stderr, "unexpected fwd of MUX %02X from rvinterf\n",
+			rvi_msg[1]);
+		exit(ERROR_RVINTERF);
+	}
+}