[Task-ID: ODUHIGH-433]Fixed the mismatch between UE ID (calculated from CRNTI) and...
[o-du/l2.git] / src / 5gnrmac / mac_mux.c
index ddc2476..82a63c5 100644 (file)
 /* header include files -- defines (.h) */
 #include "common_def.h"
 #include "lrg.h"           /* Layer manager interface includes*/
-#include "crg.h"           /* CRG interface includes*/
-#include "rgu.h"           /* RGU interface includes*/
-#include "tfu.h"           /* TFU interface includes */
-#include "rg_sch_inf.h"    /* SCH interface includes */
-#include "rg_prg.h"       /* PRG (MAC-MAC) interface includes*/
-#include "rg_env.h"       /* MAC environmental includes*/
-#include "rg.h"           /* MAC includes*/
-#include "rg_err.h"       /* MAC error includes*/
-#include "du_log.h"
-
-/* header/extern include files (.x) */
-#include "rgu.x"           /* RGU types */
-#include "tfu.x"           /* RGU types */
 #include "lrg.x"           /* layer management typedefs for MAC */
-#include "crg.x"           /* CRG interface includes */
-#include "rg_sch_inf.x"    /* SCH interface typedefs */
-#include "rg_prg.x"        /* PRG (MAC-MAC) Interface typedefs */
 #include "du_app_mac_inf.h"
+#include "mac_sch_interface.h"
+#include "lwr_mac_upr_inf.h"
 #include "mac.h"
-#include "rg.x"            /* typedefs for MAC */
+#include "mac_utils.h"
 
 /*******************************************************************
  *
  * @return void
  *
  * ****************************************************************/
