xref: /openbmc/phosphor-bmc-code-mgmt/usb/usb_manager.cpp (revision 5107c45f001dd2ff027c9ed45373a8dbd9619113)
1 #include "config.h"
2 
3 #include "usb_manager.hpp"
4 
5 namespace phosphor
6 {
7 namespace usb
8 {
9 
10 bool USBManager::run()
11 {
12     fs::path dir(usbPath);
13     if (!fs::exists(dir))
14     {
15         return false;
16     }
17 
18     for (const auto& p : std::filesystem::directory_iterator(dir))
19     {
20         if (p.path().extension() == ".tar")
21         {
22             fs::path dstPath{IMG_UPLOAD_DIR / p.path().filename()};
23             if (fs::exists(dstPath))
24             {
25                 lg2::info(
26                     "{DSTPATH} already exists in the /tmp/images directory, exit the upgrade",
27                     "DSTPATH", p.path().filename());
28 
29                 break;
30             }
31 
32             try
33             {
34                 return fs::copy_file(fs::absolute(p.path()), dstPath);
35             }
36             catch (const std::exception& e)
37             {
38                 lg2::error("Error when copying {SRC} to /tmp/images: {ERROR}",
39                            "SRC", p.path(), "ERROR", e.what());
40             }
41 
42             break;
43         }
44     }
45 
46     return false;
47 }
48 
49 void USBManager::setApplyTime()
50 {
51     utils::PropertyValue value =
52         "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
53     constexpr auto objectPath = "/xyz/openbmc_project/software/apply_time";
54     constexpr auto interface = "xyz.openbmc_project.Software.ApplyTime";
55     constexpr auto propertyName = "RequestedApplyTime";
56     try
57     {
58         utils::setProperty(bus, objectPath, interface, propertyName, value);
59     }
60     catch (const std::exception& e)
61     {
62         lg2::error("Failed to set RequestedApplyTime property, ERROR:{ERROR}",
63                    "ERROR", e.what());
64     }
65 }
66 
67 void USBManager::setRequestedActivation(const std::string& path)
68 {
69     utils::PropertyValue value =
70         "xyz.openbmc_project.Software.Activation.RequestedActivations.Active";
71     constexpr auto interface = "xyz.openbmc_project.Software.Activation";
72     constexpr auto propertyName = "RequestedActivation";
73     try
74     {
75         utils::setProperty(bus, path, interface, propertyName, value);
76     }
77     catch (const std::exception& e)
78     {
79         lg2::error("Failed to set RequestedActivation property, ERROR:{ERROR}",
80                    "ERROR", e.what());
81     }
82 
83     return;
84 }
85 
86 void USBManager::updateActivation(sdbusplus::message::message& msg)
87 {
88     std::map<std::string, std::map<std::string, std::variant<std::string>>>
89         interfaces;
90     sdbusplus::message::object_path path;
91     msg.read(path, interfaces);
92 
93     constexpr auto imageInterface = "xyz.openbmc_project.Software.Activation";
94     constexpr auto readyPro =
95         "xyz.openbmc_project.Software.Activation.Activations.Ready";
96     for (auto& interface : interfaces)
97     {
98         if (interface.first != imageInterface)
99         {
100             continue;
101         }
102 
103         try
104         {
105             auto propVal =
106                 utils::getProperty(bus, path.str, imageInterface, "Activation");
107             const auto& imageProp = std::get<std::string>(propVal);
108             if (imageProp == readyPro && isUSBCodeUpdate)
109             {
110                 setApplyTime();
111                 setRequestedActivation(path.str);
112                 event.exit(0);
113             }
114         }
115         catch (const std::exception& e)
116         {
117             lg2::error("Failed in getting Activation status, ERROR:{ERROR}",
118                        "ERROR", e.what());
119         }
120     }
121 }
122 
123 } // namespace usb
124 } // namespace phosphor
125