ipmbsensor: resend init command every boot

This stops the ipmi response from being na after
ipmitool power cycle.

Tested-by: Power cycled 5 times and sensors never
went to na

Change-Id: Ida6ac397805b9eb47025e0227e2d15fcc26bb045
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/IpmbSensor.hpp b/include/IpmbSensor.hpp
index b6bfa32..1775c13 100644
--- a/include/IpmbSensor.hpp
+++ b/include/IpmbSensor.hpp
@@ -29,6 +29,7 @@
     void read(void);
     void init(void);
     void loadDefaults(void);
+    void runInitCmd(void);
 
     IpmbType type;
     uint8_t commandAddress;
diff --git a/src/IpmbSensor.cpp b/src/IpmbSensor.cpp
index af1f725..e72f1af 100644
--- a/src/IpmbSensor.cpp
+++ b/src/IpmbSensor.cpp
@@ -45,6 +45,8 @@
 using IpmbMethodType =
     std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>;
 
+boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>> sensors;
+
 IpmbSensor::IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
                        boost::asio::io_service& io,
                        const std::string& sensorName,
@@ -92,6 +94,15 @@
     loadDefaults();
     if (initCommand)
     {
+        runInitCmd();
+    }
+    read();
+}
+
+void IpmbSensor::runInitCmd()
+{
+    if (initCommand)
+    {
         dbusConnection->async_method_call(
             [this](boost::system::error_code ec,
                    const IpmbMethodType& response) {
@@ -109,10 +120,6 @@
             "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
             "sendRequest", commandAddress, netfn, lun, *initCommand, initData);
     }
-    else
-    {
-        read();
-    }
 }
 
 void IpmbSensor::loadDefaults()
@@ -349,6 +356,29 @@
         "GetManagedObjects");
 }
 
+void reinitSensors(sdbusplus::message::message& message)
+{
+
+    std::string objectName;
+    boost::container::flat_map<std::string, std::variant<int32_t>> values;
+    message.read(objectName, values);
+    auto findPgood = values.find("pgood");
+    if (findPgood != values.end())
+    {
+        int32_t powerStatus = std::get<int32_t>(findPgood->second);
+        if (powerStatus)
+        {
+            for (auto& sensor : sensors)
+            {
+                if (sensor.second)
+                {
+                    sensor.second->runInitCmd();
+                }
+            }
+        }
+    }
+}
+
 int main(int argc, char** argv)
 {
 
@@ -356,8 +386,6 @@
     auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
     systemBus->request_name("xyz.openbmc_project.IpmbSensor");
     sdbusplus::asio::object_server objectServer(systemBus);
-    boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>>
-        sensors;
 
     io.post([&]() { createSensors(io, objectServer, sensors, systemBus); });
 
@@ -380,12 +408,19 @@
             });
         };
 
-    sdbusplus::bus::match::match match(
+    sdbusplus::bus::match::match configMatch(
         static_cast<sdbusplus::bus::bus&>(*systemBus),
         "type='signal',member='PropertiesChanged',path_namespace='" +
             std::string(inventoryPath) + "',arg0namespace='" + configInterface +
             "'",
         eventHandler);
 
+    sdbusplus::bus::match::match powerChangeMatch(
+        static_cast<sdbusplus::bus::bus&>(*systemBus),
+        "type='signal',interface='org.freedesktop.DBus.Properties',path_"
+        "namespace='/xyz/openbmc_project/Chassis/Control/"
+        "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
+        reinitSensors);
+
     io.run();
 }