[Epic-ID: ODUHIGH-406][Task-ID: ODUHIGH-415]: Paging Configuration
[o-du/l2.git] / src / cm / common_def.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 #include "common_def.h"
20
21 /**
22  * @brief frequency domain allocation function. 
23  *
24  * @details
25  *
26  *     Function: fillCoresetFeqDomAllocMap
27  *     
28  *     This function does allocation in frequency domain resource.
29  *     This is a bitmap defining  non-overlapping groups of 6 PRBs in ascending order.
30  *     
31  *  @param[in]  startPrb - start PRB from where the freq alloc starts.  
32  *  @param[in]  prbSize - number of PRBs to be allocted.
33  *  @param[in]  freqDomain - 6 bytes of info, each bit represents a group of 6 PRB.
34  *  @return   void
35  **/
36 void fillCoresetFeqDomAllocMap(uint16_t startPrbGrp, uint16_t numPrbGrp, uint8_t *freqDomain)
37 {
38    uint8_t  idx;
39    uint8_t  prbGrpStartBit = 0;
40    uint8_t  numBitsToRightShift = 0;
41    uint64_t mask = 0;
42    uint64_t freqAllocBitMap = 0;
43
44    /* 
45     * Frequency allocation bit string is 45 bits long. Hence using 6 bytes (i.e. 48 bits) to represent it.
46     * Each bit corresponds to a group of 6 RBs.
47     *
48     * For example if a coreset includes PRB 24 to 47, then on dividing the PRBs into group of 6, 
49     * startPrbGrp = 24/6 = 4
50     * numPrbGrp = 24/6 = 4
51     * 
52     * Frequency allocation bit string is 48 bits long i.e. Bit 47...0
53     * Here, Bit 47 represents RB group 0, Bit 46 represent RB group 45 and so on.
54     * Since startPrbGrp = 4 and numPrbGrp = 4, it means RB group 4,5,6 and 7 are used in coreset.
55     * i.e. Bits 43, 42, 42 and 40 are masked to 1 and rest all bits are 0 in bitstring    
56     */
57    prbGrpStartBit = 47; 
58    while(numPrbGrp)
59    {
60       mask = 1;
61       mask = mask << (prbGrpStartBit - startPrbGrp);
62       freqAllocBitMap = freqAllocBitMap | mask;
63       startPrbGrp++;
64       numPrbGrp--;
65    }
66
67    /* Copying 48 LSBs from 64-bit integer to the 45 MSBS in 6-byte array 
68     * The first (left-most / most significant) bit corresponds to the first RB
69     * group in the BWP, and so on
70     */
71    /* On right shifting freqAllocBitMap by 40 bits, the bits 47 to 40 of freqAllocBitMap 
72     * will now become 8-LSB. Copying these 8-bits into freqDomain[].
73     * Now shifting freqAllocBitMap by 32 bits, the bit 39 to 32 of freqAllocBitMap will
74     * now become 8-LSB. Copying these 8-bits into next index of freqDomain.
75     * and so on.
76     */
77    numBitsToRightShift = 40; 
78    mask = 0x0000FF0000000000;
79    for(idx=0; idx<FREQ_DOM_RSRC_SIZE; idx++)
80    {
81       freqDomain[idx] = (freqAllocBitMap & mask) >> numBitsToRightShift;
82       numBitsToRightShift -= 8;
83       mask = mask >> 8;
84    }
85 }
86
87 /*******************************************************************
88  *
89  * @brief Reverse and copy fixed buffer to mBuf 
90  *
91  * @details
92  *
93  *    Function : oduCpyFixBufToMsg
94  *
95  *    Functionality: Reverse and copy fixed buffer to mBuf
96  *
97  * @params[in] Fixed buffer, msg buffer, length of message
98  * @return ROK     - success
99  *         RFAILED - failure
100  *
101  * ****************************************************************/
102 void oduCpyFixBufToMsg(uint8_t *fixBuf, Buffer *mBuf, uint16_t len)                            
103 {
104    uint16_t idx = 0, revIdx = 0, temp = 0, copyLen = 0;
105
106    /* ODU_COPY_FIX_BUF_TO_MSG copies fixed buffer in reverse order. \
107     * Hence reversing the fixed buffer before copying in order to \
108     * maintain the actual order*/
109    for(idx = 0, revIdx = len-1; idx < len/2; idx++, revIdx--)   
110    {                                                            
111         temp = fixBuf[idx];                                          
112         fixBuf[idx] = fixBuf[revIdx];                                   
113         fixBuf[revIdx] = temp;                                       
114    }                                                            
115    ODU_COPY_FIX_BUF_TO_MSG(fixBuf, mBuf, 0, len, (MsgLen *)&copyLen);
116 }
117
118 /*******************************************************************
119  *
120  * @brief Builds PLMN ID 
121  *
122  * @details
123  *
124  *    Function : plmnBuildId
125  *
126  *    Functionality: Building the PLMN ID
127  *
128  * @params[in] PLMNID plmn
129  * @params[out] PLMNID in string format
130  * @return ROK     - success
131  *         RFAILED - failure
132  *
133  * ****************************************************************/
134 uint8_t buildPlmnId(Plmn plmn, uint8_t *buf)
135 {
136    uint8_t mncCnt;
137    mncCnt = 2;
138    buf[0] = ((plmn.mcc[1] << 4) | (plmn.mcc[0]));
139    if(mncCnt == 2)
140    {
141       buf[1]  = ((0xf0) | (plmn.mcc[2]));
142       buf[2] = ((plmn.mnc[1] << 4) | (plmn.mnc[0]));
143    }
144    else
145    {
146       buf[1] = ((plmn.mnc[0] << 4) | (plmn.mcc[2]));
147       buf[2] = ((plmn.mnc[2] << 4) | (plmn.mnc[1]));
148    }
149    return ROK;
150 }
151
152 /*******************************************************************
153  *
154  * @brief Function to map Sub carrier spacing enum value to value in kHz
155  *
156  * @details
157  *
158  *    Function : convertScsEnumValToScsVal
159  *
160  *    Functionality:
161  *       Function to map Sub carrier spacing enum value to value in kHz
162  *
163  * @params[in] sub-carrier spacing enum value
164  * @return sub-carrier spacing value in kHz
165  *
166  * ****************************************************************/
167 uint16_t convertScsEnumValToScsVal(uint8_t scsEnumValue)
168 {
169    switch(scsEnumValue)
170    {
171       case SCS_15KHZ:
172       case SCS_30KHZ:
173       case SCS_60KHZ:
174       case SCS_120KHZ:
175       case SCS_240KHZ:
176          return (15 * pow(2,scsEnumValue));
177       default:
178          return 15;
179    }
180 }
181
182 /*******************************************************************
183  * @brief convert scs offset value into the enum value received from O1 
184  *
185  * @details
186  *
187  *    Function : convertScsValToScsEnum
188  *
189  *    Functionality:
190  *       - convert scs periodicity value 
191  *
192  * @params[in] uint32_t num
193  * @return ROK     - success
194  *         RFAILED - failure
195  *
196  * ****************************************************************/
197
198 uint8_t convertScsValToScsEnum(uint32_t num)
199 {
200    switch(num)
201    {
202       case 15:
203          return SCS_15KHZ;
204
205       case 30:
206          return SCS_30KHZ;
207
208       case 60:
209          return SCS_60KHZ;
210
211       case 120:
212          return SCS_120KHZ;
213       
214       case 240:
215          return SCS_240KHZ;
216       
217       default:
218          return SCS_15KHZ;
219    }
220 }
221
222 /*******************************************************************
223  * @brief convert scs periodicity value into the enum value received from O1 
224  *
225  * @details
226  *
227  *    Function : convertScsPeriodicityToEnum
228  *
229  *    Functionality:
230  *       - convert scs periodicity value 
231  *
232  * @params[in] uint32_t num
233  * @return ROK     - success
234  *         RFAILED - failure
235  *
236  * ****************************************************************/
237 uint8_t convertScsPeriodicityToEnum(uint32_t num)
238 {
239    switch(num)
240    {
241       case 5:
242          return SCS_5MS;
243
244       case 10:
245          return SCS_10MS;
246
247       case 20:
248          return SCS_20MS;
249
250       case 40:
251          return SCS_40MS;
252
253       case 80:
254          return SCS_80MS;
255
256       case 160:
257          return SCS_160MS;
258
259       default:
260          return SCS_5MS;
261    }
262 }
263 /*******************************************************************
264 *
265 * @brief  SGetSBuf with debug logs
266 *
267 * @details
268 *
269 *    Function : SGetSBufNewForDebug
270 *
271 *    Functionality: SGetSBuf with debug logs
272 *
273 * @params[in] file name, fun name, region, pool, data ptr, size
274 *
275 * @return ROK     - success
276 *         RFAILED - failure
277 *
278 * ****************************************************************/
279 uint8_t SGetSBufNewForDebug(char *file, char *func, char *line, Region region, Pool pool, Data **ptr, Size size)
280 {
281    if(SGetSBuf(region, pool, ptr, size) == ROK)
282    {
283 #ifdef ODU_MEMORY_DEBUG_LOG
284       if (strncmp(func,"cmInetRecvMsg",sizeof("cmInetRecvMsg")))
285       {
286          printf("\nCM_ALLOC=== SGetSBufNewForDebug %s +%d, %s, %d, %p\n",\
287          file, line, func, size, *ptr);
288       }
289 #endif
290       return ROK;
291    }
292    else
293       return RFAILED;
294 }
295
296 /*******************************************************************
297 *
298 * @brief  SPutSBuf with debug logs
299 *
300 * @details
301 *
302 *    Function : SPutSBufNewForDebug
303 *
304 *    Functionality: SPutSBuf with debug logs
305 *
306 * @params[in] file name, fun name, region, pool, data ptr, size
307 *
308 * @return ROK     - success
309 *         RFAILED - failure
310 *
311 * ****************************************************************/
312 uint8_t SPutSBufNewForDebug(char *file, char *func, char *line, Region region, Pool pool, Data *ptr, Size size)
313 {
314    if(SPutSBuf(region, pool, ptr, size) == ROK)
315    {
316 #ifdef ODU_MEMORY_DEBUG_LOG
317       if (strncmp(func,"cmInetRecvMsg",sizeof("cmInetRecvMsg")))
318       {
319          printf("\nCM_FREE=== SPutSBufNewForDebug %s +%d, %s, %d, %p\n",\
320          file, line, func, size, ptr);
321       }
322 #endif
323       return ROK;
324    }
325    else
326       return RFAILED;
327 }
328
329
330 /*******************************************************************
331 *
332 * @brief  SGetStaticBuf with debug logs
333 *
334 * @details
335 *
336 *    Function : SGetStaticBufNewForDebug
337 *
338 *    Functionality: SGetStaticBuf with debug logs
339 *
340 * @params[in] file name, fun name, region, pool, data ptr, size, memType
341 *
342 * @return ROK     - success
343 *         RFAILED - failure
344 *
345 * ****************************************************************/
346 uint8_t SGetStaticBufNewForDebug(char *file, char *func, char *line, \
347 Region region, Pool pool, Data **ptr, Size size, uint8_t memType)
348 {
349    if(SGetStaticBuffer(region, pool, ptr, size, memType) == ROK)
350    {
351 #ifdef ODU_MEMORY_DEBUG_LOG
352       printf("\nCM_ALLOC=== SGetStaticBufNewForDebug %s +%d, %s, %d, %p\n",\
353          file, line, func, size, *ptr);
354 #endif
355       return ROK;
356    }
357    else
358       return RFAILED;
359 }
360
361 /*******************************************************************
362 *
363 * @brief  SPutStaticBuf with debug logs
364 *
365 * @details
366 *
367 *    Function : SPutStaticBufNewForDebug 
368 *
369 *    Functionality: SPutStaticBuf with debug logs
370 *
371 * @params[in] file name, fun name, region, pool, data ptr, size, memType
372 *
373 * @return ROK     - success
374 *         RFAILED - failure
375 *
376 * ****************************************************************/
377 uint8_t SPutStaticBufNewForDebug(char *file, char *func, char *line, \
378 Region region, Pool pool, Data *ptr, Size size, uint8_t memType)
379 {
380    if(SPutStaticBuffer(region, pool, ptr, size, memType) == ROK)
381    {
382 #ifdef ODU_MEMORY_DEBUG_LOG
383       printf("\nCM_FREE=== SPutStaticBufNewForDebug %s +%d, %s, %d, %p\n",\
384          file, line, func, size, ptr);
385 #endif
386       return ROK;
387    }
388    else
389       return RFAILED;
390 }
391
392
393 /*******************************************************************
394 *
395 * @brief  countSetBits in an integer
396 *
397 * @details
398 *
399 *    Function : countSetBits 
400 *
401 *    Functionality: countSetBits in unsigned integer
402 *
403 * @params[in] uint32_t number/Bitmask
404 *
405 * @return [out] uint8_t count of Set Bits
406 *
407 * ****************************************************************/
408 uint8_t countSetBits(uint32_t num)
409 {
410    uint8_t count = 0;
411
412    while(num)
413    {
414       count += num & 1;
415       num >>= 1;
416    }
417    return(count);
418 }
419 /**********************************************************************
420          End of file
421 **********************************************************************/