Merge commit '7459d6cfd1765f8431b3d9a262eff332f9af807c' into sylvain/pending
authorSylvain Munaut <tnt@246tNt.com>
Tue, 27 Jul 2010 18:50:57 +0000 (20:50 +0200)
committerSylvain Munaut <tnt@246tNt.com>
Tue, 27 Jul 2010 18:50:57 +0000 (20:50 +0200)
14 files changed:
1  2 
src/shared/libosmocore/configure.in
src/shared/libosmocore/include/osmocore/Makefile.am
src/shared/libosmocore/include/osmocore/gsm0808.h
src/shared/libosmocore/include/osmocore/msgb.h
src/shared/libosmocore/include/osmocore/panic.h
src/shared/libosmocore/include/osmocore/protocol/Makefile.am
src/shared/libosmocore/include/osmocore/protocol/gsm_04_12.h
src/shared/libosmocore/include/osmocore/rsl.h
src/shared/libosmocore/src/Makefile.am
src/shared/libosmocore/src/gsm0808.c
src/shared/libosmocore/src/gsm_utils.c
src/shared/libosmocore/src/panic.c
src/shared/libosmocore/src/rsl.c
src/shared/libosmocore/tests/sms/sms_test.c

index e3e178c,0000000..c3e0061
mode 100644,000000..100644
--- /dev/null
@@@ -1,72 -1,0 +1,95 @@@
-       [  --disable-talloc Disable building talloc memory allocator ],
 +AC_INIT([libosmocore],
 +      m4_esyscmd([./git-version-gen .tarball-version]),
 +      [openbsc-devel@lists.openbsc.org])
 +
 +AM_INIT_AUTOMAKE([dist-bzip2])
 +
 +dnl kernel style compile messages
 +m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
 +
 +dnl checks for programs
 +AC_PROG_MAKE_SET
 +AC_PROG_CC
 +AC_PROG_INSTALL
 +LT_INIT
 +AC_PROG_LIBTOOL
 +
 +AC_CONFIG_MACRO_DIR([m4])
 +
 +dnl checks for header files
 +AC_HEADER_STDC
 +AC_CHECK_HEADERS(execinfo.h sys/select.h)
 +
 +# The following test is taken from WebKit's webkit.m4
 +saved_CFLAGS="$CFLAGS"
 +CFLAGS="$CFLAGS -fvisibility=hidden "
 +AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden])
 +AC_COMPILE_IFELSE([char foo;],
 +      [ AC_MSG_RESULT([yes])
 +        SYMBOL_VISIBILITY="-fvisibility=hidden"],
 +        AC_MSG_RESULT([no]))
 +CFLAGS="$saved_CFLAGS"
 +AC_SUBST(SYMBOL_VISIBILITY)
 +
 +dnl Generate the output
 +AM_CONFIG_HEADER(config.h)
 +
 +AC_ARG_ENABLE(talloc,
-       [  --disable-plugin Disable support for dlopen plugins ],
++      [AS_HELP_STRING(
++              [--disable-talloc],
++              [Disable building talloc memory allocator]
++      )],
 +      [enable_talloc=0], [enable_talloc=1])
 +AM_CONDITIONAL(ENABLE_TALLOC, test "x$enable_talloc" = "x1")
 +
 +AC_ARG_ENABLE(plugin,
-       [  --disable-tests Disable building test programs ],
++      [AS_HELP_STRING(
++              [--disable-plugin],
++              [Disable support for dlopen plugins],
++      )],
 +      [enable_plugin=0], [enable_plugin=1])
 +AM_CONDITIONAL(ENABLE_PLUGIN, test "x$enable_plugin" = "x1")
 +
 +AC_ARG_ENABLE(tests,
-       [  --disable-vty Disable building VTY telnet interface ],
++      [AS_HELP_STRING(
++              [--disable-tests],
++              [Disable building test programs]
++      )],
 +      [enable_tests=0], [enable_tests=1])
 +AM_CONDITIONAL(ENABLE_TESTS, test "x$enable_tests" = "x1")
 +
 +AC_ARG_ENABLE(vty,
++      [AS_HELP_STRING(
++              [--disable-vty],
++              [Disable building VTY telnet interface]
++      )],
 +      [enable_vty=0], [enable_vty=1])
 +AM_CONDITIONAL(ENABLE_VTY, test "x$enable_vty" = "x1")
 +
++AC_ARG_ENABLE(panic_infloop,
++      [AS_HELP_STRING(
++              [--enable-panic-infloop],
++              [Trigger infinite loop on panic rather than fprintf/abort]
++      )],
++      [panic_infloop=1], [panic_infloop=0])
++if test "x$panic_infloop" = "x1"
++then
++      AC_DEFINE([PANIC_INFLOOP],[1],[Use infinite loop on panic rather than fprintf/abort])
++fi
++
 +
 +AC_OUTPUT(
 +      libosmocore.pc
 +      libosmovty.pc
 +      include/osmocom/Makefile
 +      include/osmocom/vty/Makefile
 +      include/osmocom/crypt/Makefile
 +      include/osmocore/Makefile
 +      include/osmocore/protocol/Makefile
 +      include/Makefile
 +      src/Makefile
 +      src/vty/Makefile
 +      tests/Makefile
 +      tests/timer/Makefile
 +      tests/sms/Makefile
 +      Makefile)
index 8334f14,0000000..ecdc65c
mode 100644,000000..100644
--- /dev/null
@@@ -1,13 -1,0 +1,13 @@@
-                  plugin.h crc16.h
 +osmocore_HEADERS = signal.h linuxlist.h timer.h select.h msgb.h \
 +                 tlv.h bitvec.h comp128.h statistics.h gsm_utils.h utils.h \
 +                 gsmtap.h write_queue.h rsl.h gsm48.h rxlev_stat.h mncc.h \
 +                 gsm48_ie.h logging.h gsm0808.h rate_ctr.h gsmtap_util.h \
++                 plugin.h crc16.h panic.h
 +
 +if ENABLE_TALLOC
 +osmocore_HEADERS += talloc.h
 +endif
 +
 +osmocoredir = $(includedir)/osmocore
 +
 +SUBDIRS = protocol
index 9166e54,0000000..2d609c9
mode 100644,000000..100644
--- /dev/null
@@@ -1,43 -1,0 +1,43 @@@
- struct msgb *gsm0808_create_layer3(struct msgb *msg, uint16_t netcode, uint16_t countrycode, int lac, int ci);
 +/* (C) 2009,2010 by Holger Hans Peter Freyther <zecke@selfish.org>
 + * (C) 2009,2010 by On-Waves
 + * All Rights Reserved
 + *
 + * 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.,
 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 + *
 + */
 +#ifndef OSMOCORE_GSM0808_H
 +#define OSMOCORE_GSM0808_H
 +
 +#include "tlv.h"
 +
 +struct msgb;
 +
