blob: 07916159f786df56fe0340439f0329878336946e [file] [log] [blame]
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +05301/**
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 Subbanna506aa0f2017-01-24 14:58:25 +053016#include "config.h"
Patrick Venture0b02be92018-08-31 11:55:55 -070017
18#include "softoff.hpp"
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053019
Patrick Venture0b02be92018-08-31 11:55:55 -070020#include <phosphor-logging/elog-errors.hpp>
21#include <phosphor-logging/elog.hpp>
George Liuce338fd2024-07-17 14:22:14 +080022#include <phosphor-logging/lg2.hpp>
William A. Kennington III4f67cc32018-10-18 15:24:49 -070023#include <sdeventplus/event.hpp>
24#include <sdeventplus/exception.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070025#include <xyz/openbmc_project/State/Host/error.hpp>
26
Andrew Geissler83527ef2017-05-18 16:54:29 -050027// Return -1 on any errors to ensure we follow the calling targets OnFailure=
28// path
Willy Tu11d68892022-01-20 10:37:34 -080029int main(int, char**)
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053030{
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053031 using namespace phosphor::logging;
32
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053033 // Get a handle to system dbus.
34 auto bus = sdbusplus::bus::new_default();
35
36 // Add systemd object manager.
Patrick Williams5d82f472022-07-22 19:26:53 -050037 sdbusplus::server::manager_t(bus, SOFTOFF_OBJPATH);
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053038
William A. Kennington III4f67cc32018-10-18 15:24:49 -070039 // Get default event loop
40 auto event = sdeventplus::Event::get_default();
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053041
42 // Attach the bus to sd_event to service user requests
William A. Kennington III4f67cc32018-10-18 15:24:49 -070043 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053044
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053045 // 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 Mills8991dd62017-10-25 17:11:29 -050047 // message as a response to ack from host.
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053048 bus.request_name(SOFTOFF_BUSNAME);
49
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053050 // Create the SoftPowerOff object.
William A. Kennington III4f67cc32018-10-18 15:24:49 -070051 phosphor::ipmi::SoftPowerOff powerObj(bus, event.get(), SOFTOFF_OBJPATH);
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053052
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053053 // Wait for client requests until this application has processed
54 // at least one successful SoftPowerOff or we timed out
Patrick Venture0b02be92018-08-31 11:55:55 -070055 while (!powerObj.isCompleted() && !powerObj.isTimerExpired())
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053056 {
William A. Kennington III4f67cc32018-10-18 15:24:49 -070057 try
58 {
59 event.run(std::nullopt);
60 }
61 catch (const sdeventplus::SdEventError& e)
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053062 {
George Liuce338fd2024-07-17 14:22:14 +080063 lg2::error("Failure in processing request: {ERROR}", "ERROR", e);
William A. Kennington III4f67cc32018-10-18 15:24:49 -070064 return 1;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053065 }
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053066 }
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053067
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053068 // Log an error if we timed out after getting Ack for SMS_ATN and before
69 // getting the Host Shutdown response
Patrick Venture0b02be92018-08-31 11:55:55 -070070 if (powerObj.isTimerExpired() &&
71 (powerObj.responseReceived() ==
72 phosphor::ipmi::Base::SoftPowerOff::HostResponse::SoftOffReceived))
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053073 {
Marri Devender Rao5cc05932017-04-15 06:15:19 -050074 using error =
Willy Tu523e2d12023-09-05 11:36:48 -070075 sdbusplus::error::xyz::openbmc_project::state::host::SoftOffTimeout;
76 using errorMetadata = xyz::openbmc_project::state::host::SoftOffTimeout;
Marri Devender Rao5cc05932017-04-15 06:15:19 -050077 report<error>(prev_entry<errorMetadata::TIMEOUT_IN_MSEC>());
Andrew Geissler83527ef2017-05-18 16:54:29 -050078 return -1;
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053079 }
80
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053081 return 0;
82}