aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Kluchnikov <kluchnikovi@gmail.com>2012-04-30 17:51:23 +0400
committerIvan Kluchnikov <kluchnikovi@gmail.com>2012-04-30 17:51:23 +0400
commit962f97b21c7f992a4ef83b4d39fc0b42c1b5487e (patch)
treeefc5830c2601d33b21830c1fcc3da5fb7bd0976a
parent5310d4542c1e97b4ebfaf22aca962eedee3a2833 (diff)
Additional functions for Osmocom bit vector abstraction.
-rw-r--r--Makefile.am6
-rw-r--r--bitvector.cpp118
-rw-r--r--bitvector.h45
3 files changed, 167 insertions, 2 deletions
diff --git a/Makefile.am b/Makefile.am
index 6bba1f20..c2123be1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -31,7 +31,8 @@ libgprs_la_SOURCES = \
gprs_bssgp_pcu.cpp \
gprs_rlcmac.cpp \
pcu_l1_if.cpp \
- gsm_timer.cpp
+ gsm_timer.cpp \
+ bitvector.cpp
noinst_PROGRAMS = \
RLCMACTest \
@@ -43,7 +44,8 @@ noinst_HEADERS = \
gprs_bssgp_pcu.h \
gprs_rlcmac.h \
pcu_l1_if.h \
- gsm_timer.h
+ gsm_timer.h \
+ bitvector.h
RLCMACTest_SOURCES = RLCMACTest.cpp
RLCMACTest_LDADD = \
diff --git a/bitvector.cpp b/bitvector.cpp
new file mode 100644
index 00000000..36ef7988
--- /dev/null
+++ b/bitvector.cpp
@@ -0,0 +1,118 @@
+/* bitvector.cpp
+ *
+ * Copyright (C) 2012 Ivan Klyuchnikov
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*! \addtogroup bitvector
+ * @{
+ */
+
+/*! \file bitvector.cpp
+ * \brief Additional functions for Osmocom bit vector abstraction.
+ */
+
+#include <bitvector.h>
+extern "C" {
+#include <osmocom/core/talloc.h>
+}
+
+void *bv_tall_ctx;
+
+struct bitvec *bitvec_alloc(unsigned size)
+{
+ struct bitvec *bv = talloc_zero(bv_tall_ctx, struct bitvec);
+ bv->data_len = size;
+ bv->cur_bit = 0;
+ bv->data = talloc_zero_array(bv_tall_ctx, uint8_t, size);
+ return bv;
+}
+
+void bitvec_free(struct bitvec *bv)
+{
+ talloc_free(bv->data);
+ talloc_free(bv);
+}
+
+int bitvec_pack(struct bitvec *bv, uint8_t *buffer)
+{
+ int i = 0;
+ for (i = 0; i < bv->data_len; i++)
+ {
+ buffer[i] = bv->data[i];
+ }
+ return i;
+}
+
+int bitvec_unpack(struct bitvec *bv, uint8_t *buffer)
+{
+ int i = 0;
+ for (i = 0; i < bv->data_len; i++)
+ {
+ bv->data[i] = buffer[i];
+ }
+ return i;
+}
+
+
+int bitvec_unhex(struct bitvec *bv, const char* src)
+{
+ unsigned val;
+ unsigned write_index = 0;
+ unsigned digits = bv->data_len*2;
+ for (unsigned i=0; i<digits; i++) {
+ if (sscanf(src+i, "%1x", &val) < 1) {
+ return 1;
+ }
+ bitvec_write_field(bv, write_index,val, 4);
+ }
+ return 0;
+}
+
+uint64_t bitvec_read_field(struct bitvec *bv, unsigned& read_index, unsigned len)
+{
+ int i;
+ uint64_t ui = 0;
+ bv->cur_bit = read_index;
+
+ for (i = 0; i < len; i++) {
+ int bit = bitvec_get_bit_pos((const struct bitvec *)bv, bv->cur_bit);
+ if (bit < 0)
+ return bit;
+ if (bit)
+ ui |= ((uint64_t)1 << (len - i - 1));
+ bv->cur_bit++;
+ }
+ read_index += len;
+ return ui;
+}
+
+
+int bitvec_write_field(struct bitvec *bv, unsigned& write_index, uint64_t val, unsigned len)
+{
+ int i, rc;
+ bv->cur_bit = write_index;
+ for (i = 0; i < len; i++) {
+ int bit = 0;
+ if (val & ((uint64_t)1 << (len - i - 1)))
+ bit = 1;
+ rc = bitvec_set_bit(bv, (bit_value)bit);
+ if (rc)
+ return rc;
+ }
+ write_index += len;
+ return 0;
+}
diff --git a/bitvector.h b/bitvector.h
new file mode 100644
index 00000000..7409d551
--- /dev/null
+++ b/bitvector.h
@@ -0,0 +1,45 @@
+/* bitvector.h
+ *
+ * Copyright (C) 2012 Ivan Klyuchnikov
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef BITVECTOR_H
+#define BITVECTOR_H
+
+/*! \defgroup bitvector Bit vectors
+ * @{
+ */
+
+/*! \file bitvector.h
+ * \brief Additional functions for Osmocom bit vector abstraction.
+ */
+
+extern "C" {
+#include <osmocom/core/bitvec.h>
+}
+
+struct bitvec *bitvec_alloc(unsigned size);
+void bitvec_free(struct bitvec *bv);
+int bitvec_unhex(struct bitvec *bv, const char* src);
+int bitvec_pack(struct bitvec *bv, uint8_t *buffer);
+int bitvec_unpack(struct bitvec *bv, uint8_t *buffer);
+uint64_t bitvec_read_field(struct bitvec *bv, unsigned& read_index, unsigned len);
+int bitvec_write_field(struct bitvec *bv, unsigned& write_index, uint64_t val, unsigned len);
+
+/*! }@ */
+
+#endif // BITVECTOR_H