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"
29 /*******************************************************************
31 * @brief pack the bits
35 * Function : packBytes
38 * pack the bits in the corresponding byte
40 * @params[in] buffer pointer, byte and bit position, value and its size
43 * ****************************************************************/
44 void packBytes(uint8_t *buf, uint8_t *bytePos, uint8_t *bitPos, uint32_t val, uint8_t valSize)
49 uint8_t bytePart1Size;
50 uint32_t bytePart2Size;
52 if(*bitPos - valSize + 1 >= 0)
54 bytePart1 = (uint8_t)val;
55 bytePart1 = (bytePart1 << (*bitPos -valSize +1));
56 buf[*bytePos] |= bytePart1;
57 if(*bitPos - valSize < 0)
68 bytePart1Size = *bitPos +1;
69 bytePart2Size = valSize - bytePart1Size;
71 bytePart1 = (val >> bytePart2Size) << (*bitPos -bytePart1Size +1);
72 bytePart2 = (~((~temp) << bytePart2Size)) & val;
74 buf[*bytePos] |= bytePart1;
77 packBytes(buf, bytePos, bitPos, bytePart2, bytePart2Size);
81 /*******************************************************************
83 * @brief fill the RAR PDU
87 * Function : fillRarPdu
90 * The RAR PDU will be MUXed and formed
92 * @params[in] RAR info
95 * ****************************************************************/
96 void fillRarPdu(RarInfo *rarInfo)
98 uint8_t *rarPdu = rarInfo->rarPdu;
99 uint16_t totalBits = 0;
100 uint8_t numBytes = 0;
104 /* RAR subheader fields */
109 /* RAR payload fields */
111 uint16_t timeAdv = 0;
112 uint32_t ulGrant = 0;
113 uint16_t tmpCrnti = 0;
114 uint8_t paddingLcid = 63;
116 /* Size(in bits) of RAR subheader files */
117 uint8_t EBitSize = 1;
118 uint8_t TBitSize = 1;
119 uint8_t rapidSize = 6;
120 uint8_t paddingLcidSize = 6;
121 uint8_t paddingSize = 8;
124 /* Size(in bits) of RAR payload fields */
125 uint8_t RBitSize = 1;
126 uint8_t timeAdvSize = 12;
127 uint8_t ulGrantSize = 27;
128 uint8_t tmpCrntiSize = 16;
130 /* Fill RAR pdu fields */
133 rapId = rarInfo->RAPID;
136 timeAdv = rarInfo->ta;
137 ulGrant = 0; /* this will be done when implementing msg3 */
138 tmpCrnti = rarInfo->tcrnti;
140 /* Calulating total number of bytes in buffer */
141 totalBits = EBitSize + TBitSize + rapidSize + RBitSize + timeAdvSize \
142 + ulGrantSize + tmpCrntiSize;
144 /* add padding size */
145 totalBits += RBitSize*2 + paddingLcidSize + paddingSize;
147 /* Calulating total number of bytes in buffer */
148 numBytes = totalBits/8;
152 rarInfo->rarPduLen = numBytes;
154 /* Initialize buffer */
155 for(bytePos = 0; bytePos < numBytes; bytePos++)
161 /* Packing fields into RAR PDU */
162 packBytes(rarPdu, &bytePos, &bitPos, EBit, EBitSize);
163 packBytes(rarPdu, &bytePos, &bitPos, TBit, TBitSize);
164 packBytes(rarPdu, &bytePos, &bitPos, rapId, rapidSize);
165 packBytes(rarPdu, &bytePos, &bitPos, RBit, RBitSize);
166 packBytes(rarPdu, &bytePos, &bitPos, timeAdv, timeAdvSize);
167 packBytes(rarPdu, &bytePos, &bitPos, ulGrant, ulGrantSize);
168 packBytes(rarPdu, &bytePos, &bitPos, tmpCrnti, tmpCrntiSize);
170 /* padding of 2 bytes */
171 packBytes(rarPdu, &bytePos, &bitPos, RBit, RBitSize*2);
172 packBytes(rarPdu, &bytePos, &bitPos, paddingLcid, paddingLcidSize);
173 packBytes(rarPdu, &bytePos, &bitPos, 0, paddingSize);
177 /*******************************************************************
179 * @brief Database required to form MAC PDU
183 * Function : createMacRaCb
186 * stores the required params for muxing
188 * @params[in] Pointer to cellId,
192 * ****************************************************************/
193 void createMacRaCb(uint16_t cellId, uint16_t crnti)
195 uint8_t idx = 0; /* supporting 1 UE */
198 GET_CELL_IDX(cellId, cellIdx);
200 macCb.macCell[cellIdx]->macRaCb[idx].cellId = cellId;
201 macCb.macCell[cellIdx]->macRaCb[idx].crnti = crnti;
204 /*************************************************
205 * @brief fill RLC DL Data
209 * Function : fillMsg4DlData
210 * This function sends Dl Data
213 * @param[in] MacDlData *dlData
215 ************************************************/
217 void fillMsg4DlData(uint16_t cellId, MacDlData *dlData, uint8_t *msg4Pdu)
223 GET_CELL_IDX(cellId, cellIdx);
226 dlData->pduInfo[idx].lcId = MAC_LCID_CCCH;
227 dlData->pduInfo[idx].pduLen = macCb.macCell[cellIdx]->macRaCb[idx].msg4PduLen;
228 for(idx2 = 0; idx2 < dlData->pduInfo[idx].pduLen; idx2++)
230 dlData->pduInfo[idx].dlPdu[idx2] = msg4Pdu[idx2];
234 /*************************************************
235 * @brief fill Mac Ce Info
239 * Function : fillMacCe
240 * This function fills Mac ce identities
242 * @param[in] RlcMacData *dlData
244 ************************************************/
246 void fillMacCe(MacCeInfo *macCeInfo, uint8_t *msg3Pdu)
249 macCeInfo->numCes = 1;
250 for(idx = 0; idx < macCeInfo->numCes; idx++)
252 macCeInfo->macCe[idx].macCeLcid = MAC_LCID_CRI;
253 memcpy(macCeInfo->macCe[idx].macCeValue, \
254 msg3Pdu, MAX_CRI_SIZE);
258 /*******************************************************************
260 * @brief Forms MAC PDU
264 * Function : macMuxPdu
267 * The MAC PDU will be MUXed and formed
269 * @params[in] MacDlData *, MacCeInfo *, msg4TxPdu *, tbSize
271 * ****************************************************************/
273 void macMuxPdu(MacDlData *dlData, MacCeInfo *macCeData, uint8_t *msg4TxPdu, uint16_t tbSize)
278 uint8_t macPdu[tbSize];
279 memset(macPdu, 0, (tbSize * sizeof(uint8_t)));
281 /* subheader fields */
282 uint8_t RBit = 0; /* Reserved bit */
283 uint8_t FBit; /* Format Indicator */
284 uint8_t lcid; /* LCID */
285 uint16_t lenField = 0; /* Length field */
287 /* subheader field size (in bits) */
288 uint8_t RBitSize = 1;
289 uint8_t FBitSize = 1;
290 uint8_t lcidSize = 6;
291 uint8_t lenFieldSize = 0; /* 8-bit or 16-bit L field */
293 /* PACK ALL MAC CE */
294 for(idx = 0; idx < macCeData->numCes; idx++)
296 lcid = macCeData->macCe[idx].macCeLcid;
301 /* Packing fields into MAC PDU R/R/LCID */
302 packBytes(macPdu, &bytePos, &bitPos, RBit, (RBitSize * 2));
303 packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
304 memcpy(&macPdu[bytePos], macCeData->macCe[idx].macCeValue,\
306 bytePos += MAX_CRI_SIZE;
310 DU_LOG("\n MAC: Invalid LCID %d in mac pdu",lcid);
315 /* PACK ALL MAC SDUs */
316 for(idx = 0; idx < dlData->numPdu; idx++)
318 lcid = dlData->pduInfo[idx].lcId;
323 lenField = dlData->pduInfo[idx].pduLen;
324 if(dlData->pduInfo[idx].pduLen > 255)
335 /* Packing fields into MAC PDU R/F/LCID/L */
336 packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
337 packBytes(macPdu, &bytePos, &bitPos, FBit, FBitSize);
338 packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
339 packBytes(macPdu, &bytePos, &bitPos, lenField, lenFieldSize);
340 memcpy(&macPdu[bytePos], dlData->pduInfo[idx].dlPdu, lenField);
346 DU_LOG("\n MAC: Invalid LCID %d in mac pdu",lcid);
351 if(bytePos < tbSize && (tbSize-bytePos >= 1))
353 /* padding remaining bytes */
355 lcid = MAC_LCID_PADDING;
356 packBytes(macPdu, &bytePos, &bitPos, RBit, RBitSize);
357 packBytes(macPdu, &bytePos, &bitPos, lcid, lcidSize);
360 /*Storing the muxed pdu in macRaCb */
361 if(msg4TxPdu != NULLP)
363 memcpy(msg4TxPdu, macPdu, tbSize);
367 /**********************************************************************
369 **********************************************************************/