blob: 98d67966c4bdb38f65096b5d8198537994ab2d35 [file] [log] [blame]
George Liu073a6532021-10-25 14:40:03 +08001#pragma once
2
George Liu5107c452021-11-09 20:06:31 +08003#include "utils.hpp"
4
George Liu073a6532021-10-25 14:40:03 +08005#include <phosphor-logging/lg2.hpp>
George Liu5107c452021-11-09 20:06:31 +08006#include <sdeventplus/event.hpp>
George Liu073a6532021-10-25 14:40:03 +08007
8#include <filesystem>
9
10namespace phosphor
11{
12namespace usb
13{
14namespace fs = std::filesystem;
George Liu5107c452021-11-09 20:06:31 +080015namespace MatchRules = sdbusplus::bus::match::rules;
George Liu073a6532021-10-25 14:40:03 +080016
17class USBManager
18{
19 public:
20 ~USBManager() = default;
21 USBManager() = delete;
22 USBManager(const USBManager&) = delete;
23 USBManager(USBManager&&) = default;
24 USBManager& operator=(const USBManager&) = delete;
Pavithra Barithaya4fd0d0f2024-06-22 00:37:22 -050025 USBManager& operator=(USBManager&&) = delete;
George Liu073a6532021-10-25 14:40:03 +080026
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050027 explicit USBManager(sdbusplus::bus_t& bus, sdeventplus::Event& event,
George Liu6d775e62021-10-26 10:44:30 +080028 const fs::path& devPath, const fs::path& usbPath) :
Patrick Williamsfc33ba82024-08-16 15:19:54 -040029 bus(bus), event(event), devicePath(devPath), usbPath(usbPath),
George Liu6d775e62021-10-26 10:44:30 +080030 isUSBCodeUpdate(false),
George Liu5107c452021-11-09 20:06:31 +080031 fwUpdateMatcher(bus,
32 MatchRules::interfacesAdded() +
33 MatchRules::path("/xyz/openbmc_project/software"),
34 std::bind(std::mem_fn(&USBManager::updateActivation),
35 this, std::placeholders::_1))
36 {
37 if (!run())
38 {
39 lg2::error("Failed to FW Update via USB, usbPath:{USBPATH}",
40 "USBPATH", usbPath);
41 event.exit(0);
42 }
43
44 isUSBCodeUpdate = true;
45 }
George Liu073a6532021-10-25 14:40:03 +080046
47 /** @brief Find the first file with a .tar extension according to the USB
48 * file path.
49 *
50 * @return Success or Fail
51 */
52 bool run();
53
George Liu5107c452021-11-09 20:06:31 +080054 /** @brief Creates an Activation D-Bus object.
55 *
56 * @param[in] msg - Data associated with subscribed signal
57 */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050058 void updateActivation(sdbusplus::message_t& msg);
George Liu5107c452021-11-09 20:06:31 +080059
60 /** @brief Set Apply Time to OnReset.
61 *
62 */
63 void setApplyTime();
64
65 /** @brief Method to set the RequestedActivation D-Bus property.
66 *
67 * @param[in] path - Update the object path of the firmware
68 */
69 void setRequestedActivation(const std::string& path);
70
George Liu073a6532021-10-25 14:40:03 +080071 private:
George Liu5107c452021-11-09 20:06:31 +080072 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050073 sdbusplus::bus_t& bus;
George Liu5107c452021-11-09 20:06:31 +080074
75 /** sd event handler. */
76 sdeventplus::Event& event;
77
George Liu6d775e62021-10-26 10:44:30 +080078 /** The USB device path. */
79 const fs::path& devicePath;
80
81 /** The USB mount path. */
George Liu073a6532021-10-25 14:40:03 +080082 const fs::path& usbPath;
George Liu5107c452021-11-09 20:06:31 +080083
84 /** Indicates whether USB codeupdate is going on. */
85 bool isUSBCodeUpdate;
86
87 /** sdbusplus signal match for new image. */
88 sdbusplus::bus::match_t fwUpdateMatcher;
George Liu073a6532021-10-25 14:40:03 +080089};
90
91} // namespace usb
92} // namespace phosphor