Merge commit '07f1103782a94090c2cef46de8a3f6d03ddfeef7'

This commit is contained in:
Sylvain Munaut 2011-10-21 22:14:52 +02:00
commit 02d469ad67
30 changed files with 4054 additions and 1855 deletions

View File

@ -7,6 +7,8 @@ Makefile.in
*.la
*.pc
aclocal.m4
acinclude.m4
aminclude.am
m4/*.m4
autom4te.cache
config.h*
@ -36,6 +38,7 @@ tests/timer/timer_test
tests/msgfile/msgfile_test
tests/ussd/ussd_test
tests/smscb/smscb_test
tests/bits/bitrev_test
utils/osmo-arfcn
@ -44,3 +47,6 @@ doc/core
doc/vty
doc/gsm
doc/html.tar
src/crc*gen.c
include/osmocom/core/crc*gen.h

View File

@ -2,8 +2,11 @@ osmocore_HEADERS = signal.h linuxlist.h timer.h select.h msgb.h bits.h \
bitvec.h statistics.h utils.h socket.h \
gsmtap.h write_queue.h prim.h \
logging.h rate_ctr.h gsmtap_util.h \
crc16.h panic.h process.h \
backtrace.h conv.h application.h
crc16.h panic.h process.h linuxrbtree.h \
backtrace.h conv.h application.h \
crcgen.h crc8gen.h crc16gen.h crc32gen.h crc64gen.h
noinst_HEADERS = timer_compat.h
if ENABLE_PLUGIN
osmocore_HEADERS += plugin.h
@ -22,3 +25,7 @@ osmocore_HEADERS += serial.h
endif
osmocoredir = $(includedir)/osmocom/core
crc%gen.h: crcXXgen.h.tpl
@echo " SED $< -> $@"
@sed -e's/XX/$*/g' $< > $@

View File

@ -0,0 +1,59 @@
/*
* crcXXgen.h
*
* Copyright (C) 2011 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.
*/
#ifndef __OSMO_CRCXXGEN_H__
#define __OSMO_CRCXXGEN_H__
/*! \addtogroup crcgen
* @{
*/
/*! \file crcXXgen.h
* \file Osmocom generic CRC routines (for max XX bits poly) header
*/
#include <stdint.h>
#include <osmocom/core/bits.h>
/*! \brief structure describing a given CRC code of max XX bits */
struct osmo_crcXXgen_code {
int bits; /*!< \brief Actual number of bits of the CRC */
uintXX_t poly; /*!< \brief Polynom (normal representation, MSB omitted */
uintXX_t init; /*!< \brief Initialization value of the CRC state */
uintXX_t remainder; /*!< \brief Remainder of the CRC (final XOR) */
};
uintXX_t osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code,
const ubit_t *in, int len);
int osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code,
const ubit_t *in, int len, const ubit_t *crc_bits);
void osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code,
const ubit_t *in, int len, ubit_t *crc_bits);
/*! }@ */
#endif /* __OSMO_CRCXXGEN_H__ */
/* vim: set syntax=c: */

View File

@ -0,0 +1,41 @@
/*
* crcgen.h
*
* Copyright (C) 2011 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.
*/
#ifndef __OSMO_CRCGEN_H__
#define __OSMO_CRCGEN_H__
/*! \defgroup crcgen Osmocom generic CRC routines
* @{
*/
/*! \file crcgen.h
* \file Osmocom generic CRC routines global header
*/
#include <osmocom/core/crc8gen.h>
#include <osmocom/core/crc16gen.h>
#include <osmocom/core/crc32gen.h>
#include <osmocom/core/crc64gen.h>
/*! }@ */
#endif /* __OSMO_CRCGEN_H__ */

View File

@ -14,6 +14,21 @@
#include <stdint.h>
/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */
/* The GSMTAP format definition is maintained in libosmocore,
* specifically the latest version can always be obtained from
* http://cgit.osmocom.org/cgit/libosmocore/tree/include/osmocom/core/gsmtap.h
*
* If you want to introduce new protocol/burst/channel types or extend
* GSMTAP in any way, please contact the GSMTAP maintainer at either the
* public openbsc@lists.osmocom.org mailing list, or privately at
* Harald Welte <laforge@gnumonks.org>.
*
* Your cooperation ensures that all projects will use the same GSMTAP
* definitions and remain compatible with each other.
*/
#define GSMTAP_VERSION 0x02
#define GSMTAP_TYPE_UM 0x01
@ -22,6 +37,12 @@
#define GSMTAP_TYPE_SIM 0x04
#define GSMTAP_TYPE_TETRA_I1 0x05 /* tetra air interface */
#define GSMTAP_TYPE_TETRA_I1_BURST 0x06 /* tetra air interface */
#define GSMTAP_TYPE_WMX_BURST 0x07 /* WiMAX burst */
#define GSMTAP_TYPE_GB_LLC 0x08 /* GPRS Gb interface: LLC */
#define GSMTAP_TYPE_GB_SNDCP 0x09 /* GPRS Gb interface: SNDCP */
#define GSMTAP_TYPE_GMR1_UM 0x0a /* GMR-1 L2 packets */
/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */
/* sub-types for TYPE_UM_BURST */
#define GSMTAP_BURST_UNKNOWN 0x00
@ -34,6 +55,15 @@
#define GSMTAP_BURST_DUMMY 0x07
#define GSMTAP_BURST_ACCESS 0x08
#define GSMTAP_BURST_NONE 0x09
/* WiMAX bursts */
#define GSMTAP_BURST_CDMA_CODE 0x10 /* WiMAX CDMA Code Attribute burst */
#define GSMTAP_BURST_FCH 0x11 /* WiMAX FCH burst */
#define GSMTAP_BURST_FFB 0x12 /* WiMAX Fast Feedback burst */
#define GSMTAP_BURST_PDU 0x13 /* WiMAX PDU burst */
#define GSMTAP_BURST_HACK 0x14 /* WiMAX HARQ ACK burst */
#define GSMTAP_BURST_PHY_ATTRIBUTES 0x15 /* WiMAX PHY Attributes burst */
/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */
/* sub-types for TYPE_UM */
#define GSMTAP_CHANNEL_UNKNOWN 0x00
@ -47,8 +77,15 @@
#define GSMTAP_CHANNEL_SDCCH8 0x08
#define GSMTAP_CHANNEL_TCH_F 0x09
#define GSMTAP_CHANNEL_TCH_H 0x0a
#define GSMTAP_CHANNEL_CBCH51 0x0b
#define GSMTAP_CHANNEL_CBCH52 0x0c
#define GSMTAP_CHANNEL_PDCH 0x0d
#define GSMTAP_CHANNEL_PTCCH 0x0e
#define GSMTAP_CHANNEL_PACCH 0x0f
#define GSMTAP_CHANNEL_ACCH 0x80
/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */
/* sub-types for TYPE_TETRA_AIR */
#define GSMTAP_TETRA_BSCH 0x01
#define GSMTAP_TETRA_AACH 0x02
@ -59,6 +96,30 @@
#define GSMTAP_TETRA_STCH 0x07
#define GSMTAP_TETRA_TCH_F 0x08
/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */
/* sub-types for TYPE_GMR1_UM */
#define GSMTAP_GMR1_UNKNOWN 0x00
#define GSMTAP_GMR1_BCCH 0x01
#define GSMTAP_GMR1_CCCH 0x02 /* either AGCH or PCH */
#define GSMTAP_GMR1_PCH 0x03
#define GSMTAP_GMR1_AGCH 0x04
#define GSMTAP_GMR1_BACH 0x05
#define GSMTAP_GMR1_RACH 0x06
#define GSMTAP_GMR1_CBCH 0x07
#define GSMTAP_GMR1_SDCCH 0x08
#define GSMTAP_GMR1_TACCH 0x09
#define GSMTAP_GMR1_GBCH 0x0a
#define GSMTAP_GMR1_SACCH 0x01 /* to be combined with _TCH{6,9} */
#define GSMTAP_GMR1_FACCH 0x02 /* to be combines with _TCH{3,6,9} */
#define GSMTAP_GMR1_DKAB 0x03 /* to be combined with _TCH3 */
#define GSMTAP_GMR1_TCH3 0x10
#define GSMTAP_GMR1_TCH6 0x14
#define GSMTAP_GMR1_TCH9 0x18
/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */
/* flags for the ARFCN */
#define GSMTAP_ARFCN_F_PCS 0x8000
#define GSMTAP_ARFCN_F_UPLINK 0x4000
@ -67,6 +128,7 @@
/* IANA-assigned well-known UDP port for GSMTAP messages */
#define GSMTAP_UDP_PORT 4729
/* ====== DO NOT MAKE UNAPPROVED MODIFICATIONS HERE ===== */
struct gsmtap_hdr {
uint8_t version; /* version, set to 0x01 currently */
uint8_t hdr_len; /* length in number of 32bit words */

View File

@ -12,6 +12,10 @@
uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t rsl_link_id);
struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type,
uint8_t ss, uint32_t fn, int8_t signal_dbm,
uint8_t snr, const uint8_t *data, unsigned int len);
struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
uint8_t ss, uint32_t fn, int8_t signal_dbm,
uint8_t snr, const uint8_t *data, unsigned int len);
@ -40,6 +44,11 @@ int gsmtap_source_add_sink(struct gsmtap_inst *gti);
int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg);
int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts,
uint8_t chan_type, uint8_t ss, uint32_t fn,
int8_t signal_dbm, uint8_t snr, const uint8_t *data,
unsigned int len);
int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
uint8_t chan_type, uint8_t ss, uint32_t fn,
int8_t signal_dbm, uint8_t snr, const uint8_t *data,

