lapdm: use msgb_tlv_put instead of manual equivalent
[osmocom-bb.git] / src / host / layer23 / src / common / lapdm.c
index 974c34c..e6fc75e 100644 (file)
@@ -1,6 +1,6 @@
 /* GSM LAPDm (TS 04.06) implementation */
 
-/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
+/* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
  *
  * All Rights Reserved
 #include <osmocom/gsm/tlv.h>
 #include <osmocom/core/utils.h>
 #include <osmocom/gsm/rsl.h>
+#include <osmocom/gsm/prim.h>
+#include <osmocom/gsm/gsm_utils.h>
 #include <osmocom/gsm/protocol/gsm_04_08.h>
 #include <osmocom/gsm/protocol/gsm_08_58.h>
 
-#include <osmocom/bb/common/osmocom_data.h>
-#include <osmocom/bb/common/l1ctl.h>
 #include <osmocom/bb/common/lapdm.h>
 #include <osmocom/bb/common/logging.h>
 
-#include <l1ctl_proto.h>
-
 /* TS 04.06 Figure 4 / Section 3.2 */
 #define LAPDm_LPD_NORMAL  0
 #define LAPDm_LPD_SMSCB          1
@@ -186,16 +184,21 @@ static void lapdm_dl_init(struct lapdm_datalink *dl,
        dl->entity = entity;
 }
 
-void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms)
+void lapdm_entity_init(struct lapdm_entity *le)
 {
        unsigned int i;
 
        for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
                lapdm_dl_init(&le->datalink[i], le);
+}
 
-       le->ms = ms;
+void lapdm_channel_init(struct lapdm_channel *lc)
+{
+       lapdm_entity_init(&lc->lapdm_acch);
+       lapdm_entity_init(&lc->lapdm_dcch);
 }
 
+
 static void lapdm_dl_flush_send(struct lapdm_datalink *dl)
 {
        struct msgb *msg;
@@ -222,7 +225,7 @@ static void lapdm_dl_flush_tx(struct lapdm_datalink *dl)
                dl->tx_length[i] = 0;
 }
 
-void lapdm_exit(struct lapdm_entity *le)
+void lapdm_entity_exit(struct lapdm_entity *le)
 {
        unsigned int i;
        struct lapdm_datalink *dl;
@@ -236,6 +239,12 @@ void lapdm_exit(struct lapdm_entity *le)
        }
 }
 
+void lapdm_channel_exit(struct lapdm_channel *lc)
+{
+       lapdm_entity_exit(&lc->lapdm_acch);
+       lapdm_entity_exit(&lc->lapdm_dcch);
+}
+
 static void lapdm_dl_newstate(struct lapdm_datalink *dl, uint32_t state)
 {
        LOGP(DLAPDM, LOGL_INFO, "new state %s -> %s\n",
@@ -280,12 +289,29 @@ static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
        memset(data, 0x2B, pad_len);
 }
 
+/* input function that L2 calls when sending messages up to L3 */
+static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
+{
+       if (!le->l3_cb) {
+               msgb_free(msg);
+               return -EIO;
+       }
+
+       /* call the layer2 message handler that is registered */
+       return le->l3_cb(msg, le, le->l3_ctx);
+}
+
 /* write a frame into the tx queue */
 static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
                                uint8_t chan_nr, uint8_t link_id, uint8_t n201)
 {
        struct lapdm_entity *le = dl->entity;
-       struct osmocom_ms *ms = le->ms;
+       struct osmo_phsap_prim pp;
+
+       osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
+                       PRIM_OP_REQUEST, msg);
+       pp.u.data.chan_nr = chan_nr;
+       pp.u.data.link_id = link_id;
 
        /* if there is a pending message, queue it */
        if (le->tx_pending) {
@@ -299,25 +325,27 @@ static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
        /* send the frame now */
        le->tx_pending = 0; /* disabled flow control */
        lapdm_pad_msgb(msg, n201);
-       return l1ctl_tx_data_req(ms, msg, chan_nr, link_id);
+
+       return le->l1_prim_cb(&pp.oph, le->l1_ctx);
 }
 
 /* get next frame from the tx queue. because the ms has multiple datalinks,
  * each datalink's queue is read round-robin.
  */
