xref: /openbmc/libbej/include/libbej/bej_tree.h (revision 6f3082ea)
1 #pragma once
2 
3 #include "bej_common.h"
4 
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 
9 #ifdef __cplusplus
10 extern "C"
11 {
12 #endif
13 
14 /**
15  * @brief Holds info needed to encode a parent in the JSON tree.
16  */
17 struct BejEncoderParentMetaData
18 {
19     // Starting dictionary index of the children properties.
20     uint16_t childrenDictPropOffset;
21     // Index of the child pointed by nextChild. Used to store the array
22     // indexes of bejArray elements.
23     uint16_t nextChildIndex;
24     // BEJ sequence number of the property.
25     uint32_t sequenceNumber;
26     // Size needed to encode Sequence number, Format and Length of the
27     // value.
28     size_t sflSize;
29     // Size of the value.
30     size_t vSize;
31     // Dictionary used for this parent.
32     const uint8_t* dictionary;
33     // Points to the next node which is need to process.
34     void* nextChild;
35 };
36 
37 /**
38  * @brief Holds info needed to encode a leaf type in the JSON tree.
39  */
40 struct BejEncoderLeafMetaData
41 {
42     // BEJ sequence number of the property.
43     uint32_t sequenceNumber;
44     // Size needed to encode Sequence number, Format and Length of the
45     // value.
46     size_t sflSize;
47     // Size of the value.
48     size_t vSize;
49 };
50 
51 /**
52  * @brief Common attributes of a JSON property.
53  */
54 struct RedfishPropertyNode
55 {
56     const char* name;
57     struct BejTupleF format;
58     // Properties belonging to the same set or
59     // array and has the same depth.
60     void* sibling;
61 };
62 
63 /**
64  * @brief Used to store parent type property info.
65  *
66  * bejArray, bejSet and bejPropertyAnnotation are the parent type nodes.
67  */
68 struct RedfishPropertyParent
69 {
70     // Common property attributes.
71     struct RedfishPropertyNode nodeAttr;
72     // Number of children in the case of bejSet
73     // or bejArray.
74     size_t nChildren;
75     //  Points to the first child.
76     void* firstChild;
77     // Points to the last child. Technically we only need the firstChild
78     // pointer to add a new element to the list. But to support bejArray
79     // type, we need to traverse the linked list in the same order as the
80     // elements in the array. So we need a pointer to the first child and
81     // keep adding new nodes using the lastChild.
82     void* lastChild;
83     // Metadata used during encoding.
84     struct BejEncoderParentMetaData metaData;
85 };
86 
87 /**
88  * @brief Used to store leaf type property info.
89  *
90  * Every type that doesn't belong to parent type are considered as a leaf
91  * property within a JSON tree. Each specific leaf type has its own struct.
92  * They should include this struct as the first member of their struct.
93  */
94 struct RedfishPropertyLeaf
95 {
96     struct RedfishPropertyNode nodeAttr;
97     struct BejEncoderLeafMetaData metaData;
98 };
99 
100 /**
101  * @brief bejNull type property node.
102  */
103 struct RedfishPropertyLeafNull
104 {
105     struct RedfishPropertyLeaf leaf;
106 };
107 
108 /**
109  * @brief bejInteger type property node.
110  */
111 struct RedfishPropertyLeafInt
112 {
113     struct RedfishPropertyLeaf leaf;
114     int64_t value;
115 };
116 
117 /**
118  * @brief bejEnum type property node.
119  */
120 struct RedfishPropertyLeafEnum
121 {
122     struct RedfishPropertyLeaf leaf;
123     // A string representation of the enum value.
124     const char* value;
125     // Sequence number of the enum value. Populated during bej encoding.
126     uint16_t enumValueSeq;
127 };
128 
129 /**
130  * @brief bejString type property node.
131  */
132 struct RedfishPropertyLeafString
133 {
134     struct RedfishPropertyLeaf leaf;
135     const char* value;
136 };
137 
138 /**
139  * @brief bejReal type property node.
140  */
141 struct RedfishPropertyLeafReal
142 {
143     struct RedfishPropertyLeaf leaf;
144     double value;
145     // bejReal representation of the value. Populated during bej encoding.
146     struct BejReal bejReal;
147 };
148 
149 /**
150  * @brief bejBoolean type property node.
151  */
152 struct RedfishPropertyLeafBool
153 {
154     struct RedfishPropertyLeaf leaf;
155     bool value;
156 };
157 
158 /**
159  * @brief Initialize a bejSet type node.
160  *
161  * @param[in] node - pointer to a RedfishPropertyParent struct.
162  * @param[in] name - name of the node.
163  */
164 void bejTreeInitSet(struct RedfishPropertyParent* node, const char* name);
165 
166 /**
167  * @brief Initialize a bejArray type node.
168  *
169  * @param[in] node - pointer to a RedfishPropertyParent struct.
170  * @param[in] name - name of the node.
171  */
172 void bejTreeInitArray(struct RedfishPropertyParent* node, const char* name);
173 
174 /**
175  * @brief Initialize a bejPropertyAnnotation type node.
176  *
177  * @param[in] node - pointer to a RedfishPropertyParent struct.
178  * @param[in] name - name of the node.
179  */
180 void bejTreeInitPropertyAnnotated(struct RedfishPropertyParent* node,
181                                   const char* name);
182 
183 /**
184  * @brief Check if a node is a parent type node.
185  *
186  * @param node - node to be checked.
187  * @return true if the node is a parent type node.
188  */
189 bool bejTreeIsParentType(struct RedfishPropertyNode* node);
190 
191 /**
192  * @brief Initialize a bejNull type node.
193  *
194  * @param parent - a pointer to an initialized parent struct.
195  * @param child - a pointer to an uninitialized bejNull type node.
196  * @param name - name of the bejNull type property.
197  */
198 void bejTreeAddNull(struct RedfishPropertyParent* parent,
199                     struct RedfishPropertyLeafNull* child, const char* name);
200 
201 /**
202  * @brief Add a bejInteger type node to a parent node.
203  *
204  * @param[in] parent - a pointer to an initialized parent struct.
205  * @param[in] child - a pointer to an uninitialized bejInteger type node.
206  * @param[in] name - name of the bejInteger type property.
207  * @param[in] value - value of the bejInteger type property.
208  */
209 void bejTreeAddInteger(struct RedfishPropertyParent* parent,
210                        struct RedfishPropertyLeafInt* child, const char* name,
211                        int64_t value);
212 
213 /**
214  * @brief Set a new value in bejInteger type node.
215  *
216  * @param[in] node - initialized bejInteger type node.
217  * @param[in] newValue - new integer value.
218  */
219 void bejTreeSetInteger(struct RedfishPropertyLeafInt* node, int64_t newValue);
220 
221 /**
222  * @brief Add a bejEnum type node to a parent node.
223  *
224  * @param[in] parent - a pointer to an initialized parent struct.
225  * @param[in] child - a pointer to an uninitialized bejEnum type node.
226  * @param[in] name - name of the bejEnum type property.
227  * @param[in] value - value of the bejEnum type property.
228  */
229 void bejTreeAddEnum(struct RedfishPropertyParent* parent,
230                     struct RedfishPropertyLeafEnum* child, const char* name,
231                     const char* value);
232 
233 /**
234  * @brief Add a bejString type node to a parent node.
235  *
236  * @param[in] parent - a pointer to an initialized parent struct.
237  * @param[in] child - a pointer to an uninitialized bejString type node.
238  * @param[in] name - name of the bejString type property.
239  * @param[in] value - value of the bejString type property.
240  */
241 void bejTreeAddString(struct RedfishPropertyParent* parent,
242                       struct RedfishPropertyLeafString* child, const char* name,
243                       const char* value);
244 
245 /**
246  * @brief Add a bejReal type node to a parent node.
247  *
248  * @param[in] parent - a pointer to an initialized parent struct.
249  * @param[in] child - a pointer to an uninitialized bejReal type node.
250  * @param[in] name - name of the bejReal type property.
251  * @param[in] value - value of the bejReal type property.
252  */
253 void bejTreeAddReal(struct RedfishPropertyParent* parent,
254                     struct RedfishPropertyLeafReal* child, const char* name,
255                     double value);
256 
257 /**
258  * @brief Set a new value in bejReal type node.
259  *
260  * @param[in] node - initialized bejReal type node.
261  * @param[in] newValue - new double value.
262  */
263 void bejTreeSetReal(struct RedfishPropertyLeafReal* node, double newValue);
264 
265 /**
266  * @brief Add a bejBoolean type node to a parent node.
267  *
268  * @param[in] parent - a pointer to an initialized parent struct.
269  * @param[in] child - a pointer to an uninitialized bejBoolean type node.
270  * @param[in] name - name of the bejBoolean type property.
271  * @param[in] value - value of the bejBoolean type property.
272  */
273 void bejTreeAddBool(struct RedfishPropertyParent* parent,
274                     struct RedfishPropertyLeafBool* child, const char* name,
275                     bool value);
276 
277 /**
278  * @brief Link a node to its parent.
279  *
280  * @param[in] parent  - a pointer to an initialized parent struct.
281  * @param[in] child - a pointer to an initialized child struct.
282  */
283 void bejTreeLinkChildToParent(struct RedfishPropertyParent* parent,
284                               void* child);
285 
286 /**
287  * @brief Set Bej format flags of a node.
288  *
289  * @param node - an initialized node.
290  * @param deferredBinding - set deferred binding flag.
291  * @param readOnlyProperty - set read only property flag.
292  * @param nullableProperty - set nullable property flag.
293  */
294 void bejTreeUpdateNodeFlags(struct RedfishPropertyNode* node,
295                             bool deferredBinding, bool readOnlyProperty,
296                             bool nullableProperty);
297 
298 /**
299  * @brief Get the next child node to be processed.
300  *
301  * This function returns the next child node of the parent node. This also
302  * updates the parent node's next child properties.
303  *
304  * @param parent - parent node.
305  * @param currentChild - current child node being processed.
306  * @return pointer to the next child node to be processed. NULL if there is
307  * no next child or one of the params are NULL.
308  */
309 void* bejParentGoToNextChild(struct RedfishPropertyParent* parent,
310                              struct RedfishPropertyNode* currentChild);
311 
312 #ifdef __cplusplus
313 }
314 #endif
315