Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "scheduled_host_transition.hpp" |
| 4 | |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 5 | #include <getopt.h> |
| 6 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
| 8 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 9 | #include <cstdlib> |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 10 | #include <exception> |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 11 | #include <filesystem> |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 12 | |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 13 | using ScheduledHostTransition = |
| 14 | sdbusplus::server::xyz::openbmc_project::state::ScheduledHostTransition; |
| 15 | |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 16 | int main(int argc, char** argv) |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 17 | { |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 18 | size_t hostId = 0; |
| 19 | |
| 20 | int arg; |
| 21 | int optIndex = 0; |
| 22 | |
Pavithra Barithaya | f15b954 | 2024-06-21 08:18:48 -0500 | [diff] [blame] | 23 | static struct option longOpts[] = { |
| 24 | {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}}; |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 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 | |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 38 | namespace fs = std::filesystem; |
| 39 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 40 | // Get a default event loop |
| 41 | auto event = sdeventplus::Event::get_default(); |
| 42 | |
| 43 | // Get a handle to system dbus |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 44 | auto bus = sdbusplus::bus::new_default(); |
| 45 | |
| 46 | // For now, we only have one instance of the host |
Andrew Geissler | a0d7bb8 | 2023-03-01 12:50:14 -0600 | [diff] [blame] | 47 | auto objPathInst = std::string{HOST_SCHED_OBJPATH} + std::to_string(hostId); |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 48 | |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 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 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 56 | // Add sdbusplus ObjectManager. |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 57 | sdbusplus::server::manager_t objManager(bus, objPathInst.c_str()); |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 58 | |
| 59 | phosphor::state::manager::ScheduledHostTransition manager( |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 60 | bus, objPathInst.c_str(), hostId, event); |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 61 | |
Andrew Geissler | 960017a | 2023-01-17 15:17:22 -0700 | [diff] [blame] | 62 | // For backwards compatibility, request a busname without host id if |
| 63 | // input id is 0. |
| 64 | if (hostId == 0) |
| 65 | { |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 66 | bus.request_name(ScheduledHostTransition::interface); |
Andrew Geissler | 960017a | 2023-01-17 15:17:22 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 69 | bus.request_name((std::string{ScheduledHostTransition::interface} + |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 70 | std::to_string(hostId)) |
| 71 | .c_str()); |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 72 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 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 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 77 | return 0; |
| 78 | } |