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