Allow disabling PID loops at runtime

<design concept>
Add the map of object enable interface to pid loops
in the zone then we can disable/enable each pid loop
process in a zone by dbus command.

[note]
Enable = true  : enable process (default)
Enable = false : disable process

Tested:
In this case: we set Enable = false to disable
pidloop:Zone_Temp_0, and see how it affects
the zone final pwm, when pidloop: Zone_Temp_0
in zone 0 is disabled.

then even we are trying to heat up the temperature
of a sensor: Temp_0 in pidloop: Zone_Temp_0, this
set point of the pidloop will not be taken into the
calculation for the final set point of the whole zone.

```
<service object>
root@openbmc:/tmp# busctl tree xyz.openbmc_project.State.FanCtrl
`-/xyz
  `-/xyz/openbmc_project
    `-/xyz/openbmc_project/settings
      `-/xyz/openbmc_project/settings/fanctrl
        |-/xyz/openbmc_project/settings/fanctrl/zone0
        | |-/xyz/openbmc_project/settings/fanctrl/zone0/Zone_Temp
        | |-/xyz/openbmc_project/settings/fanctrl/zone0/Zone_Temp_0
        | `-/xyz/openbmc_project/settings/fanctrl/zone0/Zone_Temp_1

====Enable process for pidloop:Zone_Temp_0 with p-switch temperature sensor:Temp_0 at runtime====
root@openbmc:~# busctl introspect xyz.openbmc_project.State.FanCtrl /xyz/openbmc_project/settings/fanctrl/zone0/Zone_Temp_0
NAME                                TYPE      SIGNATURE RESULT/VALUE FLAGS
xyz.openbmc_project.Object.Enable   interface -         -            -
.Enabled                            property  b         true         emits-change writable

