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, uint8_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;
100 uint16_t totalBits = 0;
101 uint8_t numBytes = 0;
105 /* RAR subheader fields */
110 /* RAR payload fields */
112 uint16_t timeAdv = 0;
113 uint32_t ulGrant = 0;
114 uint16_t tmpCrnti = 0;
115 uint8_t paddingLcid = 63;
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;
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;
131 /* Fill RAR pdu fields */
134 rapId = rarInfo->RAPID;
137 timeAdv = rarInfo->ta;
138 ulGrant = 0; /* this will be done when implementing msg3 */
139 tmpCrnti = rarInfo->tcrnti;
141 /* Calulating total number of bytes in buffer */
142 totalBits = EBitSize + TBitSize + rapidSize + RBitSize + timeAdvSize \
143 + ulGrantSize + tmpCrntiSize;
145 /* add padding size */
146 totalBits += RBitSize*2 + paddingLcidSize + paddingSize;
148 /* Calulating total number of bytes in buffer */
149 numBytes = totalBits/8;
153 rarInfo->rarPduLen = numBytes;
155 /* Initialize buffer */
156 for(bytePos = 0; bytePos < numBytes; bytePos++)
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);
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);
178 /*******************************************************************
180 * @brief Database required to form MAC PDU
184 * Function : createMacRaCb
187 * stores the required params for muxing
189 * @params[in] Pointer to cellId,
193 * ****************************************************************/
194 void createMacRaCb(RachIndInfo *rachIndInfo)
198 uint16_t cellIdx = 0;
200 GET_CELL_IDX(rachIndInfo->cellId, cellIdx);
202 crnti = getNewCrnti(&macCb.macCell[cellIdx]->crntiMap);
206 GET_UE_IDX(crnti, ueIdx);
209 /* store in rach ind structure */
210 rachIndInfo->crnti = crnti;
213 macCb.macCell[cellIdx]->macRaCb[ueIdx].cellId = rachIndInfo->cellId;
214 macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti = crnti;
217 /*************************************************
218 * @brief fill RLC DL Data
222 * Function : fillMsg4DlData
223 * This function sends Dl Data
226 * @param[in] MacDlData *dlData
228 ************************************************/
230 void fillMsg4DlData(MacDlData *dlData, uint16_t msg4PduLen, uint8_t *msg4Pdu)
235 dlData->pduInfo[idx].lcId = MAC_LCID_CCCH;
236 dlData->pduInfo[idx].pduLen = msg4PduLen;
237 memcpy(dlData->pduInfo[idx].dlPdu, msg4Pdu, msg4PduLen);
240 /*************************************************
241 * @brief fill Mac Ce Info
245 * Function : fillMacCe
246 * This function fills Mac ce identities
248 * @param[in] RlcMacData *dlData
250 ************************************************/
252 void fillMacCe(MacCeInfo *macCeInfo, uint8_t *msg3Pdu)
255 macCeInfo->numCes = 1;
256 for(idx = 0; idx < macCeInfo->numCes; idx++)
258 macCeInfo->macCe[idx].macCeLcid = MAC_LCID_CRI;
259 memcpy(macCeInfo->macCe[idx].macCeValue, \
260 msg3Pdu, MAX_CRI_SIZE);
264 /*******************************************************************
266 * @brief Forms MAC PDU
270 * Function : macMuxPdu
273 * The MAC PDU will be MUXed and formed
275 * @params[in] MacDlData *, MacCeInfo *, txPdu *, tbSize
277 * ****************************************************************/
279 void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *txPdu, uint16_t tbSize)
284 uint8_t macPdu[tbSize];
285 memset(macPdu, 0, (tbSize * sizeof(uint8_t)));
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 */
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 */
299 /* PACK ALL MAC CE */
300 if(macCeData != NULLP)
302 for(idx = 0; idx < macCeData->numCes; idx++)
304 lcid = macCeData->macCe[idx].macCeLcid;
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,\
314 bytePos += MAX_CRI_SIZE;
318 DU_LOG("\nERROR --> MAC: Invalid LCID %d in mac pdu",lcid);
324 /* PACK ALL MAC SDUs */
325 for(idx = 0; idx < dlData->numPdu; idx++)
327 lcid = dlData->pduInfo[idx].lcId;
331 case MAC_LCID_MIN ... MAC_LCID_MAX :
333 lenField = dlData->pduInfo[idx].pduLen;
334 if(dlData->pduInfo[idx].pduLen > 255)
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);
356 DU_LOG("\nERROR --> MAC: Invalid LCID %d in mac pdu",lcid);
361 if(bytePos < tbSize && (tbSize-bytePos >= 1))
363 /* padding remaining bytes */
365 lcid = MAC_LCID_PADDING;
366 packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
367 packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
370 /*Storing the muxed pdu */
373 memcpy(txPdu, macPdu, tbSize);
377 /**********************************************************************
379 **********************************************************************/