xref: /openbmc/phosphor-state-manager/chassis_state_manager_main.cpp (revision 2eb6029cd9696b1db92c59e85a6752ac4ba4a5a0)
1a90a31a9SAndrew Geissler #include "config.h"
2e426b589SAndrew Geissler 
3a90a31a9SAndrew Geissler #include "chassis_state_manager.hpp"
4a90a31a9SAndrew Geissler 
570f36d8eSPotin Lai #include <getopt.h>
670f36d8eSPotin Lai 
7e426b589SAndrew Geissler #include <sdbusplus/bus.hpp>
8e426b589SAndrew Geissler 
9e426b589SAndrew Geissler #include <cstdlib>
10e426b589SAndrew Geissler #include <exception>
11ba182f0cSAllen.Wang #include <filesystem>
1278c066f6SPatrick Williams #include <format>
13e426b589SAndrew Geissler #include <iostream>
14e426b589SAndrew Geissler 
15ba182f0cSAllen.Wang constexpr auto LEGACY_POH_COUNTER_PERSIST_PATH =
16ba182f0cSAllen.Wang     "/var/lib/phosphor-state-manager/POHCounter";
17ba182f0cSAllen.Wang constexpr auto LEGACY_STATE_CHANGE_PERSIST_PATH =
18ba182f0cSAllen.Wang     "/var/lib/phosphor-state-manager/chassisStateChangeTime";
19ba182f0cSAllen.Wang 
20f566c964SAmithash Prasasd using ChassisState = sdbusplus::server::xyz::openbmc_project::state::Chassis;
21f566c964SAmithash Prasasd 
main(int argc,char ** argv)2270f36d8eSPotin Lai int main(int argc, char** argv)
23a90a31a9SAndrew Geissler {
2470f36d8eSPotin Lai     size_t chassisId = 0;
2570f36d8eSPotin Lai     int arg;
2670f36d8eSPotin Lai     int optIndex = 0;
27f15b9544SPavithra Barithaya     static struct option longOpts[] = {
28f15b9544SPavithra Barithaya         {"chassis", required_argument, nullptr, 'c'}, {nullptr, 0, nullptr, 0}};
2970f36d8eSPotin Lai 
3070f36d8eSPotin Lai     while ((arg = getopt_long(argc, argv, "c:", longOpts, &optIndex)) != -1)
3170f36d8eSPotin Lai     {
3270f36d8eSPotin Lai         switch (arg)
3370f36d8eSPotin Lai         {
3470f36d8eSPotin Lai             case 'c':
3570f36d8eSPotin Lai                 chassisId = std::stoul(optarg);
3670f36d8eSPotin Lai                 break;
3770f36d8eSPotin Lai             default:
3870f36d8eSPotin Lai                 break;
3970f36d8eSPotin Lai         }
4070f36d8eSPotin Lai     }
4170f36d8eSPotin Lai 
42ba182f0cSAllen.Wang     namespace fs = std::filesystem;
43ba182f0cSAllen.Wang 
44a90a31a9SAndrew Geissler     auto bus = sdbusplus::bus::new_default();
45a90a31a9SAndrew Geissler 
46f566c964SAmithash Prasasd     auto chassisBusName = ChassisState::interface + std::to_string(chassisId);
47f566c964SAmithash Prasasd     const auto* objPath = ChassisState::namespace_path::value;
48f566c964SAmithash Prasasd     auto chassisName = std::string(ChassisState::namespace_path::chassis) +
49f566c964SAmithash Prasasd                        std::to_string(chassisId);
50f566c964SAmithash Prasasd     std::string objPathInst =
51f566c964SAmithash Prasasd         sdbusplus::message::object_path(objPath) / chassisName;
52a90a31a9SAndrew Geissler 
53ba182f0cSAllen.Wang     if (chassisId == 0)
54ba182f0cSAllen.Wang     {
55ba182f0cSAllen.Wang         // Chassis State Manager was only support single-chassis and there only
56ba182f0cSAllen.Wang         // two file to store persist values(POH and state change time), to
573ff5a360SManojkiran Eda         // support multi-chassis state management, each service access new file
58ba182f0cSAllen.Wang         // paths with prefix 'chassisN', if any legacy persist file is exist,
59ba182f0cSAllen.Wang         // rename it to the new file format of chassis0.
60ba182f0cSAllen.Wang 
61ba182f0cSAllen.Wang         fs::path legacyPohPath{LEGACY_POH_COUNTER_PERSIST_PATH};
62ba182f0cSAllen.Wang         fs::path legacyStateChangePath{LEGACY_STATE_CHANGE_PERSIST_PATH};
6378c066f6SPatrick Williams         fs::path newPohPath{std::format(POH_COUNTER_PERSIST_PATH, chassisId)};
64ba182f0cSAllen.Wang         fs::path newStateChangePath{
6578c066f6SPatrick Williams             std::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, chassisId)};
66ba182f0cSAllen.Wang         if (fs::exists(legacyPohPath))
67ba182f0cSAllen.Wang         {
68ba182f0cSAllen.Wang             fs::rename(legacyPohPath, newPohPath);
69ba182f0cSAllen.Wang         }
70ba182f0cSAllen.Wang         if (fs::exists(legacyStateChangePath))
71ba182f0cSAllen.Wang         {
72ba182f0cSAllen.Wang             fs::rename(legacyStateChangePath, newStateChangePath);
73ba182f0cSAllen.Wang         }
74ba182f0cSAllen.Wang     }
75ba182f0cSAllen.Wang 
76a90a31a9SAndrew Geissler     // Add sdbusplus ObjectManager.
77*2eb6029cSAmithash Prasasd     sdbusplus::server::manager_t objManager(bus, objPath);
7870f36d8eSPotin Lai     phosphor::state::manager::Chassis manager(bus, objPathInst.c_str(),
7970f36d8eSPotin Lai                                               chassisId);
80a90a31a9SAndrew Geissler 
8170f36d8eSPotin Lai     // For backwards compatibility, request a busname without chassis id if
8270f36d8eSPotin Lai     // input id is 0.
8370f36d8eSPotin Lai     if (chassisId == 0)
8470f36d8eSPotin Lai     {
85f566c964SAmithash Prasasd         bus.request_name(ChassisState::interface);
8670f36d8eSPotin Lai     }
87a90a31a9SAndrew Geissler 
8870f36d8eSPotin Lai     bus.request_name(chassisBusName.c_str());
8970f36d8eSPotin Lai     manager.startPOHCounter();
90a90a31a9SAndrew Geissler     return 0;
91a90a31a9SAndrew Geissler }
92