- struct msgb *gsm0808_create_assignment_completed(struct gsm_lchan *lchan, uint8_t rr_cause,
++struct msgb *gsm0808_create_layer3(struct msgb *msg, uint16_t netcode, uint16_t countrycode, int lac, uint16_t ci);
 +struct msgb *gsm0808_create_reset(void);
 +struct msgb *gsm0808_create_clear_complete(void);
 +struct msgb *gsm0808_create_cipher_complete(struct msgb *layer3, uint8_t alg_id);
 +struct msgb *gsm0808_create_cipher_reject(uint8_t cause);
 +struct msgb *gsm0808_create_classmark_update(const uint8_t *classmark, uint8_t length);
 +struct msgb *gsm0808_create_sapi_reject(uint8_t link_id);
++struct msgb *gsm0808_create_assignment_completed(uint8_t rr_cause,
 +                                               uint8_t chosen_channel, uint8_t encr_alg_id,
 +                                               uint8_t speech_mode);
 +struct msgb *gsm0808_create_assignment_failure(uint8_t cause, uint8_t *rr_cause);
 +
 +void gsm0808_prepend_dtap_header(struct msgb *msg, uint8_t link_id);
 +
 +const struct tlv_definition *gsm0808_att_tlvdef();
 +
 +#endif
index 2841dc5,0000000..354c5d8
mode 100644,000000..100644
--- /dev/null
@@@ -1,165 -1,0 +1,190 @@@
- static inline int msgb_tailroom(const struct msgb *msgb)
- {
-       return (msgb->head + msgb->data_len) - msgb->tail;
- }
 +#ifndef _MSGB_H
 +#define _MSGB_H
 +
 +/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
 + * All Rights Reserved
 + *
 + * 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.,
 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 + *
 + */
 +
 +#include <stdint.h>
 +#include "linuxlist.h"
 +
++#define MSGB_DEBUG
++
 +struct msgb {
 +      struct llist_head list;
 +
 +      /* Part of which TRX logical channel we were received / transmitted */
 +      /* FIXME: move them into the control buffer */
 +      struct gsm_bts_trx *trx;
 +      struct gsm_lchan *lchan;
 +
 +      /* the Layer1 header (if any) */
 +      unsigned char *l1h;
 +      /* the A-bis layer 2 header: OML, RSL(RLL), NS */
 +      unsigned char *l2h;
 +      /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
 +      unsigned char *l3h;
 +      /* the layer 4 header */
 +      unsigned char *l4h;
 +
 +      /* the 'control buffer', large enough to contain 5 pointers */
 +      unsigned long cb[5];
 +
 +      uint16_t data_len;
 +      uint16_t len;
 +
 +      unsigned char *head;
 +      unsigned char *tail;
 +      unsigned char *data;
 +      unsigned char _data[0];
 +};
 +
 +extern struct msgb *msgb_alloc(uint16_t size, const char *name);
 +extern void msgb_free(struct msgb *m);
 +extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
 +extern struct msgb *msgb_dequeue(struct llist_head *queue);
 +extern void msgb_reset(struct msgb *m);
 +
++#ifdef MSGB_DEBUG
++#include <osmocore/panic.h>
++static inline void msgb_abort(struct msgb *msg, const char *text)
++{
++      osmo_panic("%s", text);
++}
++#endif
++
 +#define msgb_l1(m)    ((void *)(m->l1h))
 +#define msgb_l2(m)    ((void *)(m->l2h))
 +#define msgb_l3(m)    ((void *)(m->l3h))
 +#define msgb_sms(m)   ((void *)(m->l4h))
 +
 +static inline unsigned int msgb_l1len(const struct msgb *msgb)
 +{
 +      return msgb->tail - (uint8_t *)msgb_l1(msgb);
 +}
 +
 +static inline unsigned int msgb_l2len(const struct msgb *msgb)
 +{
 +      return msgb->tail - (uint8_t *)msgb_l2(msgb);
 +}
 +
 +static inline unsigned int msgb_l3len(const struct msgb *msgb)
 +{
 +      return msgb->tail - (uint8_t *)msgb_l3(msgb);
 +}
 +
 +static inline unsigned int msgb_headlen(const struct msgb *msgb)
 +{
 +      return msgb->len - msgb->data_len;
 +}
++
++static inline int msgb_tailroom(const struct msgb *msgb)
++{
++      return (msgb->head + msgb->data_len) - msgb->tail;
++}
++
++static inline int msgb_headroom(const struct msgb *msgb)
++{
++      return (msgb->data - msgb->head);
++}
++
 +static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
 +{
 +      unsigned char *tmp = msgb->tail;
++#ifdef MSGB_DEBUG
++      if (msgb_tailroom(msgb) < len)
++              msgb_abort(msgb, "Not enough tailroom\n");
++#endif
 +      msgb->tail += len;
 +      msgb->len += len;
 +      return tmp;
 +}
 +static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
 +{
 +      uint8_t *space = msgb_put(msgb, 1);
 +      space[0] = word & 0xFF;
 +}
 +static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
 +{
 +      uint8_t *space = msgb_put(msgb, 2);
 +      space[0] = word >> 8 & 0xFF;
 +      space[1] = word & 0xFF;
 +}
 +static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
 +{
 +      uint8_t *space = msgb_put(msgb, 4);
 +      space[0] = word >> 24 & 0xFF;
 +      space[1] = word >> 16 & 0xFF;
 +      space[2] = word >> 8 & 0xFF;
 +      space[3] = word & 0xFF;
 +}
 +static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
 +{
 +      unsigned char *tmp = msgb->data;
 +      msgb->data += len;
 +      msgb->len -= len;
 +      return tmp;
 +}
 +static inline uint8_t msgb_get_u8(struct msgb *msgb)
 +{
 +      uint8_t *space = msgb_get(msgb, 1);
 +      return space[0];
 +}
 +static inline uint16_t msgb_get_u16(struct msgb *msgb)
 +{
 +      uint8_t *space = msgb_get(msgb, 2);
 +      return space[0] << 8 | space[1];
 +}
 +static inline uint32_t msgb_get_u32(struct msgb *msgb)
 +{
 +      uint8_t *space = msgb_get(msgb, 4);
 +      return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
 +}
 +static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
 +{
++#ifdef MSGB_DEBUG
++      if (msgb_headroom(msgb) < len)
++              msgb_abort(msgb, "Not enough headroom\n");
++#endif
 +      msgb->data -= len;
 +      msgb->len += len;
 +      return msgb->data;
 +}
 +static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
 +{
 +      msgb->len -= len;
 +      return msgb->data += len;
 +}
 +
 +/* increase the headroom of an empty msgb, reducing the tailroom */
 +static inline void msgb_reserve(struct msgb *msg, int len)
 +{
 +      msg->data += len;
 +      msg->tail += len;
 +}
 +
 +static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
 +                                              const char *name)
 +{
 +      struct msgb *msg = msgb_alloc(size, name);
 +      if (msg)
 +              msgb_reserve(msg, headroom);
 +      return msg;
 +}
 +
 +#endif /* _MSGB_H */
index 0000000,0000000..cee9535
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,11 @@@
++#ifndef OSMOCORE_PANIC_H
++#define OSMOCORE_PANIC_H
++
++#include <stdarg.h>
++
++typedef void (*osmo_panic_handler_t)(const char *fmt, va_list args);
++
++void osmo_panic(const char *fmt, ...);
++void osmo_set_panic_handler(osmo_panic_handler_t *h);
++
++#endif
index 557950e,0000000..5df9fe7
mode 100644,000000..100644
--- /dev/null
@@@ -1,3 -1,0 +1,4 @@@
- osmocore_proto_HEADERS = gsm_04_08.h gsm_04_11.h gsm_04_80.h gsm_08_58.h gsm_12_21.h gsm_08_08.h
++osmocore_proto_HEADERS = gsm_04_08.h gsm_04_11.h gsm_04_80.h gsm_08_58.h \
++                       gsm_12_21.h gsm_08_08.h gsm_04_12.h
 +
 +osmocore_protodir = $(includedir)/osmocore/protocol
index 0000000,0000000..bd9e088
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,31 @@@
++#ifndef PROTO_GSM_04_12_H
++#define PROTO_GSM_04_12_H
++
++#include <stdint.h>
++
++/* GSM TS 04.12 definitions for Short Message Service Cell Broadcast */
++
++#define GSM412_SEQ_FST_BLOCK          0x0
++#define GSM412_SEQ_SND_BLOCK          0x1
++#define GSM412_SEQ_TRD_BLOCK          0x2
++#define GSM412_SEQ_FTH_BLOCK          0x3
++#define GSM412_SEQ_FST_SCHED_BLOCK    0x8
++#define GSM413_SEQ_NULL_MSG           0xf
++
++struct gsm412_block_type {
++      uint8_t seq_nr : 4,
++              lb : 1,
++              lpd : 2,
++              spare : 1;
++} __attribute__((packed));
++
++struct gsm413_sched_msg {
++      uint8_t beg_slot_nr : 6,
++              type : 2;
++      uint8_t end_slot_nr : 6,
++              spare1 : 1, spare2: 1;
++      uint8_t cbsms_msg_map[6];
++      uint8_t data[0];
++} __attribute__((packed));
++
++#endif
index cd84057,0000000..54d6703
mode 100644,000000..100644
--- /dev/null
@@@ -1,38 -1,0 +1,41 @@@
 +#ifndef _OSMOCORE_RSL_H
 +#define _OSMOCORE_RSL_H
 +
 +#include <stdint.h>
 +#include <osmocore/utils.h>
 +#include <osmocore/protocol/gsm_08_58.h>
 +
 +void rsl_init_rll_hdr(struct abis_rsl_rll_hdr *dh, uint8_t msg_type);
 +
 +void rsl_init_cchan_hdr(struct abis_rsl_cchan_hdr *ch, uint8_t msg_type);
 +
 +extern const struct tlv_definition rsl_att_tlvdef;
 +#define rsl_tlv_parse(dec, buf, len)     \
 +                      tlv_parse(dec, &rsl_att_tlvdef, buf, len, 0, 0)
 +
 +/* encode channel number as per Section 9.3.1 */
 +uint8_t rsl_enc_chan_nr(uint8_t type, uint8_t subch, uint8_t timeslot);
 +/* decode channel number as per Section 9.3.1 */
 +int rsl_dec_chan_nr(uint8_t chan_nr, uint8_t *type, uint8_t *subch, uint8_t *timeslot);
++/* Turns channel number into a string */
++const char *rsl_chan_nr_str(uint8_t chan_nr);
++
 +
 +const char *rsl_err_name(uint8_t err);
 +const char *rsl_rlm_cause_name(uint8_t err);
 +
 +/* Section 3.3.2.3 TS 05.02. I think this looks like a table */
 +int rsl_ccch_conf_to_bs_cc_chans(int ccch_conf);
 +
 +/* Push a RSL RLL header */
 +void rsl_rll_push_hdr(struct msgb *msg, uint8_t msg_type, uint8_t chan_nr,
 +                    uint8_t link_id, int transparent);
 +
 +/* Push a RSL RLL header with L3_INFO IE */
 +void rsl_rll_push_l3(struct msgb *msg, uint8_t msg_type, uint8_t chan_nr,
 +                   uint8_t link_id, int transparent);
 +
 +/* Allocate msgb and fill with simple RSL RLL header */
 +struct msgb *rsl_rll_simple(uint8_t msg_type, uint8_t chan_nr,
 +                          uint8_t link_id, int transparent);
 +#endif /* _OSMOCORE_RSL_H */
index a20fbc8,0000000..e197e17
mode 100644,000000..100644
--- /dev/null
@@@ -1,25 -1,0 +1,25 @@@
-                        gprs_cipher_core.c crc16.c
 +SUBDIRS=vty
 +
 +# This is _NOT_ the library release version, it's an API version.
 +# Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification
 +LIBVERSION=0:0:0
 +
 +INCLUDES = $(all_includes) -I$(top_srcdir)/include
 +AM_CFLAGS = -fPIC -Wall
 +
 +lib_LTLIBRARIES = libosmocore.la
 +
 +libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c rxlev_stat.c \
 +                       tlv_parser.c bitvec.c comp128.c gsm_utils.c statistics.c \
 +                       write_queue.c utils.c rsl.c gsm48.c gsm48_ie.c \
 +                       logging.c gsm0808.c rate_ctr.c gsmtap_util.c \
