Merge commit '1523d7039d9b8c49a2174d93c3673aa8c5a100a9'
authorHarald Welte <laforge@gnumonks.org>
Wed, 4 Aug 2010 09:48:20 +0000 (11:48 +0200)
committerHarald Welte <laforge@gnumonks.org>
Wed, 4 Aug 2010 09:48:20 +0000 (11:48 +0200)
1  2 
src/shared/libosmocore/include/osmocom/vty/logging.h
src/shared/libosmocore/include/osmocore/gsm48_ie.h
src/shared/libosmocore/include/osmocore/logging.h
src/shared/libosmocore/include/osmocore/utils.h
src/shared/libosmocore/src/gsm48_ie.c
src/shared/libosmocore/src/logging.c
src/shared/libosmocore/src/rsl.c
src/shared/libosmocore/src/utils.c

index f8ffbc3,0000000..45d74fd
mode 100644,000000..100644
--- /dev/null
@@@ -1,7 -1,0 +1,9 @@@
 +#ifndef _VTY_LOGGING_H
 +#define _VTY_LOGGING_H
 +
 +#define LOGGING_STR   "Configure log message to this terminal\n"
 +#define FILTER_STR    "Filter log messages\n"
 +
++void logging_vty_add_cmds(void);
++
 +#endif /* _VTY_LOGGING_H */
index 200619a,0000000..fa66159
mode 100644,000000..100644
--- /dev/null
@@@ -1,107 -1,0 +1,117 @@@
 +#ifndef _OSMOCORE_GSM48_IE_H
 +#define _OSMOCORE_GSM48_IE_H
 +
 +#include <stdint.h>
 +#include <string.h>
 +#include <errno.h>
 +
 +#include <osmocore/msgb.h>
 +#include <osmocore/tlv.h>
 +#include <osmocore/mncc.h>
 +#include <osmocore/protocol/gsm_04_08.h>
 +
 +/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
 +int gsm48_decode_bcd_number(char *output, int output_len,
 +                          const uint8_t *bcd_lv, int h_len);
 +
 +/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
 +int gsm48_encode_bcd_number(uint8_t *bcd_lv, uint8_t max_len,
 +                          int h_len, const char *input);
 +/* decode 'bearer capability' */
 +int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
 +                           const uint8_t *lv);
 +/* encode 'bearer capability' */
 +int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
 +                           const struct gsm_mncc_bearer_cap *bcap);
 +/* decode 'call control cap' */
 +int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv);
 +/* encode 'call control cap' */
 +int gsm48_encode_cccap(struct msgb *msg,
 +                      const struct gsm_mncc_cccap *ccap);
 +/* decode 'called party BCD number' */
 +int gsm48_decode_called(struct gsm_mncc_number *called,
 +                       const uint8_t *lv);
 +/* encode 'called party BCD number' */
 +int gsm48_encode_called(struct msgb *msg,
 +                       const struct gsm_mncc_number *called);
 +/* decode callerid of various IEs */
 +int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
 +                       const uint8_t *lv);
 +/* encode callerid of various IEs */
 +int gsm48_encode_callerid(struct msgb *msg, int ie, int max_len,
 +                         const struct gsm_mncc_number *callerid);
 +/* decode 'cause' */
 +int gsm48_decode_cause(struct gsm_mncc_cause *cause,
 +                      const uint8_t *lv);
 +/* encode 'cause' */
 +int gsm48_encode_cause(struct msgb *msg, int lv_only,
 +                      const struct gsm_mncc_cause *cause);
 +/* decode 'calling number' */
 +int gsm48_decode_calling(struct gsm_mncc_number *calling,
 +                       const uint8_t *lv);
 +/* encode 'calling number' */
 +int gsm48_encode_calling(struct msgb *msg, 
 +                        const struct gsm_mncc_number *calling);
 +/* decode 'connected number' */
 +int gsm48_decode_connected(struct gsm_mncc_number *connected,
 +                       const uint8_t *lv);
 +/* encode 'connected number' */
 +int gsm48_encode_connected(struct msgb *msg,
 +                          const struct gsm_mncc_number *connected);
 +/* decode 'redirecting number' */
 +int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
 +                       const uint8_t *lv);
 +/* encode 'redirecting number' */
 +int gsm48_encode_redirecting(struct msgb *msg,
 +                            const struct gsm_mncc_number *redirecting);
 +/* decode 'facility' */
 +int gsm48_decode_facility(struct gsm_mncc_facility *facility,
 +                         const uint8_t *lv);
 +/* encode 'facility' */
 +int gsm48_encode_facility(struct msgb *msg, int lv_only,
 +                         const struct gsm_mncc_facility *facility);
 +/* decode 'notify' */
 +int gsm48_decode_notify(int *notify, const uint8_t *v);
 +/* encode 'notify' */
 +int gsm48_encode_notify(struct msgb *msg, int notify);
 +/* decode 'signal' */
 +int gsm48_decode_signal(int *signal, const uint8_t *v);
 +/* encode 'signal' */
 +int gsm48_encode_signal(struct msgb *msg, int signal);
 +/* decode 'keypad' */
 +int gsm48_decode_keypad(int *keypad, const uint8_t *lv);
 +/* encode 'keypad' */
 +int gsm48_encode_keypad(struct msgb *msg, int keypad);
 +/* decode 'progress' */
 +int gsm48_decode_progress(struct gsm_mncc_progress *progress,
 +                         const uint8_t *lv);
 +/* encode 'progress' */
 +int gsm48_encode_progress(struct msgb *msg, int lv_only,
 +                         const struct gsm_mncc_progress *p);
 +/* decode 'user-user' */
 +int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
 +                         const uint8_t *lv);
 +/* encode 'useruser' */
 +int gsm48_encode_useruser(struct msgb *msg, int lv_only,
 +                         const struct gsm_mncc_useruser *uu);
 +/* decode 'ss version' */
 +int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
 +                          const uint8_t *lv);
 +/* encode 'ss version' */
 +int gsm48_encode_ssversion(struct msgb *msg,
 +                         const struct gsm_mncc_ssversion *ssv);
 +/* decode 'more data' does not require a function, because it has no value */
 +/* encode 'more data' */
 +int gsm48_encode_more(struct msgb *msg);
 +
++/* structure of one frequency */
++struct gsm_sysinfo_freq {
++      /* if the frequency included in the sysinfo */
++      uint8_t mask;
++};
++
++/* decode "Cell Channel Description" (10.5.2.1b) and other frequency lists */
++int gsm48_decode_freq_list(struct gsm_sysinfo_freq *f, uint8_t *cd,
++                         uint8_t len, uint8_t mask, uint8_t frqt);
++
 +#endif
