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