Renaming GET_UE_IDX to GET_UE_ID in 5gnrmac [Issue-ID: ODUHIGH-401]
[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   ueId = 0;        /* UE Identity */
50    uint8_t   ueIdx = 0;       /* Iterator for UE list */
51    uint8_t   lcId = 0;        /* LC ID of a sub pdu */
52    uint8_t   fBit = 0;        /* Value of F Bit in MAC sub-header */
53    uint8_t   rxPduIdx = 0;    /* Iterator for received PDU */
54    uint16_t  length = 0;      /* Length of payload in a sub-PDU */ 
55    uint8_t   *pdu = NULLP;    /* Payload in sub-PDU */
56    uint16_t  pduLen = 0;      /* Length of undecoded PDU */
57    uint8_t   *rxDataPdu = NULLP;  /* Received PDU in Rx Data Ind */
58    uint16_t  cellIdx = 0;     /* Cell Index */
59    uint8_t   ret =ROK;
60
61    GET_CELL_IDX(cellId, cellIdx);
62    
63    if(rxDataIndPdu == NULLP)
64    {
65       DU_LOG("\nERROR --> MAC: Rx Data is empty");
66       return RFAILED;        
67    }
68
69    if(macCb.macCell[cellIdx] == NULLP)
70    {
71       DU_LOG("\nERROR --> CellId :%d is not created, as CellCB is empty", cellId);
72       return RFAILED;
73    }
74    pduLen = rxDataIndPdu->pduLength;
75    rxDataPdu = rxDataIndPdu->pduData;
76    GET_UE_ID(rxDataIndPdu->rnti, ueId);
77    ueIdx = ueId -1;
78
79    while(pduLen > 0)
80    {
81       /* MSB in 1st octet is Reserved bit. Hence not decoding it. 
82          2nd MSB in 1st octet is R/F bit depending upon type of payload */
83       fBit = (1 << 7) & rxDataPdu[rxPduIdx];
84
85       /* LC id is the 6 LSB in 1st octet */
86       lcId = (~((~0) << 6)) & rxDataPdu[rxPduIdx];
87
88       pdu = NULLP;
89       switch(lcId)
90       {
91          case MAC_LCID_CCCH :
92             {
93                pduLen--;
94
95                /* for UL CCCH,fixed length of MAC SDU */
96                length = 6;
97               
98                /*  Allocating sharable memory to send ul ccch msg to du app*/
99                MAC_ALLOC_SHRABL_BUF(pdu, length);
100                if(!pdu)
101                {
102                   DU_LOG("\nERROR  -->  MAC : UL CCCH PDU memory allocation failed");
103                   return RFAILED;
104                }  
105                rxPduIdx++;
106                memcpy(pdu, &rxDataPdu[rxPduIdx], length);
107                pduLen -= length;
108                rxPduIdx = rxPduIdx + length;
109
110                /* store msg3 pdu in macRaCb for CRI value */
111                memcpy(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg3Pdu, pdu, length);
112
113                /* Send UL-CCCH Indication to DU APP */
114                ret = macProcUlCcchInd(macCb.macCell[cellIdx]->cellId, rxDataIndPdu->rnti, length, pdu);
115                break;
116             }
117
118          case MAC_LCID_MIN ... MAC_LCID_MAX :
119             {
120                DU_LOG("\nINFO   -->  MAC : PDU received for LC ID %d", lcId);
121                pduLen--;
122                rxPduIdx++;
123
124                length = rxDataPdu[rxPduIdx];
125                if(fBit)
126                {
127                   pduLen--;
128                   rxPduIdx++;
129                   length = (length << 8) & rxDataPdu[rxPduIdx];
130                }
131
132                pdu = NULLP;
133                /*  Copying the payload to send to RLC */
134                MAC_ALLOC_SHRABL_BUF(pdu, length);
135                if(!pdu)
136                {
137                   DU_LOG("\nERROR  -->  MAC : Memory allocation failed while demuxing Rx Data PDU");
138                   return RFAILED;
139                }
140                pduLen--;
141                rxPduIdx++;
142                memcpy(pdu, &rxDataPdu[rxPduIdx], length);
143                pduLen -= length;
144                rxPduIdx = rxPduIdx + length;
145
146                /* Delete RA cb once RRC setup complete received */
147                if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == rxDataIndPdu->rnti)
148                {
149                   MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
150                         macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
151                   MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, \
152                         macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TbSize - TX_PAYLOAD_HDR_LEN);
153                   memset(&macCb.macCell[cellIdx]->macRaCb[ueIdx], 0, sizeof(MacRaCbInfo));
154                }
155
156                /* Send UL Data to RLC */
157                ret = macProcUlData(cellId, rxDataIndPdu->rnti, slotInfo, lcId, length, pdu);
158
159                break;
160             }
161          case MAC_LCID_RESERVED_MIN ... MAC_LCID_RESERVED_MAX :
162             break;
163
164          case MAC_LCID_CCCH_48BIT :
165             break;
166
167          case MAC_LCID_BIT_RATE_QUERY :
168             break;
169
170          case MAC_LCID_MULT_PHR_FOUR_OCT :
171             break;
172
173          case MAC_LCID_CFG_GRANT_CFM :
174             break;
175
176          case MAC_LCID_MULT_PHR_ONE_OCT:
177             break;
178
179          case MAC_LCID_SINGLE_PHR :
180             break;
181
182          case MAC_LCID_CRNTI :
183             break;
184
185          case MAC_LCID_SHORT_TRUNC_BSR :
186             break;
187
188          case MAC_LCID_LONG_TRUNC_BSR :
189             break;
190
191          case MAC_LCID_SHORT_BSR :
192             {
193                uint8_t  lcgId         = 0;
194                uint8_t  bufferSizeIdx = 0;
195                uint8_t  crnti         = 0;
196                uint32_t bufferSize    = 0;
197
198                pduLen--;
199
200                rxPduIdx++;
201                crnti = rxDataIndPdu->rnti;
202                /* 5 LSB bits in pdu represent buffer size */
203                bufferSizeIdx = (~((~0) << 5)) & rxDataPdu[rxPduIdx];
204                /* first 3 MSB bits in pdu represent LCGID */
205                lcgId = (rxDataPdu[rxPduIdx]) >> 5;
206                /* determine actual number of bytes requested */
207                bufferSize = shortBsrBytesTable[bufferSizeIdx];
208                ret = macProcShortBsr(macCb.macCell[cellIdx]->cellId, crnti, lcgId, bufferSize);
209                pduLen--;
210                rxPduIdx++;
211
212                break;
213             }
214
215          case MAC_LCID_LONG_BSR :
216             {
217                DataVolInfo dataVolInfo[MAX_NUM_LOGICAL_CHANNEL_GROUPS];
218                memset(dataVolInfo, 0,MAX_NUM_LOGICAL_CHANNEL_GROUPS * sizeof(DataVolInfo));
219                uint8_t  lcgIdx        = 0;
220                uint8_t  crnti         = 0;
221                uint8_t  numLcg        = 0;
222                uint8_t  lcgIdxPos     = 0;
223                pduLen--;
224
225                rxPduIdx++;/*To reach the Octet where lcgIdx will be present*/
226                crnti = rxDataIndPdu->rnti;
227
228                lcgIdxPos = rxPduIdx;
229
230                pduLen--;
231                rxPduIdx++;/*To reach the Octet where bsrIdx starts*/
232                for(lcgIdx = 0; lcgIdx < MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcgIdx++)
233                {
234                   if(rxDataPdu[lcgIdxPos]  & (1 << lcgIdx))
235                   {
236                      if(rxDataPdu[rxPduIdx] > 0 && rxDataPdu[rxPduIdx] < MAX_LONG_BSR_TABLE_ENTRIES)
237                      {
238                         dataVolInfo[numLcg].dataVol = longBsrBytesTable[rxDataPdu[rxPduIdx]];
239                         dataVolInfo[numLcg].lcgId = lcgIdx;
240                         numLcg++;
241                      }
242                      else
243                      {
244                         DU_LOG("\nERROR  -->  MAC: Invalid BsrIdx:%d rcvd for lcgIdx:%d",lcgIdx,rxDataPdu[rxPduIdx]);
245                      }
246                      /*next byte in PDU*/
247                      pduLen--;
248                      rxPduIdx++;
249                   }
250                }
251
252                ret = macProcLongBsr(macCb.macCell[cellIdx]->cellId, crnti, numLcg, dataVolInfo);
253
254                break;
255             }
256
257          case MAC_LCID_PADDING :
258             break;
259
260          default:
261             {
262                DU_LOG("\nERROR  -->  MAC : Invalid LC Id %d", lcId);
263                return RFAILED;
264             }
265       } /* End of switch */
266
267       if(lcId == MAC_LCID_PADDING)
268       {
269          break;
270       }
271    } /* End of While */
272
273    return ret;
274 } /* End of unpackRxData */
275
276 /**********************************************************************
277   End of file
278  **********************************************************************/