blob: 28efec67e444539ec62cdc4a4fed5b9c8101e124 [file] [log] [blame]
/*
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include "utils.hpp"
#include <boost/container/flat_map.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <sdbusplus/bus/match.hpp>
#include <filesystem>
#include <fstream>
#include <memory>
#include <regex>
#include <stdexcept>
#include <string>
#include <utility>
#include <variant>
#include <vector>
static bool powerStatusOn = false;
static bool biosHasPost = false;
static bool chassisStatusOn = false;
static std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
static std::unique_ptr<sdbusplus::bus::match_t> postMatch = nullptr;
static std::unique_ptr<sdbusplus::bus::match_t> chassisMatch = nullptr;
std::list<PowerCallbackEntry::callback_t> PowerCallbackEntry::list;
bool isPowerOn()
{
if (!powerMatch)
{
throw std::runtime_error("Power Match Not Created");
}
return powerStatusOn;
}
bool hasBiosPost()
{
if (!postMatch)
{
throw std::runtime_error("Post Match Not Created");
}
return biosHasPost;
}
bool isChassisOn() {
if (!chassisMatch)
{
throw std::runtime_error("Chassis On Match Not Created");
}
return chassisStatusOn;
}
bool readingStateGood(const PowerState& powerState)
{
if (powerState == PowerState::on && !isPowerOn())
{
return false;
}
if (powerState == PowerState::biosPost && (!hasBiosPost() || !isPowerOn()))
{
return false;
}
if (powerState == PowerState::chassisOn && !isChassisOn())
{
return false;
}
return true;
}
static void
getPowerStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
size_t retries = 2)
{
conn->async_method_call(
[conn, retries](boost::system::error_code ec,
const std::variant<std::string>& state) {
if (ec)
{
if (retries != 0U)
{
auto timer = std::make_shared<boost::asio::steady_timer>(
conn->get_io_context());
timer->expires_after(std::chrono::seconds(15));
timer->async_wait(
[timer, conn, retries](boost::system::error_code) {
getPowerStatus(conn, retries - 1);
});
return;
}
// we commonly come up before power control, we'll capture the
// property change later
std::cerr << "error getting power status " << ec.message()
<< "\n";
return;
}
powerStatusOn = std::get<std::string>(state).ends_with(".Running");
},
power::busname, power::path, properties::interface, properties::get,
power::interface, power::property);
}
static void
getPostStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
size_t retries = 2)
{
conn->async_method_call(
[conn, retries](boost::system::error_code ec,
const std::variant<std::string>& state) {
if (ec)
{
if (retries != 0U)
{
auto timer = std::make_shared<boost::asio::steady_timer>(
conn->get_io_context());
timer->expires_after(std::chrono::seconds(15));
timer->async_wait(
[timer, conn, retries](boost::system::error_code) {
getPostStatus(conn, retries - 1);
});
return;
}
// we commonly come up before power control, we'll capture the
// property change later
std::cerr << "error getting post status " << ec.message()
<< "\n";
return;
}
const auto& value = std::get<std::string>(state);
biosHasPost = (value != "Inactive") &&
(value != "xyz.openbmc_project.State.OperatingSystem."
"Status.OSStatus.Inactive");
},
post::busname, post::path, properties::interface, properties::get,
post::interface, post::property);
}
static void
getChassisStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
size_t retries = 2)
{
conn->async_method_call(
[conn, retries](boost::system::error_code ec,
const std::variant<std::string>& state) {
if (ec)
{
if (retries != 0U)
{
auto timer = std::make_shared<boost::asio::steady_timer>(
conn->get_io_context());
timer->expires_after(std::chrono::seconds(15));
timer->async_wait(
[timer, conn, retries](boost::system::error_code) {
getChassisStatus(conn, retries - 1);
});
return;
}
// we commonly come up before power control, we'll capture the
// property change later
std::cerr << "error getting chassis power status "
<< ec.message() << "\n";
return;
}
chassisStatusOn =
std::get<std::string>(state).ends_with(chassis::sOn);
},
chassis::busname, chassis::path, properties::interface, properties::get,
chassis::interface, chassis::property);
}
std::unique_ptr<PowerCallbackEntry> setupPowerMatchCallback(
const std::shared_ptr<sdbusplus::asio::connection>& conn,
std::function<void(PowerState type, bool state)>&& hostStatusCallback)
{
static boost::asio::steady_timer timer(conn->get_io_context());
static boost::asio::steady_timer timerChassisOn(conn->get_io_context());
auto entry =
std::make_unique<PowerCallbackEntry>(std::move(hostStatusCallback));
// create a match for powergood changes, first time do a method call to
// cache the correct value
if (powerMatch)
{
return entry;
}
powerMatch = std::make_unique<sdbusplus::bus::match_t>(
static_cast<sdbusplus::bus_t&>(*conn),
"type='signal',interface='" + std::string(properties::interface) +
"',path='" + std::string(power::path) + "',arg0='" +
std::string(power::interface) + "'",
[](sdbusplus::message_t& message) {
std::string objectName;
boost::container::flat_map<std::string, std::variant<std::string>>
values;
message.read(objectName, values);
auto findState = values.find(power::property);
if (findState != values.end())
{
bool on = std::get<std::string>(findState->second)
.ends_with(".Running");
if (!on)
{
timer.cancel();
powerStatusOn = false;
for (const auto& cb : PowerCallbackEntry::list)
{
if (cb)
{
cb(PowerState::on, powerStatusOn);
}
}
return;
}
// on comes too quickly
timer.expires_after(std::chrono::seconds(10));
timer.async_wait([](boost::system::error_code ec) {
if (ec == boost::asio::error::operation_aborted)
{
return;
}
if (ec)
{
std::cerr << "Timer error " << ec.message() << "\n";
return;
}
powerStatusOn = true;
for (const auto& cb : PowerCallbackEntry::list)
{
if (cb)
{
cb(PowerState::on, powerStatusOn);
}
}
});
}
});
postMatch = std::make_unique<sdbusplus::bus::match_t>(
static_cast<sdbusplus::bus_t&>(*conn),
"type='signal',interface='" + std::string(properties::interface) +
"',path='" + std::string(post::path) + "',arg0='" +
std::string(post::interface) + "'",
[](sdbusplus::message_t& message) {
std::string objectName;
boost::container::flat_map<std::string, std::variant<std::string>>
values;
message.read(objectName, values);
auto findState = values.find(post::property);
if (findState != values.end())
{
auto& value = std::get<std::string>(findState->second);
biosHasPost =
(value != "Inactive") &&
(value != "xyz.openbmc_project.State.OperatingSystem."
"Status.OSStatus.Inactive");
for (const auto& cb : PowerCallbackEntry::list)
{
if (cb)
{
cb(PowerState::biosPost, biosHasPost);
}
}
}
});
chassisMatch = std::make_unique<sdbusplus::bus::match_t>(
static_cast<sdbusplus::bus_t&>(*conn),
"type='signal',interface='" + std::string(properties::interface) +
"',path='" + std::string(chassis::path) + "',arg0='" +
std::string(chassis::interface) + "'",
[](sdbusplus::message_t& message) {
std::string objectName;
boost::container::flat_map<std::string, std::variant<std::string>>
values;
message.read(objectName, values);
auto findState = values.find(chassis::property);
if (findState != values.end())
{
bool on = std::get<std::string>(findState->second)
.ends_with(chassis::sOn);
if (!on)
{
timerChassisOn.cancel();
chassisStatusOn = false;
for (const auto& cb : PowerCallbackEntry::list)
{
if (cb)
{
cb(PowerState::chassisOn, chassisStatusOn);
}
}
return;
}
// on comes too quickly
timerChassisOn.expires_after(std::chrono::seconds(10));
timerChassisOn.async_wait([](boost::system::error_code ec) {
if (ec == boost::asio::error::operation_aborted)
{
return;
}
if (ec)
{
std::cerr << "Timer error " << ec.message() << "\n";
return;
}
chassisStatusOn = true;
for (const auto& cb : PowerCallbackEntry::list)
{
if (cb)
{
cb(PowerState::chassisOn, chassisStatusOn);
}
}
});
}
});
getPowerStatus(conn);
getPostStatus(conn);
getChassisStatus(conn);
return entry;
}
void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
{
setupPowerMatchCallback(conn, [](PowerState, bool) {});
}
std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
setupPropertiesChangedMatches(
sdbusplus::asio::connection& bus, std::span<const char* const> types,
const std::function<void(sdbusplus::message_t&)>& handler)
{
std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
for (const char* type : types)
{
auto match = std::make_unique<sdbusplus::bus::match_t>(
static_cast<sdbusplus::bus_t&>(bus),
"type='signal',member='PropertiesChanged',path_namespace='" +
std::string(inventoryPath) + "',arg0namespace='" +
configInterfaceName(type) + "'",
handler);
matches.emplace_back(std::move(match));
}
return matches;
}