136529022SAndrew Geissler #include "config.h"
2e426b589SAndrew Geissler
336529022SAndrew Geissler #include "host_state_manager.hpp"
436529022SAndrew Geissler
579b45003SAllen.Wang #include <getopt.h>
679b45003SAllen.Wang
7e426b589SAndrew Geissler #include <sdbusplus/bus.hpp>
8e426b589SAndrew Geissler
9e426b589SAndrew Geissler #include <cstdlib>
10e426b589SAndrew Geissler #include <exception>
116ed41eabSPatrick Williams #include <filesystem>
12*78c066f6SPatrick Williams #include <format>
13e426b589SAndrew Geissler #include <iostream>
14e426b589SAndrew Geissler
15ba182f0cSAllen.Wang constexpr auto LEGACY_HOST_STATE_PERSIST_PATH =
16ba182f0cSAllen.Wang "/var/lib/phosphor-state-manager/requestedHostTransition";
17ba182f0cSAllen.Wang
main(int argc,char ** argv)1879b45003SAllen.Wang int main(int argc, char** argv)
1936529022SAndrew Geissler {
2079b45003SAllen.Wang size_t hostId = 0;
2179b45003SAllen.Wang
2279b45003SAllen.Wang int arg;
2379b45003SAllen.Wang int optIndex = 0;
2479b45003SAllen.Wang
2579b45003SAllen.Wang static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
2679b45003SAllen.Wang {0, 0, 0, 0}};
2779b45003SAllen.Wang
2879b45003SAllen.Wang while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
2979b45003SAllen.Wang {
3079b45003SAllen.Wang switch (arg)
3179b45003SAllen.Wang {
3279b45003SAllen.Wang case 'h':
3379b45003SAllen.Wang hostId = std::stoul(optarg);
3479b45003SAllen.Wang break;
3579b45003SAllen.Wang default:
3679b45003SAllen.Wang break;
3779b45003SAllen.Wang }
3879b45003SAllen.Wang }
3979b45003SAllen.Wang
406ed41eabSPatrick Williams namespace fs = std::filesystem;
413f475242SDhruvaraj Subhashchandran
4236529022SAndrew Geissler auto bus = sdbusplus::bus::new_default();
4336529022SAndrew Geissler
4479b45003SAllen.Wang auto hostBusName = std::string{HOST_BUSNAME} + std::to_string(hostId);
4579b45003SAllen.Wang auto objPathInst = std::string{HOST_OBJPATH} + std::to_string(hostId);
461cb8b707SAndrew Geissler
47ba182f0cSAllen.Wang if (hostId == 0)
48ba182f0cSAllen.Wang {
49ba182f0cSAllen.Wang // Host State Manager was only support single-host and there only one
50ba182f0cSAllen.Wang // file to store persist values, to support multi-host state management,
51ba182f0cSAllen.Wang // each service instance access new file path format with prefix 'hostN'
52ba182f0cSAllen.Wang // now.For backward compatibility if there is a legacy persist file
53ba182f0cSAllen.Wang // exist, rename it to the new file format of host0.
54ba182f0cSAllen.Wang
55ba182f0cSAllen.Wang fs::path legacyPath{LEGACY_HOST_STATE_PERSIST_PATH};
56*78c066f6SPatrick Williams fs::path newPath{std::format(HOST_STATE_PERSIST_PATH, hostId)};
57ba182f0cSAllen.Wang if (fs::exists(legacyPath))
58ba182f0cSAllen.Wang {
59ba182f0cSAllen.Wang fs::rename(legacyPath, newPath);
60ba182f0cSAllen.Wang }
61ba182f0cSAllen.Wang }
62ba182f0cSAllen.Wang
6336529022SAndrew Geissler // Add sdbusplus ObjectManager.
64f053e6feSPatrick Williams sdbusplus::server::manager_t objManager(bus, objPathInst.c_str());
6536529022SAndrew Geissler
6679b45003SAllen.Wang phosphor::state::manager::Host manager(bus, objPathInst.c_str(), hostId);
67a90a31a9SAndrew Geissler
683f475242SDhruvaraj Subhashchandran auto dir = fs::path(HOST_STATE_PERSIST_PATH).parent_path();
693f475242SDhruvaraj Subhashchandran fs::create_directories(dir);
703f475242SDhruvaraj Subhashchandran
7179b45003SAllen.Wang // For backwards compatibility, request a busname without host id if
7279b45003SAllen.Wang // input id is 0.
7379b45003SAllen.Wang if (hostId == 0)
7479b45003SAllen.Wang {
75a90a31a9SAndrew Geissler bus.request_name(HOST_BUSNAME);
7679b45003SAllen.Wang }
7779b45003SAllen.Wang
7879b45003SAllen.Wang bus.request_name(hostBusName.c_str());
7936529022SAndrew Geissler
8036529022SAndrew Geissler while (true)
8136529022SAndrew Geissler {
8236529022SAndrew Geissler bus.process_discard();
8336529022SAndrew Geissler bus.wait();
8436529022SAndrew Geissler }
8536529022SAndrew Geissler return 0;
8636529022SAndrew Geissler }
87