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 | { |
| 49 | error("Parsing softoff config JSON file failed"); |
| 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 | { |
| 72 | error("Message get Sensor PDRs error. PLDM error code = {RC}", |
| 73 | "RC", lg2::hex, static_cast<int>(rc)); |
| 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 | { |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 116 | error("PLDM host soft off: Can't get current host state: {ERROR}", |
| 117 | "ERROR", e); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 118 | hasError = true; |
| 119 | return PLDM_ERROR; |
| 120 | } |
| 121 | |
| 122 | return PLDM_SUCCESS; |
| 123 | } |
| 124 | |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 125 | void SoftPowerOff::hostSoftOffComplete(sdbusplus::message_t& msg) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 126 | { |
| 127 | pldm::pdr::TerminusID msgTID; |
| 128 | pldm::pdr::SensorID msgSensorID; |
| 129 | pldm::pdr::SensorOffset msgSensorOffset; |
| 130 | pldm::pdr::EventState msgEventState; |
| 131 | pldm::pdr::EventState msgPreviousEventState; |
| 132 | |
| 133 | // Read the msg and populate each variable |
| 134 | msg.read(msgTID, msgSensorID, msgSensorOffset, msgEventState, |
| 135 | msgPreviousEventState); |
| 136 | |
| 137 | if (msgSensorID == sensorID && msgSensorOffset == sensorOffset && |
Zach Clark | e98c51b | 2021-06-08 11:29:06 -0500 | [diff] [blame^] | 138 | msgEventState == PLDM_SW_TERM_GRACEFUL_SHUTDOWN && msgTID == TID) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 139 | { |
| 140 | // Receive Graceful shutdown completion event message. Disable the timer |
| 141 | auto rc = timer.stop(); |
| 142 | if (rc < 0) |
| 143 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 144 | error("PLDM soft off: Failure to STOP the timer. ERRNO={RC}", "RC", |
| 145 | rc); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // This marks the completion of pldm soft power off. |
| 149 | completed = true; |
| 150 | } |
| 151 | } |
| 152 | |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 153 | Json SoftPowerOff::parseConfig() |
| 154 | { |
| 155 | fs::path softoffConfigJson(fs::path(SOFTOFF_CONFIG_JSON) / |
| 156 | "softoff_config.json"); |
| 157 | |
| 158 | if (!fs::exists(softoffConfigJson) || fs::is_empty(softoffConfigJson)) |
| 159 | { |
| 160 | error("Parsing softoff config JSON file failed, File does not exist"); |
| 161 | return PLDM_ERROR; |
| 162 | } |
| 163 | |
| 164 | std::ifstream jsonFile(softoffConfigJson); |
| 165 | return Json::parse(jsonFile); |
| 166 | } |
| 167 | |
| 168 | bool SoftPowerOff::getEffecterID(pldm::pdr::EntityType& entityType, |
| 169 | pldm::pdr::StateSetId& stateSetId) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 170 | { |
| 171 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 172 | try |
| 173 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 174 | std::vector<std::vector<uint8_t>> response{}; |
| 175 | auto method = bus.new_method_call( |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 176 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 177 | "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR"); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 178 | method.append(TID, entityType, stateSetId); |
| 179 | auto responseMsg = bus.call(method, dbusTimeout); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 180 | |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 181 | responseMsg.read(response); |
| 182 | if (response.size()) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 183 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 184 | for (auto& rep : response) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 185 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 186 | auto softoffPdr = |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 187 | reinterpret_cast<pldm_state_effecter_pdr*>(rep.data()); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 188 | effecterID = softoffPdr->effecter_id; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | else |
| 192 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 193 | return false; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 194 | } |
| 195 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 196 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 197 | { |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 198 | error("PLDM soft off: Error get softPowerOff PDR,ERROR={ERR_EXCEP}", |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 199 | "ERR_EXCEP", e.what()); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 200 | return false; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 201 | } |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 202 | return true; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 203 | } |
| 204 | |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 205 | int SoftPowerOff::getSensorInfo(pldm::pdr::EntityType& entityType, |
| 206 | pldm::pdr::StateSetId& stateSetId) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 207 | { |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 208 | try |
| 209 | { |
| 210 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 211 | std::vector<std::vector<uint8_t>> Response{}; |
| 212 | auto method = bus.new_method_call( |
| 213 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 214 | "xyz.openbmc_project.PLDM.PDR", "FindStateSensorPDR"); |
Sagar Srinivas | 4d99c31 | 2024-03-28 06:50:13 -0500 | [diff] [blame] | 215 | method.append(TID, entityType, stateSetId); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 216 | |
vkaverap@in.ibm.com | 91a092f | 2023-09-18 23:39:44 -0500 | [diff] [blame] | 217 | auto ResponseMsg = bus.call(method, dbusTimeout); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 218 | |
| 219 | ResponseMsg.read(Response); |
| 220 | |
| 221 | if (Response.size() == 0) |
| 222 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 223 | error("No sensor PDR has been found that matches the criteria"); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 224 | return PLDM_ERROR; |
| 225 | } |
| 226 | |
ThuBaNguyen | 499a29d | 2023-05-30 06:32:06 +0700 | [diff] [blame] | 227 | pldm_state_sensor_pdr* pdr = nullptr; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 228 | for (auto& rep : Response) |
| 229 | { |
| 230 | pdr = reinterpret_cast<pldm_state_sensor_pdr*>(rep.data()); |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 231 | if (!pdr) |
| 232 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 233 | error("Failed to get state sensor PDR."); |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 234 | return PLDM_ERROR; |
| 235 | } |
Manojkiran Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 236 | } |
| 237 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 238 | sensorID = pdr->sensor_id; |
| 239 | |
| 240 | auto compositeSensorCount = pdr->composite_sensor_count; |
| 241 | auto possibleStatesStart = pdr->possible_states; |
| 242 | |
| 243 | for (auto offset = 0; offset < compositeSensorCount; offset++) |
| 244 | { |
| 245 | auto possibleStates = |
| 246 | reinterpret_cast<state_sensor_possible_states*>( |
| 247 | possibleStatesStart); |
| 248 | auto setId = possibleStates->state_set_id; |
| 249 | auto possibleStateSize = possibleStates->possible_states_size; |
| 250 | |
| 251 | if (setId == PLDM_STATE_SET_SW_TERMINATION_STATUS) |
| 252 | { |
| 253 | sensorOffset = offset; |
| 254 | break; |
| 255 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 256 | possibleStatesStart += possibleStateSize + sizeof(setId) + |
| 257 | sizeof(possibleStateSize); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 258 | } |
| 259 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 260 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 261 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 262 | error("PLDM soft off: Error get State Sensor PDR,ERROR={ERR_EXCEP}", |
| 263 | "ERR_EXCEP", e.what()); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 264 | return PLDM_ERROR; |
| 265 | } |
| 266 | |
| 267 | return PLDM_SUCCESS; |
| 268 | } |
| 269 | |
| 270 | int SoftPowerOff::hostSoftOff(sdeventplus::Event& event) |
| 271 | { |
| 272 | constexpr uint8_t effecterCount = 1; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 273 | PldmTransport pldmTransport{}; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 274 | uint8_t instanceID; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 275 | uint8_t mctpEID; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 276 | |
| 277 | mctpEID = pldm::utils::readHostEID(); |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 278 | // TODO: fix mapping to work around OpenBMC ecosystem deficiencies |
| 279 | pldm_tid_t pldmTID = static_cast<pldm_tid_t>(mctpEID); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 280 | |
| 281 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterID) + |
| 282 | sizeof(effecterCount) + |
| 283 | sizeof(set_effecter_state_field)> |
| 284 | requestMsg{}; |
| 285 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 286 | set_effecter_state_field stateField{ |
| 287 | PLDM_REQUEST_SET, PLDM_SW_TERM_GRACEFUL_SHUTDOWN_REQUESTED}; |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 288 | instanceID = instanceIdDb.next(pldmTID); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 289 | auto rc = encode_set_state_effecter_states_req( |
| 290 | instanceID, effecterID, effecterCount, &stateField, request); |
| 291 | if (rc != PLDM_SUCCESS) |
| 292 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 293 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 294 | error("Message encode failure. PLDM error code = {RC}", "RC", lg2::hex, |
| 295 | static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 296 | return PLDM_ERROR; |
| 297 | } |
| 298 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 299 | // Add a timer to the event loop, default 30s. |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 300 | auto timerCallback = [=, this](Timer& /*source*/, |
| 301 | Timer::TimePoint /*time*/) mutable { |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 302 | if (!responseReceived) |
| 303 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 304 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 305 | error( |
| 306 | "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] | 307 | exit(-1); |
| 308 | } |
| 309 | return; |
| 310 | }; |
| 311 | Timer time(event, (Clock(event).now() + std::chrono::seconds{30}), |
| 312 | std::chrono::seconds{1}, std::move(timerCallback)); |
| 313 | |
| 314 | // Add a callback to handle EPOLLIN on fd |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 315 | auto callback = [=, &pldmTransport, this](IO& io, int fd, |
| 316 | uint32_t revents) mutable { |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 317 | if (fd != pldmTransport.getEventSource()) |
| 318 | { |
| 319 | return; |
| 320 | } |
| 321 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 322 | if (!(revents & EPOLLIN)) |
| 323 | { |
| 324 | return; |
| 325 | } |
| 326 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 327 | void* responseMsg = nullptr; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 328 | size_t responseMsgSize{}; |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 329 | pldm_tid_t srcTID = pldmTID; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 330 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 331 | auto rc = pldmTransport.recvMsg(pldmTID, responseMsg, responseMsgSize); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 332 | if (rc) |
| 333 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 334 | error("Soft off: failed to recv pldm data. PLDM RC = {RC}", "RC", |
| 335 | static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 339 | std::unique_ptr<void, decltype(std::free)*> responseMsgPtr{responseMsg, |
| 340 | std::free}; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 341 | |
| 342 | // We've got the response meant for the PLDM request msg that was |
| 343 | // sent out |
| 344 | io.set_enabled(Enabled::Off); |
| 345 | auto response = reinterpret_cast<pldm_msg*>(responseMsgPtr.get()); |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 346 | |
| 347 | if (srcTID != pldmTID || |
| 348 | !pldm_msg_hdr_correlate_response(&request->hdr, &response->hdr)) |
| 349 | { |
| 350 | /* This isn't the response we were looking for */ |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | /* We have the right response, release the instance ID and process */ |
| 355 | io.set_enabled(Enabled::Off); |
| 356 | instanceIdDb.free(pldmTID, instanceID); |
| 357 | |
George Liu | 9915d02 | 2021-12-21 14:04:31 +0800 | [diff] [blame] | 358 | if (response->payload[0] != PLDM_SUCCESS) |
| 359 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 360 | error("Getting the wrong response. PLDM RC = {RC}", "RC", |
| 361 | (unsigned)response->payload[0]); |
George Liu | 9915d02 | 2021-12-21 14:04:31 +0800 | [diff] [blame] | 362 | exit(-1); |
| 363 | } |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 364 | |
| 365 | responseReceived = true; |
| 366 | |
| 367 | // Start Timer |
| 368 | using namespace std::chrono; |
| 369 | auto timeMicroseconds = |
| 370 | duration_cast<microseconds>(seconds(SOFTOFF_TIMEOUT_SECONDS)); |
| 371 | |
| 372 | auto ret = startTimer(timeMicroseconds); |
| 373 | if (ret < 0) |
| 374 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 375 | error( |
| 376 | "Failure to start Host soft off wait timer, ERRNO = {RET}. Exit the pldm-softpoweroff", |
| 377 | "RET", ret); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 378 | exit(-1); |
| 379 | } |
| 380 | else |
| 381 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 382 | error( |
| 383 | "Timer started waiting for host soft off, TIMEOUT_IN_SEC = {TIMEOUT_SEC}", |
| 384 | "TIMEOUT_SEC", SOFTOFF_TIMEOUT_SECONDS); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 385 | } |
| 386 | return; |
| 387 | }; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 388 | IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 389 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 390 | // Asynchronously send the PLDM request |
| 391 | rc = pldmTransport.sendMsg(pldmTID, requestMsg.data(), requestMsg.size()); |
Sampa Misra | 9f8d2b0 | 2021-03-24 08:33:14 +0000 | [diff] [blame] | 392 | if (0 > rc) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 393 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 394 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 395 | error( |
| 396 | "Failed to send message/receive response. RC = {RC}, errno = {ERR}", |
| 397 | "RC", static_cast<int>(rc), "ERR", errno); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 398 | return PLDM_ERROR; |
| 399 | } |
| 400 | |
| 401 | // Time out or soft off complete |
| 402 | while (!isCompleted() && !isTimerExpired()) |
| 403 | { |
| 404 | try |
| 405 | { |
| 406 | event.run(std::nullopt); |
| 407 | } |
| 408 | catch (const sdeventplus::SdEventError& e) |
| 409 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 410 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 411 | error( |
| 412 | "PLDM host soft off: Failure in processing request.ERROR= {ERR_EXCEP}", |
| 413 | "ERR_EXCEP", e.what()); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 414 | return PLDM_ERROR; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return PLDM_SUCCESS; |
| 419 | } |
| 420 | |
| 421 | int SoftPowerOff::startTimer(const std::chrono::microseconds& usec) |
| 422 | { |
| 423 | return timer.start(usec); |
| 424 | } |
| 425 | } // namespace pldm |