[PATCH] smpp: Move the coding/mode detection into a utils file

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/OpenBSC@lists.osmocom.org/.

Holger Hans Peter Freyther holger at freyther.de
Sat Jul 13 15:24:55 UTC 2013


From: Holger Hans Peter Freyther <holger at moiji-mobile.com>

Make sure to not ever have issues with this code again, move the
utility code to a new file and create a basic testcase. The method
currently has 100% line and branch coverage.
---
 openbsc/.gitignore                |  1 +
 openbsc/configure.ac              |  1 +
 openbsc/src/libmsc/Makefile.am    |  2 +-
 openbsc/src/libmsc/smpp_openbsc.c | 36 +------------------
 openbsc/src/libmsc/smpp_smsc.h    |  5 +++
 openbsc/tests/Makefile.am         |  4 +++
 openbsc/tests/smpp/Makefile.am    | 12 +++++++
 openbsc/tests/smpp/smpp_test.c    | 73 +++++++++++++++++++++++++++++++++++++++
 openbsc/tests/smpp/smpp_test.err  |  2 ++
 openbsc/tests/smpp/smpp_test.ok   |  1 +
 openbsc/tests/testsuite.at        |  8 +++++
 11 files changed, 109 insertions(+), 36 deletions(-)
 create mode 100644 openbsc/tests/smpp/Makefile.am
 create mode 100644 openbsc/tests/smpp/smpp_test.c
 create mode 100644 openbsc/tests/smpp/smpp_test.err
 create mode 100644 openbsc/tests/smpp/smpp_test.ok

diff --git a/openbsc/.gitignore b/openbsc/.gitignore
index 53c8a4a..3743f69 100644
--- a/openbsc/.gitignore
+++ b/openbsc/.gitignore
@@ -61,6 +61,7 @@ tests/timer/timer_test
 tests/gprs/gprs_test
 tests/abis/abis_test
 tests/si/si_test
+tests/smpp/smpp_test
 
 tests/atconfig
 tests/atlocal
diff --git a/openbsc/configure.ac b/openbsc/configure.ac
index 3dd2212..8997e2e 100644
--- a/openbsc/configure.ac
+++ b/openbsc/configure.ac
@@ -160,6 +160,7 @@ AC_OUTPUT(
     tests/gprs/Makefile
     tests/si/Makefile
     tests/abis/Makefile
+    tests/smpp/Makefile
     doc/Makefile
     doc/examples/Makefile
     Makefile)
diff --git a/openbsc/src/libmsc/Makefile.am b/openbsc/src/libmsc/Makefile.am
index 7faee43..c36ba92 100644
--- a/openbsc/src/libmsc/Makefile.am
+++ b/openbsc/src/libmsc/Makefile.am
@@ -19,5 +19,5 @@ libmsc_a_SOURCES =	auth.c \
 			osmo_msc.c
 
 if BUILD_SMPP
-libmsc_a_SOURCES += smpp_smsc.c smpp_openbsc.c smpp_vty.c
+libmsc_a_SOURCES += smpp_smsc.c smpp_openbsc.c smpp_vty.c smpp_utils.c
 endif
diff --git a/openbsc/src/libmsc/smpp_openbsc.c b/openbsc/src/libmsc/smpp_openbsc.c
index 1744b0e..a3d2487 100644
--- a/openbsc/src/libmsc/smpp_openbsc.c
+++ b/openbsc/src/libmsc/smpp_openbsc.c
@@ -78,9 +78,6 @@ static struct tlv_t *find_tlv(struct tlv_t *head, uint16_t tag)
 	return NULL;
 }
 
-#define MODE_7BIT	7
-#define MODE_8BIT	8
-
 /*! \brief convert from submit_sm_t to gsm_sms */
 static int submit_to_sms(struct gsm_sms **psms, struct gsm_network *net,
 			 const struct submit_sm_t *submit)
