blob: 5da436906e546cbc6ef9c178997cb76392a3276b [file] [log] [blame]
Harald Weltee779c362010-06-29 20:51:13 +02001/* GSMTAP output for Osmocom Layer2 (will only work on the host PC) */
2/*
3 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
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.
11 *
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.
16 *
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.
20 *
21 */
22
23#include "../config.h"
24
25#ifdef HAVE_SYS_SELECT_H
26
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>
34
35#include <arpa/inet.h>
36#include <sys/socket.h>
37#include <netinet/in.h>
38
39#include <stdio.h>
40#include <unistd.h>
41#include <stdint.h>
42#include <string.h>
43#include <errno.h>
44
45static struct bsc_fd gsmtap_bfd = { .fd = -1 };
46static LLIST_HEAD(gsmtap_txqueue);
47
48uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
49{
50 uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
51
52 switch (rsl_chantype) {
53 case RSL_CHAN_Bm_ACCHs:
54 ret = GSMTAP_CHANNEL_TCH_F;
55 break;
56 case RSL_CHAN_Lm_ACCHs:
57 ret = GSMTAP_CHANNEL_TCH_H;
58 break;
59 case RSL_CHAN_SDCCH4_ACCH:
60 ret = GSMTAP_CHANNEL_SDCCH4;
61 break;
62 case RSL_CHAN_SDCCH8_ACCH:
63 ret = GSMTAP_CHANNEL_SDCCH8;
64 break;
65 case RSL_CHAN_BCCH:
66 ret = GSMTAP_CHANNEL_BCCH;
67 break;
68 case RSL_CHAN_RACH:
69 ret = GSMTAP_CHANNEL_RACH;
70 break;
71 case RSL_CHAN_PCH_AGCH:
72 /* it could also be AGCH... */
73 ret = GSMTAP_CHANNEL_PCH;
74 break;
75 }
76
77 if (link_id & 0x40)
78 ret |= GSMTAP_CHANNEL_ACCH;
79
80 return ret;
81}
82
83/* receive a message from L1/L2 and put it in GSMTAP */
84int gsmtap_sendmsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, uint8_t ss,
85 uint32_t fn, int8_t signal_dbm, uint8_t snr,
86 const uint8_t *data, unsigned int len)
87{
88 struct msgb *msg;
89 struct gsmtap_hdr *gh;
90 uint8_t *dst;
91
92 /* gsmtap was never initialized, so don't try to send anything */
93 if (gsmtap_bfd.fd == -1)
94 return 0;
95
96 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
97 if (!msg)
98 return -ENOMEM;
99
100 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
101
102 gh->version = GSMTAP_VERSION;
103 gh->hdr_len = sizeof(*gh)/4;
104 gh->type = GSMTAP_TYPE_UM;
105 gh->timeslot = ts;
106 gh->sub_slot = ss;
107 gh->arfcn = htons(arfcn);
108 gh->snr_db = snr;
109 gh->signal_dbm = signal_dbm;
110 gh->frame_number = htonl(fn);
111 gh->sub_type = chan_type;
112 gh->antenna_nr = 0;
113
114 dst = msgb_put(msg, len);
115 memcpy(dst, data, len);
116
117 msgb_enqueue(&gsmtap_txqueue, msg);
118 gsmtap_bfd.when |= BSC_FD_WRITE;
119
120 return 0;
121}
122
123/* Callback from select layer if we can write to the socket */
124static int gsmtap_fd_cb(struct bsc_fd *fd, unsigned int flags)
125{
126 struct msgb *msg;
127 int rc;
128
129 if (!(flags & BSC_FD_WRITE))
130 return 0;
131
132 msg = msgb_dequeue(&gsmtap_txqueue);
133 if (!msg) {
134 /* no more messages in the queue, disable READ cb */
135 gsmtap_bfd.when = 0;
136 return 0;
137 }
138 rc = write(gsmtap_bfd.fd, msg->data, msg->len);
139 if (rc < 0) {
140 perror("writing msgb to gsmtap fd");
141 msgb_free(msg);
142 return rc;
143 }
144 if (rc != msg->len) {
145 perror("short write to gsmtap fd");
146 msgb_free(msg);
147 return -EIO;
148 }
149
150 msgb_free(msg);
151 return 0;
152}
153
154int gsmtap_init(uint32_t dst_ip)
155{
156 int rc;
157 struct sockaddr_in sin;
158
159 sin.sin_family = AF_INET;
160 sin.sin_port = htons(GSMTAP_UDP_PORT);
161 sin.sin_addr.s_addr = htonl(dst_ip);
162
163 /* FIXME: create socket */
164 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
165 if (rc < 0) {
166 perror("creating UDP socket");
167 return rc;
168 }
169 gsmtap_bfd.fd = rc;
170 rc = connect(rc, (struct sockaddr *)&sin, sizeof(sin));
171 if (rc < 0) {
172 perror("connecting UDP socket");
173 close(gsmtap_bfd.fd);
174 gsmtap_bfd.fd = 0;
175 return rc;
176 }
177
178 gsmtap_bfd.when = BSC_FD_WRITE;
179 gsmtap_bfd.cb = gsmtap_fd_cb;
180 gsmtap_bfd.data = NULL;
181
182 return bsc_register_fd(&gsmtap_bfd);
183}
184
185#endif /* HAVE_SYS_SELECT_H */