src: use new libosmogsm and include/osmocom/[gsm|core] path to headers
[osmocom-bb.git] / src / host / layer23 / src / common / lapdm.c
index b199c62..61e2287 100644 (file)
 #include <errno.h>
 #include <arpa/inet.h>
 
-#include <osmocore/logging.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/core/logging.h>
+#include <osmocom/core/timer.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/gsm/tlv.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/gsm/rsl.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>
@@ -155,7 +155,7 @@ enum lapdm_format {
 };
 
 static void lapdm_t200_cb(void *data);
-static int rslms_send_i(struct lapdm_msg_ctx *mctx);
+static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line);
 
 /* UTILITY FUNCTIONS */
 
@@ -524,9 +524,8 @@ static void lapdm_t200_cb(void *data)
                        send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
                        /* send MDL ERROR INIDCATION to L3 */
                        rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
-                       /* flush buffers */
+                       /* flush tx buffers */
                        lapdm_dl_flush_tx(dl);
-                       lapdm_dl_flush_send(dl);
                        /* go back to idle state */
                        lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
                        /* NOTE: we must not change any other states or buffers
@@ -574,20 +573,21 @@ static void lapdm_t200_cb(void *data)
                dl->retrans_ctr++;
                if (dl->retrans_ctr < N200) {
                        /* retransmit I frame (V_s-1) with P=1, if any */
-                       if (dl->tx_length[dl->V_send - 1]) {
+                       if (dl->tx_length[sub_mod8(dl->V_send, 1)]) {
                                struct msgb *msg;
                                int length;
 
                                LOGP(DLAPDM, LOGL_INFO, "retransmit last frame "
-                                       "V(S)=%d\n", dl->V_send - 1);
+                                       "V(S)=%d\n", sub_mod8(dl->V_send, 1));
                                /* Create I frame (segment) from tx_hist */
-                               length = dl->tx_length[dl->V_send - 1];
+                               length = dl->tx_length[sub_mod8(dl->V_send, 1)];
                                msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
                                msg->l2h = msgb_put(msg, length);
-                               memcpy(msg->l2h, dl->tx_hist[dl->V_send - 1],
+                               memcpy(msg->l2h,
+                                       dl->tx_hist[sub_mod8(dl->V_send, 1)],
                                        length);
                                msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv,
-                                               dl->V_send - 1, 1); /* P=1 */
+                                       sub_mod8(dl->V_send, 1), 1); /* P=1 */
                                tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr,
                                        dl->mctx.link_id, dl->mctx.n201);
                        } else {
@@ -1046,7 +1046,7 @@ static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx)
                /* enter multiple-frame-established state */
                lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
                /* send outstanding frames, if any (resume / reconnect) */
-               rslms_send_i(mctx);
+               rslms_send_i(mctx, __LINE__);
                /* send notification to L3 */
                rc = send_rll_simple(RSL_MT_EST_CONF, mctx);
                msgb_free(msg);
@@ -1079,9 +1079,11 @@ static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
                rsl_rll_error(RLL_CAUSE_SFRM_INC_PARAM, mctx);
                return -EIO;
        }
-       /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
+
        if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
-        && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
+        && LAPDm_CTRL_PF_BIT(mctx->ctrl)
+        && dl->state != LAPDm_STATE_TIMER_RECOV) {
+               /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
                LOGP(DLAPDM, LOGL_NOTICE, "S frame response with F=1 error\n");
                rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
        }
@@ -1124,9 +1126,21 @@ static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
                                        "so we reply with RR frame\n");
                                lapdm_send_rnr(mctx, 1);
                        }
+               } else if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
+                       && LAPDm_CTRL_PF_BIT(mctx->ctrl)
+                       && dl->state == LAPDm_STATE_TIMER_RECOV) {
+                       LOGP(DLAPDM, LOGL_INFO, "RR response with F==1, "
+                               "and we are in timer recovery state, so "
+                               "we leave that state\n");
+                       /* V(S) to the N(R) in the RR frame */
+                       dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
+                       /* reset Timer T200 */
+                       bsc_del_timer(&dl->t200);
+                       /* 5.5.7 Clear timer recovery condition */
+                       lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
                }
                /* Send message, if possible due to acknowledged data */
-               rslms_send_i(mctx);
+               rslms_send_i(mctx, __LINE__);
 
                break;
        case LAPDm_S_RNR:
@@ -1159,7 +1173,7 @@ static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
                                LOGP(DLAPDM, LOGL_INFO, "RNR poll response "
                                        "and we in timer recovery state, so "
                                        "we leave that state\n");
-                               /* Clear timer recovery condition */
+                               /* 5.5.7 Clear timer recovery condition */
                                lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
                                /* V(S) to the N(R) in the RNR frame */
                                dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
@@ -1169,7 +1183,7 @@ static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
                                "received\n");
 
                /* Send message, if possible due to acknowledged data */
-               rslms_send_i(mctx);
+               rslms_send_i(mctx, __LINE__);
 
                break;
        case LAPDm_S_REJ:
@@ -1227,7 +1241,7 @@ static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
                                "recovery state received\n");
                        /* Clear an existing peer receiver busy condition */
                        dl->peer_busy = 0;