++                       gprs_cipher_core.c crc16.c panic.c
 +
 +if ENABLE_PLUGIN
 +libosmocore_la_SOURCES += plugin.c
 +libosmocore_la_LDFLAGS = -ldl
 +endif
 +
 +if ENABLE_TALLOC
 +libosmocore_la_SOURCES += talloc.c
 +endif
index 1dc035b,0000000..c8dc664
mode 100644,000000..100644
--- /dev/null
@@@ -1,306 -1,0 +1,311 @@@
- struct msgb *gsm0808_create_layer3(struct msgb *msg_l3, uint16_t nc, uint16_t cc, int lac, int _ci)
 +/* (C) 2009,2010 by Holger Hans Peter Freyther <zecke@selfish.org>
 + * (C) 2009,2010 by On-Waves
 + * All Rights Reserved
 + *
 + * 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.,
 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 + *
 + */
 +
 +#include <osmocore/gsm0808.h>
 +#include <osmocore/protocol/gsm_08_08.h>
 +#include <osmocore/gsm48.h>
 +
 +#include <arpa/inet.h>
 +
 +#define BSSMAP_MSG_SIZE 512
 +#define BSSMAP_MSG_HEADROOM 128
 +
-       uint16_t *ci;
++static void put_data_16(uint8_t *data, const uint16_t val)
++{
++      memcpy(data, &val, sizeof(val));
++}
++
++struct msgb *gsm0808_create_layer3(struct msgb *msg_l3, uint16_t nc, uint16_t cc, int lac, uint16_t _ci)
 +{
 +      uint8_t *data;
-       ci = (uint16_t *) msgb_put(msg, 2);
-       *ci = htons(_ci);
++      uint8_t *ci;
 +      struct msgb* msg;
 +      struct gsm48_loc_area_id *lai;
 +
 +      msg  = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM,
 +                                 "bssmap cmpl l3");
 +      if (!msg)
 +              return NULL;
 +
 +      /* create the bssmap header */
 +      msg->l3h = msgb_put(msg, 2);
 +      msg->l3h[0] = 0x0;
 +
 +      /* create layer 3 header */
 +      data = msgb_put(msg, 1);
 +      data[0] = BSS_MAP_MSG_COMPLETE_LAYER_3;
 +
 +      /* create the cell header */
 +      data = msgb_put(msg, 3);
 +      data[0] = GSM0808_IE_CELL_IDENTIFIER;
 +      data[1] = 1 + sizeof(*lai) + 2;
 +      data[2] = CELL_IDENT_WHOLE_GLOBAL;
 +
 +      lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
 +      gsm48_generate_lai(lai, cc, nc, lac);
 +
- struct msgb *gsm0808_create_assignment_completed(struct gsm_lchan *lchan, uint8_t rr_cause,
++      ci = msgb_put(msg, 2);
++      put_data_16(ci, htons(_ci));
 +
 +      /* copy the layer3 data */
 +      data = msgb_put(msg, msgb_l3len(msg_l3) + 2);
 +      data[0] = GSM0808_IE_LAYER_3_INFORMATION;
 +      data[1] = msgb_l3len(msg_l3);
 +      memcpy(&data[2], msg_l3->l3h, data[1]);
 +
 +      /* update the size */
 +      msg->l3h[1] = msgb_l3len(msg) - 2;
 +
 +      return msg;
 +}
 +
 +struct msgb *gsm0808_create_reset(void)
 +{
 +      struct msgb *msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM,
 +                                             "bssmap: reset");
 +      if (!msg)
 +              return NULL;
 +
 +      msg->l3h = msgb_put(msg, 6);
 +      msg->l3h[0] = BSSAP_MSG_BSS_MANAGEMENT;
 +      msg->l3h[1] = 0x04;
 +      msg->l3h[2] = 0x30;
 +      msg->l3h[3] = 0x04;
 +      msg->l3h[4] = 0x01;
 +      msg->l3h[5] = 0x20;
 +      return msg;
 +}
 +
 +struct msgb *gsm0808_create_clear_complete(void)
 +{
 +      struct msgb *msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM,
 +                                             "bssmap: clear complete");
 +      if (!msg)
 +              return NULL;
 +
 +      msg->l3h = msgb_put(msg, 3);
 +      msg->l3h[0] = BSSAP_MSG_BSS_MANAGEMENT;
 +      msg->l3h[1] = 1;
 +      msg->l3h[2] = BSS_MAP_MSG_CLEAR_COMPLETE;
 +
 +      return msg;
 +}
 +
 +struct msgb *gsm0808_create_cipher_complete(struct msgb *layer3, uint8_t alg_id)
 +{
 +      struct msgb *msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM,
 +                                             "cipher-complete");
 +      if (!msg)
 +              return NULL;
 +
 +        /* send response with BSS override for A5/1... cheating */
 +      msg->l3h = msgb_put(msg, 3);
 +      msg->l3h[0] = BSSAP_MSG_BSS_MANAGEMENT;
 +      msg->l3h[1] = 0xff;
 +      msg->l3h[2] = BSS_MAP_MSG_CIPHER_MODE_COMPLETE;
 +
 +      /* include layer3 in case we have at least two octets */
 +      if (layer3 && msgb_l3len(layer3) > 2) {
 +              msg->l4h = msgb_put(msg, msgb_l3len(layer3) + 2);
 +              msg->l4h[0] = GSM0808_IE_LAYER_3_MESSAGE_CONTENTS;
 +              msg->l4h[1] = msgb_l3len(layer3);
 +              memcpy(&msg->l4h[2], layer3->l3h, msgb_l3len(layer3));
 +      }
 +
 +      /* and the optional BSS message */
 +      msg->l4h = msgb_put(msg, 2);
 +      msg->l4h[0] = GSM0808_IE_CHOSEN_ENCR_ALG;
 +      msg->l4h[1] = alg_id;
 +
 +      /* update the size */
 +      msg->l3h[1] = msgb_l3len(msg) - 2;
 +      return msg;
 +}
 +
 +struct msgb *gsm0808_create_cipher_reject(uint8_t cause)
 +{
 +      struct msgb *msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM,
 +                                             "bssmap: clear complete");
 +      if (!msg)
 +              return NULL;
 +
 +      msg->l3h = msgb_put(msg, 3);
 +      msg->l3h[0] = BSSAP_MSG_BSS_MANAGEMENT;
 +      msg->l3h[1] = 2;
 +      msg->l3h[2] = BSS_MAP_MSG_CIPHER_MODE_REJECT;
 +      msg->l3h[3] = cause;
 +
 +      return msg;
 +}
 +
 +struct msgb *gsm0808_create_classmark_update(const uint8_t *classmark_data, uint8_t length)
 +{
 +      struct msgb *msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM,
 +                                             "classmark-update");
 +      if (!msg)
 +              return NULL;
 +
 +      msg->l3h = msgb_put(msg, 3);
 +      msg->l3h[0] = BSSAP_MSG_BSS_MANAGEMENT;
 +      msg->l3h[1] = 0xff;
 +      msg->l3h[2] = BSS_MAP_MSG_CLASSMARK_UPDATE;
 +
 +      msg->l4h = msgb_put(msg, length);
 +      memcpy(msg->l4h, classmark_data, length);
 +
 +      /* update the size */
 +      msg->l3h[1] = msgb_l3len(msg) - 2;
 +      return msg;
 +}
 +
 +struct msgb *gsm0808_create_sapi_reject(uint8_t link_id)
 +{
 +      struct msgb *msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM,
 +                                             "bssmap: sapi 'n' reject");
 +      if (!msg)
 +              return NULL;
 +
 +      msg->l3h = msgb_put(msg, 5);
 +      msg->l3h[0] = BSSAP_MSG_BSS_MANAGEMENT;
 +      msg->l3h[1] = 3;
 +      msg->l3h[2] = BSS_MAP_MSG_SAPI_N_REJECT;
 +      msg->l3h[3] = link_id;
 +      msg->l3h[4] = GSM0808_CAUSE_BSS_NOT_EQUIPPED;
 +
 +      return msg;
 +}
 +
