lapdm: use msgb_tlv_put instead of manual equivalent
[osmocom-bb.git] / src / host / layer23 / src / common / lapdm.c
index 8b5690e..e6fc75e 100644 (file)
 #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/lapdm.h>
 #include <osmocom/bb/common/logging.h>
 
@@ -305,6 +306,12 @@ 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 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) {
@@ -318,24 +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(le->l1_ctx, 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 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 {
@@ -350,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);
@@ -364,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(le->l1_ctx, msg, chan_nr, link_id);
+
+       return le->l1_prim_cb(&pp.oph, le->l1_ctx);
 }
 
 /* Create RSLms various RSLms messages */
@@ -413,15 +427,11 @@ static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
 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;
+       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);
 }
 
@@ -1513,7 +1523,7 @@ 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, uint8_t chan_nr, uint8_t link_id)
+static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t chan_nr, uint8_t link_id)
 {
        uint8_t cbits = chan_nr >> 3;
        uint8_t sapi = link_id & 7;
@@ -1598,6 +1608,50 @@ int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t chan_nr, u
        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 */
@@ -2049,7 +2103,10 @@ static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
 {
        struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
        void *l1ctx = lc->lapdm_dcch.l1_ctx;
-       int rc;
+       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");
@@ -2059,35 +2116,36 @@ static int rslms_rx_chan_rqd(struct lapdm_channel *lc, 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(l1ctx, 0 - cch->data[5], cch->data[7]);
-
-       rc = l1ctl_tx_rach_req(l1ctx, 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));
@@ -2101,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->lapdm_channel.lapdm_dcch);
-}
-
-/* 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[] = {
@@ -2222,7 +2252,7 @@ static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
        }
 
        LOGP(DRSL, LOGL_INFO, "(%p) RLL Message '%s' received in state %s\n",
-               lc->name, get_rsl_name(msg_type), lapdm_state_names[dl->state]);
+               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++) {
@@ -2303,12 +2333,18 @@ int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
        return rc;
 }
 
-int osmol2_register_handler(struct osmocom_ms *ms, lapdm_cb_t cb)
+void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
 {
-       ms->lapdm_channel.lapdm_acch.l3_cb = cb;
-       ms->lapdm_channel.lapdm_dcch.l3_cb = cb;
-       ms->lapdm_channel.lapdm_acch.l3_ctx = ms;
-       ms->lapdm_channel.lapdm_dcch.l3_ctx = 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;
+}
 
-       return 0;
+void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
+{
+       lc->lapdm_dcch.l3_cb = cb;
+       lc->lapdm_acch.l3_cb = cb;
+       lc->lapdm_dcch.l3_ctx = ctx;
+       lc->lapdm_acch.l3_ctx = ctx;
 }