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