blob: 7545eed0ee56d5f21e8a3c8571892eed047a3d7a [file] [log] [blame]
Patrick Ventureaadb30d2020-08-10 09:17:11 -07001#include "util.hpp"
2
3#include <cmath>
Patrick Venture8b4478c2020-10-06 08:30:27 -07004#include <cstdint>
Patrick Ventureaadb30d2020-08-10 09:17:11 -07005#include <iostream>
Patrick Venture8b4478c2020-10-06 08:30:27 -07006#include <map>
Patrick Ventureaadb30d2020-08-10 09:17:11 -07007#include <set>
Patrick Venture8b4478c2020-10-06 08:30:27 -07008#include <string>
Patrick Ventureaadb30d2020-08-10 09:17:11 -07009#include <variant>
10
11using Property = std::string;
12using Value = std::variant<int64_t, double, std::string, bool>;
13using PropertyMap = std::map<Property, Value>;
14
15namespace pid_control
16{
17
18std::string getSensorPath(const std::string& type, const std::string& id)
19{
20 std::string layer = type;
21 if (type == "fan")
22 {
23 layer = "fan_tach";
24 }
25 else if (type == "temp")
26 {
27 layer = "temperature";
28 }
Josh Lehan3e2f7582020-09-20 22:06:03 -070029 else if (type == "margin")
30 {
31 layer = "temperature";
32 }
Patrick Ventureaadb30d2020-08-10 09:17:11 -070033 else
34 {
35 layer = "unknown"; // TODO(venture): Need to handle.
36 }
37
38 return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id);
39}
40
41std::string getMatch(const std::string& type, const std::string& id)
42{
43 return std::string("type='signal',"
44 "interface='org.freedesktop.DBus.Properties',"
45 "member='PropertiesChanged',"
46 "path='" +
47 getSensorPath(type, id) + "'");
48}
49
50bool validType(const std::string& type)
51{
Josh Lehan3e2f7582020-09-20 22:06:03 -070052 static std::set<std::string> valid = {"fan", "temp", "margin"};
Patrick Ventureaadb30d2020-08-10 09:17:11 -070053 return (valid.find(type) != valid.end());
54}
55
56void scaleSensorReading(const double min, const double max, double& value)
57{
58 if (max <= 0 || max <= min)
59 {
60 return;
61 }
Josh Lehan91fe17f2020-09-20 22:50:26 -070062 value -= min;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070063 value /= (max - min);
64}
65
66} // namespace pid_control