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