[PATCH] libosmocore[master]: gsm0808: Add utils for Cell Identifier List

2017-04-04 Thread dexter
Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

https://gerrit.osmocom.org/2181

to look at the new patch set (#3).

gsm0808: Add utils for Cell Identifier List

The planned support for true A over IP requires the encoding of
the a Cell Identifier List element (see also BSS_MAP_MSG_PAGING).

This commt adds encoding/decoding functionality and tests for
the element mentioned above, however, it is not yet actively used.

Change-Id: I625245dd1dd396fc2bc189e8cd2c444a33042528
---
M include/osmocom/gsm/gsm0808_utils.h
M include/osmocom/gsm/protocol/gsm_08_08.h
M src/gsm/gsm0808_utils.c
M src/gsm/libosmogsm.map
M tests/gsm0808/gsm0808_test.c
5 files changed, 181 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/81/2181/3

diff --git a/include/osmocom/gsm/gsm0808_utils.h 
b/include/osmocom/gsm/gsm0808_utils.h
index edaa915..e335aa4 100644
--- a/include/osmocom/gsm/gsm0808_utils.h
+++ b/include/osmocom/gsm/gsm0808_utils.h
@@ -62,3 +62,11 @@
 /* Decode Encryption Information element */
 int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
 const uint8_t *elem, uint8_t len);
+
+/* Encode Cell Identifier List element */
+uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
+struct gsm0808_cell_id_list *cil);
+
+/* Decode Cell Identifier List element */
+int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
+const uint8_t *elem, uint8_t len);
diff --git a/include/osmocom/gsm/protocol/gsm_08_08.h 
b/include/osmocom/gsm/protocol/gsm_08_08.h
index 3939aed..e5e7e1e 100644
--- a/include/osmocom/gsm/protocol/gsm_08_08.h
+++ b/include/osmocom/gsm/protocol/gsm_08_08.h
@@ -457,3 +457,11 @@
uint8_t key[ENCRY_INFO_KEY_MAXLEN];
unsigned int key_len;
 };
+
+/* 3GPP TS 48.008 3.2.2.27 Cell Identifier List */
+#define CELL_ID_LIST_LAC_MAXLEN 127
+struct gsm0808_cell_id_list {
+   uint8_t id_discr;
+   uint16_t id_list_lac[CELL_ID_LIST_LAC_MAXLEN];
+   unsigned int id_list_len;
+};
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index 690ca2e..0ad0268 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -450,3 +450,81 @@
 
return (int)(elem - old_elem);
 }
+
+/* Encode Cell Identifier List element */
+uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
+struct gsm0808_cell_id_list *cil)
+{
+   uint8_t *old_tail;
+   uint8_t *tlv_len;
+   unsigned int i;
+
+   OSMO_ASSERT(msg);
+   OSMO_ASSERT(cil);
+
+   msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
+   tlv_len = msgb_put(msg, 1);
+   old_tail = msg->tail;
+
+   msgb_put_u8(msg, cil->id_discr & 0x0f);
+
+   switch (cil->id_discr) {
+   case CELL_IDENT_LAC:
+   OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN)
+   for (i=0;iid_list_len;i++) {
+   msgb_put_u16(msg, cil->id_list_lac[i]);
+   }
+   break;
+   case CELL_IDENT_BSS:
+   /* Does not have any list items */
+   break;
+   default:
+   /* FIXME: Implement support for all identifier list elements */
+   OSMO_ASSERT(false);
+   }
+
+   *tlv_len = (uint8_t) (msg->tail - old_tail);
+   return *tlv_len + 2;
+}
+
+/* Decode Cell Identifier List element */
+int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
+const uint8_t *elem, uint8_t len)
+{
+   uint8_t id_discr;
+   const uint8_t *old_elem = elem;
+   unsigned int item_count = 0;
+
+   OSMO_ASSERT(cil);
+   if (!elem)
+   return -EINVAL;
+   if (len <= 0)
+   return -EINVAL;
+
+   memset(cil, 0, sizeof(*cil));
+
+   id_discr = *elem & 0x0f;
+   elem++;
+   len--;
+
+   cil->id_discr = id_discr;
+
+   switch (id_discr) {
+   case CELL_IDENT_LAC:
+   while (len >= 2) {
+   cil->id_list_lac[item_count] = osmo_load16be(elem);
+   elem += 2;
+   item_count++;
+   len -= 2;
+   }
+   case CELL_IDENT_BSS:
+   /* Does not have any list items */
+   break;
+   default:
+   /* FIXME: Implement support for all identifier list elements */
+   return -EINVAL;
+   }
+
+   cil->id_list_len = item_count;
+   return (int)(elem - old_elem);
+}
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index e64fdd9..ac8d467 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -150,6 +150,8 @@
 gsm0808_dec_channel_type;
 gsm0808_enc_encrypt_info;
 gsm0808_dec_encrypt_info;
+gsm0808_enc_cell_id_list;
+gsm0808_dec_cell_id_list;
 
 gsm0858_rsl_ul_meas_enc;
 
diff --git a/tests/gsm0808/gsm0808_test.c b/tests/gsm0808/gsm0808_test.c
index 

[PATCH] libosmocore[master]: gsm0808: Add utils for Cell Identifier List

2017-03-30 Thread dexter

Review at  https://gerrit.osmocom.org/2181

