| 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 |  | 
| Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 5 | #include <getopt.h> | 
 | 6 |  | 
| Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> | 
 | 8 |  | 
 | 9 | #include <cstdlib> | 
 | 10 | #include <exception> | 
| Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 11 | #include <filesystem> | 
| Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 12 | #include <format> | 
| 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 |  | 
| Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 20 | using ChassisState = sdbusplus::server::xyz::openbmc_project::state::Chassis; | 
 | 21 |  | 
| Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 22 | int main(int argc, char** argv) | 
| Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 23 | { | 
| Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 24 |     size_t chassisId = 0; | 
 | 25 |     int arg; | 
 | 26 |     int optIndex = 0; | 
| Pavithra Barithaya | f15b954 | 2024-06-21 08:18:48 -0500 | [diff] [blame] | 27 |     static struct option longOpts[] = { | 
 | 28 |         {"chassis", required_argument, nullptr, 'c'}, {nullptr, 0, nullptr, 0}}; | 
| Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 29 |  | 
 | 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.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 42 |     namespace fs = std::filesystem; | 
 | 43 |  | 
| Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 44 |     auto bus = sdbusplus::bus::new_default(); | 
 | 45 |  | 
| Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 46 |     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 Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 52 |  | 
| Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 53 |     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 Eda | 3ff5a36 | 2024-06-17 10:59:07 +0530 | [diff] [blame] | 57 |         // support multi-chassis state management, each service access new file | 
| Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 58 |         // 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 Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 63 |         fs::path newPohPath{std::format(POH_COUNTER_PERSIST_PATH, chassisId)}; | 
| Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 64 |         fs::path newStateChangePath{ | 
| Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 65 |             std::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, chassisId)}; | 
| Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 66 |         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 Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 76 |     // Add sdbusplus ObjectManager. | 
| Amithash Prasasd | 2eb6029 | 2024-08-13 19:19:07 -0700 | [diff] [blame] | 77 |     sdbusplus::server::manager_t objManager(bus, objPath); | 
| Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 78 |     phosphor::state::manager::Chassis manager(bus, objPathInst.c_str(), | 
 | 79 |                                               chassisId); | 
| Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 80 |  | 
| Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 81 |     // For backwards compatibility, request a busname without chassis id if | 
 | 82 |     // input id is 0. | 
 | 83 |     if (chassisId == 0) | 
 | 84 |     { | 
| Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 85 |         bus.request_name(ChassisState::interface); | 
| Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 86 |     } | 
| Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 87 |  | 
| Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 88 |     bus.request_name(chassisBusName.c_str()); | 
| Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 89 |     manager.startPOHCounter(); | 
| Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 90 |     return 0; | 
 | 91 | } |