Merge "Using TAPA Task to generated PHY Stub's slot indication [Issue-Id: ODUHIGH...
[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    uint8_t idx = 0;
218
219    dlData->numPdu = 1;
220    dlData->pduInfo[idx].lcId = MAC_LCID_CCCH;
221    dlData->pduInfo[idx].pduLen = msg4PduLen;
222    memcpy(dlData->pduInfo[idx].dlPdu, msg4Pdu, msg4PduLen);
223 }
224
225 /*************************************************
226  * @brief fill Mac Ce Info
227  *
228  * @details
229  *
230  * Function : fillMacCe
231  *      This function fills Mac ce identities
232  *           
233  * @param[in]  RlcMacData *dlData
234  *             Msg3Pdu Data
235  ************************************************/
236
237 void fillMacCe(MacCeInfo *macCeInfo, uint8_t *msg3Pdu)
238 {
239    uint8_t idx;
240    macCeInfo->numCes = 1;
241    for(idx = 0; idx < macCeInfo->numCes; idx++)
242    {
243       macCeInfo->macCe[idx].macCeLcid = MAC_LCID_CRI;
244       memcpy(macCeInfo->macCe[idx].macCeValue, \
245             msg3Pdu, MAX_CRI_SIZE);
246    }
247 }
248
249 /*******************************************************************
250  *
251  * @brief Forms MAC PDU
252  *
253  * @details
254  *
255  *    Function : macMuxPdu
256  *
257  *    Functionality:
258  *     The MAC PDU will be MUXed and formed
259  *
260  * @params[in] MacDlData *, MacCeInfo *, txPdu *, tbSize
261  * @return void
262  * ****************************************************************/
263
264 void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *txPdu, uint16_t tbSize)
265 {
266    uint16_t bytePos = 0;
267    uint8_t bitPos = 7;
268    uint8_t idx = 0;
269    uint8_t macPdu[tbSize];
270    memset(macPdu, 0, (tbSize * sizeof(uint8_t)));
271
272    /* subheader fields */
273    uint8_t RBit = 0;              /* Reserved bit */
274    uint8_t FBit =0;                  /* Format Indicator */
275    uint8_t lcid =0;                  /* LCID */
276    uint16_t lenField = 0;         /* Length field */
277
278    /* subheader field size (in bits) */
279    uint8_t RBitSize = 1;
280    uint8_t FBitSize = 1;
281    uint8_t lcidSize = 6;
282    uint8_t lenFieldSize = 0;      /* 8-bit or 16-bit L field  */
283
284    /* PACK ALL MAC CE */
285    if(macCeData != NULLP)
286    {
287       for(idx = 0; idx < macCeData->numCes; idx++)
288       {
289          lcid = macCeData->macCe[idx].macCeLcid;
290          switch(lcid)
291          {
292             case MAC_LCID_CRI:
293                {
294                   /* Packing fields into MAC PDU R/R/LCID */
295                   packBytes(macPdu, &bytePos, &bitPos, RBit, (RBitSize * 2));
296                   packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
297                   memcpy(&macPdu[bytePos], macCeData->macCe[idx].macCeValue,\
298                         MAX_CRI_SIZE);
299                   bytePos += MAX_CRI_SIZE;
300                   break;
301                }
302             default:
303                DU_LOG("\nERROR  -->  MAC: Invalid LCID %d in mac pdu",lcid);
304                break;
305          }
306       }
307    }
308
309    /* PACK ALL MAC SDUs */
310    for(idx = 0; idx < dlData->numPdu; idx++)
311    {
312       lcid = dlData->pduInfo[idx].lcId;
313       switch(lcid)
314       {
315          case MAC_LCID_CCCH:
316          case MAC_LCID_MIN ... MAC_LCID_MAX :
317             {
318                lenField = dlData->pduInfo[idx].pduLen;
319                if(dlData->pduInfo[idx].pduLen > 255)
320                {
321                   FBit = 1;
322                   lenFieldSize = 16;
323
324                }
325                else
326                {
327                   FBit = 0;
328                   lenFieldSize = 8;
329                }
330                /* Packing fields into MAC PDU R/F/LCID/L */
331                packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
332                packBytes(macPdu, &bytePos, &bitPos, FBit, FBitSize);
333                packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
334                packBytes(macPdu, &bytePos, &bitPos, lenField, lenFieldSize);
335                memcpy(&macPdu[bytePos], dlData->pduInfo[idx].dlPdu, lenField);
336                bytePos += lenField;
337                break;
338             }
339
340          default:
341             DU_LOG("\nERROR  -->  MAC: Invalid LCID %d in mac pdu",lcid);
342             break;
343       }
344    }
345    if(bytePos < tbSize && (tbSize-bytePos >= 1))
346    {
347       /* padding remaining bytes */
348       RBitSize = 2;
349       lcid = MAC_LCID_PADDING;
350       packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
351       packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
352    }
353
354    /*Storing the muxed pdu */
355    if(txPdu != NULLP)
356    {
357       memcpy(txPdu, macPdu, tbSize);
358    }
359 }
360
361 /**********************************************************************
362   End of file
363  **********************************************************************/