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