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;
254fd0d0f0SPavithra Barithaya     USBManager& operator=(USBManager&&) = delete;
26073a6530SGeorge Liu 
USBManager(sdbusplus::bus_t & bus,sdeventplus::Event & event,const fs::path & devPath,const fs::path & usbPath)27bf2bb2b1SPatrick Williams     explicit USBManager(sdbusplus::bus_t& bus, sdeventplus::Event& event,
286d775e64SGeorge Liu                         const fs::path& devPath, const fs::path& usbPath) :
29*fc33ba86SPatrick Williams         bus(bus), event(event), devicePath(devPath), usbPath(usbPath),
306d775e64SGeorge Liu         isUSBCodeUpdate(false),
315107c45fSGeorge Liu         fwUpdateMatcher(bus,
325107c45fSGeorge Liu                         MatchRules::interfacesAdded() +
335107c45fSGeorge Liu                             MatchRules::path("/xyz/openbmc_project/software"),
345107c45fSGeorge Liu                         std::bind(std::mem_fn(&USBManager::updateActivation),
355107c45fSGeorge Liu                                   this, std::placeholders::_1))
365107c45fSGeorge Liu     {
375107c45fSGeorge Liu         if (!run())
385107c45fSGeorge Liu         {
395107c45fSGeorge Liu             lg2::error("Failed to FW Update via USB, usbPath:{USBPATH}",
405107c45fSGeorge Liu                        "USBPATH", usbPath);
415107c45fSGeorge Liu             event.exit(0);
425107c45fSGeorge Liu         }
435107c45fSGeorge Liu 
445107c45fSGeorge Liu         isUSBCodeUpdate = true;
455107c45fSGeorge Liu     }
46073a6530SGeorge Liu 
47073a6530SGeorge Liu     /** @brief Find the first file with a .tar extension according to the USB
48073a6530SGeorge Liu      *         file path.
49073a6530SGeorge Liu      *
50073a6530SGeorge Liu      *  @return Success or Fail
51073a6530SGeorge Liu      */
52073a6530SGeorge Liu     bool run();
53073a6530SGeorge Liu 
545107c45fSGeorge Liu     /** @brief Creates an Activation D-Bus object.
555107c45fSGeorge Liu      *
565107c45fSGeorge Liu      * @param[in]  msg   - Data associated with subscribed signal
575107c45fSGeorge Liu      */
58bf2bb2b1SPatrick Williams     void updateActivation(sdbusplus::message_t& msg);
595107c45fSGeorge Liu 
605107c45fSGeorge Liu     /** @brief Set Apply Time to OnReset.
615107c45fSGeorge Liu      *
625107c45fSGeorge Liu      */
635107c45fSGeorge Liu     void setApplyTime();
645107c45fSGeorge Liu 
655107c45fSGeorge Liu     /** @brief Method to set the RequestedActivation D-Bus property.
665107c45fSGeorge Liu      *
675107c45fSGeorge Liu      *  @param[in] path  - Update the object path of the firmware
685107c45fSGeorge Liu      */
695107c45fSGeorge Liu     void setRequestedActivation(const std::string& path);
705107c45fSGeorge Liu 
71073a6530SGeorge Liu   private:
725107c45fSGeorge Liu     /** @brief Persistent sdbusplus DBus bus connection. */
73bf2bb2b1SPatrick Williams     sdbusplus::bus_t& bus;
745107c45fSGeorge Liu 
755107c45fSGeorge Liu     /** sd event handler. */
765107c45fSGeorge Liu     sdeventplus::Event& event;
775107c45fSGeorge Liu 
786d775e64SGeorge Liu     /** The USB device path. */
796d775e64SGeorge Liu     const fs::path& devicePath;
806d775e64SGeorge Liu 
816d775e64SGeorge Liu     /** The USB mount path. */
82073a6530SGeorge Liu     const fs::path& usbPath;
835107c45fSGeorge Liu 
845107c45fSGeorge Liu     /** Indicates whether USB codeupdate is going on. */
855107c45fSGeorge Liu     bool isUSBCodeUpdate;
865107c45fSGeorge Liu 
875107c45fSGeorge Liu     /** sdbusplus signal match for new image. */
885107c45fSGeorge Liu     sdbusplus::bus::match_t fwUpdateMatcher;
89073a6530SGeorge Liu };
90073a6530SGeorge Liu 
91073a6530SGeorge Liu } // namespace usb
92073a6530SGeorge Liu } // namespace phosphor
93