index 2e82959,0000000..7bf2440
mode 100644,000000..100644
--- /dev/null
@@@ -1,135 -1,0 +1,134 @@@
- char *hexdump(const unsigned char *buf, int len);
 +#ifndef _OSMOCORE_LOGGING_H
 +#define _OSMOCORE_LOGGING_H
 +
 +#include <stdio.h>
 +#include <stdint.h>
 +#include <osmocore/linuxlist.h>
 +
 +#define LOG_MAX_CATEGORY      32
 +#define LOG_MAX_CTX           8
 +#define LOG_MAX_FILTERS       8
 +
 +#define DEBUG
 +
 +#ifdef DEBUG
 +#define DEBUGP(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 0, fmt, ## args)
 +#define DEBUGPC(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 1, fmt, ## args)
 +#else
 +#define DEBUGP(xss, fmt, args...)
 +#define DEBUGPC(ss, fmt, args...)
 +#endif
 +
 +#define static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1];
 +
 +void logp(unsigned int subsys, char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
 +
 +/* new logging interface */
 +#define LOGP(ss, level, fmt, args...) \
 +      logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
 +#define LOGPC(ss, level, fmt, args...) \
 +      logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args)
 +
 +/* different levels */
 +#define LOGL_DEBUG    1       /* debugging information */
 +#define LOGL_INFO     3
 +#define LOGL_NOTICE   5       /* abnormal/unexpected condition */
 +#define LOGL_ERROR    7       /* error condition, requires user action */
 +#define LOGL_FATAL    8       /* fatal, program aborted */
 +
 +#define LOG_FILTER_ALL        0x0001
 +
 +struct log_category {
 +      uint8_t loglevel;
 +      uint8_t enabled;
 +};
 +
 +struct log_info_cat {
 +      const char *name;
 +      const char *color;
 +      const char *description;
 +      uint8_t loglevel;
 +      uint8_t enabled;
 +};
 +
 +/* log context information, passed to filter */
 +struct log_context {
 +      void *ctx[LOG_MAX_CTX+1];
 +};
 +
 +struct log_target;
 +
 +typedef int log_filter(const struct log_context *ctx,
 +                     struct log_target *target);
 +
 +struct log_info {
 +      /* filter callback function */
 +      log_filter *filter_fn;
 +
 +      /* per-category information */
 +      const struct log_info_cat *cat;
 +      unsigned int num_cat;
 +};
 +
 +struct log_target {
 +        struct llist_head entry;
 +
 +      int filter_map;
 +      void *filter_data[LOG_MAX_FILTERS+1];
 +
 +      struct log_category categories[LOG_MAX_CATEGORY+1];
 +      uint8_t loglevel;
 +      int use_color:1;
 +      int print_timestamp:1;
 +
 +      union {
 +              struct {
 +                      FILE *out;
 +              } tgt_stdout;
 +
 +              struct {
 +                      int priority;
 +              } tgt_syslog;
 +
 +              struct {
 +                      void *vty;
 +              } tgt_vty;
 +      };
 +
 +        void (*output) (struct log_target *target, const char *string);
 +};
 +
 +/* use the above macros */
 +void logp2(unsigned int subsys, unsigned int level, char *file,
 +         int line, int cont, const char *format, ...)
 +                              __attribute__ ((format (printf, 6, 7)));
 +void log_init(const struct log_info *cat);
 +
 +/* context management */
 +void log_reset_context(void);
 +int log_set_context(uint8_t ctx, void *value);
 +
 +/* filter on the targets */
 +void log_set_all_filter(struct log_target *target, int);
 +
 +void log_set_use_color(struct log_target *target, int);
 +void log_set_print_timestamp(struct log_target *target, int);
 +void log_set_log_level(struct log_target *target, int log_level);
 +void log_parse_category_mask(struct log_target *target, const char* mask);
 +int log_parse_level(const char *lvl);
 +const char *log_level_str(unsigned int lvl);
 +int log_parse_category(const char *category);
 +void log_set_category_filter(struct log_target *target, int category,
 +                             int enable, int level);
 +
 +/* management of the targets */
 +struct log_target *log_target_create(void);
 +struct log_target *log_target_create_stderr(void);
 +void log_add_target(struct log_target *target);
 +void log_del_target(struct log_target *target);
 +
 +/* Gernerate command argument strings for VTY use */
 +const char *log_vty_category_string(struct log_info *info);
 +const char *log_vty_level_string(struct log_info *info);
 +
 +#endif /* _OSMOCORE_LOGGING_H */
index 51c6f03,0000000..3574f7f
mode 100644,000000..100644
--- /dev/null
@@@ -1,20 -1,0 +1,24 @@@
 +#ifndef OSMOCORE_UTIL_H
 +#define OSMOCORE_UTIL_H
 +
 +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 +
 +#include <stdint.h>
 +
 +struct value_string {
 +      unsigned int value;
 +      const char *str;
 +};
 +
 +const char *get_value_string(const struct value_string *vs, uint32_t val);
 +int get_string_value(const struct value_string *vs, const char *str);
 +
 +char bcd2char(uint8_t bcd);
 +/* only works for numbers in ascci */
 +uint8_t char2bcd(char c);
 +
++int hexparse(const char *str, uint8_t *b, int max_len);
++char *hexdump(const unsigned char *buf, int len);
++char *hexdump_nospc(const unsigned char *buf, int len);
++
 +#endif
