Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 16 | #include "config.h" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 17 | |
| 18 | #include "softoff.hpp" |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 19 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 20 | #include <phosphor-logging/elog-errors.hpp> |
| 21 | #include <phosphor-logging/elog.hpp> |
George Liu | ce338fd | 2024-07-17 14:22:14 +0800 | [diff] [blame] | 22 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | 4f67cc3 | 2018-10-18 15:24:49 -0700 | [diff] [blame] | 23 | #include <sdeventplus/event.hpp> |
| 24 | #include <sdeventplus/exception.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 25 | #include <xyz/openbmc_project/State/Host/error.hpp> |
| 26 | |
Andrew Geissler | 83527ef | 2017-05-18 16:54:29 -0500 | [diff] [blame] | 27 | // Return -1 on any errors to ensure we follow the calling targets OnFailure= |
| 28 | // path |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 29 | int main(int, char**) |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 30 | { |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame] | 31 | using namespace phosphor::logging; |
| 32 | |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 33 | // Get a handle to system dbus. |
| 34 | auto bus = sdbusplus::bus::new_default(); |
| 35 | |
| 36 | // Add systemd object manager. |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 37 | sdbusplus::server::manager_t(bus, SOFTOFF_OBJPATH); |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 38 | |
William A. Kennington III | 4f67cc3 | 2018-10-18 15:24:49 -0700 | [diff] [blame] | 39 | // Get default event loop |
| 40 | auto event = sdeventplus::Event::get_default(); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 41 | |
| 42 | // Attach the bus to sd_event to service user requests |
William A. Kennington III | 4f67cc3 | 2018-10-18 15:24:49 -0700 | [diff] [blame] | 43 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 44 | |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame] | 45 | // Claim the bus. Delaying it until sending SMS_ATN may result |
| 46 | // in a race condition between this available and IPMI trying to send |
Gunnar Mills | 8991dd6 | 2017-10-25 17:11:29 -0500 | [diff] [blame] | 47 | // message as a response to ack from host. |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 48 | bus.request_name(SOFTOFF_BUSNAME); |
| 49 | |
Vishwanatha Subbanna | 917454b | 2017-02-24 00:16:05 +0530 | [diff] [blame] | 50 | // Create the SoftPowerOff object. |
William A. Kennington III | 4f67cc3 | 2018-10-18 15:24:49 -0700 | [diff] [blame] | 51 | phosphor::ipmi::SoftPowerOff powerObj(bus, event.get(), SOFTOFF_OBJPATH); |
Vishwanatha Subbanna | 917454b | 2017-02-24 00:16:05 +0530 | [diff] [blame] | 52 | |
Vishwanatha Subbanna | 31272b8 | 2017-02-01 18:53:14 +0530 | [diff] [blame] | 53 | // Wait for client requests until this application has processed |
| 54 | // at least one successful SoftPowerOff or we timed out |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 55 | while (!powerObj.isCompleted() && !powerObj.isTimerExpired()) |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 56 | { |
William A. Kennington III | 4f67cc3 | 2018-10-18 15:24:49 -0700 | [diff] [blame] | 57 | try |
| 58 | { |
| 59 | event.run(std::nullopt); |
| 60 | } |
| 61 | catch (const sdeventplus::SdEventError& e) |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 62 | { |
George Liu | ce338fd | 2024-07-17 14:22:14 +0800 | [diff] [blame] | 63 | lg2::error("Failure in processing request: {ERROR}", "ERROR", e); |
William A. Kennington III | 4f67cc3 | 2018-10-18 15:24:49 -0700 | [diff] [blame] | 64 | return 1; |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 65 | } |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 66 | } |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 67 | |
Vishwanatha Subbanna | 917454b | 2017-02-24 00:16:05 +0530 | [diff] [blame] | 68 | // Log an error if we timed out after getting Ack for SMS_ATN and before |
| 69 | // getting the Host Shutdown response |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 70 | if (powerObj.isTimerExpired() && |
| 71 | (powerObj.responseReceived() == |
| 72 | phosphor::ipmi::Base::SoftPowerOff::HostResponse::SoftOffReceived)) |
Vishwanatha Subbanna | 917454b | 2017-02-24 00:16:05 +0530 | [diff] [blame] | 73 | { |
Marri Devender Rao | 5cc0593 | 2017-04-15 06:15:19 -0500 | [diff] [blame] | 74 | using error = |
Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 75 | sdbusplus::error::xyz::openbmc_project::state::host::SoftOffTimeout; |
| 76 | using errorMetadata = xyz::openbmc_project::state::host::SoftOffTimeout; |
Marri Devender Rao | 5cc0593 | 2017-04-15 06:15:19 -0500 | [diff] [blame] | 77 | report<error>(prev_entry<errorMetadata::TIMEOUT_IN_MSEC>()); |
Andrew Geissler | 83527ef | 2017-05-18 16:54:29 -0500 | [diff] [blame] | 78 | return -1; |
Vishwanatha Subbanna | 917454b | 2017-02-24 00:16:05 +0530 | [diff] [blame] | 79 | } |
| 80 | |
Vishwanatha Subbanna | 506aa0f | 2017-01-24 14:58:25 +0530 | [diff] [blame] | 81 | return 0; |
| 82 | } |