Merge "Fixes in current code for Multi-UE support [Issue-ID: ODUHIGH-354]"
[o-du/l2.git] / src / 5gnrmac / mac_mux.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
20 /* header include files -- defines (.h) */
21 #include "common_def.h"
22 #include "lrg.h"           /* Layer manager interface includes*/
23 #include "lrg.x"           /* layer management typedefs for MAC */
24 #include "du_app_mac_inf.h"
25 #include "mac_sch_interface.h"
26 #include "lwr_mac_upr_inf.h"
27 #include "mac.h"
28 #include "mac_utils.h"
29
30 /*******************************************************************
31  *
32  * @brief pack the bits
33  *
34  * @details
35  *
36  *    Function : packBytes
37  *
38  *    Functionality:
39  *     pack the bits in the corresponding byte
40  *
41  * @params[in] buffer pointer, byte and bit position, value and its size
42  * @return void
43  *
44  * ****************************************************************/
45 void packBytes(uint8_t *buf, uint16_t *bytePos, uint8_t *bitPos, uint32_t val, uint8_t valSize)
46 {
47    uint32_t  temp;
48    uint8_t   bytePart1;
49    uint32_t  bytePart2;
50    uint8_t   bytePart1Size;
51    uint32_t  bytePart2Size;
52
53    if(*bitPos - valSize + 1 >= 0)
54    {
55       bytePart1 = (uint8_t)val;
56       bytePart1 = (bytePart1 << (*bitPos -valSize +1));
57       buf[*bytePos] |= bytePart1;
58       if(*bitPos - valSize < 0)
59       {
60          *bitPos = 7;
61          (*bytePos)++;
62       }
63       else
64          *bitPos -= valSize;
65    }
66    else
67    {
68       temp = 0;
69       bytePart1Size = *bitPos +1;
70       bytePart2Size = valSize - bytePart1Size;
71
72       bytePart1 = (val >> bytePart2Size) << (*bitPos -bytePart1Size +1);
73       bytePart2 =  (~((~temp) << bytePart2Size)) & val;
74
75       buf[*bytePos] |= bytePart1;
76       (*bytePos)++;
77       *bitPos = 7;
78       packBytes(buf, bytePos, bitPos, bytePart2, bytePart2Size);
79    }  
80 }
81
82 /*******************************************************************
83  *
84  * @brief fill the RAR PDU
85  *
86  * @details
87  *
88  *    Function : fillRarPdu
89  *
90  *    Functionality:
91  *     The RAR PDU will be MUXed and formed
92  *
93  * @params[in] RAR info
94  * @return void
95  *
96  * ****************************************************************/
97 void fillRarPdu(RarInfo *rarInfo)
98 {
99    uint8_t   *rarPdu = rarInfo->rarPdu;
100    uint16_t  bytePos= 0;
101    uint8_t   bitPos = 0;
102
103    /* RAR subheader fields */
104    uint8_t   EBit = 0;
105    uint8_t   TBit = 0;
106    uint8_t   rapId = 0;
107
108    /* RAR payload fields */
109    uint8_t   RBit = 0;
110    uint16_t  timeAdv = 0;
111    uint32_t  ulGrant = 0;
112    uint16_t  tmpCrnti = 0; 
113    uint8_t   paddingLcid = 63;
114
115    /* Size(in bits) of RAR subheader files */
116    uint8_t   EBitSize = 1;
117    uint8_t   TBitSize = 1;
118    uint8_t   rapidSize = 6;
119    uint8_t   paddingLcidSize = 6;
120    uint8_t   paddingSize = 8;
121
122
123    /* Size(in bits) of RAR payload fields */
124    uint8_t   RBitSize = 1;
125    uint8_t   timeAdvSize = 12;
126    uint8_t   ulGrantSize = 27;
127    uint8_t   tmpCrntiSize = 16;
128
129    /* Fill RAR pdu fields */
130    EBit = 0;
131    TBit = 1;
132    rapId = rarInfo->RAPID;
133
134    RBit = 0;
135    timeAdv = rarInfo->ta;
136    ulGrant = 0; /* this will be done when implementing msg3 */ 
137    tmpCrnti = rarInfo->tcrnti;
138    rarInfo->rarPduLen = RAR_PAYLOAD_SIZE;
139
140    /* Initialize buffer */
141    for(bytePos = 0; bytePos < rarInfo->rarPduLen; bytePos++)
142       rarPdu[bytePos] = 0;
143
144    bytePos = 0;
145    bitPos = 7;
146
147    /* Packing fields into RAR PDU */
148    packBytes(rarPdu, &bytePos, &bitPos, EBit, EBitSize); 
149    packBytes(rarPdu, &bytePos, &bitPos, TBit, TBitSize);
150    packBytes(rarPdu, &bytePos, &bitPos, rapId, rapidSize);
151    packBytes(rarPdu, &bytePos, &bitPos, RBit, RBitSize);
152    packBytes(rarPdu, &bytePos, &bitPos, timeAdv, timeAdvSize);
153    packBytes(rarPdu, &bytePos, &bitPos, ulGrant, ulGrantSize);
154    packBytes(rarPdu, &bytePos, &bitPos, tmpCrnti, tmpCrntiSize);
155
156    /* padding of 2 bytes */
157    packBytes(rarPdu, &bytePos, &bitPos, RBit, RBitSize*2);
158    packBytes(rarPdu, &bytePos, &bitPos, paddingLcid, paddingLcidSize);
159    packBytes(rarPdu, &bytePos, &bitPos, 0, paddingSize);
160
161 }
162
163 /*******************************************************************
164  *
165  * @brief Database required to form MAC PDU
166  *
167  * @details
168  *
169  *    Function : createMacRaCb
170  *
171  *    Functionality:
172  *     stores the required params for muxing
173  *
174  * @params[in] Pointer to cellId,
175  *                        crnti
176  * @return void
177  *
178  * ****************************************************************/
179 void createMacRaCb(RachIndInfo *rachIndInfo)
180 {
181    uint8_t  ueIdx = 0;
182    uint16_t crnti = 0;
183    uint16_t cellIdx = 0;
184
185    GET_CELL_IDX(rachIndInfo->cellId, cellIdx);
186    
187    crnti = getNewCrnti(&macCb.macCell[cellIdx]->crntiMap);
188    if(crnti == -1)
189       return;
190
191    GET_UE_IDX(crnti, ueIdx);
192    ueIdx = ueIdx -1;
193
194    /* store in rach ind structure */
195    rachIndInfo->crnti  = crnti;
196
197    /* store in raCb */
198    macCb.macCell[cellIdx]->macRaCb[ueIdx].cellId = rachIndInfo->cellId;
199    macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti  = crnti;
200 }
201
202 /*************************************************
203  * @brief fill RLC DL Data
204  *
205  * @details
206  *
207  * Function : fillMsg4DlData
208  *      This function sends Dl Data
209  *      to form MAC SDUs
210  *           
211  * @param[in]  MacDlData *dlData
212  *             msg4Pdu pointer
213  ************************************************/
214
215 void fillMsg4DlData(MacDlData *dlData, uint16_t msg4PduLen, uint8_t *msg4Pdu)
216 {
217    dlData->pduInfo[dlData->numPdu].lcId = MAC_LCID_CCCH;
218    dlData->pduInfo[dlData->numPdu].pduLen = msg4PduLen;
219    memcpy(dlData->pduInfo[dlData->numPdu].dlPdu, msg4Pdu, msg4PduLen);
220    dlData->numPdu++;
221 }
222
223 /*************************************************
224  * @brief fill Mac Ce Info
225  *
226  * @details
227  *
228  * Function : fillMacCe
229  *      This function fills Mac ce identities
230  *           
231  * @param[in]  RlcMacData *dlData
232  *             Msg3Pdu Data
233  ************************************************/
234
235 void fillMacCe(MacCeInfo *macCeInfo, uint8_t *msg3Pdu)
236 {
237    uint8_t idx;
238    macCeInfo->numCes = 1;
239    for(idx = 0; idx < macCeInfo->numCes; idx++)
240    {
241       macCeInfo->macCe[idx].macCeLcid = MAC_LCID_CRI;
242       memcpy(macCeInfo->macCe[idx].macCeValue, \
243             msg3Pdu, MAX_CRI_SIZE);
244    }
245 }
246
247 /*******************************************************************
248  *
249  * @brief Forms MAC PDU
250  *
251  * @details
252  *
253  *    Function : macMuxPdu
254  *
255  *    Functionality:
256  *     The MAC PDU will be MUXed and formed
257  *
258  * @params[in] MacDlData *, MacCeInfo *, txPdu *, tbSize
259  * @return void
260  * ****************************************************************/
261
262 void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *txPdu, uint16_t tbSize)
263 {
264    uint16_t bytePos = 0;
265    uint8_t bitPos = 7;
266    uint8_t idx = 0;
267    uint8_t macPdu[tbSize];
268    memset(macPdu, 0, (tbSize * sizeof(uint8_t)));
269
270    /* subheader fields */
271    uint8_t RBit = 0;              /* Reserved bit */
272    uint8_t FBit =0;                  /* Format Indicator */
273    uint8_t lcid =0;                  /* LCID */
274    uint16_t lenField = 0;         /* Length field */
275
276    /* subheader field size (in bits) */
277    uint8_t RBitSize = 1;
278    uint8_t FBitSize = 1;
279    uint8_t lcidSize = 6;
280    uint8_t lenFieldSize = 0;      /* 8-bit or 16-bit L field  */
281
282    /* PACK ALL MAC CE */
283    if(macCeData != NULLP)
284    {
285       for(idx = 0; idx < macCeData->numCes; idx++)
286       {
287          lcid = macCeData->macCe[idx].macCeLcid;
288          switch(lcid)
289          {
290             case MAC_LCID_CRI:
291                {
292                   /* Packing fields into MAC PDU R/R/LCID */
293                   packBytes(macPdu, &bytePos, &bitPos, RBit, (RBitSize * 2));
294                   packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
295                   memcpy(&macPdu[bytePos], macCeData->macCe[idx].macCeValue,\
296                         MAX_CRI_SIZE);
297                   bytePos += MAX_CRI_SIZE;
298                   break;
299                }
300             default:
301                DU_LOG("\nERROR  -->  MAC: Invalid LCID %d in mac pdu",lcid);
302                break;
303          }
304       }
305    }
306
307    /* PACK ALL MAC SDUs */
308    for(idx = 0; idx < dlData->numPdu; idx++)
309    {
310       lcid = dlData->pduInfo[idx].lcId;
311       switch(lcid)
312       {
313          case MAC_LCID_CCCH:
314          case MAC_LCID_MIN ... MAC_LCID_MAX :
315             {
316                lenField = dlData->pduInfo[idx].pduLen;
317                if(dlData->pduInfo[idx].pduLen > 255)
318                {
319                   FBit = 1;
320                   lenFieldSize = 16;
321
322                }
323                else
324                {
325                   FBit = 0;
326                   lenFieldSize = 8;
327                }
328                /* Packing fields into MAC PDU R/F/LCID/L */
329                packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
330                packBytes(macPdu, &bytePos, &bitPos, FBit, FBitSize);
331                packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
332                packBytes(macPdu, &bytePos, &bitPos, lenField, lenFieldSize);
333                memcpy(&macPdu[bytePos], dlData->pduInfo[idx].dlPdu, lenField);
334                bytePos += lenField;
335                break;
336             }
337
338          default:
339             DU_LOG("\nERROR  -->  MAC: Invalid LCID %d in mac pdu",lcid);
340             break;
341       }
342    }
343    if(bytePos < tbSize && (tbSize-bytePos >= 1))
344    {
345       /* padding remaining bytes */
346       RBitSize = 2;
347       lcid = MAC_LCID_PADDING;
348       packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
349       packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
350    }
351
352    /*Storing the muxed pdu */
353    if(txPdu != NULLP)
354    {
355       memcpy(txPdu, macPdu, tbSize);
356    }
357 }
358
359 /**********************************************************************
360   End of file
361  **********************************************************************/