JIRA-ID:[ODUHIGH-295]- Tunnel creation in EGTP
[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, uint8_t val)
52 {
53    uint8_t tmp;
54    if(id->buf == NULLP)
55    {
56       return RFAILED;
57    }
58
59    for (tmp = 0 ; tmp < (byteSize-1); tmp++)
60    {
61       id->buf[tmp] = tmp;
62    }
63    id->buf[byteSize-1]   = val;
64    id->bits_unused = unusedBits;
65    return ROK;
66 }
67
68 /*******************************************************************
69  *
70  * @brief Converts bit strings to integer
71  *
72  * @details
73  *
74  *    Function : bitStringToInt
75  *
76  *    Functionality:
77  *      - Converts ASN bit string format IEs to integer type
78  *
79  * @params[in] void
80  * @return ROK     - success
81  *         RFAILED - failure
82  *
83  * ****************************************************************/
84 uint8_t bitStringToInt(BIT_STRING_t *bitString, void *value)
85 {
86    uint16_t idx;
87    uint32_t *val = NULLP;
88
89    if(bitString->buf == NULL || bitString->size <= 0)
90    {
91       DU_LOG("\nDU_APP : Bit string is empty");
92       return RFAILED;
93    }
94
95    if(value)
96       val = (uint32_t *)value;
97    else
98       return RFAILED;
99
100    for(idx=0; idx< bitString->size-1; idx++)
101    {
102       *val |= bitString->buf[idx];
103       *val <<= 8;
104     }
105    *val |= bitString->buf[idx];
106    *val >>= bitString->bits_unused;
107
108    return ROK;
109 }
110
111 /*******************************************************************
112  *
113  * @brief Function to decode teId value from the octect String
114  *
115  * @details
116  *
117  *    Function : teIdStringToInt
118  *
119  *    Functionality: Function to decode teId value from the octect string
120  *                   It can used as generic function to convert 
121  *                   octect string to uint32_t value 
122  *
123  * @params[in]  buf, value
124  * @return void
125  *
126  * ****************************************************************/
127 void teIdStringToInt(uint8_t *buf, uint32_t *val)
128 {
129    uint32_t temp1 = 0, temp2 = 0, temp3 = 0;
130
131    temp1 |= buf[0];
132    temp1 <<= 24;
133
134    temp2 |= buf[1];
135    temp2 <<= 16;
136
137    temp3 |= buf[2];
138    temp3 <<= 8;
139    
140    *val = temp1|temp2|temp3|buf[3];
141 }
142
143 /*******************************************************************
144  *
145  * @brief Function to encode teId value to the octect String
146  *
147  * @details
148  *
149  *    Function : fillTeIdString
150  *
151  *    Functionality: Function to encode teId value to the octect String
152  *                   It can used as generic function to encode 
153  *                   uint32_t value to octect string
154  *
155  * @params[in]  bufSize, value, buf
156  * @return void
157  *
158  * ****************************************************************/
159
160 void fillTeIdString(uint8_t bufSize, uint32_t val, uint8_t *buf)
161 {
162    uint8_t bitPos;
163
164    for(bitPos = 0; bitPos < TEID_BIT_SIZE; bitPos += 8, bufSize--)
165    {
166       /*extracting bitBits from the bitPos*/
167       buf[bufSize] = (((1 << 8) - 1) & (val >> (bitPos))); 
168    }
169 }
170
171 /**********************************************************************
172   End of file
173  **********************************************************************/