++struct msgb *gsm0808_create_assignment_completed(uint8_t rr_cause,
 +                                               uint8_t chosen_channel, uint8_t encr_alg_id,
 +                                               uint8_t speech_mode)
 +{
 +      uint8_t *data;
 +
 +      struct msgb *msg = msgb_alloc(35, "bssmap: ass compl");
 +      if (!msg)
 +              return NULL;
 +
 +      msg->l3h = msgb_put(msg, 3);
 +      msg->l3h[0] = BSSAP_MSG_BSS_MANAGEMENT;
 +      msg->l3h[1] = 0xff;
 +      msg->l3h[2] = BSS_MAP_MSG_ASSIGMENT_COMPLETE;
 +
 +      /* write 3.2.2.22 */
 +      data = msgb_put(msg, 2);
 +      data[0] = GSM0808_IE_RR_CAUSE;
 +      data[1] = rr_cause;
 +
 +      /* write cirtcuit identity  code 3.2.2.2 */
 +      /* write cell identifier 3.2.2.17 */
 +      /* write chosen channel 3.2.2.33 when BTS picked it */
 +      data = msgb_put(msg, 2);
 +      data[0] = GSM0808_IE_CHOSEN_CHANNEL;
 +      data[1] = chosen_channel;
 +
 +      /* write chosen encryption algorithm 3.2.2.44 */
 +      data = msgb_put(msg, 2);
 +      data[0] = GSM0808_IE_CHOSEN_ENCR_ALG;
 +      data[1] = encr_alg_id;
 +
 +      /* write circuit pool 3.2.2.45 */
 +      /* write speech version chosen: 3.2.2.51 when BTS picked it */
 +      if (speech_mode != 0) {
 +              data = msgb_put(msg, 2);
 +              data[0] = GSM0808_IE_SPEECH_VERSION;
 +              data[1] = speech_mode;
 +      }
 +
 +      /* write LSA identifier 3.2.2.15 */
 +
 +
 +      /* update the size */
 +      msg->l3h[1] = msgb_l3len(msg) - 2;
 +      return msg;
 +}
 +
 +struct msgb *gsm0808_create_assignment_failure(uint8_t cause, uint8_t *rr_cause)
 +{
 +      uint8_t *data;
 +      struct msgb *msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM,
 +                                             "bssmap: ass fail");
 +      if (!msg)
 +              return NULL;
 +
 +      msg->l3h = msgb_put(msg, 6);
 +      msg->l3h[0] = BSSAP_MSG_BSS_MANAGEMENT;
 +      msg->l3h[1] = 0xff;
 +      msg->l3h[2] = BSS_MAP_MSG_ASSIGMENT_FAILURE;
 +      msg->l3h[3] = GSM0808_IE_CAUSE;
 +      msg->l3h[4] = 1;
 +      msg->l3h[5] = cause;
 +
 +      /* RR cause 3.2.2.22 */
 +      if (rr_cause) {
 +              data = msgb_put(msg, 2);
 +              data[0] = GSM0808_IE_RR_CAUSE;
 +              data[1] = *rr_cause;
 +      }
 +
 +      /* Circuit pool 3.22.45 */
 +      /* Circuit pool list 3.2.2.46 */
 +
 +      /* update the size */
 +      msg->l3h[1] = msgb_l3len(msg) - 2;
 +      return msg;
 +}
 +
 +void gsm0808_prepend_dtap_header(struct msgb *msg, uint8_t link_id)
 +{
 +      uint8_t *hh = msgb_push(msg, 3);
 +      hh[0] = BSSAP_MSG_DTAP;
 +      hh[1] = link_id;
 +      hh[2] = msg->len - 3;
 +}
 +
 +static const struct tlv_definition bss_att_tlvdef = {
 +      .def = {
 +              [GSM0808_IE_IMSI]                   = { TLV_TYPE_TLV },
 +              [GSM0808_IE_TMSI]                   = { TLV_TYPE_TLV },
 +              [GSM0808_IE_CELL_IDENTIFIER_LIST]   = { TLV_TYPE_TLV },
 +              [GSM0808_IE_CHANNEL_NEEDED]         = { TLV_TYPE_TV },
 +              [GSM0808_IE_EMLPP_PRIORITY]         = { TLV_TYPE_TV },
 +              [GSM0808_IE_CHANNEL_TYPE]           = { TLV_TYPE_TLV },
 +              [GSM0808_IE_PRIORITY]               = { TLV_TYPE_TLV },
 +              [GSM0808_IE_CIRCUIT_IDENTITY_CODE]  = { TLV_TYPE_TV },
 +              [GSM0808_IE_DOWNLINK_DTX_FLAG]      = { TLV_TYPE_TV },
 +              [GSM0808_IE_INTERFERENCE_BAND_TO_USE] = { TLV_TYPE_TV },
 +              [GSM0808_IE_CLASSMARK_INFORMATION_T2] = { TLV_TYPE_TLV },
 +              [GSM0808_IE_GROUP_CALL_REFERENCE]   = { TLV_TYPE_TLV },
 +              [GSM0808_IE_TALKER_FLAG]            = { TLV_TYPE_T },
 +              [GSM0808_IE_CONFIG_EVO_INDI]        = { TLV_TYPE_TV },
 +              [GSM0808_IE_LSA_ACCESS_CTRL_SUPPR]  = { TLV_TYPE_TV },
 +              [GSM0808_IE_SERVICE_HANDOVER]       = { TLV_TYPE_TV},
 +              [GSM0808_IE_ENCRYPTION_INFORMATION] = { TLV_TYPE_TLV },
 +              [GSM0808_IE_CIPHER_RESPONSE_MODE]   = { TLV_TYPE_TV },
 +              [GSM0808_IE_CELL_IDENTIFIER]        = { TLV_TYPE_TLV },
 +              [GSM0808_IE_CHOSEN_CHANNEL]         = { TLV_TYPE_TV },
 +              [GSM0808_IE_LAYER_3_INFORMATION]    = { TLV_TYPE_TLV },
 +      },
 +};
 +
 +const struct tlv_definition *gsm0808_att_tlvdef()
 +{
 +      return &bss_att_tlvdef;
 +}
index dc97cef,0000000..4b4e2c6
mode 100644,000000..100644
--- /dev/null
@@@ -1,393 -1,0 +1,481 @@@
- /* GSM 03.38 6.2.1 Charachter packing */
 +/*
 + * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
 + * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
 + * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
++ * (C) 2010 by Nico Golde <nico@ngolde.de>
 + *
 + * All Rights Reserved
 + *
 + * 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.,
 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 + *
 + */
 +
 +//#include <openbsc/gsm_data.h>
 +#include <osmocore/utils.h>
 +#include <osmocore/gsm_utils.h>
 +
 +#include <stdlib.h>
 +#include <stdint.h>
 +#include <string.h>
 +#include <stdio.h>
 +#include <errno.h>
 +#include <ctype.h>
 +
 +#include "../config.h"
 +
-         /* FIXME: We need to account for user data headers here */
++/* ETSI GSM 03.38 6.2.1 and 6.2.1.1 default alphabet
++ * Greek symbols at hex positions 0x10 and 0x12-0x1a
++ * left out as they can't be handled with a char and
++ * since most phones don't display or write these
++ * characters this would only needlessly make the code
++ * more complex
++*/
++static unsigned char gsm_7bit_alphabet[] = {
++      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0xff, 0xff, 0x0d, 0xff,
++      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
++      0xff, 0xff, 0x20, 0x21, 0x22, 0x23, 0x02, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
++      0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
++      0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
++      0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
++      0x5a, 0x3c, 0x2f, 0x3e, 0x14, 0x11, 0xff, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
++      0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
++      0x78, 0x79, 0x7a, 0x28, 0x40, 0x29, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
++      0xff, 0xff, 0x0c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff,
++      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x40, 0xff, 0x01, 0xff,
++      0x03, 0xff, 0x7b, 0x7d, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0xff, 0xff, 0xff, 0xff,
++      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5b, 0x7e, 0x5d, 0xff, 0x7c, 0xff, 0xff, 0xff,
++      0xff, 0x5b, 0x0e, 0x1c, 0x09, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5d,
++      0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0x0b, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff, 0x1e, 0x7f,
++      0xff, 0xff, 0xff, 0x7b, 0x0f, 0x1d, 0xff, 0x04, 0x05, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff,
++      0xff, 0x7d, 0x08, 0xff, 0xff, 0xff, 0x7c, 0xff, 0x0c, 0x06, 0xff, 0xff, 0x7e, 0xff, 0xff
++};
++
++/* GSM 03.38 6.2.1 Character lookup for decoding */
++static int gsm_septet_lookup(uint8_t ch)
++{
++      int i = 0;
++      for(; i < sizeof(gsm_7bit_alphabet); i++){
++              if(gsm_7bit_alphabet[i] == ch)
++                      return i;
++      }
++      return -1;
++}
++
++/* GSM 03.38 6.2.1 Character unpacking */
 +int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length)
 +{
 +      int i = 0;
 +      int l = 0;
++      int septet_l = (length * 8) / 7;
++      uint8_t *rtext = calloc(septet_l, sizeof(uint8_t));
++      uint8_t tmp;
 +
-       for (; i < length; i ++)
-               *(text ++) =
++      /* FIXME: We need to account for user data headers here */
 +      i += l;
-       return i - l;
++      for (; i < septet_l; i++){
++              rtext[i] =
 +                      ((user_data[(i * 7 + 7) >> 3] <<
 +                        (7 - ((i * 7 + 7) & 7))) |
 +                       (user_data[(i * 7) >> 3] >>
 +                        ((i * 7) & 7))) & 0x7f;
++      }
++
++      for(i = 0; i < septet_l; i++){
++              /* this is an extension character */
++              if(rtext[i] == 0x1b && i + 1 < length){
++                      tmp = rtext[i+1];
++                      *(text++) = gsm_7bit_alphabet[0x7f + tmp];
++                      i++;
++                      continue;
++              }
++
++              *(text++) = gsm_septet_lookup(rtext[i]);
++      }
++
 +      *text = '\0';
++      free(rtext);
 +
- /* GSM 03.38 6.2.1 Charachter packing */
++      return i;
 +}
 +
