George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame^] | 3 | #include "utils.hpp" |
| 4 | |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame^] | 6 | #include <sdeventplus/event.hpp> |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 7 | |
| 8 | #include <filesystem> |
| 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace usb |
| 13 | { |
| 14 | namespace fs = std::filesystem; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame^] | 15 | namespace MatchRules = sdbusplus::bus::match::rules; |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 16 | |
| 17 | class 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 Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame^] | 27 | explicit USBManager(sdbusplus::bus::bus& bus, sdeventplus::Event& event, |
| 28 | const fs::path& path) : |
| 29 | bus(bus), |
| 30 | event(event), usbPath(path), isUSBCodeUpdate(false), |
| 31 | 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 Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 46 | |
| 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 Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame^] | 54 | /** @brief Creates an Activation D-Bus object. |
| 55 | * |
| 56 | * @param[in] msg - Data associated with subscribed signal |
| 57 | */ |
| 58 | void updateActivation(sdbusplus::message::message& msg); |
| 59 | |
| 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 Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 71 | private: |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame^] | 72 | /** @brief Persistent sdbusplus DBus bus connection. */ |
| 73 | sdbusplus::bus::bus& bus; |
| 74 | |
| 75 | /** sd event handler. */ |
| 76 | sdeventplus::Event& event; |
| 77 | |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 78 | /** The USB path detected. */ |
| 79 | const fs::path& usbPath; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame^] | 80 | |
| 81 | /** Indicates whether USB codeupdate is going on. */ |
| 82 | bool isUSBCodeUpdate; |
| 83 | |
| 84 | /** sdbusplus signal match for new image. */ |
| 85 | sdbusplus::bus::match_t fwUpdateMatcher; |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace usb |
| 89 | } // namespace phosphor |