isup: Make isup CIC parsing/using work on big endian machines as well

This is a hot fix to make CIC reading (and later status) work on
big endian machines. There might be a more elegant way to do it
and I will explore this later.
diff --git a/include/isup_types.h b/include/isup_types.h
index 083f2bf..c633fdf 100644
--- a/include/isup_types.h
+++ b/include/isup_types.h
@@ -48,6 +48,7 @@
 	uint8_t  pointer_int;
 };
 
+uint16_t isup_cic_to_local(const struct isup_msg_hdr *hdr);
 int mtp_link_set_isup(struct mtp_link_set *set, struct msgb *msg, int sls);
 
 int isup_parse_status(const uint8_t *data, uint8_t length);
diff --git a/src/isup.c b/src/isup.c
index 31b4221..2827f21 100644
--- a/src/isup.c
+++ b/src/isup.c
@@ -21,10 +21,13 @@
 #include <isup_types.h>
 #include <cellmgr_debug.h>
 #include <mtp_data.h>
+#include <mtp_level3.h>
 
 #include <osmocom/core/msgb.h>
 #include <osmocom/gsm/tlv.h>
 
+#include <endian.h>
+
 static struct msgb *isup_status_alloc(int cic, int msg_type, uint8_t *extra, int range, int val)
 {
 	struct isup_msg_hdr *hdr;
@@ -260,3 +263,15 @@
 
 	return rc;
 }
+
+uint16_t isup_cic_to_local(const struct isup_msg_hdr *hdr)
+{
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+	return hdr->cic;
+#elif __BYTE_ORDER == __BIG_ENDIAN
+	return c_swap_16(hdr->cic);
+#else
+	#error "Unknown endian"
+#endif
+}
+
diff --git a/src/isup_filter.c b/src/isup_filter.c
index 0e9a10c..b7c8e1a 100644
--- a/src/isup_filter.c
+++ b/src/isup_filter.c
@@ -61,6 +61,7 @@
 {
 	struct isup_msg_hdr *hdr;
 	int range;
+	uint16_t cic;
 
 	/* too small for an isup message? */
 	if (msgb_l3len(msg) < sizeof(*hdr)) {
@@ -76,6 +77,8 @@
 	}
 
 	hdr = (struct isup_msg_hdr *) msg->l3h;
+	cic = isup_cic_to_local(hdr);
+
 	switch (hdr->msg_type) {
 	case ISUP_MSG_GRS:
 		range = isup_parse_status(&hdr->data[0],
@@ -88,14 +91,14 @@
 
 		LOGP(DISUP, LOGL_DEBUG,
 			"Going to reset ISUP for app %s, cic %d range %d\n",
-			app->name, hdr->cic, range);
-		reset_cics(app, hdr->cic, range);
+			app->name, cic, range);
+		reset_cics(app, cic, range);
 		break;
 	case ISUP_MSG_RSC:
 		LOGP(DISUP, LOGL_DEBUG,
 			"Going to reset single CIC %d on app %s\n",
-			hdr->cic, app->name);
-		reset_cic(app, hdr->cic);
+			cic, app->name);
+		reset_cic(app, cic);
 		break;
 	}
 
diff --git a/tests/isup/isup_parse_test.c b/tests/isup/isup_parse_test.c
index 4010fcf..7e116b3 100644
--- a/tests/isup/isup_parse_test.c
+++ b/tests/isup/isup_parse_test.c
@@ -38,7 +38,7 @@
 	printf("Testing CIC parsing.\n");
 
 	hdr = (struct isup_msg_hdr *) isup_grs;
-	ASSERT(hdr->cic, 3);
+	ASSERT(isup_cic_to_local(hdr), 3);
 	ASSERT(hdr->msg_type, ISUP_MSG_GRS);
 }
 
@@ -53,7 +53,7 @@
 	hdr = (struct isup_msg_hdr *) isup_grs;
 	range = isup_parse_status(&hdr->data[0], 3);
 
-	ASSERT(hdr->cic, 3);
+	ASSERT(isup_cic_to_local(hdr), 3);
 	ASSERT(hdr->msg_type, ISUP_MSG_GRS);
 	ASSERT(range, 28);
 }
@@ -70,7 +70,7 @@
 	printf("Testing GRA parsing.\n");
 	hdr = (struct isup_msg_hdr *) isup_gra;
 	range = isup_parse_status(&hdr->data[0], 3);
-	ASSERT(hdr->cic, 2);
+	ASSERT(isup_cic_to_local(hdr), 2);
 	ASSERT(hdr->msg_type, ISUP_MSG_GRA);
 	ASSERT(range, 29);
 }
@@ -82,7 +82,7 @@
 
 	printf("Testing RSC parsing.\n");
 	hdr = (struct isup_msg_hdr *) isup_rsc;
-	ASSERT(hdr->cic, 1);
+	ASSERT(isup_cic_to_local(hdr), 1);
 	ASSERT(hdr->msg_type, ISUP_MSG_RSC);
 }