Fix for devices behind the mux being wrongly detected on lower bus addresses

The scanning of the buses was changed to be now sorted by bus number
irrespective of whether the device is behind a mux or not. Also, if a device
was already found on a lower numbered bus and address, FruDevice does not add
it again to the DBus

Signed-off-by: Nikhil Potade <nikhil.potade@linux.intel.com>
Change-Id: Iee492e07dcb1aad0d02897e5a2f4f83b2cfd0154
diff --git a/src/Utils.cpp b/src/Utils.cpp
index 06d2549..ac16d36 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -47,6 +47,36 @@
     return true;
 }
 
+bool getI2cDevicePaths(const fs::path& dirPath,
+                       boost::container::flat_map<size_t, fs::path>& busPaths)
+{
+    if (!fs::exists(dirPath))
+    {
+        return false;
+    }
+
+    // Regex for matching the path
+    std::regex searchPath(std::string(R"(i2c-\d+$)"));
+    // Regex for matching the bus numbers
+    std::regex searchBus(std::string(R"(\w[^-]*$)"));
+    std::smatch matchPath;
+    std::smatch matchBus;
+    for (const auto& p : fs::directory_iterator(dirPath))
+    {
+        std::string path = p.path().string();
+        if (std::regex_search(path, matchPath, searchPath))
+        {
+            if (std::regex_search(path, matchBus, searchBus))
+            {
+                size_t bus = stoul(*matchBus.begin());
+                busPaths.insert(std::pair<size_t, fs::path>(bus, p.path()));
+            }
+        }
+    }
+
+    return true;
+}
+
 bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input)
 {
     valijson::Schema schema;