Modify licenses
[scp/ocu/5gnr.git] / Include / bitsPack.h
1 /******************************************************************************
2 *
3 *   Copyright (c) 2020 ICT/CAS.
4 *
5 *   Licensed under the O-RAN Software License, Version 1.0 (the "Software 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 *       https://www.o-ran.org/software
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
20
21 #ifndef __BITS_PACK_H__
22 #define __BITS_PACK_H__
23
24 #include "vos_types.h"
25
26 /* Dependencies ------------------------------------------------------------- */
27
28 /* Constants ---------------------------------------------------------------- */
29
30 /*
31  * The Bits operation type.
32  */
33 typedef enum bitOptType
34 {
35     BIT_PACKED,     /* Packed bits. */
36     BIT_UNPACKED    /* Unpacked bits. */
37 } BitOptType_t;
38
39 /* Types -------------------------------------------------------------------- */
40
41 /*
42  * Type of Bits for pack message operation.
43  */
44 typedef struct bitOpt
45 {
46     UINT8 *pMsg;       /* Point to the message whitch packed or unpacked */
47     UINT32 bitOffset;      /* The bit offset from the start of msg_p. */
48     UINT32 msgLen;         /* The total length of the message. */
49 } BitOpt_t;
50
51
52 /* Globals ------------------------------------------------------------------ */
53
54 extern INT32 unpackBitsWithdebug(BitOpt_t *pBit, UINT32 n,unsigned char *fun, unsigned long line);
55 extern INT32 packBitsWithdebug(BitOpt_t *pBit, UINT32 n, UINT32 value,unsigned char *fun, unsigned long line);
56 /* Functions ---------------------------------------------------------------- */
57 /*******************************************************************************
58  * Convert length of bits to length of bytes.
59  *
60  * Input: bitLen : The lenght of bits.
61  *
62  * Output: return the length of bytes.
63  ******************************************************************************/
64 extern UINT32 convertBitlen2Bytelen(UINT32 bitLen);
65
66 /*******************************************************************************
67  * Get the length of message.
68  *
69  * Input: pBit : The pointer to struct bit operation.
70  *
71  * Output: return the length of message, or -1 when filed.
72  ******************************************************************************/
73 extern INT32 getBitsMsgLen(BitOpt_t *pBit, UINT32 optType);
74
75
76 /*******************************************************************************
77  * Show every bits of memory.
78  *
79  * Input: pMsg: The start pointer of memory.
80  *        msgLen: The length of memory.
81  *        lable: The lable of the memory.
82  *
83  * Output: None.
84  ******************************************************************************/
85 extern void showBits(UINT8 *pMsg, UINT32 msgLen, INT8 *name);
86
87 /*******************************************************************************
88  * Initial the struct of BitOpt.
89  *
90  * Input: pBit : The pointer to struct bit operation.
91  *        pMsg : Point to the message, that will be packed or unpacked.
92  *        msgLen : The length of message.
93  *        bitOffset : The bit offset from the start of msg_p;
94  *
95  * Output: return 0, or -1 when filed.
96  ******************************************************************************/
97 extern INT32 initBits(BitOpt_t *pBit, UINT8 *pMsg, UINT32 msgLen, UINT8 bitOffset);
98
99 /*******************************************************************************
100  * Skip n bits in packed or unpacked operation. Pack operation, not insure the
101  * validity of the bits that ware sikped.
102  *
103  * Input: pBit : The pointer to struct bit operation.
104  *        n : The number of bits will be skiped.
105  *
106  * Output: return 0, or -1 when filed.
107  ******************************************************************************/
108 extern INT32 skipBits(BitOpt_t *pBit, UINT32 n, UINT8 optType);
109
110 /*******************************************************************************
111  * Unpack n bits from message, not support 32 bits or more.
112  *
113  * Input: pBit : The pointer to struct bit operation.
114  *        n : The number of bits will be unpacked.
115  *        msgLen : The length of message.
116  *
117  * Output: return the unpacked value, or -1 when error occur.
118  ******************************************************************************/
119 #define unpackBits(pBit,n)  unpackBitsWithdebug(pBit,  n,(UCHAR *)__func__,__LINE__)
120
121 /*******************************************************************************
122  * Pack the value into message with n bits, not support more than 32 bits.
123  *
124  * Input: pBit : The pointer to struct bit operation.
125  *        n : The number of bits will be packed.
126  *        value : The value need pacded into message.
127  *
128  * Output: return the length of message after packed, or -1 when filed.
129  ******************************************************************************/
130 #define packBits(pBit,n,value) packBitsWithdebug(pBit,  n,  value,(UCHAR *)__func__,__LINE__)
131
132 #endif