Add function for appending range1024 to bitvec
diff mbox

Message ID 1461074775-23946-1-git-send-email-msuraev@sysmocom.de
State Not Applicable
Headers show

Commit Message

Max April 19, 2016, 2:06 p.m. UTC
From: Max <msuraev@sysmocom.de>

Add convenience function to append range1024 encoded data (see 3GPP TS
44.018 Annex J) to a given bitvec.
---
 include/osmocom/core/bitvec.h |  3 +++
 src/bitvec.c                  | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

Comments

Holger Freyther April 22, 2016, 1:22 p.m. UTC | #1
> On 19 Apr 2016, at 16:06, msuraev@sysmocom.de wrote:
> 
> From: Max <msuraev@sysmocom.de>
> 
> Add convenience function to append range1024 encoded data (see 3GPP TS
> 44.018 Annex J) to a given bitvec.
> ---
> include/osmocom/core/bitvec.h |  3 +++
> src/bitvec.c                  | 36 ++++++++++++++++++++++++++++++++++++
> 2 files changed, 39 insertions(+)
> 
> diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h
> index c3c1153..704573c 100644
> --- a/include/osmocom/core/bitvec.h
> +++ b/include/osmocom/core/bitvec.h
> @@ -44,6 +44,8 @@
> #include <talloc.h>
> #include <stdbool.h>
> 
> +#include <osmocom/gsm/protocol/gsm_04_08.h>

the code itself looks okay (adding lo+hi seems to result in 8 bits. But I think the place is wrong. The bit vector should not know about GSM (different layers, different library). Sure you are only using a data structure but let's keep our boundaries right.

The closest we have as an example is msgb + tlv. One in GSM and one in Core. Can you add a bitvec_gsm.h or similar.

holger

Patch
diff mbox

diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h
index c3c1153..704573c 100644
--- a/include/osmocom/core/bitvec.h
+++ b/include/osmocom/core/bitvec.h
@@ -44,6 +44,8 @@ 
 #include <talloc.h>
 #include <stdbool.h>
 
+#include <osmocom/gsm/protocol/gsm_04_08.h>
+
 /*! \brief A single GSM bit
  *
  * In GSM mac blocks, every bit can be 0 or 1, or L or H.  L/H are
@@ -75,6 +77,7 @@  int bitvec_set_uint(struct bitvec *bv, uint32_t in, unsigned int count);
 int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits);
 int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val);
 int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit);
+void bitvec_add_range1024(struct bitvec *bv, const struct gsm48_range_1024 *r);
 int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count);
 int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count);
 struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *bvctx);
diff --git a/src/bitvec.c b/src/bitvec.c
index a92fd71..7334341 100644
--- a/src/bitvec.c
+++ b/src/bitvec.c
@@ -38,6 +38,7 @@ 
 
 #include <osmocom/core/bits.h>
 #include <osmocom/core/bitvec.h>
+#include <osmocom/gsm/protocol/gsm_04_08.h>
 
 #define BITNUM_FROM_COMP(byte, bit)	((byte*8)+bit)
 
@@ -523,6 +524,41 @@  static inline unsigned leading_bits(uint8_t x, bool b)
 	}
 	return 7;
 }
+
+/*! \brief append range1024 encoded data to bit vector */
+void bitvec_add_range1024(struct bitvec *bv, const struct gsm48_range_1024 *r)
+{
+	bitvec_set_uint(bv, r->w1_hi, 2);
+	bitvec_set_uint(bv, r->w1_lo, 8);
+	bitvec_set_uint(bv, r->w2_hi, 8);
+	bitvec_set_uint(bv, r->w2_lo, 1);
+	bitvec_set_uint(bv, r->w3_hi, 7);
+	bitvec_set_uint(bv, r->w3_lo, 2);
+	bitvec_set_uint(bv, r->w4_hi, 6);
+	bitvec_set_uint(bv, r->w4_lo, 2);
+	bitvec_set_uint(bv, r->w5_hi, 6);
+	bitvec_set_uint(bv, r->w5_lo, 2);
+	bitvec_set_uint(bv, r->w6_hi, 6);
+	bitvec_set_uint(bv, r->w6_lo, 2);
+	bitvec_set_uint(bv, r->w7_hi, 6);
+	bitvec_set_uint(bv, r->w7_lo, 2);
+	bitvec_set_uint(bv, r->w8_hi, 6);
+	bitvec_set_uint(bv, r->w8_lo, 1);
+	bitvec_set_uint(bv, r->w9, 7);
+	bitvec_set_uint(bv, r->w10, 7);
+	bitvec_set_uint(bv, r->w11_hi, 1);
+	bitvec_set_uint(bv, r->w11_lo, 6);
+	bitvec_set_uint(bv, r->w12_hi, 2);
+	bitvec_set_uint(bv, r->w12_lo, 5);
+	bitvec_set_uint(bv, r->w13_hi, 3);
+	bitvec_set_uint(bv, r->w13_lo, 4);
+	bitvec_set_uint(bv, r->w14_hi, 4);
+	bitvec_set_uint(bv, r->w14_lo, 3);
+	bitvec_set_uint(bv, r->w15_hi, 5);
+	bitvec_set_uint(bv, r->w15_lo, 2);
+	bitvec_set_uint(bv, r->w16, 6);
+}
+
 /*! \brief force bit vector to all 0 and current bit to the beginnig of the vector */
 void bitvec_zero(struct bitvec *bv)
 {