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 | 1a7c49f | 2020-10-06 15:49:27 -0700 | [diff] [blame^] | 7 | #include <regex> |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 8 | #include <set> |
Patrick Venture | 8b4478c | 2020-10-06 08:30:27 -0700 | [diff] [blame] | 9 | #include <string> |
Patrick Venture | 1a7c49f | 2020-10-06 15:49:27 -0700 | [diff] [blame^] | 10 | #include <unordered_map> |
| 11 | #include <utility> |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 12 | #include <variant> |
Patrick Venture | 1a7c49f | 2020-10-06 15:49:27 -0700 | [diff] [blame^] | 13 | #include <vector> |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 14 | |
| 15 | using Property = std::string; |
| 16 | using Value = std::variant<int64_t, double, std::string, bool>; |
| 17 | using PropertyMap = std::map<Property, Value>; |
| 18 | |
| 19 | namespace pid_control |
| 20 | { |
| 21 | |
Patrick Venture | 1a7c49f | 2020-10-06 15:49:27 -0700 | [diff] [blame^] | 22 | bool 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 Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 38 | std::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 Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 49 | else if (type == "margin") |
| 50 | { |
| 51 | layer = "temperature"; |
| 52 | } |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 53 | else |
| 54 | { |
| 55 | layer = "unknown"; // TODO(venture): Need to handle. |
| 56 | } |
| 57 | |
| 58 | return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id); |
| 59 | } |
| 60 | |
| 61 | std::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 | |
| 70 | bool validType(const std::string& type) |
| 71 | { |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 72 | static std::set<std::string> valid = {"fan", "temp", "margin"}; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 73 | return (valid.find(type) != valid.end()); |
| 74 | } |
| 75 | |
| 76 | void scaleSensorReading(const double min, const double max, double& value) |
| 77 | { |
| 78 | if (max <= 0 || max <= min) |
| 79 | { |
| 80 | return; |
| 81 | } |
Josh Lehan | 91fe17f | 2020-09-20 22:50:26 -0700 | [diff] [blame] | 82 | value -= min; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 83 | value /= (max - min); |
| 84 | } |
| 85 | |
| 86 | } // namespace pid_control |