1073a6530SGeorge Liu #pragma once
2073a6530SGeorge Liu 
35107c45fSGeorge Liu #include "utils.hpp"
45107c45fSGeorge Liu 
5073a6530SGeorge Liu #include <phosphor-logging/lg2.hpp>
65107c45fSGeorge Liu #include <sdeventplus/event.hpp>
7073a6530SGeorge Liu 
8073a6530SGeorge Liu #include <filesystem>
9073a6530SGeorge Liu 
10073a6530SGeorge Liu namespace phosphor
11073a6530SGeorge Liu {
12073a6530SGeorge Liu namespace usb
13073a6530SGeorge Liu {
14073a6530SGeorge Liu namespace fs = std::filesystem;
155107c45fSGeorge Liu namespace MatchRules = sdbusplus::bus::match::rules;
16073a6530SGeorge Liu 
17073a6530SGeorge Liu class USBManager
18073a6530SGeorge Liu {
19073a6530SGeorge Liu   public:
20073a6530SGeorge Liu     ~USBManager() = default;
21073a6530SGeorge Liu     USBManager() = delete;
22073a6530SGeorge Liu     USBManager(const USBManager&) = delete;
23073a6530SGeorge Liu     USBManager(USBManager&&) = default;
24073a6530SGeorge Liu     USBManager& operator=(const USBManager&) = delete;
25*4fd0d0f0SPavithra Barithaya     USBManager& operator=(USBManager&&) = delete;
26073a6530SGeorge Liu 
27bf2bb2b1SPatrick Williams     explicit USBManager(sdbusplus::bus_t& bus, sdeventplus::Event& event,
286d775e64SGeorge Liu                         const fs::path& devPath, const fs::path& usbPath) :
295107c45fSGeorge Liu         bus(bus),
306d775e64SGeorge Liu         event(event), devicePath(devPath), usbPath(usbPath),
316d775e64SGeorge Liu         isUSBCodeUpdate(false),
325107c45fSGeorge Liu         fwUpdateMatcher(bus,
335107c45fSGeorge Liu                         MatchRules::interfacesAdded() +
345107c45fSGeorge Liu                             MatchRules::path("/xyz/openbmc_project/software"),
355107c45fSGeorge Liu                         std::bind(std::mem_fn(&USBManager::updateActivation),
365107c45fSGeorge Liu                                   this, std::placeholders::_1))
375107c45fSGeorge Liu     {
385107c45fSGeorge Liu         if (!run())
395107c45fSGeorge Liu         {
405107c45fSGeorge Liu             lg2::error("Failed to FW Update via USB, usbPath:{USBPATH}",
415107c45fSGeorge Liu                        "USBPATH", usbPath);
425107c45fSGeorge Liu             event.exit(0);
435107c45fSGeorge Liu         }
445107c45fSGeorge Liu 
455107c45fSGeorge Liu         isUSBCodeUpdate = true;
465107c45fSGeorge Liu     }
47073a6530SGeorge Liu 
48073a6530SGeorge Liu     /** @brief Find the first file with a .tar extension according to the USB
49073a6530SGeorge Liu      *         file path.
50073a6530SGeorge Liu      *
51073a6530SGeorge Liu      *  @return Success or Fail
52073a6530SGeorge Liu      */
53073a6530SGeorge Liu     bool run();
54073a6530SGeorge Liu 
555107c45fSGeorge Liu     /** @brief Creates an Activation D-Bus object.
565107c45fSGeorge Liu      *
575107c45fSGeorge Liu      * @param[in]  msg   - Data associated with subscribed signal
585107c45fSGeorge Liu      */
59bf2bb2b1SPatrick Williams     void updateActivation(sdbusplus::message_t& msg);
605107c45fSGeorge Liu 
615107c45fSGeorge Liu     /** @brief Set Apply Time to OnReset.
625107c45fSGeorge Liu      *
635107c45fSGeorge Liu      */
645107c45fSGeorge Liu     void setApplyTime();
655107c45fSGeorge Liu 
665107c45fSGeorge Liu     /** @brief Method to set the RequestedActivation D-Bus property.
675107c45fSGeorge Liu      *
685107c45fSGeorge Liu      *  @param[in] path  - Update the object path of the firmware
695107c45fSGeorge Liu      */
705107c45fSGeorge Liu     void setRequestedActivation(const std::string& path);
715107c45fSGeorge Liu 
72073a6530SGeorge Liu   private:
735107c45fSGeorge Liu     /** @brief Persistent sdbusplus DBus bus connection. */
74bf2bb2b1SPatrick Williams     sdbusplus::bus_t& bus;
755107c45fSGeorge Liu 
765107c45fSGeorge Liu     /** sd event handler. */
775107c45fSGeorge Liu     sdeventplus::Event& event;
785107c45fSGeorge Liu 
796d775e64SGeorge Liu     /** The USB device path. */
806d775e64SGeorge Liu     const fs::path& devicePath;
816d775e64SGeorge Liu 
826d775e64SGeorge Liu     /** The USB mount path. */
83073a6530SGeorge Liu     const fs::path& usbPath;
845107c45fSGeorge Liu 
855107c45fSGeorge Liu     /** Indicates whether USB codeupdate is going on. */
865107c45fSGeorge Liu     bool isUSBCodeUpdate;
875107c45fSGeorge Liu 
885107c45fSGeorge Liu     /** sdbusplus signal match for new image. */
895107c45fSGeorge Liu     sdbusplus::bus::match_t fwUpdateMatcher;
90073a6530SGeorge Liu };
91073a6530SGeorge Liu 
92073a6530SGeorge Liu } // namespace usb
93073a6530SGeorge Liu } // namespace phosphor
94