summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tests/Makefile.am8
-rw-r--r--tests/sercomm/sercomm_test.c136
-rw-r--r--tests/sercomm/sercomm_test.ok21
-rw-r--r--tests/testsuite.at6
4 files changed, 169 insertions, 2 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d95083b0..97af698c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -16,7 +16,7 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \
tlv/tlv_test gsup/gsup_test oap/oap_test fsm/fsm_test \
write_queue/wqueue_test socket/socket_test \
coding/coding_test conv/conv_gsm0503_test \
- abis/abis_test endian/endian_test
+ abis/abis_test endian/endian_test sercomm/sercomm_test
if ENABLE_MSGFILE
check_PROGRAMS += msgfile/msgfile_test
@@ -165,6 +165,9 @@ coding_coding_test_LDADD = \
endian_endian_test_SOURCES = endian/endian_test.c
endian_endian_test_LDADD = $(top_builddir)/src/libosmocore.la
+sercomm_sercomm_test_SOURCES = sercomm/sercomm_test.c
+sercomm_sercomm_test_LDADD = $(top_builddir)/src/libosmocore.la
+
# The `:;' works around a Bash 3.2 bug when the output is not writeable.
$(srcdir)/package.m4: $(top_srcdir)/configure.ac
:;{ \
@@ -207,7 +210,8 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \
osmo-auc-gen/osmo-auc-gen_test.sh \
osmo-auc-gen/osmo-auc-gen_test.ok \
osmo-auc-gen/osmo-auc-gen_test.err \
- conv/conv_gsm0503_test.ok endian/endian_test.ok
+ conv/conv_gsm0503_test.ok endian/endian_test.ok \
+ sercomm/sercomm_test.ok
DISTCLEANFILES = atconfig atlocal conv/gsm0503_test_vectors.c
BUILT_SOURCES = conv/gsm0503_test_vectors.c
diff --git a/tests/sercomm/sercomm_test.c b/tests/sercomm/sercomm_test.c
new file mode 100644
index 00000000..8b1ffdfa
--- /dev/null
+++ b/tests/sercomm/sercomm_test.c
@@ -0,0 +1,136 @@
+
+/* (C) 2017 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * 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 <stdio.h>
+#include <errno.h>
+
+#include <osmocom/core/sercomm.h>
+#include <osmocom/core/msgb.h>
+
+struct osmo_sercomm_inst g_osi;
+
+static const uint8_t valid_dlci3[] = { 0x7E, 3, 0x03, 'f', 'o', 'o', 0x7E };
+static const uint8_t valid_dlci23[] = { 0x7E, 23, 0x03, '2', '3', 0x7E };
+static const uint8_t valid_dlci23esc[] = { 0x7E, 23, 0x03, 0x7D, '2' ^ (1 << 5), '3', 0x7E };
+static const uint8_t valid_echo[] = { 0x7E, SC_DLCI_ECHO, 0x03, 'e', 'c', 'h', 'o', 0x7E };
+
+static void rx_ser_data(struct osmo_sercomm_inst *sc, const uint8_t *data, unsigned int len)
+{
+ unsigned int i;
+
+ printf("Feeding data into sercomm: %s\n", osmo_hexdump(data, len));
+ for (i = 0; i < len; i++) {
+ int rc = osmo_sercomm_drv_rx_char(sc, data[i]);
+ OSMO_ASSERT(rc == 1);
+ }
+}
+
+
+static void dlci_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg)
+{
+ printf("%s(): %s\n", __func__, msgb_hexdump(msg));
+}
+
+static struct msgb *create_mahlzeit_msg(void)
+{
+ struct msgb *msg = osmo_sercomm_alloc_msgb(10);
+ OSMO_ASSERT(msg);
+ msgb_put_u8(msg, 'M');
+ msgb_put_u8(msg, 'a');
+ msgb_put_u8(msg, 'h');
+ msgb_put_u8(msg, 'l');
+ msgb_put_u8(msg, 'z');
+ msgb_put_u8(msg, 'e');
+ msgb_put_u8(msg, 'i');
+ msgb_put_u8(msg, 't');
+ return msg;
+}
+
+static void drain_from_uart_side(struct osmo_sercomm_inst *osi)
+{
+ uint8_t ch;
+ int rc;
+
+ printf("Draining from UART: ");
+ while ((rc = osmo_sercomm_drv_pull(osi, &ch) == 1))
+ printf("0x%02x ", ch);
+ printf("\n");
+}
+
+static void test_echo(struct osmo_sercomm_inst *osi)
+{
+ printf("Testing built-in echo DLCI\n");
+ OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, SC_DLCI_ECHO) == 0);
+ rx_ser_data(osi, valid_echo, sizeof(valid_echo));
+ OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, SC_DLCI_ECHO) == 1);
+ drain_from_uart_side(osi);
+ OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, SC_DLCI_ECHO) == 0);
+}
+
+static void test_sercomm(void)
+{
+ int rc;
+ uint8_t ch;
+ struct msgb *msg;
+
+ printf("Initializing sercomm_inst\n");
+ osmo_sercomm_init(&g_osi);
+ g_osi.uart_id = 2342;
+
+ printf("Registering callback for invalid DLCI\n");
+ rc = osmo_sercomm_register_rx_cb(&g_osi, 255, NULL);
+ OSMO_ASSERT(rc == -EINVAL);
+
+ printf("Registering callback for valid DLCI\n");
+ rc = osmo_sercomm_register_rx_cb(&g_osi, 23, &dlci_rx_cb);
+ OSMO_ASSERT(rc == 0);
+
+ printf("Checking reject of overlod of valid DLCI\n");
+ rc = osmo_sercomm_register_rx_cb(&g_osi, 23, NULL);
+ OSMO_ASSERT(rc == -EBUSY);
+
+ printf("Checking Rx of incoming msg for valid DLCI\n");
+ rx_ser_data(&g_osi, valid_dlci23, sizeof(valid_dlci23));
+ printf("Checking Rx of incoming msg for unequipped DLCI\n");
+ rx_ser_data(&g_osi, valid_dlci3, sizeof(valid_dlci3));
+ printf("Checking Rx of incoming msg for valid DLCI\n");
+ rx_ser_data(&g_osi, valid_dlci23, sizeof(valid_dlci23));
+ printf("Checking Rx of incoming msg with escaped char for valid DLCI\n");
+ rx_ser_data(&g_osi, valid_dlci23esc, sizeof(valid_dlci23esc));
+
+ printf("Checking that no chars are to be transmitted\n");
+ OSMO_ASSERT(osmo_sercomm_drv_pull(&g_osi, &ch) == 0);
+
+ printf("Transmitting msgb through sercomm\n");
+ OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, 42) == 0);
+ msg = create_mahlzeit_msg();
+ osmo_sercomm_sendmsg(&g_osi, 42, msg);
+ OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, 42) == 1);
+ drain_from_uart_side(&g_osi);
+ OSMO_ASSERT(osmo_sercomm_tx_queue_depth(&g_osi, 42) == 0);
+
+ test_echo(&g_osi);
+}
+
+int main(int argc, char **argv)
+{
+ test_sercomm();
+}
diff --git a/tests/sercomm/sercomm_test.ok b/tests/sercomm/sercomm_test.ok
new file mode 100644
index 00000000..f2e65032
--- /dev/null
+++ b/tests/sercomm/sercomm_test.ok
@@ -0,0 +1,21 @@
+Initializing sercomm_inst
+Registering callback for invalid DLCI
+Registering callback for valid DLCI
+Checking reject of overlod of valid DLCI
+Checking Rx of incoming msg for valid DLCI
+Feeding data into sercomm: 7e 17 03 32 33 7e
+dlci_rx_cb(): 32 33
+Checking Rx of incoming msg for unequipped DLCI
+Feeding data into sercomm: 7e 03 03 66 6f 6f 7e
+Checking Rx of incoming msg for valid DLCI
+Feeding data into sercomm: 7e 17 03 32 33 7e
+dlci_rx_cb(): 32 33
+Checking Rx of incoming msg with escaped char for valid DLCI
+Feeding data into sercomm: 7e 17 03 7d 12 33 7e
+dlci_rx_cb(): 32 33
+Checking that no chars are to be transmitted
+Transmitting msgb through sercomm
+Draining from UART: 0x7e 0x2a 0x03 0x4d 0x61 0x68 0x6c 0x7a 0x65 0x69 0x74 0x7e
+Testing built-in echo DLCI
+Feeding data into sercomm: 7e 80 03 65 63 68 6f 7e
+Draining from UART: 0x7e 0x80 0x03 0x65 0x63 0x68 0x6f 0x7e
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 4c8950cb..63027d96 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -274,3 +274,9 @@ AT_KEYWORDS([endian])
cat $abs_srcdir/endian/endian_test.ok > expout
AT_CHECK([$abs_top_builddir/tests/endian/endian_test], [0], [expout], [ignore])
AT_CLEANUP
+
+AT_SETUP([sercomm])
+AT_KEYWORDS([sercomm])
+cat $abs_srcdir/sercomm/sercomm_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/sercomm/sercomm_test], [0], [expout], [ignore])
+AT_CLEANUP