f3c96ec8991d264194a2181b1b52d240338574a8
[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       printf("startPrbGrp [%d] numPrbGrp [%d] diff [%d]\n",startPrbGrp, numPrbGrp,  (prbGrpStartBit - startPrbGrp));
62       mask = mask << (prbGrpStartBit - startPrbGrp);
63       freqAllocBitMap = freqAllocBitMap | mask;
64       startPrbGrp++;
65       numPrbGrp--;
66    }
67
68    /* Copying 48 LSBs from 64-bit integer to the 45 MSBS in 6-byte array 
69     * The first (left-most / most significant) bit corresponds to the first RB
70     * group in the BWP, and so on
71     */
72    /* On right shifting freqAllocBitMap by 40 bits, the bits 47 to 40 of freqAllocBitMap 
73     * will now become 8-LSB. Copying these 8-bits into freqDomain[].
74     * Now shifting freqAllocBitMap by 32 bits, the bit 39 to 32 of freqAllocBitMap will
75     * now become 8-LSB. Copying these 8-bits into next index of freqDomain.
76     * and so on.
77     */
78    numBitsToRightShift = 40; 
79    mask = 0x0000FF0000000000;
80    for(idx=0; idx<FREQ_DOM_RSRC_SIZE; idx++)
81    {
82       freqDomain[idx] = (freqAllocBitMap & mask) >> numBitsToRightShift;
83       numBitsToRightShift -= 8;
84       mask = mask >> 8;
85    }
86 }
87
88 /*******************************************************************
89  *
90  * @brief Reverse and copy fixed buffer to mBuf 
91  *
92  * @details
93  *
94  *    Function : oduCpyFixBufToMsg
95  *
96  *    Functionality: Reverse and copy fixed buffer to mBuf
97  *
98  * @params[in] Fixed buffer, msg buffer, length of message
99  * @return ROK     - success
100  *         RFAILED - failure
101  *
102  * ****************************************************************/
103 void oduCpyFixBufToMsg(uint8_t *fixBuf, Buffer *mBuf, uint16_t len)                            
104 {
105    uint16_t idx = 0, revIdx = 0, temp = 0, copyLen = 0;
106
107    /* ODU_COPY_FIX_BUF_TO_MSG copies fixed buffer in reverse order. \
108     * Hence reversing the fixed buffer before copying in order to \
109     * maintain the actual order*/
110    for(idx = 0, revIdx = len-1; idx < len/2; idx++, revIdx--)   
111    {                                                            
112         temp = fixBuf[idx];                                          
113         fixBuf[idx] = fixBuf[revIdx];                                   
114         fixBuf[revIdx] = temp;                                       
115    }                                                            
116    ODU_COPY_FIX_BUF_TO_MSG(fixBuf, mBuf, 0, len, (MsgLen *)&copyLen);
117 }
118
119 /*******************************************************************
120  *
121  * @brief Builds PLMN ID 
122  *
123  * @details
124  *
125  *    Function : plmnBuildId
126  *
127  *    Functionality: Building the PLMN ID
128  *
129  * @params[in] PLMNID plmn
130  * @params[out] PLMNID in string format
131  * @return ROK     - success
132  *         RFAILED - failure
133  *
134  * ****************************************************************/
135 uint8_t buildPlmnId(Plmn plmn, uint8_t *buf)
136 {
137    uint8_t mncCnt;
138    mncCnt = 2;
139    buf[0] = ((plmn.mcc[1] << 4) | (plmn.mcc[0]));
140    if(mncCnt == 2)
141    {
142       buf[1]  = ((0xf0) | (plmn.mcc[2]));
143       buf[2] = ((plmn.mnc[1] << 4) | (plmn.mnc[0]));
144    }
145    else
146    {
147       buf[1] = ((plmn.mnc[0] << 4) | (plmn.mcc[2]));
148       buf[2] = ((plmn.mnc[2] << 4) | (plmn.mnc[1]));
149    }
150    return ROK;
151 }
152
153 /*******************************************************************
154 *
155 * @brief  SGetSBuf with debug logs
156 *
157 * @details
158 *
159 *    Function : SGetSBufNewForDebug
160 *
161 *    Functionality: SGetSBuf with debug logs
162 *
163 * @params[in] file name, fun name, region, pool, data ptr, size
164 *
165 * @return ROK     - success
166 *         RFAILED - failure
167 *
168 * ****************************************************************/
169 uint8_t SGetSBufNewForDebug(char *file, char *func, char *line, Region region, Pool pool, Data **ptr, Size size)
170 {
171    if(SGetSBuf(region, pool, ptr, size) == ROK)
172    {
173 #ifdef ODU_MEMORY_DEBUG_LOG
174       printf("\nCM_ALLOC=== SGetSBufNewForDebug %s +%d, %s, %d, %p\n",\
175          file, line, func, size, *ptr);
176 #endif
177       return ROK;
178    }
179    else
180       return RFAILED;
181 }
182
183 /*******************************************************************
184 *
185 * @brief  SPutSBuf with debug logs
186 *
187 * @details
188 *
189 *    Function : SPutSBufNewForDebug
190 *
191 *    Functionality: SPutSBuf with debug logs
192 *
193 * @params[in] file name, fun name, region, pool, data ptr, size
194 *
195 * @return ROK     - success
196 *         RFAILED - failure
197 *
198 * ****************************************************************/
199 uint8_t SPutSBufNewForDebug(char *file, char *func, char *line, Region region, Pool pool, Data *ptr, Size size)
200 {
201    if(SPutSBuf(region, pool, ptr, size) == ROK)
202    {
203 #ifdef ODU_MEMORY_DEBUG_LOG
204       printf("\nCM_FREE=== SPutSBufNewForDebug %s +%d, %s, %d, %p\n",\
205          file, line, func, size, ptr);
206 #endif
207       return ROK;
208    }
209    else
210       return RFAILED;
211 }
212
213
214 /*******************************************************************
215 *
216 * @brief  SGetStaticBuf with debug logs
217 *
218 * @details
219 *
220 *    Function : SGetStaticBufNewForDebug
221 *
222 *    Functionality: SGetStaticBuf with debug logs
223 *
224 * @params[in] file name, fun name, region, pool, data ptr, size, memType
225 *
226 * @return ROK     - success
227 *         RFAILED - failure
228 *
229 * ****************************************************************/
230 uint8_t SGetStaticBufNewForDebug(char *file, char *func, char *line, \
231 Region region, Pool pool, Data **ptr, Size size, uint8_t memType)
232 {
233    if(SGetStaticBuffer(region, pool, ptr, size, memType) == ROK)
234    {
235 #ifdef ODU_MEMORY_DEBUG_LOG
236       printf("\nCM_ALLOC=== SGetStaticBufNewForDebug %s +%d, %s, %d, %p\n",\
237          file, line, func, size, *ptr);
238 #endif
239       return ROK;
240    }
241    else
242       return RFAILED;
243 }
244
245 /*******************************************************************
246 *
247 * @brief  SPutStaticBuf with debug logs
248 *
249 * @details
250 *
251 *    Function : SPutStaticBufNewForDebug 
252 *
253 *    Functionality: SPutStaticBuf with debug logs
254 *
255 * @params[in] file name, fun name, region, pool, data ptr, size, memType
256 *
257 * @return ROK     - success
258 *         RFAILED - failure
259 *
260 * ****************************************************************/
261 uint8_t SPutStaticBufNewForDebug(char *file, char *func, char *line, \
262 Region region, Pool pool, Data *ptr, Size size, uint8_t memType)
263 {
264    if(SPutStaticBuffer(region, pool, ptr, size, memType) == ROK)
265    {
266 #ifdef ODU_MEMORY_DEBUG_LOG
267       printf("\nCM_FREE=== SPutStaticBufNewForDebug %s +%d, %s, %d, %p\n",\
268          file, line, func, size, ptr);
269 #endif
270       return ROK;
271    }
272    else
273       return RFAILED;
274 }
275 /**********************************************************************
276          End of file
277 **********************************************************************/