-int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
+static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
 {
-       struct osmocom_ms *ms = le->ms;
        struct lapdm_datalink *dl;
        int last = le->last_tx_dequeue;
        int i = last, n = ARRAY_SIZE(le->datalink);
-       uint8_t chan_nr, link_id, n201;
+       struct osmo_phsap_prim pp;
+       uint8_t n201;
 
        /* we may send again */
        le->tx_pending = 0;
 
        /* free confirm message */
-       msgb_free(msg);
+       if (msg)
+               msgb_free(msg);
 
        /* round-robin dequeue */
        do {
@@ -332,10 +360,13 @@ int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
        if (!msg)
                return 0;
 
+       osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
+                       PRIM_OP_REQUEST, msg);
+
        /* Pull chan_nr and link_id */
-       chan_nr = *msg->data;
+       pp.u.data.chan_nr = *msg->data;
        msgb_pull(msg, 1);
-       link_id = *msg->data;
+       pp.u.data.link_id = *msg->data;
        msgb_pull(msg, 1);
        n201 = *msg->data;
        msgb_pull(msg, 1);
@@ -346,7 +377,8 @@ int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
        /* Pad the frame, we can transmit now */
        le->tx_pending = 1;
        lapdm_pad_msgb(msg, n201);
-       return l1ctl_tx_data_req(ms, msg, chan_nr, link_id);
+
+       return le->l1_prim_cb(&pp.oph, le->l1_ctx);
 }
 
 /* Create RSLms various RSLms messages */
@@ -357,7 +389,7 @@ static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
        rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
 
        /* send off the RSLms message to L3 */
-       return rslms_sendmsg(msg, mctx->dl->entity->ms);
+       return rslms_sendmsg(msg, mctx->dl->entity);
 }
 
 /* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
@@ -379,7 +411,7 @@ static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
        rllh->data[2] = RSL_IE_MS_POWER;
        rllh->data[3] = mctx->tx_power_ind;
        
-       return rslms_sendmsg(msg, mctx->dl->entity->ms);
+       return rslms_sendmsg(msg, mctx->dl->entity);
 }
 
 static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
@@ -389,22 +421,18 @@ static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
        msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
 
        /* send off the RSLms message to L3 */
-       return rslms_sendmsg(msg, mctx->dl->entity->ms);
+       return rslms_sendmsg(msg, mctx->dl->entity);
 }
 
 static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
 {
        struct msgb *msg;
-       uint8_t *tlv;
 
        LOGP(DLAPDM, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
        msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
-       msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr) + 3);
-       tlv = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
-       tlv[0] = RSL_IE_RLM_CAUSE;
-       tlv[1] = 1;
-       tlv[2] = cause;
-       return rslms_sendmsg(msg, mctx->dl->entity->ms);
+       msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr));
+       msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
+       return rslms_sendmsg(msg, mctx->dl->entity);
 }
 
 static int check_length_ind(struct lapdm_msg_ctx *mctx, uint8_t length_ind)
@@ -1495,10 +1523,10 @@ static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx)
 }
 
 /* input into layer2 (from layer 1) */
