pldm: Add sensor scan retry in rde_fan_sensor

When all minibmc reboot, rded tries to do the minibmc discovery
and retrieve all the dictionaries from the minibmc which keeps rded
very busy and not able to serve any Redfish request during this
period (the requests timeout). So in this period when rde_fan_sensor
tries to send queries, they fail causing rde_fan_sensor to give up
and not scan sensors on the minibmc.

This change adds a way to retry the sensor scan. Additionally,
with this fix the 180 seconds sleep to during the rde_fan_sensor
startup (which was a workaround to avoid the same problem at bootup)
is also not needed anymore.

Fusion-Link: https://fusion2.corp.google.com/592c26eb-b013-35f8-8af9-7ffd77131c9b
Tested: Verified in the rde_fan_sensor logs, that sensor rescan happens
        when the issue occurs and that rde_fan_sensor proceed to
	function as expected.
Google-Bug-Id: 352833159
Change-Id: I641abbac62238820bace1f3ee14f94e4bedb7fad
Signed-off-by: Nikhil Namjoshi <nikhilnamjoshi@google.com>
diff --git a/recipes-phosphor/pldm/pldm/0009-Add-rde_fan_sensor-daemon.patch b/recipes-phosphor/pldm/pldm/0009-Add-rde_fan_sensor-daemon.patch
index f9588c1..b5ba71c 100644
--- a/recipes-phosphor/pldm/pldm/0009-Add-rde_fan_sensor-daemon.patch
+++ b/recipes-phosphor/pldm/pldm/0009-Add-rde_fan_sensor-daemon.patch
@@ -1,7 +1,7 @@
-From 62b0708317fa72781f01d6f39ce68b36127d6f72 Mon Sep 17 00:00:00 2001
+From f98a5868ccad38dbdb95f0f231ecedab38e8742b Mon Sep 17 00:00:00 2001
 From: Nikhil Namjoshi <nikhilnamjoshi@google.com>
 Date: Fri, 7 Jul 2023 22:43:07 +0000
-Subject: [PATCH] Add rde_fan_sensor daemon
+Subject: [PATCH 1/5] Add rde_fan_sensor daemon
 
 Add the source code for the rde_fan_sensor daemon.
 The intention of this daemon is to communicate the
@@ -33,8 +33,8 @@
 Signed-off-by: Nikhil Namjoshi <nikhilnamjoshi@google.com>
 ---
  meson.build                                   |   1 +
- rde_fan_sensor/RdeFanSensorMain.cpp           | 470 ++++++++++++++++++
- rde_fan_sensor/RdePwmSensor.cpp               | 255 ++++++++++
+ rde_fan_sensor/RdeFanSensorMain.cpp           | 488 ++++++++++++++++++
+ rde_fan_sensor/RdePwmSensor.cpp               | 255 +++++++++
  rde_fan_sensor/RdePwmSensor.hpp               |  60 +++
  rde_fan_sensor/RdeTachSensor.cpp              |  91 ++++
  rde_fan_sensor/RdeTachSensor.hpp              |  44 ++
@@ -42,9 +42,9 @@
  rde_fan_sensor/RdeUtils.hpp                   | 219 ++++++++
  rde_fan_sensor/meson.build                    |  59 +++
  .../service_files/rde_fan_sensor.service      |  16 +
- test/meson.build                              |   5 +
+ test/meson.build                              |   4 +
  test/rde_utils_test.cpp                       | 230 +++++++++
- 12 files changed, 1569 insertions(+)
+ 12 files changed, 1586 insertions(+)
  create mode 100644 rde_fan_sensor/RdeFanSensorMain.cpp
  create mode 100644 rde_fan_sensor/RdePwmSensor.cpp
  create mode 100644 rde_fan_sensor/RdePwmSensor.hpp
@@ -57,10 +57,10 @@
  create mode 100644 test/rde_utils_test.cpp
 
 diff --git a/meson.build b/meson.build
-index c4e689e..1f054c2 100644
+index 9d01270..9e723a6 100644
 --- a/meson.build
 +++ b/meson.build
-@@ -209,6 +209,7 @@ endif
+@@ -207,6 +207,7 @@ endif
  
  subdir('pldmtool')
  subdir('rded')
@@ -70,10 +70,10 @@
  if get_option('utilities').enabled()
 diff --git a/rde_fan_sensor/RdeFanSensorMain.cpp b/rde_fan_sensor/RdeFanSensorMain.cpp
 new file mode 100644
-index 0000000..c9ec4a0
+index 0000000..cd3fad5
 --- /dev/null
 +++ b/rde_fan_sensor/RdeFanSensorMain.cpp
