Verify currect node is a child of current parent

This commit is to add the pldm_is_current_parent_child method and
to verify that the current node is a child of the current parent,
If so, return true, otherwise return false.

Tested: built pldm successfully and unit test passes.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: Ie2f01d948178dfdf3a5bfa1c6c0a939b6fdd96e3
diff --git a/libpldm/pdr.c b/libpldm/pdr.c
index 562c9f9..d8c0f77 100644
--- a/libpldm/pdr.c
+++ b/libpldm/pdr.c
@@ -520,6 +520,25 @@
 	return count;
 }
 
+bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node)
+{
+	assert(parent != NULL);
+	assert(node != NULL);
+
+	pldm_entity_node *curr = parent->first_child;
+	while (curr != NULL) {
+		if (node->entity_type == curr->entity.entity_type &&
+		    node->entity_instance_num ==
+			curr->entity.entity_instance_num) {
+
+			return true;
+		}
+		curr = curr->next_sibling;
+	}
+
+	return false;
+}
+
 static void _entity_association_pdr_add_entry(pldm_entity_node *curr,
 					      pldm_pdr *repo, uint16_t size,
 					      uint8_t contained_count,
diff --git a/libpldm/pdr.h b/libpldm/pdr.h
index bf31405..8d827dc 100644
--- a/libpldm/pdr.h
+++ b/libpldm/pdr.h
@@ -293,6 +293,13 @@
 uint8_t pldm_entity_get_num_children(pldm_entity_node *node,
 				     uint8_t association_type);
 
+/** @brief Verify that the current node is a child of the current parent
+ *
+ *  @param[in] parent    - opaque pointer acting as a handle to an entity parent
+ *  @param[in] node      - pointer to the node of the pldm entity
+ */
+bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node);
+
 /** @brief Find an entity in the entity association tree
  *
  *  @param[in] tree - pointer to entity association tree
diff --git a/libpldm/tests/libpldm_pdr_test.cpp b/libpldm/tests/libpldm_pdr_test.cpp
index 8dcb413..b7fb59a 100644
--- a/libpldm/tests/libpldm_pdr_test.cpp
+++ b/libpldm/tests/libpldm_pdr_test.cpp
@@ -1277,3 +1277,43 @@
 
     free(out);
 }
+
+TEST(EntityAssociationPDR, testGetChildren)
+{
+    pldm_entity entities[4]{};
+    entities[0].entity_type = 1;
+    entities[1].entity_type = 2;
+    entities[2].entity_type = 2;
+    entities[3].entity_type = 3;
+
+    auto tree = pldm_entity_association_tree_init();
+    auto l1 = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
+                                               PLDM_ENTITY_ASSOCIAION_PHYSICAL);
+    EXPECT_NE(l1, nullptr);
+    auto l2a = pldm_entity_association_tree_add(
+        tree, &entities[1], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
+    EXPECT_NE(l2a, nullptr);
+    auto l2b = pldm_entity_association_tree_add(
+        tree, &entities[2], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
+    EXPECT_NE(l2b, nullptr);
+    auto l2c = pldm_entity_association_tree_add(
+        tree, &entities[3], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
+    EXPECT_NE(l2c, nullptr);
+
+    pldm_entity et1;
+    et1.entity_type = 2;
+    et1.entity_instance_num = 1;
+    EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
+
+    pldm_entity et2;
+    et2.entity_type = 2;
+    et2.entity_instance_num = 2;
+    EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
+
+    pldm_entity et3;
+    et3.entity_type = 2;
+    et3.entity_instance_num = 3;
+    EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
+
+    pldm_entity_association_tree_destroy(tree);
+}
\ No newline at end of file