Merge "Added a new Flag ODU_LWR_MAC_DEBUG in the code"
[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 extern uint32_t shortBsrBytesTable[MAX_SHORT_BSR_TABLE_ENTRIES];
30
31 /*******************************************************************
32  *
33  * @brief De-mux of MAC-Sub PDUs from Rx Data Ind Pdu
34  *
35  * @detail
36  *
37  *    Function : unpackRxData
38  *
39  *    Functionality:
40  *     De-mux of MAC-Sub PDUs from Rx Data Ind Pdu
41  *
42  * @params[in] Pointer to PDU received
43  *             PDU length
44  * @return ROK
45  *         RFAILED
46  *
47  * ****************************************************************/
48 uint8_t unpackRxData(uint16_t cellId, SlotIndInfo slotInfo, RxDataIndPdu *rxDataIndPdu)
49 {
50    uint8_t   ueIdx;       /* Iterator for UE list */
51    uint8_t   lcId;        /* LC ID of a sub pdu */
52    uint8_t   fBit = 0;    /* Value of F Bit in MAC sub-header */
53    uint8_t   idx = 0;     /* Iterator for received PDU */
54    uint16_t  length;      /* Length of payload in a sub-PDU */ 
55    uint8_t   *pdu;        /* Payload in sub-PDU */
56    uint16_t  pduLen;      /* Length of undecoded PDU */
57    uint8_t   *rxDataPdu;  /* Received PDU in Rx Data Ind */
58    uint16_t  cellIdx;     /* Cell Index */
59    uint8_t   ret =ROK;
60
61    GET_CELL_IDX(cellId, cellIdx);
62    pduLen = rxDataIndPdu->pduLength;
63    rxDataPdu = rxDataIndPdu->pduData;
64    GET_UE_IDX(rxDataIndPdu->rnti, ueIdx);
65    ueIdx = ueIdx -1;
66
67    while(pduLen > 0)
68    {
69       /* MSB in 1st octet is Reserved bit. Hence not decoding it. 
70          2nd MSB in 1st octet is R/F bit depending upon type of payload */
71       fBit = (1 << 7) & rxDataPdu[idx];
72
73       /* LC id is the 6 LSB in 1st octet */
74       lcId = (~((~0) << 6)) & rxDataPdu[idx];
75
76       switch(lcId)
77       {
78          case MAC_LCID_CCCH :
79             {
80                pduLen--;
81
82                /* for UL CCCH,fixed length of MAC SDU */
83                length = 6;
84
85                /*  Allocating sharable memory to send ul ccch msg to du app*/
86                MAC_ALLOC_SHRABL_BUF(pdu, length);
87                if(!pdu)
88                {
89                   DU_LOG("\nMAC : UL CCCH PDU memory allocation failed");
90                   return RFAILED;
91                }  
92                idx++;
93                memcpy(pdu, &rxDataPdu[idx], length);
94                pduLen -= length;
95                idx = idx + length;
96
97                /* store msg3 pdu in macRaCb for CRI value */
98                memcpy(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg3Pdu, pdu, length);
99
100                /* Send UL-CCCH Indication to DU APP */
101                ret = macProcUlCcchInd(macCb.macCell[cellIdx]->cellId, rxDataIndPdu->rnti, length, pdu);
102                break;
103             }
104
105          case MAC_LCID_MIN ... MAC_LCID_MAX :
106             {
107                DU_LOG("\nMAC : PDU received for LC ID %d", lcId);
108                pduLen--;
109                idx++;
110
111                length = rxDataPdu[idx];
112                if(fBit)
113                {
114                   pduLen--;
115                   idx++;
116                   length = (length << 8) & rxDataPdu[idx];
117                }
118
119                /*  Copying the payload to send to RLC */
120                MAC_ALLOC_SHRABL_BUF(pdu, length);
121                if(!pdu)
122                {
123                   DU_LOG("\nMAC : Memory allocation failed while demuxing Rx Data PDU");
124                   return RFAILED;
125                }
126                pduLen--;
127                idx++;
128                memcpy(pdu, &rxDataPdu[idx], length);
129                pduLen -= length;
130                idx = idx + length;
131
132                /* Delete RA cb once RRC setup complete received */
133                if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == rxDataIndPdu->rnti)
134                {
135                   MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
136                      macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
137                   MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, \
138                       macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TbSize);
139                   memset(&macCb.macCell[cellIdx]->macRaCb[ueIdx], 0, sizeof(MacRaCbInfo));
140                }
141                
142                /* Send UL Data to RLC */
143                ret = macProcUlData(cellId, rxDataIndPdu->rnti, slotInfo, lcId, length, pdu);
144
145                break;
146             }
147          case MAC_LCID_RESERVED_MIN ... MAC_LCID_RESERVED_MAX :
148             break;
149
150          case MAC_LCID_CCCH_48BIT :
151             break;
152
153          case MAC_LCID_BIT_RATE_QUERY :
154             break;
155
156          case MAC_LCID_MULT_PHR_FOUR_OCT :
157             break;
158
159          case MAC_LCID_CFG_GRANT_CFM :
160             break;
161
162          case MAC_LCID_MULT_PHR_ONE_OCT:
163             break;
164
165          case MAC_LCID_SINGLE_PHR :
166             break;
167
168          case MAC_LCID_CRNTI :
169             break;
170
171          case MAC_LCID_SHORT_TRUNC_BSR :
172             break;
173
174          case MAC_LCID_LONG_TRUNC_BSR :
175             break;
176
177          case MAC_LCID_SHORT_BSR :
178             {
179                uint8_t  lcgId         = 0;
180                uint8_t  bufferSizeIdx = 0;
181                uint8_t  crnti         = 0;
182                uint32_t bufferSize    = 0;
183
184                pduLen--;
185
186                idx++;
187                crnti = rxDataIndPdu->rnti;
188                /* 5 LSB bits in pdu represent buffer size */
189                bufferSizeIdx = (~((~0) << 5)) & rxDataPdu[idx];
190                /* first 3 MSB bits in pdu represent LCGID */
191                lcgId = (rxDataPdu[idx]) >> 5;
192                /* determine actual number of bytes requested */
193                bufferSize = shortBsrBytesTable[bufferSizeIdx];
194                ret = macProcShortBsr(macCb.macCell[cellIdx]->cellId, crnti, lcgId, bufferSize);
195                pduLen--;
196                idx++;
197         
198                break;
199             }
200
201          case MAC_LCID_LONG_BSR :
202             break;
203
204          case MAC_LCID_PADDING :
205             break;
206          
207          default:
208             {
209                DU_LOG("\nMAC : Invalid LC Id %d", lcId);
210                return RFAILED;
211             }
212       } /* End of switch */
213
214       if(lcId == MAC_LCID_PADDING)
215       {
216          break;
217       }
218    } /* End of While */
219
220    return ret;
221 } /* End of unpackRxData */
222
223 /**********************************************************************
224   End of file
225  **********************************************************************/