Addressing flow wise comments
[o-du/l2.git] / src / 5gnrmac / mac_mux.c
index d9321f4..dcab8fd 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdbool.h>
 
 /* header include files -- defines (.h) */
 #include "envopt.h"        /* environment options */
 #include "mac.h"
 #include "rg.x"            /* typedefs for MAC */
 
+/*******************************************************************
+ *
+ * @brief pack the bits
+ *
+ * @details
+ *
+ *    Function : packBytes
+ *
+ *    Functionality:
+ *     pack the bits in the corresponding byte
+ *
+ * @params[in] buffer pointer, byte and bit position, value and its size
+ * @return void
+ *
+ * ****************************************************************/
 void packBytes(uint8_t *buf, uint8_t *bytePos, uint8_t *bitPos, uint32_t val, uint8_t valSize)
 {
    uint32_t  temp;
@@ -98,29 +114,48 @@ void packBytes(uint8_t *buf, uint8_t *bytePos, uint8_t *bitPos, uint32_t val, ui
    }  
 }
 
+/*******************************************************************
+ *
+ * @brief fill the RAR PDU
+ *
+ * @details
+ *
+ *    Function : fillRarPdu
+ *
+ *    Functionality:
+ *     The RAR PDU will be MUXed and formed
+ *
+ * @params[in] RAR info
+ * @return void
+ *
+ * ****************************************************************/
 void fillRarPdu(RarInfo *rarInfo)
 {
    uint8_t   *rarPdu = rarInfo->rarPdu;
-   uint16_t  totalBits;
-   uint8_t   numBytes;
-   uint8_t   bytePos;
-   uint8_t   bitPos;
+   uint16_t  totalBits = 0;
+   uint8_t   numBytes = 0;
+   uint8_t   bytePos= 0;
+   uint8_t   bitPos = 0;
 
    /* RAR subheader fields */
-   uint8_t   EBit;
-   uint8_t   TBit;
-   uint8_t   rapId;
+   uint8_t   EBit = 0;
+   uint8_t   TBit = 0;
+   uint8_t   rapId = 0;
 
    /* RAR payload fields */
-   uint8_t   RBit;
-   uint16_t  timeAdv;
-   uint32_t  ulGrant;
-   uint16_t  tmpCrnti; 
+   uint8_t   RBit = 0;
+   uint16_t  timeAdv = 0;
+   uint32_t  ulGrant = 0;
+   uint16_t  tmpCrnti = 0; 
+       uint8_t   paddingLcid = 63;
 
    /* Size(in bits) of RAR subheader files */
    uint8_t   EBitSize = 1;
    uint8_t   TBitSize = 1;
    uint8_t   rapidSize = 6;
+       uint8_t   paddingLcidSize = 6;
+       uint8_t   paddingSize = 8;
+
 
    /* Size(in bits) of RAR payload fields */
    uint8_t   RBitSize = 1;
@@ -141,6 +176,9 @@ void fillRarPdu(RarInfo *rarInfo)
    /* Calulating total number of bytes in buffer */
    totalBits = EBitSize + TBitSize + rapidSize + RBitSize + timeAdvSize \
      + ulGrantSize + tmpCrntiSize;
+
+   /* add padding size */
+       totalBits += RBitSize*2 + paddingLcidSize + paddingSize;
    
    /* Calulating total number of bytes in buffer */
    numBytes = totalBits/8;
@@ -164,6 +202,190 @@ void fillRarPdu(RarInfo *rarInfo)
    packBytes(rarPdu, &bytePos, &bitPos, timeAdv, timeAdvSize);
    packBytes(rarPdu, &bytePos, &bitPos, ulGrant, ulGrantSize);
    packBytes(rarPdu, &bytePos, &bitPos, tmpCrnti, tmpCrntiSize);
+
+       /* padding of 2 bytes */
+   packBytes(rarPdu, &bytePos, &bitPos, RBit, RBitSize*2);
+   packBytes(rarPdu, &bytePos, &bitPos, paddingLcid, paddingLcidSize);
+   packBytes(rarPdu, &bytePos, &bitPos, 0, paddingSize);
+       
+}
+
+/*******************************************************************
+ *
+ * @brief Database required to form MAC PDU
+ *
+ * @details
+ *
+ *    Function : createMacRaCb
+ *
+ *    Functionality:
+ *     stores the required params for muxing
+ *
+ * @params[in] Pointer to cellId,
+ *                        crnti
+ * @return void
+ *
+ * ****************************************************************/
+void createMacRaCb(uint16_t cellId, uint16_t crnti)
+{
+   uint8_t idx = 0; /* supporting 1 UE */
+   macCb.macCell->macRaCb[idx].cellId = cellId;
+   macCb.macCell->macRaCb[idx].crnti = crnti;
+}
+
+/*************************************************
+ * @brief fill RLC DL Data
+ *
+ * @details
+ *
+ * Function : fillMsg4DlData
+ *      This function is a stub which sends Dl Data
+ *      to form MAC SDUs
+ *           
+ * @param[in]  MacDlData *dlData
+ ************************************************/
+
+void fillMsg4DlData(MacDlData *dlData)
+{
+   uint8_t idx = 0;
+   dlData->numPdu = 1;
+   dlData->pduInfo[idx].lcId = MAC_LCID_CCCH;
+   dlData->pduInfo[idx].pduLen = macCb.macCell->macRaCb[0].msg4PduLen;
+   memcpy(dlData->pduInfo[idx].dlPdu, macCb.macCell->macRaCb[0].msg4Pdu,\
+    macCb.macCell->macRaCb[0].msg4PduLen);
+}
+
+/*************************************************
+ * @brief fill Mac Ce Info
+ *
+ * @details
+ *
+ * Function : fillMacCe
+ *      This function fills Mac ce identities
+ *           
+ * @param[in]  RlcMacData *dlData
+ ************************************************/
+
+void fillMacCe(MacCeInfo *macCeInfo)
+{
+   uint8_t idx;
+   macCeInfo->numCes = 1;
+   for(idx = 0; idx < macCeInfo->numCes; idx++)
+   {
+      macCeInfo->macCe[idx].macCeLcid = MAC_LCID_CRI;
+      memcpy(&macCeInfo->macCe[idx].macCeValue, \
+         &macCb.macCell->macRaCb[idx].msg3Pdu, MAX_CRI_SIZE);
+   }
+}
+
+/*******************************************************************
+ *
+ * @brief Forms MAC PDU
+ *
+ * @details
+ *
+ *    Function : buildMacPdu
+ *
+ *    Functionality:
+ *     The MAC PDU will be MUXed and formed
+ *
+ * @params[in] MacDlData *, MacCeInfo *, tbSize
+ * @return void
+ *
+ * ****************************************************************/
+
+void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint16_t tbSize)
+{
+   uint8_t bytePos = 0;
+   uint8_t bitPos = 7;
+   uint8_t idx = 0;
+   uint8_t macPdu[tbSize];
+   memset(macPdu, 0, (tbSize * sizeof(uint8_t)));
+
+   /* subheader fields */
+   uint8_t RBit = 0;              /* Reserved bit */
+   uint8_t FBit;                  /* Format Indicator */
+   uint8_t lcid;                  /* LCID */
+   uint8_t lenField = 0;          /* Length field */
+
+   /* subheader field size (in bits) */
+   uint8_t RBitSize = 1;
+   uint8_t FBitSize = 1;
+   uint8_t lcidSize = 6;
+   uint8_t lenFieldSize = 0;          /* 8-bit or 16-bit L field  */
+
+   /* PACK ALL MAC CE */
+   for(idx = 0; idx < macCeData->numCes; idx++)
+   {
+      lcid = macCeData->macCe[idx].macCeLcid;
+      switch(lcid)
+      {
+         case MAC_LCID_CRI:
+        {
+            /* Packing fields into MAC PDU R/R/LCID */
+            packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
+            packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
+            packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
+            memcpy(&macPdu[bytePos], macCeData->macCe[idx].macCeValue,\
+            MAX_CRI_SIZE);
+            break;
+         }
+         default:
+            DU_LOG("\n MAC: Invalid LCID %d in mac pdu",lcid);
+            break;
+      }
+   }
+
+   /* PACK ALL MAC SDUs */
+   for(idx = 0; idx < dlData->numPdu; idx++)
+   {
+      lcid = dlData->pduInfo[idx].lcId;
+      lenField = dlData->pduInfo[idx].pduLen;
+      switch(lcid)
+      {
+         case MAC_LCID_CCCH:
+        {
+            if(dlData->pduInfo[idx].pduLen > 255)
+            {
+               FBit = 1;
+               lenFieldSize = 16;
+
+            }
+            else
+            {
+               FBit = 0;
+               lenFieldSize = 8;
+            }
+            /* Packing fields into MAC PDU R/F/LCID/L */
+            packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
+            packBytes(macPdu, &bytePos, &bitPos, FBit, FBitSize);
+            packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
+            packBytes(macPdu, &bytePos, &bitPos, lenField, lenFieldSize);
+            memcpy(&macPdu[bytePos], dlData->pduInfo[idx].dlPdu, lenField);
+            break;
+        }
+
+         default:
+            DU_LOG("\n MAC: Invalid LCID %d in mac pdu",lcid);
+            break;
+      }
+
+   }
+   if(bytePos < tbSize && (tbSize-bytePos >= 1))
+   {
+      /* padding remaining bytes */
+      RBitSize = 2;
+      lcid = MAC_LCID_PADDING;
+      packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
+      packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
+   }
+
+   MAC_ALLOC(macCb.macCell->macRaCb[0].msg4TxPdu, macCb.macCell->macRaCb[0].msg4TbSize);
+   if(macCb.macCell->macRaCb[0].msg4TxPdu != NULLP)
+   {
+      memcpy(macCb.macCell->macRaCb[0].msg4TxPdu, macPdu,\
+         macCb.macCell->macRaCb[0].msg4TbSize);
+   }
 }
 
 /**********************************************************************