++/* GSM 03.38 6.2.1 Prepare character packing */
++static int gsm_septet_encode(uint8_t *result, const char *data)
++{
++      int i, y = 0;
++      uint8_t ch;
++      for(i = 0; i < strlen(data); i++){
++              ch = data[i];
++              switch(ch){
++              /* fall-through for extension characters */
++              case 0x0c:
++              case 0x5e:
++              case 0x7b:
++              case 0x7d:
++              case 0x5c:
++              case 0x5b:
++              case 0x7e:
++              case 0x5d:
++              case 0x7c:
++                      result[y++] = 0x1b;
++              default:
++                      result[y] = gsm_7bit_alphabet[ch];
++                      break;
++              }
++              y++;
++      }
 +
-       int i,j = 0;
-       unsigned char ch1, ch2;
++      return y;
++}
++
++/* GSM 03.38 6.2.1 Character packing */
 +int gsm_7bit_encode(uint8_t *result, const char *data)
 +{
-       for ( i=0; i<strlen(data); i++ ) {
++      int i,y,z = 0;
++      /* prepare for the worst case, every character expanding to two bytes */
++      uint8_t *rdata = calloc(strlen(data) * 2, sizeof(uint8_t));
++      uint8_t cb, nb;
 +      int shift = 0;
 +
-               ch1 = data[i] & 0x7F;
-               ch1 = ch1 >> shift;
-               ch2 = data[(i+1)] & 0x7F;
-               ch2 = ch2 << (7-shift);
++      y = gsm_septet_encode(rdata, data);
 +
-               ch1 = ch1 | ch2;
++      for(i = 0; i < y; i++) {
++              if(shift == 7 && i + 1 < y){
++                      shift = 0;
++                      continue;
++              }
 +
-               result[j++] = ch1;
++              cb = (rdata[i] & 0x7f) >> shift;
++              if(i + 1 < y){
++                      nb = (rdata[i + 1] & 0x7f) << (7 - shift);
++                      cb = cb | nb;
++              }
 +
-               if ((shift == 7) && (i+1<strlen(data))) {
-                       shift = 0;
-                       i++;
-               }
++              result[z++] = cb;
 +
 +              shift++;
-       return i;
 +      }
 +
++      free(rdata);
++      return z;
 +}
 +
 +/* determine power control level for given dBm value, as indicated
 + * by the tables in chapter 4.1.1 of GSM TS 05.05 */
 +int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm)
 +{
 +      switch (band) {
 +      case GSM_BAND_450:
 +      case GSM_BAND_480:
 +      case GSM_BAND_750:
 +      case GSM_BAND_900:
 +      case GSM_BAND_810:
 +      case GSM_BAND_850:
 +              if (dbm >= 39)
 +                      return 0;
 +              else if (dbm < 5)
 +                      return 19;
 +              else {
 +                      /* we are guaranteed to have (5 <= dbm < 39) */
 +                      return 2 + ((39 - dbm) / 2);
 +              }
 +              break;
 +      case GSM_BAND_1800:
 +              if (dbm >= 36)
 +                      return 29;
 +              else if (dbm >= 34)     
 +                      return 30;
 +              else if (dbm >= 32)
 +                      return 31;
 +              else if (dbm == 31)
 +                      return 0;
 +              else {
 +                      /* we are guaranteed to have (0 <= dbm < 31) */
 +                      return (30 - dbm) / 2;
 +              }
 +              break;
 +      case GSM_BAND_1900:
 +              if (dbm >= 33)
 +                      return 30;
 +              else if (dbm >= 32)
 +                      return 31;
 +              else if (dbm == 31)
 +                      return 0;
 +              else {
 +                      /* we are guaranteed to have (0 <= dbm < 31) */
 +                      return (30 - dbm) / 2;
 +              }
 +              break;
 +      }
 +      return -EINVAL;
 +}
 +
 +int ms_pwr_dbm(enum gsm_band band, uint8_t lvl)
 +{
 +      lvl &= 0x1f;
 +
 +      switch (band) {
 +      case GSM_BAND_450:
 +      case GSM_BAND_480:
 +      case GSM_BAND_750:
 +      case GSM_BAND_900:
 +      case GSM_BAND_810:
 +      case GSM_BAND_850:
 +              if (lvl < 2)
 +                      return 39;
 +              else if (lvl < 20)
 +                      return 39 - ((lvl - 2) * 2) ;
 +              else
 +                      return 5;
 +              break;
 +      case GSM_BAND_1800:
 +              if (lvl < 16)
 +                      return 30 - (lvl * 2);
 +              else if (lvl < 29)
 +                      return 0;
 +              else
 +                      return 36 - ((lvl - 29) * 2);
 +              break;
 +      case GSM_BAND_1900:
 +              if (lvl < 16)
 +                      return 30 - (lvl * 2);
 +              else if (lvl < 30)
 +                      return -EINVAL;
 +              else
 +                      return 33 - (lvl - 30);
 +              break;
 +      }
 +      return -EINVAL;
 +}
 +
 +/* According to TS 08.05 Chapter 8.1.4 */
 +int rxlev2dbm(uint8_t rxlev)
 +{
 +      if (rxlev > 63)
 +              rxlev = 63;
 +
 +      return -110 + rxlev;
 +}
 +
 +/* According to TS 08.05 Chapter 8.1.4 */
 +uint8_t dbm2rxlev(int dbm)
 +{
 +      int rxlev = dbm + 110;
 +
 +      if (rxlev > 63)
 +              rxlev = 63;
 +      else if (rxlev < 0)
 +              rxlev = 0;
 +
 +      return rxlev;
 +}
 +
 +const char *gsm_band_name(enum gsm_band band)
 +{
 +      switch (band) {
 +      case GSM_BAND_450:
 +              return "GSM450";
 +      case GSM_BAND_480:
 +              return "GSM480";
 +      case GSM_BAND_750:
 +              return "GSM750";
 +      case GSM_BAND_810:
 +              return "GSM810";
 +      case GSM_BAND_850:
 +              return "GSM850";
 +      case GSM_BAND_900:
 +              return "GSM900";
 +      case GSM_BAND_1800:
 +              return "DCS1800";
 +      case GSM_BAND_1900:
 +              return "PCS1900";
 +      }
 +      return "invalid";
 +}
 +
 +enum gsm_band gsm_band_parse(const char* mhz)
 +{
 +      while (*mhz && !isdigit(*mhz))
 +              mhz++;
 +
 +      if (*mhz == '\0')
 +              return -EINVAL;
 +
 +      switch (strtol(mhz, NULL, 10)) {
 +      case 450:
 +              return GSM_BAND_450;
 +      case 480:
 +              return GSM_BAND_480;
 +      case 750:
 +              return GSM_BAND_750;
 +      case 810:
 +              return GSM_BAND_810;
 +      case 850:
 +              return GSM_BAND_850;
 +      case 900:
 +              return GSM_BAND_900;
 +      case 1800:
 +              return GSM_BAND_1800;
 +      case 1900:
 +              return GSM_BAND_1900;
 +      default:
 +              return -EINVAL;
 +      }
 +}
 +
 +
 +#ifdef HAVE_EXECINFO_H
 +#include <execinfo.h>
 +void generate_backtrace()
 +{
 +      int i, nptrs;
 +      void *buffer[100];
 +      char **strings;
 +
 +      nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
 +      printf("backtrace() returned %d addresses\n", nptrs);
 +
 +      strings = backtrace_symbols(buffer, nptrs);
 +      if (!strings)
 +              return;
 +
 +      for (i = 1; i < nptrs; i++)
 +              printf("%s\n", strings[i]);
 +
 +      free(strings);
 +}
 +#endif
 +
 +enum gsm_band gsm_arfcn2band(uint16_t arfcn)
 +{
 +      if (arfcn & ARFCN_PCS)
 +              return GSM_BAND_1900;
 +      else if (arfcn <= 124)
 +              return GSM_BAND_900;
 +      else if (arfcn >= 955 && arfcn <= 1023)
 +              return GSM_BAND_900;
 +      else if (arfcn >= 128 && arfcn <= 251)
 +              return GSM_BAND_850;
 +      else if (arfcn >= 512 && arfcn <= 885)
 +              return GSM_BAND_1800;
 +      else if (arfcn >= 259 && arfcn <= 293)
 +              return GSM_BAND_450;
 +      else if (arfcn >= 306 && arfcn <= 340)
 +              return GSM_BAND_480;
 +      else if (arfcn >= 350 && arfcn <= 425)
 +              return GSM_BAND_810;
 +      else if (arfcn >= 438 && arfcn <= 511)
 +              return GSM_BAND_750;
 +      else
 +              return GSM_BAND_1800;
 +}
 +
 +/* Convert an ARFCN to the frequency in MHz * 10 */
 +uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink)
 +{
 +      uint16_t freq10_ul;
 +      uint16_t freq10_dl;
 +
 +      if (arfcn & ARFCN_PCS) {
 +              /* DCS 1900 */
 +              arfcn &= ~ARFCN_PCS;
 +              freq10_ul = 18502 + 2 * (arfcn-512);
 +              freq10_dl = freq10_ul + 800;
 +      } else if (arfcn <= 124) {
 +              /* Primary GSM + ARFCN 0 of E-GSM */
 +              freq10_ul = 8900 + 2 * arfcn;
 +              freq10_dl = freq10_ul + 450;
 +      } else if (arfcn >= 955 && arfcn <= 1023) {
 +              /* E-GSM and R-GSM */
 +              freq10_ul = 8900 + 2 * (arfcn - 1024);
 +              freq10_dl = freq10_ul + 450;
 +      } else if (arfcn >= 128 && arfcn <= 251) {
 +              /* GSM 850 */
 +              freq10_ul = 8242 + 2 * (arfcn - 128);
 +              freq10_dl = freq10_ul + 450;
 +      } else if (arfcn >= 512 && arfcn <= 885) {
 +              /* DCS 1800 */
 +              freq10_ul = 17102 + 2 * (arfcn - 512);
 +              freq10_dl = freq10_ul + 950;
 +      } else if (arfcn >= 259 && arfcn <= 293) {
 +              /* GSM 450 */
 +              freq10_ul = 4506 + 2 * (arfcn - 259);
 +              freq10_dl = freq10_ul + 100;
 +      } else if (arfcn >= 306 && arfcn <= 340) {
 +              /* GSM 480 */
 +              freq10_ul = 4790 + 2 * (arfcn - 306);
 +              freq10_dl = freq10_ul + 100;
 +      } else if (arfcn >= 350 && arfcn <= 425) {
 +              /* GSM 810 */
 +              freq10_ul = 8060 + 2 * (arfcn - 350);
 +              freq10_dl = freq10_ul + 450;
 +      } else if (arfcn >= 438 && arfcn <= 511) {
 +              /* GSM 750 */
 +              freq10_ul = 7472 + 2 * (arfcn - 438);
 +              freq10_dl = freq10_ul + 300;
 +      } else
 +              return 0xffff;
 +
 +      if (uplink)
 +              return freq10_ul;
 +      else
 +              return freq10_dl;
 +}
 +
 +void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn)
 +{
 +      time->fn = fn;
 +      time->t1 = time->fn / (26*51);
 +      time->t2 = time->fn % 26;
 +      time->t3 = time->fn % 51;
 +      time->tc = (time->fn / 51) % 8;
 +}
 +
 +uint32_t gsm_gsmtime2fn(struct gsm_time *time)
 +{
 +      /* TS 05.02 Chapter 4.3.3 TDMA frame number */
 +      return (51 * ((time->t3 - time->t2 + 26) % 26) + time->t3 + (26 * 51 * time->t1));
 +}
 +
 +/* TS 03.03 Chapter 2.6 */
 +int gprs_tlli_type(uint32_t tlli)
 +{
 +      if ((tlli & 0xc0000000) == 0xc0000000)
 +              return TLLI_LOCAL;
 +      else if ((tlli & 0xc0000000) == 0x80000000)
 +              return TLLI_FOREIGN;
 +      else if ((tlli & 0xf8000000) == 0x78000000)
 +              return TLLI_RANDOM;
 +      else if ((tlli & 0xf8000000) == 0x70000000)
 +              return TLLI_AUXILIARY;
 +
 +      return TLLI_RESERVED;
 +}
 +
 +uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type)
 +{
 +      uint32_t tlli;
 +      switch (type) {
 +      case TLLI_LOCAL:
 +              tlli = p_tmsi | 0xc0000000;
 +              break;
 +      case TLLI_FOREIGN:
 +              tlli = (p_tmsi & 0x3fffffff) | 0x80000000;
 +              break;
 +      default:
 +              tlli = 0;
 +              break;
 +      }
 +      return tlli;
 +}
