blob: 600ee72e00df58cc7a00d65d387bc8193e63facb [file] [log] [blame]
Andrew Geisslere426b582020-05-28 12:40:55 -05001#include "config.h"
2
3#include "scheduled_host_transition.hpp"
4
Patrick Williams211d9722022-04-07 21:24:33 -05005#include <getopt.h>
6
Andrew Geisslere426b582020-05-28 12:40:55 -05007#include <sdbusplus/bus.hpp>
8
Carol Wang71230ef2020-02-18 17:39:49 +08009#include <cstdlib>
Carol Wang71230ef2020-02-18 17:39:49 +080010#include <exception>
Carol Wang1dbbef42020-03-09 11:51:23 +080011#include <filesystem>
Carol Wang71230ef2020-02-18 17:39:49 +080012
Patrick Williams211d9722022-04-07 21:24:33 -050013int main(int argc, char** argv)
Carol Wang71230ef2020-02-18 17:39:49 +080014{
Patrick Williams211d9722022-04-07 21:24:33 -050015 size_t hostId = 0;
16
17 int arg;
18 int optIndex = 0;
19
20 static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
21 {0, 0, 0, 0}};
22
23 while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
24 {
25 switch (arg)
26 {
27 case 'h':
28 hostId = std::stoul(optarg);
29 break;
30 default:
31 break;
32 }
33 }
34
Carol Wang1dbbef42020-03-09 11:51:23 +080035 namespace fs = std::filesystem;
36
Carol Wang6a5db3d2020-02-21 10:12:01 +080037 // Get a default event loop
38 auto event = sdeventplus::Event::get_default();
39
40 // Get a handle to system dbus
Carol Wang71230ef2020-02-18 17:39:49 +080041 auto bus = sdbusplus::bus::new_default();
42
43 // For now, we only have one instance of the host
Patrick Williams211d9722022-04-07 21:24:33 -050044 auto objPathInst = std::string{HOST_OBJPATH} + std::to_string(hostId);
Carol Wang71230ef2020-02-18 17:39:49 +080045
Carol Wang1dbbef42020-03-09 11:51:23 +080046 // Check SCHEDULED_HOST_TRANSITION_PERSIST_PATH
47 auto dir = fs::path(SCHEDULED_HOST_TRANSITION_PERSIST_PATH).parent_path();
48 if (!fs::exists(dir))
49 {
50 fs::create_directories(dir);
51 }
52
Carol Wang71230ef2020-02-18 17:39:49 +080053 // Add sdbusplus ObjectManager.
54 sdbusplus::server::manager::manager objManager(bus, objPathInst.c_str());
55
56 phosphor::state::manager::ScheduledHostTransition manager(
Patrick Williams211d9722022-04-07 21:24:33 -050057 bus, objPathInst.c_str(), hostId, event);
Carol Wang71230ef2020-02-18 17:39:49 +080058
Patrick Williams211d9722022-04-07 21:24:33 -050059 bus.request_name((std::string{SCHEDULED_HOST_TRANSITION_BUSNAME} +
60 std::to_string(hostId))
61 .c_str());
Carol Wang71230ef2020-02-18 17:39:49 +080062
Carol Wang6a5db3d2020-02-21 10:12:01 +080063 // Attach the bus to sd_event to service user requests
64 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
65 event.loop();
66
Carol Wang71230ef2020-02-18 17:39:49 +080067 return 0;
68}