xref: /openbmc/phosphor-bmc-code-mgmt/cpld/cpld_software_manager.cpp (revision bd5081f0b99a27dde8101576524b2c02a4a580b6)
1 #include "cpld_software_manager.hpp"
2 
3 #include "common/include/dbus_helper.hpp"
4 #include "cpld.hpp"
5 
6 #include <phosphor-logging/lg2.hpp>
7 #include <sdbusplus/async.hpp>
8 
9 PHOSPHOR_LOG2_USING;
10 
11 using namespace phosphor::software::cpld;
12 
13 sdbusplus::async::task<bool> CPLDSoftwareManager::initDevice(
14     const std::string& service, const std::string& path, SoftwareConfig& config)
15 {
16     std::string configIface =
17         "xyz.openbmc_project.Configuration." + config.configType;
18 
19     std::optional<uint64_t> busNo = co_await dbusGetRequiredProperty<uint64_t>(
20         ctx, service, path, configIface, "Bus");
21     std::optional<uint64_t> address =
22         co_await dbusGetRequiredProperty<uint64_t>(ctx, service, path,
23                                                    configIface, "Address");
24     std::optional<std::string> chipType =
25         co_await dbusGetRequiredProperty<std::string>(ctx, service, path,
26                                                       configIface, "Type");
27     std::optional<std::string> chipName =
28         co_await dbusGetRequiredProperty<std::string>(ctx, service, path,
29                                                       configIface, "Name");
30 
31     if (!busNo.has_value() || !address.has_value() || !chipType.has_value() ||
32         !chipName.has_value())
33     {
34         error("missing config property");
35         co_return false;
36     }
37 
38     lg2::debug(
39         "CPLD device type: {TYPE} - {NAME} on Bus: {BUS} at Address: {ADDR}",
40         "TYPE", chipType.value(), "NAME", chipName.value(), "BUS",
41         busNo.value(), "ADDR", address.value());
42 
43     auto cpld = std::make_unique<CPLDDevice>(
44         ctx, chipType.value(), chipName.value(), busNo.value(), address.value(),
45         config, this);
46 
47     std::string version = "unknown";
48     if (!(co_await cpld->getVersion(version)))
49     {
50         lg2::error("Failed to get CPLD version for {NAME}", "NAME",
51                    chipName.value());
52     }
53 
54     std::unique_ptr<Software> software = std::make_unique<Software>(ctx, *cpld);
55 
56     software->setVersion(version);
57 
58     std::set<RequestedApplyTimes> allowedApplyTimes = {
59         RequestedApplyTimes::Immediate, RequestedApplyTimes::OnReset};
60 
61     software->enableUpdate(allowedApplyTimes);
62 
63     cpld->softwareCurrent = std::move(software);
64 
65     devices.insert({config.objectPath, std::move(cpld)});
66 
67     co_return true;
68 }
69 
70 void CPLDSoftwareManager::start()
71 {
72     std::vector<std::string> configIntfs;
73     auto configs = CPLDFactory::instance().getConfigs();
74     configIntfs.reserve(configs.size());
75     for (const auto& config : configs)
76     {
77         configIntfs.push_back("xyz.openbmc_project.Configuration." + config);
78     }
79 
80     ctx.spawn(initDevices(configIntfs));
81     ctx.run();
82 }
83 
84 int main()
85 {
86     sdbusplus::async::context ctx;
87 
88     CPLDSoftwareManager cpldSoftwareManager(ctx);
89 
90     cpldSoftwareManager.start();
91 
92     return 0;
93 }
94