index 0000000,0000000..a25067d
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,71 @@@
++/* Panic handling */
++/*
++ * (C) 2010 by Sylvain Munaut <tnt@246tNt.com>
++ *
++ * All Rights Reserved
++ *
++ * 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.,
++ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
++ *
++ */
++
++#include <osmocore/panic.h>
++
++#include "../config.h"
++
++
++static osmo_panic_handler_t osmo_panic_handler = (void*)0;
++
++
++#ifndef PANIC_INFLOOP
++
++#include <stdio.h>
++#include <stdlib.h>
++
++static void osmo_panic_default(const char *fmt, va_list args)
++{
++      vfprintf(stderr, fmt, args);
++      abort();
++}
++
++#else
++
++static void osmo_panic_default(const char *fmt, va_list args)
++{
++      while (1);
++}
++
++#endif
++
++
++void osmo_panic(const char *fmt, ...)
++{
++      va_list args;
++
++      va_start(args, fmt);
++
++      if (osmo_panic_handler)
++              osmo_panic_handler(fmt, args);
++      else
++              osmo_panic_default(fmt, args);
++
++      va_end(args);
++}
++ 
++
++void osmo_set_panic_handler(osmo_panic_handler_t *h)
++{
++      osmo_panic_handler = h;
++}
++
index 7bc1712,0000000..10fba9b
mode 100644,000000..100644
--- /dev/null
@@@ -1,344 -1,0 +1,370 @@@
 +/* GSM Radio Signalling Link messages on the A-bis interface 
 + * 3GPP TS 08.58 version 8.6.0 Release 1999 / ETSI TS 100 596 V8.6.0 */
 +
 +/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
 + *
 + * All Rights Reserved
 + *
 + * 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.,
 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 + *
 + */
 +
 +#include <stdint.h>
 +#include <errno.h>
 +
 +#include <osmocore/tlv.h>
 +#include <osmocore/rsl.h>
 +
 +#define RSL_ALLOC_SIZE                200
 +#define RSL_ALLOC_HEADROOM    56
 +
 +void rsl_init_rll_hdr(struct abis_rsl_rll_hdr *dh, uint8_t msg_type)
 +{
 +      dh->c.msg_discr = ABIS_RSL_MDISC_RLL;
 +      dh->c.msg_type = msg_type;
 +      dh->ie_chan = RSL_IE_CHAN_NR;
 +      dh->ie_link_id = RSL_IE_LINK_IDENT;
 +}
 +
 +void rsl_init_cchan_hdr(struct abis_rsl_cchan_hdr *ch, uint8_t msg_type)
 +{
 +      ch->c.msg_discr = ABIS_RSL_MDISC_COM_CHAN;
 +      ch->c.msg_type = msg_type;
 +      ch->ie_chan = RSL_IE_CHAN_NR;
 +}
 +
 +const struct tlv_definition rsl_att_tlvdef = {
 +      .def = {
 +              [RSL_IE_CHAN_NR]                = { TLV_TYPE_TV },
 +              [RSL_IE_LINK_IDENT]             = { TLV_TYPE_TV },
 +              [RSL_IE_ACT_TYPE]               = { TLV_TYPE_TV },
 +              [RSL_IE_BS_POWER]               = { TLV_TYPE_TV },
 +              [RSL_IE_CHAN_IDENT]             = { TLV_TYPE_TLV },
 +              [RSL_IE_CHAN_MODE]              = { TLV_TYPE_TLV },
 +              [RSL_IE_ENCR_INFO]              = { TLV_TYPE_TLV },
 +              [RSL_IE_FRAME_NUMBER]           = { TLV_TYPE_FIXED, 2 },
 +              [RSL_IE_HANDO_REF]              = { TLV_TYPE_TV },
 +              [RSL_IE_L1_INFO]                = { TLV_TYPE_FIXED, 2 },
 +              [RSL_IE_L3_INFO]                = { TLV_TYPE_TL16V },
 +              [RSL_IE_MS_IDENTITY]            = { TLV_TYPE_TLV },
 +              [RSL_IE_MS_POWER]               = { TLV_TYPE_TV },
 +              [RSL_IE_PAGING_GROUP]           = { TLV_TYPE_TV },
 +              [RSL_IE_PAGING_LOAD]            = { TLV_TYPE_FIXED, 2 },
 +              [RSL_IE_PYHS_CONTEXT]           = { TLV_TYPE_TLV },
 +              [RSL_IE_ACCESS_DELAY]           = { TLV_TYPE_TV },
 +              [RSL_IE_RACH_LOAD]              = { TLV_TYPE_TLV },
 +              [RSL_IE_REQ_REFERENCE]          = { TLV_TYPE_FIXED, 3 },
 +              [RSL_IE_RELEASE_MODE]           = { TLV_TYPE_TV },
 +              [RSL_IE_RESOURCE_INFO]          = { TLV_TYPE_TLV },
 +              [RSL_IE_RLM_CAUSE]              = { TLV_TYPE_TLV },
 +              [RSL_IE_STARTNG_TIME]           = { TLV_TYPE_FIXED, 2 },
 +              [RSL_IE_TIMING_ADVANCE]         = { TLV_TYPE_TV },
 +              [RSL_IE_UPLINK_MEAS]            = { TLV_TYPE_TLV },
 +              [RSL_IE_CAUSE]                  = { TLV_TYPE_TLV },
 +              [RSL_IE_MEAS_RES_NR]            = { TLV_TYPE_TV },
 +              [RSL_IE_MSG_ID]                 = { TLV_TYPE_TV },
 +              [RSL_IE_SYSINFO_TYPE]           = { TLV_TYPE_TV },
 +              [RSL_IE_MS_POWER_PARAM]         = { TLV_TYPE_TLV },
 +              [RSL_IE_BS_POWER_PARAM]         = { TLV_TYPE_TLV },
 +              [RSL_IE_PREPROC_PARAM]          = { TLV_TYPE_TLV },
 +              [RSL_IE_PREPROC_MEAS]           = { TLV_TYPE_TLV },
 +              [RSL_IE_IMM_ASS_INFO]           = { TLV_TYPE_TLV },
 +              [RSL_IE_SMSCB_INFO]             = { TLV_TYPE_FIXED, 23 },
 +              [RSL_IE_MS_TIMING_OFFSET]       = { TLV_TYPE_TV },
 +              [RSL_IE_ERR_MSG]                = { TLV_TYPE_TLV },
 +              [RSL_IE_FULL_BCCH_INFO]         = { TLV_TYPE_TLV },
 +              [RSL_IE_CHAN_NEEDED]            = { TLV_TYPE_TV },
 +              [RSL_IE_CB_CMD_TYPE]            = { TLV_TYPE_TV },
 +              [RSL_IE_SMSCB_MSG]              = { TLV_TYPE_TLV },
 +              [RSL_IE_FULL_IMM_ASS_INFO]      = { TLV_TYPE_TLV },
 +              [RSL_IE_SACCH_INFO]             = { TLV_TYPE_TLV },
 +              [RSL_IE_CBCH_LOAD_INFO]         = { TLV_TYPE_TV },
 +              [RSL_IE_SMSCB_CHAN_INDICATOR]   = { TLV_TYPE_TV },
 +              [RSL_IE_GROUP_CALL_REF]         = { TLV_TYPE_TLV },
 +              [RSL_IE_CHAN_DESC]              = { TLV_TYPE_TLV },
 +              [RSL_IE_NCH_DRX_INFO]           = { TLV_TYPE_TLV },
 +              [RSL_IE_CMD_INDICATOR]          = { TLV_TYPE_TLV },
 +              [RSL_IE_EMLPP_PRIO]             = { TLV_TYPE_TV },
 +              [RSL_IE_UIC]                    = { TLV_TYPE_TLV },
 +              [RSL_IE_MAIN_CHAN_REF]          = { TLV_TYPE_TV },
 +              [RSL_IE_MR_CONFIG]              = { TLV_TYPE_TLV },
 +              [RSL_IE_MR_CONTROL]             = { TLV_TYPE_TV },
 +              [RSL_IE_SUP_CODEC_TYPES]        = { TLV_TYPE_TLV },
 +              [RSL_IE_CODEC_CONFIG]           = { TLV_TYPE_TLV },
 +              [RSL_IE_RTD]                    = { TLV_TYPE_TV },
 +              [RSL_IE_TFO_STATUS]             = { TLV_TYPE_TV },
 +              [RSL_IE_LLP_APDU]               = { TLV_TYPE_TLV },
 +              [RSL_IE_SIEMENS_MRPCI]          = { TLV_TYPE_TV },
 +              [RSL_IE_IPAC_PROXY_UDP]         = { TLV_TYPE_FIXED, 2 },
 +              [RSL_IE_IPAC_BSCMPL_TOUT]       = { TLV_TYPE_TV },
 +              [RSL_IE_IPAC_REMOTE_IP]         = { TLV_TYPE_FIXED, 4 },
 +              [RSL_IE_IPAC_REMOTE_PORT]       = { TLV_TYPE_FIXED, 2 },
 +              [RSL_IE_IPAC_RTP_PAYLOAD]       = { TLV_TYPE_TV },
 +              [RSL_IE_IPAC_LOCAL_PORT]        = { TLV_TYPE_FIXED, 2 },
 +              [RSL_IE_IPAC_SPEECH_MODE]       = { TLV_TYPE_TV },
 +              [RSL_IE_IPAC_LOCAL_IP]          = { TLV_TYPE_FIXED, 4 },
 +              [RSL_IE_IPAC_CONN_ID]           = { TLV_TYPE_FIXED, 2 },
 +              [RSL_IE_IPAC_RTP_CSD_FMT]       = { TLV_TYPE_TV },
 +              [RSL_IE_IPAC_RTP_JIT_BUF]       = { TLV_TYPE_FIXED, 2 },
 +              [RSL_IE_IPAC_RTP_COMPR]         = { TLV_TYPE_TV },
 +              [RSL_IE_IPAC_RTP_PAYLOAD2]      = { TLV_TYPE_TV },
 +              [RSL_IE_IPAC_RTP_MPLEX]         = { TLV_TYPE_FIXED, 8 },
 +              [RSL_IE_IPAC_RTP_MPLEX_ID]      = { TLV_TYPE_TV },
 +      },
 +};
 +
 +/* encode channel number as per Section 9.3.1 */
 +uint8_t rsl_enc_chan_nr(uint8_t type, uint8_t subch, uint8_t timeslot)
 +{
 +      uint8_t ret;
 +
 +      ret = (timeslot & 0x07) | type;
 +
 +      switch (type) {
 +      case RSL_CHAN_Lm_ACCHs:
 +              subch &= 0x01;
 +              break;
 +      case RSL_CHAN_SDCCH4_ACCH:
 +              subch &= 0x03;
 +              break;
 +      case RSL_CHAN_SDCCH8_ACCH:
 +              subch &= 0x07;
 +              break;
 +      default:
 +              /* no subchannels allowed */
 +              subch = 0x00;
 +              break;
 +      }
 +      ret |= (subch << 3);
 +
 +      return ret;
 +}
 +
 +int rsl_dec_chan_nr(uint8_t chan_nr, uint8_t *type, uint8_t *subch, uint8_t *timeslot)
 +{
 +      *timeslot = chan_nr & 0x7;
 +
 +      if ((chan_nr & 0xf8) == RSL_CHAN_Bm_ACCHs) {
 +              *type = RSL_CHAN_Bm_ACCHs;
 +              *subch = 0;
 +      } else if ((chan_nr & 0xf0) == RSL_CHAN_Lm_ACCHs) {
 +              *type = RSL_CHAN_Lm_ACCHs;
 +              *subch = (chan_nr >> 3) & 0x1;
 +      } else if ((chan_nr & 0xe0) == RSL_CHAN_SDCCH4_ACCH) {
 +              *type = RSL_CHAN_SDCCH4_ACCH;
 +              *subch = (chan_nr >> 3) & 0x3;
 +      } else if ((chan_nr & 0xc0) == RSL_CHAN_SDCCH8_ACCH) {
 +              *type = RSL_CHAN_SDCCH8_ACCH;
 +              *subch = (chan_nr >> 3) & 0x7;
 +      } else if ((chan_nr & 0xf8) == RSL_CHAN_BCCH) {
 +              *type = RSL_CHAN_BCCH;
 +              *subch = 0;
 +      } else if ((chan_nr & 0xf8) == RSL_CHAN_RACH) {
 +              *type = RSL_CHAN_RACH;
 +              *subch = 0;
 +      } else if ((chan_nr & 0xf8) == RSL_CHAN_PCH_AGCH) {
 +              *type = RSL_CHAN_PCH_AGCH;
 +              *subch = 0;
 +      } else
 +              return -EINVAL;
 +
 +      return 0;
 +}
 +
