1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
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*/
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"
42 #include "rg.x" /* typedefs for MAC */
44 /*******************************************************************
46 * @brief pack the bits
50 * Function : packBytes
53 * pack the bits in the corresponding byte
55 * @params[in] buffer pointer, byte and bit position, value and its size
58 * ****************************************************************/
59 void packBytes(uint8_t *buf, uint8_t *bytePos, uint8_t *bitPos, uint32_t val, uint8_t valSize)
64 uint8_t bytePart1Size;
65 uint32_t bytePart2Size;
67 if(*bitPos - valSize + 1 >= 0)
69 bytePart1 = (uint8_t)val;
70 bytePart1 = (bytePart1 << (*bitPos -valSize +1));
71 buf[*bytePos] |= bytePart1;
72 if(*bitPos - valSize < 0)
83 bytePart1Size = *bitPos +1;
84 bytePart2Size = valSize - bytePart1Size;
86 bytePart1 = (val >> bytePart2Size) << (*bitPos -bytePart1Size +1);
87 bytePart2 = (~((~temp) << bytePart2Size)) & val;
89 buf[*bytePos] |= bytePart1;
92 packBytes(buf, bytePos, bitPos, bytePart2, bytePart2Size);
96 /*******************************************************************
98 * @brief fill the RAR PDU
102 * Function : fillRarPdu
105 * The RAR PDU will be MUXed and formed
107 * @params[in] RAR info
110 * ****************************************************************/
111 void fillRarPdu(RarInfo *rarInfo)
113 uint8_t *rarPdu = rarInfo->rarPdu;
114 uint16_t totalBits = 0;
115 uint8_t numBytes = 0;
119 /* RAR subheader fields */
124 /* RAR payload fields */
126 uint16_t timeAdv = 0;
127 uint32_t ulGrant = 0;
128 uint16_t tmpCrnti = 0;
129 uint8_t paddingLcid = 63;
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;
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;
145 /* Fill RAR pdu fields */
148 rapId = rarInfo->RAPID;
151 timeAdv = rarInfo->ta;
152 ulGrant = 0; /* this will be done when implementing msg3 */
153 tmpCrnti = rarInfo->tcrnti;
155 /* Calulating total number of bytes in buffer */
156 totalBits = EBitSize + TBitSize + rapidSize + RBitSize + timeAdvSize \
157 + ulGrantSize + tmpCrntiSize;
159 /* add padding size */
160 totalBits += RBitSize*2 + paddingLcidSize + paddingSize;
162 /* Calulating total number of bytes in buffer */
163 numBytes = totalBits/8;
167 rarInfo->rarPduLen = numBytes;
169 /* Initialize buffer */
170 for(bytePos = 0; bytePos < numBytes; bytePos++)
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);
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);
192 /*******************************************************************
194 * @brief Database required to form MAC PDU
198 * Function : createMacRaCb
201 * stores the required params for muxing
203 * @params[in] Pointer to cellId,
207 * ****************************************************************/
208 void createMacRaCb(uint16_t cellId, uint16_t crnti)
210 uint8_t idx = 0; /* supporting 1 UE */
211 macCb.macCell->macRaCb[idx].cellId = cellId;
212 macCb.macCell->macRaCb[idx].crnti = crnti;
215 /*************************************************
216 * @brief fill RLC DL Data
220 * Function : fillMsg4DlData
221 * This function sends Dl Data
224 * @param[in] MacDlData *dlData
226 ************************************************/
228 void fillMsg4DlData(MacDlData *dlData, uint8_t *msg4Pdu)
233 dlData->pduInfo[idx].lcId = MAC_LCID_CCCH;
234 dlData->pduInfo[idx].pduLen = macCb.macCell->macRaCb[idx].msg4PduLen;
235 for(idx2 = 0; idx2 < dlData->pduInfo[idx].pduLen; idx2++)
237 dlData->pduInfo[idx].dlPdu[idx2] = msg4Pdu[idx2];
241 /*************************************************
242 * @brief fill Mac Ce Info
246 * Function : fillMacCe
247 * This function fills Mac ce identities
249 * @param[in] RlcMacData *dlData
251 ************************************************/
253 void fillMacCe(MacCeInfo *macCeInfo, uint8_t *msg3Pdu)
256 macCeInfo->numCes = 1;
257 for(idx = 0; idx < macCeInfo->numCes; idx++)
259 macCeInfo->macCe[idx].macCeLcid = MAC_LCID_CRI;
260 memcpy(macCeInfo->macCe[idx].macCeValue, \
261 msg3Pdu, MAX_CRI_SIZE);
265 /*******************************************************************
267 * @brief Forms MAC PDU
271 * Function : macMuxPdu
274 * The MAC PDU will be MUXed and formed
276 * @params[in] MacDlData *, MacCeInfo *, msg4TxPdu *, tbSize
278 * ****************************************************************/
280 void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *msg4TxPdu, uint16_t tbSize)
285 uint8_t macPdu[tbSize];
286 memset(macPdu, 0, (tbSize * sizeof(uint8_t)));
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 */
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 */
300 /* PACK ALL MAC CE */
301 for(idx = 0; idx < macCeData->numCes; idx++)
303 lcid = macCeData->macCe[idx].macCeLcid;
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,\
313 bytePos += MAX_CRI_SIZE;
317 DU_LOG("\n MAC: Invalid LCID %d in mac pdu",lcid);
322 /* PACK ALL MAC SDUs */
323 for(idx = 0; idx < dlData->numPdu; idx++)
325 lcid = dlData->pduInfo[idx].lcId;
330 lenField = dlData->pduInfo[idx].pduLen;
331 if(dlData->pduInfo[idx].pduLen > 255)
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);
353 DU_LOG("\n MAC: Invalid LCID %d in mac pdu",lcid);
358 if(bytePos < tbSize && (tbSize-bytePos >= 1))
360 /* padding remaining bytes */
362 lcid = MAC_LCID_PADDING;
363 packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
364 packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
367 /*Storing the muxed pdu in macRaCb */
368 if(msg4TxPdu != NULLP)
370 memcpy(msg4TxPdu, macPdu, tbSize);
374 /**********************************************************************
376 **********************************************************************/