a54228dd5c1613a8c30494d5663ce8529c59260e
[o-du/l2.git] / src / 5gnrmac / mac_demux.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/ 
18
19 /* header include files -- defines (.h) */
20 #include "common_def.h"
21 #include "lrg.h"           /* Layer manager interface includes*/
22 #include "lrg.x"           /* layer management typedefs for MAC */
23 #include "du_app_mac_inf.h"
24 #include "mac_sch_interface.h"
25 #include "lwr_mac_upr_inf.h"
26 #include "mac.h"
27 #include "mac_utils.h"
28
29
30 /*******************************************************************
31  *
32  * @brief De-mux of MAC-Sub PDUs from Rx Data Ind Pdu
33  *
34  * @detail
35  *
36  *    Function : unpackRxData
37  *
38  *    Functionality:
39  *     De-mux of MAC-Sub PDUs from Rx Data Ind Pdu
40  *
41  * @params[in] Pointer to PDU received
42  *             PDU length
43  * @return ROK
44  *         RFAILED
45  *
46  * ****************************************************************/
47 uint8_t unpackRxData(uint16_t cellId, SlotTimingInfo slotInfo, RxDataIndPdu *rxDataIndPdu)
48 {
49    uint8_t   ueIdx = 0;       /* Iterator for UE list */
50    uint8_t   lcId = 0;        /* LC ID of a sub pdu */
51    uint8_t   fBit = 0;        /* Value of F Bit in MAC sub-header */
52    uint8_t   rxPduIdx = 0;         /* Iterator for received PDU */
53    uint16_t  length = 0;      /* Length of payload in a sub-PDU */ 
54    uint8_t   *pdu = NULLP;    /* Payload in sub-PDU */
55    uint16_t  pduLen = 0;      /* Length of undecoded PDU */
56    uint8_t   *rxDataPdu = NULLP;  /* Received PDU in Rx Data Ind */
57    uint16_t  cellIdx = 0;     /* Cell Index */
58    uint8_t   ret =ROK;
59
60    GET_CELL_IDX(cellId, cellIdx);
61    
62    if(rxDataIndPdu == NULLP)
63    {
64       DU_LOG("\nERROR --> MAC: Rx Data is empty");
65       return RFAILED;        
66    }
67
68    if(macCb.macCell[cellIdx] == NULLP)
69    {
70       DU_LOG("\nERROR --> CellId :%d is not created, as CellCB is empty", cellId);
71       return RFAILED;
72    }
73    pduLen = rxDataIndPdu->pduLength;
74    rxDataPdu = rxDataIndPdu->pduData;
75    GET_UE_IDX(rxDataIndPdu->rnti, ueIdx);
76    ueIdx = ueIdx -1;
77
78    while(pduLen > 0)
79    {
80       /* MSB in 1st octet is Reserved bit. Hence not decoding it. 
81          2nd MSB in 1st octet is R/F bit depending upon type of payload */
82       fBit = (1 << 7) & rxDataPdu[rxPduIdx];
83
84       /* LC id is the 6 LSB in 1st octet */
85       lcId = (~((~0) << 6)) & rxDataPdu[rxPduIdx];
86
87       pdu = NULLP;
88       switch(lcId)
89       {
90          case MAC_LCID_CCCH :
91             {
92                pduLen--;
93
94                /* for UL CCCH,fixed length of MAC SDU */
95                length = 6;
96               
97                /*  Allocating sharable memory to send ul ccch msg to du app*/
98                MAC_ALLOC_SHRABL_BUF(pdu, length);
99                if(!pdu)
100                {
101                   DU_LOG("\nERROR  -->  MAC : UL CCCH PDU memory allocation failed");
102                   return RFAILED;
103                }  
104                rxPduIdx++;
105                memcpy(pdu, &rxDataPdu[rxPduIdx], length);
106                pduLen -= length;
107                rxPduIdx = rxPduIdx + length;
108
109                /* store msg3 pdu in macRaCb for CRI value */
110                memcpy(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg3Pdu, pdu, length);
111
112                /* Send UL-CCCH Indication to DU APP */
113                ret = macProcUlCcchInd(macCb.macCell[cellIdx]->cellId, rxDataIndPdu->rnti, length, pdu);
114                break;
115             }
116
117          case MAC_LCID_MIN ... MAC_LCID_MAX :
118             {
119                DU_LOG("\nINFO   -->  MAC : PDU received for LC ID %d", lcId);
120                pduLen--;
121                rxPduIdx++;
122
123                length = rxDataPdu[rxPduIdx];
124                if(fBit)
125                {
126                   pduLen--;
127                   rxPduIdx++;
128                   length = (length << 8) & rxDataPdu[rxPduIdx];
129                }
130
131                pdu = NULLP;
132                /*  Copying the payload to send to RLC */
133                MAC_ALLOC_SHRABL_BUF(pdu, length);
134                if(!pdu)
135                {
136                   DU_LOG("\nERROR  -->  MAC : Memory allocation failed while demuxing Rx Data PDU");
137                   return RFAILED;
138                }
139                pduLen--;
140                rxPduIdx++;
141                memcpy(pdu, &rxDataPdu[rxPduIdx], length);
142                pduLen -= length;
143                rxPduIdx = rxPduIdx + length;
144
145                /* Delete RA cb once RRC setup complete received */
146                if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == rxDataIndPdu->rnti)
147                {
148                   MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
149                         macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
150                   MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, \
151                         macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TbSize - TX_PAYLOAD_HDR_LEN);
152                   memset(&macCb.macCell[cellIdx]->macRaCb[ueIdx], 0, sizeof(MacRaCbInfo));
153                }
154
155                /* Send UL Data to RLC */
156                ret = macProcUlData(cellId, rxDataIndPdu->rnti, slotInfo, lcId, length, pdu);
157
158                break;
159             }
160          case MAC_LCID_RESERVED_MIN ... MAC_LCID_RESERVED_MAX :
161             break;
162
163          case MAC_LCID_CCCH_48BIT :
164             break;
165
166          case MAC_LCID_BIT_RATE_QUERY :
167             break;
168
169          case MAC_LCID_MULT_PHR_FOUR_OCT :
170             break;
171
172          case MAC_LCID_CFG_GRANT_CFM :
173             break;
174
175          case MAC_LCID_MULT_PHR_ONE_OCT:
176             break;
177
178          case MAC_LCID_SINGLE_PHR :
179             break;
180
181          case MAC_LCID_CRNTI :
182             break;
183
184          case MAC_LCID_SHORT_TRUNC_BSR :
185             break;
186
187          case MAC_LCID_LONG_TRUNC_BSR :
188             break;
189
190          case MAC_LCID_SHORT_BSR :
191             {
192                uint8_t  lcgId         = 0;
193                uint8_t  bufferSizeIdx = 0;
194                uint8_t  crnti         = 0;
195                uint32_t bufferSize    = 0;
196
197                pduLen--;
198
199                rxPduIdx++;
200                crnti = rxDataIndPdu->rnti;
201                /* 5 LSB bits in pdu represent buffer size */
202                bufferSizeIdx = (~((~0) << 5)) & rxDataPdu[rxPduIdx];
203                /* first 3 MSB bits in pdu represent LCGID */
204                lcgId = (rxDataPdu[rxPduIdx]) >> 5;
205                /* determine actual number of bytes requested */
206                bufferSize = shortBsrBytesTable[bufferSizeIdx];
207                ret = macProcShortBsr(macCb.macCell[cellIdx]->cellId, crnti, lcgId, bufferSize);
208                pduLen--;
209                rxPduIdx++;
210
211                break;
212             }
213
214          case MAC_LCID_LONG_BSR :
215             {
216                DataVolInfo dataVolInfo[MAX_NUM_LOGICAL_CHANNEL_GROUPS];
217                memset(dataVolInfo, 0,MAX_NUM_LOGICAL_CHANNEL_GROUPS * sizeof(DataVolInfo));
218                uint8_t  lcgIdx        = 0;
219                uint8_t  crnti         = 0;
220                uint8_t  numLcg        = 0;
221                uint8_t  lcgIdxPos     = 0;
222                pduLen--;
223
224                rxPduIdx++;/*To reach the Octet where lcgIdx will be present*/
225                crnti = rxDataIndPdu->rnti;
226
227                lcgIdxPos = rxPduIdx;
228
229                pduLen--;
230                rxPduIdx++;/*To reach the Octet where bsrIdx starts*/
231                for(lcgIdx = 0; lcgIdx < MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcgIdx++)
232                {
233                   if(rxDataPdu[lcgIdxPos]  & (1 << lcgIdx))
234                   {
235                      if(rxDataPdu[rxPduIdx] > 0 && rxDataPdu[rxPduIdx] < MAX_LONG_BSR_TABLE_ENTRIES)
236                      {
237                         dataVolInfo[numLcg].dataVol = longBsrBytesTable[rxDataPdu[rxPduIdx]];
238                         dataVolInfo[numLcg].lcgId = lcgIdx;
239                         numLcg++;
240                      }
241                      else
242                      {
243                         DU_LOG("\nERROR  -->  MAC: Invalid BsrIdx:%d rcvd for lcgIdx:%d",lcgIdx,rxDataPdu[rxPduIdx]);
244                      }
245                      /*next byte in PDU*/
246                      pduLen--;
247                      rxPduIdx++;
248                   }
249                }
250
251                ret = macProcLongBsr(macCb.macCell[cellIdx]->cellId, crnti, numLcg, dataVolInfo);
252
253                break;
254             }
255
256          case MAC_LCID_PADDING :
257             break;
258
259          default:
260             {
261                DU_LOG("\nERROR  -->  MAC : Invalid LC Id %d", lcId);
262                return RFAILED;
263             }
264       } /* End of switch */
265
266       if(lcId == MAC_LCID_PADDING)
267       {
268          break;
269       }
270    } /* End of While */
271
272    return ret;
273 } /* End of unpackRxData */
274
275 /**********************************************************************
276   End of file
277  **********************************************************************/