++const char *rsl_chan_nr_str(uint8_t chan_nr)
++{
++      static char str[20];
++      int ts = chan_nr & 7;
++      uint8_t cbits = chan_nr >> 3;
++
++      if (cbits == 0x01)
++              sprintf(str, "TCH/F on TS%d", ts);
++      else if ((cbits & 0x1e) == 0x02)
++              sprintf(str, "TCH/H(%u) on TS%d", cbits & 0x01, ts);
++      else if ((cbits & 0x1c) == 0x04)
++              sprintf(str, "SDCCH/4(%u) on TS%d", cbits & 0x03, ts);
++      else if ((cbits & 0x18) == 0x08)
++              sprintf(str, "SDCCH/8(%u) on TS%d", cbits & 0x07, ts);
++      else if (cbits == 0x10)
++              sprintf(str, "BCCH on TS%d", ts);
++      else if (cbits == 0x11)
++              sprintf(str, "RACH on TS%d", ts);
++      else if (cbits == 0x12)
++              sprintf(str, "PCH/AGCH on TS%d", ts);
++      else
++              sprintf(str, "UNKNOWN on TS%d", ts);
++
++        return str;
++}
++
 +static const struct value_string rsl_err_vals[] = {
 +      { RSL_ERR_RADIO_IF_FAIL,        "Radio Interface Failure" },
 +      { RSL_ERR_RADIO_LINK_FAIL,      "Radio Link Failure" },
 +      { RSL_ERR_HANDOVER_ACC_FAIL,    "Handover Access Failure" },
 +      { RSL_ERR_TALKER_ACC_FAIL,      "Talker Access Failure" },
 +      { RSL_ERR_OM_INTERVENTION,      "O&M Intervention" },
 +      { RSL_ERR_NORMAL_UNSPEC,        "Normal event, unspecified" },
 +      { RSL_ERR_T_MSRFPCI_EXP,        "Siemens: T_MSRFPCI Expired" },
 +      { RSL_ERR_EQUIPMENT_FAIL,       "Equipment Failure" },
 +      { RSL_ERR_RR_UNAVAIL,           "Radio Resource not available" },
 +      { RSL_ERR_TERR_CH_FAIL,         "Terrestrial Channel Failure" },
 +      { RSL_ERR_CCCH_OVERLOAD,        "CCCH Overload" },
 +      { RSL_ERR_ACCH_OVERLOAD,        "ACCH Overload" },
 +      { RSL_ERR_PROCESSOR_OVERLOAD,   "Processor Overload" },
 +      { RSL_ERR_RES_UNAVAIL,          "Resource not available, unspecified" },
 +      { RSL_ERR_TRANSC_UNAVAIL,       "Transcoding not available" },
 +      { RSL_ERR_SERV_OPT_UNAVAIL,     "Service or Option not available" },
 +      { RSL_ERR_ENCR_UNIMPL,          "Encryption algorithm not implemented" },
 +      { RSL_ERR_SERV_OPT_UNIMPL,      "Service or Option not implemented" },
 +      { RSL_ERR_RCH_ALR_ACTV_ALLOC,   "Radio channel already activated" },
 +      { RSL_ERR_INVALID_MESSAGE,      "Invalid Message, unspecified" },
 +      { RSL_ERR_MSG_DISCR,            "Message Discriminator Error" },
 +      { RSL_ERR_MSG_TYPE,             "Message Type Error" },
 +      { RSL_ERR_MSG_SEQ,              "Message Sequence Error" },
 +      { RSL_ERR_IE_ERROR,             "General IE error" },
 +      { RSL_ERR_MAND_IE_ERROR,        "Mandatory IE error" },
 +      { RSL_ERR_OPT_IE_ERROR,         "Optional IE error" },
 +      { RSL_ERR_IE_NONEXIST,          "IE non-existent" },
 +      { RSL_ERR_IE_LENGTH,            "IE length error" },
 +      { RSL_ERR_IE_CONTENT,           "IE content error" },
 +      { RSL_ERR_PROTO,                "Protocol error, unspecified" },
 +      { RSL_ERR_INTERWORKING,         "Interworking error, unspecified" },
 +      { 0,                            NULL }
 +};
 +
 +const char *rsl_err_name(uint8_t err)
 +{
 +      return get_value_string(rsl_err_vals, err);
 +}
 +
 +static const struct value_string rsl_rlm_cause_strs[] = {
 +      { RLL_CAUSE_T200_EXPIRED,       "Timer T200 expired (N200+1) times" },
 +      { RLL_CAUSE_REEST_REQ,          "Re-establishment request" },
 +      { RLL_CAUSE_UNSOL_UA_RESP,      "Unsolicited UA response" },
 +      { RLL_CAUSE_UNSOL_DM_RESP,      "Unsolicited DM response" },
 +      { RLL_CAUSE_UNSOL_DM_RESP_MF,   "Unsolicited DM response, multiple frame" },
 +      { RLL_CAUSE_UNSOL_SPRV_RESP,    "Unsolicited supervisory response" },
 +      { RLL_CAUSE_SEQ_ERR,            "Sequence Error" },
 +      { RLL_CAUSE_UFRM_INC_PARAM,     "U-Frame with incorrect parameters" },
 +      { RLL_CAUSE_SFRM_INC_PARAM,     "S-Frame with incorrect parameters" },
 +      { RLL_CAUSE_IFRM_INC_MBITS,     "I-Frame with incorrect use of M bit" },
 +      { RLL_CAUSE_IFRM_INC_LEN,       "I-Frame with incorrect length" },
 +      { RLL_CAUSE_FRM_UNIMPL,         "Fraeme not implemented" },
 +      { RLL_CAUSE_SABM_MF,            "SABM command, multiple frame established state" },
 +      { RLL_CAUSE_SABM_INFO_NOTALL,   "SABM frame with information not allowed in this state" },
 +      { 0,                            NULL },
 +};
 +
 +const char *rsl_rlm_cause_name(uint8_t err)
 +{
 +      return get_value_string(rsl_rlm_cause_strs, err);
 +}
 +
 +/* Section 3.3.2.3 TS 05.02. I think this looks like a table */
 +int rsl_ccch_conf_to_bs_cc_chans(int ccch_conf)
 +{
 +      switch (ccch_conf) {
 +      case RSL_BCCH_CCCH_CONF_1_NC:
 +              return 1;
 +      case RSL_BCCH_CCCH_CONF_1_C:
 +              return 1;
 +      case RSL_BCCH_CCCH_CONF_2_NC:
 +              return 2;
 +      case RSL_BCCH_CCCH_CONF_3_NC:
 +              return 3;
 +      case RSL_BCCH_CCCH_CONF_4_NC:
 +              return 4;
 +      default:
 +              return -1;
 +      }
 +}
 +
 +/* Section 3.3.2.3 TS 05.02 */
 +int rsl_ccch_conf_to_bs_ccch_sdcch_comb(int ccch_conf)
 +{
 +      switch (ccch_conf) {
 +      case RSL_BCCH_CCCH_CONF_1_NC:
 +              return 0;
 +      case RSL_BCCH_CCCH_CONF_1_C:
 +              return 1;
 +      case RSL_BCCH_CCCH_CONF_2_NC:
 +              return 0;
 +      case RSL_BCCH_CCCH_CONF_3_NC:
 +              return 0;
 +      case RSL_BCCH_CCCH_CONF_4_NC:
 +              return 0;
 +      default:
 +              return -1;
 +      }
 +}
 +
 +/* Push a RSL RLL header */
 +void rsl_rll_push_hdr(struct msgb *msg, uint8_t msg_type, uint8_t chan_nr,
 +                    uint8_t link_id, int transparent)
 +{
 +      struct abis_rsl_rll_hdr *rh;
 +
 +      rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
 +      rsl_init_rll_hdr(rh, msg_type);
 +      if (transparent)
 +              rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
 +      rh->chan_nr = chan_nr;
 +      rh->link_id = link_id;
 +
 +      /* set the l2 header pointer */
 +      msg->l2h = (uint8_t *)rh;
 +}
 +
 +/* Push a RSL RLL header with L3_INFO IE */
 +void rsl_rll_push_l3(struct msgb *msg, uint8_t msg_type, uint8_t chan_nr,
 +                   uint8_t link_id, int transparent)
 +{
 +      uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
 +
 +      /* construct a RSLms RLL message (DATA INDICATION, UNIT DATA
 +       * INDICATION) and send it off via RSLms */
 +
 +      /* Push the L3 IE tag and lengh */
 +      msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
 +
 +      /* Then push the RSL header */
 +      rsl_rll_push_hdr(msg, msg_type, chan_nr, link_id, transparent);
 +}
 +
 +struct msgb *rsl_rll_simple(uint8_t msg_type, uint8_t chan_nr,
 +                          uint8_t link_id, int transparent)
 +{
 +      struct abis_rsl_rll_hdr *rh;
 +      struct msgb *msg;
 +
 +      msg = msgb_alloc_headroom(RSL_ALLOC_SIZE+RSL_ALLOC_HEADROOM,
 +                                RSL_ALLOC_HEADROOM, "rsl_rll_simple");
 +
 +      if (!msg)
 +              return NULL;
 +
 +      /* put the RSL header */
 +      rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
 +      rsl_init_rll_hdr(rh, msg_type);
 +      if (transparent)
 +              rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
 +      rh->chan_nr = chan_nr;
 +      rh->link_id = link_id;
 +
 +      /* set the l2 header pointer */
 +      msg->l2h = (uint8_t *)rh;
 +
 +      return msg;
 +}
