1 #include "config.h"
2 
3 #include "scheduled_host_transition.hpp"
4 
5 #include <getopt.h>
6 
7 #include <sdbusplus/bus.hpp>
8 
9 #include <cstdlib>
10 #include <exception>
11 #include <filesystem>
12 
13 using ScheduledHostTransition =
14     sdbusplus::server::xyz::openbmc_project::state::ScheduledHostTransition;
15 
16 int main(int argc, char** argv)
17 {
18     size_t hostId = 0;
19 
20     int arg;
21     int optIndex = 0;
22 
23     static struct option longOpts[] = {
24         {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
25 
26     while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
27     {
28         switch (arg)
29         {
30             case 'h':
31                 hostId = std::stoul(optarg);
32                 break;
33             default:
34                 break;
35         }
36     }
37 
38     namespace fs = std::filesystem;
39 
40     // Get a default event loop
41     auto event = sdeventplus::Event::get_default();
42 
43     // Get a handle to system dbus
44     auto bus = sdbusplus::bus::new_default();
45 
46     // For now, we only have one instance of the host
47     auto objPathInst = std::string{HOST_SCHED_OBJPATH} + std::to_string(hostId);
48 
49     // Check SCHEDULED_HOST_TRANSITION_PERSIST_PATH
50     auto dir = fs::path(SCHEDULED_HOST_TRANSITION_PERSIST_PATH).parent_path();
51     if (!fs::exists(dir))
52     {
53         fs::create_directories(dir);
54     }
55 
56     // Add sdbusplus ObjectManager.
57     sdbusplus::server::manager_t objManager(bus, objPathInst.c_str());
58 
59     phosphor::state::manager::ScheduledHostTransition manager(
60         bus, objPathInst.c_str(), hostId, event);
61 
62     // For backwards compatibility, request a busname without host id if
63     // input id is 0.
64     if (hostId == 0)
65     {
66         bus.request_name(ScheduledHostTransition::interface);
67     }
68 
69     bus.request_name((std::string{ScheduledHostTransition::interface} +
70                       std::to_string(hostId))
71                          .c_str());
72 
73     // Attach the bus to sd_event to service user requests
74     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
75     event.loop();
76 
77     return 0;
78 }
79