gsm0808: Add utils for Cell Identifier List

The planned support for true A over IP requires the encoding of
the a Cell Identifier List element (see also BSS_MAP_MSG_PAGING).

This commt adds encoding/decoding functionality and tests for
the element mentioned above, however, it is not yet actively used.

Change-Id: I625245dd1dd396fc2bc189e8cd2c444a33042528
---
M include/osmocom/gsm/gsm0808_utils.h
M include/osmocom/gsm/protocol/gsm_08_08.h
M src/gsm/gsm0808_utils.c
M src/gsm/libosmogsm.map
M tests/gsm0808/gsm0808_test.c
5 files changed, 195 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/81/2181/1

diff --git a/include/osmocom/gsm/gsm0808_utils.h 
b/include/osmocom/gsm/gsm0808_utils.h
index 2196f94..c9fd124 100644
--- a/include/osmocom/gsm/gsm0808_utils.h
+++ b/include/osmocom/gsm/gsm0808_utils.h
@@ -57,3 +57,10 @@
 /* Decode Encryption Information element */
 struct gsm0808_encrypt_info *gsm0808_dec_encrypt_info(const void *ctx,
  struct msgb *msg);
+
+/* Encode Cell Identifier List element */
+struct msgb *gsm0808_enc_cell_id_list(struct gsm0808_cell_id_list *cil);
+
+/* Decode Cell Identifier List element */
+struct gsm0808_cell_id_list *gsm0808_dec_cell_id_list(const void *ctx,
+ struct msgb *msg);
diff --git a/include/osmocom/gsm/protocol/gsm_08_08.h 
b/include/osmocom/gsm/protocol/gsm_08_08.h
index d52af9f..ad7f8eb 100644
--- a/include/osmocom/gsm/protocol/gsm_08_08.h
+++ b/include/osmocom/gsm/protocol/gsm_08_08.h
@@ -452,3 +452,17 @@
uint8_t key[ENCRY_INFO_KEY_MAXLEN];
unsigned int key_len;
 };
+
+/* 3GPP TS 48.008 3.2.2.10 Cell Identifier List */
+struct gsm0808_cell_id_list {
+   uint8_t id_discr;
+   struct llist_head id_list;
+};
+
+/* 3GPP TS 48.008 3.2.2.10 Cell Identifier List
+ * (Coding of i-th Cell Identification for Cell
+ * identification discriminator = 0101) */
+struct gsm0808_cell_id_lac {
+   struct llist_head list;
+   uint16_t lac;
+};
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index 7dfe2e9..564f171 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -426,3 +426,87 @@
 
return ei;
 }
+
+/* Encode Cell Identifier List element */
+struct msgb *gsm0808_enc_cell_id_list(struct gsm0808_cell_id_list *cil)
+{
+   struct msgb *msg;
+   struct gsm0808_cell_id_lac *lac;
+
+   OSMO_ASSERT(cil);
+
+   /* FIXME: Implement support for all identifier list elements */
+   OSMO_ASSERT(cil->id_discr == CELL_IDENT_LAC
+   || cil->id_discr == CELL_IDENT_BSS)
+
+   msg = msgb_alloc(ELEMENT_MSGB_MAXLEN, "Cell-ID list Element");
+   if (!msg)
+   return NULL;
+
+   msgb_put_u8(msg, cil->id_discr & 0x0f);
+
+   switch (cil->id_discr) {
+   case CELL_IDENT_LAC:
+   llist_for_each_entry(lac, >id_list, list) {
+   msgb_put_u16(msg, lac->lac);
+   }
+   break;
+
+   case CELL_IDENT_BSS:
+   /* Does not have any list items */
+   break;
+
+   default:
+   /* Unspported encoding */
+   OSMO_ASSERT(false);
+   }
+
+   return msg;
+}
+
+/* Decode Cell Identifier List element */
+struct gsm0808_cell_id_list *gsm0808_dec_cell_id_list(const void *ctx,
+ struct msgb *msg)
+{
+   uint8_t id_discr;
+   struct gsm0808_cell_id_list *cil;
+   struct gsm0808_cell_id_lac *lac;
+
+   if (!msg)
+   return NULL;
+
+   id_discr = msgb_pull_u8(msg) & 0x0f;
+
+   /* FIXME: Implement support for all identifier list elements */
+   if (id_discr != CELL_IDENT_LAC && id_discr != CELL_IDENT_BSS)
+   return NULL;
+
+   cil = talloc_zero(ctx, struct gsm0808_cell_id_list);
+   if (!cil)
+   return NULL;
+   INIT_LLIST_HEAD(>id_list);
+
+   cil->id_discr = id_discr;
+
+   switch (id_discr) {
+   case CELL_IDENT_LAC:
+   while (msg->len >= 2) {
+   lac = talloc_zero(cil, struct gsm0808_cell_id_lac);
+   if (!lac) {
+   talloc_free(cil);
+   return NULL;
+   }
+   lac->lac = msgb_pull_u16(msg);
+   llist_add(>list, >id_list);
+   }
+
+   case CELL_IDENT_BSS:
+   /* Does not have any list items */
+   break;
+   default:
+   /* Unspported encoding */
+   OSMO_ASSERT(false);
+   }
+
+   return cil;
+}
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index e64fdd9..ac8d467 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -150,6 +150,8 @@
 gsm0808_dec_channel_type;