Major update: Start L2/L3 implementation on PC side
authorHarald Welte <laforge@gnumonks.org>
Mon, 1 Mar 2010 22:54:32 +0000 (23:54 +0100)
committerHarald Welte <laforge@gnumonks.org>
Mon, 1 Mar 2010 22:54:32 +0000 (23:54 +0100)
Using the following changes, it is now possible to receive the PCH and AGCH
messages in the PC-side layer3, as well as trigger RACH sending inside the phone
from the PC:
* merge l1_dedic_mode_data_ind, l1_dedic_mode_data_req and l1_ccch_info_ind into l1_data_ind
* add partial LAPDm implementation (layer2/src/lapdm.c)
* introduce RSLms between LAPDm and L3 (layer2/src/osmocom_rslms.c)
* use new layer1 header field of msgb
* tx_ph_rach_req() and tx_ph_data_req() to send data from PC to target
* implement DEDIC_MODE_DATA_REQ on firmware side

src/host/layer2/include/osmocom/lapdm.h [new file with mode: 0644]
src/host/layer2/src/lapdm.c [new file with mode: 0644]
src/host/layer2/src/osmocom_rslms.c [new file with mode: 0644]

diff --git a/src/host/layer2/include/osmocom/lapdm.h b/src/host/layer2/include/osmocom/lapdm.h
new file mode 100644 (file)
index 0000000..14b26e5
--- /dev/null
@@ -0,0 +1,60 @@
+#ifndef _OSMOCOM_LAPDM_H
+#define _OSMOCOM_LAPDM_H
+
+#include <stdint.h>
+
+#include <osmocore/timer.h>
+#include <osmocore/msgb.h>
+
+#include <l1a_l23_interface.h>
+
+enum lapdm_state {
+       LAPDm_STATE_NULL,
+       LAPDm_STATE_IDLE,
+       LAPDm_STATE_SABM_SENT,
+       LAPDm_STATE_MF_EST,
+       LAPDm_STATE_TIMER_RECOV,
+       LAPDm_STATE_OWN_RCVR_BUSY,
+};
+
+struct lapdm_entity;
+struct osmocom_ms;
+
+/* TS 04.06 / Section 3.5.2 */
+struct lapdm_datalink {
+       uint8_t V_send; /* seq nr of next I frame to be transmitted */
+       uint8_t V_ack;  /* last frame ACKed by peer */
+       uint8_t N_send; /* ? set to V_send at Tx time*/
+       uint8_t V_recv; /* seq nr of next I frame expected to be received */
+       uint8_t N_recv; /* expected send seq nr of the next received I frame */
+       enum lapdm_state state;
+       struct timer_list t200;
+       uint8_t retrans_ctr;
+
+       struct lapdm_entity *entity;
+};
+
+enum lapdm_dl_sapi {
+       DL_SAPI0        = 0,
+       DL_SAPI3        = 1,
+       _NR_DL_SAPI
+};
+
+struct lapdm_entity {
+       struct lapdm_datalink datalink[_NR_DL_SAPI];
+       struct osmocom_ms *ms;
+};
+
+/* initialize a LAPDm entity */
+void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms);
+
+/* input into layer2 (from layer 1) */
+int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1_info_dl *l1i);
+
+/* input into layer2 (from layer 3) */
+int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms);
+
+/* sending messages up from L2 to L3 */
+int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms);
+
+#endif /* _OSMOCOM_LAPDM_H */
diff --git a/src/host/layer2/src/lapdm.c b/src/host/layer2/src/lapdm.c
new file mode 100644 (file)
index 0000000..d15179a
--- /dev/null
@@ -0,0 +1,823 @@
+/* GSM LAPDm (TS 04.06) implementation */
+
+/* (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 <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+
+#include <osmocore/timer.h>
+#include <osmocore/msgb.h>
+#include <osmocore/tlv.h>
+#include <osmocore/utils.h>
+#include <osmocore/rsl.h>
+#include <osmocore/protocol/gsm_04_08.h>
+#include <osmocore/protocol/gsm_08_58.h>
+
+#include <osmocom/debug.h>
+#include <osmocom/osmocom_data.h>
+#include <osmocom/osmocom_layer2.h>
+#include <osmocom/lapdm.h>
+
+#include <l1a_l23_interface.h>
+
+/* TS 04.06 Figure 4 / Section 3.2 */
+#define LAPDm_LPD_NORMAL  0
+#define LAPDm_LPD_SMSCB          1
+#define LAPDm_SAPI_NORMAL 0
+#define LAPDm_SAPI_SMS   3
+#define LAPDm_ADDR(lpd, sapi, cr) (((lpd & 0x3) << 5) | ((sapi & 0x7) << 2) | ((cr & 0x1) << 1) | 0x1)
+
+#define LAPDm_ADDR_SAPI(addr) ((addr >> 2) & 0x7)
+
+/* TS 04.06 Table 3 / Section 3.4.3 */
+#define LAPDm_CTRL_I(nr, ns, p)        (((nr & 0x7) << 5) | ((p & 0x1) << 4) | ((ns & 0x7) << 1))
+#define LAPDm_CTRL_S(nr, s, p) (((nr & 0x7) << 5) | ((p & 0x1) << 4) | ((s & 0x3) << 2) | 0x1)
+#define LAPDm_CTRL_U(u, p)     (((u & 0x1c) << 5) | ((p & 0x1) << 4) | ((u & 0x3) << 2) | 0x3)
+
+#define LAPDm_CTRL_is_I(ctrl)  ((ctrl & 0x1) == 0)
+#define LAPDm_CTRL_is_S(ctrl)  ((ctrl & 0x3) == 1)
+#define LAPDm_CTRL_is_U(ctrl)  ((ctrl & 0x3) == 3)
+
+#define LAPDm_CTRL_U_BITS(ctrl)        (((ctrl & 0xC) >> 2) | (ctrl & 0xE) >> 3)
+#define LAPDm_CTRL_PF_BIT(ctrl)        ((ctrl >> 4) & 0x1)
+
+#define LAPDm_CTRL_S_BITS(ctrl)        ((ctrl & 0xC) >> 2)
+
+#define LAPDm_CTRL_I_Ns(ctrl)  ((ctrl & 0xE) >> 1)
+#define LAPDm_CTRL_I_Nr(ctrl)  ((ctrl & 0xE0) >> 5)
+
+/* TS 04.06 Table 4 / Section 3.8.1 */
+#define LAPDm_U_SABM   0x7
+#define LAPDm_U_DM     0x3
+#define LAPDm_U_UI     0x0
+#define LAPDm_U_DISC   0x8
+#define LAPDm_U_UA     0xC
+
+#define LAPDm_S_RR     0x0
+#define LAPDm_S_RNR    0x1
+#define LAPDm_S_REJ    0x2
+
+#define LAPDm_LEN(len) ((len << 2) | 0x1)
+
+/* TS 04.06 Section 5.8.3 */
+#define N201_AB_SACCH          18
+#define N201_AB_SDCCH          20
+#define N201_AB_FACCH          20
+#define N201_Bbis              23
+#define N201_Bter_SACCH                21
+#define N201_Bter_SDCCH                23
+#define N201_Bter_FACCH                23
+#define N201_B4                        19
+
+/* 5.8.2.1 N200 during establish and release */
+#define N200_EST_REL           5
+/* 5.8.2.1 N200 during timer recovery state */
+#define N200_TR_SACCH          5
+#define N200_TR_SDCCH          23
+#define N200_TR_FACCH_FR       34
+#define N200_TR_EFACCH_FR      48
+#define N200_TR_FACCH_HR       29
+/* FIXME: this depends on chan type */
+#define N200   N200_TR_SACCH
+
+#define CR_MS2BS_CMD   0
+#define CR_MS2BS_RESP  1
+#define CR_BS2MS_CMD   1
+#define CR_BS2MS_RESP  0
+
+/* Set T200 to 1 Second (OpenBTS uses 900ms) */
+#define T200   1, 0
+
+enum lapdm_format {
+       LAPDm_FMT_A,
+       LAPDm_FMT_B,
+       LAPDm_FMT_Bbis,
+       LAPDm_FMT_Bter,
+       LAPDm_FMT_B4,
+};
+
+struct lapdm_msg_ctx {
+       struct lapdm_datalink *dl;
+       enum lapdm_format lapdm_fmt;
+       uint8_t chan_nr;
+       uint8_t link_id;
+       uint8_t addr;
+       uint8_t ctrl;
+};
+
+static void lapdm_t200_cb(void *data);
+
+/* UTILITY FUNCTIONS */
+
+static inline uint8_t inc_mod8(uint8_t x)
+{
+       return (x + 1) % 8;
+}
+
+static void lapdm_dl_init(struct lapdm_datalink *dl,
+                         struct lapdm_entity *entity)
+{
+       memset(dl, 0, sizeof(*dl));
+       dl->t200.data = dl;
+       dl->t200.cb = &lapdm_t200_cb;
+       dl->entity = entity;
+}
+
+void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms)
+{
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
+               lapdm_dl_init(&le->datalink[i], le);
+
+       le->ms = ms;
+}
+
+static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
+{
+       switch (sapi) {
+       case LAPDm_SAPI_NORMAL:
+               return &le->datalink[0];
+       case LAPDm_SAPI_SMS:
+               return &le->datalink[1];
+       default:
+               return NULL;
+       }
+}
+
+/* remove the L2 header from a MSGB */
+static inline unsigned char *msgb_pull_l2h(struct msgb *msg)
+{
+       unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h);
+       msg->l2h = NULL;
+       return ret;
+}
+
+/* Take a Bbis format message from L1 and create RSLms UNIT DATA IND */
+static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
+                            struct msgb *msg)
+{
+       uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
+       struct abis_rsl_rll_hdr *rh;
+
+       /* construct a RSLms RLL message (DATA INDICATION, UNIT DATA
+        * INDICATION) and send it off via RSLms */
+
+       /* Push the L3 IE tag and lengh */
+       msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
+
+       /* Then push the RSL header */
+       rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
+       rsl_init_rll_hdr(rh, msg_type);
+       rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
+       rh->chan_nr = mctx->chan_nr;
+       rh->link_id = mctx->link_id;
+
+       /* set the l2 header pointer */
+       msg->l2h = (uint8_t *)rh;
+
+       /* send off the RSLms message to L3 */
+       return rslms_sendmsg(msg, mctx->dl->entity->ms);
+}
+
+static int send_rslms_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
+{
+       struct abis_rsl_rll_hdr *rh;
+       struct msgb *msg = msgb_alloc(sizeof(*rh), "rslms_rll_simple");
+
+       /* put the RSL header */
+       rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
+       rsl_init_rll_hdr(rh, msg_type);
+       rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
+       rh->chan_nr = mctx->chan_nr;
+       rh->link_id = mctx->link_id;
+
+       /* set the l2 header pointer */
+       msg->l2h = (uint8_t *)rh;
+
+       /* send off the RSLms message to L3 */
+       return rslms_sendmsg(msg, mctx->dl->entity->ms);
+}
+
+static int check_length_ind(uint8_t length_ind)
+{
+       if (!(length_ind & 0x01)) {
+               /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION
+                * primitive with cause "frame not implemented" is sent to the
+                * mobile management entity. */
+               printf("we don't support multi-octet length\n");
+               return -EINVAL;
+       }
+       if (length_ind & 0x02) {
+               printf("we don't support LAPDm fragmentation yet\n");
+               return -EINVAL;
+       }
+       return 0;
+}
+
+/* Timer callback on T200 expiry */
+static void lapdm_t200_cb(void *data)
+{
+       struct lapdm_datalink *dl = data;
+
+       switch (dl->state) {
+       case LAPDm_STATE_SABM_SENT:
+               /* 5.4.1.3 */
+               if (dl->retrans_ctr >= N200_EST_REL + 1) {
+                       /* FIXME: send RELEASE INDICATION to L3 */
+                       dl->retrans_ctr = 0;
+                       dl->state = LAPDm_STATE_IDLE;
+               }
+               /* FIXME: retransmit SABM command */
+
+               /* increment re-transmission counter */
+               dl->retrans_ctr++;
+               /* restart T200 (PH-READY-TO-SEND) */
+               bsc_schedule_timer(&dl->t200, T200);
+               break;
+       case LAPDm_STATE_MF_EST:
+               /* 5.5.7 */
+               dl->retrans_ctr = 0;
+               dl->state = LAPDm_STATE_TIMER_RECOV;
+       case LAPDm_STATE_TIMER_RECOV:
+               dl->retrans_ctr++;
+               if (dl->retrans_ctr < N200) {
+                       /* FIXME: retransmit I frame (V_s-1) with P=1 */
+                       /* FIXME: send appropriate supervision frame with P=1 */
+                       /* restart T200 (PH-READY-TO-SEND) */
+                       bsc_schedule_timer(&dl->t200, T200);
+               } else {
+                       /* FIXME: send ERROR INDICATION to L3 */
+               }
+               break;
+       default:
+               printf("T200 expired in dl->state %u\n", dl->state);
+       }
+}
+
+static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
+{
+       uint8_t sapi = mctx->link_id & 7;
+       struct msgb *msg = msgb_alloc(24, "LAPDm RR");
+       uint8_t *data = msgb_put(msg, 3);
+
+       data[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
+       data[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit);
+       data[2] = LAPDm_LEN(0);
+
+       return tx_ph_data_req(mctx->dl->entity->ms, msg, mctx->chan_nr, mctx->link_id);
+}
+
+/* L1 -> L2 */
+
+/* Receive a LAPDm S (Unnumbered) message from L1 */
+static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx)
+{
+       struct lapdm_datalink *dl = mctx->dl;
+       uint8_t length;
+       int rc;
+
+       switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) {
+       case LAPDm_U_SABM:
+               /* Must be Format B */
+               rc = check_length_ind(msg->l2h[2]);
+               if (rc < 0)
+                       return rc;
+               length = msg->l2h[2] >> 2;
+               /* FIXME: G.4.5 check */
+               if (dl->state == LAPDm_STATE_MF_EST) {
+                       if (length == 0) {
+                               /* FIXME: re-establishment procedure 5.6 */
+                       } else {
+                               /* FIXME: check for contention resoultion */
+                               printf("SABM command, multiple frame established state\n");
+                               msgb_free(msg);
+                               return 0;
+                       }
+               }
+               if (length == 0) {
+                       /* 5.4.1.2 Normal establishment procedures */
+                       rc = send_rslms_rll_simple(RSL_MT_EST_IND, mctx);
+                       msgb_free(msg);
+               } else {
+                       /* 5.4.1.4 Contention resolution establishment */
+                       msg->l3h = msg->l2h + 3;
+                       msgb_pull_l2h(msg);
+                       rc = send_rslms_rll_l3(RSL_MT_EST_IND, mctx, msg);
+               }
+               if (rc == 0)
+                       dl->state = LAPDm_STATE_SABM_SENT;
+               break;
+       case LAPDm_U_DM:
+               if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
+                       /* 5.4.1.2 DM responses with the F bit set to "0" shall be ignored. */
+                       msgb_free(msg);
+                       return 0;
+               }
+               switch (dl->state) {
+               case LAPDm_STATE_IDLE:
+                       /* 5.4.5 all other frame types shall be discarded */
+                       msgb_free(msg);
+                       return 0;
+               case LAPDm_STATE_MF_EST:
+                       if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1)
+                               printf("unsolicited DM resposne\n");
+                       else
+                               printf("unsolicited DM resposne, multiple frame established state\n");
+                       msgb_free(msg);
+                       return 0;
+               case LAPDm_STATE_TIMER_RECOV:
+                       /* DM is normal in case PF = 1 */
+                       if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) {
+                               printf("unsolicited DM resposne, multiple frame established state\n");
+                               msgb_free(msg);
+                               return 0;
+                       }
+                       break;
+               }
+               /* reset T200 */
+               bsc_del_timer(&dl->t200);
+               rc = send_rslms_rll_simple(RSL_MT_REL_IND, mctx);
+               msgb_free(msg);
+               break;
+       case LAPDm_U_UI:
+               if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
+                       length = N201_B4;
+                       msg->l3h = msg->l2h + 2;
+               } else {
+                       rc = check_length_ind(msg->l2h[2]);
+                       if (rc < 0)
+                               return rc;
+                       length = msg->l2h[2] >> 2;
+                       msg->l3h = msg->l2h + 3;
+               }
+               /* do some length checks */
+               if (length == 0) {
+                       /* 5.3.3 UI frames received with the length indicator set to "0" shall be ignored */
+                       msgb_free(msg);
+                       return 0;
+               }
+               /* FIXME: G.4.5 check */
+               switch (LAPDm_ADDR_SAPI(mctx->ctrl)) {
+               case LAPDm_SAPI_NORMAL:
+               case LAPDm_SAPI_SMS:
+                       break;
+               default:
+                       /* 5.3.3 UI frames with invalid SAPI values shall be discarded */
+                       msgb_free(msg);
+                       return 0;
+               }
+               msgb_pull_l2h(msg);
+               rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, mctx, msg);
+               break;
+       case LAPDm_U_DISC:
+               length = msg->l2h[2] >> 2;
+               if (length > 0 || msg->l2h[2] & 0x02) {
+                       /* G.4.4 If a DISC or DM frame is received with L>0 or
+                        * with the M bit set to "1", an MDL-ERROR-INDICATION
+                        * primitive with cause "U frame with incorrect
+                        * parameters" is sent to the mobile management entity. */
+                       return -EIO;
+               }
+               switch (dl->state) {
+               case LAPDm_STATE_IDLE:
+                       /* FIXME: send DM with F=P */
+                       break;
+               default:
+                       /* FIXME */
+                       break;
+               }
+               break;
+       case LAPDm_U_UA:
+               /* FIXME: G.4.5 check */
+               if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
+                       /* 5.4.1.2 A UA response with the F bit set to "0" shall be ignored. */
+                       msgb_free(msg);
+                       return 0;
+               }
+               switch (dl->state) {
+               case LAPDm_STATE_SABM_SENT:
+                       break;
+               case LAPDm_STATE_IDLE:
+                       /* 5.4.5 all other frame types shall be discarded */
+               default:
+                       printf("unsolicited UA response!\n");
+                       msgb_free(msg);
+                       return 0;
+               }
+               /* reset Timer T200 */
+               bsc_del_timer(&dl->t200);
+               /* set Vs, Vr and Va to 0 */
+               dl->V_send = dl->V_recv = dl->V_ack = 0;
+               /* enter multiple-frame-established state */
+               dl->state = LAPDm_STATE_MF_EST;
+               /* send notification to L3 */
+               rc = send_rslms_rll_simple(RSL_MT_EST_CONF, mctx);
+               msgb_free(msg);
+               break;
+       }
+       return rc;
+}
+
+/* Receive a LAPDm S (Supervisory) message from L1 */
+static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
+{
+       struct lapdm_datalink *dl = mctx->dl;
+       uint8_t length;
+
+       length = msg->l2h[2] >> 2;
+       if (length > 0 || msg->l2h[2] & 0x02) {
+               /* G.4.3 If a supervisory frame is received with L>0 or
+                * with the M bit set to "1", an MDL-ERROR-INDICATION
+                * primitive with cause "S frame with incorrect
+                * parameters" is sent to the mobile management entity. */
+               return -EIO;
+       }
+       switch (dl->state) {
+       case LAPDm_STATE_IDLE:
+               /* FIXME: if P=1, respond DM with F=1 (5.2.2) */
+               /* 5.4.5 all other frame types shall be discarded */
+               break;
+       }
+       switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) {
+       case LAPDm_S_RR:
+               /* FIXME */
+               break;
+       case LAPDm_S_RNR:
+               /* FIXME */
+               break;
+       case LAPDm_S_REJ:
+               /* FIXME */
+               break;
+       }
+       return 0;
+}
+
+/* Receive a LAPDm I (Information) message from L1 */
+static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx)
+{
+       struct lapdm_datalink *dl = mctx->dl;
+       uint8_t nr = LAPDm_CTRL_I_Nr(mctx->ctrl);
+       uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl);
+       uint8_t length;
+       int rc;
+
+       length = msg->l2h[2] >> 2;
+       /* FIXME: check for length > N201 */
+       if (length == 0) {
+               /* G.4.2 If the length indicator of an I frame is set
+                * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
+                * primitive with cause "I frame with incorrect length"
+                * is sent to the mobile management entity. */
+               return -EIO;
+       }
+       /* FIXME: G.4.2 If the numerical value of L is L<N201 and the M
+        * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
+        * cause "I frame with incorrect use of M bit" is sent to the
+        * mobile management entity. */
+       switch (dl->state) {
+       case LAPDm_STATE_IDLE:
+               /* FIXME: if P=1, respond DM with F=1 (5.2.2) */
+               /* 5.4.5 all other frame types shall be discarded */
+               break;
+       }
+
+       /* processing of Nr, Ns and P fields */
+       if (ns == dl->V_recv) {
+               /* FIXME: check for M bit! */
+               dl->V_recv = inc_mod8(dl->V_recv);
+
+               /* send a DATA INDICATION to L3 */
+               msg->l3h = msg->l2h + 2;
+               msgb_pull_l2h(msg);
+               rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg);
+       } else {
+               printf("N(s) sequence error: Ns=%u, V_recv=%u\n", ns, dl->V_recv);
+               /* FIXME: 5.7.1: N(s) sequence error */
+               /* discard data */
+               return -EIO;
+       }
+
+       /* Check for P bit */
+       if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
+               /* 5.5.2.1 */
+               /* FIXME: check ifwe are in own receiver busy */
+               /* FIXME: Send RR with F=1 */
+               rc = lapdm_send_rr(mctx, 1);
+       } else {
+               /* 5.5.2.2 */
+               /* FIXME: check ifwe are in own receiver busy */
+               //if (we_have_I_frame_pending) {
+               if (0) {
+                       /* FIXME: send that I frame with Nr=Vr */
+               } else {
+                       /* Send RR with F=0 */
+                       rc = lapdm_send_rr(mctx, 0);
+               }
+       }
+
+       if (dl->state != LAPDm_STATE_TIMER_RECOV) {
+               /* When not in the timer recovery condition, the data
+                * link layer entity shall reset the timer T200 on
+                * receipt of a valid I frame with N(R) higher than V(A) */
+               if (nr > dl->V_ack) {
+                       /* FIXME: 5.5.3.1 Note 1 + 2 */
+                       bsc_del_timer(&dl->t200);
+                       /* FIXME: if there are outstanding I frames
+                        * still unacknowledged, the data link layer
+                        * entity shall set timer T200 */
+               }
+
+               /* FIXME: 5.7.4: N(R) sequence error */
+               /* N(R) is called valid, if and only if (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8. */
+       }
+
+       /* V(A) shall be set to the value of N(R) */
+       dl->V_ack = LAPDm_CTRL_I_Nr(mctx->ctrl);
+
+       return rc;
+}
+
+/* Receive a LAPDm message from L1 */
+static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx)
+{
+       int rc;
+
+       if (LAPDm_CTRL_is_U(mctx->ctrl))
+               rc = lapdm_rx_u(msg, mctx);
+       else if (LAPDm_CTRL_is_S(mctx->ctrl))
+               rc = lapdm_rx_s(msg, mctx);
+       else if (LAPDm_CTRL_is_I(mctx->ctrl))
+               rc = lapdm_rx_i(msg, mctx);
+       else {
+               printf("unknown LAPDm format\n");
+               rc = -EINVAL;
+       }
+       return rc;
+}
+
+/* input into layer2 (from layer 1) */
+int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1_info_dl *l1i)
+{
+       uint8_t cbits = l1i->chan_nr >> 3;
+       uint8_t sapi = l1i->link_id & 7;
+       struct lapdm_msg_ctx mctx;
+       int rc;
+
+       /* when we reach here, we have a msgb with l2h pointing to the raw
+        * 23byte mac block. The l1h has already been purged. */
+
+       mctx.dl = datalink_for_sapi(le, sapi);
+       mctx.chan_nr = l1i->chan_nr;
+       mctx.link_id = l1i->link_id;
+       mctx.addr = mctx.ctrl = 0;
+
+       /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
+       if (cbits == 0x10 || cbits == 0x12) {
+               /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
+               mctx.lapdm_fmt = LAPDm_FMT_Bbis;
+       } else {
+               if (mctx.link_id & 0x40) {
+                       /* It was received from network on SACCH, thus
+                        * lapdm_fmt must be B4 */
+                       mctx.lapdm_fmt = LAPDm_FMT_B4;
+               } else
+                       mctx.lapdm_fmt = LAPDm_FMT_B;
+       }
+
+       switch (mctx.lapdm_fmt) {
+       case LAPDm_FMT_A:
+       case LAPDm_FMT_B:
+       case LAPDm_FMT_B4:
+               mctx.addr = msg->l2h[0];
+               if (!(mctx.addr & 0x01)) {
+                       printf("we don't support multibyte addresses\n");
+                       return -EINVAL;
+               }
+               mctx.ctrl = msg->l2h[1];
+               /* obtain SAPI from address field */
+               mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr);
+               rc = lapdm_ph_data_ind(msg, &mctx);
+               break;
+       case LAPDm_FMT_Bter:
+               /* FIXME */
+               break;
+       case LAPDm_FMT_Bbis:
+               msg->l3h = msg->l2h;
+               msgb_pull_l2h(msg);
+               rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
+               break;
+       }
+
+       return rc;
+}
+
+/* L3 -> L2 */
+
+/* L3 requests establishment of data link */
+static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
+{
+       struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
+       uint8_t chan_nr = rllh->chan_nr;
+       uint8_t link_id = rllh->link_id;
+       uint8_t sapi = rllh->link_id & 7;
+       struct tlv_parsed tv;
+       uint8_t len;
+       uint8_t *lapdh;
+
+       rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
+       if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
+               /* contention resolution establishment procedure */
+               if (dl->state != LAPDm_STATE_IDLE) {
+                       /* 5.4.1.4: The data link layer shall, however, ignore any such
+                        * service request if it is not in the idle state when the
+                        * request is received. */
+                       msgb_free(msg);
+                       return 0;
+               }
+               if (sapi != 0) {
+                       /* According to clause 6, the contention resolution
+                        * procedure is only permitted with SAPI value 0 */
+                       msgb_free(msg);
+                       return -EINVAL;
+               }
+               /* transmit a SABM command with the P bit set to "1". The SABM
+                * command shall contain the layer 3 message unit */
+               len = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO));
+
+               /* FIXME: store information field in dl entity */
+       } else {
+               /* normal establishment procedure */
+               len = LAPDm_LEN(0);
+       }
+
+       /* Remove RLL header from msgb */
+       msgb_pull_l2h(msg);
+
+       /* Push LAPDm header on msgb */
+       lapdh = msgb_push(msg, 3);
+       lapdh[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
+       lapdh[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
+       lapdh[2] = len;
+
+       /* Tramsmit and start T200 */
+       bsc_schedule_timer(&dl->t200, T200);
+       return tx_ph_data_req(dl->entity->ms, msg, chan_nr, link_id);
+}
+
+/* L3 requests transfer of unnumbered information */
+static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
+{
+       struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
+       uint8_t chan_nr = rllh->chan_nr;
+       uint8_t link_id = rllh->link_id;
+       uint8_t sapi = link_id & 7;
+       struct tlv_parsed tv;
+       uint8_t *lapdh;
+
+       rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
+
+       /* Remove RLL header from msgb */
+       msgb_pull_l2h(msg);
+
+       /* Push LAPDm header on msgb */
+       lapdh = msgb_push(msg, 3);
+       lapdh[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
+       lapdh[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
+       lapdh[2] = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO));
+
+       /* Tramsmit and start T200 */
+       bsc_schedule_timer(&dl->t200, T200);
+       return tx_ph_data_req(dl->entity->ms, msg, chan_nr, link_id);
+}
+
+/* L3 requests transfer of acknowledged information */
+static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
+{
+       struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
+       uint8_t chan_nr = rllh->chan_nr;
+       uint8_t link_id = rllh->link_id;
+       uint8_t sapi = rllh->link_id & 7;
+       struct tlv_parsed tv;
+       uint8_t *lapdh;
+
+       switch (dl->state) {
+       case LAPDm_STATE_MF_EST:
+               break;
+       default:
+               printf("refusing RLL DATA REQ during DL state %u\n", dl->state);
+               return -EIO;
+               break;
+       }
+
+       /* FIXME: check if the layer3 message length exceeds N201 */
+
+       rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
+
+       /* Remove the RSL/RLL header */
+       msgb_pull_l2h(msg);
+
+       /* Push the LAPDm header */
+       lapdh = msgb_put(msg, 3);
+       lapdh[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
+       lapdh[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
+       lapdh[2] = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO));
+
+       /* The value of the send state variable V(S) shall be incremented by 1
+        * at the end of the transmission of the I frame */
+       dl->V_send = inc_mod8(dl->V_send);
+
+       /* If timer T200 is not running at the time right before transmitting a
+        * frame, when the PH-READY-TO-SEND primitive is received from the
+        * physical layer., it shall be set. */
+       if (!bsc_timer_pending(&dl->t200))
+               bsc_schedule_timer(&dl->t200, T200);
+
+       /* FIXME: If the send state variable V(S) is equal to V(A) plus k
+        * (where k is the maximum number of outstanding I frames - see
+        * subclause 5.8.4), the data link layer entity shall not transmit any
+        * new I frames, but shall retransmit an I frame as a result
+        * of the error recovery procedures as described in subclauses 5.5.4 and
+        * 5.5.7. */
+
+       return tx_ph_data_req(dl->entity->ms, msg, chan_nr, link_id);
+}
+
+/* incoming RSLms RLL message from L3 */
+static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
+{
+       struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
+       int rc = 0;
+       uint8_t sapi = rllh->link_id & 7;
+       struct lapdm_entity *le;
+       struct lapdm_datalink *dl;
+
+       if (rllh->link_id & 0x40)
+               le = &ms->lapdm_acch;
+       else
+               le = &ms->lapdm_dcch;
+       dl = datalink_for_sapi(le, sapi);
+
+       switch (rllh->c.msg_type) {
+       case RSL_MT_UNIT_DATA_REQ:
+               /* create and send UI command */
+               rc = rslms_rx_rll_udata_req(msg, dl);
+               break;
+       case RSL_MT_EST_REQ:
+               /* create and send SABM command */
+               rc = rslms_rx_rll_est_req(msg, dl);
+               break;
+       case RSL_MT_DATA_REQ:
+               /* create and send I command */
+               rc = rslms_rx_rll_data_req(msg, dl);
+               break;
+       case RSL_MT_REL_REQ:
+               /* FIXME: create and send DISC command */
+       default:
+               printf("unknown RLL message type 0x%02x\n",
+                       rllh->c.msg_type);
+               break;
+       }
+
+       return rc;
+}
+
+/* input into layer2 (from layer 3) */
+int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms)
+{
+       struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
+       int rc = 0;
+
+       switch (rslh->msg_discr & 0xfe) {
+       case ABIS_RSL_MDISC_RLL:
+               rc = rslms_rx_rll(msg, ms);
+               break;
+       default:
+               printf("unknown RSLms message discriminator 0x%02x",
+                       rslh->msg_discr);
+               msgb_free(msg);
+               return -EINVAL;
+       }
+
+       return rc;
+}
+
diff --git a/src/host/layer2/src/osmocom_rslms.c b/src/host/layer2/src/osmocom_rslms.c
new file mode 100644 (file)
index 0000000..a0afa46
--- /dev/null
@@ -0,0 +1,171 @@
+
+#include <stdint.h>
+#include <errno.h>
+#include <stdio.h>
+
+#include <osmocore/msgb.h>
+#include <osmocore/rsl.h>
+#include <osmocore/tlv.h>
+#include <osmocore/protocol/gsm_04_08.h>
+#include <osmocom/lapdm.h>
+#include <osmocom/osmocom_data.h>
+#include <osmocom/osmocom_layer2.h>
+
+int rsl_dec_chan_nr(uint8_t chan_nr, uint8_t *type, uint8_t *subch, uint8_t *timeslot)
+{
+       *timeslot = chan_nr & 0x7;
+
+       if ((chan_nr & 0xf8) == RSL_CHAN_Bm_ACCHs) {
+               *type = RSL_CHAN_Bm_ACCHs;
+               *subch = 0;
+       } else if ((chan_nr & 0xf0) == RSL_CHAN_Lm_ACCHs) {
+               *type = RSL_CHAN_Lm_ACCHs;
+               *subch = (chan_nr >> 3) & 0x1;
+       } else if ((chan_nr & 0xe0) == RSL_CHAN_SDCCH4_ACCH) {
+               *type = RSL_CHAN_SDCCH4_ACCH;
+               *subch = (chan_nr >> 3) & 0x3;
+       } else if ((chan_nr & 0xc0) == RSL_CHAN_SDCCH8_ACCH) {
+               *type = RSL_CHAN_SDCCH8_ACCH;
+               *subch = (chan_nr >> 3) & 0x7;
+       } else {
+               printf("unable to decode chan_nr\n");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+
+static int gsm48_rx_imm_ass(struct msgb *msg, struct osmocom_ms *ms)
+{
+       struct gsm48_imm_ass *ia = msgb_l3(msg);
+       uint8_t ch_type, ch_subch, ch_ts;
+
+       rsl_dec_chan_nr(ia->chan_desc.chan_nr, &ch_type, &ch_subch, &ch_ts);
+
+       printf("GSM48 IMM ASS (ra=0x%02x, chan_nr=0x%02x, TS=%u, SS=%u, TSC=%u) ",
+               ia->req_ref.ra, ia->chan_desc.chan_nr, ch_ts, ch_subch,
+               ia->chan_desc.h0.tsc);
+
+       /* FIXME: compare RA and GSM time with when we sent RACH req */
+
+       /* check if we can support this type of channel at the moment */
+       if (ch_type != RSL_CHAN_SDCCH4_ACCH || ch_ts != 0 ||
+           ia->chan_desc.h0.h == 1) {
+               printf("UNSUPPORTED!\n");
+               return 0;
+       }
+
+       /* FIXME: request L1 to go to dedicated mode on assigned channel */
+
+       return 0;
+}
+
+static int gsm48_rx_ccch(struct msgb *msg, struct osmocom_ms *ms)
+{
+       struct gsm48_system_information_type_header *sih = msgb_l3(msg);
+       int rc = 0;
+
+       if (sih->rr_protocol_discriminator != GSM48_PDISC_RR)
+               printf("PCH pdisc != RR\n");
+       
+       switch (sih->system_information) {
+       case GSM48_MT_RR_PAG_REQ_1:
+       case GSM48_MT_RR_PAG_REQ_2:
+       case GSM48_MT_RR_PAG_REQ_3:
+               /* FIXME: implement decoding of paging request */
+               break;
+       case GSM48_MT_RR_IMM_ASS:
+               rc = gsm48_rx_imm_ass(msg, ms);
+               break;
+       default:
+               printf("unknown PCH/AGCH type 0x%02x\n", sih->system_information);
+               rc = -EINVAL;
+       }
+
+       return rc;
+}
+
+static int rach_count = 0;
+
+static int rslms_rx_udata_ind(struct msgb *msg, struct osmocom_ms *ms)
+{
+       struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
+       struct tlv_parsed tv;
+       int rc = 0;
+       
+       printf("RSLms UNIT DATA IND chan_nr=0x%02x link_id=0x%02x\n",
+               rllh->chan_nr, rllh->link_id);
+
+       rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
+       if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
+               printf("UNIT_DATA_IND without L3 INFO ?!?\n");
+               return -EIO;
+       }
+       msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
+
+       if (rllh->chan_nr == RSL_CHAN_PCH_AGCH)
+               rc = gsm48_rx_ccch(msg, ms);
+       else if (rllh->chan_nr == RSL_CHAN_BCCH) {
+               //rc = gsm48_rx_bcch(msg);
+               if (rach_count < 2) {
+                       tx_ph_rach_req(ms);
+                       rach_count++;
+               }
+       }
+
+       return rc;
+}
+
+
+
+static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
+{
+       struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
+       int rc = 0;
+
+       switch (rllh->c.msg_type) {
+       case RSL_MT_DATA_IND:
+               printf("RSLms DATA IND\n");
+               break;
+       case RSL_MT_UNIT_DATA_IND:
+               rc = rslms_rx_udata_ind(msg, ms);
+               break;
+       case RSL_MT_EST_IND:
+               printf("RSLms EST IND\n");
+               break;
+       case RSL_MT_EST_CONF:
+               printf("RSLms EST CONF\n");
+               break;
+       case RSL_MT_REL_CONF:
+               printf("RSLms REL CONF\n");
+               break;
+       case RSL_MT_ERROR_IND:
+               printf("RSLms ERR IND\n");
+               break;
+       default:
+               printf("unknown RSLms message type 0x%02x\n", rllh->c.msg_type);
+               rc = -EINVAL;
+               break;
+       }
+       return rc;
+}
+
+/* sending messages up from L2 to L3 */
+int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms)
+{
+       struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
+       int rc = 0;
+
+       switch (rslh->msg_discr & 0xfe) {
+       case ABIS_RSL_MDISC_RLL:
+               rc = rslms_rx_rll(msg, ms);
+               break;
+       default:
+               printf("unknown RSLms msg_discr 0x%02x\n", rslh->msg_discr);
+               rc = -EINVAL;
+               break;
+       }
+
+       return rc;
+}