xref: /openbmc/libbej/src/bej_tree.c (revision 2bc745a3)
1 #include "bej_tree.h"
2 
3 static void bejTreeInitParent(struct RedfishPropertyParent* node,
4                               const char* name, enum BejPrincipalDataType type)
5 {
6     node->nodeAttr.name = name;
7     node->nodeAttr.format.principalDataType = type;
8     node->nodeAttr.format.deferredBinding = 0;
9     node->nodeAttr.format.readOnlyProperty = 0;
10     node->nodeAttr.format.nullableProperty = 0;
11     node->nodeAttr.sibling = NULL;
12     node->nChildren = 0;
13     node->firstChild = NULL;
14     node->lastChild = NULL;
15 }
16 
17 void bejTreeInitSet(struct RedfishPropertyParent* node, const char* name)
18 {
19     bejTreeInitParent(node, name, bejSet);
20 }
21 
22 void bejTreeInitArray(struct RedfishPropertyParent* node, const char* name)
23 {
24     bejTreeInitParent(node, name, bejArray);
25 }
26 
27 void bejTreeInitPropertyAnnotated(struct RedfishPropertyParent* node,
28                                   const char* name)
29 {
30     bejTreeInitParent(node, name, bejPropertyAnnotation);
31 }
32 
33 bool bejTreeIsParentType(struct RedfishPropertyNode* node)
34 {
35     return node->format.principalDataType == bejSet ||
36            node->format.principalDataType == bejArray ||
37            node->format.principalDataType == bejPropertyAnnotation;
38 }
39 
40 static void bejTreeInitChildNode(struct RedfishPropertyLeaf* node,
41                                  const char* name,
42                                  enum BejPrincipalDataType type)
43 {
44     node->nodeAttr.name = name;
45     node->nodeAttr.format.principalDataType = type;
46     node->nodeAttr.format.deferredBinding = 0;
47     node->nodeAttr.format.readOnlyProperty = 0;
48     node->nodeAttr.format.nullableProperty = 0;
49     node->nodeAttr.sibling = NULL;
50 }
51 
52 void bejTreeAddNull(struct RedfishPropertyParent* parent,
53                     struct RedfishPropertyLeafNull* child, const char* name)
54 {
55     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejNull);
56     bejTreeLinkChildToParent(parent, child);
57 }
58 
59 void bejTreeAddInteger(struct RedfishPropertyParent* parent,
60                        struct RedfishPropertyLeafInt* child, const char* name,
61                        int64_t value)
62 {
63     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejInteger);
64     child->value = value;
65     bejTreeLinkChildToParent(parent, child);
66 }
67 
68 void bejTreeSetInteger(struct RedfishPropertyLeafInt* node, int64_t newValue)
69 {
70     node->value = newValue;
71 }
72 
73 void bejTreeAddEnum(struct RedfishPropertyParent* parent,
74                     struct RedfishPropertyLeafEnum* child, const char* name,
75                     const char* value)
76 {
77     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejEnum);
78     child->value = value;
79     bejTreeLinkChildToParent(parent, child);
80 }
81 
82 void bejTreeAddString(struct RedfishPropertyParent* parent,
83                       struct RedfishPropertyLeafString* child, const char* name,
84                       const char* value)
85 {
86     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejString);
87     child->value = value;
88     bejTreeLinkChildToParent(parent, child);
89 }
90 
91 void bejTreeAddReal(struct RedfishPropertyParent* parent,
92                     struct RedfishPropertyLeafReal* child, const char* name,
93                     double value)
94 {
95     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejReal);
96     child->value = value;
97     bejTreeLinkChildToParent(parent, child);
98 }
99 
100 void bejTreeSetReal(struct RedfishPropertyLeafReal* node, double newValue)
101 {
102     node->value = newValue;
103 }
104 
105 void bejTreeAddBool(struct RedfishPropertyParent* parent,
106                     struct RedfishPropertyLeafBool* child, const char* name,
107                     bool value)
108 {
109     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejBoolean);
110     child->value = value;
111     bejTreeLinkChildToParent(parent, child);
112 }
113 
114 void bejTreeLinkChildToParent(struct RedfishPropertyParent* parent, void* child)
115 {
116     // A new node is added at the end of the list.
117     if (parent->firstChild == NULL)
118     {
119         parent->firstChild = child;
120     }
121     else
122     {
123         struct RedfishPropertyNode* lastChild = parent->lastChild;
124         lastChild->sibling = child;
125     }
126     parent->lastChild = child;
127     parent->nChildren += 1;
128 }
129 
130 void bejTreeUpdateNodeFlags(struct RedfishPropertyNode* node,
131                             bool deferredBinding, bool readOnlyProperty,
132                             bool nullableProperty)
133 {
134     node->format.deferredBinding = deferredBinding;
135     node->format.readOnlyProperty = readOnlyProperty;
136     node->format.nullableProperty = nullableProperty;
137 }
138 
139 void* bejParentGoToNextChild(struct RedfishPropertyParent* parent,
140                              struct RedfishPropertyNode* currentChild)
141 {
142     if (parent == NULL || currentChild == NULL)
143     {
144         return NULL;
145     }
146 
147     parent->metaData.nextChildIndex += 1;
148     parent->metaData.nextChild = currentChild->sibling;
149     return currentChild->sibling;
150 }
151