cpusensor: refine CPU detection logic

This commit refines CPU detection logic using gpio line name based
handling.

Tested: Worked well with new SGPIO kernel driver and updated
platform configuration.

Change-Id: I5dae2b7497b6b5291b56e73d2175ca8cbb0580d4
Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com>
diff --git a/include/CPUSensor.hpp b/include/CPUSensor.hpp
index 5f48674..7a2819f 100644
--- a/include/CPUSensor.hpp
+++ b/include/CPUSensor.hpp
@@ -49,74 +49,57 @@
 
 // this is added to cpusensor.hpp to avoid having every sensor have to link
 // against libgpiod, if another sensor needs it we may move it to utils
-inline bool hostIsPresent(size_t gpioNum)
+inline bool cpuIsPresent(
+    const boost::container::flat_map<std::string, BasicVariantType>& gpioConfig)
 {
-    static boost::container::flat_map<size_t, bool> cpuPresence;
+    static boost::container::flat_map<std::string, bool> cpuPresence;
 
-    auto findIndex = cpuPresence.find(gpioNum);
+    auto findName = gpioConfig.find("Name");
+    if (findName == gpioConfig.end())
+    {
+        return false;
+    }
+    std::string gpioName =
+        std::visit(VariantToStringVisitor(), findName->second);
+
+    auto findIndex = cpuPresence.find(gpioName);
     if (findIndex != cpuPresence.end())
     {
         return findIndex->second;
     }
 
-    constexpr size_t sgpioBase = 232;
-
-    // check if sysfs has device
-    bool sysfs = std::filesystem::exists(gpioPath + std::string("gpio") +
-                                         std::to_string(gpioNum));
-
-    // todo: delete this when we remove all sysfs code
-    if (sysfs)
+    bool activeHigh = true;
+    auto findPolarity = gpioConfig.find("Polarity");
+    if (findPolarity != gpioConfig.end())
     {
-        // close it, we'll reopen it at the end
-        std::ofstream unexport(gpioPath + std::string("unexport"));
-        if (unexport.good())
+        if (std::string("Low") ==
+            std::visit(VariantToStringVisitor(), findPolarity->second))
         {
-            unexport << gpioNum;
-        }
-        else
-        {
-            std::cerr << "Error cleaning up sysfs device\n";
+            activeHigh = false;
         }
     }
 
-    size_t chipNum = (gpioNum - sgpioBase) / 8;
-    size_t index = (gpioNum - sgpioBase) % 8;
-    gpiod::chip chip("gpiochip" + std::to_string(chipNum));
-    auto line = chip.get_line(index);
-
+    auto line = gpiod::find_line(gpioName);
     if (!line)
     {
-        std::cerr << "Error requesting gpio\n";
-        return true;
+        std::cerr << "Error requesting gpio: " << gpioName << "\n";
+        return false;
     }
 
-    bool resp = true;
+    bool resp;
     try
     {
-        line.request({"adcsensor", gpiod::line_request::DIRECTION_INPUT, 0});
-        resp = !line.get_value();
+        line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT,
+                      activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW});
+        resp = line.get_value();
     }
     catch (std::system_error&)
     {
-        std::cerr << "Error reading gpio\n";
-        return true;
+        std::cerr << "Error reading gpio: " << gpioName << "\n";
+        return false;
     }
 
-    // todo: delete this when we remove all sysfs code
-    if (sysfs)
-    {
-        // reopen it
-        std::ofstream populate(gpioPath + std::string("export"));
-        if (populate.good())
-        {
-            populate << gpioNum;
-        }
-        else
-        {
-            std::cerr << "Error cleaning up sysfs device\n";
-        }
-    }
-    cpuPresence[gpioNum] = resp;
+    cpuPresence[gpioName] = resp;
+
     return resp;
 }