View File

@ -0,0 +1,160 @@
/*
Red Black Trees
(C) 1999 Andrea Arcangeli <andrea@suse.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
linux/include/linux/rbtree.h
To use rbtrees you'll have to implement your own insert and search cores.
This will avoid us to use callbacks and to drop drammatically performances.
I know it's not the cleaner way, but in C (not in C++) to get
performances and genericity...
Some example of insert and search follows here. The search is a plain
normal search over an ordered tree. The insert instead must be implemented
int two steps: as first thing the code must insert the element in
order as a red leaf in the tree, then the support library function
rb_insert_color() must be called. Such function will do the
not trivial work to rebalance the rbtree if necessary.
-----------------------------------------------------------------------
static inline struct page * rb_search_page_cache(struct inode * inode,
unsigned long offset)
{
struct rb_node * n = inode->i_rb_page_cache.rb_node;
struct page * page;
while (n)
{
page = rb_entry(n, struct page, rb_page_cache);
if (offset < page->offset)
n = n->rb_left;
else if (offset > page->offset)
n = n->rb_right;
else
return page;
}
return NULL;
}
static inline struct page * __rb_insert_page_cache(struct inode * inode,
unsigned long offset,
struct rb_node * node)
{
struct rb_node ** p = &inode->i_rb_page_cache.rb_node;
struct rb_node * parent = NULL;
struct page * page;
while (*p)
{
parent = *p;
page = rb_entry(parent, struct page, rb_page_cache);
if (offset < page->offset)
p = &(*p)->rb_left;
else if (offset > page->offset)
p = &(*p)->rb_right;
else
return page;
}
rb_link_node(node, parent, p);
return NULL;
}
static inline struct page * rb_insert_page_cache(struct inode * inode,
unsigned long offset,
struct rb_node * node)
{
struct page * ret;
if ((ret = __rb_insert_page_cache(inode, offset, node)))
goto out;
rb_insert_color(node, &inode->i_rb_page_cache);
out:
return ret;
}
-----------------------------------------------------------------------
*/
#ifndef _LINUX_RBTREE_H
#define _LINUX_RBTREE_H
#include <stdlib.h>
struct rb_node
{
unsigned long rb_parent_color;
#define RB_RED 0
#define RB_BLACK 1
struct rb_node *rb_right;
struct rb_node *rb_left;
} __attribute__((aligned(sizeof(long))));
/* The alignment might seem pointless, but allegedly CRIS needs it */
struct rb_root
{
struct rb_node *rb_node;
};
#define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
#define rb_color(r) ((r)->rb_parent_color & 1)
#define rb_is_red(r) (!rb_color(r))
#define rb_is_black(r) rb_color(r)
#define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
#define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
{
rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
}
static inline void rb_set_color(struct rb_node *rb, int color)
{
rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
}
#define RB_ROOT { NULL, }
#define rb_entry(ptr, type, member) container_of(ptr, type, member)
#define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
#define RB_EMPTY_NODE(node) (rb_parent(node) == node)
#define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
extern void rb_insert_color(struct rb_node *, struct rb_root *);
extern void rb_erase(struct rb_node *, struct rb_root *);
/* Find logical next and previous nodes in a tree */
extern struct rb_node *rb_next(struct rb_node *);
extern struct rb_node *rb_prev(struct rb_node *);
extern struct rb_node *rb_first(struct rb_root *);
extern struct rb_node *rb_last(struct rb_root *);
/* Fast replacement of a single node without remove/rebalance/add/rebalance */
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
struct rb_root *root);
static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
struct rb_node ** rb_link)
{
node->rb_parent_color = (unsigned long )parent;
node->rb_left = node->rb_right = NULL;
*rb_link = node;
}
#endif /* _LINUX_RBTREE_H */

View File

@ -62,7 +62,7 @@ void logp(int subsys, char *file, int line, int cont, const char *format, ...) _
/* logging levels defined by the library itself */
#define DLGLOBAL -1
#define DLLAPDM -2
#define DLLAPD -2
#define DLINP -3
#define DLMUX -4
#define DLMI -5

View File

