Fix PECI client creation flow

This commit fixes the PECI client creation flow to make it retry
the creation when the client is not exposed correctly.

Change-Id: I779cc1865e9ebb8fff90efcbac4503379cadea5a
Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@intel.com>
Signed-off-by: AppaRao Puli <apparao.puli@intel.com>
Signed-off-by: Oleksandr Shulzhenko <oleksandr.shulzhenko.viktorovych@intel.com>
diff --git a/src/IntelCPUSensorMain.cpp b/src/IntelCPUSensorMain.cpp
index bf6e08b..011f2fb 100644
--- a/src/IntelCPUSensorMain.cpp
+++ b/src/IntelCPUSensorMain.cpp
@@ -85,6 +85,7 @@
 };
 
 static constexpr const char* peciDev = "/dev/peci-";
+static constexpr const char* peciDevPath = "/sys/bus/peci/devices/";
 static constexpr const unsigned int rankNumMax = 8;
 
 namespace fs = std::filesystem;
@@ -168,8 +169,9 @@
     }
 
     std::vector<fs::path> hwmonNamePaths;
-    if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"),
-                   R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5))
+    if (!findFiles(fs::path(peciDevPath),
+                   R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
+                   hwmonNamePaths, 6))
     {
         std::cerr << "No CPU sensors in system\n";
         return true;
@@ -403,7 +405,7 @@
     return true;
 }
 
-void exportDevice(const CPUConfig& config)
+bool exportDevice(const CPUConfig& config)
 {
     std::ostringstream hex;
     hex << std::hex << config.addr;
@@ -411,9 +413,12 @@
     std::string busStr = std::to_string(config.bus);
 
     std::string parameters = "peci-client 0x" + addrHexStr;
-    std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
+    std::string devPath = peciDevPath;
+    std::string delDevice = devPath + "peci-" + busStr + "/delete_device";
+    std::string newDevice = devPath + "peci-" + busStr + "/new_device";
+    std::string newClient = devPath + busStr + "-" + addrHexStr + "/driver";
 
-    std::filesystem::path devicePath(device);
+    std::filesystem::path devicePath(newDevice);
     const std::string& dir = devicePath.parent_path().string();
     for (const auto& path : std::filesystem::directory_iterator(dir))
     {
@@ -431,20 +436,38 @@
                 std::cout << parameters << " on bus " << busStr
                           << " is already exported\n";
             }
-            return;
+
+            std::ofstream delDeviceFile(delDevice);
+            if (!delDeviceFile.good())
+            {
+                std::cerr << "Error opening " << delDevice << "\n";
+                return false;
+            }
+            delDeviceFile << parameters;
+            delDeviceFile.close();
+
+            break;
         }
     }
 
-    std::ofstream deviceFile(device);
+    std::ofstream deviceFile(newDevice);
     if (!deviceFile.good())
     {
-        std::cerr << "Error writing " << device << "\n";
-        return;
+        std::cerr << "Error opening " << newDevice << "\n";
+        return false;
     }
     deviceFile << parameters;
     deviceFile.close();
 
+    if (!std::filesystem::exists(newClient))
+    {
+        std::cerr << "Error creating " << newClient << "\n";
+        return false;
+    }
+
     std::cout << parameters << " on bus " << busStr << " is exported\n";
+
+    return true;
 }
 
 void detectCpu(boost::asio::steady_timer& pingTimer,
@@ -460,6 +483,11 @@
 
     for (CPUConfig& config : cpuConfigs)
     {
+        if (config.state == State::READY)
+        {
+            continue;
+        }
+
         std::string peciDevPath = peciDev + std::to_string(config.bus);
 
         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
@@ -519,8 +547,23 @@
             {
                 if (config.state == State::OFF)
                 {
-                    std::cout << config.name << " is detected\n";
-                    exportDevice(config);
+                    std::array<uint8_t, 8> pkgConfig{};
+                    uint8_t cc = 0;
+
+                    if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_CPU_ID, 0,
+                                         4, &pkgConfig[0],
+                                         &cc) == PECI_CC_SUCCESS)
+                    {
+                        std::cout << config.name << " is detected\n";
+                        if (!exportDevice(config))
+                        {
+                            newState = State::OFF;
+                        }
+                    }
+                    else
+                    {
+                        newState = State::OFF;
+                    }
                 }
 
                 if (newState == State::ON)