-int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i)
+static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t chan_nr, uint8_t link_id)
 {
-       uint8_t cbits = l1i->chan_nr >> 3;
-       uint8_t sapi = l1i->link_id & 7;
+       uint8_t cbits = chan_nr >> 3;
+       uint8_t sapi = link_id & 7;
        struct lapdm_msg_ctx mctx;
        int rc = 0;
 
@@ -1506,8 +1534,8 @@ int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_
         * 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.chan_nr = chan_nr;
+       mctx.link_id = link_id;
        mctx.addr = mctx.ctrl = 0;
 
        /* G.2.1 No action schall be taken on frames containing an unallocated
@@ -1580,6 +1608,50 @@ int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_
        return rc;
 }
 
+static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
+
+int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
+{
+       struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
+       int rc = 0;
+
+       if (oph->sap != SAP_GSM_PH)
+               return -ENODEV;
+
+       if (oph->operation != PRIM_OP_INDICATION)
+               return -ENODEV;
+
+       switch (oph->primitive) {
+       case PRIM_PH_DATA:
+               if (oph->operation != PRIM_OP_INDICATION)
+                       return -ENODEV;
+               rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
+                                   pp->u.data.link_id);
+               break;
+       case PRIM_PH_RTS:
+               if (oph->operation != PRIM_OP_INDICATION)
+                       return -ENODEV;
+               rc = l2_ph_data_conf(oph->msg, le);
+               break;
+       case PRIM_PH_RACH:
+               switch (oph->operation) {
+               case PRIM_OP_INDICATION:
+#warning FIX BTS
+                       //rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn);
+                       break;
+               case PRIM_OP_CONFIRM:
+                       rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
+                       break;
+               default:
+                       return -EIO;
+               }
+               break;
+       }
+
+       return rc;
+}
+
+
 /* L3 -> L2 / RSLMS -> LAPDm */
 
 /* L3 requests establishment of data link */
@@ -2027,10 +2099,14 @@ static int rslms_rx_rll_rel_req_idle(struct msgb *msg, struct lapdm_datalink *dl
 }
 
 /* L3 requests channel in idle state */
-static int rslms_rx_chan_rqd(struct osmocom_ms *ms, struct msgb *msg)
+static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
 {
        struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
-       int rc;
+       void *l1ctx = lc->lapdm_dcch.l1_ctx;
+       struct osmo_phsap_prim pp;
+
+       osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
+                       PRIM_OP_REQUEST, NULL);
 
        if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
                LOGP(DRSL, LOGL_ERROR, "Message too short for CHAN RQD!\n");
@@ -2040,35 +2116,36 @@ static int rslms_rx_chan_rqd(struct osmocom_ms *ms, struct msgb *msg)
                LOGP(DRSL, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
                return -EINVAL;
        }
+       pp.u.rach_req.ra = cch->data[1];
+       pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
+       pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
+
        if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
                LOGP(DRSL, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
                return -EINVAL;
        }
+       /* TA = 0 - delay */
+       pp.u.rach_req.ta = 0 - cch->data[5];
+
        if (cch->data[6] != RSL_IE_MS_POWER) {
                LOGP(DRSL, LOGL_ERROR, "Missing MS POWER IE\n");
                return -EINVAL;
        }
-
-       /* TA = 0 - delay */
-       rc = l1ctl_tx_param_req(ms, 0 - cch->data[5], cch->data[7]);
-
-       rc = l1ctl_tx_rach_req(ms, cch->data[1],
-               ((cch->data[2] & 0x7f) << 8) | cch->data[3], cch->data[2] >> 7);
+       pp.u.rach_req.tx_power = cch->data[7];
 
        msgb_free(msg);
 
-       return rc;
+       return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
 }
 
 /* L1 confirms channel request */
