JIRA-ID:[ODUHIGH-295]- Tunnel creation in EGTP
[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: freqDomRscAllocType0
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 freqDomRscAllocType0(uint16_t startPrb, uint16_t prbSize, uint8_t *freqDomain)
37 {
38    uint8_t remBits = prbSize; /* each bit represents 6 PRBs */
39    uint8_t firstByte = 1;
40    uint8_t numBits,startBit,byteCount = 5;
41
42    while(remBits)
43    {
44       /* when the startPrb is not in this byteCount */
45       if(startPrb/8)
46       {
47          startPrb -= 8;
48          byteCount--;
49          continue;
50       }
51
52       /* max bytecount is 6 nearly equal to 45 bits*/
53       if(byteCount >= 6)
54           break;
55
56       /* when we are filling the second byte, then the start should be equal to 0 */
57       if(firstByte)
58          startBit = startPrb;
59       else
60          startBit = 0;
61
62       /* calculate the number of bits to be set in this byte */
63       if((remBits+startPrb) <= 8)
64          numBits = remBits;
65       else
66          numBits = 8 - startBit;
67
68       /* bit operation to set the bits */
69                 SET_BITS_MSB((startBit % 8),numBits,freqDomain[byteCount])
70       firstByte = 0;
71
72       /* the ramaining bits should be subtracted with the numBits set in this byte */
73       remBits -= numBits;
74       byteCount--;
75    }
76 }
77
78 /*******************************************************************
79  *
80  * @brief Reverse and copy fixed buffer to mBuf 
81  *
82  * @details
83  *
84  *    Function : oduCpyFixBufToMsg
85  *
86  *    Functionality: Reverse and copy fixed buffer to mBuf
87  *
88  * @params[in] Fixed buffer, msg buffer, length of message
89  * @return ROK     - success
90  *         RFAILED - failure
91  *
92  * ****************************************************************/
93 void oduCpyFixBufToMsg(uint8_t *fixBuf, Buffer *mBuf, uint16_t len)                            
94 {
95    uint16_t idx = 0, revIdx = 0, temp = 0, copyLen = 0;
96
97    /* ODU_COPY_FIX_BUF_TO_MSG copies fixed buffer in reverse order. \
98     * Hence reversing the fixed buffer before copying in order to \
99     * maintain the actual order*/
100    for(idx = 0, revIdx = len-1; idx < len/2; idx++, revIdx--)   
101    {                                                            
102         temp = fixBuf[idx];                                          
103         fixBuf[idx] = fixBuf[revIdx];                                   
104         fixBuf[revIdx] = temp;                                       
105    }                                                            
106    ODU_COPY_FIX_BUF_TO_MSG(fixBuf, mBuf, 0, len, (MsgLen *)&copyLen);
107 }
108
109 /*******************************************************************
110  *
111  * @brief Builds PLMN ID 
112  *
113  * @details
114  *
115  *    Function : plmnBuildId
116  *
117  *    Functionality: Building the PLMN ID
118  *
119  * @params[in] PLMNID plmn
120  * @params[out] PLMNID in string format
121  * @return ROK     - success
122  *         RFAILED - failure
123  *
124  * ****************************************************************/
125 uint8_t buildPlmnId(Plmn plmn, uint8_t *buf)
126 {
127    uint8_t mncCnt;
128    mncCnt = 2;
129    buf[0] = ((plmn.mcc[1] << 4) | (plmn.mcc[0]));
130    if(mncCnt == 2)
131    {
132       buf[1]  = ((0xf0) | (plmn.mcc[2]));
133       buf[2] = ((plmn.mnc[1] << 4) | (plmn.mnc[0]));
134    }
135    else
136    {
137       buf[1] = ((plmn.mnc[0] << 4) | (plmn.mcc[2]));
138       buf[2] = ((plmn.mnc[2] << 4) | (plmn.mnc[1]));
139    }
140    return ROK;
141 }
142
143
144 /**********************************************************************
145          End of file
146 **********************************************************************/