@ -10,6 +10,9 @@
#include <stdint.h>
#include <osmocom/core/msgb.h>
#define OSMO_PRIM(prim, op) ((prim << 8) | (op & 0xFF))
#define OSMO_PRIM_HDR(oph) OSMO_PRIM((oph)->primitive, (oph)->operation)
/*! \brief primitive operation */
enum osmo_prim_operation {
PRIM_OP_REQUEST, /*!< \brief request */

View File

@ -32,6 +32,7 @@
#include <sys/time.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/linuxrbtree.h>
/**
* Timer management:
@ -51,11 +52,10 @@
*/
/*! \brief A structure representing a single instance of a timer */
struct osmo_timer_list {
struct llist_head entry; /*!< \brief linked list header */
struct rb_node node; /*!< \brief rb-tree node header */
struct llist_head list; /*!< \brief internal list header */
struct timeval timeout; /*!< \brief expiration time */
unsigned int active : 1; /*!< \brief is it active? */
unsigned int handled : 1; /*!< \brief did we already handle it */
unsigned int in_list : 1; /*!< \brief is it in the global list? */
void (*cb)(void*); /*!< \brief call-back called at timeout */
void *data; /*!< \brief user data for callback */

View File

@ -0,0 +1,79 @@
/*
* (C) 2011 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.
*
*/
/*! \defgroup timer Osmocom timers
* @{
*/
/*! \file timer_compat.h
* \brief Compatibility header with some helpers
*/
#ifndef TIMER_COMPAT_H
#define TIMER_COMPAT_H
/* Convenience macros for operations on timevals.
NOTE: `timercmp' does not work for >= or <=. */
#ifndef timerisset
# define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
#endif
#ifndef timerclear
# define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
#endif
#ifndef timercmp
# define timercmp(a, b, CMP) \
(((a)->tv_sec == (b)->tv_sec) ? \
((a)->tv_usec CMP (b)->tv_usec) : \
((a)->tv_sec CMP (b)->tv_sec))
#endif
#ifndef timeradd
# define timeradd(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
(result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
if ((result)->tv_usec >= 1000000) \
{ \
++(result)->tv_sec; \
(result)->tv_usec -= 1000000; \
} \
} while (0)
#endif
#ifndef timersub
# define timersub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
if ((result)->tv_usec < 0) { \
--(result)->tv_sec; \
(result)->tv_usec += 1000000; \
} \
} while (0)
#endif
/*! }@ */
#endif /* TIMER_COMPAT_H */

View File

@ -1,6 +1,6 @@
osmogsm_HEADERS = a5.h comp128.h gsm0808.h gsm48_ie.h mncc.h rxlev_stat.h \
gsm0480.h gsm48.h gsm_utils.h rsl.h tlv.h abis_nm.h \
sysinfo.h prim.h gsm0502.h lapdm.h
sysinfo.h prim.h gsm0502.h lapd_core.h lapdm.h
SUBDIRS = protocol

View File

@ -0,0 +1,171 @@
#ifndef _OSMOCOM_LAPD_H
#define _OSMOCOM_LAPD_H
#include <stdint.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/msgb.h>
#include <osmocom/gsm/prim.h>
/*! \defgroup lapd LAPD implementation common part
* @{
*/
/*! \file lapd.h */
/* primitive related sutff */
/*! \brief LAPD related primitives (L2<->L3 SAP)*/
enum osmo_dl_prim {
PRIM_DL_UNIT_DATA, /*!< \brief DL-UNIT-DATA */
PRIM_DL_DATA, /*!< \brief DL-DATA */
PRIM_DL_EST, /*!< \brief DL-ESTABLISH */
PRIM_DL_REL, /*!< \brief DL-RLEEASE */
PRIM_DL_SUSP, /*!< \brief DL-SUSPEND */
PRIM_DL_RES, /*!< \brief DL-RESUME */
PRIM_DL_RECON, /*!< \brief DL-RECONNECT */
PRIM_MDL_ERROR, /*!< \brief MDL-ERROR */
};
/* Uses the same values as RLL, so no conversion for GSM is required. */
#define MDL_CAUSE_T200_EXPIRED 0x01
#define MDL_CAUSE_REEST_REQ 0x02
#define MDL_CAUSE_UNSOL_UA_RESP 0x03
#define MDL_CAUSE_UNSOL_DM_RESP 0x04
#define MDL_CAUSE_UNSOL_DM_RESP_MF 0x05
#define MDL_CAUSE_UNSOL_SPRV_RESP 0x06
#define MDL_CAUSE_SEQ_ERR 0x07
#define MDL_CAUSE_UFRM_INC_PARAM 0x08
#define MDL_CAUSE_SFRM_INC_PARAM 0x09
#define MDL_CAUSE_IFRM_INC_MBITS 0x0a
#define MDL_CAUSE_IFRM_INC_LEN 0x0b
#define MDL_CAUSE_FRM_UNIMPL 0x0c
#define MDL_CAUSE_SABM_MF 0x0d
#define MDL_CAUSE_SABM_INFO_NOTALL 0x0e
#define MDL_CAUSE_FRMR 0x0f
/*! \brief for MDL-ERROR.ind */
struct mdl_error_ind_param {
uint8_t cause; /*!< \brief generic cause value */
};
/*! \brief for DL-REL.req */
struct dl_rel_req_param {
uint8_t mode; /*!< \brief release mode */
};
/*! \brief primitive header for LAPD DL-SAP primitives */
struct osmo_dlsap_prim {
struct osmo_prim_hdr oph; /*!< \brief generic primitive header */
union {
struct mdl_error_ind_param error_ind;
struct dl_rel_req_param rel_req;
} u; /*!< \brief request-specific data */
};
/*! \brief LAPD mode/role */
enum lapd_mode {
LAPD_MODE_USER, /*!< \brief behave like user */
LAPD_MODE_NETWORK, /*!< \brief behave like network */
};
/*! \brief LAPD state (Figure B.2/Q.921)*/
enum lapd_state {
LAPD_STATE_NULL = 0,
LAPD_STATE_TEI_UNASS,
LAPD_STATE_ASS_TEI_WAIT,
LAPD_STATE_EST_TEI_WAIT,
LAPD_STATE_IDLE,
LAPD_STATE_SABM_SENT,
LAPD_STATE_DISC_SENT,
LAPD_STATE_MF_EST,
LAPD_STATE_TIMER_RECOV,
};
/*! \brief LAPD message format (I / S / U) */
enum lapd_format {
LAPD_FORM_UKN = 0,
LAPD_FORM_I,
LAPD_FORM_S,
LAPD_FORM_U,
};
/*! \brief LAPD message context */
struct lapd_msg_ctx {
struct lapd_datalink *dl;
int n201;
/* address */
uint8_t cr;
uint8_t sapi;
uint8_t tei;
uint8_t lpd;
/* control */
uint8_t format;
uint8_t p_f; /* poll / final bit */
uint8_t n_send;
uint8_t n_recv;
uint8_t s_u; /* S or repectivly U function bits */
/* length */
int length;
uint8_t more;
};
struct lapd_cr_ent {
uint8_t cmd;
uint8_t resp;
};
struct lapd_history {
struct msgb *msg; /* message to be sent / NULL, if histoy is empty */
int more; /* if message is fragmented */
};
/*! \brief LAPD datalink */
struct lapd_datalink {
int (*send_dlsap)(struct osmo_dlsap_prim *dp,
struct lapd_msg_ctx *lctx);
int (*send_ph_data_req)(struct lapd_msg_ctx *lctx, struct msgb *msg);
struct {
/*! \brief filled-in once we set the lapd_mode above */
struct lapd_cr_ent loc2rem;
struct lapd_cr_ent rem2loc;
} cr;
enum lapd_mode mode; /*!< \brief current mode of link */
int use_sabme; /*!< \brief use SABME instead of SABM */
int reestablish; /*!< \brief enable reestablish support */
int n200, n200_est_rel; /*!< \brief number of retranmissions */
struct lapd_msg_ctx lctx; /*!< \brief LAPD context */
int maxf; /*!< \brief maximum frame size (after defragmentation) */
uint8_t k; /*!< \brief maximum number of unacknowledged frames */
uint8_t v_range; /*!< \brief range of sequence numbers */
uint8_t v_send; /*!< \brief seq nr of next I frame to be transmitted */
uint8_t v_ack; /*!< \brief last frame ACKed by peer */
uint8_t v_recv; /*!< \brief seq nr of next I frame expected to be received */
uint32_t state; /*!< \brief LAPD state (\ref lapd_state) */
int seq_err_cond; /*!< \brief condition of sequence error */
uint8_t own_busy; /*!< \brief receiver busy on our side */
uint8_t peer_busy; /*!< \brief receiver busy on remote side */
int t200_sec, t200_usec; /*!< \brief retry timer (default 1 sec) */
int t203_sec, t203_usec; /*!< \brief retry timer (default 10 secs) */
struct osmo_timer_list t200; /*!< \brief T200 timer */
struct osmo_timer_list t203; /*!< \brief T203 timer */
uint8_t retrans_ctr; /*!< \brief re-transmission counter */
struct llist_head tx_queue; /*!< \brief frames to L1 */
struct llist_head send_queue; /*!< \brief frames from L3 */
struct msgb *send_buffer; /*!< \brief current frame transmitting */
int send_out; /*!< \brief how much was sent from send_buffer */
struct lapd_history *tx_hist; /*!< \brief tx history structure array */
uint8_t range_hist; /*!< \brief range of history buffer 2..2^n */
struct msgb *rcv_buffer; /*!< \brief buffer to assemble the received message */
struct msgb *cont_res; /*!< \brief buffer to store content resolution data on network side, to detect multiple phones on same channel */
};
void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
int maxf);
void lapd_dl_exit(struct lapd_datalink *dl);
void lapd_dl_reset(struct lapd_datalink *dl);
int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode);
int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx);
int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx);
#endif /* _OSMOCOM_LAPD_H */

View File

