Change in osmo-e1d[master]: Add osmo-e1d-pipe utility

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Mon Jun 29 20:59:28 UTC 2020


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-e1d/+/19060 )


Change subject: Add osmo-e1d-pipe utility
......................................................................

Add osmo-e1d-pipe utility

This adds the 'osmo-e1d-pipe' utlility program, which can be
used as a command-line client to open a given E1 timeslot and
connect it to stdin/stdout.  This in turn allows to rediect
file input/output via the shell.

Change-Id: Ib9d55af786c87e15465b8e73493680b35afb5913
---
M .gitignore
M src/Makefile.am
A src/e1d-ts-pipe.c
3 files changed, 203 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-e1d refs/changes/60/19060/1

diff --git a/.gitignore b/.gitignore
index 7185862..aef7cd0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,5 +23,6 @@
 *.o
 
 src/osmo-e1d
+src/osmo-e1d-pipe
 contrib/e1-prbs-test/e1-prbs-test
 contrib/e1-prbs-test/ice40-e1-prbs-check
diff --git a/src/Makefile.am b/src/Makefile.am
index e811f0a..9c098e7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -27,6 +27,7 @@
 
 bin_PROGRAMS = \
 	osmo-e1d \
+	osmo-e1d-pipe \
 	$(NULL)
 
 osmo_e1d_SOURCES = \
@@ -41,3 +42,9 @@
 
 osmo_e1d_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOVTY_LIBS) \
 		 $(LIBOSMOUSB_LIBS) $(LIBUSB_LIBS) libosmo-e1d.la
