changeset 1:e5527fc2050b

libutil: copy from ice1-trau-tester
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 29 Aug 2024 12:58:56 +0000
parents 06c795d4781d
children 5c18cd38c8ad
files libutil/Makefile libutil/open_ts.c libutil/open_ts.h libutil/stdin_handler.c libutil/stdin_handler.h
diffstat 5 files changed, 138 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libutil/Makefile	Thu Aug 29 12:58:56 2024 +0000
@@ -0,0 +1,15 @@
+OBJS=	open_ts.o stdin_handler.o
+LIB=	libutil.a
+
+include ../config.defs
+
+CPPFLAGS=${OSMO_INCLUDE}
+
+all:	${LIB}
+
+${LIB}:	${OBJS}
+	ar rcu $@ ${OBJS}
+	ranlib $@
+
+clean:
+	rm -f *.[oa] errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libutil/open_ts.c	Thu Aug 29 12:58:56 2024 +0000
@@ -0,0 +1,60 @@
+/*
+ * The function in this common module is responsible for parsing the
+ * user-supplied E1 timeslot specification and then opening that ts
+ * in osmo-e1d.
+ *
+ * This code is based on osmo-e1d-pipe,
+ * (C) 2020-2022 by Harald Welte <laforge@osmocom.org>,
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <ctype.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <osmocom/e1d/proto_clnt.h>
+
+#include "open_ts.h"
+
+static int parse_component(const char *str, const char **outp, uint8_t *var)
+{
+	if (!isdigit(*str))
+		return -1;
+	*var = atoi(str);
+	while (isdigit(*str))
+		str++;
+	*outp = str;
+	return 0;
+}
+
+int open_e1d_ts(struct osmo_e1dp_client *e1_client, const char *ts_spec)
+{
+	uint8_t intf_nr, line_nr, ts_nr;
+	const char *cp;
+	int fd;
+
+	if (parse_component(ts_spec, &cp, &intf_nr) < 0) {
+inv_syntax:	fprintf(stderr, "error: invalid timeslot spec \"%s\"\n",
+			ts_spec);
+		exit(1);
+	}
+	if (*cp++ != ':')
+		goto inv_syntax;
+	if (parse_component(cp, &cp, &line_nr) < 0)
+		goto inv_syntax;
+	if (*cp++ != ':')
+		goto inv_syntax;
+	if (parse_component(cp, &cp, &ts_nr) < 0)
+		goto inv_syntax;
+	if (*cp)
+		goto inv_syntax;
+	fd = osmo_e1dp_client_ts_open(e1_client, intf_nr, line_nr, ts_nr,
+					E1DP_TSMODE_RAW, 160);
+	if (fd < 0) {
+		fprintf(stderr, "error: failed to open E1 ts %s\n", ts_spec);
+		exit(1);
+	}
+	return fd;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libutil/open_ts.h	Thu Aug 29 12:58:56 2024 +0000
@@ -0,0 +1,3 @@
+#pragma once
+
+int open_e1d_ts(struct osmo_e1dp_client *e1_client, const char *ts_spec);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libutil/stdin_handler.c	Thu Aug 29 12:58:56 2024 +0000
@@ -0,0 +1,49 @@
+/*
+ * Stdin handler function: gets called from Osmocom select loop for stdin,
+ * does line read and initial parsing into arguments, then calls
+ * program-supplied handler.
+ */
+
+#include <ctype.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <osmocom/core/select.h>
+
+#include "stdin_handler.h"
+
+#define	MAX_ARGS	16
+
+int stdin_select_cb(struct osmo_fd *ofd, unsigned int what)
+{
+	stdin_dispatch_t dispatch_func = ofd->data;
+	char buf[256], *argv[MAX_ARGS+1], *cp;
+	int argc;
+
+	fgets(buf, sizeof buf, stdin);
+	argc = 0;
+	for (cp = buf; ; ) {
+		while (isspace(*cp))
+			cp++;
+		if (*cp == '\0' || *cp == '#')
+			break;
+		if (argc >= MAX_ARGS) {
+			printf("error: input command exceeds MAX_ARGS\n");
+			return 0;
+		}
+		argv[argc] = cp;
+		while (*cp && !isspace(*cp))
+			cp++;
+		if (*cp)
+			*cp++ = '\0';
+		argc++;
+	}
+	if (!argc)
+		return 0;
+	argv[argc] = NULL;
+	dispatch_func(argc, argv);
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libutil/stdin_handler.h	Thu Aug 29 12:58:56 2024 +0000
@@ -0,0 +1,11 @@
+/*
+ * This header file defines the interface to the stdin handler module:
+ * gets called from Osmocom select loop for stdin, does line read and
+ * initial parsing into arguments, then calls program-supplied handler.
+ */
+
+#pragma once
+
+typedef void (*stdin_dispatch_t)(int argc, char **argv);
+
+int stdin_select_cb(struct osmo_fd *ofd, unsigned int what);