2010-02-18 15:46:36 +00:00
|
|
|
/* Layer2 handling code... LAPD and stuff
|
|
|
|
*
|
|
|
|
* (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
|
2010-03-02 17:47:01 +00:00
|
|
|
* (C) 2010 by Harald Welte <laforge@gnumonks.org>
|
2010-02-18 15:46:36 +00:00
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <stdint.h>
|
2010-03-02 17:47:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2010-02-18 15:46:36 +00:00
|
|
|
#include <l1a_l23_interface.h>
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
#include <osmocore/timer.h>
|
|
|
|
#include <osmocore/msgb.h>
|
|
|
|
#include <osmocore/tlv.h>
|
|
|
|
#include <osmocore/protocol/gsm_04_08.h>
|
|
|
|
#include <osmocore/protocol/gsm_08_58.h>
|
|
|
|
|
2010-03-02 22:05:49 +00:00
|
|
|
#include <osmocom/layer2.h>
|
2010-03-02 17:47:01 +00:00
|
|
|
#include <osmocom/osmocom_data.h>
|
|
|
|
#include <osmocom/lapdm.h>
|
|
|
|
#include <osmocom/debug.h>
|
|
|
|
|
2010-02-19 21:43:51 +00:00
|
|
|
#include "gsmtap_util.h"
|
2010-02-18 15:46:36 +00:00
|
|
|
|
|
|
|
static struct msgb *osmo_l1_alloc(uint8_t msg_type)
|
|
|
|
{
|
2010-03-02 17:47:01 +00:00
|
|
|
struct l1ctl_info_ul *ul;
|
2010-02-18 15:46:36 +00:00
|
|
|
struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1");
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
fprintf(stderr, "Failed to allocate memory.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
msg->l1h = msgb_put(msg, sizeof(*ul));
|
|
|
|
ul = (struct l1ctl_info_ul *) msg->l1h;
|
2010-02-18 15:46:36 +00:00
|
|
|
ul->msg_type = msg_type;
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
|
2010-02-18 15:46:36 +00:00
|
|
|
static int osmo_make_band_arfcn(struct osmocom_ms *ms)
|
|
|
|
{
|
|
|
|
/* TODO: Include the band */
|
|
|
|
return ms->arfcn;
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
static int rx_l1_ccch_resp(struct osmocom_ms *ms, struct msgb *msg)
|
2010-02-18 15:46:36 +00:00
|
|
|
{
|
2010-03-02 17:47:01 +00:00
|
|
|
struct l1ctl_info_dl *dl;
|
|
|
|
struct l1ctl_sync_new_ccch_resp *sb;
|
2010-02-18 15:46:36 +00:00
|
|
|
|
|
|
|
if (msgb_l3len(msg) < sizeof(*sb)) {
|
|
|
|
fprintf(stderr, "MSG too short for CCCH RESP: %u\n", msgb_l3len(msg));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
dl = (struct l1ctl_info_dl *) msg->l1h;
|
|
|
|
sb = (struct l1ctl_sync_new_ccch_resp *) msg->l2h;
|
2010-02-18 15:46:36 +00:00
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
printf("SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n",
|
2010-02-18 15:46:36 +00:00
|
|
|
dl->snr[0], dl->time.t1, dl->time.t2, dl->time.t3, sb->bsic);
|
2010-03-01 10:08:38 +00:00
|
|
|
|
2010-02-18 15:46:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-01 10:08:38 +00:00
|
|
|
char *chan_nr2string(uint8_t chan_nr)
|
|
|
|
{
|
|
|
|
static char str[20];
|
|
|
|
uint8_t cbits = chan_nr >> 3;
|
|
|
|
|
|
|
|
str[0] = '\0';
|
|
|
|
|
|
|
|
if (cbits == 0x01)
|
|
|
|
sprintf(str, "TCH/F");
|
|
|
|
else if ((cbits & 0x1e) == 0x02)
|
|
|
|
sprintf(str, "TCH/H(%u)", cbits & 0x01);
|
|
|
|
else if ((cbits & 0x1c) == 0x04)
|
|
|
|
sprintf(str, "SDCCH/4(%u)", cbits & 0x03);
|
|
|
|
else if ((cbits & 0x18) == 0x08)
|
|
|
|
sprintf(str, "SDCCH/8(%u)", cbits & 0x07);
|
|
|
|
else if (cbits == 0x10)
|
|
|
|
sprintf(str, "BCCH");
|
|
|
|
else if (cbits == 0x11)
|
|
|
|
sprintf(str, "RACH");
|
|
|
|
else if (cbits == 0x12)
|
|
|
|
sprintf(str, "PCH/AGCH");
|
|
|
|
else
|
|
|
|
sprintf(str, "UNKNOWN");
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
/* Data Indication from L1 */
|
|
|
|
static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
|
2010-02-18 15:46:36 +00:00
|
|
|
{
|
2010-03-02 17:47:01 +00:00
|
|
|
struct l1ctl_info_dl *dl, dl_cpy;
|
|
|
|
struct l1ctl_data_ind *ccch;
|
|
|
|
struct lapdm_entity *le;
|
2010-02-18 15:46:36 +00:00
|
|
|
|
|
|
|
if (msgb_l3len(msg) < sizeof(*ccch)) {
|
2010-03-02 17:47:01 +00:00
|
|
|
fprintf(stderr, "MSG too short Data Ind: %u\n", msgb_l3len(msg));
|
2010-02-18 15:46:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
dl = (struct l1ctl_info_dl *) msg->l1h;
|
|
|
|
ccch = (struct l1ctl_data_ind *) msg->l2h;
|
2010-03-03 13:00:50 +00:00
|
|
|
printf("%s (%.4u/%.2u/%.2u) %s\n",
|
2010-03-01 10:08:38 +00:00
|
|
|
chan_nr2string(dl->chan_nr), dl->time.t1, dl->time.t2,
|
2010-03-03 13:00:50 +00:00
|
|
|
dl->time.t3, hexdump(ccch->data, sizeof(ccch->data)));
|
2010-02-18 15:46:36 +00:00
|
|
|
|
2010-02-19 21:43:51 +00:00
|
|
|
/* send CCCH data via GSMTAP */
|
2010-03-01 10:08:38 +00:00
|
|
|
gsmtap_sendmsg(dl->chan_nr & 0x07, dl->band_arfcn, dl->time.fn, ccch->data,
|
2010-02-19 21:43:51 +00:00
|
|
|
sizeof(ccch->data));
|
2010-03-02 17:47:01 +00:00
|
|
|
|
|
|
|
/* determine LAPDm entity based on SACCH or not */
|
|
|
|
if (dl->link_id & 0x40)
|
|
|
|
le = &ms->lapdm_acch;
|
|
|
|
else
|
|
|
|
le = &ms->lapdm_dcch;
|
|
|
|
/* make local stack copy of l1ctl_info_dl, as LAPDm will overwrite skb hdr */
|
|
|
|
memcpy(&dl_cpy, dl, sizeof(dl_cpy));
|
|
|
|
|
|
|
|
/* pull the L1 header from the msgb */
|
|
|
|
msgb_pull(msg, msg->l2h - msg->l1h);
|
|
|
|
msg->l1h = NULL;
|
|
|
|
|
|
|
|
/* send it up into LAPDm */
|
|
|
|
l2_ph_data_ind(msg, le, &dl_cpy);
|
|
|
|
|
2010-02-18 15:46:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg,
|
|
|
|
uint8_t chan_nr, uint8_t link_id)
|
|
|
|
{
|
|
|
|
struct l1ctl_info_ul *l1i_ul;
|
|
|
|
|
2010-03-03 13:00:50 +00:00
|
|
|
printf("tx_ph_data_req(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
|
|
|
|
|
|
|
|
if (msgb_l2len(msg) > 23) {
|
|
|
|
printf("L1 cannot handle message length > 23 (%u)\n", msgb_l2len(msg));
|
|
|
|
msgb_free(msg);
|
|
|
|
return -EINVAL;
|
|
|
|
} else if (msgb_l2len(msg) < 23)
|
|
|
|
printf("L1 message length < 23 (%u) doesn't seem right!\n", msgb_l2len(msg));
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
/* prepend uplink info header */
|
2010-03-03 13:00:50 +00:00
|
|
|
printf("sizeof(struct l1ctl_info_ul)=%lu\n", sizeof(*l1i_ul));
|
|
|
|
msg->l1h = msgb_push(msg, sizeof(*l1i_ul));
|
|
|
|
l1i_ul = (struct l1ctl_info_ul *) msg->l1h;
|
2010-03-02 17:47:01 +00:00
|
|
|
|
|
|
|
l1i_ul->msg_type = L1CTL_DATA_REQ;
|
|
|
|
|
|
|
|
l1i_ul->chan_nr = chan_nr;
|
|
|
|
l1i_ul->link_id = link_id;
|
|
|
|
|
|
|
|
/* FIXME: where to get this from? */
|
|
|
|
l1i_ul->tx_power = 0;
|
|
|
|
|
|
|
|
return osmo_send_l1(ms, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rx_l1_reset(struct osmocom_ms *ms)
|
2010-02-18 15:46:36 +00:00
|
|
|
{
|
|
|
|
struct msgb *msg;
|
2010-03-02 17:47:01 +00:00
|
|
|
struct l1ctl_sync_new_ccch_req *req;
|
2010-02-18 15:46:36 +00:00
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
msg = osmo_l1_alloc(L1CTL_NEW_CCCH_REQ);
|
2010-02-18 15:46:36 +00:00
|
|
|
if (!msg)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf("Layer1 Reset.\n");
|
2010-03-02 17:47:01 +00:00
|
|
|
req = (struct l1ctl_sync_new_ccch_req *) msgb_put(msg, sizeof(*req));
|
2010-02-18 15:46:36 +00:00
|
|
|
req->band_arfcn = osmo_make_band_arfcn(ms);
|
|
|
|
|
|
|
|
return osmo_send_l1(ms, msg);
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
int tx_ph_rach_req(struct osmocom_ms *ms)
|
2010-03-01 10:08:38 +00:00
|
|
|
{
|
|
|
|
struct msgb *msg;
|
2010-03-02 17:47:01 +00:00
|
|
|
struct l1ctl_rach_req *req;
|
2010-03-01 10:08:38 +00:00
|
|
|
static uint8_t i = 0;
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
msg = osmo_l1_alloc(L1CTL_RACH_REQ);
|
2010-03-01 10:08:38 +00:00
|
|
|
if (!msg)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf("RACH Req.\n");
|
2010-03-02 17:47:01 +00:00
|
|
|
req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
|
2010-03-01 10:08:38 +00:00
|
|
|
req->ra = i++;
|
|
|
|
|
|
|
|
return osmo_send_l1(ms, msg);
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr)
|
|
|
|
{
|
|
|
|
struct msgb *msg;
|
|
|
|
struct l1ctl_info_ul *ul;
|
|
|
|
struct l1ctl_dm_est_req *req;
|
|
|
|
|
|
|
|
msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
|
|
|
|
if (!msg)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf("Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n",
|
|
|
|
band_arfcn, chan_nr);
|
2010-03-03 13:00:50 +00:00
|
|
|
ul = (struct l1ctl_info_ul *) msg->l1h;
|
2010-03-02 17:47:01 +00:00
|
|
|
ul->chan_nr = chan_nr;
|
|
|
|
ul->link_id = 0;
|
|
|
|
ul->tx_power = 0; /* FIXME: initial TX power */
|
|
|
|
req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
|
|
|
|
req->band_arfcn = band_arfcn;
|
|
|
|
|
|
|
|
return osmo_send_l1(ms, msg);
|
|
|
|
|
|
|
|
}
|
2010-02-18 15:46:36 +00:00
|
|
|
|
|
|
|
int osmo_recv(struct osmocom_ms *ms, struct msgb *msg)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
2010-03-02 17:47:01 +00:00
|
|
|
struct l1ctl_info_dl *dl;
|
2010-02-18 15:46:36 +00:00
|
|
|
|
|
|
|
if (msgb_l2len(msg) < sizeof(*dl)) {
|
|
|
|
fprintf(stderr, "Short Layer2 message: %u\n", msgb_l2len(msg));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-02 17:47:01 +00:00
|
|
|
dl = (struct l1ctl_info_dl *) msg->l1h;
|
|
|
|
msg->l2h = &msg->l1h[0] + sizeof(*dl);
|
2010-02-18 15:46:36 +00:00
|
|
|
|
|
|
|
switch (dl->msg_type) {
|
2010-03-02 17:47:01 +00:00
|
|
|
case L1CTL_NEW_CCCH_RESP:
|
|
|
|
rc = rx_l1_ccch_resp(ms, msg);
|
2010-02-18 15:46:36 +00:00
|
|
|
break;
|
2010-03-02 17:47:01 +00:00
|
|
|
case L1CTL_DATA_IND:
|
|
|
|
rc = rx_ph_data_ind(ms, msg);
|
2010-02-18 15:46:36 +00:00
|
|
|
break;
|
2010-03-02 17:47:01 +00:00
|
|
|
case L1CTL_RESET:
|
|
|
|
rc = rx_l1_reset(ms);
|
2010-02-18 15:46:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown MSG: %u\n", dl->msg_type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|