+
+osmo_e1d_pipe_SOURCES = \
+	e1d-ts-pipe.c \
+	$(NULL)
+
+osmo_e1d_pipe_LDADD = $(LIBOSMOCORE_LIBS) libosmo-e1d.la
diff --git a/src/e1d-ts-pipe.c b/src/e1d-ts-pipe.c
new file mode 100644
index 0000000..b4eb6f5
--- /dev/null
+++ b/src/e1d-ts-pipe.c
@@ -0,0 +1,195 @@
+/* Virtual E1 interface pair: Two virtual interfaces with N lines each,
+ * where data written to A can be read from B and vice-versa.
+ *
+ * (C) 2020 by Harald Welte <laforge at osmocom.org>
+ *
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <getopt.h>
+#include <fcntl.h>
+
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/application.h>
+
+#include <osmocom/e1d/proto_clnt.h>
+
+
+static void *g_ctx;
+static struct osmo_e1dp_client *g_client;
+static struct osmo_fd ts_ofd;
+static int outfd = 1;
+static int infd = 0;
+
+
+static int ts_open(uint8_t intf_nr, uint8_t line_nr, uint8_t ts_nr,
+		   enum osmo_e1dp_ts_mode mode)
+{
+	int  rc = osmo_e1dp_client_ts_open(g_client, intf_nr, line_nr, ts_nr, mode);
+	if (rc < 0)
+		fprintf(stderr, "Cannot open e1d timeslot %u:%u:%u\n", intf_nr, line_nr, ts_nr);
+	return rc;
+}
+
+static int ts_fd_cb(struct osmo_fd *ofd, unsigned int what)
+{
+	uint8_t buf[32*10];
+	int rc, count;
+
+	if (what & OSMO_FD_READ) {
+		rc = read(ofd->fd, buf, sizeof(buf));
+		if (rc < 0 && errno != EAGAIN)
+			exit(3);
+		else if (rc > 0) {
+			count = rc;
+			rc = write(outfd, buf, count);
+			if (rc < 0 && errno != EAGAIN)
+				exit(3);
+		}
+	}
+
+	if (what & OSMO_FD_WRITE) {
+		rc = read(infd, buf, sizeof(buf));
+		if (rc < 0 && errno != EAGAIN)
+			exit(4);
+		else if (rc > 0) {
+			count = rc;
+			rc = write(ofd->fd, buf, count);
+			if (rc < 0 && errno != EAGAIN)
+				exit(4);
+		}
+
+	}
+
+	return 0;
+}
+
+static int set_nonblock(int fd)
+{
+	int rc, flags;
+
+	flags = fcntl(fd, F_GETFL);
+	OSMO_ASSERT(flags >= 0);
+
+	rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+	OSMO_ASSERT(rc >= 0);
+
+	return 0;
+}
+
+static void print_help(void)
+{
+	printf(
+	" -h --help                     This help message\n"
+	" -p --path PATH                Path of the osmo-e1d control socket\n"
+	" -i --interface <0-255>        E1 Interface Number\n"
+	" -l --line <0-255>             E1 Line Number\n"
+	" -t --timeslot <0-31>          E1 Timeslot Number\n"
+	" -m --mode (RAW|HDLC-FCS)      E1 Timeslot Mode\n"
+	);
+}
+
+int main(int argc, char **argv)
+{
+	int intf_nr = -1, line_nr = -1, ts_nr = -1;
+	enum osmo_e1dp_ts_mode mode = E1DP_TSMODE_RAW;
+	char *path = E1DP_DEFAULT_SOCKET;
+	int tsfd;
+	int option_index, rc;
+
+	g_ctx = talloc_named_const(NULL, 0, "g_ctx");
+	OSMO_ASSERT(g_ctx);
+
+	osmo_init_logging2(g_ctx, NULL);
+
+	/* FIXME: handle options */
+	while (1) {
+		int c;
+		static const struct option long_options[] = {
+			{ "help", 0, 0, 'h' },
+			{ "path", 1, 0, 'p' },
+			{ "interface", 1, 0, 'i' },
+			{ "line", 1, 0, 'l' },
+			{ "timeslot", 1, 0, 't' },
+			{ "mode", 1, 0, 'm' },
+			{ 0,0,0,0 }
+		};
+
+		c = getopt_long(argc, argv, "hp:i:l:t:m:", long_options, &option_index);
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'h':
+			print_help();
+			exit(0);
+		case 'p':
+			path = optarg;
+			break;
+		case 'i':
+			intf_nr = atoi(optarg);
+			break;
+		case 'l':
+			line_nr = atoi(optarg);
+			break;
+		case 't':
+			ts_nr = atoi(optarg);
+			break;
+		case 'm':
+			rc = get_string_value(osmo_e1dp_ts_mode_names, optarg);
+			if (rc < 0) {
+				fprintf(stderr, "Unknown mode '%s'\n", optarg);
+				exit(2);
+			}
+			mode = rc;
+			break;
+		}
+	}
+
+	if (intf_nr == -1 || line_nr == -1 || ts_nr == -1) {
+		fprintf(stderr, "You must at least specify interface, line and timeslot numbers\n");
+		exit(2);
+	}
+
+	g_client = osmo_e1dp_client_create(g_ctx, path);
+	if (!g_client) {
+		fprintf(stderr, "Cannot establish connection to osmo-e1d at %s\n", path);
+		exit(1);
+	}
+
+	tsfd = ts_open(intf_nr, line_nr, ts_nr, mode);
+	if (tsfd < 0)
+		exit(2);
+
+	osmo_fd_setup(&ts_ofd, tsfd, OSMO_FD_READ|OSMO_FD_WRITE, ts_fd_cb, NULL, 0);
+	osmo_fd_register(&ts_ofd);
+
+	set_nonblock(infd);
+	set_nonblock(outfd);
+
+	while (1) {
+		osmo_select_main(0);
+	}
+}
+

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-e1d/+/19060
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-e1d
Gerrit-Branch: master
Gerrit-Change-Id: Ib9d55af786c87e15465b8e73493680b35afb5913
Gerrit-Change-Number: 19060
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200629/0ce93936/attachment.htm>


More information about the gerrit-log mailing list