xref: /openbmc/libbej/src/bej_tree.c (revision 99bd6c909c01e2ad0f7838216b826dd87989e0d4)
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 bejTreeAddInteger(struct RedfishPropertyParent* parent,
53                        struct RedfishPropertyLeafInt* child, const char* name,
54                        int64_t value)
55 {
56     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejInteger);
57     child->value = value;
58     bejTreeLinkChildToParent(parent, child);
59 }
60 
61 void bejTreeSetInteger(struct RedfishPropertyLeafInt* node, int64_t newValue)
62 {
63     node->value = newValue;
64 }
65 
66 void bejTreeAddEnum(struct RedfishPropertyParent* parent,
67                     struct RedfishPropertyLeafEnum* child, const char* name,
68                     const char* value)
69 {
70     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejEnum);
71     child->value = value;
72     bejTreeLinkChildToParent(parent, child);
73 }
74 
75 void bejTreeAddString(struct RedfishPropertyParent* parent,
76                       struct RedfishPropertyLeafString* child, const char* name,
77                       const char* value)
78 {
79     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejString);
80     child->value = value;
81     bejTreeLinkChildToParent(parent, child);
82 }
83 
84 void bejTreeAddReal(struct RedfishPropertyParent* parent,
85                     struct RedfishPropertyLeafReal* child, const char* name,
86                     double value)
87 {
88     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejReal);
89     child->value = value;
90     bejTreeLinkChildToParent(parent, child);
91 }
92 
93 void bejTreeSetReal(struct RedfishPropertyLeafReal* node, double newValue)
94 {
95     node->value = newValue;
96 }
97 
98 void bejTreeAddBool(struct RedfishPropertyParent* parent,
99                     struct RedfishPropertyLeafBool* child, const char* name,
100                     bool value)
101 {
102     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejBoolean);
103     child->value = value;
104     bejTreeLinkChildToParent(parent, child);
105 }
106 
107 void bejTreeLinkChildToParent(struct RedfishPropertyParent* parent, void* child)
108 {
109     // A new node is added at the end of the list.
110     if (parent->firstChild == NULL)
111     {
112         parent->firstChild = child;
113     }
114     else
115     {
116         struct RedfishPropertyNode* lastChild = parent->lastChild;
117         lastChild->sibling = child;
118     }
119     parent->lastChild = child;
120     parent->nChildren += 1;
121 }
122 
123 void bejTreeUpdateNodeFlags(struct RedfishPropertyNode* node,
124                             bool deferredBinding, bool readOnlyProperty,
125                             bool nullableProperty)
126 {
127     node->format.deferredBinding = deferredBinding;
128     node->format.readOnlyProperty = readOnlyProperty;
129     node->format.nullableProperty = nullableProperty;
130 }
131 
132 void* bejParentGoToNextChild(struct RedfishPropertyParent* parent,
133                              struct RedfishPropertyNode* currentChild)
134 {
135     if (parent == NULL || currentChild == NULL)
136     {
137         return NULL;
138     }
139 
140     parent->metaData.nextChildIndex += 1;
141     parent->metaData.nextChild = currentChild->sibling;
142     return currentChild->sibling;
143 }
144