blob: 6cdad14d7dffda8f9eace3ef365be6cf955bb841 [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 Venture1a7c49f2020-10-06 15:49:27 -07007#include <regex>
Patrick Ventureaadb30d2020-08-10 09:17:11 -07008#include <set>
Patrick Venture8b4478c2020-10-06 08:30:27 -07009#include <string>
Patrick Venture1a7c49f2020-10-06 15:49:27 -070010#include <unordered_map>
11#include <utility>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070012#include <variant>
Patrick Venture1a7c49f2020-10-06 15:49:27 -070013#include <vector>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070014
15using Property = std::string;
16using Value = std::variant<int64_t, double, std::string, bool>;
17using PropertyMap = std::map<Property, Value>;
18
19namespace pid_control
20{
21
Patrick Venture1a7c49f2020-10-06 15:49:27 -070022bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
23 const std::string& search,
24 std::vector<std::pair<std::string, std::string>>& matches)
25{
26 std::smatch match;
27 std::regex reg(search + '$');
28 for (const auto& sensor : sensors)
29 {
30 if (std::regex_search(sensor.first, match, reg))
31 {
32 matches.push_back(sensor);
33 }
34 }
35 return matches.size() > 0;
36}
37
Patrick Ventureaadb30d2020-08-10 09:17:11 -070038std::string getSensorPath(const std::string& type, const std::string& id)
39{
40 std::string layer = type;
41 if (type == "fan")
42 {
43 layer = "fan_tach";
44 }
45 else if (type == "temp")
46 {
47 layer = "temperature";
48 }
Josh Lehan3e2f7582020-09-20 22:06:03 -070049 else if (type == "margin")
50 {
51 layer = "temperature";
52 }
Patrick Ventureaadb30d2020-08-10 09:17:11 -070053 else
54 {
55 layer = "unknown"; // TODO(venture): Need to handle.
56 }
57
58 return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id);
59}
60
61std::string getMatch(const std::string& type, const std::string& id)
62{
63 return std::string("type='signal',"
64 "interface='org.freedesktop.DBus.Properties',"
65 "member='PropertiesChanged',"
66 "path='" +
67 getSensorPath(type, id) + "'");
68}
69
70bool validType(const std::string& type)
71{
Josh Lehan3e2f7582020-09-20 22:06:03 -070072 static std::set<std::string> valid = {"fan", "temp", "margin"};
Patrick Ventureaadb30d2020-08-10 09:17:11 -070073 return (valid.find(type) != valid.end());
74}
75
76void scaleSensorReading(const double min, const double max, double& value)
77{
78 if (max <= 0 || max <= min)
79 {
80 return;
81 }
Josh Lehan91fe17f2020-09-20 22:50:26 -070082 value -= min;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070083 value /= (max - min);
84}
85
86} // namespace pid_control