valgrind memory leak fixes
[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, uint8_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  totalBits = 0;
101    uint8_t   numBytes = 0;
102    uint8_t   bytePos= 0;
103    uint8_t   bitPos = 0;
104
105    /* RAR subheader fields */
106    uint8_t   EBit = 0;
107    uint8_t   TBit = 0;
108    uint8_t   rapId = 0;
109
110    /* RAR payload fields */
111    uint8_t   RBit = 0;
112    uint16_t  timeAdv = 0;
113    uint32_t  ulGrant = 0;
114    uint16_t  tmpCrnti = 0; 
115    uint8_t   paddingLcid = 63;
116
117    /* Size(in bits) of RAR subheader files */
118    uint8_t   EBitSize = 1;
119    uint8_t   TBitSize = 1;
120    uint8_t   rapidSize = 6;
121    uint8_t   paddingLcidSize = 6;
122    uint8_t   paddingSize = 8;
123
124
125    /* Size(in bits) of RAR payload fields */
126    uint8_t   RBitSize = 1;
127    uint8_t   timeAdvSize = 12;
128    uint8_t   ulGrantSize = 27;
129    uint8_t   tmpCrntiSize = 16;
130
131    /* Fill RAR pdu fields */
132    EBit = 0;
133    TBit = 1;
134    rapId = rarInfo->RAPID;
135
136    RBit = 0;
137    timeAdv = rarInfo->ta;
138    ulGrant = 0; /* this will be done when implementing msg3 */ 
139    tmpCrnti = rarInfo->tcrnti;
140
141    /* Calulating total number of bytes in buffer */
142    totalBits = EBitSize + TBitSize + rapidSize + RBitSize + timeAdvSize \
143                + ulGrantSize + tmpCrntiSize;
144
145    /* add padding size */
146    totalBits += RBitSize*2 + paddingLcidSize + paddingSize;
147
148    /* Calulating total number of bytes in buffer */
149    numBytes = totalBits/8;
150    if(totalBits % 8)
151       numBytes += 1;
152
153    rarInfo->rarPduLen = numBytes;
154
155    /* Initialize buffer */
156    for(bytePos = 0; bytePos < numBytes; bytePos++)
157       rarPdu[bytePos] = 0;
158
159    bytePos = 0;
160    bitPos = 7;
161
162    /* Packing fields into RAR PDU */
163    packBytes(rarPdu, &bytePos, &bitPos, EBit, EBitSize); 
164    packBytes(rarPdu, &bytePos, &bitPos, TBit, TBitSize);
165    packBytes(rarPdu, &bytePos, &bitPos, rapId, rapidSize);
166    packBytes(rarPdu, &bytePos, &bitPos, RBit, RBitSize);
167    packBytes(rarPdu, &bytePos, &bitPos, timeAdv, timeAdvSize);
168    packBytes(rarPdu, &bytePos, &bitPos, ulGrant, ulGrantSize);
169    packBytes(rarPdu, &bytePos, &bitPos, tmpCrnti, tmpCrntiSize);
170
171    /* padding of 2 bytes */
172    packBytes(rarPdu, &bytePos, &bitPos, RBit, RBitSize*2);
173    packBytes(rarPdu, &bytePos, &bitPos, paddingLcid, paddingLcidSize);
174    packBytes(rarPdu, &bytePos, &bitPos, 0, paddingSize);
175
176 }
177
178 /*******************************************************************
179  *
180  * @brief Database required to form MAC PDU
181  *
182  * @details
183  *
184  *    Function : createMacRaCb
185  *
186  *    Functionality:
187  *     stores the required params for muxing
188  *
189  * @params[in] Pointer to cellId,
190  *                        crnti
191  * @return void
192  *
193  * ****************************************************************/
194 void createMacRaCb(RachIndInfo *rachIndInfo)
195 {
196    uint8_t  ueIdx = 0;
197    uint16_t crnti = 0;
198    uint16_t cellIdx = 0;
199
200    GET_CELL_IDX(rachIndInfo->cellId, cellIdx);
201    
202    crnti = getNewCrnti(&macCb.macCell[cellIdx]->crntiMap);
203    if(crnti == -1)
204       return;
205
206    GET_UE_IDX(crnti, ueIdx);
207    ueIdx = ueIdx -1;
208
209    /* store in rach ind structure */
210    rachIndInfo->crnti  = crnti;
211
212    /* store in raCb */
213    macCb.macCell[cellIdx]->macRaCb[ueIdx].cellId = rachIndInfo->cellId;
214    macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti  = crnti;
215 }
216
217 /*************************************************
218  * @brief fill RLC DL Data
219  *
220  * @details
221  *
222  * Function : fillMsg4DlData
223  *      This function sends Dl Data
224  *      to form MAC SDUs
225  *           
226  * @param[in]  MacDlData *dlData
227  *             msg4Pdu pointer
228  ************************************************/
229
230 void fillMsg4DlData(MacDlData *dlData, uint16_t msg4PduLen, uint8_t *msg4Pdu)
231 {
232    uint8_t idx = 0;
233
234    dlData->numPdu = 1;
235    dlData->pduInfo[idx].lcId = MAC_LCID_CCCH;
236    dlData->pduInfo[idx].pduLen = msg4PduLen;
237    memcpy(dlData->pduInfo[idx].dlPdu, msg4Pdu, msg4PduLen);
238 }
239
240 /*************************************************
241  * @brief fill Mac Ce Info
242  *
243  * @details
244  *
245  * Function : fillMacCe
246  *      This function fills Mac ce identities
247  *           
248  * @param[in]  RlcMacData *dlData
249  *             Msg3Pdu Data
250  ************************************************/
251
252 void fillMacCe(MacCeInfo *macCeInfo, uint8_t *msg3Pdu)
253 {
254    uint8_t idx;
255    macCeInfo->numCes = 1;
256    for(idx = 0; idx < macCeInfo->numCes; idx++)
257    {
258       macCeInfo->macCe[idx].macCeLcid = MAC_LCID_CRI;
259       memcpy(macCeInfo->macCe[idx].macCeValue, \
260             msg3Pdu, MAX_CRI_SIZE);
261    }
262 }
263
264 /*******************************************************************
265  *
266  * @brief Forms MAC PDU
267  *
268  * @details
269  *
270  *    Function : macMuxPdu
271  *
272  *    Functionality:
273  *     The MAC PDU will be MUXed and formed
274  *
275  * @params[in] MacDlData *, MacCeInfo *, txPdu *, tbSize
276  * @return void
277  * ****************************************************************/
278
279 void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *txPdu, uint16_t tbSize)
280 {
281    uint8_t bytePos = 0;
282    uint8_t bitPos = 7;
283    uint8_t idx = 0;
284    uint8_t macPdu[tbSize];
285    memset(macPdu, 0, (tbSize * sizeof(uint8_t)));
286
287    /* subheader fields */
288    uint8_t RBit = 0;              /* Reserved bit */
289    uint8_t FBit =0;                  /* Format Indicator */
290    uint8_t lcid =0;                  /* LCID */
291    uint16_t lenField = 0;         /* Length field */
292
293    /* subheader field size (in bits) */
294    uint8_t RBitSize = 1;
295    uint8_t FBitSize = 1;
296    uint8_t lcidSize = 6;
297    uint8_t lenFieldSize = 0;      /* 8-bit or 16-bit L field  */
298
299    /* PACK ALL MAC CE */
300    if(macCeData != NULLP)
301    {
302       for(idx = 0; idx < macCeData->numCes; idx++)
303       {
304          lcid = macCeData->macCe[idx].macCeLcid;
305          switch(lcid)
306          {
307             case MAC_LCID_CRI:
308                {
309                   /* Packing fields into MAC PDU R/R/LCID */
310                   packBytes(macPdu, &bytePos, &bitPos, RBit, (RBitSize * 2));
311                   packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
312                   memcpy(&macPdu[bytePos], macCeData->macCe[idx].macCeValue,\
313                         MAX_CRI_SIZE);
314                   bytePos += MAX_CRI_SIZE;
315                   break;
316                }
317             default:
318                DU_LOG("\n MAC: Invalid LCID %d in mac pdu",lcid);
319                break;
320          }
321       }
322    }
323
324    /* PACK ALL MAC SDUs */
325    for(idx = 0; idx < dlData->numPdu; idx++)
326    {
327       lcid = dlData->pduInfo[idx].lcId;
328       switch(lcid)
329       {
330          case MAC_LCID_CCCH:
331          case MAC_LCID_MIN ... MAC_LCID_MAX :
332             {
333                lenField = dlData->pduInfo[idx].pduLen;
334                if(dlData->pduInfo[idx].pduLen > 255)
335                {
336                   FBit = 1;
337                   lenFieldSize = 16;
338
339                }
340                else
341                {
342                   FBit = 0;
343                   lenFieldSize = 8;
344                }
345                /* Packing fields into MAC PDU R/F/LCID/L */
346                packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
347                packBytes(macPdu, &bytePos, &bitPos, FBit, FBitSize);
348                packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
349                packBytes(macPdu, &bytePos, &bitPos, lenField, lenFieldSize);
350                memcpy(&macPdu[bytePos], dlData->pduInfo[idx].dlPdu, lenField);
351                bytePos += lenField;
352                break;
353             }
354
355          default:
356             DU_LOG("\n MAC: Invalid LCID %d in mac pdu",lcid);
357             break;
358       }
359
360    }
361    if(bytePos < tbSize && (tbSize-bytePos >= 1))
362    {
363       /* padding remaining bytes */
364       RBitSize = 2;
365       lcid = MAC_LCID_PADDING;
366       packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
367       packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
368    }
369
370    /*Storing the muxed pdu */
371    if(txPdu != NULLP)
372    {
373       memcpy(txPdu, macPdu, tbSize);
374    }
375 }
376
377 /**********************************************************************
378   End of file
379  **********************************************************************/