Add entity instance as an input parameter to pldm_pdr_tree_add()

- The intent behind this commit is to allow other projects that are
  consuming the libpldm(like openpower/hostboot) to create the
  entities with custom entity instance numbers while creating
  the entity association PDR's.

- If the entity instance number value is 0xFFFF or its parent is
  NULL, then use the existing logic.

- If the entity instance number value is not 0xFFFF and the parent is
  not NULL, should be verify whether the entity exists, if it exists,
  return the entity, otherwise add the entity to the tree.

Tested: Built pldm successfully and Unit Test passes.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: If9b0ac9c1dac3147ed6f95a3e027d155fe2a38c6
diff --git a/libpldm/pdr.c b/libpldm/pdr.c
index d8c0f77..d887917 100644
--- a/libpldm/pdr.c
+++ b/libpldm/pdr.c
@@ -373,12 +373,23 @@
 	return start;
 }
 
-pldm_entity_node *
-pldm_entity_association_tree_add(pldm_entity_association_tree *tree,
-				 pldm_entity *entity, pldm_entity_node *parent,
-				 uint8_t association_type)
+pldm_entity_node *pldm_entity_association_tree_add(
+    pldm_entity_association_tree *tree, pldm_entity *entity,
+    uint16_t entity_instance_number, pldm_entity_node *parent,
+    uint8_t association_type)
 {
 	assert(tree != NULL);
+	assert(entity != NULL);
+
+	if (entity_instance_number != 0xFFFF && parent != NULL) {
+		pldm_entity node;
+		node.entity_type = entity->entity_type;
+		node.entity_instance_num = entity_instance_number;
+		if (pldm_is_current_parent_child(parent, &node)) {
+			return NULL;
+		}
+	}
+
 	assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
 	       association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL);
 	pldm_entity_node *node = malloc(sizeof(pldm_entity_node));
@@ -387,7 +398,8 @@
 	node->first_child = NULL;
 	node->next_sibling = NULL;
 	node->entity.entity_type = entity->entity_type;
-	node->entity.entity_instance_num = 1;
+	node->entity.entity_instance_num =
+	    entity_instance_number != 0xFFFF ? entity_instance_number : 1;
 	node->association_type = association_type;
 
 	if (tree->root == NULL) {
@@ -409,7 +421,9 @@
 		if (prev->entity.entity_type == entity->entity_type) {
 			assert(prev->entity.entity_instance_num != UINT16_MAX);
 			node->entity.entity_instance_num =
-			    prev->entity.entity_instance_num + 1;
+			    entity_instance_number != 0xFFFF
+				? entity_instance_number
+				: prev->entity.entity_instance_num + 1;
 		}
 		prev->next_sibling = node;
 		node->parent = prev->parent;