blob: 3a47cc403985ae7f89fcc4824593fb7ab15d82fc [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 Subbannabcb76882017-01-25 16:29:43 +053016#include <systemd/sd-event.h>
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053017#include <phosphor-logging/elog.hpp>
18#include <phosphor-logging/elog-errors.hpp>
19#include <xyz/openbmc_project/State/Host/error.hpp>
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053020#include "softoff.hpp"
21#include "config.h"
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053022#include "timer.hpp"
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053023
Andrew Geissler83527ef2017-05-18 16:54:29 -050024// Return -1 on any errors to ensure we follow the calling targets OnFailure=
25// path
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053026int main(int argc, char** argv)
27{
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053028 using namespace phosphor::logging;
29
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053030 // systemd event handler
31 sd_event* events = nullptr;
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.
37 sdbusplus::server::manager::manager(bus, SOFTOFF_OBJPATH);
38
Andrew Geissler83527ef2017-05-18 16:54:29 -050039 // sd_event object
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053040 auto r = sd_event_default(&events);
41 if (r < 0)
42 {
43 log<level::ERR>("Failure to create sd_event handler",
Aditya Saripalli5fb14602017-11-09 14:46:27 +053044 entry("ERRNO=0x%X", -r));
Andrew Geissler83527ef2017-05-18 16:54:29 -050045 return -1;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053046 }
47
48 // Attach the bus to sd_event to service user requests
49 bus.attach_event(events, SD_EVENT_PRIORITY_NORMAL);
50
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053051 // Claim the bus. Delaying it until sending SMS_ATN may result
52 // in a race condition between this available and IPMI trying to send
Gunnar Mills8991dd62017-10-25 17:11:29 -050053 // message as a response to ack from host.
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053054 bus.request_name(SOFTOFF_BUSNAME);
55
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053056 // Create the SoftPowerOff object.
57 phosphor::ipmi::SoftPowerOff powerObj(bus, events, SOFTOFF_OBJPATH);
58
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053059 // Wait for client requests until this application has processed
60 // at least one successful SoftPowerOff or we timed out
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053061 while(!powerObj.isCompleted() && !powerObj.isTimerExpired())
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053062 {
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053063 // -1 denotes wait for ever
64 r = sd_event_run(events, (uint64_t)-1);
65 if (r < 0)
66 {
67 log<level::ERR>("Failure in processing request",
Aditya Saripalli5fb14602017-11-09 14:46:27 +053068 entry("ERRNO=0x%X", -r));
Andrew Geissler83527ef2017-05-18 16:54:29 -050069 return -1;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053070 }
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053071 }
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053072
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053073 // Log an error if we timed out after getting Ack for SMS_ATN and before
74 // getting the Host Shutdown response
75 if(powerObj.isTimerExpired() && (powerObj.responseReceived() ==
76 phosphor::ipmi::Base::SoftPowerOff::HostResponse::SoftOffReceived))
77 {
Marri Devender Rao5cc05932017-04-15 06:15:19 -050078 using error =
79 sdbusplus::xyz::openbmc_project::State::Host::Error::SoftOffTimeout;
80 using errorMetadata = xyz::openbmc_project::State::Host::SoftOffTimeout;
81 report<error>(prev_entry<errorMetadata::TIMEOUT_IN_MSEC>());
Andrew Geissler83527ef2017-05-18 16:54:29 -050082 return -1;
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053083 }
84
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053085 // Cleanup the event handler
86 events = sd_event_unref(events);
87
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053088 return 0;
89}