index 3c2a1f7,0000000..71910c6
mode 100644,000000..100644
--- /dev/null
@@@ -1,659 -1,0 +1,1094 @@@
 +/* GSM Mobile Radio Interface Layer 3 messages
 + * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
 +
 +/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
 + * (C) 2009-2010 by Andreas Eversberg
 + *
 + * 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 <string.h>
 +#include <errno.h>
 +
 +#include <osmocore/utils.h>
 +#include <osmocore/msgb.h>
 +#include <osmocore/tlv.h>
 +#include <osmocore/mncc.h>
 +#include <osmocore/protocol/gsm_04_08.h>
++#include <osmocore/gsm48_ie.h>
 +
 +static const char bcd_num_digits[] = {
 +      '0', '1', '2', '3', '4', '5', '6', '7',
 +      '8', '9', '*', '#', 'a', 'b', 'c', '\0'
 +};
 +
 +/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
 +int gsm48_decode_bcd_number(char *output, int output_len,
 +                          const uint8_t *bcd_lv, int h_len)
 +{
 +      uint8_t in_len = bcd_lv[0];
 +      int i;
 +
 +      for (i = 1 + h_len; i <= in_len; i++) {
 +              /* lower nibble */
 +              output_len--;
 +              if (output_len <= 1)
 +                      break;
 +              *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
 +
 +              /* higher nibble */
 +              output_len--;
 +              if (output_len <= 1)
 +                      break;
 +              *output++ = bcd_num_digits[bcd_lv[i] >> 4];
 +      }
 +      if (output_len >= 1)
 +              *output++ = '\0';
 +
 +      return 0;
 +}
 +
 +/* convert a single ASCII character to call-control BCD */
 +static int asc_to_bcd(const char asc)
 +{
 +      int i;
 +
 +      for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
 +              if (bcd_num_digits[i] == asc)
 +                      return i;
 +      }
 +      return -EINVAL;
 +}
 +
 +/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
 +int gsm48_encode_bcd_number(uint8_t *bcd_lv, uint8_t max_len,
 +                    int h_len, const char *input)
 +{
 +      int in_len = strlen(input);
 +      int i;
 +      uint8_t *bcd_cur = bcd_lv + 1 + h_len;
 +
 +      /* two digits per byte, plus type byte */
 +      bcd_lv[0] = in_len/2 + h_len;
 +      if (in_len % 2)
 +              bcd_lv[0]++;
 +
 +      if (bcd_lv[0] > max_len)
 +              return -EIO;
 +
 +      for (i = 0; i < in_len; i++) {
 +              int rc = asc_to_bcd(input[i]);
 +              if (rc < 0)
 +                      return rc;
 +              if (i % 2 == 0)
 +                      *bcd_cur = rc;
 +              else
 +                      *bcd_cur++ |= (rc << 4);
 +      }
 +      /* append padding nibble in case of odd length */
 +      if (i % 2)
 +              *bcd_cur++ |= 0xf0;
 +
 +      /* return how many bytes we used */
 +      return (bcd_cur - bcd_lv);
 +}
 +
 +/* decode 'bearer capability' */
 +int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
 +                           const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +      int i, s;
 +
 +      if (in_len < 1)
 +              return -EINVAL;
 +
 +      bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
 +
 +      /* octet 3 */
 +      bcap->transfer = lv[1] & 0x07;
 +      bcap->mode = (lv[1] & 0x08) >> 3;
 +      bcap->coding = (lv[1] & 0x10) >> 4;
 +      bcap->radio = (lv[1] & 0x60) >> 5;
 +
 +      if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
 +              i = 1;
 +              s = 0;
 +              while(!(lv[i] & 0x80)) {
 +                      i++; /* octet 3a etc */
 +                      if (in_len < i)
 +                              return 0;
 +                      bcap->speech_ver[s++] = lv[i] & 0x0f;
 +                      bcap->speech_ver[s] = -1; /* end of list */
 +                      if (i == 2) /* octet 3a */
 +                              bcap->speech_ctm = (lv[i] & 0x20) >> 5;
 +                      if (s == 7) /* maximum speech versions + end of list */
 +                              return 0;
 +              }
 +      } else {
 +              i = 1;
 +              while (!(lv[i] & 0x80)) {
 +                      i++; /* octet 3a etc */
 +                      if (in_len < i)
 +                              return 0;
 +                      /* ignore them */
 +              }
 +              /* FIXME: implement OCTET 4+ parsing */
 +      }
 +
 +      return 0;
 +}
 +
 +/* encode 'bearer capability' */
 +int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
 +                           const struct gsm_mncc_bearer_cap *bcap)
 +{
 +      uint8_t lv[32 + 1];
 +      int i = 1, s;
 +
 +      lv[1] = bcap->transfer;
 +      lv[1] |= bcap->mode << 3;
 +      lv[1] |= bcap->coding << 4;
 +      lv[1] |= bcap->radio << 5;
 +
 +      if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
 +              for (s = 0; bcap->speech_ver[s] >= 0; s++) {
 +                      i++; /* octet 3a etc */
 +                      lv[i] = bcap->speech_ver[s];
 +                      if (i == 2) /* octet 3a */
 +                              lv[i] |= bcap->speech_ctm << 5;
 +              }
 +              lv[i] |= 0x80; /* last IE of octet 3 etc */
 +      } else {
 +              /* FIXME: implement OCTET 4+ encoding */
 +      }
 +
 +      lv[0] = i;
 +      if (lv_only)
 +              msgb_lv_put(msg, lv[0], lv+1);
 +      else
 +              msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
 +
 +      return 0;
 +}
 +
 +/* decode 'call control cap' */
 +int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +
 +      if (in_len < 1)
 +              return -EINVAL;
 +
 +      /* octet 3 */
 +      ccap->dtmf = lv[1] & 0x01;
 +      ccap->pcp = (lv[1] & 0x02) >> 1;
 +
 +      return 0;
 +}
 +
 +/* encode 'call control cap' */
 +int gsm48_encode_cccap(struct msgb *msg,
 +                      const struct gsm_mncc_cccap *ccap)
 +{
 +      uint8_t lv[2];
 +
 +      lv[0] = 1;
 +      lv[1] = 0;
 +      if (ccap->dtmf)
 +              lv [1] |= 0x01;
 +      if (ccap->pcp)
 +              lv [1] |= 0x02;
 +
 +      msgb_tlv_put(msg, GSM48_IE_CC_CAP, lv[0], lv+1);
 +
 +      return 0;
 +}
 +
 +/* decode 'called party BCD number' */
 +int gsm48_decode_called(struct gsm_mncc_number *called,
 +                       const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +
 +      if (in_len < 1)
 +              return -EINVAL;
 +
 +      /* octet 3 */
 +      called->plan = lv[1] & 0x0f;
 +      called->type = (lv[1] & 0x70) >> 4;
 +
 +      /* octet 4..N */
 +      gsm48_decode_bcd_number(called->number, sizeof(called->number), lv, 1);
 +
 +      return 0;
 +}
 +
 +/* encode 'called party BCD number' */
 +int gsm48_encode_called(struct msgb *msg,
 +                       const struct gsm_mncc_number *called)
 +{
 +      uint8_t lv[18];
 +      int ret;
 +
 +      /* octet 3 */
 +      lv[1] = called->plan;
 +      lv[1] |= called->type << 4;
 +
 +      /* octet 4..N, octet 2 */
 +      ret = gsm48_encode_bcd_number(lv, sizeof(lv), 1, called->number);
 +      if (ret < 0)
 +              return ret;
 +
 +      msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
 +
 +      return 0;
 +}
 +
 +/* decode callerid of various IEs */
 +int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
 +                       const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +      int i = 1;
 +
 +      if (in_len < 1)
 +              return -EINVAL;
 +
 +      /* octet 3 */
 +      callerid->plan = lv[1] & 0x0f;
 +      callerid->type = (lv[1] & 0x70) >> 4;
 +
 +      /* octet 3a */
 +      if (!(lv[1] & 0x80)) {
 +              callerid->screen = lv[2] & 0x03;
 +              callerid->present = (lv[2] & 0x60) >> 5;
 +              i = 2;
 +      }
 +
 +      /* octet 4..N */
 +      gsm48_decode_bcd_number(callerid->number, sizeof(callerid->number), lv, i);
 +
 +      return 0;
 +}
 +
 +/* encode callerid of various IEs */
 +int gsm48_encode_callerid(struct msgb *msg, int ie, int max_len,
 +                         const struct gsm_mncc_number *callerid)
 +{
 +      uint8_t lv[max_len - 1];
 +      int h_len = 1;
 +      int ret;
 +
 +      /* octet 3 */
 +      lv[1] = callerid->plan;
 +      lv[1] |= callerid->type << 4;
 +
 +      if (callerid->present || callerid->screen) {
 +              /* octet 3a */
 +              lv[2] = callerid->screen;
 +              lv[2] |= callerid->present << 5;
 +              lv[2] |= 0x80;
 +              h_len++;
 +      } else
 +              lv[1] |= 0x80;
 +
 +      /* octet 4..N, octet 2 */
 +      ret = gsm48_encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
 +      if (ret < 0)
 +              return ret;
 +
 +      msgb_tlv_put(msg, ie, lv[0], lv+1);
 +
 +      return 0;
 +}
 +
 +/* decode 'cause' */
 +int gsm48_decode_cause(struct gsm_mncc_cause *cause,
 +                      const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +      int i;
 +
 +      if (in_len < 2)
 +              return -EINVAL;
 +
 +      cause->diag_len = 0;
 +
 +      /* octet 3 */
 +      cause->location = lv[1] & 0x0f;
 +      cause->coding = (lv[1] & 0x60) >> 5;
 +
 +      i = 1;
 +      if (!(lv[i] & 0x80)) {
 +              i++; /* octet 3a */
 +              if (in_len < i+1)
 +                      return 0;
 +              cause->rec = 1;
 +              cause->rec_val = lv[i] & 0x7f;
 +      }
 +      i++;
 +
 +      /* octet 4 */
 +      cause->value = lv[i] & 0x7f;
 +      i++;
 +
 +      if (in_len < i) /* no diag */
 +              return 0;
 +
 +      if (in_len - (i-1) > 32) /* maximum 32 octets */
 +              return 0;
 +
 +      /* octet 5-N */
 +      memcpy(cause->diag, lv + i, in_len - (i-1));
 +      cause->diag_len = in_len - (i-1);
 +
 +      return 0;
 +}
 +
 +/* encode 'cause' */
 +int gsm48_encode_cause(struct msgb *msg, int lv_only,
 +                      const struct gsm_mncc_cause *cause)
 +{
 +      uint8_t lv[32+4];
 +      int i;
 +
 +      if (cause->diag_len > 32)
 +              return -EINVAL;
 +
 +      /* octet 3 */
 +      lv[1] = cause->location;
 +      lv[1] |= cause->coding << 5;
 +
 +      i = 1;
 +      if (cause->rec) {
 +              i++; /* octet 3a */
 +              lv[i] = cause->rec_val;
 +      }
 +      lv[i] |= 0x80; /* end of octet 3 */
 +
 +      /* octet 4 */
 +      i++;
 +      lv[i] = 0x80 | cause->value;
 +
 +      /* octet 5-N */
 +      if (cause->diag_len) {
 +              memcpy(lv + i, cause->diag, cause->diag_len);
 +              i += cause->diag_len;
 +      }
 +
 +      lv[0] = i;
 +      if (lv_only)
 +              msgb_lv_put(msg, lv[0], lv+1);
 +      else
 +              msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
 +
 +      return 0;
 +}
 +
 +/* decode 'calling number' */
 +int gsm48_decode_calling(struct gsm_mncc_number *calling,
 +                       const uint8_t *lv)
 +{
 +      return gsm48_decode_callerid(calling, lv);
 +}
 +
 +/* encode 'calling number' */
 +int gsm48_encode_calling(struct msgb *msg, 
 +                        const struct gsm_mncc_number *calling)
 +{
 +      return gsm48_encode_callerid(msg, GSM48_IE_CALLING_BCD, 14, calling);
 +}
 +
 +/* decode 'connected number' */
 +int gsm48_decode_connected(struct gsm_mncc_number *connected,
 +                       const uint8_t *lv)
 +{
 +      return gsm48_decode_callerid(connected, lv);
 +}
 +
 +/* encode 'connected number' */
 +int gsm48_encode_connected(struct msgb *msg,
 +                          const struct gsm_mncc_number *connected)
 +{
 +      return gsm48_encode_callerid(msg, GSM48_IE_CONN_BCD, 14, connected);
 +}
 +
 +/* decode 'redirecting number' */
 +int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
 +                       const uint8_t *lv)
 +{
 +      return gsm48_decode_callerid(redirecting, lv);
 +}
 +
 +/* encode 'redirecting number' */
 +int gsm48_encode_redirecting(struct msgb *msg,
 +                            const struct gsm_mncc_number *redirecting)
 +{
 +      return gsm48_encode_callerid(msg, GSM48_IE_REDIR_BCD, 19, redirecting);
 +}
 +
 +/* decode 'facility' */
 +int gsm48_decode_facility(struct gsm_mncc_facility *facility,
 +                         const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +
 +      if (in_len < 1)
 +              return -EINVAL;
 +
 +      if (in_len > sizeof(facility->info))
 +              return -EINVAL;
 +
 +      memcpy(facility->info, lv+1, in_len);
 +      facility->len = in_len;
 +
 +      return 0;
 +}
 +
 +/* encode 'facility' */
 +int gsm48_encode_facility(struct msgb *msg, int lv_only,
 +                         const struct gsm_mncc_facility *facility)
 +{
 +      uint8_t lv[GSM_MAX_FACILITY + 1];
 +
 +      if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
 +              return -EINVAL;
 +
 +      memcpy(lv+1, facility->info, facility->len);
 +      lv[0] = facility->len;
 +      if (lv_only)
 +              msgb_lv_put(msg, lv[0], lv+1);
 +      else
 +              msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
 +
 +      return 0;
 +}
 +
 +/* decode 'notify' */
 +int gsm48_decode_notify(int *notify, const uint8_t *v)
 +{
 +      *notify = v[0] & 0x7f;
 +
 +      return 0;
 +}
 +
 +/* encode 'notify' */
 +int gsm48_encode_notify(struct msgb *msg, int notify)
 +{
 +      msgb_v_put(msg, notify | 0x80);
 +
 +      return 0;
 +}
 +
 +/* decode 'signal' */
 +int gsm48_decode_signal(int *signal, const uint8_t *v)
 +{
 +      *signal = v[0];
 +
 +      return 0;
 +}
 +
 +/* encode 'signal' */
 +int gsm48_encode_signal(struct msgb *msg, int signal)
 +{
 +      msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
 +
 +      return 0;
 +}
 +
 +/* decode 'keypad' */
 +int gsm48_decode_keypad(int *keypad, const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +
 +      if (in_len < 1)
 +              return -EINVAL;
 +
 +      *keypad = lv[1] & 0x7f;
 +
 +      return 0;
 +}
 +
 +/* encode 'keypad' */
 +int gsm48_encode_keypad(struct msgb *msg, int keypad)
 +{
 +      msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
 +
 +      return 0;
 +}
 +
 +/* decode 'progress' */
 +int gsm48_decode_progress(struct gsm_mncc_progress *progress,
 +                         const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +
 +      if (in_len < 2)
 +              return -EINVAL;
 +
 +      progress->coding = (lv[1] & 0x60) >> 5;
 +      progress->location = lv[1] & 0x0f;
 +      progress->descr = lv[2] & 0x7f;
 +
 +      return 0;
 +}
 +
 +/* encode 'progress' */
 +int gsm48_encode_progress(struct msgb *msg, int lv_only,
 +                         const struct gsm_mncc_progress *p)
 +{
 +      uint8_t lv[3];
 +
 +      lv[0] = 2;
 +      lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
 +      lv[2] = 0x80 | (p->descr & 0x7f);
 +      if (lv_only)
 +              msgb_lv_put(msg, lv[0], lv+1);
 +      else
 +              msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
 +
 +      return 0;
 +}
 +
 +/* decode 'user-user' */
 +int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
 +                         const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +      char *info = uu->info;
 +      int info_len = sizeof(uu->info);
 +      int i;
 +
 +      if (in_len < 1)
 +              return -EINVAL;
 +
 +      uu->proto = lv[1];
 +
 +      for (i = 2; i <= in_len; i++) {
 +              info_len--;
 +              if (info_len <= 1)
 +                      break;
 +              *info++ = lv[i];
 +      }
 +      if (info_len >= 1)
 +              *info++ = '\0';
 +
 +      return 0;
 +}
 +
 +/* encode 'useruser' */
 +int gsm48_encode_useruser(struct msgb *msg, int lv_only,
 +                         const struct gsm_mncc_useruser *uu)
 +{
 +      uint8_t lv[GSM_MAX_USERUSER + 2];
 +
 +      if (strlen(uu->info) > GSM_MAX_USERUSER)
 +              return -EINVAL;
 +
 +      lv[0] = 1 + strlen(uu->info);
 +      lv[1] = uu->proto;
 +      memcpy(lv + 2, uu->info, strlen(uu->info));
 +      if (lv_only)
 +              msgb_lv_put(msg, lv[0], lv+1);
 +      else
 +              msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
 +
 +      return 0;
 +}
 +
 +/* decode 'ss version' */
 +int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
 +                          const uint8_t *lv)
 +{
 +      uint8_t in_len = lv[0];
 +
 +      if (in_len < 1 || in_len < sizeof(ssv->info))
 +              return -EINVAL;
 +
 +      memcpy(ssv->info, lv + 1, in_len);
 +      ssv->len = in_len;
 +
 +      return 0;
 +}
 +
 +/* encode 'ss version' */
 +int gsm48_encode_ssversion(struct msgb *msg,
 +                         const struct gsm_mncc_ssversion *ssv)
 +{
 +      uint8_t lv[GSM_MAX_SSVERSION + 1];
 +
 +      if (ssv->len > GSM_MAX_SSVERSION)
 +              return -EINVAL;
 +
 +      lv[0] = ssv->len;
 +      memcpy(lv + 1, ssv->info, ssv->len);
 +      msgb_tlv_put(msg, GSM48_IE_SS_VERS, lv[0], lv+1);
 +
 +      return 0;
 +}
 +
 +/* decode 'more data' does not require a function, because it has no value */
 +
 +/* encode 'more data' */
 +int gsm48_encode_more(struct msgb *msg)
 +{
 +      uint8_t *ie;
 +
 +      ie = msgb_put(msg, 1);
 +      ie[0] = GSM48_IE_MORE_DATA;
 +
 +      return 0;
 +}
 +