@@ -468,39 +465,8 @@ static int deliver_to_esme(struct osmo_esme *esme, struct gsm_sms *sms,
 
 	/* Figure out SMPP DCS from TP-DCS */
 	dcs = sms->data_coding_scheme;
-	if ((dcs & 0xF0) == 0xF0) {
-		if (dcs & 0x04) {
-			/* bit 2 == 1: 8bit data */
-			deliver.data_coding = 0x02;
-			mode = MODE_8BIT;
-		} else {
-			/* bit 2 == 0: default alphabet */
-			deliver.data_coding = 0x01;
-			mode = MODE_7BIT;
-		}
-	} else if ((dcs & 0xE0) == 0) {
-		switch (dcs & 0xC) {
-		case 0:
-			deliver.data_coding = 0x01;
-			mode = MODE_7BIT;
-			break;
-		case 4:
-			deliver.data_coding = 0x02;
-			mode = MODE_8BIT;
-			break;
-		case 8:
-			deliver.data_coding = 0x08;	/* UCS-2 */
-			mode = MODE_8BIT;
-			break;
-		default:
-			goto unknown_mo;
-		}
-	} else {
-unknown_mo:
-		LOGP(DLSMS, LOGL_ERROR, "SMPP MO Unknown Data Coding 0x%02x\n",
-			dcs);
+	if (smpp_determine_scheme(dcs, &deliver.data_coding, &mode) == -1)
 		return -1;
-	}
 
 	/* Transparently pass on DCS via SMPP if requested */
 	if (esme->acl->dcs_transparent)
diff --git a/openbsc/src/libmsc/smpp_smsc.h b/openbsc/src/libmsc/smpp_smsc.h
index 86fbeb5..028e572 100644
--- a/openbsc/src/libmsc/smpp_smsc.h
+++ b/openbsc/src/libmsc/smpp_smsc.h
@@ -15,6 +15,9 @@
 #define SMPP_SYS_ID_LEN	16
 #define SMPP_PASSWD_LEN	16
 
+#define MODE_7BIT	7
+#define MODE_8BIT	8
+
 enum esme_read_state {
 	READ_ST_IN_LEN = 0,
 	READ_ST_IN_MSG = 1,
@@ -124,4 +127,6 @@ int smpp_route_pfx_add(struct osmo_smpp_acl *acl,
 		       const struct osmo_smpp_addr *pfx);
 int smpp_route_pfx_del(struct osmo_smpp_acl *acl,
 		       const struct osmo_smpp_addr *pfx);
+
+int smpp_determine_scheme(uint8_t dcs, uint8_t *data_coding, int *mode);
 #endif
diff --git a/openbsc/tests/Makefile.am b/openbsc/tests/Makefile.am
index c0e48df..c9afeed 100644
--- a/openbsc/tests/Makefile.am
+++ b/openbsc/tests/Makefile.am
@@ -4,6 +4,10 @@ if BUILD_NAT
 SUBDIRS += bsc-nat
 endif
 
+if BUILD_SMPP
+SUBDIRS += smpp
+endif
+
 
 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
diff --git a/openbsc/tests/smpp/Makefile.am b/openbsc/tests/smpp/Makefile.am
new file mode 100644
index 0000000..eb25965
--- /dev/null
+++ b/openbsc/tests/smpp/Makefile.am
@@ -0,0 +1,12 @@
+AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include -I$(top_srcdir)/src/libmsc
+AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOSCCP_CFLAGS) $(LIBOSMOABIS_CFLAGS) $(COVERAGE_CFLAGS)
+AM_LDFLAGS = $(COVERAGE_LDFLAGS)
+
+EXTRA_DIST = smpp_test.ok smpp_test.err
+
+noinst_PROGRAMS = smpp_test
+
+smpp_test_SOURCES = smpp_test.c \
+	$(top_srcdir)/src/libmsc/smpp_utils.c
+smpp_test_LDADD = $(LIBOSMOCORE_LIBS) \
+	$(top_srcdir)/src/libcommon/libcommon.a
diff --git a/openbsc/tests/smpp/smpp_test.c b/openbsc/tests/smpp/smpp_test.c
new file mode 100644
index 0000000..62fa9d2
--- /dev/null
+++ b/openbsc/tests/smpp/smpp_test.c
@@ -0,0 +1,73 @@
+/*
+ * (C) 2013 by Holger Hans Peter Freyther
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <openbsc/debug.h>
+
+#include <osmocom/core/application.h>
+#include <osmocom/core/backtrace.h>
+
+#include "smpp_smsc.h"
+
+struct coding_test {
+	uint8_t dcs;
+	uint8_t coding;
+	int	mode;
+	int	res;
+};
+
+static struct coding_test codecs[] = {
+	{ .dcs = 0xf6		, .coding = 0x02, .mode = MODE_8BIT,	.res = 0  },
+	{ .dcs = 0xf2		, .coding = 0x01, .mode = MODE_7BIT,	.res = 0  },
+	{ .dcs = 0x02		, .coding = 0x01, .mode = MODE_7BIT,	.res = 0  },
+	{ .dcs = 0x06		, .coding = 0x02, .mode = MODE_8BIT,	.res = 0  },
+	{ .dcs = 0x0A		, .coding = 0x08, .mode = MODE_8BIT,	.res = 0  },
+	{ .dcs = 0x0E		, .coding = 0xFF, .mode = 0xFF,		.res = -1 },
+	{ .dcs = 0xE0		, .coding = 0xFF, .mode = 0xFF,		.res = -1 },
+};
+
+static void test_coding_scheme(void)
+{
+	int i;
+	printf("Testing coding scheme support\n");
+
+	for (i = 0; i < ARRAY_SIZE(codecs); ++i) {
+		uint8_t coding;
+		int mode, res;
+
+		res = smpp_determine_scheme(codecs[i].dcs, &coding, &mode);
+		OSMO_ASSERT(res == codecs[i].res);
+		if (res != -1) {
+			OSMO_ASSERT(mode == codecs[i].mode);
+			OSMO_ASSERT(coding == codecs[i].coding);
+		}
+	}
+}
+
+int main(int argc, char **argv)
+{
+	osmo_init_logging(&log_info);
+	log_set_use_color(osmo_stderr_target, 0);
+	log_set_print_filename(osmo_stderr_target, 0);
+
+	test_coding_scheme();
+	return EXIT_SUCCESS;
+}
diff --git a/openbsc/tests/smpp/smpp_test.err b/openbsc/tests/smpp/smpp_test.err
new file mode 100644
index 0000000..ec966ba
--- /dev/null
+++ b/openbsc/tests/smpp/smpp_test.err
@@ -0,0 +1,2 @@
+SMPP MO Unknown Data Coding 0x0e
+SMPP MO Unknown Data Coding 0xe0
diff --git a/openbsc/tests/smpp/smpp_test.ok b/openbsc/tests/smpp/smpp_test.ok
new file mode 100644
index 0000000..fd44804
--- /dev/null
+++ b/openbsc/tests/smpp/smpp_test.ok
@@ -0,0 +1 @@
+Testing coding scheme support
diff --git a/openbsc/tests/testsuite.at b/openbsc/tests/testsuite.at
index e649f03..ea3fa1c 100644
--- a/openbsc/tests/testsuite.at
+++ b/openbsc/tests/testsuite.at
@@ -40,6 +40,14 @@ cat $abs_srcdir/bsc-nat/bsc_nat_test.ok > expout
 AT_CHECK([$abs_top_builddir/tests/bsc-nat/bsc_nat_test], [], [expout], [ignore])
 AT_CLEANUP
 
+AT_SETUP([smpp])
+AT_KEYWORDS([smpp])
+AT_CHECK([test "$enable_smpp_test" != no || exit 77])
+cat $abs_srcdir/smpp/smpp_test.ok > expout
+cat $abs_srcdir/smpp/smpp_test.err > experr
+AT_CHECK([$abs_top_builddir/tests/smpp/smpp_test], [], [expout], [experr])
+AT_CLEANUP
+
 AT_SETUP([si])
 AT_KEYWORDS([si])
 cat $abs_srcdir/si/si_test.ok > expout
-- 
1.8.3.2





More information about the OpenBSC mailing list