Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 1 | #include "softoff.hpp" |
| 2 | |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 3 | #include "common/instance_id.hpp" |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 4 | #include "common/transport.hpp" |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 5 | #include "common/utils.hpp" |
| 6 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 7 | #include <libpldm/entity.h> |
| 8 | #include <libpldm/platform.h> |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 9 | #include <libpldm/state_set.h> |
| 10 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 12 | #include <sdbusplus/bus.hpp> |
| 13 | #include <sdeventplus/clock.hpp> |
George Liu | a2767e6 | 2021-02-24 18:53:34 +0800 | [diff] [blame] | 14 | #include <sdeventplus/exception.hpp> |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 15 | #include <sdeventplus/source/io.hpp> |
| 16 | #include <sdeventplus/source/time.hpp> |
| 17 | |
| 18 | #include <array> |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 19 | #include <filesystem> |
| 20 | #include <fstream> |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 21 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 22 | PHOSPHOR_LOG2_USING; |
| 23 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 24 | namespace pldm |
| 25 | { |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 26 | using namespace sdeventplus; |
| 27 | using namespace sdeventplus::source; |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 28 | namespace fs = std::filesystem; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 29 | constexpr auto clockId = sdeventplus::ClockId::RealTime; |
| 30 | using Clock = Clock<clockId>; |
| 31 | using Timer = Time<clockId>; |
| 32 | |
Zach Clark | e98c51b | 2021-06-08 11:29:06 -0500 | [diff] [blame] | 33 | using sdbusplus::exception::SdBusError; |
| 34 | |
| 35 | // Shutdown effecter terminus ID, set when we look up the effecter |
| 36 | pldm::pdr::TerminusID TID = 0; |
| 37 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 38 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 39 | |
Manojkiran Eda | 3fcfaa1 | 2024-02-26 15:17:58 +0530 | [diff] [blame] | 40 | SoftPowerOff::SoftPowerOff(sdbusplus::bus_t& bus, sd_event* event, |
| 41 | pldm::InstanceIdDb& instanceIdDb) : |
| 42 | bus(bus), |
| 43 | timer(event), instanceIdDb(instanceIdDb) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 44 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 45 | auto jsonData = parseConfig(); |
| 46 | |
| 47 | if (jsonData.is_discarded()) |
| 48 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 49 | error("Failed to parse softoff config JSON file"); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 53 | getHostState(); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 54 | if (hasError || completed) |
| 55 | { |
| 56 | return; |
| 57 | } |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 58 | const std::vector<Json> emptyJsonList{}; |
| 59 | auto entries = jsonData.value("entries", emptyJsonList); |
| 60 | for (const auto& entry : entries) |
| 61 | { |
| 62 | TID = entry.value("tid", 0); |
| 63 | pldm::pdr::EntityType entityType = entry.value("entityType", 0); |
| 64 | pldm::pdr::StateSetId stateSetId = entry.value("stateSetId", 0); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 65 | |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 66 | bool effecterFound = getEffecterID(entityType, stateSetId); |
| 67 | if (effecterFound) |
| 68 | { |
| 69 | auto rc = getSensorInfo(entityType, stateSetId); |
| 70 | if (rc != PLDM_SUCCESS) |
| 71 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 72 | error("Failed to get Sensor PDRs, response code '{RC}'", "RC", |
| 73 | lg2::hex, static_cast<int>(rc)); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 74 | hasError = true; |
| 75 | return; |
| 76 | } |
| 77 | break; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | continue; |
| 82 | } |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Matches on the pldm StateSensorEvent signal |
| 86 | pldmEventSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 87 | bus, |
| 88 | sdbusRule::type::signal() + sdbusRule::member("StateSensorEvent") + |
| 89 | sdbusRule::path("/xyz/openbmc_project/pldm") + |
| 90 | sdbusRule::interface("xyz.openbmc_project.PLDM.Event"), |
| 91 | std::bind(std::mem_fn(&SoftPowerOff::hostSoftOffComplete), this, |
| 92 | std::placeholders::_1)); |
| 93 | } |
| 94 | |
| 95 | int SoftPowerOff::getHostState() |
| 96 | { |
| 97 | try |
| 98 | { |
| 99 | pldm::utils::PropertyValue propertyValue = |
| 100 | pldm::utils::DBusHandler().getDbusPropertyVariant( |
| 101 | "/xyz/openbmc_project/state/host0", "CurrentHostState", |
| 102 | "xyz.openbmc_project.State.Host"); |
| 103 | |
Andrew Geissler | 5b5fa43 | 2021-01-22 16:27:24 -0600 | [diff] [blame] | 104 | if ((std::get<std::string>(propertyValue) != |
| 105 | "xyz.openbmc_project.State.Host.HostState.Running") && |
| 106 | (std::get<std::string>(propertyValue) != |
| 107 | "xyz.openbmc_project.State.Host.HostState.TransitioningToOff")) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 108 | { |
| 109 | // Host state is not "Running", this app should return success |
| 110 | completed = true; |
| 111 | return PLDM_SUCCESS; |
| 112 | } |
| 113 | } |
| 114 | catch (const std::exception& e) |
| 115 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 116 | error( |
| 117 | "PLDM remote terminus soft off. Can't get current remote terminus state, error - {ERROR}", |
| 118 | "ERROR", e); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 119 | hasError = true; |
| 120 | return PLDM_ERROR; |
| 121 | } |
| 122 | |
| 123 | return PLDM_SUCCESS; |
| 124 | } |
| 125 | |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 126 | void SoftPowerOff::hostSoftOffComplete(sdbusplus::message_t& msg) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 127 | { |
| 128 | pldm::pdr::TerminusID msgTID; |
| 129 | pldm::pdr::SensorID msgSensorID; |
| 130 | pldm::pdr::SensorOffset msgSensorOffset; |
| 131 | pldm::pdr::EventState msgEventState; |
| 132 | pldm::pdr::EventState msgPreviousEventState; |
| 133 | |
| 134 | // Read the msg and populate each variable |
| 135 | msg.read(msgTID, msgSensorID, msgSensorOffset, msgEventState, |
| 136 | msgPreviousEventState); |
| 137 | |
| 138 | if (msgSensorID == sensorID && msgSensorOffset == sensorOffset && |
Zach Clark | e98c51b | 2021-06-08 11:29:06 -0500 | [diff] [blame] | 139 | msgEventState == PLDM_SW_TERM_GRACEFUL_SHUTDOWN && msgTID == TID) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 140 | { |
| 141 | // Receive Graceful shutdown completion event message. Disable the timer |
| 142 | auto rc = timer.stop(); |
| 143 | if (rc < 0) |
| 144 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 145 | error( |
| 146 | "Failure to STOP the timer of PLDM soft off, response code '{RC}'", |
| 147 | "RC", rc); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // This marks the completion of pldm soft power off. |
| 151 | completed = true; |
| 152 | } |
| 153 | } |
| 154 | |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 155 | Json SoftPowerOff::parseConfig() |
| 156 | { |
| 157 | fs::path softoffConfigJson(fs::path(SOFTOFF_CONFIG_JSON) / |
| 158 | "softoff_config.json"); |
| 159 | |
| 160 | if (!fs::exists(softoffConfigJson) || fs::is_empty(softoffConfigJson)) |
| 161 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 162 | error( |
| 163 | "Failed to parse softoff config JSON file '{PATH}', file does not exist", |
| 164 | "PATH", softoffConfigJson); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 165 | return PLDM_ERROR; |
| 166 | } |
| 167 | |
| 168 | std::ifstream jsonFile(softoffConfigJson); |
| 169 | return Json::parse(jsonFile); |
| 170 | } |
| 171 | |
| 172 | bool SoftPowerOff::getEffecterID(pldm::pdr::EntityType& entityType, |
| 173 | pldm::pdr::StateSetId& stateSetId) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 174 | { |
| 175 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 176 | try |
| 177 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 178 | std::vector<std::vector<uint8_t>> response{}; |
| 179 | auto method = bus.new_method_call( |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 180 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 181 | "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR"); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 182 | method.append(TID, entityType, stateSetId); |
| 183 | auto responseMsg = bus.call(method, dbusTimeout); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 184 | |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 185 | responseMsg.read(response); |
| 186 | if (response.size()) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 187 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 188 | for (auto& rep : response) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 189 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 190 | auto softoffPdr = |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 191 | reinterpret_cast<pldm_state_effecter_pdr*>(rep.data()); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 192 | effecterID = softoffPdr->effecter_id; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | else |
| 196 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 197 | return false; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 198 | } |
| 199 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 200 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 201 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 202 | error("Failed to get softPowerOff PDR, error - {ERROR}", "ERROR", e); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 203 | return false; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 204 | } |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 205 | return true; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 206 | } |
| 207 | |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 208 | int SoftPowerOff::getSensorInfo(pldm::pdr::EntityType& entityType, |
| 209 | pldm::pdr::StateSetId& stateSetId) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 210 | { |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 211 | try |
| 212 | { |
| 213 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 214 | std::vector<std::vector<uint8_t>> Response{}; |
| 215 | auto method = bus.new_method_call( |
| 216 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 217 | "xyz.openbmc_project.PLDM.PDR", "FindStateSensorPDR"); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 218 | method.append(TID, entityType, stateSetId); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 219 | |
vkaverap@in.ibm.com | 91a092f | 2023-09-18 23:39:44 -0500 | [diff] [blame] | 220 | auto ResponseMsg = bus.call(method, dbusTimeout); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 221 | |
| 222 | ResponseMsg.read(Response); |
| 223 | |
| 224 | if (Response.size() == 0) |
| 225 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 226 | error("No sensor PDR has been found that matches the criteria"); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 227 | return PLDM_ERROR; |
| 228 | } |
| 229 | |
ThuBaNguyen | 499a29d | 2023-05-30 06:32:06 +0700 | [diff] [blame] | 230 | pldm_state_sensor_pdr* pdr = nullptr; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 231 | for (auto& rep : Response) |
| 232 | { |
| 233 | pdr = reinterpret_cast<pldm_state_sensor_pdr*>(rep.data()); |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 234 | if (!pdr) |
| 235 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 236 | error("Failed to get state sensor PDR."); |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 237 | return PLDM_ERROR; |
| 238 | } |
Manojkiran Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 239 | } |
| 240 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 241 | sensorID = pdr->sensor_id; |
| 242 | |
| 243 | auto compositeSensorCount = pdr->composite_sensor_count; |
| 244 | auto possibleStatesStart = pdr->possible_states; |
| 245 | |
| 246 | for (auto offset = 0; offset < compositeSensorCount; offset++) |
| 247 | { |
| 248 | auto possibleStates = |
| 249 | reinterpret_cast<state_sensor_possible_states*>( |
| 250 | possibleStatesStart); |
| 251 | auto setId = possibleStates->state_set_id; |
| 252 | auto possibleStateSize = possibleStates->possible_states_size; |
| 253 | |
| 254 | if (setId == PLDM_STATE_SET_SW_TERMINATION_STATUS) |
| 255 | { |
| 256 | sensorOffset = offset; |
| 257 | break; |
| 258 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 259 | possibleStatesStart += possibleStateSize + sizeof(setId) + |
| 260 | sizeof(possibleStateSize); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 261 | } |
| 262 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 263 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 264 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 265 | error("Failed to get state sensor PDR during soft-off, error - {ERROR}", |
| 266 | "ERROR", e); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 267 | return PLDM_ERROR; |
| 268 | } |
| 269 | |
| 270 | return PLDM_SUCCESS; |
| 271 | } |
| 272 | |
| 273 | int SoftPowerOff::hostSoftOff(sdeventplus::Event& event) |
| 274 | { |
| 275 | constexpr uint8_t effecterCount = 1; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 276 | PldmTransport pldmTransport{}; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 277 | uint8_t instanceID; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 278 | uint8_t mctpEID; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 279 | |
| 280 | mctpEID = pldm::utils::readHostEID(); |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 281 | // TODO: fix mapping to work around OpenBMC ecosystem deficiencies |
| 282 | pldm_tid_t pldmTID = static_cast<pldm_tid_t>(mctpEID); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 283 | |
| 284 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterID) + |
| 285 | sizeof(effecterCount) + |
| 286 | sizeof(set_effecter_state_field)> |
| 287 | requestMsg{}; |
| 288 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 289 | set_effecter_state_field stateField{ |
| 290 | PLDM_REQUEST_SET, PLDM_SW_TERM_GRACEFUL_SHUTDOWN_REQUESTED}; |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 291 | instanceID = instanceIdDb.next(pldmTID); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 292 | auto rc = encode_set_state_effecter_states_req( |
| 293 | instanceID, effecterID, effecterCount, &stateField, request); |
| 294 | if (rc != PLDM_SUCCESS) |
| 295 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 296 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 297 | error( |
| 298 | "Failed to encode set state effecter states request message, response code '{RC}'", |
| 299 | "RC", lg2::hex, static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 300 | return PLDM_ERROR; |
| 301 | } |
| 302 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 303 | // Add a timer to the event loop, default 30s. |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 304 | auto timerCallback = [=, this](Timer& /*source*/, |
| 305 | Timer::TimePoint /*time*/) mutable { |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 306 | if (!responseReceived) |
| 307 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 308 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 309 | error( |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 310 | "PLDM soft off failed, can't get the response for the PLDM request msg. Time out! Exit the pldm-softpoweroff"); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 311 | exit(-1); |
| 312 | } |
| 313 | return; |
| 314 | }; |
| 315 | Timer time(event, (Clock(event).now() + std::chrono::seconds{30}), |
| 316 | std::chrono::seconds{1}, std::move(timerCallback)); |
| 317 | |
| 318 | // Add a callback to handle EPOLLIN on fd |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 319 | auto callback = [=, &pldmTransport, this](IO& io, int fd, |
| 320 | uint32_t revents) mutable { |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 321 | if (fd != pldmTransport.getEventSource()) |
| 322 | { |
| 323 | return; |
| 324 | } |
| 325 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 326 | if (!(revents & EPOLLIN)) |
| 327 | { |
| 328 | return; |
| 329 | } |
| 330 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 331 | void* responseMsg = nullptr; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 332 | size_t responseMsgSize{}; |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 333 | pldm_tid_t srcTID = pldmTID; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 334 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 335 | auto rc = pldmTransport.recvMsg(pldmTID, responseMsg, responseMsgSize); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 336 | if (rc) |
| 337 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 338 | error( |
| 339 | "Failed to receive pldm data during soft-off, response code '{RC}'", |
| 340 | "RC", static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 341 | return; |
| 342 | } |
| 343 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 344 | std::unique_ptr<void, decltype(std::free)*> responseMsgPtr{responseMsg, |
| 345 | std::free}; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 346 | |
| 347 | // We've got the response meant for the PLDM request msg that was |
| 348 | // sent out |
| 349 | io.set_enabled(Enabled::Off); |
| 350 | auto response = reinterpret_cast<pldm_msg*>(responseMsgPtr.get()); |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 351 | |
| 352 | if (srcTID != pldmTID || |
| 353 | !pldm_msg_hdr_correlate_response(&request->hdr, &response->hdr)) |
| 354 | { |
| 355 | /* This isn't the response we were looking for */ |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | /* We have the right response, release the instance ID and process */ |
| 360 | io.set_enabled(Enabled::Off); |
| 361 | instanceIdDb.free(pldmTID, instanceID); |
| 362 | |
George Liu | 9915d02 | 2021-12-21 14:04:31 +0800 | [diff] [blame] | 363 | if (response->payload[0] != PLDM_SUCCESS) |
| 364 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 365 | error("Getting the wrong response, response code '{RC}'", "RC", |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 366 | (unsigned)response->payload[0]); |
George Liu | 9915d02 | 2021-12-21 14:04:31 +0800 | [diff] [blame] | 367 | exit(-1); |
| 368 | } |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 369 | |
| 370 | responseReceived = true; |
| 371 | |
| 372 | // Start Timer |
| 373 | using namespace std::chrono; |
| 374 | auto timeMicroseconds = |
| 375 | duration_cast<microseconds>(seconds(SOFTOFF_TIMEOUT_SECONDS)); |
| 376 | |
| 377 | auto ret = startTimer(timeMicroseconds); |
| 378 | if (ret < 0) |
| 379 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 380 | error( |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 381 | "Failure to start remote terminus soft off wait timer, Exit the pldm-softpoweroff with response code:{NUM}", |
| 382 | "NUM", ret); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 383 | exit(-1); |
| 384 | } |
| 385 | else |
| 386 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 387 | error( |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 388 | "Timer started waiting for remote terminus soft off, timeout in sec '{TIMEOUT_SEC}'", |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 389 | "TIMEOUT_SEC", SOFTOFF_TIMEOUT_SECONDS); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 390 | } |
| 391 | return; |
| 392 | }; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 393 | IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 394 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 395 | // Asynchronously send the PLDM request |
| 396 | rc = pldmTransport.sendMsg(pldmTID, requestMsg.data(), requestMsg.size()); |
Sampa Misra | 9f8d2b0 | 2021-03-24 08:33:14 +0000 | [diff] [blame] | 397 | if (0 > rc) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 398 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 399 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 400 | error( |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 401 | "Failed to send message/receive response, response code '{RC}' and error - {ERROR}", |
| 402 | "RC", static_cast<int>(rc), "ERROR", errno); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 403 | return PLDM_ERROR; |
| 404 | } |
| 405 | |
| 406 | // Time out or soft off complete |
| 407 | while (!isCompleted() && !isTimerExpired()) |
| 408 | { |
| 409 | try |
| 410 | { |
| 411 | event.run(std::nullopt); |
| 412 | } |
| 413 | catch (const sdeventplus::SdEventError& e) |
| 414 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 415 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 416 | error( |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 417 | "Failed to process request while remote terminus soft off, error - {ERROR}", |
| 418 | "ERROR", e); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 419 | return PLDM_ERROR; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return PLDM_SUCCESS; |
| 424 | } |
| 425 | |
| 426 | int SoftPowerOff::startTimer(const std::chrono::microseconds& usec) |
| 427 | { |
| 428 | return timer.start(usec); |
| 429 | } |
| 430 | } // namespace pldm |