-@@ -0,0 +1,470 @@
+@@ -0,0 +1,488 @@
 +/**
 + * Copyright 2023 Google Inc.
 + *
@@ -312,8 +312,18 @@
 +
 +                if (isResponseError(trayResp))
 +                {
-+                    std::cerr << "Unable to get tray collection" << std::endl;
-+                    continue;
++                    std::cerr << "Unable to get tray collection for " << rdeDeviceId
++                              << "." << std::endl;
++                    std::cerr << "Rde device is probably busy with rded "
++                              << "daemon discovery requests. Retrying."
++                              << std::endl;
++
++                    boost::asio::post(io, [&]() {
++                        createSensors(trayCountMap, rdeIdToTrayIdMap, io,
++                                      objectServer, pwmSensors, tachSensors,
++                                     asioConnection, dbusConnection, rdeDevicesChanged);
++                    });
++                    return;
 +                }
 +
 +                nlohmann::json trayRespJson;
@@ -346,7 +356,18 @@
 +                    PLDM_RDE_OPERATION_READ, std::string(""));
 +                if (isResponseError(sensorCollectionResp))
 +                {
-+                    std::cerr << "Unable to get sensor collection" << std::endl;
++                    std::cerr << "Unable to get sensor collection for "
++                              << rdeDeviceId << "." << std::endl;
++                    std::cerr
++                        << "Rde device is probably busy with rde daemon "
++                        << "discovery requests. Retrying."
++                        << std::endl;
++
++                    boost::asio::post(io, [&]() {
++                        createSensors(trayCountMap, rdeIdToTrayIdMap, io,
++                                      objectServer, pwmSensors, tachSensors,
++                                      asioConnection, dbusConnection, rdeDevicesChanged);
++                    });
 +                    return;
 +                }
 +                nlohmann::json sensorCollectionRespJson;
@@ -415,9 +436,6 @@
 +
 +int main()
 +{
-+    // TODO Nikhil : Remove this later once b/283151098 is addressed
-+    // We are allowing rded daemon to be ready after initializing miniBMCs
-+    sleep(180);
 +    boost::asio::io_context io;
 +
 +    auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
@@ -1456,7 +1474,7 @@
 +[Install]
 +WantedBy=multi-user.target
 diff --git a/test/meson.build b/test/meson.build
-index 1ae20c8..ace555e 100644
+index 1ae20c8..b1016a0 100644
 --- a/test/meson.build
 +++ b/test/meson.build
 @@ -4,6 +4,7 @@ test_src = declare_dependency(
@@ -1467,11 +1485,9 @@
  ]
  
  foreach t : tests
-@@ -13,8 +14,12 @@ foreach t : tests
-                      build_rpath: get_option('oe-sdk').enabled() ? rpath : '',
+@@ -14,7 +15,10 @@ foreach t : tests
                       dependencies: [
                           libpldm_dep,
-+
                           nlohmann_json,
 +                         sdbusplus,
 +                         boost,
@@ -1717,5 +1733,5 @@
 +    }
 +}
 -- 
-2.42.0.rc2.253.gd59a3bf2b4-goog
+2.46.0.792.g87dc391469-goog
 
diff --git a/recipes-phosphor/pldm/pldm/0010-Add-generic-patching-support-in-rded.patch b/recipes-phosphor/pldm/pldm/0010-Add-generic-patching-support-in-rded.patch
index b5ac502..e041d3f 100644
--- a/recipes-phosphor/pldm/pldm/0010-Add-generic-patching-support-in-rded.patch
+++ b/recipes-phosphor/pldm/pldm/0010-Add-generic-patching-support-in-rded.patch
@@ -1,7 +1,7 @@
-From bd6c746b67389685560f0b0ac70181abd2e8dd93 Mon Sep 17 00:00:00 2001
+From 665012d8d8e3e46df165426eec2f442a1a4f7874 Mon Sep 17 00:00:00 2001
 From: Nikhil Namjoshi <nikhilnamjoshi@google.com>
 Date: Fri, 10 Nov 2023 19:09:53 +0000
-Subject: [PATCH] Add generic patching support in rded
+Subject: [PATCH 2/5] Add generic patching support in rded
 
 Design Doc : go/minibmc-redfish-patch-support
 
@@ -29,20 +29,22 @@
 Signed-off-by: Nikhil Namjoshi <nikhilnamjoshi@google.com>
 
 %% original patch: 0010-Add-generic-patching-support-in-rded.patch
+
+%% original patch: 0010-Add-generic-patching-support-in-rded.patch
 ---
  rded/helper/rde_operation/rde_operation.cpp   |  57 +-
  .../helper/rde_operation/rde_update_utils.cpp | 341 +++++++++++
  .../helper/rde_operation/rde_update_utils.hpp |  35 ++
  rded/meson.build                              |  11 +
- test/meson.build                              |   6 +-
+ test/meson.build                              |   5 +-
  test/rde_update_utils_test.cpp                | 579 ++++++++++++++++++
- 6 files changed, 971 insertions(+), 58 deletions(-)
+ 6 files changed, 971 insertions(+), 57 deletions(-)
  create mode 100644 rded/helper/rde_operation/rde_update_utils.cpp
  create mode 100644 rded/helper/rde_operation/rde_update_utils.hpp
  create mode 100644 test/rde_update_utils_test.cpp
 
 diff --git a/rded/helper/rde_operation/rde_operation.cpp b/rded/helper/rde_operation/rde_operation.cpp
-index 5c6c16f..b1d9395 100644
+index ab36eeb..03155e2 100644
 --- a/rded/helper/rde_operation/rde_operation.cpp
 +++ b/rded/helper/rde_operation/rde_operation.cpp
 @@ -11,6 +11,7 @@
@@ -534,7 +536,7 @@
    'rded',
    sources,
 diff --git a/test/meson.build b/test/meson.build
-index ace555e..bb27327 100644
+index b1016a0..bb27327 100644
 --- a/test/meson.build
 +++ b/test/meson.build
 @@ -4,7 +4,8 @@ test_src = declare_dependency(
@@ -547,14 +549,13 @@
  ]
  
  foreach t : tests
-@@ -12,9 +13,10 @@ foreach t : tests
+@@ -12,8 +13,10 @@ foreach t : tests
                       implicit_include_directories: false,
                       link_args: dynamic_linker,
                       build_rpath: get_option('oe-sdk').enabled() ? rpath : '',
 +                     link_with: librdedupdatelib,
                       dependencies: [
                           libpldm_dep,
--
 +                         libbej_dep,
                           nlohmann_json,
                           sdbusplus,
@@ -1145,5 +1146,5 @@
 +    EXPECT_EQ(decodedPayloadJson.find("ControlMode").value(), nullptr);
 +}
 -- 
-2.43.0.rc1.413.gea7ed67945-goog
+2.46.0.792.g87dc391469-goog