blob: d01eac9beaec95248c162195a330dad72ee1ea12 [file] [log] [blame]
Jason M. Bills1490b142019-07-01 15:48:43 -07001/*
2// Copyright (c) 2019 Intel 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*/
Jason M. Bills08b2c7a2020-08-28 15:39:14 -070016#include <boost/asio/io_service.hpp>
Jason M. Bills5245ed62020-12-04 16:50:21 -080017#include <error_monitors.hpp>
Jason M. Billsd711cc82020-12-04 16:46:39 -080018#include <host_error_monitor.hpp>
Jason M. Bills1490b142019-07-01 15:48:43 -070019#include <sdbusplus/asio/object_server.hpp>
Jason M. Bills48e5dff2020-06-10 13:47:47 -070020
Jason M. Bills48e5dff2020-06-10 13:47:47 -070021#include <iostream>
Jason M. Billsd1a19f62019-08-06 11:52:58 -070022#include <variant>
Jason M. Bills1490b142019-07-01 15:48:43 -070023
24namespace host_error_monitor
25{
26static boost::asio::io_service io;
27static std::shared_ptr<sdbusplus::asio::connection> conn;
28
29static bool hostOff = true;
Jason M. Billsd711cc82020-12-04 16:46:39 -080030bool hostIsOff()
31{
32 return hostOff;
33}
Jason M. Bills1490b142019-07-01 15:48:43 -070034
Jason M. Bills5245ed62020-12-04 16:50:21 -080035static void init()
Jason M. Bills1490b142019-07-01 15:48:43 -070036{
Jason M. Bills5245ed62020-12-04 16:50:21 -080037 // Get the current host state to prepare to start the signal monitors
Jason M. Bills1490b142019-07-01 15:48:43 -070038 conn->async_method_call(
39 [](boost::system::error_code ec,
40 const std::variant<std::string>& property) {
41 if (ec)
42 {
43 return;
44 }
45 const std::string* state = std::get_if<std::string>(&property);
46 if (state == nullptr)
47 {
48 std::cerr << "Unable to read host state value\n";
49 return;
50 }
51 hostOff = *state == "xyz.openbmc_project.State.Host.HostState.Off";
Jason M. Bills5245ed62020-12-04 16:50:21 -080052
53 // Now we have the host state, start the signal monitors
54 if (!error_monitors::startMonitors(io, conn))
55 {
56 throw std::runtime_error("Failed to start signal monitors");
57 }
Jason M. Bills1490b142019-07-01 15:48:43 -070058 },
59 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
60 "org.freedesktop.DBus.Properties", "Get",
61 "xyz.openbmc_project.State.Host", "CurrentHostState");
62}
63
64static std::shared_ptr<sdbusplus::bus::match::match> startHostStateMonitor()
65{
66 return std::make_shared<sdbusplus::bus::match::match>(
67 *conn,
68 "type='signal',interface='org.freedesktop.DBus.Properties',"
Jason M. Bills2fbb9ea2020-06-19 14:46:54 -070069 "member='PropertiesChanged',arg0='xyz.openbmc_project.State.Host'",
Jason M. Bills1490b142019-07-01 15:48:43 -070070 [](sdbusplus::message::message& msg) {
71 std::string interfaceName;
72 boost::container::flat_map<std::string, std::variant<std::string>>
73 propertiesChanged;
Jason M. Bills1490b142019-07-01 15:48:43 -070074 try
75 {
76 msg.read(interfaceName, propertiesChanged);
Jason M. Bills1490b142019-07-01 15:48:43 -070077 }
78 catch (std::exception& e)
79 {
80 std::cerr << "Unable to read host state\n";
81 return;
82 }
Jason M. Bills566ccc42020-06-18 16:38:26 -070083 // We only want to check for CurrentHostState
84 if (propertiesChanged.begin()->first != "CurrentHostState")
85 {
86 return;
87 }
88 std::string* state =
89 std::get_if<std::string>(&(propertiesChanged.begin()->second));
90 if (state == nullptr)
91 {
92 std::cerr << propertiesChanged.begin()->first
93 << " property invalid\n";
94 return;
95 }
96
97 hostOff = *state == "xyz.openbmc_project.State.Host.HostState.Off";
Jason M. Bills1490b142019-07-01 15:48:43 -070098
Jason M. Bills8fa1c962020-12-10 14:33:56 -080099 if (!hostOff)
Jason M. Billse94f5e12019-09-13 11:11:34 -0700100 {
Jason M. Billsdae0e922020-12-14 17:16:41 -0800101 // Notify error monitors when the host turns on
Jason M. Bills5245ed62020-12-04 16:50:21 -0800102 error_monitors::sendHostOn();
Jason M. Billse94f5e12019-09-13 11:11:34 -0700103 }
Jason M. Bills1490b142019-07-01 15:48:43 -0700104 });
105}
Jason M. Bills1490b142019-07-01 15:48:43 -0700106} // namespace host_error_monitor
107
108int main(int argc, char* argv[])
109{
110 // setup connection to dbus
111 host_error_monitor::conn =
112 std::make_shared<sdbusplus::asio::connection>(host_error_monitor::io);
113
Jason M. Billsc4b91f22019-11-26 17:04:50 -0800114 // Host Error Monitor Service
Jason M. Bills1490b142019-07-01 15:48:43 -0700115 host_error_monitor::conn->request_name(
116 "xyz.openbmc_project.HostErrorMonitor");
117 sdbusplus::asio::object_server server =
118 sdbusplus::asio::object_server(host_error_monitor::conn);
119
120 // Start tracking host state
121 std::shared_ptr<sdbusplus::bus::match::match> hostStateMonitor =
122 host_error_monitor::startHostStateMonitor();
123
Jason M. Bills5245ed62020-12-04 16:50:21 -0800124 // Initialize the signal monitors
125 host_error_monitor::init();
126
Jason M. Bills1490b142019-07-01 15:48:43 -0700127 host_error_monitor::io.run();
128
129 return 0;
130}