Move getConnectedTarget() to generic pdbg util

This was a helper function specific to the resolution code. Instead, it
should be a generic utility function for anyone to use.

Change-Id: Ic52fb5cc45b8b82f3b3baa0935e12e611f24ae8c
Signed-off-by: Zane Shelley <zshelle@us.ibm.com>
diff --git a/util/pdbg.cpp b/util/pdbg.cpp
index d4170a7..c8c8a4f 100644
--- a/util/pdbg.cpp
+++ b/util/pdbg.cpp
@@ -9,6 +9,8 @@
 #include <attributes_info.H>
 #endif
 
+using namespace analyzer;
+
 namespace util
 {
 
@@ -71,6 +73,83 @@
 
 //------------------------------------------------------------------------------
 
+pdbg_target* getConnectedTarget(pdbg_target* i_rxTarget,
+                                const callout::BusType& i_busType)
+{
+    assert(nullptr != i_rxTarget);
+
+    pdbg_target* txTarget = nullptr;
+
+    auto rxType        = util::pdbg::getTrgtType(i_rxTarget);
+    std::string rxPath = util::pdbg::getPath(i_rxTarget);
+
+    if (callout::BusType::SMP_BUS == i_busType &&
+        util::pdbg::TYPE_IOLINK == rxType)
+    {
+        // TODO: Will need to reference some sort of data that can tell us how
+        //       the processors are connected in the system. For now, return the
+        //       RX target to avoid returning a nullptr.
+        trace::inf("No support to get peer target on SMP bus");
+        txTarget = i_rxTarget;
+    }
+    else if (callout::BusType::SMP_BUS == i_busType &&
+             util::pdbg::TYPE_IOHS == rxType)
+    {
+        // TODO: Will need to reference some sort of data that can tell us how
+        //       the processors are connected in the system. For now, return the
+        //       RX target to avoid returning a nullptr.
+        trace::inf("No support to get peer target on SMP bus");
+        txTarget = i_rxTarget;
+    }
+    else if (callout::BusType::OMI_BUS == i_busType &&
+             util::pdbg::TYPE_OMI == rxType)
+    {
+        // This is a bit clunky. The pdbg APIs only give us the ability to
+        // iterate over the children instead of just returning a list. So we'll
+        // push all the children to a list and go from there.
+        std::vector<pdbg_target*> childList;
+
+        pdbg_target* childTarget = nullptr;
+        pdbg_for_each_target("ocmb", i_rxTarget, childTarget)
+        {
+            if (nullptr != childTarget)
+            {
+                childList.push_back(childTarget);
+            }
+        }
+
+        // We know there should only be one OCMB per OMI.
+        if (1 != childList.size())
+        {
+            throw std::logic_error("Invalid child list size for " + rxPath);
+        }
+
+        // Get the connected target.
+        txTarget = childList.front();
+    }
+    else if (callout::BusType::OMI_BUS == i_busType &&
+             util::pdbg::TYPE_OCMB == rxType)
+    {
+        txTarget = pdbg_target_parent("omi", i_rxTarget);
+        if (nullptr == txTarget)
+        {
+            throw std::logic_error("No parent OMI found for " + rxPath);
+        }
+    }
+    else
+    {
+        // This would be a code bug.
+        throw std::logic_error("Unsupported config: i_rxTarget=" + rxPath +
+                               " i_busType=" + i_busType.getString());
+    }
+
+    assert(nullptr != txTarget); // just in case we missed something above
+
+    return txTarget;
+}
+
+//------------------------------------------------------------------------------
+
 pdbg_target* getPibTrgt(pdbg_target* i_procTrgt)
 {
     // The input target must be a processor.