@ -1,11 +1,7 @@
#ifndef _OSMOCOM_LAPDM_H
#define _OSMOCOM_LAPDM_H
#include <stdint.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/msgb.h>
#include <osmocom/gsm/prim.h>
#include <osmocom/gsm/lapd_core.h>
/*! \defgroup lapdm LAPDm implementation according to GSM TS 04.06
* @{
@ -15,7 +11,7 @@
/* primitive related sutff */
/*! \brief LAPDm related primitives */
/*! \brief LAPDm related primitives (L1<->L2 SAP) */
enum osmo_ph_prim {
PRIM_PH_DATA, /*!< \brief PH-DATA */
PRIM_PH_RACH, /*!< \brief PH-RANDOM_ACCESS */
@ -68,52 +64,22 @@ enum lapdm_mode {
LAPDM_MODE_BTS, /*!< \brief behave like a BTS (network) */
};
/*! \brief LAPDm state */
enum lapdm_state {
LAPDm_STATE_NULL = 0,
LAPDm_STATE_IDLE,
LAPDm_STATE_SABM_SENT,
LAPDm_STATE_MF_EST,
LAPDm_STATE_TIMER_RECOV,
LAPDm_STATE_DISC_SENT,
};
struct lapdm_entity;
/*! \brief LAPDm message context */
struct lapdm_msg_ctx {
struct lapdm_datalink *dl;
int lapdm_fmt;
uint8_t n201;
uint8_t chan_nr;
uint8_t link_id;
uint8_t addr;
uint8_t ctrl;
uint8_t ta_ind;
uint8_t tx_power_ind;
};
/*! \brief LAPDm datalink like TS 04.06 / Section 3.5.2 */
struct lapdm_datalink {
uint8_t V_send; /*!< \brief seq nr of next I frame to be transmitted */
uint8_t V_ack; /*!< \brief last frame ACKed by peer */
uint8_t N_send; /*!< \brief ? set to V_send at Tx time*/
uint8_t V_recv; /*!< \brief seq nr of next I frame expected to be received */
uint8_t N_recv; /*!< \brief expected send seq nr of the next received I frame */
uint32_t state; /*!< \brief LAPDm state (\ref lapdm_state) */
int seq_err_cond; /*!< \brief condition of sequence error */
uint8_t own_busy; /*!< \brief receiver busy on our side */
uint8_t peer_busy; /*!< \brief receiver busy on remote side */
struct osmo_timer_list t200; /*!< \brief T200 timer */
uint8_t retrans_ctr; /*!< \brief re-transmission counter */
struct llist_head send_queue; /*!< \brief frames from L3 */
struct msgb *send_buffer; /*!< \brief current frame transmitting */
int send_out; /*!< \brief how much was sent from send_buffer */
uint8_t tx_hist[8][200]; /*!< \brief tx history buffer */
int tx_length[8]; /*!< \brief length in history buffer */
struct llist_head tx_queue; /*!< \brief frames to L1 */
struct lapd_datalink dl; /* \brief common LAPD */
struct lapdm_msg_ctx mctx; /*!< \brief context of established connection */
struct msgb *rcv_buffer; /*!< \brief buffer to assemble the received message */
struct lapdm_entity *entity; /*!< \brief LAPDm entity we are part of */
};
@ -127,11 +93,6 @@ enum lapdm_dl_sapi {
typedef int (*lapdm_cb_t)(struct msgb *msg, struct lapdm_entity *le, void *ctx);
struct lapdm_cr_ent {
uint8_t cmd;
uint8_t resp;
};
#define LAPDM_ENT_F_EMPTY_FRAME 0x0001
#define LAPDM_ENT_F_POLLING_ONLY 0x0002
@ -144,12 +105,6 @@ struct lapdm_entity {
enum lapdm_mode mode; /*!< \brief are we in BTS mode or MS mode */
unsigned int flags;
struct {
/*! \brief filled-in once we set the lapdm_mode above */
struct lapdm_cr_ent loc2rem;
struct lapdm_cr_ent rem2loc;
} cr;
void *l1_ctx; /*!< \brief context for layer1 instance */
void *l3_ctx; /*!< \brief context for layer3 instance */

View File

@ -1,6 +1,6 @@
osmogsm_proto_HEADERS = gsm_03_41.h \
gsm_04_08.h gsm_04_11.h gsm_04_12.h gsm_04_80.h \
gsm_08_08.h gsm_08_58.h \
gsm_08_08.h gsm_08_58.h gsm_44_318.h \
gsm_12_21.h ipaccess.h
osmogsm_protodir = $(includedir)/osmocom/gsm/protocol

View File

@ -0,0 +1,153 @@
#ifndef PROTO_GSM_44_318_H
#define PROTO_GSM_44_318_H
#include <stdint.h>
/* Definitions according to 3GPP TS 44.318 6.8.0 Release 6 */
/* Table 11.1.1.4.1: Message types for URR */
enum gan_msg_type {
GA_MT_RC_DISCOVERY_REQUEST = 0x01,
GA_MT_RC_DISCOVERY_ACCEPT = 0x02,
GA_MT_RC_DISCOVERY_REJECT = 0x03,
GA_MT_RC_REGISTER_REQUEST = 0x10,
GA_MT_RC_REGISTER_ACCEPT = 0x11,
GA_MT_RC_REGISTER_REDIRECT = 0x12,
GA_MT_RC_REGISTER_REJECT = 0x13,
GA_MT_RC_DEREGISTER = 0x14,
GA_MT_RC_REGISTER_UPDATE_UL = 0x15,
GA_MT_RC_REGISTER_UPDATE_DL = 0x16,
GA_MT_RC_CELL_BCAST_INFO = 0x17,
GA_MT_CSR_CIPH_MODE_CMD = 0x20,
GA_MT_CSR_CIPH_MODE_COMPL = 0x21,
GA_MT_CSR_ACT_CHAN = 0x30,
GA_MT_CSR_ACT_CHAN_ACK = 0x31,
GA_MT_CSR_ACT_CHAN_COMPL = 0x32,
GA_MT_CSR_ACT_CHAN_FAIL = 0x33,
GA_MT_CSR_CHAN_MODE_MOD = 0x34,
GA_MT_CSR_CHAN_MODE_MOD_ACK = 0x35,
GA_MT_CSR_RELEASE = 0x40,
GA_MT_CSR_RELEASE_COMPL = 0x41,
GA_MT_CSR_CLEAR_REQ = 0x42,
GA_MT_CSR_HO_ACCESS = 0x50,
GA_MT_CSR_HO_COMPL = 0x51,
GA_MT_CSR_UL_QUAL_IND = 0x52,
GA_MT_CSR_HO_INFO = 0x53,
GA_MT_CSR_HO_CMD = 0x54,
GA_MT_CSR_HO_FAIL = 0x55,
GA_MT_CSR_PAGING_REQ = 0x60,
GA_MT_CSR_PAGING_RESP = 0x61,
GA_MT_CSR_UL_DIRECT_XFER = 0x70,
GA_MT_CSR_DL_DIRECT_XFER = 0x72,
GA_MT_CSR_STATUS = 0x73,
GA_MT_RC_KEEPALIVE = 0x74,
GA_MT_CSR_CM_ENQ = 0x75,
GA_MT_CSR_CM_CHANGE = 0x76,
GA_MT_CSR_REQUEST = 0x80,
GA_MT_CSR_REQUEST_ACCEPT = 0x81,
GA_MT_CSR_REQUEST_REJECT = 0x82,
};
/* All tables in 10.1.x and 10.2.x / Table 11.2.1 */
enum gan_iei {
GA_IE_MI = 1,
GA_IE_GAN_RELEASE_IND = 2,
GA_IE_RADIO_IE = 3,
GA_IE_GERAN_CELL_ID = 4,
GA_IE_LAC = 5,
GA_IE_GERAN_COV_IND = 6,
GA_IE_GAN_CM = 7,
GA_IE_GEO_LOC = 8,
GA_IE_DEF_SEGW_IP = 9,
GA_IE_DEF_SEGW_FQDN = 10,
GA_IE_REDIR_CTR = 11,
GA_IE_DISCOV_REJ_CAUSE = 12,
GA_IE_GANC_CELL_DESC = 13,
GA_IE_GANC_CTRL_CH_DESC = 14,
GA_IE_GERAN_CELL_ID_LIST= 15,
GA_IE_TU3907_TIMER = 16,
GA_IE_RR_STATE = 17,
GA_IE_RAI = 18,
GA_IE_GAN_BAND = 19,
GA_IE_GARC_GACSR_STATE = 20,
GA_IE_REG_REJ_CAUSE = 21,
GA_IE_TU3906_TIMER = 22,
GA_IE_TU3910_TIMER = 23,
GA_IE_TU3902_TIMER = 24,
GA_IE_L3_MSG = 26,
GA_IE_CHAN_MODE = 27,
GA_IE_MS_CLASSMARK2 = 28,
GA_IE_RR_CAUSE = 29,
GA_EI_CIPH_MODE_SET = 30,
GA_IE_GPRS_RESUMPTION = 31,
GA_IE_HO_FROM_GAN_CMD = 32,
GA_IE_UL_QUAL_IND = 33,
GA_IE_TLLI = 34,
GA_IE_PFI = 35,
GA_IE_SUSP_CAUSE = 36,
GA_IE_TU3820_TIMER = 37,
GA_IE_REQD_QOS = 38,
GA_IE_P_DEACT_CAUSE = 39
GA_IE_REQD_UL_RATE = 40,
GA_IE_RAC = 41,
GA_IE_AP_LOCATION = 42,
GA_IE_TU4001_TIMER = 43,
GA_IE_LOC_STATUS = 44,
GA_IE_CIPH_RESP = 45,
GA_IE_CIPH_RAND = 46,
GA_IE_CIPH_MAC = 47,
GA_IE_CKSN = 48,
GA_IE_SAPI_ID = 49,
GA_IE_EST_CAUSE = 50,
GA_IE_CHAN_NEEDED = 51,
GA_IE_PDU_IN_ERROR = 52,
GA_IE_SAMPLE_SIZE = 53,
GA_IE_PAYLOAD_TYPE = 54,
GA_IE_MULTIRATE_CONF = 55,
GA_IE_MS_CLASSMARK3 = 56,
GA_IE_LLC_PDU = 57,
GA_IE_LOC_BLACKL_IND = 58,
GA_IE_RESET_IND = 59,
GA_IE_TU4003_TIMER = 60,
GA_IE_AP_SERV_NAME = 61,
GA_IE_SERV_ZONE_INFO = 62,
GA_IE_RTP_RED_CONF = 63,
GA_IE_UTRAN_CLASSMARK = 64,
GA_IE_CM_ENQ_MASK = 65,
GA_IE_UTRAN_CELLID_LIST = 66,
GA_IE_SERV_GANC_TBL_IND = 67,
GA_IE_AP_REG_IND = 68,
GA_IE_GAN_PLMN_LIST = 69,
GA_IE_REQD_GAN_SERV = 71,
GA_IE_BCAST_CONTAINER = 72,
GA_IE_3G_CELL_ID = 73,
GA_IE_MS_RADIO_ID = 96,
GA_IE_DEF_GANC_IP = 97,
GA_IE_DEF_GANC_FQDN = 98,
GA_IE_GPRS_IP_ADDR = 99,
GA_IE_GPRS_UDP_PORT = 100
GA_IE_GANC_TCP_PORT = 103,
GA_IE_RTP_UDP_PORT = 104,
GA_IE_RTCP_UDP_PORT = 105,
GA_IE_GERAN_RCV_SIGL_LIST = 106,
GA_IE_UTRAN_RCV_SIGL_LIST = 107,
};
/* 11.1.1 GA-RC and GA-CSR Message header IE */
struct gan_rc_csr_hdr {
uint16_t len;
uint8_t pdisc:4,
skip_ind:4;
uint8_t msg_type;
} __attribute__((packed));
#endif /* PROTO_GSM_44_318_H */

View File

@ -37,7 +37,7 @@ typedef uint8_t sysinfo_buf_t[GSM_MACBLOCK_LEN];
extern const struct value_string osmo_sitype_strs[_MAX_SYSINFO_TYPE];
uint8_t gsm_sitype2rsl(enum osmo_sysinfo_type si_type);
uint8_t osmo_sitype2rsl(enum osmo_sysinfo_type si_type);
enum osmo_sysinfo_type osmo_rsl2sitype(uint8_t rsl_si);
#endif /* _OSMO_GSM_SYSINFO_H */

View File

@ -179,6 +179,9 @@ int vty_current_node(struct vty *vty);
extern void *tall_vty_ctx;
extern struct cmd_element cfg_description_cmd;
extern struct cmd_element cfg_no_description_cmd;
/*! }@ */
#endif

View File

@ -2,9 +2,9 @@ SUBDIRS=. vty codec gsm
# 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=2:0:0
LIBVERSION=3:0:0
INCLUDES = $(all_includes) -I$(top_srcdir)/include
INCLUDES = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/include
AM_CFLAGS = -fPIC -Wall
lib_LTLIBRARIES = libosmocore.la
@ -14,7 +14,8 @@ libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c bits.c \
write_queue.c utils.c socket.c \
logging.c logging_syslog.c rate_ctr.c \
gsmtap_util.c crc16.c panic.c backtrace.c \
conv.c application.c
conv.c application.c rbtree.c \
crc8gen.c crc16gen.c crc32gen.c crc64gen.c
if ENABLE_PLUGIN
libosmocore_la_SOURCES += plugin.c
@ -25,6 +26,8 @@ endif
if ENABLE_TALLOC
libosmocore_la_SOURCES += talloc.c
else
libosmocore_la_LIBADD = -ltalloc
endif
if ENABLE_MSGFILE
@ -34,3 +37,7 @@ endif
if ENABLE_SERIAL
libosmocore_la_SOURCES += serial.c
endif
crc%gen.c: crcXXgen.c.tpl
@echo " SED $< -> $@"
@sed -e's/XX/$*/g' $< > $@

View File

@ -0,0 +1,120 @@
/*
* crcXXgen.c
*
* Generic CRC routines (for max XX bits poly)
*
* Copyright (C) 2011 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.
*/
/*! \addtogroup crcgen
* @{
*/
/*! \file crcXXgen.c
* \file Osmocom generic CRC routines (for max XX bits poly)
*/
#include <stdint.h>
#include <osmocom/core/bits.h>
#include <osmocom/core/crcXXgen.h>
/*! \brief Compute the CRC value of a given array of hard-bits
* \param[in] code The CRC code description to apply
* \param[in] in Array of hard bits
* \param[in] len Length of the array of hard bits
* \returns The CRC value
*/
uintXX_t
osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code,
const ubit_t *in, int len)
{
const uintXX_t poly = code->poly;
uintXX_t crc = code->init;
int i, n = code->bits-1;
for (i=0; i<len; i++) {
uintXX_t bit = in[i] & 1;
crc ^= (bit << n);
if (crc & (1 << n)) {
crc <<= 1;
crc ^= poly;
} else {
crc <<= 1;
}
crc &= (1ULL << code->bits) - 1;
}
crc ^= code->remainder;
return crc;
}
/*! \brief Checks the CRC value of a given array of hard-bits
* \param[in] code The CRC code description to apply
* \param[in] in Array of hard bits
* \param[in] len Length of the array of hard bits
* \param[in] crc_bits Array of hard bits with the alleged CRC
* \returns 0 if CRC matches. 1 in case of error.
*
* The crc_bits array must have a length of code->len
*/
int
osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code,
const ubit_t *in, int len, const ubit_t *crc_bits)
{
uintXX_t crc;
int i;
crc = osmo_crcXXgen_compute_bits(code, in, len);
for (i=0; i<code->bits; i++)
if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1))
return 1;
return 0;
}
/*! \brief Computes and writes the CRC value of a given array of bits
* \param[in] code The CRC code description to apply
* \param[in] in Array of hard bits
* \param[in] len Length of the array of hard bits
* \param[in] crc_bits Array of hard bits to write the computed CRC to
*
* The crc_bits array must have a length of code->len
*/
void
osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code,
const ubit_t *in, int len, ubit_t *crc_bits)
{
uintXX_t crc;
int i;
crc = osmo_crcXXgen_compute_bits(code, in, len);
for (i=0; i<code->bits; i++)
crc_bits[i] = ((crc >> (code->bits-i-1)) & 1);
}
/*! }@ */
/* vim: set syntax=c: */

