Add ability to configure from dbus
This allows configuration of PID control from dbus using
entity manager. Sample configuration will be pushed to
entity-manager repo shortly.
Tested-by: Used yaml configuration and dbus / entity-manager
based configuration and pid seemed to work the same.
Verified printout of configuration matched generated
cpp files.
Change-Id: Ia7b016e53262791ffcccdb9b21c1ccddae2926bc
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/Makefile.am b/Makefile.am
index 10cefda..36eeb80 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,7 +2,9 @@
sbin_PROGRAMS = swampd setsensor
-BUILT_SOURCES = sensorlist-gen.cpp pidlist-gen.cpp zoneinfo-gen.cpp
+if !CONFIGURE_DBUS
+ BUILT_SOURCES = sensorlist-gen.cpp pidlist-gen.cpp zoneinfo-gen.cpp
+endif
CLEANFILES = $(BUILT_SOURCES)
sensorlist-gen.cpp:
@@ -41,6 +43,8 @@
dbus/util.cpp \
dbus/dbuspassive.cpp \
dbus/dbusactiveread.cpp \
+ dbus/dbusconfiguration.cpp \
+ dbus/dbuswrite.cpp \
sysfs/sysfsread.cpp \
sysfs/sysfswrite.cpp \
sysfs/util.cpp \
diff --git a/configure.ac b/configure.ac
index 06c18ae..ca26b79 100644
--- a/configure.ac
+++ b/configure.ac
@@ -68,6 +68,14 @@
ZONEGEN="$PYTHON ${srcdir}/scripts/zone_gen.py -i $ZONE_YAML_GEN"
AC_SUBST(ZONEGEN)
+AC_ARG_ENABLE([configure-dbus],
+ AS_HELP_STRING([--enable-configure-dbus], [Enable configuring pid from D-Bus.]))
+AM_CONDITIONAL(CONFIGURE_DBUS, [test "x$enable_configure_dbus" = "xyes"])
+AS_IF([test "x$enable_configure_dbus" = "xyes"],
+ [AC_DEFINE(configureDbus, [1], [Read configuration from D-Bus.])],
+ [AC_DEFINE(configureDbus, [0], [Do not read configuration from D-Bus.])]
+)
+
# Create configured output
AC_CONFIG_FILES([Makefile test/Makefile])
AC_OUTPUT
diff --git a/dbus/dbusconfiguration.cpp b/dbus/dbusconfiguration.cpp
new file mode 100644
index 0000000..4c354c1
--- /dev/null
+++ b/dbus/dbusconfiguration.cpp
@@ -0,0 +1,341 @@
+/*
+// Copyright (c) 2018 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 <conf.hpp>
+#include <dbus/util.hpp>
+#include <iostream>
+#include <sdbusplus/bus.hpp>
+#include <set>
+#include <unordered_map>
+
+static constexpr bool DEBUG = false; // enable to print found configuration
+
+std::map<std::string, struct sensor> SensorConfig = {};
+std::map<int64_t, PIDConf> ZoneConfig = {};
+std::map<int64_t, struct zone> ZoneDetailsConfig = {};
+
+constexpr const char *pidConfigurationInterface =
+ "xyz.openbmc_project.Configuration.Pid";
+constexpr const char *objectManagerInterface =
+ "org.freedesktop.DBus.ObjectManager";
+constexpr const char *pidZoneConfigurationInterface =
+ "xyz.openbmc_project.Configuration.Pid.Zone";
+constexpr const char *sensorInterface = "xyz.openbmc_project.Sensor.Value";
+constexpr const char *pwmInterface = "xyz.openbmc_project.Control.FanPwm";
+
+namespace dbus_configuration
+{
+
+bool findSensor(const std::unordered_map<std::string, std::string> &sensors,
+ const std::string &search,
+ std::pair<std::string, std::string> &sensor)
+{
+ for (const auto &s : sensors)
+ {
+ if (s.first.find(search) != std::string::npos)
+ {
+ sensor = s;
+ return true;
+ }
+ }
+ return false;
+}
+
+// this function prints the configuration into a form similar to the cpp
+// generated code to help in verification, should be turned off during normal
+// use
+void debugPrint(void)
+{
+ // print sensor config
+ std::cout << "sensor config:\n";
+ std::cout << "{\n";
+ for (auto &pair : SensorConfig)
+ {
+
+ std::cout << "\t{" << pair.first << ",\n\t\t{";
+ std::cout << pair.second.type << ", ";
+ std::cout << pair.second.readpath << ", ";
+ std::cout << pair.second.writepath << ", ";
+ std::cout << pair.second.min << ", ";
+ std::cout << pair.second.max << ", ";
+ std::cout << pair.second.timeout << "},\n\t},\n";
+ }
+ std::cout << "}\n\n";
+ std::cout << "ZoneDetailsConfig\n";
+ std::cout << "{\n";
+ for (auto &zone : ZoneDetailsConfig)
+ {
+ std::cout << "\t{" << zone.first << ",\n";
+ std::cout << "\t\t{" << zone.second.minthermalrpm << ", ";
+ std::cout << zone.second.failsafepercent << "}\n\t},\n";
+ }
+ std::cout << "}\n\n";
+ std::cout << "ZoneConfig\n";
+ std::cout << "{\n";
+ for (auto &zone : ZoneConfig)
+ {
+ std::cout << "\t{" << zone.first << "\n";
+ for (auto &pidconf : zone.second)
+ {
+ std::cout << "\t\t{" << pidconf.first << ",\n";
+ std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
+ std::cout << "\t\t\t{";
+ for (auto &input : pidconf.second.inputs)
+ {
+ std::cout << "\n\t\t\t" << input << ",\n";
+ }
+ std::cout << "\t\t\t}\n";
+ std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
+ std::cout << "\t\t\t{" << pidconf.second.info.ts << ",\n";
+ std::cout << "\t\t\t" << pidconf.second.info.p_c << ",\n";
+ std::cout << "\t\t\t" << pidconf.second.info.i_c << ",\n";
+ std::cout << "\t\t\t" << pidconf.second.info.ff_off << ",\n";
+ std::cout << "\t\t\t" << pidconf.second.info.ff_gain << ",\n";
+ std::cout << "\t\t\t{" << pidconf.second.info.i_lim.min << ","
+ << pidconf.second.info.i_lim.max << "},\n";
+ std::cout << "\t\t\t{" << pidconf.second.info.out_lim.min << ","
+ << pidconf.second.info.out_lim.max << "},\n";
+ std::cout << "\t\t\t" << pidconf.second.info.slew_neg << ",\n";
+ std::cout << "\t\t\t" << pidconf.second.info.slew_pos << ",\n";
+ std::cout << "\t\t\t}\n\t\t}\n";
+ }
+ std::cout << "\t},\n";
+ }
+ std::cout << "}\n\n";
+}
+
+void init(sdbusplus::bus::bus &bus)
+{
+ using ManagedObjectType = std::unordered_map<
+ sdbusplus::message::object_path,
+ std::unordered_map<
+ std::string,
+ std::unordered_map<std::string,
+ sdbusplus::message::variant<
+ uint64_t, int64_t, double, std::string,
+ std::vector<std::string>>>>>;
+ auto mapper =
+ bus.new_method_call("xyz.openbmc_project.ObjectMapper",
+ "/xyz/openbmc_project/object_mapper",
+ "xyz.openbmc_project.ObjectMapper", "GetSubTree");
+ mapper.append("", 0,
+ std::array<const char *, 5>{objectManagerInterface,
+ pidConfigurationInterface,
+ pidZoneConfigurationInterface,
+ sensorInterface, pwmInterface});
+ auto resp = bus.call(mapper);
+ if (resp.is_method_error())
+ {
+ throw std::runtime_error("ObjectMapper Call Failure");
+ }
+ std::unordered_map<
+ std::string, std::unordered_map<std::string, std::vector<std::string>>>
+ respData;
+
+ resp.read(respData);
+ if (respData.empty())
+ {
+ throw std::runtime_error("No configuration data available from Mapper");
+ }
+ // create a map of pair of <has pid configuration, ObjectManager path>
+ std::unordered_map<std::string, std::pair<bool, std::string>> owners;
+ // and a map of <path, interface> for sensors
+ std::unordered_map<std::string, std::string> sensors;
+ for (const auto &objectPair : respData)
+ {
+ for (const auto &ownerPair : objectPair.second)
+ {
+ auto &owner = owners[ownerPair.first];
+ for (const std::string &interface : ownerPair.second)
+ {
+
+ if (interface == objectManagerInterface)
+ {
+ owner.second = objectPair.first;
+ }
+ if (interface == pidConfigurationInterface ||
+ interface == pidZoneConfigurationInterface)
+ {
+ owner.first = true;
+ }
+ if (interface == sensorInterface || interface == pwmInterface)
+ {
+ // we're not interested in pwm sensors, just pwm control
+ if (interface == sensorInterface &&
+ objectPair.first.find("pwm") != std::string::npos)
+ {
+ continue;
+ }
+ sensors[objectPair.first] = interface;
+ }
+ }
+ }
+ }
+ ManagedObjectType configurations;
+ for (const auto &owner : owners)
+ {
+ // skip if no pid configuration (means probably a sensor)
+ if (!owner.second.first)
+ {
+ continue;
+ }
+ auto endpoint = bus.new_method_call(
+ owner.first.c_str(), owner.second.second.c_str(),
+ "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
+ auto responce = bus.call(endpoint);
+ if (responce.is_method_error())
+ {
+ throw std::runtime_error("Error getting managed objects from " +
+ owner.first);
+ }
+ ManagedObjectType configuration;
+ responce.read(configuration);
+ for (auto &pathPair : configuration)
+ {
+ if (pathPair.second.find(pidConfigurationInterface) !=
+ pathPair.second.end() ||
+ pathPair.second.find(pidZoneConfigurationInterface) !=
+ pathPair.second.end())
+ {
+ configurations.emplace(pathPair);
+ }
+ }
+ }
+ for (const auto &configuration : configurations)
+ {
+ auto findZone =
+ configuration.second.find(pidZoneConfigurationInterface);
+ if (findZone != configuration.second.end())
+ {
+ const auto &zone = findZone->second;
+ auto &details =
+ ZoneDetailsConfig[sdbusplus::message::variant_ns::get<uint64_t>(
+ zone.at("Index"))];
+ details.minthermalrpm = mapbox::util::apply_visitor(
+ VariantToFloatVisitor(), zone.at("MinThermalRpm"));
+ details.failsafepercent = mapbox::util::apply_visitor(
+ VariantToFloatVisitor(), zone.at("FailSafePercent"));
+ }
+ auto findBase = configuration.second.find(pidConfigurationInterface);
+ if (findBase == configuration.second.end())
+ {
+ continue;
+ }
+ // if the base configuration is found, these are required
+ const auto &base = configuration.second.at(pidConfigurationInterface);
+ const auto &iLim = configuration.second.at(pidConfigurationInterface +
+ std::string(".ILimit"));
+ const auto &outLim = configuration.second.at(pidConfigurationInterface +
+ std::string(".OutLimit"));
+ PIDConf &conf =
+ ZoneConfig[sdbusplus::message::variant_ns::get<uint64_t>(
+ base.at("Index"))];
+ struct controller_info &info =
+ conf[sdbusplus::message::variant_ns::get<std::string>(
+ base.at("Name"))];
+ info.type =
+ sdbusplus::message::variant_ns::get<std::string>(base.at("Class"));
+ // todo: auto generation yaml -> c script seems to discard this value
+ // for fans, verify this is okay
+ if (info.type == "fan")
+ {
+ info.setpoint = 0;
+ }
+ else
+ {
+ info.setpoint = mapbox::util::apply_visitor(VariantToFloatVisitor(),
+ base.at("SetPoint"));
+ }
+ info.info.ts = 1.0; // currently unused
+ info.info.p_c = mapbox::util::apply_visitor(VariantToFloatVisitor(),
+ base.at("PCoefficient"));
+ info.info.i_c = mapbox::util::apply_visitor(VariantToFloatVisitor(),
+ base.at("ICoefficient"));
+ info.info.ff_off = mapbox::util::apply_visitor(
+ VariantToFloatVisitor(), base.at("FFOffCoefficient"));
+ info.info.ff_gain = mapbox::util::apply_visitor(
+ VariantToFloatVisitor(), base.at("FFGainCoefficient"));
+ auto value = mapbox::util::apply_visitor(VariantToFloatVisitor(),
+ iLim.at("Max"));
+ info.info.i_lim.max = value;
+ info.info.i_lim.min = mapbox::util::apply_visitor(
+ VariantToFloatVisitor(), iLim.at("Min"));
+ info.info.out_lim.max = mapbox::util::apply_visitor(
+ VariantToFloatVisitor(), outLim.at("Max"));
+ info.info.out_lim.min = mapbox::util::apply_visitor(
+ VariantToFloatVisitor(), outLim.at("Min"));
+ info.info.slew_neg = mapbox::util::apply_visitor(
+ VariantToFloatVisitor(), base.at("SlewNeg"));
+ info.info.slew_pos = mapbox::util::apply_visitor(
+ VariantToFloatVisitor(), base.at("SlewPos"));
+
+ std::pair<std::string, std::string> sensorPathIfacePair;
+ std::vector<std::string> sensorNames =
+ sdbusplus::message::variant_ns::get<std::vector<std::string>>(
+ base.at("Inputs"));
+
+ for (const std::string &sensorName : sensorNames)
+ {
+ std::string name = sensorName;
+ // replace spaces with underscores to be legal on dbus
+ std::replace(name.begin(), name.end(), ' ', '_');
+
+ if (!findSensor(sensors, name, sensorPathIfacePair))
+ {
+ throw std::runtime_error(
+ "Could not map configuration to sensor " + name);
+ }
+ if (sensorPathIfacePair.second == sensorInterface)
+ {
+ info.inputs.push_back(name);
+ auto &config = SensorConfig[name];
+ config.type = sdbusplus::message::variant_ns::get<std::string>(
+ base.at("Class"));
+ config.readpath = sensorPathIfacePair.first;
+ // todo: maybe un-hardcode this if we run into slower timeouts
+ // with sensors
+ if (config.type == "temp")
+ {
+ config.timeout = 500;
+ }
+ }
+ if (sensorPathIfacePair.second == pwmInterface)
+ {
+ // copy so we can modify it
+ for (std::string otherSensor : sensorNames)
+ {
+ if (otherSensor == sensorName)
+ {
+ continue;
+ }
+ std::replace(otherSensor.begin(), otherSensor.end(), ' ',
+ '_');
+ auto &config = SensorConfig[otherSensor];
+ config.writepath = sensorPathIfacePair.first;
+ // todo: un-hardcode this if there are fans with different
+ // ranges
+ config.max = 255;
+ config.min = 0;
+ }
+ }
+ }
+ }
+ if (DEBUG)
+ {
+ debugPrint();
+ }
+}
+} // namespace dbus_configuration
diff --git a/dbus/dbusconfiguration.hpp b/dbus/dbusconfiguration.hpp
new file mode 100644
index 0000000..9b5ef63
--- /dev/null
+++ b/dbus/dbusconfiguration.hpp
@@ -0,0 +1,24 @@
+/*
+// Copyright (c) 2018 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.
+*/
+
+#pragma once
+#include <conf.hpp>
+#include <sdbusplus/bus.hpp>
+
+namespace dbus_configuration
+{
+void init(sdbusplus::bus::bus &bus);
+} // namespace dbus_configuration
\ No newline at end of file
diff --git a/dbus/dbuswrite.cpp b/dbus/dbuswrite.cpp
new file mode 100644
index 0000000..4c045e5
--- /dev/null
+++ b/dbus/dbuswrite.cpp
@@ -0,0 +1,72 @@
+/*
+// Copyright (c) 2018 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 <dbus/dbuswrite.hpp>
+#include <iostream>
+#include <sdbusplus/bus.hpp>
+
+// this bus object is treated as a singleton because the class is constructed in
+// a different thread than it is used, and as bus objects are relatively
+// expensive we'd prefer to only have one
+std::unique_ptr<sdbusplus::bus::bus> writeBus = nullptr;
+
+void initBus()
+{
+ if (writeBus == nullptr)
+ {
+ writeBus = std::make_unique<sdbusplus::bus::bus>(
+ sdbusplus::bus::new_default());
+ }
+}
+
+void DbusWritePercent::write(double value)
+{
+ float minimum = getMin();
+ float maximum = getMax();
+
+ float range = maximum - minimum;
+ float offset = range * value;
+ float ovalue = offset + minimum;
+ initBus();
+ auto mesg =
+ writeBus->new_method_call(connectionName.c_str(), path.c_str(),
+ "org.freedesktop.DBus.Properties", "Set");
+ mesg.append(pwmInterface, "Target",
+ sdbusplus::message::variant<uint64_t>(ovalue));
+ auto resp = writeBus->call(mesg);
+ if (resp.is_method_error())
+ {
+ std::cerr << "Error sending message to " << path << "\n";
+ }
+ return;
+}
+
+void DbusWrite::write(double value)
+{
+ initBus();
+ auto mesg =
+ writeBus->new_method_call(connectionName.c_str(), path.c_str(),
+ "org.freedesktop.DBus.Properties", "Set");
+ mesg.append(pwmInterface, "Target",
+ sdbusplus::message::variant<uint64_t>(value));
+ auto resp = writeBus->call(mesg);
+ if (resp.is_method_error())
+ {
+ std::cerr << "Error sending message to " << path << "\n";
+ }
+
+ return;
+}
diff --git a/dbus/dbuswrite.hpp b/dbus/dbuswrite.hpp
new file mode 100644
index 0000000..5beebb3
--- /dev/null
+++ b/dbus/dbuswrite.hpp
@@ -0,0 +1,63 @@
+/*
+// Copyright (c) 2018 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.
+*/
+
+#pragma once
+
+#include <string>
+
+#include <dbus/util.hpp>
+#include <interfaces.hpp>
+#include <sdbusplus/bus.hpp>
+
+constexpr const char *pwmInterface = "xyz.openbmc_project.Control.FanPwm";
+
+class DbusWritePercent : public WriteInterface
+{
+ public:
+ DbusWritePercent(const std::string &path, int64_t min, int64_t max,
+ DbusHelperInterface &helper) :
+ WriteInterface(min, max),
+ path(path)
+ {
+ auto tempBus = sdbusplus::bus::new_default();
+ connectionName = helper.GetService(tempBus, pwmInterface, path);
+ }
+
+ void write(double value) override;
+
+ private:
+ std::string path;
+ std::string connectionName;
+};
+
+class DbusWrite : public WriteInterface
+{
+ public:
+ DbusWrite(const std::string &path, int64_t min, int64_t max,
+ DbusHelperInterface &helper) :
+ WriteInterface(min, max),
+ path(path)
+ {
+ auto tempBus = sdbusplus::bus::new_default();
+ connectionName = helper.GetService(tempBus, pwmInterface, path);
+ }
+
+ void write(double value) override;
+
+ private:
+ std::string path;
+ std::string connectionName;
+};
diff --git a/dbus/util.hpp b/dbus/util.hpp
index 44e137c..72cf4e3 100644
--- a/dbus/util.hpp
+++ b/dbus/util.hpp
@@ -59,3 +59,20 @@
std::string GetSensorPath(const std::string& type, const std::string& id);
std::string GetMatch(const std::string& type, const std::string& id);
bool ValidType(const std::string& type);
+
+struct VariantToFloatVisitor
+{
+ template <typename T>
+ std::enable_if_t<std::is_arithmetic<T>::value, float>
+ operator()(const T &t) const
+ {
+ return static_cast<float>(t);
+ }
+
+ template <typename T>
+ std::enable_if_t<!std::is_arithmetic<T>::value, float>
+ operator()(const T &t) const
+ {
+ throw std::invalid_argument("Cannot translate type to float");
+ }
+};
diff --git a/main.cpp b/main.cpp
index fa94dd6..75b55fd 100644
--- a/main.cpp
+++ b/main.cpp
@@ -29,6 +29,8 @@
/* Configuration. */
#include "conf.hpp"
+#include "config.h"
+#include <dbus/dbusconfiguration.hpp>
/* Misc. */
#include "util.hpp"
@@ -88,6 +90,10 @@
}
auto ModeControlBus = sdbusplus::bus::new_default();
+ if (configureDbus)
+ {
+ dbus_configuration::init(ModeControlBus);
+ }
SensorManager mgmr;
std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
diff --git a/sensors/builder.cpp b/sensors/builder.cpp
index 01b0a64..106b154 100644
--- a/sensors/builder.cpp
+++ b/sensors/builder.cpp
@@ -22,6 +22,7 @@
#include "conf.hpp"
#include "dbus/dbuspassive.hpp"
+#include "dbus/dbuswrite.hpp"
#include "interfaces.hpp"
#include "notimpl/readonly.hpp"
#include "notimpl/writeonly.hpp"
@@ -104,6 +105,23 @@
}
break;
+ case IOInterfaceType::DBUSACTIVE:
+ if (info->max > 0)
+ {
+ wi = std::make_unique<DbusWritePercent>(info->writepath,
+ info->min,
+ info->max,
+ helper);
+ }
+ else
+ {
+ wi = std::make_unique<DbusWrite>(info->writepath,
+ info->min,
+ info->max,
+ helper);
+ }
+
+ break;
default:
wi = std::make_unique<ReadOnlyNoExcept>();
break;
diff --git a/util.cpp b/util.cpp
index 1889256..ca79f35 100644
--- a/util.cpp
+++ b/util.cpp
@@ -22,6 +22,7 @@
static constexpr auto external_sensor =
"/xyz/openbmc_project/extsensors/"; // type/
static constexpr auto openbmc_sensor = "/xyz/openbmc_project/"; // type/
+static constexpr auto dbus_pwm = "/xyz/openbmc_project/control/fanpwm/";
static constexpr auto sysfs = "/sys/";
@@ -38,6 +39,11 @@
return IOInterfaceType::SYSFS;
}
+ if (path.find(dbus_pwm) != std::string::npos)
+ {
+ return IOInterfaceType::DBUSACTIVE;
+ }
+
return IOInterfaceType::UNKNOWN;
}