diff options
author | Harald Welte <laforge@gnumonks.org> | 2010-06-29 20:51:13 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2010-06-29 21:57:25 +0200 |
commit | e779c364ac7eb1f6bcf42e507972bc3322e6f908 (patch) | |
tree | d91a9169d722eeefed82d4cc48e39fed073e3bd6 | |
parent | 1067e8cc9a5d4f28f986f7473093cbac874d4208 (diff) |
Add gsmtap utility functions to libosmocore
This is imported from OsmocomBB/Layer23
-rw-r--r-- | include/osmocore/Makefile.am | 2 | ||||
-rw-r--r-- | include/osmocore/gsmtap_util.h | 16 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/gsmtap_util.c | 185 |
4 files changed, 203 insertions, 2 deletions
diff --git a/include/osmocore/Makefile.am b/include/osmocore/Makefile.am index fde01027..647b5c00 100644 --- a/include/osmocore/Makefile.am +++ b/include/osmocore/Makefile.am @@ -1,7 +1,7 @@ osmocore_HEADERS = signal.h linuxlist.h timer.h select.h msgb.h \ tlv.h bitvec.h comp128.h statistics.h gsm_utils.h utils.h \ gsmtap.h write_queue.h rsl.h gsm48.h rxlev_stat.h mncc.h \ - gsm48_ie.h logging.h gsm0808.h rate_ctr.h + gsm48_ie.h logging.h gsm0808.h rate_ctr.h gsmtap_util.h if ENABLE_TALLOC osmocore_HEADERS += talloc.h diff --git a/include/osmocore/gsmtap_util.h b/include/osmocore/gsmtap_util.h new file mode 100644 index 00000000..30fc1267 --- /dev/null +++ b/include/osmocore/gsmtap_util.h @@ -0,0 +1,16 @@ +#ifndef _GSMTAP_UTIL_H +#define _GSMTAP_UTIL_H + +#include <stdint.h> + +/* convert RSL channel number to GSMTAP channel type */ +uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t rsl_link_id); + +/* receive a message from L1/L2 and put it in GSMTAP */ +int gsmtap_sendmsg(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_init(uint32_t dst_ip); + +#endif /* _GSMTAP_UTIL_H */ diff --git a/src/Makefile.am b/src/Makefile.am index 1a7d87f3..dc2309a0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,7 @@ lib_LTLIBRARIES = libosmocore.la libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c rxlev_stat.c \ tlv_parser.c bitvec.c comp128.c gsm_utils.c statistics.c \ write_queue.c utils.c rsl.c gsm48.c gsm48_ie.c \ - logging.c gsm0808.c rate_ctr.c + logging.c gsm0808.c rate_ctr.c gsmtap_util.c if ENABLE_TALLOC libosmocore_la_SOURCES += talloc.c diff --git a/src/gsmtap_util.c b/src/gsmtap_util.c new file mode 100644 index 00000000..5da43690 --- /dev/null +++ b/src/gsmtap_util.c @@ -0,0 +1,185 @@ +/* GSMTAP output for Osmocom Layer2 (will only work on the host PC) */ +/* + * (C) 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 "../config.h" + +#ifdef HAVE_SYS_SELECT_H + +#include <osmocore/gsmtap_util.h> +#include <osmocore/logging.h> +#include <osmocore/protocol/gsm_04_08.h> +#include <osmocore/gsmtap.h> +#include <osmocore/msgb.h> +#include <osmocore/rsl.h> +#include <osmocore/select.h> + +#include <arpa/inet.h> +#include <sys/socket.h> +#include <netinet/in.h> + +#include <stdio.h> +#include <unistd.h> +#include <stdint.h> +#include <string.h> +#include <errno.h> + +static struct bsc_fd gsmtap_bfd = { .fd = -1 }; +static LLIST_HEAD(gsmtap_txqueue); + +uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id) +{ + uint8_t ret = GSMTAP_CHANNEL_UNKNOWN; + + switch (rsl_chantype) { + case RSL_CHAN_Bm_ACCHs: + ret = GSMTAP_CHANNEL_TCH_F; + break; + case RSL_CHAN_Lm_ACCHs: + ret = GSMTAP_CHANNEL_TCH_H; + break; + case RSL_CHAN_SDCCH4_ACCH: + ret = GSMTAP_CHANNEL_SDCCH4; + break; + case RSL_CHAN_SDCCH8_ACCH: + ret = GSMTAP_CHANNEL_SDCCH8; + break; + case RSL_CHAN_BCCH: + ret = GSMTAP_CHANNEL_BCCH; + break; + case RSL_CHAN_RACH: + ret = GSMTAP_CHANNEL_RACH; + break; + case RSL_CHAN_PCH_AGCH: + /* it could also be AGCH... */ + ret = GSMTAP_CHANNEL_PCH; + break; + } + + if (link_id & 0x40) + ret |= GSMTAP_CHANNEL_ACCH; + + return ret; +} + +/* receive a message from L1/L2 and put it in GSMTAP */ +int gsmtap_sendmsg(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; + + /* gsmtap was never initialized, so don't try to send anything */ + if (gsmtap_bfd.fd == -1) + return 0; + + msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx"); + if (!msg) + return -ENOMEM; + + 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); + + msgb_enqueue(&gsmtap_txqueue, msg); + gsmtap_bfd.when |= BSC_FD_WRITE; + + return 0; +} + +/* Callback from select layer if we can write to the socket */ +static int gsmtap_fd_cb(struct bsc_fd *fd, unsigned int flags) +{ + struct msgb *msg; + int rc; + + if (!(flags & BSC_FD_WRITE)) + return 0; + + msg = msgb_dequeue(&gsmtap_txqueue); + if (!msg) { + /* no more messages in the queue, disable READ cb */ + gsmtap_bfd.when = 0; + return 0; + } + rc = write(gsmtap_bfd.fd, msg->data, msg->len); + if (rc < 0) { + perror("writing msgb to gsmtap fd"); + msgb_free(msg); + return rc; + } + if (rc != msg->len) { + perror("short write to gsmtap fd"); + msgb_free(msg); + return -EIO; + } + + msgb_free(msg); + return 0; +} + +int gsmtap_init(uint32_t dst_ip) +{ + int rc; + struct sockaddr_in sin; + + sin.sin_family = AF_INET; + sin.sin_port = htons(GSMTAP_UDP_PORT); + sin.sin_addr.s_addr = htonl(dst_ip); + + /* FIXME: create socket */ + rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (rc < 0) { + perror("creating UDP socket"); + return rc; + } + gsmtap_bfd.fd = rc; + rc = connect(rc, (struct sockaddr *)&sin, sizeof(sin)); + if (rc < 0) { + perror("connecting UDP socket"); + close(gsmtap_bfd.fd); + gsmtap_bfd.fd = 0; + return rc; + } + + gsmtap_bfd.when = BSC_FD_WRITE; + gsmtap_bfd.cb = gsmtap_fd_cb; + gsmtap_bfd.data = NULL; + + return bsc_register_fd(&gsmtap_bfd); +} + +#endif /* HAVE_SYS_SELECT_H */ |