1 #include "util.hpp"
2 
3 #include <cmath>
4 #include <cstdint>
5 #include <iostream>
6 #include <map>
7 #include <regex>
8 #include <set>
9 #include <string>
10 #include <unordered_map>
11 #include <utility>
12 #include <variant>
13 #include <vector>
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 
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 
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     }
49     else if (type == "margin")
50     {
51         layer = "temperature";
52     }
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 {
72     static std::set<std::string> valid = {"fan", "temp", "margin"};
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     }
82     value -= min;
83     value /= (max - min);
84 }
85 
86 } // namespace pid_control
87