changeset 15:98ae717734d6

ater: starting skeleton
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 24 Jun 2024 04:01:38 +0000
parents 99426da5603d
children 4ffe22f5b4b5
files .hgignore ater/Makefile ater/globals.h ater/main.c ater/read_ts.c ater/record_ctrl.c ater/user_cmd.c
diffstat 7 files changed, 243 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Mon Jun 24 03:31:20 2024 +0000
+++ b/.hgignore	Mon Jun 24 04:01:38 2024 +0000
@@ -3,4 +3,5 @@
 \.[oa]$
 ^config\.defs$
 
+^ater/itt-ater-16$
 ^pcm/itt-pcm-one$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ater/Makefile	Mon Jun 24 04:01:38 2024 +0000
@@ -0,0 +1,22 @@
+PROG=	itt-ater-16
+OBJS=	main.o read_ts.o record_ctrl.o user_cmd.o
+LIBUTIL=../libutil/libutil.a
+
+include ../config.defs
+
+CPPFLAGS=${OSMO_INCLUDE}
+OSMO_LINK=${OSMO_LPATH} ${OSMO_RPATH} ${OSMO_LIBS}
+
+all:	${PROG}
+
+${OBJS}:	globals.h
+
+${PROG}: ${OBJS} ${LIBUTIL}
+	${CC} -o $@ ${OBJS} ${LIBUTIL} ${OSMO_LINK}
+
+install:
+	mkdir -p ${DESTDIR}${bindir}
+	install -c -m 755 ${PROG} ${DESTDIR}${bindir}
+
+clean:
+	rm -f *.o ${PROG} errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ater/globals.h	Mon Jun 24 04:01:38 2024 +0000
@@ -0,0 +1,16 @@
+/* global vars and intermodule-linkage functions in itt-ater-16 */
+
+#pragma once
+
+extern struct osmo_e1dp_client *g_client;
+extern int ts_fd;
+extern uint8_t readbuf[160];
+extern FILE *record_file;
+
+int ts_fd_cb(struct osmo_fd *ofd, unsigned int what);
+void transmit_e1_ts(void);
+
+void handle_user_cmd(int argc, char **argv);
+void cmd_record_start(int argc, char **argv);
+void cmd_record_stop(int argc, char **argv);
+void cmd_print_rx(int argc, char **argv);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ater/main.c	Mon Jun 24 04:01:38 2024 +0000
@@ -0,0 +1,81 @@
+/*
+ * This C module is the main for itt-ater-16, a program in the icE1 TRAU tester
+ * suite that operates on a single E1 timeslot on the Ater interface.
+ *
+ * This code is based on osmo-e1d-pipe,
+ * (C) 2020-2022 by Harald Welte <laforge@osmocom.org>,
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/application.h>
+#include <osmocom/e1d/proto_clnt.h>
+
+#include "../libutil/open_ts.h"
+#include "../libutil/stdin_handler.h"
+#include "globals.h"
+
+struct osmo_e1dp_client *g_client;
+int ts_fd;
+
+static const char *e1d_socket_path = E1DP_DEFAULT_SOCKET;
+static const char *timeslot_spec;
+static void *g_ctx;
+static struct osmo_fd ts_ofd, stdin_ofd;
+
+static void process_cmdline(int argc, char **argv)
+{
+	extern int optind;
+	extern char *optarg;
+	int c;
+
+	while ((c = getopt(argc, argv, "p:")) != EOF) {
+		switch (c) {
+		case 'p':
+			e1d_socket_path = optarg;
+			continue;
+		default:
+		usage:
+			fprintf(stderr, "usage: %s [-p socket] intf:line:ts\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc != optind + 1)
+		goto usage;
+	timeslot_spec = argv[optind];
+}
+
+int main(int argc, char **argv)
+{
+	process_cmdline(argc, argv);
+	g_ctx = talloc_named_const(NULL, 0, "g_ctx");
+	OSMO_ASSERT(g_ctx);
+	osmo_init_logging2(g_ctx, NULL);
+
+	g_client = osmo_e1dp_client_create(g_ctx, e1d_socket_path);
+	if (!g_client) {
+		fprintf(stderr, "error: cannot connect to osmo-e1d at %s\n",
+			e1d_socket_path);
+		exit(1);
+	}
+	ts_fd = open_e1d_ts(g_client, timeslot_spec);
+
+	osmo_fd_setup(&ts_ofd, ts_fd, OSMO_FD_READ, ts_fd_cb, NULL, 0);
+	OSMO_ASSERT(osmo_fd_register(&ts_ofd) == 0);
+
+	osmo_fd_setup(&stdin_ofd, 0, OSMO_FD_READ, stdin_select_cb,
+			handle_user_cmd, 0);
+	OSMO_ASSERT(osmo_fd_register(&stdin_ofd) == 0);
+
+	while (1) {
+		osmo_select_main(0);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ater/read_ts.c	Mon Jun 24 04:01:38 2024 +0000
@@ -0,0 +1,34 @@
+/*
+ * The function in this module gets called from Osmocom select loop
+ * whenever the data socket to osmo-e1d is ready for reading.
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <osmocom/core/select.h>
+
+#include "globals.h"
+
+uint8_t readbuf[160];
+FILE *record_file;
+
+int ts_fd_cb(struct osmo_fd *ofd, unsigned int what)
+{
+	int rc;
+
+	rc = read(ts_fd, readbuf, 160);
+	if (rc != 160) {
+		fprintf(stderr,
+			"error: read from ts returned %d instead of 160\n",
+			rc);
+		exit(1);
+	}
+	if (record_file)
+		fwrite(readbuf, 1, 160, record_file);
+	/* transmit_e1_ts(); */
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ater/record_ctrl.c	Mon Jun 24 04:01:38 2024 +0000
@@ -0,0 +1,50 @@
+/*
+ * Here we implement stdin commands that control recording of E1 timeslot
+ * read stream.
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <osmocom/core/select.h>
+
+#include "globals.h"
+
+void cmd_record_start(int argc, char **argv)
+{
+	if (argc != 2) {
+		printf("error: record command needs 1 argument\n");
+		return;
+	}
+	if (record_file) {
+		printf("error: recording already in progress\n");
+		return;
+	}
+	record_file = fopen(argv[1], "w");
+	if (!record_file)
+		perror(argv[1]);
+}
+
+void cmd_record_stop(int argc, char **argv)
+{
+	if (!record_file) {
+		printf("error: no recording in progress\n");
+		return;
+	}
+	fclose(record_file);
+	record_file = NULL;
+}
+
+void cmd_print_rx(int argc, char **argv)
+{
+	int i, j, off;
+
+	off = 0;
+	for (i = 0; i < 10; i++) {
+		for (j = 0; j < 16; j++)
+			printf(" %02X", readbuf[off++]);
+		putchar('\n');
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ater/user_cmd.c	Mon Jun 24 04:01:38 2024 +0000
@@ -0,0 +1,39 @@
+/*
+ * In this module we handle user-issued stdin commands during
+ * itt-ater-16 running session.
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <osmocom/core/select.h>
+
+#include "globals.h"
+
+static struct cmdtab {
+	char	*cmd;
+	void	(*func)(int argc, char **argv);
+} cmdtab[] = {
+	{"print-rx", cmd_print_rx},
+	{"record", cmd_record_start},
+	{"record-stop", cmd_record_stop},
+	/* table search terminator */
+	{NULL, NULL}
+};
+
+void handle_user_cmd(int argc, char **argv)
+{
+	struct cmdtab *tp;
+
+	for (tp = cmdtab; tp->cmd; tp++)
+		if (!strcmp(tp->cmd, argv[0]))
+			break;
+	if (!tp->func) {
+		printf("error: unknown or unimplemented command\n");
+		return;
+	}
+	tp->func(argc, argv);
+}