Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 1 | #include "util.hpp" |
| 2 | |
| 3 | #include <cmath> |
Patrick Venture | 8b4478c | 2020-10-06 08:30:27 -0700 | [diff] [blame^] | 4 | #include <cstdint> |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 5 | #include <iostream> |
Patrick Venture | 8b4478c | 2020-10-06 08:30:27 -0700 | [diff] [blame^] | 6 | #include <map> |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 7 | #include <set> |
Patrick Venture | 8b4478c | 2020-10-06 08:30:27 -0700 | [diff] [blame^] | 8 | #include <string> |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 9 | #include <variant> |
| 10 | |
| 11 | using Property = std::string; |
| 12 | using Value = std::variant<int64_t, double, std::string, bool>; |
| 13 | using PropertyMap = std::map<Property, Value>; |
| 14 | |
| 15 | namespace pid_control |
| 16 | { |
| 17 | |
| 18 | std::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 Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 29 | else if (type == "margin") |
| 30 | { |
| 31 | layer = "temperature"; |
| 32 | } |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 33 | else |
| 34 | { |
| 35 | layer = "unknown"; // TODO(venture): Need to handle. |
| 36 | } |
| 37 | |
| 38 | return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id); |
| 39 | } |
| 40 | |
| 41 | std::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 | |
| 50 | bool validType(const std::string& type) |
| 51 | { |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 52 | static std::set<std::string> valid = {"fan", "temp", "margin"}; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 53 | return (valid.find(type) != valid.end()); |
| 54 | } |
| 55 | |
| 56 | void scaleSensorReading(const double min, const double max, double& value) |
| 57 | { |
| 58 | if (max <= 0 || max <= min) |
| 59 | { |
| 60 | return; |
| 61 | } |
Josh Lehan | 91fe17f | 2020-09-20 22:50:26 -0700 | [diff] [blame] | 62 | value -= min; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 63 | value /= (max - min); |
| 64 | } |
| 65 | |
| 66 | } // namespace pid_control |