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 "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"
28 #include "mac_utils.h"
30 /*******************************************************************
32 * @brief pack the bits
36 * Function : packBytes
39 * pack the bits in the corresponding byte
41 * @params[in] buffer pointer, byte and bit position, value and its size
44 * ****************************************************************/
45 void packBytes(uint8_t *buf, uint16_t *bytePos, uint8_t *bitPos, uint32_t val, uint8_t valSize)
50 uint8_t bytePart1Size;
51 uint32_t bytePart2Size;
53 if(*bitPos - valSize + 1 >= 0)
55 bytePart1 = (uint8_t)val;
56 bytePart1 = (bytePart1 << (*bitPos -valSize +1));
57 buf[*bytePos] |= bytePart1;
58 if(*bitPos - valSize < 0)
69 bytePart1Size = *bitPos +1;
70 bytePart2Size = valSize - bytePart1Size;
72 bytePart1 = (val >> bytePart2Size) << (*bitPos -bytePart1Size +1);
73 bytePart2 = (~((~temp) << bytePart2Size)) & val;
75 buf[*bytePos] |= bytePart1;
78 packBytes(buf, bytePos, bitPos, bytePart2, bytePart2Size);
82 /*******************************************************************
84 * @brief fill the RAR PDU
88 * Function : fillRarPdu
91 * The RAR PDU will be MUXed and formed
93 * @params[in] RAR info
96 * ****************************************************************/
97 void fillRarPdu(RarInfo *rarInfo)
99 uint8_t *rarPdu = rarInfo->rarPdu;
103 /* RAR subheader fields */
108 /* RAR payload fields */
110 uint16_t timeAdv = 0;
111 uint32_t ulGrant = 0;
112 uint16_t tmpCrnti = 0;
113 uint8_t paddingLcid = 63;
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;
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;
129 /* Fill RAR pdu fields */
132 rapId = rarInfo->RAPID;
135 timeAdv = rarInfo->ta;
136 ulGrant = 0; /* this will be done when implementing msg3 */
137 tmpCrnti = rarInfo->tcrnti;
138 rarInfo->rarPduLen = RAR_PAYLOAD_SIZE;
140 /* Initialize buffer */
141 for(bytePos = 0; bytePos < rarInfo->rarPduLen; bytePos++)
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);
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);
163 /*******************************************************************
165 * @brief Database required to form MAC PDU
169 * Function : createMacRaCb
172 * stores the required params for muxing
174 * @params[in] Pointer to cellId,
178 * ****************************************************************/
179 void createMacRaCb(RachIndInfo *rachIndInfo)
183 uint16_t cellIdx = 0;
185 GET_CELL_IDX(rachIndInfo->cellId, cellIdx);
187 crnti = getNewCrnti(&macCb.macCell[cellIdx]->crntiMap);
191 GET_UE_IDX(crnti, ueIdx);
194 /* store in rach ind structure */
195 rachIndInfo->crnti = crnti;
198 macCb.macCell[cellIdx]->macRaCb[ueIdx].cellId = rachIndInfo->cellId;
199 macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti = crnti;
202 /*************************************************
203 * @brief fill RLC DL Data
207 * Function : fillMsg4DlData
208 * This function sends Dl Data
211 * @param[in] MacDlData *dlData
213 ************************************************/
215 void fillMsg4DlData(MacDlData *dlData, uint16_t msg4PduLen, uint8_t *msg4Pdu)
220 dlData->pduInfo[idx].lcId = MAC_LCID_CCCH;
221 dlData->pduInfo[idx].pduLen = msg4PduLen;
222 memcpy(dlData->pduInfo[idx].dlPdu, msg4Pdu, msg4PduLen);
225 /*************************************************
226 * @brief fill Mac Ce Info
230 * Function : fillMacCe
231 * This function fills Mac ce identities
233 * @param[in] RlcMacData *dlData
235 ************************************************/
237 void fillMacCe(MacCeInfo *macCeInfo, uint8_t *msg3Pdu)
240 macCeInfo->numCes = 1;
241 for(idx = 0; idx < macCeInfo->numCes; idx++)
243 macCeInfo->macCe[idx].macCeLcid = MAC_LCID_CRI;
244 memcpy(macCeInfo->macCe[idx].macCeValue, \
245 msg3Pdu, MAX_CRI_SIZE);
249 /*******************************************************************
251 * @brief Forms MAC PDU
255 * Function : macMuxPdu
258 * The MAC PDU will be MUXed and formed
260 * @params[in] MacDlData *, MacCeInfo *, txPdu *, tbSize
262 * ****************************************************************/
264 void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *txPdu, uint16_t tbSize)
266 uint16_t bytePos = 0;
269 uint8_t macPdu[tbSize];
270 memset(macPdu, 0, (tbSize * sizeof(uint8_t)));
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 */
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 */
284 /* PACK ALL MAC CE */
285 if(macCeData != NULLP)
287 for(idx = 0; idx < macCeData->numCes; idx++)
289 lcid = macCeData->macCe[idx].macCeLcid;
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,\
299 bytePos += MAX_CRI_SIZE;
303 DU_LOG("\nERROR --> MAC: Invalid LCID %d in mac pdu",lcid);
309 /* PACK ALL MAC SDUs */
310 for(idx = 0; idx < dlData->numPdu; idx++)
312 lcid = dlData->pduInfo[idx].lcId;
316 case MAC_LCID_MIN ... MAC_LCID_MAX :
318 lenField = dlData->pduInfo[idx].pduLen;
319 if(dlData->pduInfo[idx].pduLen > 255)
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);
341 DU_LOG("\nERROR --> MAC: Invalid LCID %d in mac pdu",lcid);
345 if(bytePos < tbSize && (tbSize-bytePos >= 1))
347 /* padding remaining bytes */
349 lcid = MAC_LCID_PADDING;
350 packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
351 packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
354 /*Storing the muxed pdu */
357 memcpy(txPdu, macPdu, tbSize);
361 /**********************************************************************
363 **********************************************************************/