b29351e5d3246c43f6230c002af0b3113cc4f893
[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 : fetchScsValue
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 fetchScsValue(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 *
184 * @brief  SGetSBuf with debug logs
185 *
186 * @details
187 *
188 *    Function : SGetSBufNewForDebug
189 *
190 *    Functionality: SGetSBuf with debug logs
191 *
192 * @params[in] file name, fun name, region, pool, data ptr, size
193 *
194 * @return ROK     - success
195 *         RFAILED - failure
196 *
197 * ****************************************************************/
198 uint8_t SGetSBufNewForDebug(char *file, char *func, char *line, Region region, Pool pool, Data **ptr, Size size)
199 {
200    if(SGetSBuf(region, pool, ptr, size) == ROK)
201    {
202 #ifdef ODU_MEMORY_DEBUG_LOG
203       if (strncmp(func,"cmInetRecvMsg",sizeof("cmInetRecvMsg")))
204       {
205          printf("\nCM_ALLOC=== SGetSBufNewForDebug %s +%d, %s, %d, %p\n",\
206          file, line, func, size, *ptr);
207       }
208 #endif
209       return ROK;
210    }
211    else
212       return RFAILED;
213 }
214
215 /*******************************************************************
216 *
217 * @brief  SPutSBuf with debug logs
218 *
219 * @details
220 *
221 *    Function : SPutSBufNewForDebug
222 *
223 *    Functionality: SPutSBuf with debug logs
224 *
225 * @params[in] file name, fun name, region, pool, data ptr, size
226 *
227 * @return ROK     - success
228 *         RFAILED - failure
229 *
230 * ****************************************************************/
231 uint8_t SPutSBufNewForDebug(char *file, char *func, char *line, Region region, Pool pool, Data *ptr, Size size)
232 {
233    if(SPutSBuf(region, pool, ptr, size) == ROK)
234    {
235 #ifdef ODU_MEMORY_DEBUG_LOG
236       if (strncmp(func,"cmInetRecvMsg",sizeof("cmInetRecvMsg")))
237       {
238          printf("\nCM_FREE=== SPutSBufNewForDebug %s +%d, %s, %d, %p\n",\
239          file, line, func, size, ptr);
240       }
241 #endif
242       return ROK;
243    }
244    else
245       return RFAILED;
246 }
247
248
249 /*******************************************************************
250 *
251 * @brief  SGetStaticBuf with debug logs
252 *
253 * @details
254 *
255 *    Function : SGetStaticBufNewForDebug
256 *
257 *    Functionality: SGetStaticBuf with debug logs
258 *
259 * @params[in] file name, fun name, region, pool, data ptr, size, memType
260 *
261 * @return ROK     - success
262 *         RFAILED - failure
263 *
264 * ****************************************************************/
265 uint8_t SGetStaticBufNewForDebug(char *file, char *func, char *line, \
266 Region region, Pool pool, Data **ptr, Size size, uint8_t memType)
267 {
268    if(SGetStaticBuffer(region, pool, ptr, size, memType) == ROK)
269    {
270 #ifdef ODU_MEMORY_DEBUG_LOG
271       printf("\nCM_ALLOC=== SGetStaticBufNewForDebug %s +%d, %s, %d, %p\n",\
272          file, line, func, size, *ptr);
273 #endif
274       return ROK;
275    }
276    else
277       return RFAILED;
278 }
279
280 /*******************************************************************
281 *
282 * @brief  SPutStaticBuf with debug logs
283 *
284 * @details
285 *
286 *    Function : SPutStaticBufNewForDebug 
287 *
288 *    Functionality: SPutStaticBuf with debug logs
289 *
290 * @params[in] file name, fun name, region, pool, data ptr, size, memType
291 *
292 * @return ROK     - success
293 *         RFAILED - failure
294 *
295 * ****************************************************************/
296 uint8_t SPutStaticBufNewForDebug(char *file, char *func, char *line, \
297 Region region, Pool pool, Data *ptr, Size size, uint8_t memType)
298 {
299    if(SPutStaticBuffer(region, pool, ptr, size, memType) == ROK)
300    {
301 #ifdef ODU_MEMORY_DEBUG_LOG
302       printf("\nCM_FREE=== SPutStaticBufNewForDebug %s +%d, %s, %d, %p\n",\
303          file, line, func, size, ptr);
304 #endif
305       return ROK;
306    }
307    else
308       return RFAILED;
309 }
310 /**********************************************************************
311          End of file
312 **********************************************************************/