fw-update: cpld code updater

This commit introduces a code updater for CPLD.
The Key features of this updater include:
- configuring chip vendor and name
- update CPLD through I2C

1. Display the fw inventory
curl ... -X GET
https://localhost/redfish/v1/UpdateService/FirmwareInventory
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory",
  "@odata.type":
    "#SoftwareInventoryCollection.SoftwareInventoryCollection",
  "Members": [
    ...
    {
      "@odata.id": ".../LCMXO3LF_4300C_6194"
    },
    ...
  ],
  "Members@odata.count": 26,
  "Name": "Software Inventory Collection"
}

2. Query CPLD version
curl ... -X GET
https://localhost/redfish/v1
  /UpdateService/FirmwareInventory/LCMXO3LF_4300C_6194
{
  "@odata.id": "/.../LCMXO3LF_4300C_6194",
  "@odata.type":
    "#SoftwareInventory.v1_1_0.SoftwareInventory",
  "Description": "Unknown image",
  "Id": "LCMXO3LF_4300C_6194",
  "Name": "Software Inventory",
  "Status": {
    "Health": "Warning",
    "HealthRollup": "OK",
    "State": "Disabled"
  },
  "Updateable": false,
  "Version": "00000220"
}

3. Trigger the fw update via redfish
curl -k
-H "X-Auth-Token: $token"
-H "Content-Type:multipart/form-data"
-X POST
-F UpdateParameters="{\"Targets\":
  [\"/redfish/v1/UpdateService/FirmwareInventory/LCMXO3LF_4300C_6194\"],
  \"@Redfish.OperationApplyTime\":\"Immediate\"};type=application/json"
-F "UpdateFile=@testcpld.pldm;type=application/octet-stream"
https://${bmc}/redfish/v1/UpdateService/update
{
  "@odata.id": "/redfish/v1/TaskService/Tasks/0",
  "@odata.type": "#Task.v1_4_3.Task",
  "Id": "0",
  "TaskState": "Running",
  "TaskStatus": "OK"
}

4. Query Task status
curl ... -X GET
https://localhost/redfish/v1/TaskService/Tasks/0
{
  "@odata.id": "/redfish/v1/TaskService/Tasks/0",
  "@odata.type": "#Task.v1_4_3.Task",
  "EndTime": "2025-05-28T08:10:22+00:00",
  "HidePayload": false,
  "Id": "0",
  "Messages": [
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "The task with Id '0' has started.",
      "MessageArgs": [
        "0"
      ],
      "MessageId": "TaskEvent.1.0.TaskStarted",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 15 percent complete.",
      "MessageArgs": [
        "0",
        "15"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 20 percent complete.",
      "MessageArgs": [
        "0",
        "20"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 25 percent complete.",
      "MessageArgs": [
        "0",
        "25"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 30 percent complete.",
      "MessageArgs": [
        "0",
        "30"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 40 percent complete.",
      "MessageArgs": [
        "0",
        "40"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 50 percent complete.",
      "MessageArgs": [
        "0",
        "50"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 60 percent complete.",
      "MessageArgs": [
        "0",
        "60"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 70 percent complete.",
      "MessageArgs": [
        "0",
        "70"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 80 percent complete.",
      "MessageArgs": [
        "0",
        "80"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 90 percent complete.",
      "MessageArgs": [
        "0",
        "90"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "... progress 100 percent complete.",
      "MessageArgs": [
        "0",
        "100"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "The task with Id '0' has completed.",
      "MessageArgs": [
        "0"
      ],
      "MessageId": "TaskEvent.1.0.TaskCompletedOK",
      "MessageSeverity": "OK",
      "Resolution": "None."
    }
  ],
  "Name": "Task 0",
  "Payload": {
    "HttpHeaders": [],
    "HttpOperation": "POST",
    "JsonBody": "null",
    "TargetUri": "/redfish/v1/UpdateService/update"
  },
  "PercentComplete": 100,
  "StartTime": "2025-05-28T08:10:06+00:00",
  "TaskMonitor": "/redfish/v1/TaskService/TaskMonitors/0",
  "TaskState": "Completed",
  "TaskStatus": "OK"
}

5. Display the fw inventory with newly updated fw.
curl ... -X GET
https://localhost/redfish/v1/UpdateService/FirmwareInventory
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory",
  "@odata.type":
    "#SoftwareInventoryCollection.SoftwareInventoryCollection",
  "Members": [
    ...
    {
      "@odata.id": ".../LCMXO3LF_4300C_1571"
    },
    ...
  ],
  "Members@odata.count": 26,
  "Name": "Software Inventory Collection"
}

6. Query CPLD version again
After AC cycle ..., so the number would be different.
curl ... -X GET
https://localhost/redfish/v1
  /UpdateService/FirmwareInventory/LCMXO3LF_4300C_4643
{
  "@odata.id": "/.../LCMXO3LF_4300C_4643",
  "@odata.type": "#SoftwareInventory.v1_1_0.SoftwareInventory",
  "Description": "Unknown image",
  "Id": "LCMXO3LF_4300C_4643",
  "Name": "Software Inventory",
  "Status": {
    "Health": "Warning",
    "HealthRollup": "OK",
    "State": "Disabled"
  },
  "Updateable": false,
  "Version": "00000224"
}

Change-Id: Ife8e30a00bfbb307e6e4eb55838a397c8a8162bd
Signed-off-by: Daniel Hsu <Daniel-Hsu@quantatw.com>
diff --git a/cpld/cpld_software_manager.cpp b/cpld/cpld_software_manager.cpp
new file mode 100644
index 0000000..faf14a5
--- /dev/null
+++ b/cpld/cpld_software_manager.cpp
@@ -0,0 +1,93 @@
+#include "cpld_software_manager.hpp"
+
+#include "common/include/dbus_helper.hpp"
+#include "cpld.hpp"
+
+#include <phosphor-logging/lg2.hpp>
+#include <sdbusplus/async.hpp>
+
+PHOSPHOR_LOG2_USING;
+
+using namespace phosphor::software::cpld;
+
+sdbusplus::async::task<bool> CPLDSoftwareManager::initDevice(
+    const std::string& service, const std::string& path, SoftwareConfig& config)
+{
+    std::string configIface =
+        "xyz.openbmc_project.Configuration." + config.configType;
+
+    std::optional<uint64_t> busNo = co_await dbusGetRequiredProperty<uint64_t>(
+        ctx, service, path, configIface, "Bus");
+    std::optional<uint64_t> address =
+        co_await dbusGetRequiredProperty<uint64_t>(ctx, service, path,
+                                                   configIface, "Address");
+    std::optional<std::string> chipType =
+        co_await dbusGetRequiredProperty<std::string>(ctx, service, path,
+                                                      configIface, "Type");
+    std::optional<std::string> chipName =
+        co_await dbusGetRequiredProperty<std::string>(ctx, service, path,
+                                                      configIface, "Name");
+
+    if (!busNo.has_value() || !address.has_value() || !chipType.has_value() ||
+        !chipName.has_value())
+    {
+        error("missing config property");
+        co_return false;
+    }
+
+    lg2::debug(
+        "CPLD device type: {TYPE} - {NAME} on Bus: {BUS} at Address: {ADDR}",
+        "TYPE", chipType.value(), "NAME", chipName.value(), "BUS",
+        busNo.value(), "ADDR", address.value());
+
+    auto cpld = std::make_unique<CPLDDevice>(
+        ctx, chipType.value(), chipName.value(), busNo.value(), address.value(),
+        config, this);
+
+    std::string version = "unknown";
+    if (!(co_await cpld->getVersion(version)))
+    {
+        lg2::error("Failed to get CPLD version for {NAME}", "NAME",
+                   chipName.value());
+    }
+
+    std::unique_ptr<Software> software = std::make_unique<Software>(ctx, *cpld);
+
+    software->setVersion(version);
+
+    std::set<RequestedApplyTimes> allowedApplyTimes = {
+        RequestedApplyTimes::Immediate, RequestedApplyTimes::OnReset};
+
+    software->enableUpdate(allowedApplyTimes);
+
+    cpld->softwareCurrent = std::move(software);
+
+    devices.insert({config.objectPath, std::move(cpld)});
+
+    co_return true;
+}
+
+void CPLDSoftwareManager::start()
+{
+    std::vector<std::string> configIntfs;
+    auto configs = CPLDFactory::instance().getConfigs();
+    configIntfs.reserve(configs.size());
+    for (const auto& config : configs)
+    {
+        configIntfs.push_back("xyz.openbmc_project.Configuration." + config);
+    }
+
+    ctx.spawn(initDevices(configIntfs));
+    ctx.run();
+}
+
+int main()
+{
+    sdbusplus::async::context ctx;
+
+    CPLDSoftwareManager cpldSoftwareManager(ctx);
+
+    cpldSoftwareManager.start();
+
+    return 0;
+}