blob: 188f4d5af82d73b5a1be840053ccb467acb146d1 [file] [log] [blame]
Andrew Geisslera90a31a2016-12-13 16:16:28 -06001#include "config.h"
Andrew Geisslere426b582020-05-28 12:40:55 -05002
Andrew Geisslera90a31a2016-12-13 16:16:28 -06003#include "chassis_state_manager.hpp"
4
Potin Lai70f36d82022-03-15 10:25:39 +08005#include <getopt.h>
6
Andrew Geisslere426b582020-05-28 12:40:55 -05007#include <sdbusplus/bus.hpp>
8
9#include <cstdlib>
10#include <exception>
Allen.Wangba182f02022-03-23 19:01:53 +080011#include <filesystem>
Patrick Williams78c066f2024-02-13 12:25:58 -060012#include <format>
Andrew Geisslere426b582020-05-28 12:40:55 -050013#include <iostream>
14
Allen.Wangba182f02022-03-23 19:01:53 +080015constexpr auto LEGACY_POH_COUNTER_PERSIST_PATH =
16 "/var/lib/phosphor-state-manager/POHCounter";
17constexpr auto LEGACY_STATE_CHANGE_PERSIST_PATH =
18 "/var/lib/phosphor-state-manager/chassisStateChangeTime";
19
Amithash Prasasdf566c962024-07-22 20:28:42 -070020using ChassisState = sdbusplus::server::xyz::openbmc_project::state::Chassis;
21
Potin Lai70f36d82022-03-15 10:25:39 +080022int main(int argc, char** argv)
Andrew Geisslera90a31a2016-12-13 16:16:28 -060023{
Potin Lai70f36d82022-03-15 10:25:39 +080024 size_t chassisId = 0;
25 int arg;
26 int optIndex = 0;
Pavithra Barithayaf15b9542024-06-21 08:18:48 -050027 static struct option longOpts[] = {
28 {"chassis", required_argument, nullptr, 'c'}, {nullptr, 0, nullptr, 0}};
Potin Lai70f36d82022-03-15 10:25:39 +080029
30 while ((arg = getopt_long(argc, argv, "c:", longOpts, &optIndex)) != -1)
31 {
32 switch (arg)
33 {
34 case 'c':
35 chassisId = std::stoul(optarg);
36 break;
37 default:
38 break;
39 }
40 }
41
Allen.Wangba182f02022-03-23 19:01:53 +080042 namespace fs = std::filesystem;
43
Andrew Geisslera90a31a2016-12-13 16:16:28 -060044 auto bus = sdbusplus::bus::new_default();
45
Amithash Prasasdf566c962024-07-22 20:28:42 -070046 auto chassisBusName = ChassisState::interface + std::to_string(chassisId);
47 const auto* objPath = ChassisState::namespace_path::value;
48 auto chassisName = std::string(ChassisState::namespace_path::chassis) +
49 std::to_string(chassisId);
50 std::string objPathInst =
51 sdbusplus::message::object_path(objPath) / chassisName;
Andrew Geisslera90a31a2016-12-13 16:16:28 -060052
Allen.Wangba182f02022-03-23 19:01:53 +080053 if (chassisId == 0)
54 {
55 // Chassis State Manager was only support single-chassis and there only
56 // two file to store persist values(POH and state change time), to
Manojkiran Eda3ff5a362024-06-17 10:59:07 +053057 // support multi-chassis state management, each service access new file
Allen.Wangba182f02022-03-23 19:01:53 +080058 // paths with prefix 'chassisN', if any legacy persist file is exist,
59 // rename it to the new file format of chassis0.
60
61 fs::path legacyPohPath{LEGACY_POH_COUNTER_PERSIST_PATH};
62 fs::path legacyStateChangePath{LEGACY_STATE_CHANGE_PERSIST_PATH};
Patrick Williams78c066f2024-02-13 12:25:58 -060063 fs::path newPohPath{std::format(POH_COUNTER_PERSIST_PATH, chassisId)};
Allen.Wangba182f02022-03-23 19:01:53 +080064 fs::path newStateChangePath{
Patrick Williams78c066f2024-02-13 12:25:58 -060065 std::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, chassisId)};
Allen.Wangba182f02022-03-23 19:01:53 +080066 if (fs::exists(legacyPohPath))
67 {
68 fs::rename(legacyPohPath, newPohPath);
69 }
70 if (fs::exists(legacyStateChangePath))
71 {
72 fs::rename(legacyStateChangePath, newStateChangePath);
73 }
74 }
75
Andrew Geisslera90a31a2016-12-13 16:16:28 -060076 // Add sdbusplus ObjectManager.
Amithash Prasasd2eb60292024-08-13 19:19:07 -070077 sdbusplus::server::manager_t objManager(bus, objPath);
Potin Lai70f36d82022-03-15 10:25:39 +080078 phosphor::state::manager::Chassis manager(bus, objPathInst.c_str(),
79 chassisId);
Andrew Geisslera90a31a2016-12-13 16:16:28 -060080
Potin Lai70f36d82022-03-15 10:25:39 +080081 // For backwards compatibility, request a busname without chassis id if
82 // input id is 0.
83 if (chassisId == 0)
84 {
Amithash Prasasdf566c962024-07-22 20:28:42 -070085 bus.request_name(ChassisState::interface);
Potin Lai70f36d82022-03-15 10:25:39 +080086 }
Andrew Geisslera90a31a2016-12-13 16:16:28 -060087
Potin Lai70f36d82022-03-15 10:25:39 +080088 bus.request_name(chassisBusName.c_str());
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050089 manager.startPOHCounter();
Andrew Geisslera90a31a2016-12-13 16:16:28 -060090 return 0;
91}