View File

@ -1,6 +1,6 @@
# 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=1:0:0
LIBVERSION=1:1:0
INCLUDES = $(all_includes) -I$(top_srcdir)/include
AM_CFLAGS = -fPIC -Wall
@ -10,6 +10,6 @@ lib_LTLIBRARIES = libosmogsm.la
libosmogsm_la_SOURCES = a5.c rxlev_stat.c tlv_parser.c comp128.c gsm_utils.c \
rsl.c gsm48.c gsm48_ie.c gsm0808.c sysinfo.c \
gprs_cipher_core.c gsm0480.c abis_nm.c gsm0502.c \
lapdm.c
lapd_core.c lapdm.c
libosmogsm_la_LDFLAGS = -version-info $(LIBVERSION)
libosmogsm_la_LIBADD = $(top_builddir)/src/libosmocore.la

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -88,6 +88,53 @@ uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
return ret;
}
/*! \brief create an arbitrary type GSMTAP message
* \param[in] type The GSMTAP_TYPE_xxx constant of the message to create
* \param[in] arfcn GSM ARFCN (Channel Number)
* \param[in] ts GSM time slot
* \param[in] chan_type Channel Type
* \param[in] ss Sub-slot
* \param[in] fn GSM Frame Number
* \param[in] signal_dbm Signal Strength (dBm)
* \param[in] snr Signal/Noise Ratio (SNR)
* \param[in] data Pointer to data buffer
* \param[in] len Length of \ref data
*
* This function will allocate a new msgb and fill it with a GSMTAP
* header containing the information
*/
struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type,
uint8_t ss, uint32_t fn, int8_t signal_dbm,
uint8_t snr, const uint8_t *data, unsigned int len)
{
struct msgb *msg;
struct gsmtap_hdr *gh;
uint8_t *dst;
msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
if (!msg)
return NULL;
gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
gh->version = GSMTAP_VERSION;
gh->hdr_len = sizeof(*gh)/4;
gh->type = type;
gh->timeslot = ts;
gh->sub_slot = ss;
gh->arfcn = htons(arfcn);
gh->snr_db = snr;
gh->signal_dbm = signal_dbm;
gh->frame_number = htonl(fn);
gh->sub_type = chan_type;
gh->antenna_nr = 0;
dst = msgb_put(msg, len);
memcpy(dst, data, len);
return msg;
}
/*! \brief create L1/L2 data and put it into GSMTAP
* \param[in] arfcn GSM ARFCN (Channel Number)
* \param[in] ts GSM time slot
@ -106,32 +153,8 @@ struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
uint8_t ss, uint32_t fn, int8_t signal_dbm,
uint8_t snr, const uint8_t *data, unsigned int len)
{
struct msgb *msg;
struct gsmtap_hdr *gh;
uint8_t *dst;
msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
if (!msg)
return NULL;
gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
gh->version = GSMTAP_VERSION;
gh->hdr_len = sizeof(*gh)/4;
gh->type = GSMTAP_TYPE_UM;
gh->timeslot = ts;
gh->sub_slot = ss;
gh->arfcn = htons(arfcn);
gh->snr_db = snr;
gh->signal_dbm = signal_dbm;
gh->frame_number = htonl(fn);
gh->sub_type = chan_type;
gh->antenna_nr = 0;
dst = msgb_put(msg, len);
memcpy(dst, data, len);
return msg;
return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type,
ss, fn, signal_dbm, snr, data, len);
}
#ifdef HAVE_SYS_SOCKET_H
@ -208,8 +231,10 @@ int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
}
}
/*! \brief receive a message from L1/L2 and put it in GSMTAP */
int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
/*! \brief send an arbitrary type through GSMTAP.
* See \ref gsmtap_makemsg_ex for arguments
*/
int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts,
uint8_t chan_type, uint8_t ss, uint32_t fn,
int8_t signal_dbm, uint8_t snr, const uint8_t *data,
unsigned int len)
@ -219,7 +244,7 @@ int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
if (!gti)
return -ENODEV;
msg = gsmtap_makemsg(arfcn, ts, chan_type, ss, fn, signal_dbm,
msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm,
snr, data, len);
if (!msg)
return -ENOMEM;
@ -227,6 +252,18 @@ int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
return gsmtap_sendmsg(gti, msg);
}
/*! \brief send a message from L1/L2 through GSMTAP.
* See \ref gsmtap_makemsg for arguments
*/
int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
uint8_t chan_type, uint8_t ss, uint32_t fn,
int8_t signal_dbm, uint8_t snr, const uint8_t *data,
unsigned int len)
{
return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn,
signal_dbm, snr, data, len);
}
/* Callback from select layer if we can write to the socket */
static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
{

View File

@ -72,9 +72,9 @@ static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
.loglevel = LOGL_NOTICE,
.enabled = 1,
},
[INT2IDX(DLLAPDM)] = { /* -2 becomes 1 */
.name = "DLLAPDM",
.description = "LAPDm in libosmogsm",
[INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
.name = "DLLAPD",
.description = "LAPD in libosmogsm",
.loglevel = LOGL_NOTICE,
.enabled = 1,
},

View File

@ -0,0 +1,389 @@
/*
Red Black Trees
(C) 1999 Andrea Arcangeli <andrea@suse.de>
(C) 2002 David Woodhouse <dwmw2@infradead.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
linux/lib/rbtree.c
*/
#include <osmocom/core/linuxrbtree.h>
static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
{
struct rb_node *right = node->rb_right;
struct rb_node *parent = rb_parent(node);
if ((node->rb_right = right->rb_left))
rb_set_parent(right->rb_left, node);
right->rb_left = node;
rb_set_parent(right, parent);
if (parent)
{
if (node == parent->rb_left)
parent->rb_left = right;
else
parent->rb_right = right;
}
else
root->rb_node = right;
rb_set_parent(node, right);
}
static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
{
struct rb_node *left = node->rb_left;
struct rb_node *parent = rb_parent(node);
if ((node->rb_left = left->rb_right))
rb_set_parent(left->rb_right, node);
left->rb_right = node;
rb_set_parent(left, parent);
if (parent)
{
if (node == parent->rb_right)
parent->rb_right = left;
else
parent->rb_left = left;
}
else
root->rb_node = left;
rb_set_parent(node, left);
}
void rb_insert_color(struct rb_node *node, struct rb_root *root)
{
struct rb_node *parent, *gparent;
while ((parent = rb_parent(node)) && rb_is_red(parent))
{
gparent = rb_parent(parent);
if (parent == gparent->rb_left)
{
{
register struct rb_node *uncle = gparent->rb_right;
if (uncle && rb_is_red(uncle))
{
rb_set_black(uncle);
rb_set_black(parent);
rb_set_red(gparent);
node = gparent;
continue;
}
}
if (parent->rb_right == node)
{
register struct rb_node *tmp;
__rb_rotate_left(parent, root);
tmp = parent;
parent = node;
node = tmp;
}
rb_set_black(parent);
rb_set_red(gparent);
__rb_rotate_right(gparent, root);
} else {
{
register struct rb_node *uncle = gparent->rb_left;
if (uncle && rb_is_red(uncle))
{
rb_set_black(uncle);
rb_set_black(parent);
rb_set_red(gparent);
node = gparent;
continue;
}
}
if (parent->rb_left == node)
{
register struct rb_node *tmp;
__rb_rotate_right(parent, root);
tmp = parent;
parent = node;
node = tmp;
}
rb_set_black(parent);
rb_set_red(gparent);
__rb_rotate_left(gparent, root);
}
}
rb_set_black(root->rb_node);
}
static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
struct rb_root *root)
{
struct rb_node *other;
while ((!node || rb_is_black(node)) && node != root->rb_node)
{
if (parent->rb_left == node)
{
other = parent->rb_right;
if (rb_is_red(other))
{
rb_set_black(other);
rb_set_red(parent);
__rb_rotate_left(parent, root);
other = parent->rb_right;
}
if ((!other->rb_left || rb_is_black(other->rb_left)) &&
(!other->rb_right || rb_is_black(other->rb_right)))
{
rb_set_red(other);
node = parent;
parent = rb_parent(node);
}
else
{
if (!other->rb_right || rb_is_black(other->rb_right))
{
struct rb_node *o_left;
if ((o_left = other->rb_left))
rb_set_black(o_left);
rb_set_red(other);
__rb_rotate_right(other, root);
other = parent->rb_right;
}
rb_set_color(other, rb_color(parent));
rb_set_black(parent);
if (other->rb_right)
rb_set_black(other->rb_right);
__rb_rotate_left(parent, root);
node = root->rb_node;
break;
}
}
else
{
other = parent->rb_left;
if (rb_is_red(other))
{
rb_set_black(other);
rb_set_red(parent);
__rb_rotate_right(parent, root);
other = parent->rb_left;
}
if ((!other->rb_left || rb_is_black(other->rb_left)) &&
(!other->rb_right || rb_is_black(other->rb_right)))
{
rb_set_red(other);
node = parent;
parent = rb_parent(node);
}
else
{
if (!other->rb_left || rb_is_black(other->rb_left))
{
register struct rb_node *o_right;
if ((o_right = other->rb_right))
rb_set_black(o_right);
rb_set_red(other);
__rb_rotate_left(other, root);
other = parent->rb_left;
}
rb_set_color(other, rb_color(parent));
rb_set_black(parent);
if (other->rb_left)
rb_set_black(other->rb_left);
__rb_rotate_right(parent, root);
node = root->rb_node;
break;
}
}
}
if (node)
rb_set_black(node);
}
void rb_erase(struct rb_node *node, struct rb_root *root)
{
struct rb_node *child, *parent;
int color;
if (!node->rb_left)
child = node->rb_right;
else if (!node->rb_right)
child = node->rb_left;
else
{
struct rb_node *old = node, *left;
node = node->rb_right;
while ((left = node->rb_left) != NULL)
node = left;
child = node->rb_right;
parent = rb_parent(node);
color = rb_color(node);
if (child)
rb_set_parent(child, parent);
if (parent == old) {
parent->rb_right = child;
parent = node;
} else
parent->rb_left = child;
node->rb_parent_color = old->rb_parent_color;
node->rb_right = old->rb_right;
node->rb_left = old->rb_left;
if (rb_parent(old))
{
if (rb_parent(old)->rb_left == old)
rb_parent(old)->rb_left = node;
else
rb_parent(old)->rb_right = node;
} else
root->rb_node = node;
rb_set_parent(old->rb_left, node);
if (old->rb_right)
rb_set_parent(old->rb_right, node);
goto color;
}
parent = rb_parent(node);
color = rb_color(node);
if (child)
rb_set_parent(child, parent);
if (parent)
{
if (parent->rb_left == node)
parent->rb_left = child;
else
parent->rb_right = child;
}
else
root->rb_node = child;
color:
if (color == RB_BLACK)
__rb_erase_color(child, parent, root);
}
/*
* This function returns the first node (in sort order) of the tree.
*/
struct rb_node *rb_first(struct rb_root *root)
{
struct rb_node *n;
n = root->rb_node;
if (!n)
return NULL;
while (n->rb_left)
n = n->rb_left;
return n;
}
struct rb_node *rb_last(struct rb_root *root)
{
struct rb_node *n;
n = root->rb_node;
if (!n)
return NULL;
while (n->rb_right)
n = n->rb_right;
return n;
}
struct rb_node *rb_next(struct rb_node *node)
{
struct rb_node *parent;
if (rb_parent(node) == node)
return NULL;
/* If we have a right-hand child, go down and then left as far
as we can. */
if (node->rb_right) {
node = node->rb_right;
while (node->rb_left)
node=node->rb_left;
return node;
}
/* No right-hand children. Everything down and left is
smaller than us, so any 'next' node must be in the general
direction of our parent. Go up the tree; any time the
ancestor is a right-hand child of its parent, keep going
up. First time it's a left-hand child of its parent, said
parent is our 'next' node. */
while ((parent = rb_parent(node)) && node == parent->rb_right)
node = parent;
return parent;
}
struct rb_node *rb_prev(struct rb_node *node)
{
struct rb_node *parent;
if (rb_parent(node) == node)
return NULL;
/* If we have a left-hand child, go down and then right as far
as we can. */
if (node->rb_left) {
node = node->rb_left;
while (node->rb_right)
node=node->rb_right;
return node;
}
/* No left-hand children. Go up till we find an ancestor which
is a right-hand child of its parent */
while ((parent = rb_parent(node)) && node == parent->rb_left)
node = parent;
return parent;
}
void rb_replace_node(struct rb_node *victim, struct rb_node *new,
struct rb_root *root)
{
struct rb_node *parent = rb_parent(victim);
/* Set the surrounding nodes to point to the replacement */
if (parent) {
if (victim == parent->rb_left)
parent->rb_left = new;
else
parent->rb_right = new;
} else {
root->rb_node = new;
}
if (victim->rb_left)
rb_set_parent(victim->rb_left, new);
if (victim->rb_right)
rb_set_parent(victim->rb_right, new);
/* Copy the pointers/colour from the victim to the replacement */
*new = *victim;
}

