Skip to content

Commit

Permalink
dbuspassive: allow scaling
Browse files Browse the repository at this point in the history
For tachs it is beneficial to deal in percent so that
multiple controllers can be used with different fan
types without having to recalculate. This starts using
the unused min and max fields to be able to scale readings.
Since max and min are commonly on the value interface, the
special value of <int64_t>::lowest() allows these to be gathered
from dbus instead of having to enter them manually.

Tested-by: Moved pid control to percent and printed
outputs

Change-Id: I9496eb92a18b68a7cd7f034d41d40ef5175c6974
Signed-off-by: James Feist <james.feist@linux.intel.com>
  • Loading branch information
feistjj committed Feb 26, 2019
1 parent 608304d commit 75eb769
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 8 deletions.
5 changes: 5 additions & 0 deletions conf.hpp
Expand Up @@ -7,6 +7,11 @@
#include <string>
#include <vector>

namespace conf
{
constexpr int64_t inheritValueFromDbus = std::numeric_limits<int64_t>::lowest();
} // namespace conf

/*
* General sensor structure used for configuration.
*/
Expand Down
5 changes: 5 additions & 0 deletions dbus/dbusconfiguration.cpp
Expand Up @@ -366,6 +366,11 @@ void init(sdbusplus::bus::bus& bus)
{
config.timeout = 0;
}
else if (config.type == "fan")
{
config.max = conf::inheritValueFromDbus;
config.min = conf::inheritValueFromDbus;
}
}
else if (sensorPathIfacePair.second == pwmInterface)
{
Expand Down
25 changes: 24 additions & 1 deletion dbus/dbuspassive.cpp
Expand Up @@ -27,7 +27,7 @@

std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
DbusHelperInterface* helper)
DbusHelperInterface* helper, const SensorConfig* info)
{
if (helper == nullptr)
{
Expand Down Expand Up @@ -58,6 +58,15 @@ std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
{
return nullptr;
}
if (info->max != conf::inheritValueFromDbus)
{
settings.max = info->max;
}

if (info->max != conf::inheritValueFromDbus)
{
settings.min = info->min;
}

return std::make_unique<DbusPassive>(bus, type, id, helper, settings,
failed);
Expand All @@ -72,6 +81,8 @@ DbusPassive::DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
{
_scale = settings.scale;
_value = settings.value * pow(10, _scale);
_min = settings.min * pow(10, _scale);
_max = settings.max * pow(10, _scale);
_updated = std::chrono::high_resolution_clock::now();
}

Expand Down Expand Up @@ -112,6 +123,16 @@ std::string DbusPassive::getID(void)
return _id;
}

double DbusPassive::getMax(void)
{
return _max;
}

double DbusPassive::getMin(void)
{
return _min;
}

int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
{
std::string msgSensor;
Expand All @@ -129,6 +150,8 @@ int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)

value *= std::pow(10, owner->getScale());

scaleSensorReading(owner->getMin(), owner->getMax(), value);

owner->setValue(value);
}
}
Expand Down
8 changes: 7 additions & 1 deletion dbus/dbuspassive.hpp
@@ -1,5 +1,6 @@
#pragma once

#include "conf.hpp"
#include "dbus/util.hpp"
#include "interfaces.hpp"

Expand Down Expand Up @@ -35,7 +36,8 @@ class DbusPassive : public ReadInterface
public:
static std::unique_ptr<ReadInterface>
createDbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
const std::string& id, DbusHelperInterface* helper);
const std::string& id, DbusHelperInterface* helper,
const SensorConfig* info);

DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
const std::string& id, DbusHelperInterface* helper,
Expand All @@ -48,6 +50,8 @@ class DbusPassive : public ReadInterface
void setFailed(bool value);
int64_t getScale(void);
std::string getID(void);
double getMax(void);
double getMin(void);

private:
sdbusplus::bus::bus& _bus;
Expand All @@ -58,6 +62,8 @@ class DbusPassive : public ReadInterface

std::mutex _lock;
double _value = 0;
double _max = 0;
double _min = 0;
bool _failed = false;
/* The last time the value was refreshed, not necessarily changed. */
std::chrono::high_resolution_clock::time_point _updated;
Expand Down
23 changes: 21 additions & 2 deletions dbus/util.cpp
Expand Up @@ -88,13 +88,23 @@ void DbusHelper::getProperties(sdbusplus::bus::bus& bus,
prop->unit = std::get<std::string>(findUnit->second);
}
auto findScale = propMap.find("Scale");
auto findMax = propMap.find("MaxValue");
auto findMin = propMap.find("MinValue");

prop->min = 0;
prop->max = 0;
prop->scale = 0;
if (findScale != propMap.end())
{
prop->scale = std::get<int64_t>(findScale->second);
}
else
if (findMax != propMap.end())
{
prop->max = std::visit(VariantToDoubleVisitor(), findMax->second);
}
if (findMin != propMap.end())
{
prop->scale = 0;
prop->min = std::visit(VariantToDoubleVisitor(), findMin->second);
}

prop->value = std::visit(VariantToDoubleVisitor(), propMap["Value"]);
Expand Down Expand Up @@ -174,3 +184,12 @@ bool validType(const std::string& type)
static std::set<std::string> valid = {"fan", "temp"};
return (valid.find(type) != valid.end());
}

void scaleSensorReading(const double min, const double max, double& value)
{
if (max <= 0)
{
return;
}
value /= (max - min);
}
3 changes: 3 additions & 0 deletions dbus/util.hpp
Expand Up @@ -7,6 +7,8 @@ struct SensorProperties
{
int64_t scale;
double value;
double min;
double max;
std::string unit;
};

Expand Down Expand Up @@ -83,6 +85,7 @@ class DbusHelper : public DbusHelperInterface

std::string getSensorPath(const std::string& type, const std::string& id);
std::string getMatch(const std::string& type, const std::string& id);
void scaleSensorReading(const double min, const double max, double& value);
bool validType(const std::string& type);

struct VariantToDoubleVisitor
Expand Down
4 changes: 2 additions & 2 deletions sensors/builder.cpp
Expand Up @@ -67,8 +67,8 @@ SensorManager
switch (rtype)
{
case IOInterfaceType::DBUSPASSIVE:
ri = DbusPassive::createDbusPassive(passiveListeningBus,
info->type, name, &helper);
ri = DbusPassive::createDbusPassive(
passiveListeningBus, info->type, name, &helper, info);
if (ri == nullptr)
{
throw SensorBuildException(
Expand Down
7 changes: 5 additions & 2 deletions test/dbus_passive_unittest.cpp
@@ -1,3 +1,4 @@
#include "conf.hpp"
#include "dbus/dbuspassive.hpp"
#include "test/dbushelper_mock.hpp"

Expand Down Expand Up @@ -28,9 +29,10 @@ TEST(DbusPassiveTest, FactoryFailsWithInvalidType)
std::string id = "id";

DbusHelperMock helper;
auto info = SensorConfig();

std::unique_ptr<ReadInterface> ri =
DbusPassive::createDbusPassive(bus_mock, type, id, &helper);
DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info);

EXPECT_EQ(ri, nullptr);
}
Expand Down Expand Up @@ -74,7 +76,8 @@ class DbusPassiveTestObj : public ::testing::Test
EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
.WillOnce(Return(false));

ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper);
auto info = SensorConfig();
ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info);
passive = reinterpret_cast<DbusPassive*>(ri.get());
EXPECT_FALSE(passive == nullptr);
}
Expand Down

0 comments on commit 75eb769

Please sign in to comment.