blob: 9200bb294321925838c5d194142dc95361756983 [file] [log] [blame]
Alexander Hansenaa300642025-02-04 15:51:52 +01001#include "example_device.hpp"
2
3#include "common/include/device.hpp"
4#include "common/include/software_config.hpp"
5#include "common/include/software_manager.hpp"
6
7#include <phosphor-logging/lg2.hpp>
8#include <sdbusplus/asio/connection.hpp>
9#include <sdbusplus/asio/object_server.hpp>
10#include <sdbusplus/async.hpp>
11#include <sdbusplus/server.hpp>
12#include <xyz/openbmc_project/Association/Definitions/server.hpp>
13#include <xyz/openbmc_project/Software/Update/server.hpp>
14
15#include <memory>
16
17PHOSPHOR_LOG2_USING;
18
19using namespace phosphor::software;
20using namespace phosphor::software::config;
21using namespace phosphor::software::manager;
22using namespace phosphor::software::device;
23using namespace phosphor::software::example_device;
24
25SoftwareConfig ExampleDevice::defaultConfig =
26 SoftwareConfig(exampleInvObjPath, exampleVendorIANA,
27 exampleCompatibleHardware, "Nop", exampleName);
28
29long ExampleCodeUpdater::getRandomId()
30{
31 struct timespec ts;
32 clock_gettime(CLOCK_REALTIME, &ts);
33 unsigned int seed = ts.tv_nsec ^ getpid();
34 srandom(seed);
35 return random() % 10000;
36}
37
38// nop code updater needs unique suffix on dbus for parallel unit testing
39ExampleCodeUpdater::ExampleCodeUpdater(sdbusplus::async::context& ctx,
40 long uniqueSuffix) :
41 SoftwareManager(ctx, "ExampleUpdater" + std::to_string(uniqueSuffix))
42{}
43
Alexander Hansenade5c5a2025-07-29 13:38:24 +020044ExampleCodeUpdater::ExampleCodeUpdater(sdbusplus::async::context& ctx,
45 const char* swVersion) :
46 ExampleCodeUpdater(ctx)
47{
48 const std::string exampleInvObjPath =
49 "/xyz/openbmc_project/inventory/system/board/ExampleBoard/ExampleDevice";
50 auto exampleDevice = std::make_unique<ExampleDevice>(ctx, &(*this));
51
52 devices.insert({exampleInvObjPath, std::move(exampleDevice)});
53
54 if (swVersion)
55 {
56 auto& device = getDevice();
57 device->softwareCurrent =
58 std::make_unique<ExampleSoftware>(ctx, *device);
59 device->softwareCurrent->setVersion(swVersion);
60 }
61}
62
63std::unique_ptr<ExampleDevice>& ExampleCodeUpdater::getDevice()
64{
65 if (devices.empty())
66 {
67 throw std::invalid_argument(
68 "could not find any device, example CU wrongly initialized");
69 }
70
71 auto& deviceRef = devices.begin()->second;
72
73 return reinterpret_cast<std::unique_ptr<ExampleDevice>&>(deviceRef);
74}
75
Alexander Hansenaa300642025-02-04 15:51:52 +010076sdbusplus::async::task<bool> ExampleCodeUpdater::initDevice(
77 const std::string& /*unused*/, const std::string& /*unused*/,
78 SoftwareConfig& /*unused*/)
Alexander Hansenaa300642025-02-04 15:51:52 +010079{
80 auto device = std::make_unique<ExampleDevice>(ctx, this);
81
Alexander Hansenade5c5a2025-07-29 13:38:24 +020082 device->softwareCurrent = std::make_unique<ExampleSoftware>(ctx, *device);
Alexander Hansenaa300642025-02-04 15:51:52 +010083
Daniel Hsu2e168db2025-09-08 14:06:48 +080084 device->softwareCurrent->setVersion("v1.0",
85 SoftwareVersion::VersionPurpose::Other);
Alexander Hansenaa300642025-02-04 15:51:52 +010086 device->softwareCurrent->setActivation(
87 SoftwareActivation::Activations::Active);
88
89 auto applyTimes = {RequestedApplyTimes::OnReset};
90 device->softwareCurrent->enableUpdate(applyTimes);
91
92 devices.insert({exampleInvObjPath, std::move(device)});
93
94 co_return true;
95}
96
97ExampleDevice::ExampleDevice(sdbusplus::async::context& ctx,
98 SoftwareManager* parent,
99 const SoftwareConfig& config) :
100 Device(ctx, config, parent,
101 {RequestedApplyTimes::Immediate, RequestedApplyTimes::OnReset})
102{}
103
Alexander Hansenaa300642025-02-04 15:51:52 +0100104sdbusplus::async::task<bool> ExampleDevice::updateDevice(
105 const uint8_t* /*unused*/, size_t compImageSize)
Alexander Hansenaa300642025-02-04 15:51:52 +0100106{
107 debug("Called device specific update function with image size {SIZE}",
108 "SIZE", compImageSize);
109
110 deviceSpecificUpdateFunctionCalled = true;
111
112 // Setting this property for demonstration purpose.
113 // For a real device, this could represent the
114 // percentage completion of writing the firmware,
115 // and any progress made in the update process within this function.
116 // There is no hard constraint on the values here,
117 // we do not have to reach any specific percentage.
118 // The percentage should be monotonic and increasing.
119 for (auto progress = 0; progress <= 100; progress += 20)
120 {
Alexander Hansen85aed222025-02-18 11:23:47 +0100121 setUpdateProgress(progress);
Alexander Hansenaa300642025-02-04 15:51:52 +0100122 }
123
124 co_return true;
125}
Alexander Hansenade5c5a2025-07-29 13:38:24 +0200126
127ExampleSoftware::ExampleSoftware(sdbusplus::async::context& ctx,
128 ExampleDevice& parent) : Software(ctx, parent)
129{}