Persistence the led group asserted value by default

Currently, only IBM's P10 machine will persist the Asserted attribute
value of ledGroup by default. But for other systems, most do not need
to perform persistence actions.

The intent behind this commit is to add a switch to control the
persistence function, and it is enabled by default in CI and disabled
by default in Yocto.

Tested: Regardless of whether `persistent-led-asserted` is enabled or
disabled, the ledmanager process works fine.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I75b59a12dde417a9ccadaf25b2af07fc33186a01
diff --git a/manager/group.cpp b/manager/group.cpp
index d728f2b..c9ad161 100644
--- a/manager/group.cpp
+++ b/manager/group.cpp
@@ -39,7 +39,10 @@
     auto result = manager.setGroupState(path, value, ledsAssert, ledsDeAssert);
 
     // Store asserted state
-    serialize.storeGroups(path, result);
+    if (serializePtr)
+    {
+        serializePtr->storeGroups(path, result);
+    }
 
     // If something does not go right here, then there should be an sdbusplus
     // exception thrown.
diff --git a/manager/group.hpp b/manager/group.hpp
index aec0e85..e3717ca 100644
--- a/manager/group.hpp
+++ b/manager/group.hpp
@@ -35,22 +35,22 @@
 
     /** @brief Constructs LED Group
      *
-     * @param[in] bus       - Handle to system dbus
-     * @param[in] objPath   - The D-Bus path that hosts LED group
-     * @param[in] manager   - Reference to Manager
-     * @param[in] serialize - Serialize object
-     * @param[in] callBack  - Custom callback when LED group is asserted
+     * @param[in] bus           - Handle to system dbus
+     * @param[in] objPath       - The D-Bus path that hosts LED group
+     * @param[in] manager       - Reference to Manager
+     * @param[in] serializePtr  - Serialize object
+     * @param[in] callBack      - Custom callback when LED group is asserted
      */
     Group(sdbusplus::bus_t& bus, const std::string& objPath, Manager& manager,
-          Serialize& serialize,
+          std::shared_ptr<Serialize> serializePtr,
           std::function<void(Group*, bool)> callBack = nullptr) :
 
         GroupInherit(bus, objPath.c_str(), GroupInherit::action::defer_emit),
-        path(objPath), manager(manager), serialize(serialize),
+        path(objPath), manager(manager), serializePtr(serializePtr),
         customCallBack(callBack)
     {
         // Initialize Asserted property value
-        if (serialize.getGroupSavedState(objPath))
+        if (serializePtr && serializePtr->getGroupSavedState(objPath))
         {
             asserted(true);
         }
@@ -74,7 +74,7 @@
     Manager& manager;
 
     /** @brief The serialize class for storing and restoring groups of LEDs */
-    Serialize& serialize;
+    std::shared_ptr<Serialize> serializePtr;
 
     /** @brief Custom callback when LED group is asserted
      */
diff --git a/manager/led-main.cpp b/manager/led-main.cpp
index 281cac9..df0f2bb 100644
--- a/manager/led-main.cpp
+++ b/manager/led-main.cpp
@@ -19,6 +19,7 @@
 
 #include <algorithm>
 #include <iostream>
+#include <memory>
 
 int main(int argc, char** argv)
 {
@@ -51,14 +52,18 @@
     /** @brief vector of led groups */
     std::vector<std::unique_ptr<phosphor::led::Group>> groups;
 
+    std::shared_ptr<phosphor::led::Serialize> serializePtr = nullptr;
+#ifdef PERSISTENT_LED_ASSERTED
     /** @brief store and re-store Group */
-    phosphor::led::Serialize serialize(SAVED_GROUPS_FILE);
+    serializePtr =
+        std::make_shared<phosphor::led::Serialize>(SAVED_GROUPS_FILE);
+#endif
 
 #ifdef USE_LAMP_TEST
     phosphor::led::LampTest lampTest(event, manager);
 
     groups.emplace_back(std::make_unique<phosphor::led::Group>(
-        bus, LAMP_TEST_OBJECT, manager, serialize,
+        bus, LAMP_TEST_OBJECT, manager, serializePtr,
         std::bind(std::mem_fn(&phosphor::led::LampTest::requestHandler),
                   &lampTest, std::placeholders::_1, std::placeholders::_2)));
 
@@ -71,9 +76,9 @@
 
     /** Now create so many dbus objects as there are groups */
     std::ranges::transform(systemLedMap, std::back_inserter(groups),
-                           [&bus, &manager, &serialize](auto& grp) {
+                           [&bus, &manager, serializePtr](auto& grp) {
         return std::make_unique<phosphor::led::Group>(bus, grp.first, manager,
-                                                      serialize);
+                                                      serializePtr);
     });
 
     // Attach the bus to sd_event to service user requests
diff --git a/meson.build b/meson.build
index f1a65a8..d4ee899 100644
--- a/meson.build
+++ b/meson.build
@@ -22,6 +22,7 @@
 conf_data.set('LED_USE_JSON', get_option('use-json').enabled())
 conf_data.set('USE_LAMP_TEST', get_option('use-lamp-test').enabled())
 conf_data.set('MONITOR_OPERATIONAL_STATUS', get_option('monitor-operational-status').enabled())
+conf_data.set('PERSISTENT_LED_ASSERTED', get_option('persistent-led-asserted').enabled())
 
 sdbusplus_dep = dependency('sdbusplus')
 sdeventplus_dep = dependency('sdeventplus')
diff --git a/meson.options b/meson.options
index d626a88..0556ed0 100644
--- a/meson.options
+++ b/meson.options
@@ -21,3 +21,9 @@
     value: 'disabled',
     description : 'Enable OperationalStatus monitor'
 )
+
+option(
+    'persistent-led-asserted', type : 'feature',
+    value: 'enabled',
+    description : 'Persistent the asserted status of ledgroup'
+)
diff --git a/test/meson.build b/test/meson.build
index 18c1192..29d4b95 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -21,15 +21,21 @@
 
 test_sources = [
   '../manager/manager.cpp',
-  '../manager/serialize.cpp',
   '../utils.cpp'
 ]
 
 tests = [
   'utest.cpp',
-  'utest-serialize.cpp',
   'utest-led-json.cpp',
 ]
+if get_option('persistent-led-asserted').enabled()
+  test_sources += [
+    '../manager/serialize.cpp',
+  ]
+  tests += [
+    'utest-serialize.cpp',
+  ]
+endif
 
 foreach t : tests
   test(t, executable(t.underscorify(), t,