Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 1 | /* |
| 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. Bills | 08b2c7a | 2020-08-28 15:39:14 -0700 | [diff] [blame] | 16 | #include <boost/asio/io_service.hpp> |
Jason M. Bills | 5245ed6 | 2020-12-04 16:50:21 -0800 | [diff] [blame] | 17 | #include <error_monitors.hpp> |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 18 | #include <host_error_monitor.hpp> |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 19 | #include <sdbusplus/asio/object_server.hpp> |
Jason M. Bills | 48e5dff | 2020-06-10 13:47:47 -0700 | [diff] [blame] | 20 | |
Jason M. Bills | 48e5dff | 2020-06-10 13:47:47 -0700 | [diff] [blame] | 21 | #include <iostream> |
Jason M. Bills | d1a19f6 | 2019-08-06 11:52:58 -0700 | [diff] [blame] | 22 | #include <variant> |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 23 | |
| 24 | namespace host_error_monitor |
| 25 | { |
| 26 | static boost::asio::io_service io; |
| 27 | static std::shared_ptr<sdbusplus::asio::connection> conn; |
| 28 | |
| 29 | static bool hostOff = true; |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 30 | bool hostIsOff() |
| 31 | { |
| 32 | return hostOff; |
| 33 | } |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 34 | |
Jason M. Bills | 5245ed6 | 2020-12-04 16:50:21 -0800 | [diff] [blame] | 35 | static void init() |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 36 | { |
Jason M. Bills | 5245ed6 | 2020-12-04 16:50:21 -0800 | [diff] [blame] | 37 | // Get the current host state to prepare to start the signal monitors |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 38 | 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. Bills | 5245ed6 | 2020-12-04 16:50:21 -0800 | [diff] [blame] | 52 | |
| 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. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 58 | }, |
| 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 | |
| 64 | static 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. Bills | 2fbb9ea | 2020-06-19 14:46:54 -0700 | [diff] [blame] | 69 | "member='PropertiesChanged',arg0='xyz.openbmc_project.State.Host'", |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 70 | [](sdbusplus::message::message& msg) { |
| 71 | std::string interfaceName; |
| 72 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 73 | propertiesChanged; |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 74 | try |
| 75 | { |
| 76 | msg.read(interfaceName, propertiesChanged); |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 77 | } |
| 78 | catch (std::exception& e) |
| 79 | { |
| 80 | std::cerr << "Unable to read host state\n"; |
| 81 | return; |
| 82 | } |
Jason M. Bills | 566ccc4 | 2020-06-18 16:38:26 -0700 | [diff] [blame] | 83 | // 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. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 98 | |
Jason M. Bills | 8fa1c96 | 2020-12-10 14:33:56 -0800 | [diff] [blame] | 99 | if (!hostOff) |
Jason M. Bills | e94f5e1 | 2019-09-13 11:11:34 -0700 | [diff] [blame] | 100 | { |
Jason M. Bills | dae0e92 | 2020-12-14 17:16:41 -0800 | [diff] [blame^] | 101 | // Notify error monitors when the host turns on |
Jason M. Bills | 5245ed6 | 2020-12-04 16:50:21 -0800 | [diff] [blame] | 102 | error_monitors::sendHostOn(); |
Jason M. Bills | e94f5e1 | 2019-09-13 11:11:34 -0700 | [diff] [blame] | 103 | } |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 104 | }); |
| 105 | } |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 106 | } // namespace host_error_monitor |
| 107 | |
| 108 | int 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. Bills | c4b91f2 | 2019-11-26 17:04:50 -0800 | [diff] [blame] | 114 | // Host Error Monitor Service |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 115 | 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. Bills | 5245ed6 | 2020-12-04 16:50:21 -0800 | [diff] [blame] | 124 | // Initialize the signal monitors |
| 125 | host_error_monitor::init(); |
| 126 | |
Jason M. Bills | 1490b14 | 2019-07-01 15:48:43 -0700 | [diff] [blame] | 127 | host_error_monitor::io.run(); |
| 128 | |
| 129 | return 0; |
| 130 | } |