blob: 78e54aa4c1aaecf657acd037910e0fa9f84edbd2 [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;
25 USBManager& operator=(USBManager&&) = default;
26
George Liu5107c452021-11-09 20:06:31 +080027 explicit USBManager(sdbusplus::bus::bus& bus, sdeventplus::Event& event,
George Liu6d775e62021-10-26 10:44:30 +080028 const fs::path& devPath, const fs::path& usbPath) :
George Liu5107c452021-11-09 20:06:31 +080029 bus(bus),
George Liu6d775e62021-10-26 10:44:30 +080030 event(event), devicePath(devPath), usbPath(usbPath),
31 isUSBCodeUpdate(false),
George Liu5107c452021-11-09 20:06:31 +080032 fwUpdateMatcher(bus,
33 MatchRules::interfacesAdded() +
34 MatchRules::path("/xyz/openbmc_project/software"),
35 std::bind(std::mem_fn(&USBManager::updateActivation),
36 this, std::placeholders::_1))
37 {
38 if (!run())
39 {
40 lg2::error("Failed to FW Update via USB, usbPath:{USBPATH}",
41 "USBPATH", usbPath);
42 event.exit(0);
43 }
44
45 isUSBCodeUpdate = true;
46 }
George Liu073a6532021-10-25 14:40:03 +080047
48 /** @brief Find the first file with a .tar extension according to the USB
49 * file path.
50 *
51 * @return Success or Fail
52 */
53 bool run();
54
George Liu5107c452021-11-09 20:06:31 +080055 /** @brief Creates an Activation D-Bus object.
56 *
57 * @param[in] msg - Data associated with subscribed signal
58 */
59 void updateActivation(sdbusplus::message::message& msg);
60
61 /** @brief Set Apply Time to OnReset.
62 *
63 */
64 void setApplyTime();
65
66 /** @brief Method to set the RequestedActivation D-Bus property.
67 *
68 * @param[in] path - Update the object path of the firmware
69 */
70 void setRequestedActivation(const std::string& path);
71
George Liu073a6532021-10-25 14:40:03 +080072 private:
George Liu5107c452021-11-09 20:06:31 +080073 /** @brief Persistent sdbusplus DBus bus connection. */
74 sdbusplus::bus::bus& bus;
75
76 /** sd event handler. */
77 sdeventplus::Event& event;
78
George Liu6d775e62021-10-26 10:44:30 +080079 /** The USB device path. */
80 const fs::path& devicePath;
81
82 /** The USB mount path. */
George Liu073a6532021-10-25 14:40:03 +080083 const fs::path& usbPath;
George Liu5107c452021-11-09 20:06:31 +080084
85 /** Indicates whether USB codeupdate is going on. */
86 bool isUSBCodeUpdate;
87
88 /** sdbusplus signal match for new image. */
89 sdbusplus::bus::match_t fwUpdateMatcher;
George Liu073a6532021-10-25 14:40:03 +080090};
91
92} // namespace usb
93} // namespace phosphor