blob: 86606fb89c0d384453b5233eb125c10c20deb6ce [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 Hansenaa300642025-02-04 15:51:52 +010044sdbusplus::async::task<bool> ExampleCodeUpdater::initDevice(
45 const std::string& /*unused*/, const std::string& /*unused*/,
46 SoftwareConfig& /*unused*/)
Alexander Hansenaa300642025-02-04 15:51:52 +010047{
48 auto device = std::make_unique<ExampleDevice>(ctx, this);
49
50 device->softwareCurrent = std::make_unique<Software>(ctx, *device);
51
Daniel Hsu2e168db2025-09-08 14:06:48 +080052 device->softwareCurrent->setVersion("v1.0",
53 SoftwareVersion::VersionPurpose::Other);
Alexander Hansenaa300642025-02-04 15:51:52 +010054 device->softwareCurrent->setActivation(
55 SoftwareActivation::Activations::Active);
56
57 auto applyTimes = {RequestedApplyTimes::OnReset};
58 device->softwareCurrent->enableUpdate(applyTimes);
59
60 devices.insert({exampleInvObjPath, std::move(device)});
61
62 co_return true;
63}
64
65ExampleDevice::ExampleDevice(sdbusplus::async::context& ctx,
66 SoftwareManager* parent,
67 const SoftwareConfig& config) :
68 Device(ctx, config, parent,
69 {RequestedApplyTimes::Immediate, RequestedApplyTimes::OnReset})
70{}
71
Alexander Hansenaa300642025-02-04 15:51:52 +010072sdbusplus::async::task<bool> ExampleDevice::updateDevice(
73 const uint8_t* /*unused*/, size_t compImageSize)
Alexander Hansenaa300642025-02-04 15:51:52 +010074{
75 debug("Called device specific update function with image size {SIZE}",
76 "SIZE", compImageSize);
77
78 deviceSpecificUpdateFunctionCalled = true;
79
80 // Setting this property for demonstration purpose.
81 // For a real device, this could represent the
82 // percentage completion of writing the firmware,
83 // and any progress made in the update process within this function.
84 // There is no hard constraint on the values here,
85 // we do not have to reach any specific percentage.
86 // The percentage should be monotonic and increasing.
87 for (auto progress = 0; progress <= 100; progress += 20)
88 {
Alexander Hansen85aed222025-02-18 11:23:47 +010089 setUpdateProgress(progress);
Alexander Hansenaa300642025-02-04 15:51:52 +010090 }
91
92 co_return true;
93}