Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "softoff.hpp" |
| 4 | |
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> |
| 9 | #include <libpldm/pldm.h> |
| 10 | #include <libpldm/state_set.h> |
| 11 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 12 | #include <phosphor-logging/lg2.hpp> |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 13 | #include <sdbusplus/bus.hpp> |
| 14 | #include <sdeventplus/clock.hpp> |
George Liu | a2767e6 | 2021-02-24 18:53:34 +0800 | [diff] [blame] | 15 | #include <sdeventplus/exception.hpp> |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 16 | #include <sdeventplus/source/io.hpp> |
| 17 | #include <sdeventplus/source/time.hpp> |
| 18 | |
| 19 | #include <array> |
| 20 | #include <iostream> |
| 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; |
| 28 | constexpr auto clockId = sdeventplus::ClockId::RealTime; |
| 29 | using Clock = Clock<clockId>; |
| 30 | using Timer = Time<clockId>; |
| 31 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 32 | constexpr pldm::pdr::TerminusID TID = 0; // TID will be implemented later. |
| 33 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 34 | |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 35 | SoftPowerOff::SoftPowerOff(sdbusplus::bus_t& bus, sd_event* event) : |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 36 | bus(bus), timer(event) |
| 37 | { |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 38 | getHostState(); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 39 | if (hasError || completed) |
| 40 | { |
| 41 | return; |
| 42 | } |
| 43 | |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 44 | auto rc = getEffecterID(); |
Tom Joseph | 29d2211 | 2020-11-18 15:05:15 +0530 | [diff] [blame] | 45 | if (completed) |
| 46 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 47 | error("pldm-softpoweroff: effecter to initiate softoff not found"); |
Tom Joseph | 29d2211 | 2020-11-18 15:05:15 +0530 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | else if (rc != PLDM_SUCCESS) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 51 | { |
| 52 | hasError = true; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | rc = getSensorInfo(); |
| 57 | if (rc != PLDM_SUCCESS) |
| 58 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 59 | error("Message get Sensor PDRs error. PLDM error code = {RC}", "RC", |
| 60 | lg2::hex, static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 61 | hasError = true; |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | // Matches on the pldm StateSensorEvent signal |
| 66 | pldmEventSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 67 | bus, |
| 68 | sdbusRule::type::signal() + sdbusRule::member("StateSensorEvent") + |
| 69 | sdbusRule::path("/xyz/openbmc_project/pldm") + |
| 70 | sdbusRule::interface("xyz.openbmc_project.PLDM.Event"), |
| 71 | std::bind(std::mem_fn(&SoftPowerOff::hostSoftOffComplete), this, |
| 72 | std::placeholders::_1)); |
| 73 | } |
| 74 | |
| 75 | int SoftPowerOff::getHostState() |
| 76 | { |
| 77 | try |
| 78 | { |
| 79 | pldm::utils::PropertyValue propertyValue = |
| 80 | pldm::utils::DBusHandler().getDbusPropertyVariant( |
| 81 | "/xyz/openbmc_project/state/host0", "CurrentHostState", |
| 82 | "xyz.openbmc_project.State.Host"); |
| 83 | |
Andrew Geissler | 5b5fa43 | 2021-01-22 16:27:24 -0600 | [diff] [blame] | 84 | if ((std::get<std::string>(propertyValue) != |
| 85 | "xyz.openbmc_project.State.Host.HostState.Running") && |
| 86 | (std::get<std::string>(propertyValue) != |
| 87 | "xyz.openbmc_project.State.Host.HostState.TransitioningToOff")) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 88 | { |
| 89 | // Host state is not "Running", this app should return success |
| 90 | completed = true; |
| 91 | return PLDM_SUCCESS; |
| 92 | } |
| 93 | } |
| 94 | catch (const std::exception& e) |
| 95 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 96 | error("PLDM host soft off: Can't get current host state."); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 97 | hasError = true; |
| 98 | return PLDM_ERROR; |
| 99 | } |
| 100 | |
| 101 | return PLDM_SUCCESS; |
| 102 | } |
| 103 | |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 104 | void SoftPowerOff::hostSoftOffComplete(sdbusplus::message_t& msg) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 105 | { |
| 106 | pldm::pdr::TerminusID msgTID; |
| 107 | pldm::pdr::SensorID msgSensorID; |
| 108 | pldm::pdr::SensorOffset msgSensorOffset; |
| 109 | pldm::pdr::EventState msgEventState; |
| 110 | pldm::pdr::EventState msgPreviousEventState; |
| 111 | |
| 112 | // Read the msg and populate each variable |
| 113 | msg.read(msgTID, msgSensorID, msgSensorOffset, msgEventState, |
| 114 | msgPreviousEventState); |
| 115 | |
| 116 | if (msgSensorID == sensorID && msgSensorOffset == sensorOffset && |
| 117 | msgEventState == PLDM_SW_TERM_GRACEFUL_SHUTDOWN) |
| 118 | { |
| 119 | // Receive Graceful shutdown completion event message. Disable the timer |
| 120 | auto rc = timer.stop(); |
| 121 | if (rc < 0) |
| 122 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 123 | error("PLDM soft off: Failure to STOP the timer. ERRNO={RC}", "RC", |
| 124 | rc); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // This marks the completion of pldm soft power off. |
| 128 | completed = true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | int SoftPowerOff::getEffecterID() |
| 133 | { |
| 134 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 135 | |
| 136 | // VMM is a logical entity, so the bit 15 in entity type is set. |
| 137 | pdr::EntityType entityType = PLDM_ENTITY_VIRTUAL_MACHINE_MANAGER | 0x8000; |
| 138 | |
| 139 | try |
| 140 | { |
| 141 | std::vector<std::vector<uint8_t>> VMMResponse{}; |
| 142 | auto VMMMethod = bus.new_method_call( |
| 143 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 144 | "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR"); |
| 145 | VMMMethod.append(TID, entityType, |
| 146 | (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS); |
| 147 | |
| 148 | auto VMMResponseMsg = bus.call(VMMMethod); |
| 149 | |
| 150 | VMMResponseMsg.read(VMMResponse); |
| 151 | if (VMMResponse.size() != 0) |
| 152 | { |
| 153 | for (auto& rep : VMMResponse) |
| 154 | { |
| 155 | auto VMMPdr = |
| 156 | reinterpret_cast<pldm_state_effecter_pdr*>(rep.data()); |
| 157 | effecterID = VMMPdr->effecter_id; |
| 158 | } |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | VMMPdrExist = false; |
| 163 | } |
| 164 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 165 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 166 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 167 | error("PLDM soft off: Error get VMM PDR,ERROR={ERR_EXCEP}", "ERR_EXCEP", |
| 168 | e.what()); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 169 | VMMPdrExist = false; |
| 170 | } |
| 171 | |
| 172 | if (VMMPdrExist) |
| 173 | { |
| 174 | return PLDM_SUCCESS; |
| 175 | } |
| 176 | |
| 177 | // If the Virtual Machine Manager PDRs doesn't exist, go find the System |
| 178 | // Firmware PDRs. |
| 179 | // System Firmware is a logical entity, so the bit 15 in entity type is set |
| 180 | entityType = PLDM_ENTITY_SYS_FIRMWARE | 0x8000; |
| 181 | try |
| 182 | { |
| 183 | std::vector<std::vector<uint8_t>> sysFwResponse{}; |
| 184 | auto sysFwMethod = bus.new_method_call( |
| 185 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 186 | "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR"); |
| 187 | sysFwMethod.append(TID, entityType, |
| 188 | (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS); |
| 189 | |
| 190 | auto sysFwResponseMsg = bus.call(sysFwMethod); |
| 191 | |
| 192 | sysFwResponseMsg.read(sysFwResponse); |
| 193 | |
| 194 | if (sysFwResponse.size() == 0) |
| 195 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 196 | error("No effecter ID has been found that matches the criteria"); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 197 | return PLDM_ERROR; |
| 198 | } |
| 199 | |
| 200 | for (auto& rep : sysFwResponse) |
| 201 | { |
| 202 | auto sysFwPdr = |
| 203 | reinterpret_cast<pldm_state_effecter_pdr*>(rep.data()); |
| 204 | effecterID = sysFwPdr->effecter_id; |
| 205 | } |
| 206 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 207 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 208 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 209 | error("PLDM soft off: Error get system firmware PDR,ERROR={ERR_EXCEP}", |
| 210 | "ERR_EXCEP", e.what()); |
Tom Joseph | 29d2211 | 2020-11-18 15:05:15 +0530 | [diff] [blame] | 211 | completed = true; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 212 | return PLDM_ERROR; |
| 213 | } |
| 214 | |
| 215 | return PLDM_SUCCESS; |
| 216 | } |
| 217 | |
| 218 | int SoftPowerOff::getSensorInfo() |
| 219 | { |
| 220 | pldm::pdr::EntityType entityType; |
| 221 | |
| 222 | entityType = VMMPdrExist ? PLDM_ENTITY_VIRTUAL_MACHINE_MANAGER |
| 223 | : PLDM_ENTITY_SYS_FIRMWARE; |
| 224 | |
| 225 | // The Virtual machine manager/System firmware is logical entity, so bit 15 |
| 226 | // need to be set. |
| 227 | entityType = entityType | 0x8000; |
| 228 | |
| 229 | try |
| 230 | { |
| 231 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 232 | std::vector<std::vector<uint8_t>> Response{}; |
| 233 | auto method = bus.new_method_call( |
| 234 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 235 | "xyz.openbmc_project.PLDM.PDR", "FindStateSensorPDR"); |
| 236 | method.append(TID, entityType, |
| 237 | (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS); |
| 238 | |
| 239 | auto ResponseMsg = bus.call(method); |
| 240 | |
| 241 | ResponseMsg.read(Response); |
| 242 | |
| 243 | if (Response.size() == 0) |
| 244 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 245 | error("No sensor PDR has been found that matches the criteria"); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 246 | return PLDM_ERROR; |
| 247 | } |
| 248 | |
ThuBaNguyen | 499a29d | 2023-05-30 06:32:06 +0700 | [diff] [blame] | 249 | pldm_state_sensor_pdr* pdr = nullptr; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 250 | for (auto& rep : Response) |
| 251 | { |
| 252 | pdr = reinterpret_cast<pldm_state_sensor_pdr*>(rep.data()); |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 253 | if (!pdr) |
| 254 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 255 | error("Failed to get state sensor PDR."); |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 256 | return PLDM_ERROR; |
| 257 | } |
Manojkiran Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 258 | } |
| 259 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 260 | sensorID = pdr->sensor_id; |
| 261 | |
| 262 | auto compositeSensorCount = pdr->composite_sensor_count; |
| 263 | auto possibleStatesStart = pdr->possible_states; |
| 264 | |
| 265 | for (auto offset = 0; offset < compositeSensorCount; offset++) |
| 266 | { |
| 267 | auto possibleStates = |
| 268 | reinterpret_cast<state_sensor_possible_states*>( |
| 269 | possibleStatesStart); |
| 270 | auto setId = possibleStates->state_set_id; |
| 271 | auto possibleStateSize = possibleStates->possible_states_size; |
| 272 | |
| 273 | if (setId == PLDM_STATE_SET_SW_TERMINATION_STATUS) |
| 274 | { |
| 275 | sensorOffset = offset; |
| 276 | break; |
| 277 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 278 | possibleStatesStart += possibleStateSize + sizeof(setId) + |
| 279 | sizeof(possibleStateSize); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 280 | } |
| 281 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 282 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 283 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 284 | error("PLDM soft off: Error get State Sensor PDR,ERROR={ERR_EXCEP}", |
| 285 | "ERR_EXCEP", e.what()); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 286 | return PLDM_ERROR; |
| 287 | } |
| 288 | |
| 289 | return PLDM_SUCCESS; |
| 290 | } |
| 291 | |
| 292 | int SoftPowerOff::hostSoftOff(sdeventplus::Event& event) |
| 293 | { |
| 294 | constexpr uint8_t effecterCount = 1; |
| 295 | uint8_t mctpEID; |
| 296 | uint8_t instanceID; |
| 297 | |
| 298 | mctpEID = pldm::utils::readHostEID(); |
| 299 | |
| 300 | // Get instanceID |
| 301 | try |
| 302 | { |
| 303 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 304 | auto method = bus.new_method_call( |
| 305 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 306 | "xyz.openbmc_project.PLDM.Requester", "GetInstanceId"); |
| 307 | method.append(mctpEID); |
| 308 | |
| 309 | auto ResponseMsg = bus.call(method); |
| 310 | |
| 311 | ResponseMsg.read(instanceID); |
| 312 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 313 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 314 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 315 | error("PLDM soft off: Error get instanceID,ERROR={ERR_EXCEP}", |
| 316 | "ERR_EXCEP", e.what()); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 317 | return PLDM_ERROR; |
| 318 | } |
| 319 | |
| 320 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterID) + |
| 321 | sizeof(effecterCount) + |
| 322 | sizeof(set_effecter_state_field)> |
| 323 | requestMsg{}; |
| 324 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 325 | set_effecter_state_field stateField{ |
| 326 | PLDM_REQUEST_SET, PLDM_SW_TERM_GRACEFUL_SHUTDOWN_REQUESTED}; |
| 327 | auto rc = encode_set_state_effecter_states_req( |
| 328 | instanceID, effecterID, effecterCount, &stateField, request); |
| 329 | if (rc != PLDM_SUCCESS) |
| 330 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 331 | error("Message encode failure. PLDM error code = {RC}", "RC", lg2::hex, |
| 332 | static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 333 | return PLDM_ERROR; |
| 334 | } |
| 335 | |
| 336 | // Open connection to MCTP socket |
| 337 | int fd = pldm_open(); |
| 338 | if (-1 == fd) |
| 339 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 340 | error("Failed to connect to mctp demux daemon"); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 341 | return PLDM_ERROR; |
| 342 | } |
| 343 | |
| 344 | // Add a timer to the event loop, default 30s. |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 345 | auto timerCallback = |
| 346 | [=, this](Timer& /*source*/, Timer::TimePoint /*time*/) { |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 347 | if (!responseReceived) |
| 348 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 349 | error( |
| 350 | "PLDM soft off: ERROR! 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] | 351 | exit(-1); |
| 352 | } |
| 353 | return; |
| 354 | }; |
| 355 | Timer time(event, (Clock(event).now() + std::chrono::seconds{30}), |
| 356 | std::chrono::seconds{1}, std::move(timerCallback)); |
| 357 | |
| 358 | // Add a callback to handle EPOLLIN on fd |
Deepak Kodihalli | b0d15f1 | 2021-04-16 12:18:10 +0530 | [diff] [blame] | 359 | auto callback = [=, this](IO& io, int fd, uint32_t revents) { |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 360 | if (!(revents & EPOLLIN)) |
| 361 | { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | uint8_t* responseMsg = nullptr; |
| 366 | size_t responseMsgSize{}; |
| 367 | |
| 368 | auto rc = pldm_recv(mctpEID, fd, request->hdr.instance_id, &responseMsg, |
| 369 | &responseMsgSize); |
| 370 | if (rc) |
| 371 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 372 | error("Soft off: failed to recv pldm data. PLDM RC = {RC}", "RC", |
| 373 | static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
| 377 | std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{ |
| 378 | responseMsg, std::free}; |
| 379 | |
| 380 | // We've got the response meant for the PLDM request msg that was |
| 381 | // sent out |
| 382 | io.set_enabled(Enabled::Off); |
| 383 | auto response = reinterpret_cast<pldm_msg*>(responseMsgPtr.get()); |
George Liu | 9915d02 | 2021-12-21 14:04:31 +0800 | [diff] [blame] | 384 | if (response->payload[0] != PLDM_SUCCESS) |
| 385 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 386 | error("Getting the wrong response. PLDM RC = {RC}", "RC", |
| 387 | (unsigned)response->payload[0]); |
George Liu | 9915d02 | 2021-12-21 14:04:31 +0800 | [diff] [blame] | 388 | exit(-1); |
| 389 | } |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 390 | |
| 391 | responseReceived = true; |
| 392 | |
| 393 | // Start Timer |
| 394 | using namespace std::chrono; |
| 395 | auto timeMicroseconds = |
| 396 | duration_cast<microseconds>(seconds(SOFTOFF_TIMEOUT_SECONDS)); |
| 397 | |
| 398 | auto ret = startTimer(timeMicroseconds); |
| 399 | if (ret < 0) |
| 400 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 401 | error( |
| 402 | "Failure to start Host soft off wait timer, ERRNO = {RET}. Exit the pldm-softpoweroff", |
| 403 | "RET", ret); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 404 | exit(-1); |
| 405 | } |
| 406 | else |
| 407 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 408 | error( |
| 409 | "Timer started waiting for host soft off, TIMEOUT_IN_SEC = {TIMEOUT_SEC}", |
| 410 | "TIMEOUT_SEC", SOFTOFF_TIMEOUT_SECONDS); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 411 | } |
| 412 | return; |
| 413 | }; |
| 414 | IO io(event, fd, EPOLLIN, std::move(callback)); |
| 415 | |
| 416 | // Send PLDM Request message - pldm_send doesn't wait for response |
| 417 | rc = pldm_send(mctpEID, fd, requestMsg.data(), requestMsg.size()); |
Sampa Misra | 9f8d2b0 | 2021-03-24 08:33:14 +0000 | [diff] [blame] | 418 | if (0 > rc) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 419 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 420 | error( |
| 421 | "Failed to send message/receive response. RC = {RC}, errno = {ERR}", |
| 422 | "RC", static_cast<int>(rc), "ERR", errno); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 423 | return PLDM_ERROR; |
| 424 | } |
| 425 | |
| 426 | // Time out or soft off complete |
| 427 | while (!isCompleted() && !isTimerExpired()) |
| 428 | { |
| 429 | try |
| 430 | { |
| 431 | event.run(std::nullopt); |
| 432 | } |
| 433 | catch (const sdeventplus::SdEventError& e) |
| 434 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 435 | error( |
| 436 | "PLDM host soft off: Failure in processing request.ERROR= {ERR_EXCEP}", |
| 437 | "ERR_EXCEP", e.what()); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 438 | return PLDM_ERROR; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | return PLDM_SUCCESS; |
| 443 | } |
| 444 | |
| 445 | int SoftPowerOff::startTimer(const std::chrono::microseconds& usec) |
| 446 | { |
| 447 | return timer.start(usec); |
| 448 | } |
| 449 | } // namespace pldm |