++/* decode "Cell Channel Description" (10.5.2.1b) and other frequency lists */
++int gsm48_decode_freq_list(struct gsm_sysinfo_freq *f, uint8_t *cd,
++                         uint8_t len, uint8_t mask, uint8_t frqt)
++{
++      int i;
++
++      /* NOTES:
++       *
++       * The Range format uses "SMOD" computation.
++       * e.g. "n SMOD m" equals "((n - 1) % m) + 1"
++       * A cascade of multiple SMOD computations is simpified:
++       * "(n SMOD m) SMOD o" equals "(((n - 1) % m) % o) + 1"
++       *
++       * The Range format uses 16 octets of data in SYSTEM INFORMATION.
++       * When used in dedicated messages, the length can be less.
++       * In this case the ranges are decoded for all frequencies that
++       * fit in the block of given length.
++       */
++
++      /* tabula rasa */
++      for (i = 0; i < 1024; i++)
++              f[i].mask &= ~frqt;
++
++      /* 00..XXX. */
++      if ((cd[0] & 0xc0 & mask) == 0x00) {
++              /* Bit map 0 format */
++              if (len < 16)
++                      return -EINVAL;
++              for (i = 1; i <= 124; i++)
++                      if ((cd[15 - ((i-1) >> 3)] & (1 << ((i-1) & 7))))
++                              f[i].mask |= frqt;
++
++              return 0;
++      }
++
++      /* 10..0XX. */
++      if ((cd[0] & 0xc8 & mask) == 0x80) {
++              /* Range 1024 format */
++              uint16_t w[17]; /* 1..16 */
++              struct gsm48_range_1024 *r = (struct gsm48_range_1024 *)cd;
++
++              if (len < 2)
++                      return -EINVAL;
++              memset(w, 0, sizeof(w));
++              if (r->f0)
++                      f[0].mask |= frqt;
++              w[1] = (r->w1_hi << 8) | r->w1_lo;
++              if (len >= 4)
++                      w[2] = (r->w2_hi << 1) | r->w2_lo;
++              if (len >= 5)
++                      w[3] = (r->w3_hi << 2) | r->w3_lo;
++              if (len >= 6)
++                      w[4] = (r->w4_hi << 2) | r->w4_lo;
++              if (len >= 7)
++                      w[5] = (r->w5_hi << 2) | r->w5_lo;
++              if (len >= 8)
++                      w[6] = (r->w6_hi << 2) | r->w6_lo;
++              if (len >= 9)
++                      w[7] = (r->w7_hi << 2) | r->w7_lo;
++              if (len >= 10)
++                      w[8] = (r->w8_hi << 1) | r->w8_lo;
++              if (len >= 10)
++                      w[9] = r->w9;
++              if (len >= 11)
++                      w[10] = r->w10;
++              if (len >= 12)
++                      w[11] = (r->w11_hi << 6) | r->w11_lo;
++              if (len >= 13)
++                      w[12] = (r->w12_hi << 5) | r->w12_lo;
++              if (len >= 14)
++                      w[13] = (r->w13_hi << 4) | r->w13_lo;
++              if (len >= 15)
++                      w[14] = (r->w14_hi << 3) | r->w14_lo;
++              if (len >= 16)
++                      w[15] = (r->w15_hi << 2) | r->w15_lo;
++              if (len >= 16)
++                      w[16] = r->w16;
++              if (w[1])
++                      f[w[1]].mask |= frqt;
++              if (w[2])
++                      f[((w[1] - 512 + w[2] - 1) % 1023) + 1].mask |= frqt;
++              if (w[3])
++                      f[((w[1]       + w[3] - 1) % 1023) + 1].mask |= frqt;
++              if (w[4])
++                      f[((w[1] - 512 + ((w[2] - 256 + w[4] - 1) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[5])
++                      f[((w[1]       + ((w[3] - 256 - w[5] - 1) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[6])
++                      f[((w[1] - 512 + ((w[2]       + w[6] - 1) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[7])
++                      f[((w[1]       + ((w[3]       + w[7] - 1) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[8])
++                      f[((w[1] - 512 + ((w[2] - 256 + ((w[4] - 128 + w[8] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[9])
++                      f[((w[1]       + ((w[3] - 256 + ((w[5] - 128 + w[9] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[10])
++                      f[((w[1] - 512 + ((w[2]       + ((w[6] - 128 + w[10] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[11])
++                      f[((w[1]       + ((w[3]       + ((w[7] - 128 + w[11] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[12])
++                      f[((w[1] - 512 + ((w[2] - 256 + ((w[4]       + w[12] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[13])
++                      f[((w[1]       + ((w[3] - 256 + ((w[5]       + w[13] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[14])
++                      f[((w[1] - 512 + ((w[2]       + ((w[6]       + w[14] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[15])
++                      f[((w[1]       + ((w[3]       + ((w[7]       + w[15] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
++              if (w[16])
++                      f[((w[1] - 512 + ((w[2] - 256 + ((w[4] - 128 + ((w[8] - 64 + w[16] - 1) % 127)) % 255)) % 511)) % 1023) + 1].mask |= frqt;
++
++              return 0;
++      }
++      /* 10..100. */
++      if ((cd[0] & 0xce & mask) == 0x88) {
++              /* Range 512 format */
++              uint16_t w[18]; /* 1..17 */
++              struct gsm48_range_512 *r = (struct gsm48_range_512 *)cd;
++
++              if (len < 4)
++                      return -EINVAL;
++              memset(w, 0, sizeof(w));
++              w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
++              w[1] = (r->w1_hi << 2) | r->w1_lo;
++              if (len >= 5)
++                      w[2] = (r->w2_hi << 2) | r->w2_lo;
++              if (len >= 6)
++                      w[3] = (r->w3_hi << 2) | r->w3_lo;
++              if (len >= 7)
++                      w[4] = (r->w4_hi << 1) | r->w4_lo;
++              if (len >= 7)
++                      w[5] = r->w5;
++              if (len >= 8)
++                      w[6] = r->w6;
++              if (len >= 9)
++                      w[7] = (r->w7_hi << 6) | r->w7_lo;
++              if (len >= 10)
++                      w[8] = (r->w8_hi << 4) | r->w8_lo;
++              if (len >= 11)
++                      w[9] = (r->w9_hi << 2) | r->w9_lo;
++              if (len >= 11)
++                      w[10] = r->w10;
++              if (len >= 12)
++                      w[11] = r->w11;
++              if (len >= 13)
++                      w[12] = (r->w12_hi << 4) | r->w12_lo;
++              if (len >= 14)
++                      w[13] = (r->w13_hi << 2) | r->w13_lo;
++              if (len >= 14)
++                      w[14] = r->w14;
++              if (len >= 15)
++                      w[15] = r->w15;
++              if (len >= 16)
++                      w[16] = (r->w16_hi << 3) | r->w16_lo;
++              if (len >= 16)
++                      w[17] = r->w17;
++              f[w[0]].mask |= frqt;
++              if (w[1])
++                      f[(w[0] + w[1]) % 1024].mask |= frqt;
++              if (w[2])
++                      f[(w[0] + ((w[1] - 256 + w[2] - 1) % 511) + 1) % 1024].mask |= frqt;
++              if (w[3])
++                      f[(w[0] + ((w[1]       + w[3] - 1) % 511) + 1) % 1024].mask |= frqt;
++              if (w[4])
++                      f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + w[4] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[5])
++                      f[(w[0] + ((w[1]       + ((w[3] - 128 + w[5] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[6])
++                      f[(w[0] + ((w[1] - 256 + ((w[2]       + w[6] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[7])
++                      f[(w[0] + ((w[1]       + ((w[3]       + w[7] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[8])
++                      f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4] - 64 + w[8] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[9])
++                      f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5] - 64 + w[9] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[10])
++                      f[(w[0] + ((w[1] - 256 + ((w[2]       + ((w[6] - 64 + w[10] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[11])
++                      f[(w[0] + ((w[1]       + ((w[3]       + ((w[7] - 64 + w[11] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[12])
++                      f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4]      + w[12] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[13])
++                      f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5]      + w[13] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[14])
++                      f[(w[0] + ((w[1] - 256 + ((w[2]       + ((w[6]      + w[14] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[15])
++                      f[(w[0] + ((w[1]       + ((w[3]       + ((w[7]      + w[15] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[16])
++                      f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4] - 64 + ((w[8] - 32 + w[16] - 1) % 63)) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++              if (w[17])
++                      f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5] - 64 + ((w[9] - 32 + w[17] - 1) % 63)) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
++
++              return 0;
++      }
++      /* 10..101. */
++      if ((cd[0] & 0xce & mask) == 0x8a) {
++              /* Range 256 format */
++              uint16_t w[22]; /* 1..21 */
++              struct gsm48_range_256 *r = (struct gsm48_range_256 *)cd;
++
++              if (len < 4)
++                      return -EINVAL;
++              memset(w, 0, sizeof(w));
++              w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
++              w[1] = (r->w1_hi << 1) | r->w1_lo;
++              if (len >= 4)
++                      w[2] = r->w2;
++              if (len >= 5)
++                      w[3] = r->w3;
++              if (len >= 6)
++                      w[4] = (r->w4_hi << 5) | r->w4_lo;
++              if (len >= 7)
++                      w[5] = (r->w5_hi << 3) | r->w5_lo;
++              if (len >= 8)
++                      w[6] = (r->w6_hi << 1) | r->w6_lo;
++              if (len >= 8)
++                      w[7] = r->w7;
++              if (len >= 9)
++                      w[8] = (r->w8_hi << 4) | r->w8_lo;
++              if (len >= 10)
++                      w[9] = (r->w9_hi << 1) | r->w9_lo;
++              if (len >= 10)
++                      w[10] = r->w10;
++              if (len >= 11)
++                      w[11] = (r->w11_hi << 3) | r->w11_lo;
++              if (len >= 11)
++                      w[12] = r->w12;
++              if (len >= 12)
++                      w[13] = r->w13;
++              if (len >= 13)
++                      w[14] = r->w15;
++              if (len >= 13)
++                      w[15] = (r->w14_hi << 2) | r->w14_lo;
++              if (len >= 14)
++                      w[16] = (r->w16_hi << 3) | r->w16_lo;
++              if (len >= 14)
++                      w[17] = r->w17;
++              if (len >= 15)
++                      w[18] = r->w19;
++              if (len >= 15)
++                      w[19] = (r->w18_hi << 3) | r->w18_lo;
++              if (len >= 16)
++                      w[20] = (r->w20_hi << 3) | r->w20_lo;
++              if (len >= 16)
++                      w[21] = r->w21;
++              f[w[0]].mask |= frqt;
++              if (w[1])
++                      f[(w[0] + w[1]) % 1024].mask |= frqt;
++              if (w[2])
++                      f[(w[0] + ((w[1] - 128 + w[2] - 1) % 255) + 1) % 1024].mask |= frqt;
++              if (w[3])
++                      f[(w[0] + ((w[1]       + w[3] - 1) % 255) + 1) % 1024].mask |= frqt;
++              if (w[4])
++                      f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + w[4] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[5])
++                      f[(w[0] + ((w[1]       + ((w[3] - 64 + w[5] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[6])
++                      f[(w[0] + ((w[1] - 128 + ((w[2]      + w[6] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[7])
++                      f[(w[0] + ((w[1]       + ((w[3]      + w[7] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[8])
++                      f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4] - 32 + w[8] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[9])
++                      f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5] - 32 + w[9] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[10])
++                      f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6] - 32 + w[10] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[11])
++                      f[(w[0] + ((w[1]       + ((w[3]      + ((w[7] - 32 + w[11] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[12])
++                      f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4]      + w[12] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[13])
++                      f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5]      + w[13] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[14])
++                      f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6]      + w[14] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[15])
++                      f[(w[0] + ((w[1]       + ((w[3]      + ((w[7]      + w[15] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[16])
++                      f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4] - 32 + ((w[8] - 16 + w[16] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[17])
++                      f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5] - 32 + ((w[9] - 16 + w[17] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[18])
++                      f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6] - 32 + ((w[10] - 16 + w[18] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[19])
++                      f[(w[0] + ((w[1]       + ((w[3]      + ((w[7] - 32 + ((w[11] - 16 + w[19] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[20])
++                      f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4]      + ((w[12] - 16 + w[20] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++              if (w[21])
++                      f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5]      + ((w[13] - 16 + w[21] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
++
++              return 0;
++      }
++      /* 10..110. */
++      if ((cd[0] & 0xce & mask) == 0x8c) {
++              /* Range 128 format */
++              uint16_t w[29]; /* 1..28 */
++              struct gsm48_range_128 *r = (struct gsm48_range_128 *)cd;
++
++              if (len < 3)
++                      return -EINVAL;
++              memset(w, 0, sizeof(w));
++              w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
++              w[1] = r->w1;
++              if (len >= 4)
++                      w[2] = r->w2;
++              if (len >= 5)
++                      w[3] = (r->w3_hi << 4) | r->w3_lo;
++              if (len >= 6)
++                      w[4] = (r->w4_hi << 1) | r->w4_lo;
++              if (len >= 6)
++                      w[5] = r->w5;
++              if (len >= 7)
++                      w[6] = (r->w6_hi << 3) | r->w6_lo;
++              if (len >= 7)
++                      w[7] = r->w7;
++              if (len >= 8)
++                      w[8] = r->w8;
++              if (len >= 8)
++                      w[9] = r->w9;
++              if (len >= 9)
++                      w[10] = r->w10;
++              if (len >= 9)
++                      w[11] = r->w11;
++              if (len >= 10)
++                      w[12] = r->w12;
++              if (len >= 10)
++                      w[13] = r->w13;
++              if (len >= 11)
++                      w[14] = r->w14;
++              if (len >= 11)
++                      w[15] = r->w15;
++              if (len >= 12)
++                      w[16] = r->w16;
++              if (len >= 12)
++                      w[17] = r->w17;
++              if (len >= 13)
++                      w[18] = (r->w18_hi << 1) | r->w18_lo;
++              if (len >= 13)
++                      w[19] = r->w19;
++              if (len >= 13)
++                      w[20] = r->w20;
++              if (len >= 14)
++                      w[21] = (r->w21_hi << 2) | r->w21_lo;
++              if (len >= 14)
++                      w[22] = r->w22;
++              if (len >= 14)
++                      w[23] = r->w23;
++              if (len >= 15)
++                      w[24] = r->w24;
++              if (len >= 15)
++                      w[25] = r->w25;
++              if (len >= 16)
++                      w[26] = (r->w26_hi << 1) | r->w26_lo;
++              if (len >= 16)
++                      w[27] = r->w27;
++              if (len >= 16)
++                      w[28] = r->w28;
++              f[w[0]].mask |= frqt;
++              if (w[1])
++                      f[(w[0] + w[1]) % 1024].mask |= frqt;
++              if (w[2])
++                      f[(w[0] + ((w[1] - 64 + w[2] - 1) % 127) + 1) % 1024].mask |= frqt;
++              if (w[3])
++                      f[(w[0] + ((w[1]      + w[3] - 1) % 127) + 1) % 1024].mask |= frqt;
++              if (w[4])
++                      f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + w[4] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[5])
++                      f[(w[0] + ((w[1]      + ((w[3] - 32 + w[5] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[6])
++                      f[(w[0] + ((w[1] - 64 + ((w[2]      + w[6] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[7])
++                      f[(w[0] + ((w[1]      + ((w[3]      + w[7] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[8])
++                      f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4] - 16 + w[8] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[9])
++                      f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + w[9] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[10])
++                      f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + w[10] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[11])
++                      f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + w[11] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[12])
++                      f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + w[12] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[13])
++                      f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5]      + w[13] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[14])
++                      f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6]      + w[14] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[15])
++                      f[(w[0] + ((w[1]      + ((w[3]      + ((w[7]      + w[15] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[16])
++                      f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4] - 16 + ((w[8] - 8 + w[16] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[17])
++                      f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + ((w[9] - 8 + w[17] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[18])
++                      f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + ((w[10] - 8 + w[18] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[19])
++                      f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + ((w[11] - 8 + w[19] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[20])
++                      f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + ((w[12] - 8 + w[20] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[21])
++                      f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5]      + ((w[13] - 8 + w[21] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[22])
++                      f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6]      + ((w[14] - 8 + w[22] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[23])
++                      f[(w[0] + ((w[1]      + ((w[3]      + ((w[7]      + ((w[15] - 8 + w[23] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[24])
++                      f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4] - 16 + ((w[8]     + w[24] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[25])
++                      f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + ((w[9]     + w[25] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[26])
++                      f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + ((w[10]     + w[26] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[27])
++                      f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + ((w[11]     + w[27] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++              if (w[28])
++                      f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + ((w[12]     + w[28] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
++
++              return 0;
++      }
++      /* 10..111. */
++      if ((cd[0] & 0xce & mask) == 0x8e) {
++              /* Variable bitmap format (can be any length >= 3) */
++              uint16_t orig = 0;
++              struct gsm48_var_bit *r = (struct gsm48_var_bit *)cd;
++
++              if (len < 3)
++                      return -EINVAL;
++              orig = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
++              f[orig].mask |= frqt;
++              for (i = 1; 2 + (i >> 3) < len; i++)
++                      if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
++                              f[(orig + i) % 1024].mask |= frqt;
++
++              return 0;
++      }
++
++      return 0;
++}
index 1dc30db,0000000..30316a5
mode 100644,000000..100644
--- /dev/null
@@@ -1,419 -1,0 +1,401 @@@
-       snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf);
 +/* Debugging/Logging support code */
 +
 +/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
 + * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.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 "../config.h"
 +
 +#include <stdarg.h>
 +#include <stdlib.h>
 +#include <stdio.h>
 +#include <string.h>
 +
 +#ifdef HAVE_STRINGS_H
 +#include <strings.h>
 +#endif
 +#include <time.h>
 +#include <errno.h>
 +
 +#include <osmocore/talloc.h>
 +#include <osmocore/utils.h>
 +#include <osmocore/logging.h>
 +
 +const struct log_info *osmo_log_info;
 +
 +static struct log_context log_context;
 +static void *tall_log_ctx = NULL;
 +static LLIST_HEAD(target_list);
 +
 +static const struct value_string loglevel_strs[] = {
 +      { 0,            "EVERYTHING" },
 +      { LOGL_DEBUG,   "DEBUG" },
 +      { LOGL_INFO,    "INFO" },
 +      { LOGL_NOTICE,  "NOTICE" },
 +      { LOGL_ERROR,   "ERROR" },
 +      { LOGL_FATAL,   "FATAL" },
 +      { 0, NULL },
 +};
 +
 +int log_parse_level(const char *lvl)
 +{
 +      return get_string_value(loglevel_strs, lvl);
 +}
 +
 +const char *log_level_str(unsigned int lvl)
 +{
 +      return get_value_string(loglevel_strs, lvl);
 +}
 +
 +int log_parse_category(const char *category)
 +{
 +      int i;
 +
 +      for (i = 0; i < osmo_log_info->num_cat; ++i) {
 +              if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
 +                      return i;
 +      }
 +
 +      return -EINVAL;
 +}
 +
 +/*
 + * Parse the category mask.
 + * The format can be this: category1:category2:category3
 + * or category1,2:category2,3:...
 + */
 +void log_parse_category_mask(struct log_target* target, const char *_mask)
 +{
 +      int i = 0;
 +      char *mask = strdup(_mask);
 +      char *category_token = NULL;
 +
 +      /* Disable everything to enable it afterwards */
 +      for (i = 0; i < ARRAY_SIZE(target->categories); ++i)
 +              target->categories[i].enabled = 0;
 +
 +      category_token = strtok(mask, ":");
 +      do {
 +              for (i = 0; i < osmo_log_info->num_cat; ++i) {
 +                      char* colon = strstr(category_token, ",");
 +                      int length = strlen(category_token);
 +
 +                      if (colon)
 +                          length = colon - category_token;
 +
 +                      if (strncasecmp(osmo_log_info->cat[i].name,
 +                                      category_token, length) == 0) {
 +                              int level = 0;
 +
 +                              if (colon)
 +                                      level = atoi(colon+1);
 +
 +                              target->categories[i].enabled = 1;
 +                              target->categories[i].loglevel = level;
 +                      }
 +              }
 +      } while ((category_token = strtok(NULL, ":")));
 +
 +      free(mask);
 +}
 +
 +static const char* color(int subsys)
 +{
 +      if (subsys < osmo_log_info->num_cat)
 +              return osmo_log_info->cat[subsys].color;
 +
 +      return NULL;
 +}
 +
 +static void _output(struct log_target *target, unsigned int subsys,
 +                  char *file, int line, int cont, const char *format,
 +                  va_list ap)
 +{
 +      char col[30];
 +      char sub[30];
 +      char tim[30];
 +      char buf[4096];
 +      char final[4096];
 +
 +      /* prepare the data */
 +      col[0] = '\0';
 +      sub[0] = '\0';
 +      tim[0] = '\0';
 +      buf[0] = '\0';
 +
 +      /* are we using color */
 +      if (target->use_color) {
 +              const char *c = color(subsys);
 +              if (c) {
 +                      snprintf(col, sizeof(col), "%s", color(subsys));
 +                      col[sizeof(col)-1] = '\0';
 +              }
 +      }
 +      vsnprintf(buf, sizeof(buf), format, ap);
 +      buf[sizeof(buf)-1] = '\0';
 +
 +      if (!cont) {
 +              if (target->print_timestamp) {
 +                      char *timestr;
 +                      time_t tm;
 +                      tm = time(NULL);
 +                      timestr = ctime(&tm);
 +                      timestr[strlen(timestr)-1] = '\0';
 +                      snprintf(tim, sizeof(tim), "%s ", timestr);
 +                      tim[sizeof(tim)-1] = '\0';
 +              }
 +              snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
 +              sub[sizeof(sub)-1] = '\0';
 +      }
 +
- static char hexd_buff[4096];
- char *hexdump(const unsigned char *buf, int len)
- {
-       int i;
-       char *cur = hexd_buff;
-       hexd_buff[0] = 0;
-       for (i = 0; i < len; i++) {
-               int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
-               int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
-               if (rc <= 0)
-                       break;
-               cur += rc;
-       }
-       hexd_buff[sizeof(hexd_buff)-1] = 0;
-       return hexd_buff;
- }
++      snprintf(final, sizeof(final), "%s%s%s%s%s", col, tim, sub, buf,
++               target->use_color ? "\033[0;m" : "");
 +      final[sizeof(final)-1] = '\0';
 +      target->output(target, final);
 +}
 +
 +
 +static void _logp(unsigned int subsys, int level, char *file, int line,
 +                int cont, const char *format, va_list ap)
 +{
 +      struct log_target *tar;
 +
 +      llist_for_each_entry(tar, &target_list, entry) {
 +              struct log_category *category;
 +              int output = 0;
 +
 +              category = &tar->categories[subsys];
 +              /* subsystem is not supposed to be logged */
 +              if (!category->enabled)
 +                      continue;
 +
 +              /* Check the global log level */
 +              if (tar->loglevel != 0 && level < tar->loglevel)
 +                      continue;
 +
 +              /* Check the category log level */
 +              if (tar->loglevel == 0 && category->loglevel != 0 &&
 +                  level < category->loglevel)
 +                      continue;
 +
 +              /* Apply filters here... if that becomes messy we will
 +               * need to put filters in a list and each filter will
 +               * say stop, continue, output */
 +              if ((tar->filter_map & LOG_FILTER_ALL) != 0)
 +                      output = 1;
 +              else if (osmo_log_info->filter_fn)
 +                      output = osmo_log_info->filter_fn(&log_context,
 +                                                     tar);
 +
 +              if (output) {
 +                      /* FIXME: copying the va_list is an ugly
 +                       * workaround against a bug hidden somewhere in
 +                       * _output.  If we do not copy here, the first
 +                       * call to _output() will corrupt the va_list
 +                       * contents, and any further _output() calls
 +                       * with the same va_list will segfault */
 +                      va_list bp;
 +                      va_copy(bp, ap);
 +                      _output(tar, subsys, file, line, cont, format, bp);
 +                      va_end(bp);
 +              }
 +      }
 +}
 +
 +void logp(unsigned int subsys, char *file, int line, int cont,
 +        const char *format, ...)
 +{
 +      va_list ap;
 +
 +      va_start(ap, format);
 +      _logp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
 +      va_end(ap);
 +}
 +
 +void logp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
 +{
 +      va_list ap;
 +
 +      va_start(ap, format);
 +      _logp(subsys, level, file, line, cont, format, ap);
 +      va_end(ap);
 +}
 +
 +void log_add_target(struct log_target *target)
 +{
 +      llist_add_tail(&target->entry, &target_list);
 +}
 +
 +void log_del_target(struct log_target *target)
 +{
 +      llist_del(&target->entry);
 +}
 +
 +void log_reset_context(void)
 +{
 +      memset(&log_context, 0, sizeof(log_context));
 +}
 +
 +int log_set_context(uint8_t ctx_nr, void *value)
 +{
 +      if (ctx_nr > LOG_MAX_CTX)
 +              return -EINVAL;
 +
 +      log_context.ctx[ctx_nr] = value;
 +
 +      return 0;
 +}
 +
 +void log_set_all_filter(struct log_target *target, int all)
 +{
 +      if (all)
 +              target->filter_map |= LOG_FILTER_ALL;
 +      else
 +              target->filter_map &= ~LOG_FILTER_ALL;
 +}
 +
 +void log_set_use_color(struct log_target *target, int use_color)
 +{
 +      target->use_color = use_color;
 +}
 +
 +void log_set_print_timestamp(struct log_target *target, int print_timestamp)
 +{
 +      target->print_timestamp = print_timestamp;
 +}
 +
 +void log_set_log_level(struct log_target *target, int log_level)
 +{
 +      target->loglevel = log_level;
 +}
 +
 +void log_set_category_filter(struct log_target *target, int category,
 +                             int enable, int level)
 +{
 +      if (category >= osmo_log_info->num_cat)
 +              return;
 +      target->categories[category].enabled = !!enable;
 +      target->categories[category].loglevel = level;
 +}
 +
 +/* since C89/C99 says stderr is a macro, we can safely do this! */
 +#ifdef stderr
 +static void _stderr_output(struct log_target *target, const char *log)
 +{
 +      fprintf(target->tgt_stdout.out, "%s", log);
 +      fflush(target->tgt_stdout.out);
 +}
 +#endif
 +
 +struct log_target *log_target_create(void)
 +{
 +      struct log_target *target;
 +      unsigned int i;
 +
 +      target = talloc_zero(tall_log_ctx, struct log_target);
 +      if (!target)
 +              return NULL;
 +
 +      INIT_LLIST_HEAD(&target->entry);
 +
 +      /* initialize the per-category enabled/loglevel from defaults */
 +      for (i = 0; i < osmo_log_info->num_cat; i++) {
 +              struct log_category *cat = &target->categories[i];
 +              cat->enabled = osmo_log_info->cat[i].enabled;
 +              cat->loglevel = osmo_log_info->cat[i].loglevel;
 +      }
 +
 +      /* global settings */
 +      target->use_color = 1;
 +      target->print_timestamp = 0;
 +
 +      /* global log level */
 +      target->loglevel = 0;
 +      return target;
 +}
 +
 +struct log_target *log_target_create_stderr(void)
 +{
 +/* since C89/C99 says stderr is a macro, we can safely do this! */
 +#ifdef stderr
 +      struct log_target *target;
 +
 +      target = log_target_create();
 +      if (!target)
 +              return NULL;
 +
 +      target->tgt_stdout.out = stderr;
 +      target->output = _stderr_output;
 +      return target;
 +#else
 +      return NULL;
 +#endif /* stderr */
 +}
 +
 +const char *log_vty_level_string(struct log_info *info)
 +{
 +      const struct value_string *vs;
 +      unsigned int len = 3; /* ()\0 */
 +      char *str;
 +
 +      for (vs = loglevel_strs; vs->value || vs->str; vs++)
 +              len += strlen(vs->str) + 1;
 +
 +      str = talloc_zero_size(NULL, len);
 +      if (!str)
 +              return NULL;
 +
 +      str[0] = '(';
 +      for (vs = loglevel_strs; vs->value || vs->str; vs++) {
 +              strcat(str, vs->str);
 +              strcat(str, "|");
 +      }
 +      str[strlen(str)-1] = ')';
 +
 +      return str;
 +}
 +
 +const char *log_vty_category_string(struct log_info *info)
 +{
 +      unsigned int len = 3;   /* "()\0" */
 +      unsigned int i;
 +      char *str;
 +
 +      for (i = 0; i < info->num_cat; i++)
 +              len += strlen(info->cat[i].name) + 1;
 +
 +      str = talloc_zero_size(NULL, len);
 +      if (!str)
 +              return NULL;
 +
 +      str[0] = '(';
 +      for (i = 0; i < info->num_cat; i++) {
 +              strcat(str, info->cat[i].name+1);
 +              strcat(str, "|");
 +      }
 +      str[strlen(str)-1] = ')';
 +
 +      return str;
 +}
 +
 +void log_init(const struct log_info *cat)
 +{
 +      tall_log_ctx = talloc_named_const(NULL, 1, "logging");
 +      osmo_log_info = cat;
 +}
index 10fba9b,0000000..3bfeffb
mode 100644,000000..100644
--- /dev/null
@@@ -1,370 -1,0 +1,371 @@@
 +/* 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 <stdio.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 4dab064,0000000..2155885
mode 100644,000000..100644
--- /dev/null
@@@ -1,50 -1,0 +1,106 @@@
 +
 +#include <string.h>
 +#include <stdint.h>
 +#include <errno.h>
 +#include <stdio.h>
 +
 +#include <osmocore/utils.h>
 +
 +static char namebuf[255];
 +const char *get_value_string(const struct value_string *vs, uint32_t val)
 +{
 +      int i;
 +
 +      for (i = 0;; i++) {
 +              if (vs[i].value == 0 && vs[i].str == NULL)
 +                      break;
 +              if (vs[i].value == val)
 +                      return vs[i].str;
 +      }
 +
 +      snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val);
 +      return namebuf;
 +}
 +
 +int get_string_value(const struct value_string *vs, const char *str)
 +{
 +      int i;
 +
 +      for (i = 0;; i++) {
 +              if (vs[i].value == 0 && vs[i].str == NULL)
 +                      break;
 +              if (!strcasecmp(vs[i].str, str))
 +                      return vs[i].value;
 +      }
 +      return -EINVAL;
 +}
 +
 +char bcd2char(uint8_t bcd)
 +{
 +      if (bcd < 0xa)
 +              return '0' + bcd;
 +      else
 +              return 'A' + (bcd - 0xa);
 +}
 +
 +/* only works for numbers in ascci */
 +uint8_t char2bcd(char c)
 +{
 +      return c - 0x30;
 +}
++
++int hexparse(const char *str, uint8_t *b, int max_len)
++
++{
++      int i, l, v;
++
++      l = strlen(str);
++      if ((l&1) || ((l>>1) > max_len))
++              return -1;
++
++      memset(b, 0x00, max_len);
++
++      for (i=0; i<l; i++) {
++              char c = str[i];
++              if (c >= '0' && c <= '9')
++                      v = c - '0';
++              else if (c >= 'a' && c <= 'f')
++                      v = 10 + (c - 'a');
++              else if (c >= 'A' && c <= 'F')
++                      v = 10 + (c - 'A');
++              else
++                      return -1;
++              b[i>>1] |= v << (i&1 ? 0 : 4);
++      }
++
++      return i>>1;
++}
++
++static char hexd_buff[4096];
++
++static char *_hexdump(const unsigned char *buf, int len, char *delim)
++{
++      int i;
++      char *cur = hexd_buff;
++
++      hexd_buff[0] = 0;
++      for (i = 0; i < len; i++) {
++              int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
++              int rc = snprintf(cur, len_remain, "%02x%s", buf[i], delim);
++              if (rc <= 0)
++                      break;
++              cur += rc;
++      }
++      hexd_buff[sizeof(hexd_buff)-1] = 0;
++      return hexd_buff;
++}
++
++char *hexdump(const unsigned char *buf, int len)
++{
++      return _hexdump(buf, len, " ");
++}
++
++char *hexdump_nospc(const unsigned char *buf, int len)
++{
++      return _hexdump(buf, len, "");
++}