-void packBytes(uint8_t *buf, uint8_t *bytePos, uint8_t *bitPos, uint32_t val, uint8_t valSize)
+void packBytes(uint8_t *buf, uint16_t *bytePos, uint8_t *bitPos, uint32_t val, uint8_t valSize)
 {
    uint32_t  temp;
    uint8_t   bytePart1;
    uint32_t  bytePart2;
    uint8_t   bytePart1Size;
    uint32_t  bytePart2Size;
-  
+
    if(*bitPos - valSize + 1 >= 0)
    {
       bytePart1 = (uint8_t)val;
@@ -71,11 +57,11 @@ void packBytes(uint8_t *buf, uint8_t *bytePos, uint8_t *bitPos, uint32_t val, ui
       buf[*bytePos] |= bytePart1;
       if(*bitPos - valSize < 0)
       {
-         *bitPos = 7;
-         (*bytePos)++;
+        *bitPos = 7;
+        (*bytePos)++;
       }
       else
-         *bitPos -= valSize;
+        *bitPos -= valSize;
    }
    else
    {
@@ -85,7 +71,7 @@ void packBytes(uint8_t *buf, uint8_t *bytePos, uint8_t *bitPos, uint32_t val, ui
 
       bytePart1 = (val >> bytePart2Size) << (*bitPos -bytePart1Size +1);
       bytePart2 =  (~((~temp) << bytePart2Size)) & val;
+
       buf[*bytePos] |= bytePart1;
       (*bytePos)++;
       *bitPos = 7;
@@ -111,82 +97,115 @@ void packBytes(uint8_t *buf, uint8_t *bytePos, uint8_t *bitPos, uint32_t val, ui
 void fillRarPdu(RarInfo *rarInfo)
 {
    uint8_t   *rarPdu = rarInfo->rarPdu;
-   uint16_t  totalBits = 0;
-   uint8_t   numBytes = 0;
-   uint8_t   bytePos= 0;
    uint8_t   bitPos = 0;
+   uint16_t  bytePos= 0, bwpSize = 0, rbStart = 0, rbLen = 0;
+   uint8_t   numHopInfoBitsInFreqAlloc = 0;
+   uint8_t   actualFreqRsrcAllocSize = 0;
 
    /* RAR subheader fields */
    uint8_t   EBit = 0;
    uint8_t   TBit = 0;
-   uint8_t   rapId = 0;
 
    /* RAR payload fields */
    uint8_t   RBit = 0;
-   uint16_t  timeAdv = 0;
-   uint32_t  ulGrant = 0;
-   uint16_t  tmpCrnti = 0; 
-       uint8_t   paddingLcid = 63;
+   uint16_t  msg3FreqResource = 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;
-   uint8_t   timeAdvSize = 12;
-   uint8_t   ulGrantSize = 27;
-   uint8_t   tmpCrntiSize = 16;
+   /* Considering 2 bytes of padding in RAR PDU. 
+    * 1st byte is MAC sub-header for padding
+    * 2nd byte contains padding bits
+    */
+   uint8_t   paddingSize = 8;
 
    /* Fill RAR pdu fields */
    EBit = 0;
    TBit = 1;
-   rapId = rarInfo->RAPID;
-   
    RBit = 0;
-   timeAdv = rarInfo->ta;
-   ulGrant = 0; /* this will be done when implementing msg3 */ 
-   tmpCrnti = rarInfo->tcrnti;
 
-   /* 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;
-   if(totalBits % 8)
-      numBytes += 1;
-    
-       rarInfo->rarPduLen = numBytes;
+   rarInfo->rarPduLen = RAR_PAYLOAD_SIZE;
 
    /* Initialize buffer */
-   for(bytePos = 0; bytePos < numBytes; bytePos++)
+   for(bytePos = 0; bytePos < rarInfo->rarPduLen; bytePos++)
       rarPdu[bytePos] = 0;
-   
+
    bytePos = 0;
    bitPos = 7;
 
    /* Packing fields into RAR PDU */
-   packBytes(rarPdu, &bytePos, &bitPos, EBit, EBitSize); 
-   packBytes(rarPdu, &bytePos, &bitPos, TBit, TBitSize);
-   packBytes(rarPdu, &bytePos, &bitPos, rapId, rapidSize);
-   packBytes(rarPdu, &bytePos, &bitPos, RBit, RBitSize);
-   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, EBit, E_BIT_SIZE); 
+   packBytes(rarPdu, &bytePos, &bitPos, TBit, T_BIT_SIZE);
+   packBytes(rarPdu, &bytePos, &bitPos, rarInfo->RAPID, RAPID_SIZE);
+   packBytes(rarPdu, &bytePos, &bitPos, RBit, R_BIT_SIZE);
+   packBytes(rarPdu, &bytePos, &bitPos, rarInfo->ta, TIMING_ADVANCE_SIZE);
+   /* Packing MSG3 UL Grant in RAR */
+   packBytes(rarPdu, &bytePos, &bitPos, rarInfo->ulGrant.freqHopFlag, FREQ_HOP_FLAG_SIZE);
+
+   /* Calculating freq domain resource allocation field value
+    * bwpSize = Size of BWP
+    * RBStart = Starting Resource block
+    * RBLen = length of contiguously allocted RBs
+    * Spec 38.214 Sec 6.1.2.2.2
+    */
+   bwpSize = rarInfo->ulGrant.bwpSize;
+   rbStart = rarInfo->ulGrant.msg3FreqAlloc.startPrb;
+   rbLen = rarInfo->ulGrant.msg3FreqAlloc.numPrb;
+
+   if((rbLen >=1) && (rbLen <= bwpSize - rbStart))
+   {
+      if((rbLen - 1) <= floor(bwpSize / 2))
+         msg3FreqResource = (bwpSize * (rbLen-1)) + rbStart;
+      else
+         msg3FreqResource = (bwpSize * (bwpSize - rbLen + 1)) \
+                            + (bwpSize - 1 - rbStart);
+   }
+
+   /* Calculating frequency domain resource allocation field size 
+    * and packing frequency domain resource allocation accordingly 
+    * Spec 38.213 Sec 8.3 
+    */
+   if(bwpSize < 180)
+   {
+      actualFreqRsrcAllocSize = ceil(log2(bwpSize * (bwpSize + 1) / 2));
+      packBytes(rarPdu, &bytePos, &bitPos, 0, FREQ_RSRC_ALLOC_SIZE - actualFreqRsrcAllocSize);
+      packBytes(rarPdu, &bytePos, &bitPos, msg3FreqResource, actualFreqRsrcAllocSize);
+   }
+   else
+   {
+      if(rarInfo->ulGrant.freqHopFlag == 0)
+      {
+         numHopInfoBitsInFreqAlloc = 1;
+         packBytes(rarPdu, &bytePos, &bitPos, 0, numHopInfoBitsInFreqAlloc);
+         
+         actualFreqRsrcAllocSize = abs(log2(bwpSize * (bwpSize + 1) / 2));
+         packBytes(rarPdu, &bytePos, &bitPos, 0, actualFreqRsrcAllocSize - FREQ_RSRC_ALLOC_SIZE);
+         packBytes(rarPdu, &bytePos, &bitPos, msg3FreqResource, \
+            actualFreqRsrcAllocSize - numHopInfoBitsInFreqAlloc);
+      }
+      else
+      {
+         /* TODO : If frequency hopping is supported,
+          * Fetch the Number of bits to store hopping information in frequency
+          * resource allocation field and the value to be filled from Spec 38.213, Table 8.3-1. 
+          * Fill the frequency resource allocation field as described in Spec 38.213 sec 8.3
+          */
+      }
+   }
+
+   /* Packing time domain resource allocation for UL grant */
+   packBytes(rarPdu, &bytePos, &bitPos, rarInfo->ulGrant.k2Index, TIME_RSRC_ALLOC_SIZE);
+
+   packBytes(rarPdu, &bytePos, &bitPos, rarInfo->ulGrant.mcs, MCS_SIZE);
+   packBytes(rarPdu, &bytePos, &bitPos, rarInfo->ulGrant.tpc, TPC_COMMAND_SIZE);
+   packBytes(rarPdu, &bytePos, &bitPos, rarInfo->ulGrant.csiReq, CSI_REQUEST_SIZE);
+
+   packBytes(rarPdu, &bytePos, &bitPos, rarInfo->tcrnti, T_CRNTI_SIZE);
+
+   /* padding of 2 bytes */
+   packBytes(rarPdu, &bytePos, &bitPos, RBit, R_BIT_SIZE*2);
+   packBytes(rarPdu, &bytePos, &bitPos, paddingLcid, LC_ID_SIZE);
    packBytes(rarPdu, &bytePos, &bitPos, 0, paddingSize);
-       
+
 }
 
 /*******************************************************************
@@ -205,11 +224,30 @@ void fillRarPdu(RarInfo *rarInfo)
  * @return void
  *
  * ****************************************************************/
-void createMacRaCb(uint16_t cellId, uint16_t crnti)
+void createMacRaCb(RachIndInfo *rachIndInfo)
 {
-   uint8_t idx = 0; /* supporting 1 UE */
-   macCb.macCell->macRaCb[idx].cellId = cellId;
-   macCb.macCell->macRaCb[idx].crnti = crnti;
+   int8_t ueIdx = -1;
+   uint16_t crnti = 0;
+   uint16_t cellIdx = 0;
+
+   GET_CELL_IDX(rachIndInfo->cellId, cellIdx);
+   
+   ueIdx = getFreeBitFromUeBitMap(rachIndInfo->cellId);
+   if(ueIdx == -1)
+   {
+      DU_LOG("\nERROR  -->  MAC : Failed to find free UE Idx in UE bit map of cell Id [%d]", rachIndInfo->cellId);
+      return;
+   }
+
+   /* Calculate CRNTI from UE Index */
+   GET_CRNTI(crnti, ueIdx+1);
+
+   /* store in rach ind structure */
+   rachIndInfo->crnti  = crnti;
+
+   /* store in raCb */
+   macCb.macCell[cellIdx]->macRaCb[ueIdx].cellId = rachIndInfo->cellId;
+   macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti  = crnti;
 }
 
 /*************************************************
@@ -225,17 +263,12 @@ void createMacRaCb(uint16_t cellId, uint16_t crnti)
  *             msg4Pdu pointer
  ************************************************/
 
-void fillMsg4DlData(MacDlData *dlData, uint8_t *msg4Pdu)
+void fillMsg4DlData(MacDlData *dlData, uint16_t msg4PduLen, uint8_t *msg4Pdu)
 {
-   uint8_t idx = 0;
-   uint16_t idx2;
-   dlData->numPdu = 1;
-   dlData->pduInfo[idx].lcId = MAC_LCID_CCCH;
-   dlData->pduInfo[idx].pduLen = macCb.macCell->macRaCb[idx].msg4PduLen;
-   for(idx2 = 0; idx2 <  dlData->pduInfo[idx].pduLen; idx2++)
-       {
-      dlData->pduInfo[idx].dlPdu[idx2] = msg4Pdu[idx2];
-       }
+   dlData->pduInfo[dlData->numPdu].lcId = MAC_LCID_CCCH;
+   dlData->pduInfo[dlData->numPdu].pduLen = msg4PduLen;
+   memcpy(dlData->pduInfo[dlData->numPdu].dlPdu, msg4Pdu, msg4PduLen);
+   dlData->numPdu++;
 }
 
 /*************************************************
@@ -258,7 +291,7 @@ void fillMacCe(MacCeInfo *macCeInfo, uint8_t *msg3Pdu)
    {
       macCeInfo->macCe[idx].macCeLcid = MAC_LCID_CRI;
       memcpy(macCeInfo->macCe[idx].macCeValue, \
-         msg3Pdu, MAX_CRI_SIZE);
+           msg3Pdu, MAX_CRI_SIZE);
    }
 }
 
@@ -273,22 +306,22 @@ void fillMacCe(MacCeInfo *macCeInfo, uint8_t *msg3Pdu)
  *    Functionality:
  *     The MAC PDU will be MUXed and formed
  *
- * @params[in] MacDlData *, MacCeInfo *, msg4TxPdu *, tbSize
+ * @params[in] MacDlData *, MacCeInfo *, txPdu *, tbSize
  * @return void
  * ****************************************************************/
 
-void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *msg4TxPdu, uint16_t tbSize)
+void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *txPdu, uint16_t tbSize)
 {
-   uint8_t bytePos = 0;
+   uint16_t bytePos = 0;
    uint8_t bitPos = 7;
-   uint8_t idx = 0;
+   uint8_t pduIdx = 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 FBit =0;                  /* Format Indicator */
+   uint8_t lcid =0;                  /* LCID */
    uint16_t lenField = 0;         /* Length field */
 
    /* subheader field size (in bits) */
@@ -298,62 +331,65 @@ void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *msg4TxPdu, uint
    uint8_t lenFieldSize = 0;      /* 8-bit or 16-bit L field  */
 
    /* PACK ALL MAC CE */
-   for(idx = 0; idx < macCeData->numCes; idx++)
+   if(macCeData != NULLP)
    {
-      lcid = macCeData->macCe[idx].macCeLcid;
-      switch(lcid)
+      for(pduIdx = 0; pduIdx < macCeData->numCes; pduIdx++)
       {
-         case MAC_LCID_CRI:
-        {
-            /* Packing fields into MAC PDU R/R/LCID */
-            packBytes(macPdu, &bytePos, &bitPos, RBit, (RBitSize * 2));
-            packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
-            memcpy(&macPdu[bytePos], macCeData->macCe[idx].macCeValue,\
-            MAX_CRI_SIZE);
-                               bytePos += MAX_CRI_SIZE;
-            break;
+         lcid = macCeData->macCe[pduIdx].macCeLcid;
+         switch(lcid)
+         {
+            case MAC_LCID_CRI:
+               {
+                  /* Packing fields into MAC PDU R/R/LCID */
+                  packBytes(macPdu, &bytePos, &bitPos, RBit, (RBitSize * 2));
+                  packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
+                  memcpy(&macPdu[bytePos], macCeData->macCe[pduIdx].macCeValue,\
+                        MAX_CRI_SIZE);
+                  bytePos += MAX_CRI_SIZE;
+                  break;
+               }
+            default:
+               DU_LOG("\nERROR  -->  MAC: Invalid LCID %d in mac pdu",lcid);
+               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++)
+   for(pduIdx = 0; pduIdx < dlData->numPdu; pduIdx++)
    {
-      lcid = dlData->pduInfo[idx].lcId;
+      lcid = dlData->pduInfo[pduIdx].lcId;
       switch(lcid)
       {
          case MAC_LCID_CCCH:
-        {
-            lenField = dlData->pduInfo[idx].pduLen;
-            if(dlData->pduInfo[idx].pduLen > 255)
+         case MAC_LCID_MIN ... MAC_LCID_MAX :
             {
-               FBit = 1;
-               lenFieldSize = 16;
-
-            }
-            else
-            {
-               FBit = 0;
-               lenFieldSize = 8;
+               lenField = dlData->pduInfo[pduIdx].pduLen;
+               if(dlData->pduInfo[pduIdx].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[pduIdx].dlPdu, lenField);
+               bytePos += lenField;
+               break;
             }
-            /* 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);
-                               bytePos += lenField;
-            break;
-        }
 
          default:
-            DU_LOG("\n MAC: Invalid LCID %d in mac pdu",lcid);
+            DU_LOG("\nERROR  -->  MAC: Invalid LCID %d in mac pdu",lcid);
             break;
       }
-
    }
    if(bytePos < tbSize && (tbSize-bytePos >= 1))
    {
@@ -364,10 +400,10 @@ void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *msg4TxPdu, uint
       packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
    }
 
-       /*Storing the muxed pdu in macRaCb */
-   if(msg4TxPdu != NULLP)
+   /*Storing the muxed pdu */
+   if(txPdu != NULLP)
    {
-          memcpy(msg4TxPdu, macPdu, tbSize);
+      memcpy(txPdu, macPdu, tbSize);
    }
 }