-                       /* Clear timer recovery condition */
+                       /* 5.5.7 Clear timer recovery condition */
                        lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
                        /* V(S) and V(A) to the N(R) in the REJ frame */
                        dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
@@ -1273,7 +1287,7 @@ static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
                /* FIXME: 5.5.4.2 2) */
 
                /* Send message, if possible due to acknowledged data */
-               rslms_send_i(mctx);
+               rslms_send_i(mctx, __LINE__);
 
                break;
        default:
@@ -1431,13 +1445,15 @@ static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx)
                /* check if we are not in own receiver busy */
                if (!dl->own_busy) {
                        /* NOTE: V(R) is already set above */
-                       rc = rslms_send_i(mctx);
+                       rc = rslms_send_i(mctx, __LINE__);
                        if (rc) {
                                LOGP(DLAPDM, LOGL_INFO, "we are not busy and "
                                        "have no pending data, send RR\n");
                                /* Send RR with F=0 */
                                return lapdm_send_rr(mctx, 0);
                        }
+                       /* all I or one RR is sent, we are done */
+                       return 0;
                } else {
                        LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
                        /* Send RNR with F=0 */
@@ -1446,7 +1462,7 @@ static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx)
        }
 
        /* Send message, if possible due to acknowledged data */
-       rslms_send_i(mctx);
+       rslms_send_i(mctx, __LINE__);
 
        return rc;
 }
@@ -1727,12 +1743,12 @@ static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
        msgb_enqueue(&dl->send_queue, msg);
 
        /* Send message, if possible */
-       rslms_send_i(&dl->mctx);
+       rslms_send_i(&dl->mctx, __LINE__);
        return 0;
 }
 
 /* Send next I frame from queued/buffered data */
-static int rslms_send_i(struct lapdm_msg_ctx *mctx)
+static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line)
 {
        struct lapdm_datalink *dl = mctx->dl;
        uint8_t chan_nr = mctx->chan_nr;
@@ -1741,7 +1757,9 @@ static int rslms_send_i(struct lapdm_msg_ctx *mctx)
        int k = k_sapi[sapi];
        struct msgb *msg;
        int length, left;
-       int rc = -1; /* we sent nothing */
+       int rc = - 1; /* we sent nothing */
+
+       LOGP(DLAPDM, LOGL_INFO, "%s() called from line %d\n", __func__, line);
 
        next_frame:
 
@@ -1860,13 +1878,15 @@ static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
 
        /* put back the send-buffer to the send-queue (first position) */
        if (dl->send_buffer) {
+               LOGP(DLAPDM, LOGL_INFO, "put frame in sendbuffer back to "
+                       "queue\n");
                llist_add(&dl->send_buffer->list, &dl->send_queue);
                dl->send_buffer = NULL;
-       }
+       } else
+               LOGP(DLAPDM, LOGL_INFO, "no frame in sendbuffer\n");
 
-       /* Clear transmit and send buffer, if any */
+       /* Clear transmit buffer, but keep send buffer */
        lapdm_dl_flush_tx(dl);
-       lapdm_dl_flush_send(dl);
 
        msgb_free(msg);
 
@@ -1899,13 +1919,19 @@ static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
        }
        length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
 
-       LOGP(DLAPDM, LOGL_INFO, "perform re-establishment (SABM)\n");
+       LOGP(DLAPDM, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
+               length);
        
        /* Replace message in the send-buffer (reconnect) */
        if (dl->send_buffer)
                msgb_free(dl->send_buffer);
        dl->send_out = 0;
-       dl->send_buffer = msg;
+       if (length) {
+               /* Remove the RSL/RLL header */
+               msgb_pull_l2h(msg);
+               /* Write data into the send buffer, to be sent first */
+               dl->send_buffer = msg;
+       }
 
        /* Discard partly received L3 message */
        if (dl->rcv_buffer) {
@@ -2026,7 +2052,8 @@ static int rslms_rx_chan_rqd(struct osmocom_ms *ms, struct msgb *msg)
        /* 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], cch->data[3]);
+       rc = l1ctl_tx_rach_req(ms, cch->data[1],
+               ((cch->data[2] & 0x7f) << 8) | cch->data[3], cch->data[2] >> 7);
 
        msgb_free(msg);
 
@@ -2052,7 +2079,7 @@ int l2_ph_chan_conf(struct msgb *msg, struct osmocom_ms *ms,
        ref = (struct gsm48_req_ref *) (ch->data + 1);
        ref->t1 = tm.t1;
        ref->t2 = tm.t2;
-       ref->t3_low = tm.t3 & 0x3;
+       ref->t3_low = tm.t3 & 0x7;
        ref->t3_high = tm.t3 >> 3;
        
        return rslms_sendmsg(msg, ms);
@@ -2125,7 +2152,8 @@ static struct l2downstate {
         RSL_MT_RES_REQ, rslms_rx_rll_res_req},
 
        /* create and send SABM command (reconnect) */
-       {SBIT(LAPDm_STATE_MF_EST) |
+       {SBIT(LAPDm_STATE_IDLE) |
+        SBIT(LAPDm_STATE_MF_EST) |
         SBIT(LAPDm_STATE_TIMER_RECOV),
         RSL_MT_RECON_REQ, rslms_rx_rll_res_req},