asn1helpers: Add 28 bit conversion function and use it for Cell ID

The padding bits in the bit string are at the end and the byte-order is
MSB-first. This means the number needs to be shifted left so the padding
bits are the least significant.
diff --git a/src/asn1helpers.c b/src/asn1helpers.c
index f6378e6..791d0ed 100644
--- a/src/asn1helpers.c
+++ b/src/asn1helpers.c
@@ -33,6 +33,14 @@
 	bitstr->bits_unused = 0;
 }
 
+void asn1_u28_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
+{
+	*buf = htonl(in<<4);
+	bitstr->buf = (uint8_t *) buf;
+	bitstr->size = sizeof(uint32_t);
+	bitstr->bits_unused = 4;
+}
+
 void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in)
 {
 	*buf = htonl(in);
@@ -87,6 +95,13 @@
 	return ntohl(*(uint32_t *)in->buf);
 }
 
+uint32_t asn1bitstr_to_u28(const BIT_STRING_t *in)
+{
+	OSMO_ASSERT(in && in->size == sizeof(uint32_t) && in->bits_unused == 4);
+
+	return ntohl(*(uint32_t *)in->buf) >> 4;
+}
+
 uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in)
 {
 	OSMO_ASSERT(in && in->size == 3);
diff --git a/src/hnbgw_hnbap.c b/src/hnbgw_hnbap.c
index 3e3715c..97cb32b 100644
--- a/src/hnbgw_hnbap.c
+++ b/src/hnbgw_hnbap.c
@@ -120,7 +120,7 @@
 	ctx->id.lac = asn1str_to_u16(&ies.lac);
 	ctx->id.sac = asn1str_to_u16(&ies.sac);
 	ctx->id.rac = asn1str_to_u8(&ies.rac);
-	ctx->id.cid = asn1bitstr_to_u32(&ies.cellIdentity);
+	ctx->id.cid = asn1bitstr_to_u28(&ies.cellIdentity);
 	//ctx->id.mcc FIXME
 	//ctx->id.mnc FIXME
 
diff --git a/src/tests/hnb-test.c b/src/tests/hnb-test.c
index 92170d5..6c717d0 100644
--- a/src/tests/hnb-test.c
+++ b/src/tests/hnb-test.c
@@ -144,13 +144,12 @@
 	lac = 0xc0fe;
 	sac = 0xabab;
 	rac = 0x42;
-	cid = 0xadce00;
+	cid = 0xadceaab;
 
 	asn1_u16_to_str(&request.lac, &lac, lac);
 	asn1_u16_to_str(&request.sac, &sac, sac);
 	asn1_u8_to_str(&request.rac, &rac, rac);
-	asn1_u32_to_bitstring(&request.cellIdentity, &cid, cid);
-	request.cellIdentity.bits_unused = 4;
+	asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
 
 	request.hnB_Identity.hNB_Identity_Info.buf = identity;
 	request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);