View File

@ -38,8 +38,9 @@
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef __linux__
#include <linux/serial.h>
#endif
#include <osmocom/core/serial.h>
@ -155,6 +156,7 @@ osmo_serial_set_baudrate(int fd, speed_t baudrate)
int
osmo_serial_set_custom_baudrate(int fd, int baudrate)
{
#ifdef __linux__
int rc;
struct serial_struct ser_info;
@ -174,6 +176,23 @@ osmo_serial_set_custom_baudrate(int fd, int baudrate)
}
return _osmo_serial_set_baudrate(fd, B38400); /* 38400 is a kind of magic ... */
#elif defined(__APPLE__)
#ifndef IOSSIOSPEED
#define IOSSIOSPEED _IOW('T', 2, speed_t)
#endif
int rc;
unsigned int speed = baudrate;
rc = ioctl(fd, IOSSIOSPEED, &speed);
if (rc < 0) {
dbg_perror("ioctl(IOSSIOSPEED)");
return -errno;
}
return 0;
#else
#warning osmo_serial_set_custom_baudrate: unsupported platform
return 0;
#endif
}
/*! \brief Clear any custom baudrate
@ -186,6 +205,7 @@ int
osmo_serial_clear_custom_baudrate(int fd)
{
int rc;
#ifdef __linux__
struct serial_struct ser_info;
rc = ioctl(fd, TIOCGSERIAL, &ser_info);
@ -202,7 +222,7 @@ osmo_serial_clear_custom_baudrate(int fd)
dbg_perror("ioctl(TIOCSSERIAL)");
return -errno;
}
#endif
return 0;
}

View File

@ -1,7 +1,12 @@
/*
* (C) 2008,2009 by Holger Hans Peter Freyther <zecke@selfish.org>
* (C) 2011 by Harald Welte <laforge@gnumonks.org>
* All Rights Reserved
*
* Authors: Holger Hans Peter Freyther <zecke@selfish.org>
* Harald Welte <laforge@gnumonks.org>
* Pablo Neira Ayuso <pablo@gnumonks.org>
*
* 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
@ -18,6 +23,10 @@
*
*/
/* These store the amount of time that we wait until next timer expires. */
static struct timeval nearest;
static struct timeval *nearest_p;
/*! \addtogroup timer
* @{
*/
@ -27,35 +36,42 @@
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/timer_compat.h>
#include <osmocom/core/linuxlist.h>
static LLIST_HEAD(timer_list);
static struct timeval s_nearest_time;
static struct timeval s_select_time;
static struct rb_root timer_root = RB_ROOT;
#define MICRO_SECONDS 1000000LL
static void __add_timer(struct osmo_timer_list *timer)
{
struct rb_node **new = &(timer_root.rb_node);
struct rb_node *parent = NULL;
#define TIME_SMALLER(left, right) \
(left.tv_sec*MICRO_SECONDS+left.tv_usec) <= (right.tv_sec*MICRO_SECONDS+right.tv_usec)
while (*new) {
struct osmo_timer_list *this;
this = container_of(*new, struct osmo_timer_list, node);
parent = *new;
if (timercmp(&timer->timeout, &this->timeout, <))
new = &((*new)->rb_left);
else
new = &((*new)->rb_right);
}
rb_link_node(&timer->node, parent, new);
rb_insert_color(&timer->node, &timer_root);
}
/*! \brief add a new timer to the timer management
* \param[in] timer the timer that should be added
*/
void osmo_timer_add(struct osmo_timer_list *timer)
{
struct osmo_timer_list *list_timer;
/* TODO: Optimize and remember the closest item... */
timer->active = 1;
/* this might be called from within update_timers */
llist_for_each_entry(list_timer, &timer_list, entry)
if (timer == list_timer)
return;
timer->in_list = 1;
llist_add(&timer->entry, &timer_list);
INIT_LLIST_HEAD(&timer->list);
__add_timer(timer);
}
/*! \brief schedule a timer at a given future relative time
@ -74,10 +90,9 @@ osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds
struct timeval current_time;
gettimeofday(&current_time, NULL);
unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec;
currentTime += seconds * MICRO_SECONDS + microseconds;
timer->timeout.tv_sec = currentTime / MICRO_SECONDS;
timer->timeout.tv_usec = currentTime % MICRO_SECONDS;
timer->timeout.tv_sec = seconds;
timer->timeout.tv_usec = microseconds;
timeradd(&timer->timeout, &current_time, &timer->timeout);
osmo_timer_add(timer);
}
@ -89,10 +104,12 @@ osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds
*/
void osmo_timer_del(struct osmo_timer_list *timer)
{
if (timer->in_list) {
if (timer->active) {
timer->active = 0;
timer->in_list = 0;
llist_del(&timer->entry);
rb_erase(&timer->node, &timer_root);
/* make sure this is not already scheduled for removal. */
if (!llist_empty(&timer->list))
llist_del_init(&timer->list);
}
}
@ -116,26 +133,26 @@ int osmo_timer_pending(struct osmo_timer_list *timer)
*/
struct timeval *osmo_timers_nearest(void)
{
struct timeval current_time;
/* nearest_p is exactly what we need already: NULL if nothing is
* waiting, {0,0} if we must dispatch immediately, and the correct
* delay if we need to wait */
return nearest_p;
}
if (s_nearest_time.tv_sec == 0 && s_nearest_time.tv_usec == 0)
return NULL;
if (gettimeofday(&current_time, NULL) == -1)
return NULL;
unsigned long long nearestTime = s_nearest_time.tv_sec * MICRO_SECONDS + s_nearest_time.tv_usec;
unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec;
if (nearestTime < currentTime) {
s_select_time.tv_sec = 0;
s_select_time.tv_usec = 0;
static void update_nearest(struct timeval *cand, struct timeval *current)
{
if (cand->tv_sec != LONG_MAX) {
if (timercmp(cand, current, >))
timersub(cand, current, &nearest);
else {
/* loop again inmediately */
nearest.tv_sec = 0;
nearest.tv_usec = 0;
}
nearest_p = &nearest;
} else {
s_select_time.tv_sec = (nearestTime - currentTime) / MICRO_SECONDS;
s_select_time.tv_usec = (nearestTime - currentTime) % MICRO_SECONDS;
nearest_p = NULL;
}
return &s_select_time;
}
/*
@ -143,17 +160,18 @@ struct timeval *osmo_timers_nearest(void)
*/
void osmo_timers_prepare(void)
{
struct osmo_timer_list *timer, *nearest_timer = NULL;
llist_for_each_entry(timer, &timer_list, entry) {
if (!nearest_timer || TIME_SMALLER(timer->timeout, nearest_timer->timeout)) {
nearest_timer = timer;
}
}
struct rb_node *node;
struct timeval current;
if (nearest_timer) {
s_nearest_time = nearest_timer->timeout;
gettimeofday(&current, NULL);
node = rb_first(&timer_root);
if (node) {
struct osmo_timer_list *this;
this = container_of(node, struct osmo_timer_list, node);
update_nearest(&this->timeout, &current);
} else {
memset(&s_nearest_time, 0, sizeof(struct timeval));
nearest_p = NULL;
}
}
@ -163,46 +181,41 @@ void osmo_timers_prepare(void)
int osmo_timers_update(void)
{
struct timeval current_time;
struct osmo_timer_list *timer, *tmp;
struct rb_node *node;
struct llist_head timer_eviction_list;
struct osmo_timer_list *this;
int work = 0;
gettimeofday(&current_time, NULL);
INIT_LLIST_HEAD(&timer_eviction_list);
for (node = rb_first(&timer_root); node; node = rb_next(node)) {
this = container_of(node, struct osmo_timer_list, node);
if (timercmp(&this->timeout, &current_time, >))
break;
llist_add(&this->list, &timer_eviction_list);
}
/*
* The callbacks might mess with our list and in this case
* even llist_for_each_entry_safe is not safe to use. To allow
* del_timer, add_timer, schedule_timer to be called from within
* the callback we jump through some loops.
* osmo_timer_del to be called from within the callback we need
* to restart the iteration for each element scheduled for removal.
*
* First we set the handled flag of each active timer to zero,
* then we iterate over the list and execute the callbacks. As the
* list might have been changed (specially the next) from within
* the callback we have to start over again. Once every callback
* is dispatched we will remove the non-active from the list.
*
* TODO: If this is a performance issue we can poison a global
* variable in add_timer and del_timer and only then restart.
* The problematic scenario is the following: Given two timers A
* and B that have expired at the same time. Thus, they are both
* in the eviction list in this order: A, then B. If we remove
* timer B from the A's callback, we continue with B in the next
* iteration step, leading to an access-after-release.
*/
llist_for_each_entry(timer, &timer_list, entry) {
timer->handled = 0;
}
restart:
llist_for_each_entry(timer, &timer_list, entry) {
if (!timer->handled && TIME_SMALLER(timer->timeout, current_time)) {
timer->handled = 1;
timer->active = 0;
(*timer->cb)(timer->data);
work = 1;
goto restart;
}
}
llist_for_each_entry_safe(timer, tmp, &timer_list, entry) {
timer->handled = 0;
if (!timer->active) {
osmo_timer_del(timer);
}
llist_for_each_entry(this, &timer_eviction_list, list) {
osmo_timer_del(this);
this->cb(this->data);
work = 1;
goto restart;
}
return work;
@ -210,10 +223,10 @@ restart:
int osmo_timers_check(void)
{
struct osmo_timer_list *timer;
struct rb_node *node;
int i = 0;
llist_for_each_entry(timer, &timer_list, entry) {
for (node = rb_first(&timer_root); node; node = rb_next(node)) {
i++;
}
return i;

View File

@ -185,9 +185,10 @@ DEFUN(logging_level,
DEFUN(logging_set_category_mask,
logging_set_category_mask_cmd,
"logging set log mask MASK",
"logging set-log-mask MASK",
LOGGING_STR
"Decide which categories to output.\n")
"Set the logmask of this logging target\n"
"The logmask to use\n")
{
struct log_target *tgt = osmo_log_vty2tgt(vty);
@ -198,6 +199,14 @@ DEFUN(logging_set_category_mask,
return CMD_SUCCESS;
}
ALIAS_DEPRECATED(logging_set_category_mask,
logging_set_category_mask_old_cmd,
"logging set log mask MASK",
LOGGING_STR
"Decide which categories to output.\n"
"Log commands\n" "Mask commands\n" "The logmask to use\n");
DEFUN(diable_logging,
disable_logging_cmd,
"logging disable",
@ -381,7 +390,16 @@ static struct value_string sysl_level_names[] = {
DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
"log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
LOG_STR "Logging via syslog\n")
LOG_STR "Logging via syslog\n"
"Security/authorization messages facility\n"
"Clock daemon (cron/at) facility\n"
"General system daemon facility\n"
"Ftp daemon facility\n"
"Line printer facility\n"
"Mail facility\n"
"News facility\n"
"Generic facility\n"
"UUCP facility\n")
{
int facility = get_string_value(sysl_level_names, argv[0]);
@ -560,6 +578,7 @@ void logging_vty_add_cmds(const struct log_info *cat)
install_element_ve(&logging_use_clr_cmd);
install_element_ve(&logging_prnt_timestamp_cmd);
install_element_ve(&logging_set_category_mask_cmd);
install_element_ve(&logging_set_category_mask_old_cmd);
/* Logging level strings are generated dynamically. */
logging_level_cmd.string = log_vty_command_string(cat);

View File

@ -1,7 +1,11 @@
/*
* (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
* (C) 2011 by Harald Welte <laforge@gnumonks.org>
* All Rights Reserved
*
* Authors: Holger Hans Peter Freyther <zecke@selfish.org>
* Pablo Neira Ayuso <pablo@gnumonks.org>
*
* 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
@ -19,59 +23,130 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/select.h>
#include <osmocom/core/linuxlist.h>
#include "../../config.h"
static void timer_fired(void *data);
static void main_timer_fired(void *data);
static void secondary_timer_fired(void *data);
static struct osmo_timer_list timer_one = {
.cb = timer_fired,
.data = (void*)1,
static unsigned int main_timer_step = 0;
static struct osmo_timer_list main_timer = {
.cb = main_timer_fired,
.data = &main_timer_step,
};
static struct osmo_timer_list timer_two = {
.cb = timer_fired,
.data = (void*)2,
static LLIST_HEAD(timer_test_list);
struct test_timer {
struct llist_head head;
struct osmo_timer_list timer;
struct timeval start;
struct timeval stop;
};
static struct osmo_timer_list timer_three = {
.cb = timer_fired,
.data = (void*)3,
};
/* number of test steps. We add fact(steps) timers in the whole test. */
#define MAIN_TIMER_NSTEPS 16
static void timer_fired(void *_data)
/* time between two steps, in secs. */
#define TIME_BETWEEN_STEPS 1
/* timer imprecision that we accept for this test: 10 milliseconds. */
#define TIMER_PRES_SECS 0
#define TIMER_PRES_USECS 10000
static unsigned int expired_timers = 0;
static unsigned int total_timers = 0;
static unsigned int too_late = 0;
static void main_timer_fired(void *data)
{
unsigned long data = (unsigned long) _data;
printf("Fired timer: %lu\n", data);
unsigned int *step = data;
unsigned int add_in_this_step;
int i;
if (data == 1) {
osmo_timer_schedule(&timer_one, 3, 0);
osmo_timer_del(&timer_two);
} else if (data == 2) {
printf("Should not be fired... bug in del_timer\n");
} else if (data == 3) {
printf("Timer fired not registering again\n");
} else {
printf("wtf... wrong data\n");
}
if (*step == MAIN_TIMER_NSTEPS) {
printf("Main timer has finished, please, wait a bit for the "
"final report.\n");
return;
}
/* add 2^step pair of timers per step. */
add_in_this_step = (1 << *step);
for (i=0; i<add_in_this_step; i++) {
struct test_timer *v;
v = talloc_zero(NULL, struct test_timer);
if (v == NULL) {
fprintf(stderr, "timer_test: OOM!\n");
return;
}
gettimeofday(&v->start, NULL);
v->timer.cb = secondary_timer_fired;
v->timer.data = v;
unsigned int seconds = (random() % 10) + 1;
v->stop.tv_sec = v->start.tv_sec + seconds;
osmo_timer_schedule(&v->timer, seconds, 0);
llist_add(&v->head, &timer_test_list);
}
printf("added %d timers in step %u (expired=%u)\n",
add_in_this_step, *step, expired_timers);
total_timers += add_in_this_step;
osmo_timer_schedule(&main_timer, TIME_BETWEEN_STEPS, 0);
(*step)++;
}
static void secondary_timer_fired(void *data)
{
struct test_timer *v = data, *this, *tmp;
struct timeval current, res, precision = { 1, 0 };
gettimeofday(&current, NULL);
timersub(&current, &v->stop, &res);
if (timercmp(&res, &precision, >)) {
printf("ERROR: timer %p has expired too late!\n", v->timer);
too_late++;
}
llist_del(&v->head);
talloc_free(data);
expired_timers++;
if (expired_timers == total_timers) {
printf("test over: added=%u expired=%u too_late=%u \n",
total_timers, expired_timers, too_late);
exit(EXIT_SUCCESS);
}
/* randomly (10%) deletion of timers. */
llist_for_each_entry_safe(this, tmp, &timer_test_list, head) {
if ((random() % 100) < 10) {
osmo_timer_del(&this->timer);
llist_del(&this->head);
talloc_free(this);
expired_timers++;
}
}
}
int main(int argc, char** argv)
{
printf("Starting... timer\n");
printf("Running timer test for %u steps, accepting imprecision "
"of %u.%.6u seconds\n",
MAIN_TIMER_NSTEPS, TIMER_PRES_SECS, TIMER_PRES_USECS);
osmo_timer_schedule(&timer_one, 3, 0);
osmo_timer_schedule(&timer_two, 5, 0);
osmo_timer_schedule(&timer_three, 4, 0);
osmo_timer_schedule(&main_timer, 1, 0);
#ifdef HAVE_SYS_SELECT_H
while (1) {
osmo_select_main(0);
}
while (1) {
osmo_select_main(0);
}
#else
printf("Select not supported on this platform!\n");
printf("Select not supported on this platform!\n");
#endif
}