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