-int l2_ph_chan_conf(struct msgb *msg, struct osmocom_ms *ms,
-                       struct l1ctl_info_dl *dl)
+static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
 {
        struct abis_rsl_cchan_hdr *ch;
        struct gsm_time tm;
        struct gsm48_req_ref *ref;
 
-       gsm_fn2gsmtime(&tm, htonl(dl->frame_nr));
+       gsm_fn2gsmtime(&tm, frame_nr);
 
        msgb_pull_l2h(msg);
        msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
@@ -2082,35 +2159,7 @@ int l2_ph_chan_conf(struct msgb *msg, struct osmocom_ms *ms,
        ref->t3_low = tm.t3 & 0x7;
        ref->t3_high = tm.t3 >> 3;
        
-       return rslms_sendmsg(msg, ms);
-}
-
-/* Names for Radio Link Layer Management */
-static const struct value_string rsl_msg_names[] = {
-       { RSL_MT_DATA_REQ,              "RSL_MT_DATA_REQ" },
-       { RSL_MT_DATA_IND,              "RSL_MT_DATA_IND" },
-       { RSL_MT_ERROR_IND,             "RSL_MT_ERROR_IND" },
-       { RSL_MT_EST_REQ,               "RSL_MT_EST_REQ" },
-       { RSL_MT_EST_CONF,              "RSL_MT_EST_CONF" },
-       { RSL_MT_EST_IND,               "RSL_MT_EST_IND" },
-       { RSL_MT_EST_IND,               "RSL_MT_REL_REQ" },
-       { RSL_MT_REL_REQ,               "RSL_MT_REL_REQ" },
-       { RSL_MT_REL_CONF,              "RSL_MT_REL_CONF" },
-       { RSL_MT_REL_IND,               "RSL_MT_REL_IND" },
-       { RSL_MT_UNIT_DATA_REQ,         "RSL_MT_UNIT_DATA_REQ" },
-       { RSL_MT_UNIT_DATA_IND,         "RSL_MT_UNIT_DATA_IND" },
-       { RSL_MT_SUSP_REQ,              "RSL_MT_SUSP_REQ" },
-       { RSL_MT_SUSP_CONF,             "RSL_MT_SUSP_CONF" },
-       { RSL_MT_RES_REQ,               "RSL_MT_RES_REQ" },
-       { RSL_MT_RECON_REQ,             "RSL_MT_RECON_REQ" },
-       { RSL_MT_CHAN_RQD,              "RSL_MT_CHAN_RQD" },
-       { RSL_MT_CHAN_CONF,             "RSL_MT_CHAN_CONF" },
-       { 0,                            NULL }
-};
-
-const char *get_rsl_name(int value)
-{
-       return get_value_string(rsl_msg_names, value);
+       return rslms_sendmsg(msg, le);
 }
 
 const char *lapdm_state_names[] = {
@@ -2173,7 +2222,7 @@ static struct l2downstate {
        (sizeof(l2downstatelist) / sizeof(struct l2downstate))
 
 /* incoming RSLms RLL message from L3 */
-static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
+static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
 {
        struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
        int msg_type = rllh->c.msg_type;
@@ -2189,9 +2238,9 @@ static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
        }
 
        if (rllh->link_id & 0x40)
-               le = &ms->l2_entity.lapdm_acch;
+               le = &lc->lapdm_acch;
        else
-               le = &ms->l2_entity.lapdm_dcch;
+               le = &lc->lapdm_dcch;
 
        /* G.2.1 No action schall be taken on frames containing an unallocated
         * SAPI.
@@ -2202,8 +2251,8 @@ static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
                return -EINVAL;
        }
 
-       LOGP(DRSL, LOGL_INFO, "(ms %s) RLL Message '%s' received in state %s\n",
-               ms->name, get_rsl_name(msg_type), lapdm_state_names[dl->state]);
+       LOGP(DRSL, LOGL_INFO, "(%p) RLL Message '%s' received in state %s\n",
+               lc->name, rsl_msg_name(msg_type), lapdm_state_names[dl->state]);
 
        /* find function for current state and message */
        for (i = 0; i < L2DOWNSLLEN; i++) {
@@ -2230,7 +2279,7 @@ static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
 }
 
 /* incoming RSLms COMMON CHANNEL message from L3 */
-static int rslms_rx_com_chan(struct msgb *msg, struct osmocom_ms *ms)
+static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
 {
        struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
        int msg_type = cch->c.msg_type;
@@ -2244,7 +2293,7 @@ static int rslms_rx_com_chan(struct msgb *msg, struct osmocom_ms *ms)
        switch (msg_type) {
        case RSL_MT_CHAN_RQD:
                /* create and send RACH request */
-               rc = rslms_rx_chan_rqd(ms, msg);
+               rc = rslms_rx_chan_rqd(lc, msg);
                break;
        default:
                LOGP(DRSL, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
@@ -2257,7 +2306,7 @@ static int rslms_rx_com_chan(struct msgb *msg, struct osmocom_ms *ms)
 }
 
 /* input into layer2 (from layer 3) */
-int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms)
+int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
 {
        struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
        int rc = 0;
@@ -2269,10 +2318,10 @@ int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms)
 
        switch (rslh->msg_discr & 0xfe) {
        case ABIS_RSL_MDISC_RLL:
-               rc = rslms_rx_rll(msg, ms);
+               rc = rslms_rx_rll(msg, lc);
                break;
        case ABIS_RSL_MDISC_COM_CHAN:
-               rc = rslms_rx_com_chan(msg, ms);
+               rc = rslms_rx_com_chan(msg, lc);
                break;
        default:
                LOGP(DRSL, LOGL_ERROR, "unknown RSLms message "
@@ -2284,22 +2333,18 @@ int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms)
        return rc;
 }
 
-/* input function that L2 calls when sending messages up to L3 */
-int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms)
+void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
 {
-       if (!ms->l2_entity.msg_handler) {
-               msgb_free(msg);
-               return -EIO;
-       }
-
-       /* call the layer2 message handler that is registered */
-       return ms->l2_entity.msg_handler(msg, ms);
+       lc->lapdm_dcch.l1_prim_cb = cb;
+       lc->lapdm_acch.l1_prim_cb = cb;
+       lc->lapdm_dcch.l1_ctx = ctx;
+       lc->lapdm_acch.l1_ctx = ctx;
 }
 
-/* register message handler for messages that are sent from L2->L3 */
-int osmol2_register_handler(struct osmocom_ms *ms, osmol2_cb_t cb)
+void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
 {
-       ms->l2_entity.msg_handler = cb;
-
-       return 0;
+       lc->lapdm_dcch.l3_cb = cb;
+       lc->lapdm_acch.l3_cb = cb;
+       lc->lapdm_dcch.l3_ctx = ctx;
+       lc->lapdm_acch.l3_ctx = ctx;
 }