blob: 96542b9887524ae078e10a5e78de729d8929aa8c [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
Allen.Wangba182f02022-03-23 19:01:53 +08005#include <fmt/format.h>
Potin Lai70f36d82022-03-15 10:25:39 +08006#include <getopt.h>
7
Andrew Geisslere426b582020-05-28 12:40:55 -05008#include <sdbusplus/bus.hpp>
9
10#include <cstdlib>
11#include <exception>
Allen.Wangba182f02022-03-23 19:01:53 +080012#include <filesystem>
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
Potin Lai70f36d82022-03-15 10:25:39 +080020int main(int argc, char** argv)
Andrew Geisslera90a31a2016-12-13 16:16:28 -060021{
Potin Lai70f36d82022-03-15 10:25:39 +080022 size_t chassisId = 0;
23 int arg;
24 int optIndex = 0;
25 static struct option longOpts[] = {{"chassis", required_argument, 0, 'c'},
26 {0, 0, 0, 0}};
27
28 while ((arg = getopt_long(argc, argv, "c:", longOpts, &optIndex)) != -1)
29 {
30 switch (arg)
31 {
32 case 'c':
33 chassisId = std::stoul(optarg);
34 break;
35 default:
36 break;
37 }
38 }
39
Allen.Wangba182f02022-03-23 19:01:53 +080040 namespace fs = std::filesystem;
41
Andrew Geisslera90a31a2016-12-13 16:16:28 -060042 auto bus = sdbusplus::bus::new_default();
43
Potin Lai70f36d82022-03-15 10:25:39 +080044 auto chassisBusName =
45 std::string{CHASSIS_BUSNAME} + std::to_string(chassisId);
46 auto objPathInst = std::string{CHASSIS_OBJPATH} + std::to_string(chassisId);
Andrew Geisslera90a31a2016-12-13 16:16:28 -060047
Allen.Wangba182f02022-03-23 19:01:53 +080048 if (chassisId == 0)
49 {
50 // Chassis State Manager was only support single-chassis and there only
51 // two file to store persist values(POH and state change time), to
52 // support multi-chassis state mamagement, each service access new file
53 // paths with prefix 'chassisN', if any legacy persist file is exist,
54 // rename it to the new file format of chassis0.
55
56 fs::path legacyPohPath{LEGACY_POH_COUNTER_PERSIST_PATH};
57 fs::path legacyStateChangePath{LEGACY_STATE_CHANGE_PERSIST_PATH};
58 fs::path newPohPath{fmt::format(POH_COUNTER_PERSIST_PATH, chassisId)};
59 fs::path newStateChangePath{
60 fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, chassisId)};
61 if (fs::exists(legacyPohPath))
62 {
63 fs::rename(legacyPohPath, newPohPath);
64 }
65 if (fs::exists(legacyStateChangePath))
66 {
67 fs::rename(legacyStateChangePath, newStateChangePath);
68 }
69 }
70
Andrew Geisslera90a31a2016-12-13 16:16:28 -060071 // Add sdbusplus ObjectManager.
Patrick Williamsf053e6f2022-07-22 19:26:54 -050072 sdbusplus::server::manager_t objManager(bus, objPathInst.c_str());
Potin Lai70f36d82022-03-15 10:25:39 +080073 phosphor::state::manager::Chassis manager(bus, objPathInst.c_str(),
74 chassisId);
Andrew Geisslera90a31a2016-12-13 16:16:28 -060075
Potin Lai70f36d82022-03-15 10:25:39 +080076 // For backwards compatibility, request a busname without chassis id if
77 // input id is 0.
78 if (chassisId == 0)
79 {
80 bus.request_name(CHASSIS_BUSNAME);
81 }
Andrew Geisslera90a31a2016-12-13 16:16:28 -060082
Potin Lai70f36d82022-03-15 10:25:39 +080083 bus.request_name(chassisBusName.c_str());
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050084 manager.startPOHCounter();
Andrew Geisslera90a31a2016-12-13 16:16:28 -060085 return 0;
86}