blob: 07cd47fbfbd645b1572e8013f9c7e45ba283ea9e [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
Amithash Prasasdf566c962024-07-22 20:28:42 -070013using ScheduledHostTransition =
14 sdbusplus::server::xyz::openbmc_project::state::ScheduledHostTransition;
15
Patrick Williams211d9722022-04-07 21:24:33 -050016int main(int argc, char** argv)
Carol Wang71230ef2020-02-18 17:39:49 +080017{
Patrick Williams211d9722022-04-07 21:24:33 -050018 size_t hostId = 0;
19
20 int arg;
21 int optIndex = 0;
22
Pavithra Barithayaf15b9542024-06-21 08:18:48 -050023 static struct option longOpts[] = {
24 {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
Patrick Williams211d9722022-04-07 21:24:33 -050025
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
Carol Wang1dbbef42020-03-09 11:51:23 +080038 namespace fs = std::filesystem;
39
Carol Wang6a5db3d2020-02-21 10:12:01 +080040 // Get a default event loop
41 auto event = sdeventplus::Event::get_default();
42
43 // Get a handle to system dbus
Carol Wang71230ef2020-02-18 17:39:49 +080044 auto bus = sdbusplus::bus::new_default();
45
46 // For now, we only have one instance of the host
Andrew Geisslera0d7bb82023-03-01 12:50:14 -060047 auto objPathInst = std::string{HOST_SCHED_OBJPATH} + std::to_string(hostId);
Carol Wang71230ef2020-02-18 17:39:49 +080048
Carol Wang1dbbef42020-03-09 11:51:23 +080049 // 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
Carol Wang71230ef2020-02-18 17:39:49 +080056 // Add sdbusplus ObjectManager.
Patrick Williamsf053e6f2022-07-22 19:26:54 -050057 sdbusplus::server::manager_t objManager(bus, objPathInst.c_str());
Carol Wang71230ef2020-02-18 17:39:49 +080058
59 phosphor::state::manager::ScheduledHostTransition manager(
Patrick Williams211d9722022-04-07 21:24:33 -050060 bus, objPathInst.c_str(), hostId, event);
Carol Wang71230ef2020-02-18 17:39:49 +080061
Andrew Geissler960017a2023-01-17 15:17:22 -070062 // For backwards compatibility, request a busname without host id if
63 // input id is 0.
64 if (hostId == 0)
65 {
Amithash Prasasdf566c962024-07-22 20:28:42 -070066 bus.request_name(ScheduledHostTransition::interface);
Andrew Geissler960017a2023-01-17 15:17:22 -070067 }
68
Amithash Prasasdf566c962024-07-22 20:28:42 -070069 bus.request_name((std::string{ScheduledHostTransition::interface} +
Patrick Williams211d9722022-04-07 21:24:33 -050070 std::to_string(hostId))
71 .c_str());
Carol Wang71230ef2020-02-18 17:39:49 +080072
Carol Wang6a5db3d2020-02-21 10:12:01 +080073 // Attach the bus to sd_event to service user requests
74 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
75 event.loop();
76
Carol Wang71230ef2020-02-18 17:39:49 +080077 return 0;
78}