<[Epic-ID: ODUHIGH-406][Task-ID: ODUHIGH-421]Paging Message: CU_STUB Trigger and...
[o-du/l2.git] / src / codec_utils / common / odu_common_codec.c
1 #include "common_def.h"
2 #include "OCTET_STRING.h"
3 #include "BIT_STRING.h"
4 #include "asn_codecs.h"
5 #include "odu_common_codec.h"
6
7 /*******************************************************************
8  *
9  * @brief Writes the encoded chunks into a buffer
10  *
11  * @details
12  *
13  *    Function : PrepFinalEncBuf
14  *
15  *    Functionality:Fills the encoded buffer
16  *
17  * @params[in] void *buffer,initial encoded data
18  * @params[in] size_t size,size of buffer
19  * @params[in] void *encodedBuf,final buffer
20  * @return ROK     - success
21  *         RFAILED - failure
22  *
23  * ****************************************************************/
24 int PrepFinalEncBuf(const void *buffer, size_t size, void *encodedBuf)
25 {
26    memcpy(encodedBuf + encBufSize, buffer, size);
27    encBufSize += size;
28    return 0;
29 } /* PrepFinalEncBuf */
30
31 /*******************************************************************
32  *
33  * @brief Fills the RicId
34  *
35  * @details
36  *
37  *    Function : FillRicId
38  *
39  *    Functionality: Fills the RicId
40  *
41  * @params[in] BIT_STRING_t *nbid,
42  *             uint8_t unusedBits
43  *             uint8_t byteSize
44  *             uint8_t val
45  *
46  * @return ROK     - success
47  *         RFAILED - failure
48  *
49  * ****************************************************************/
50
51 uint8_t fillBitString(BIT_STRING_t *id, uint8_t unusedBits, uint8_t byteSize, uint64_t data)
52 {
53    uint64_t tmp = 0;
54    uint8_t byteIdx = 0;
55
56    if(id->buf == NULLP)
57    {
58       DU_LOG("\nERROR  --> DU_APP : Buffer allocation is empty");
59       return RFAILED;
60    }
61    memset(id->buf, 0, byteSize);
62    data = data << unusedBits;
63    
64    /*Now, seggregate the value into 'byteSize' number of Octets in sequence:
65     * 1. Pull out a byte of value (Starting from MSB) and put in the 0th Octet
66     * 2. Fill the buffer/String Octet one by one until LSB is reached*/
67    for(byteIdx = 1; byteIdx <= byteSize; byteIdx++)
68    {
69       tmp = (uint64_t)0xFF;
70       tmp = (tmp << (8 * (byteSize - byteIdx)));
71       tmp = (data & tmp) >> (8 * (byteSize - byteIdx));
72       id->buf[byteIdx - 1]  = tmp;
73    }
74    id->bits_unused = unusedBits;
75    return ROK;
76 }
77
78 /*******************************************************************
79  *
80  * @brief Converts bit strings to integer
81  *
82  * @details
83  *
84  *    Function : bitStringToInt
85  *
86  *    Functionality:
87  *      - Converts ASN bit string format IEs to integer type
88  *
89  * @params[in] void
90  * @return ROK     - success
91  *         RFAILED - failure
92  *
93  * ****************************************************************/
94 uint8_t bitStringToInt(BIT_STRING_t *bitString, void *value)
95 {
96    uint16_t idx;
97    uint64_t *val = NULLP;
98
99    if(bitString->buf == NULL || bitString->size <= 0)
100    {
101       DU_LOG("\nERROR  --> DU_APP : Bit string is empty");
102       return RFAILED;
103    }
104
105    if(value)
106       val = (uint64_t *)value;
107    else
108       return RFAILED;
109
110    for(idx=0; idx< bitString->size-1; idx++)
111    {
112       *val |= bitString->buf[idx];
113       *val <<= 8;
114    }
115    *val |= bitString->buf[idx];
116    *val >>= bitString->bits_unused;
117
118    return ROK;
119 }
120
121 /*******************************************************************
122  *
123  * @brief Function to decode teId value from the octect String
124  *
125  * @details
126  *
127  *    Function : teIdStringToInt
128  *
129  *    Functionality: Function to decode teId value from the octect string
130  *                   It can used as generic function to convert 
131  *                   octect string to uint32_t value 
132  *
133  * @params[in]  buf, value
134  * @return void
135  *
136  * ****************************************************************/
137 void teIdStringToInt(uint8_t *buf, uint32_t *val)
138 {
139    uint32_t temp1 = 0, temp2 = 0, temp3 = 0;
140
141    temp1 |= buf[0];
142    temp1 <<= 24;
143
144    temp2 |= buf[1];
145    temp2 <<= 16;
146
147    temp3 |= buf[2];
148    temp3 <<= 8;
149    
150    *val = temp1|temp2|temp3|buf[3];
151 }
152
153 /*******************************************************************
154  *
155  * @brief Function to encode teId value to the octect String
156  *
157  * @details
158  *
159  *    Function : fillTeIdString
160  *
161  *    Functionality: Function to encode teId value to the octect String
162  *                   It can used as generic function to encode 
163  *                   uint32_t value to octect string
164  *
165  * @params[in]  bufSize, value, buf
166  * @return void
167  *
168  * ****************************************************************/
169
170 void fillTeIdString(uint8_t bufSize, uint32_t val, uint8_t *buf)
171 {
172    uint8_t bitPos;
173
174    for(bitPos = 0; bitPos < TEID_BIT_SIZE; bitPos += 8, bufSize--)
175    {
176       /*extracting bitBits from the bitPos*/
177       buf[bufSize] = (((1 << 8) - 1) & (val >> (bitPos))); 
178    }
179 }
180
181 /**********************************************************************
182   End of file
183  **********************************************************************/