xref: /openbmc/libbej/src/bej_tree.c (revision bde1bfa1)
12ebe82f5Skasunath #include "bej_tree.h"
22ebe82f5Skasunath 
bejTreeInitParent(struct RedfishPropertyParent * node,const char * name,enum BejPrincipalDataType type)32ebe82f5Skasunath static void bejTreeInitParent(struct RedfishPropertyParent* node,
42ebe82f5Skasunath                               const char* name, enum BejPrincipalDataType type)
52ebe82f5Skasunath {
62ebe82f5Skasunath     node->nodeAttr.name = name;
72ebe82f5Skasunath     node->nodeAttr.format.principalDataType = type;
82ebe82f5Skasunath     node->nodeAttr.format.deferredBinding = 0;
92ebe82f5Skasunath     node->nodeAttr.format.readOnlyProperty = 0;
102ebe82f5Skasunath     node->nodeAttr.format.nullableProperty = 0;
112ebe82f5Skasunath     node->nodeAttr.sibling = NULL;
122ebe82f5Skasunath     node->nChildren = 0;
132ebe82f5Skasunath     node->firstChild = NULL;
142ebe82f5Skasunath     node->lastChild = NULL;
152ebe82f5Skasunath }
162ebe82f5Skasunath 
bejTreeInitSet(struct RedfishPropertyParent * node,const char * name)172ebe82f5Skasunath void bejTreeInitSet(struct RedfishPropertyParent* node, const char* name)
182ebe82f5Skasunath {
192ebe82f5Skasunath     bejTreeInitParent(node, name, bejSet);
202ebe82f5Skasunath }
212ebe82f5Skasunath 
bejTreeInitArray(struct RedfishPropertyParent * node,const char * name)222ebe82f5Skasunath void bejTreeInitArray(struct RedfishPropertyParent* node, const char* name)
232ebe82f5Skasunath {
242ebe82f5Skasunath     bejTreeInitParent(node, name, bejArray);
252ebe82f5Skasunath }
262ebe82f5Skasunath 
bejTreeInitPropertyAnnotated(struct RedfishPropertyParent * node,const char * name)272ebe82f5Skasunath void bejTreeInitPropertyAnnotated(struct RedfishPropertyParent* node,
282ebe82f5Skasunath                                   const char* name)
292ebe82f5Skasunath {
302ebe82f5Skasunath     bejTreeInitParent(node, name, bejPropertyAnnotation);
312ebe82f5Skasunath }
322ebe82f5Skasunath 
bejTreeIsParentType(struct RedfishPropertyNode * node)33dc3f214bSkasunath bool bejTreeIsParentType(struct RedfishPropertyNode* node)
34dc3f214bSkasunath {
35dc3f214bSkasunath     return node->format.principalDataType == bejSet ||
36dc3f214bSkasunath            node->format.principalDataType == bejArray ||
37dc3f214bSkasunath            node->format.principalDataType == bejPropertyAnnotation;
38dc3f214bSkasunath }
39dc3f214bSkasunath 
bejTreeInitChildNode(struct RedfishPropertyLeaf * node,const char * name,enum BejPrincipalDataType type)402ebe82f5Skasunath static void bejTreeInitChildNode(struct RedfishPropertyLeaf* node,
412ebe82f5Skasunath                                  const char* name,
422ebe82f5Skasunath                                  enum BejPrincipalDataType type)
432ebe82f5Skasunath {
442ebe82f5Skasunath     node->nodeAttr.name = name;
452ebe82f5Skasunath     node->nodeAttr.format.principalDataType = type;
462ebe82f5Skasunath     node->nodeAttr.format.deferredBinding = 0;
472ebe82f5Skasunath     node->nodeAttr.format.readOnlyProperty = 0;
482ebe82f5Skasunath     node->nodeAttr.format.nullableProperty = 0;
492ebe82f5Skasunath     node->nodeAttr.sibling = NULL;
502ebe82f5Skasunath }
512ebe82f5Skasunath 
bejTreeAddNull(struct RedfishPropertyParent * parent,struct RedfishPropertyLeafNull * child,const char * name)52*bde1bfa1Skasunath void bejTreeAddNull(struct RedfishPropertyParent* parent,
53*bde1bfa1Skasunath                     struct RedfishPropertyLeafNull* child, const char* name)
54*bde1bfa1Skasunath {
55*bde1bfa1Skasunath     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejNull);
56*bde1bfa1Skasunath     bejTreeLinkChildToParent(parent, child);
57*bde1bfa1Skasunath }
58*bde1bfa1Skasunath 
bejTreeAddInteger(struct RedfishPropertyParent * parent,struct RedfishPropertyLeafInt * child,const char * name,int64_t value)592ebe82f5Skasunath void bejTreeAddInteger(struct RedfishPropertyParent* parent,
602ebe82f5Skasunath                        struct RedfishPropertyLeafInt* child, const char* name,
612ebe82f5Skasunath                        int64_t value)
622ebe82f5Skasunath {
632ebe82f5Skasunath     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejInteger);
642ebe82f5Skasunath     child->value = value;
652ebe82f5Skasunath     bejTreeLinkChildToParent(parent, child);
662ebe82f5Skasunath }
672ebe82f5Skasunath 
bejTreeSetInteger(struct RedfishPropertyLeafInt * node,int64_t newValue)682ebe82f5Skasunath void bejTreeSetInteger(struct RedfishPropertyLeafInt* node, int64_t newValue)
692ebe82f5Skasunath {
702ebe82f5Skasunath     node->value = newValue;
712ebe82f5Skasunath }
722ebe82f5Skasunath 
bejTreeAddEnum(struct RedfishPropertyParent * parent,struct RedfishPropertyLeafEnum * child,const char * name,const char * value)732ebe82f5Skasunath void bejTreeAddEnum(struct RedfishPropertyParent* parent,
742ebe82f5Skasunath                     struct RedfishPropertyLeafEnum* child, const char* name,
752ebe82f5Skasunath                     const char* value)
762ebe82f5Skasunath {
772ebe82f5Skasunath     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejEnum);
782ebe82f5Skasunath     child->value = value;
792ebe82f5Skasunath     bejTreeLinkChildToParent(parent, child);
802ebe82f5Skasunath }
812ebe82f5Skasunath 
bejTreeAddString(struct RedfishPropertyParent * parent,struct RedfishPropertyLeafString * child,const char * name,const char * value)82dc3f214bSkasunath void bejTreeAddString(struct RedfishPropertyParent* parent,
83dc3f214bSkasunath                       struct RedfishPropertyLeafString* child, const char* name,
84dc3f214bSkasunath                       const char* value)
85dc3f214bSkasunath {
86dc3f214bSkasunath     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejString);
87dc3f214bSkasunath     child->value = value;
88dc3f214bSkasunath     bejTreeLinkChildToParent(parent, child);
89dc3f214bSkasunath }
90dc3f214bSkasunath 
bejTreeAddReal(struct RedfishPropertyParent * parent,struct RedfishPropertyLeafReal * child,const char * name,double value)91dc3f214bSkasunath void bejTreeAddReal(struct RedfishPropertyParent* parent,
92dc3f214bSkasunath                     struct RedfishPropertyLeafReal* child, const char* name,
93dc3f214bSkasunath                     double value)
94dc3f214bSkasunath {
95dc3f214bSkasunath     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejReal);
96dc3f214bSkasunath     child->value = value;
97dc3f214bSkasunath     bejTreeLinkChildToParent(parent, child);
98dc3f214bSkasunath }
99dc3f214bSkasunath 
bejTreeSetReal(struct RedfishPropertyLeafReal * node,double newValue)100dc3f214bSkasunath void bejTreeSetReal(struct RedfishPropertyLeafReal* node, double newValue)
101dc3f214bSkasunath {
102dc3f214bSkasunath     node->value = newValue;
103dc3f214bSkasunath }
104dc3f214bSkasunath 
bejTreeAddBool(struct RedfishPropertyParent * parent,struct RedfishPropertyLeafBool * child,const char * name,bool value)105dc3f214bSkasunath void bejTreeAddBool(struct RedfishPropertyParent* parent,
106dc3f214bSkasunath                     struct RedfishPropertyLeafBool* child, const char* name,
107dc3f214bSkasunath                     bool value)
108dc3f214bSkasunath {
109dc3f214bSkasunath     bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejBoolean);
110dc3f214bSkasunath     child->value = value;
111dc3f214bSkasunath     bejTreeLinkChildToParent(parent, child);
112dc3f214bSkasunath }
113dc3f214bSkasunath 
bejTreeLinkChildToParent(struct RedfishPropertyParent * parent,void * child)1142ebe82f5Skasunath void bejTreeLinkChildToParent(struct RedfishPropertyParent* parent, void* child)
1152ebe82f5Skasunath {
1162ebe82f5Skasunath     // A new node is added at the end of the list.
1172ebe82f5Skasunath     if (parent->firstChild == NULL)
1182ebe82f5Skasunath     {
1192ebe82f5Skasunath         parent->firstChild = child;
1202ebe82f5Skasunath     }
1212ebe82f5Skasunath     else
1222ebe82f5Skasunath     {
1232ebe82f5Skasunath         struct RedfishPropertyNode* lastChild = parent->lastChild;
1242ebe82f5Skasunath         lastChild->sibling = child;
1252ebe82f5Skasunath     }
1262ebe82f5Skasunath     parent->lastChild = child;
1272ebe82f5Skasunath     parent->nChildren += 1;
1282ebe82f5Skasunath }
129dc3f214bSkasunath 
bejTreeUpdateNodeFlags(struct RedfishPropertyNode * node,bool deferredBinding,bool readOnlyProperty,bool nullableProperty)130dc3f214bSkasunath void bejTreeUpdateNodeFlags(struct RedfishPropertyNode* node,
131dc3f214bSkasunath                             bool deferredBinding, bool readOnlyProperty,
132dc3f214bSkasunath                             bool nullableProperty)
133dc3f214bSkasunath {
134dc3f214bSkasunath     node->format.deferredBinding = deferredBinding;
135dc3f214bSkasunath     node->format.readOnlyProperty = readOnlyProperty;
136dc3f214bSkasunath     node->format.nullableProperty = nullableProperty;
137dc3f214bSkasunath }
13899bd6c90Skasunath 
bejParentGoToNextChild(struct RedfishPropertyParent * parent,struct RedfishPropertyNode * currentChild)13999bd6c90Skasunath void* bejParentGoToNextChild(struct RedfishPropertyParent* parent,
14099bd6c90Skasunath                              struct RedfishPropertyNode* currentChild)
14199bd6c90Skasunath {
14299bd6c90Skasunath     if (parent == NULL || currentChild == NULL)
14399bd6c90Skasunath     {
14499bd6c90Skasunath         return NULL;
14599bd6c90Skasunath     }
14699bd6c90Skasunath 
14799bd6c90Skasunath     parent->metaData.nextChildIndex += 1;
14899bd6c90Skasunath     parent->metaData.nextChild = currentChild->sibling;
14999bd6c90Skasunath     return currentChild->sibling;
15099bd6c90Skasunath }
151