1 /* GSMTAP output for Osmocom Layer2 (will only work on the host PC) */
3 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "../config.h"
25 #ifdef HAVE_SYS_SELECT_H
27 #include <osmocore/gsmtap_util.h>
28 #include <osmocore/logging.h>
29 #include <osmocore/protocol/gsm_04_08.h>
30 #include <osmocore/gsmtap.h>
31 #include <osmocore/msgb.h>
32 #include <osmocore/rsl.h>
33 #include <osmocore/select.h>
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
45 static struct bsc_fd gsmtap_bfd = { .fd = -1 };
46 static LLIST_HEAD(gsmtap_txqueue);
48 uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
50 uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
52 switch (rsl_chantype) {
53 case RSL_CHAN_Bm_ACCHs:
54 ret = GSMTAP_CHANNEL_TCH_F;
56 case RSL_CHAN_Lm_ACCHs:
57 ret = GSMTAP_CHANNEL_TCH_H;
59 case RSL_CHAN_SDCCH4_ACCH:
60 ret = GSMTAP_CHANNEL_SDCCH4;
62 case RSL_CHAN_SDCCH8_ACCH:
63 ret = GSMTAP_CHANNEL_SDCCH8;
66 ret = GSMTAP_CHANNEL_BCCH;
69 ret = GSMTAP_CHANNEL_RACH;
71 case RSL_CHAN_PCH_AGCH:
72 /* it could also be AGCH... */
73 ret = GSMTAP_CHANNEL_PCH;
78 ret |= GSMTAP_CHANNEL_ACCH;
83 /* receive a message from L1/L2 and put it in GSMTAP */
84 struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
85 uint8_t ss, uint32_t fn, int8_t signal_dbm,
86 uint8_t snr, const uint8_t *data, unsigned int len)
89 struct gsmtap_hdr *gh;
92 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
96 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
98 gh->version = GSMTAP_VERSION;
99 gh->hdr_len = sizeof(*gh)/4;
100 gh->type = GSMTAP_TYPE_UM;
103 gh->arfcn = htons(arfcn);
105 gh->signal_dbm = signal_dbm;
106 gh->frame_number = htonl(fn);
107 gh->sub_type = chan_type;
110 dst = msgb_put(msg, len);
111 memcpy(dst, data, len);
116 /* receive a message from L1/L2 and put it in GSMTAP */
117 int gsmtap_sendmsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, uint8_t ss,
118 uint32_t fn, int8_t signal_dbm, uint8_t snr,
119 const uint8_t *data, unsigned int len)
123 /* gsmtap was never initialized, so don't try to send anything */
124 if (gsmtap_bfd.fd == -1)
127 msg = gsmtap_makemsg(arfcn, ts, chan_type, ss, fn, signal_dbm,
132 msgb_enqueue(&gsmtap_txqueue, msg);
133 gsmtap_bfd.when |= BSC_FD_WRITE;
138 /* Callback from select layer if we can write to the socket */
139 static int gsmtap_fd_cb(struct bsc_fd *fd, unsigned int flags)
144 if (!(flags & BSC_FD_WRITE))
147 msg = msgb_dequeue(&gsmtap_txqueue);
149 /* no more messages in the queue, disable READ cb */
153 rc = write(gsmtap_bfd.fd, msg->data, msg->len);
155 perror("writing msgb to gsmtap fd");
159 if (rc != msg->len) {
160 perror("short write to gsmtap fd");
169 int gsmtap_init(uint32_t dst_ip)
172 struct sockaddr_in sin;
174 sin.sin_family = AF_INET;
175 sin.sin_port = htons(GSMTAP_UDP_PORT);
176 sin.sin_addr.s_addr = htonl(dst_ip);
178 /* FIXME: create socket */
179 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
181 perror("creating UDP socket");
185 rc = connect(rc, (struct sockaddr *)&sin, sizeof(sin));
187 perror("connecting UDP socket");
188 close(gsmtap_bfd.fd);
193 gsmtap_bfd.when = BSC_FD_WRITE;
194 gsmtap_bfd.cb = gsmtap_fd_cb;
195 gsmtap_bfd.data = NULL;
197 return bsc_register_fd(&gsmtap_bfd);
200 #endif /* HAVE_SYS_SELECT_H */