Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 1 | #include "config.h" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 2 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 3 | #include "chassis_state_manager.hpp" |
| 4 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 5 | #include <fmt/format.h> |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 6 | #include <getopt.h> |
| 7 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
| 9 | |
| 10 | #include <cstdlib> |
| 11 | #include <exception> |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 12 | #include <filesystem> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 13 | #include <iostream> |
| 14 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 15 | constexpr auto LEGACY_POH_COUNTER_PERSIST_PATH = |
| 16 | "/var/lib/phosphor-state-manager/POHCounter"; |
| 17 | constexpr auto LEGACY_STATE_CHANGE_PERSIST_PATH = |
| 18 | "/var/lib/phosphor-state-manager/chassisStateChangeTime"; |
| 19 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 20 | int main(int argc, char** argv) |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 21 | { |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 22 | 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.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 40 | namespace fs = std::filesystem; |
| 41 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 42 | auto bus = sdbusplus::bus::new_default(); |
| 43 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 44 | auto chassisBusName = |
| 45 | std::string{CHASSIS_BUSNAME} + std::to_string(chassisId); |
| 46 | auto objPathInst = std::string{CHASSIS_OBJPATH} + std::to_string(chassisId); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 47 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 48 | 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 Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 71 | // Add sdbusplus ObjectManager. |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 72 | sdbusplus::server::manager_t objManager(bus, objPathInst.c_str()); |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 73 | phosphor::state::manager::Chassis manager(bus, objPathInst.c_str(), |
| 74 | chassisId); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 75 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 76 | // 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 Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 82 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 83 | bus.request_name(chassisBusName.c_str()); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 84 | manager.startPOHCounter(); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 85 | return 0; |
| 86 | } |