====Disable process for pidloop:Zone_Temp_0 with p-switch temperature sensor: Temp_0====
root@openbmc:~# busctl set-property xyz.openbmc_project.State.FanCtrl /xyz/openbmc_project/settings/fanctrl/zone0/Zone_Temp_0 xyz.openbmc_project.Object.Enable Enabled b false
root@openbmc:~# busctl introspect xyz.openbmc_project.State.FanCtrl /xyz/openbmc_project/settings/fanctrl/zone0/Zone_Temp_0
NAME                                TYPE      SIGNATURE RESULT/VALUE FLAGS
xyz.openbmc_project.Object.Enable   interface -         -            -
.Enabled                            property  b         false        emits-change writable
```

when Disable the process of the pidloop: Zone_Temp_0,
the requester switches from Zone_Temp_0 to the others,
when you enable the pidloop: Zone_Temp_0, the setpoint
of Zone_Temp_0 will be take into consideration again

Change-Id: I95ae700144f0d16049fff8b309f05ae690a7ef72
Signed-off-by: ykchiu <Chiu.YK@inventec.com>
diff --git a/pid/builder.cpp b/pid/builder.cpp
index d7a60b4..08526fa 100644
--- a/pid/builder.cpp
+++ b/pid/builder.cpp
@@ -44,6 +44,11 @@
     return std::string(objectPath) + std::to_string(zone);
 }
 
+static std::string getPidControlPath(int64_t zone, std::string pidname)
+{
+    return std::string(objectPath) + std::to_string(zone) + "/" + pidname;
+}
+
 std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>>
     buildZones(const std::map<int64_t, conf::PIDConf>& zonePids,
                std::map<int64_t, conf::ZoneConfig>& zoneConfigs,
@@ -109,6 +114,9 @@
                     getThermalType(info.type));
 
                 zone->addThermalPID(std::move(pid));
+                zone->addPidControlProcess(name, modeControlBus,
+                                           getPidControlPath(zoneId, name),
+                                           deferSignals);
             }
             else if (info.type == "stepwise")
             {
@@ -120,6 +128,9 @@
                 auto stepwise = StepwiseController::createStepwiseController(
                     zone.get(), name, inputs, info.stepwiseInfo);
                 zone->addThermalPID(std::move(stepwise));
+                zone->addPidControlProcess(name, modeControlBus,
+                                           getPidControlPath(zoneId, name),
+                                           deferSignals);
             }
 
             std::cerr << "inputs: ";
diff --git a/pid/buildjson.cpp b/pid/buildjson.cpp
index c37a5f6..1122f09 100644
--- a/pid/buildjson.cpp
+++ b/pid/buildjson.cpp
@@ -187,6 +187,12 @@
             auto name = pid["name"];
             auto item = pid.get<conf::ControllerInfo>();
 
+            if (thisZone.find(name) != thisZone.end())
+            {
+                std::cerr << "Warning: zone " << id
+                          << " have the same pid name " << name << std::endl;
+            }
+
             thisZone[name] = item;
         }
 
diff --git a/pid/zone.cpp b/pid/zone.cpp
index c88f41b..b76fdec 100644
--- a/pid/zone.cpp
+++ b/pid/zone.cpp
@@ -103,6 +103,12 @@
 
 void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
 {
+    /* exclude disabled pidloop from _maximumSetPoint calculation*/
+    if (!isPidProcessEnabled(name))
+    {
+        return;
+    }
+
     _SetPoints.push_back(setPoint);
     /*
      * if there are multiple thermal controllers with the same
@@ -129,6 +135,7 @@
 {
     _SetPoints.clear();
     _maximumSetPoint = 0;
+    _maximumSetPointName.clear();
 }
 
 double DbusPidZone::getFailSafePercent(void) const
@@ -264,7 +271,7 @@
     if (minThermalThreshold >= _maximumSetPoint)
     {
         _maximumSetPoint = minThermalThreshold;
-        _maximumSetPointName = "";
+        _maximumSetPointName = "Minimum";
     }
     else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
     {
@@ -461,4 +468,20 @@
     return getFailSafeMode();
 }
 
+void DbusPidZone::addPidControlProcess(std::string name, sdbusplus::bus_t& bus,
+                                       std::string objPath, bool defer)
+{
+    _pidsControlProcess[name] = std::make_unique<ProcessObject>(
+        bus, objPath.c_str(),
+        defer ? ProcessObject::action::defer_emit
+              : ProcessObject::action::emit_object_added);
+    // Default enable setting = true
+    _pidsControlProcess[name]->enabled(true);
+}
+
+bool DbusPidZone::isPidProcessEnabled(std::string name)
+{
+    return _pidsControlProcess[name]->enabled();
+}
+
 } // namespace pid_control
diff --git a/pid/zone.hpp b/pid/zone.hpp
index b985f5f..9604c73 100644
--- a/pid/zone.hpp
+++ b/pid/zone.hpp
@@ -11,6 +11,7 @@
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server.hpp>
 #include <xyz/openbmc_project/Control/Mode/server.hpp>
+#include <xyz/openbmc_project/Object/Enable/server.hpp>
 
 #include <fstream>
 #include <iostream>
@@ -24,6 +25,9 @@
 using ServerObject = typename sdbusplus::server::object_t<T...>;
 using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
 using ModeObject = ServerObject<ModeInterface>;
+using ProcessInterface =
+    sdbusplus::xyz::openbmc_project::Object::server::Enable;
+using ProcessObject = ServerObject<ProcessInterface>;
 
 namespace pid_control
 {
@@ -98,6 +102,10 @@
     bool manual(bool value) override;
     /* Method for reading whether in fail-safe mode over dbus */
     bool failSafe() const override;
+    /* Method for control process for each loop at runtime */
+    void addPidControlProcess(std::string name, sdbusplus::bus_t& bus,
+                              std::string objPath, bool defer);
+    bool isPidProcessEnabled(std::string name);
 
   private:
     template <bool fanSensorLogging>
@@ -196,6 +204,8 @@
 
     std::vector<std::unique_ptr<Controller>> _fans;
     std::vector<std::unique_ptr<Controller>> _thermals;
+
+    std::map<std::string, std::unique_ptr<ProcessObject>> _pidsControlProcess;
 };
 
 } // namespace pid_control