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