index f5183d5,0000000..9d87b5b
mode 100644,000000..100644
--- /dev/null
@@@ -1,47 -1,0 +1,122 @@@
-         /* test 7-bit coding/decoding */
-       const char *input = "test text";
 +/*
 + * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
++ * (C) 2010 by Nico Golde <nico@ngolde.de>
 + * All Rights Reserved
 + *
 + * 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.,
 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 + *
 + */
 +
 +#include <stdio.h>
 +#include <stdlib.h>
 +#include <string.h>
 +#include <sys/types.h>
 +#include <osmocore/msgb.h>
 +#include <osmocore/gsm_utils.h>
++#include <osmocore/utils.h>
++
++struct test_case {
++      const uint8_t *input;
++      const uint16_t input_length;
++
++      const uint8_t *expected;
++      const uint16_t expected_length;
++};
++
++static const char simple_text[] = "test text";
++static const uint8_t simple_enc[] = {
++      0xf4, 0xf2, 0x9c, 0x0e, 0xa2, 0x97, 0xf1, 0x74
++};
++
++static const char escape_text[] = "!$ a more#^- complicated test@@?_\%! case";
++static const uint8_t escape_enc[] = {
++      0x21, 0x01, 0x28, 0x0c, 0x6a, 0xbf, 0xe5, 0xe5, 0xd1,
++      0x86, 0xd2, 0x02, 0x8d, 0xdf, 0x6d, 0x38, 0x3b, 0x3d,
++      0x0e, 0xd3, 0xcb, 0x64, 0x10, 0xbd, 0x3c, 0xa7, 0x03,
++      0x00, 0xbf, 0x48, 0x29, 0x04, 0x1a, 0x87, 0xe7, 0x65,
++};
++
++static const struct test_case test_encode[] =
++{
++      {
++              .input = simple_text,
++              .expected = simple_enc,
++              .expected_length = sizeof(simple_enc),
++      },
++      {
++              .input = escape_text,
++              .expected = escape_enc,
++              .expected_length = sizeof(escape_enc),
++      },
++};
++
++static const struct test_case test_decode[] =
++{
++      {
++              .input = simple_enc,
++              .input_length = sizeof(simple_enc),
++              .expected = simple_text,
++      },
++      {
++              .input = escape_enc,
++              .input_length = sizeof(escape_enc),
++              .expected = escape_text,
++      },
++};
 +
 +int main(int argc, char** argv)
 +{
 +      printf("SMS testing\n");
 +      struct msgb *msg;
 +      uint8_t *sms;
 +      uint8_t i;
 +
-       length = gsm_7bit_encode(coded, input);
-       gsm_7bit_decode(result, coded, length);
-       if (strcmp(result, input) != 0) {
-               printf("7 Bit coding failed... life sucks\n");
-               printf("Wanted: '%s' got '%s'\n", input, result);
 +      uint8_t length;
 +      uint8_t coded[256];
 +      char result[256];
 +
++      /* test 7-bit encoding */
++      for (i = 0; i < ARRAY_SIZE(test_encode); ++i) {
++              memset(coded, 0x42, sizeof(coded));
++              length = gsm_7bit_encode(coded, test_encode[i].input);
++
++              if (length != test_encode[i].expected_length) {
++                      fprintf(stderr, "Failed to encode case %d. Got %d, expected %d\n",
++                              i, length, test_encode[i].expected_length);
++                      return -1;
++              }
++
++              if (memcmp(coded, test_encode[i].expected, length) != 0) {
++                      fprintf(stderr, "Encoded content does not match for %d\n",
++                              i);
++                      return -1;
++              }
 +      }
++
++      /* test 7-bit decoding */
++      for (i = 0; i < ARRAY_SIZE(test_decode); ++i) {
++              memset(result, 0x42, sizeof(coded));
++              gsm_7bit_decode(result, test_decode[i].input,
++                              test_decode[i].input_length);
++
++              if (strcmp(result, test_decode[i].expected) != 0) {
++                      fprintf(stderr, "Test case %d failed to decode.\n", i);
++                      return -1;
++              }
++      }
++
++      printf("OK\n");
++      return 0;
 +}