[PATCH 1/5] Add helper routines.

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

Max Max.Suraev at fairwaves.ru
Sun Apr 7 12:53:52 UTC 2013


---
 include/osmocom/core/bits.h     |   14 +++++++++++++-
 include/osmocom/gsm/gsm_utils.h |   16 ++++++++++++++++
 src/bits.c                      |   33 +++++++++++++++++++++++++++++++++
 tests/bits/bitrev_test.c        |   18 ++++++++++++++++--
 tests/bits/bitrev_test.ok       |    1 +
 5 files changed, 79 insertions(+), 3 deletions(-)

diff --git a/include/osmocom/core/bits.h b/include/osmocom/core/bits.h
index 4c68532..bf79445 100644
--- a/include/osmocom/core/bits.h
+++ b/include/osmocom/core/bits.h
@@ -2,7 +2,7 @@
 #define _OSMO_BITS_H
 
 #include <stdint.h>
-
+#include <stddef.h>
 /*! \defgroup bits soft, unpacked and packed bits
  *  @{
  */
@@ -73,6 +73,18 @@ uint32_t osmo_revbytebits_8(uint8_t x);
 /* \brief reverse the bits of each byte in a given buffer */
 void osmo_revbytebits_buf(uint8_t *buf, int len);
 
+/* \brief reverse the order of the bytes in a given buffer */
+void osmo_revbytes_buf(uint8_t *buf, size_t len);
+
+/* \brief left circular shift */
+uint16_t rol16(uint16_t in, unsigned shift);
+
+/* return 2 bytes from a given array glued into single uint16_t */
+uint16_t osmo_get2bytes(const uint8_t *a);
+
+/* convert uint64_t into array of 8 bytes in out */
+void osmo_64pack2pbit(uint64_t in, pbit_t *out);
+
 /*! @} */
 
 #endif /* _OSMO_BITS_H */
diff --git a/include/osmocom/gsm/gsm_utils.h b/include/osmocom/gsm/gsm_utils.h
index cdbac87..f215c7b 100644
--- a/include/osmocom/gsm/gsm_utils.h
+++ b/include/osmocom/gsm/gsm_utils.h
@@ -85,6 +85,22 @@ static inline int ms_cm2_a5n_support(uint8_t *cm2, int n) {
 	}
 }
 
+/* According to GSM 04.08 Chapter 10.5.1.7 */
+static inline int ms_cm3_a5n_support(uint8_t *cm3, int n) {
+	switch (n) {
+		case 4: return (cm3[0] & (1<<0)) ? 1 : 0;
+		case 5: return (cm3[0] & (1<<1)) ? 1 : 0;
+		case 6: return (cm3[0] & (1<<2)) ? 1 : 0;
+	        case 7: return (cm3[0] & (1<<3)) ? 1 : 0;
+		default:
+			return 0;
+	}
+}
+
+static inline int ms_a5n_support(uint8_t *cm, int n) {
+    return ((n < 4) ? ms_cm2_a5n_support(cm, n) : ms_cm3_a5n_support(cm, n));
+}
+
 /* According to GSM 04.08 Chapter 10.5.2.29 */
 static inline int rach_max_trans_val2raw(int val) { return (val >> 1) & 3; }
 static inline int rach_max_trans_raw2val(int raw) {
diff --git a/src/bits.c b/src/bits.c
index 6be6d7a..41c46df 100644
--- a/src/bits.c
+++ b/src/bits.c
@@ -211,4 +211,37 @@ void osmo_revbytebits_buf(uint8_t *buf, int len)
 	}
 }
 
+void osmo_revbytes_buf(uint8_t *buf, size_t len)
+{
+    uint8_t *end = buf + len - 1, tmp;
+
+    while (buf < end) {
+        tmp = *buf;
+        *buf++ = *end;
+        *end-- = tmp;
+    }
+}
+
+/* left circular shift */
+uint16_t rol16(uint16_t in, unsigned shift)
+{
+    return (in << shift) | (in >> (16 - shift));
+}
+
+/* return 2 bytes from a given array glued into single uint16_t */
+uint16_t osmo_get2bytes(const uint8_t *a)
+{ /* UNSAFE! NO out-of-bounds access check. Do NOT use unless you know what you are doing! */
+    return (uint16_t)((((uint16_t)a[0]) << 8) + (uint16_t)a[1]);
+}
+
+/* convert uint64_t into array of 8 bytes in out */
+void osmo_64pack2pbit(uint64_t in, pbit_t *out)
+{
+    int i;
+    for (i = 7; i >=0; i--) {
+	out[i] = in & 0xFF;
+	in >>= 8;
+    }
+}
+
 /*! @} */
diff --git a/tests/bits/bitrev_test.c b/tests/bits/bitrev_test.c
index 5eca990..938d261 100644
--- a/tests/bits/bitrev_test.c
+++ b/tests/bits/bitrev_test.c
@@ -1,4 +1,4 @@
-
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
@@ -12,7 +12,7 @@ static const uint8_t exp_out[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x0
 
 int main(int argc, char **argv)
 {
-	uint8_t out[ARRAY_SIZE(input)];
+        uint8_t out[ARRAY_SIZE(input)], test[8];
 	unsigned int offs;
 
 	for (offs = 0; offs < sizeof(out); offs++) {
@@ -32,5 +32,19 @@ int main(int argc, char **argv)
 		printf("\n");
 	}
 
+	printf("checking bit packer... ");
+	uint64_t _test = ((uint64_t)rand() << 32) + rand();
+	osmo_64pack2pbit(_test, test);
+	char s[18], *p;
+	snprintf(s, 17, "%.16" PRIx64, _test);
+	p = osmo_hexdump_nospc(test, 8);
+	if (0 != memcmp(s, p, 8)) {
+	    printf ("%s\t", s);
+	    printf ("%s\t", p);
+	    printf("FAILED!\n");
+	    return 2;
+	}
+	printf("OK\n");
+
 	return 0;
 }
diff --git a/tests/bits/bitrev_test.ok b/tests/bits/bitrev_test.ok
index 47f402f..8a5fc6e 100644
--- a/tests/bits/bitrev_test.ok
+++ b/tests/bits/bitrev_test.ok
@@ -22,3 +22,4 @@ REVERSED: 02 01
 INORDER:  80 
 REVERSED: 01 
 
+checking bit packer... OK
-- 
1.7.10.4


--------------060001040407040305040404
Content-Type: text/x-patch;
 name="0002-Add-KASUMI-implementation.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0002-Add-KASUMI-implementation.patch"



More information about the baseband-devel mailing list