xref: /openbmc/phosphor-pid-control/dbus/dbusutil.cpp (revision 90b0a66d9d95247ef269b3657a5b09c6ec8b3658)
1aadb30ddSPatrick Venture #include "util.hpp"
2aadb30ddSPatrick Venture 
3*90b0a66dSPatrick Williams #include <sdbusplus/bus/match.hpp>
4*90b0a66dSPatrick Williams 
5aadb30ddSPatrick Venture #include <cmath>
68b4478ccSPatrick Venture #include <cstdint>
7aadb30ddSPatrick Venture #include <iostream>
88b4478ccSPatrick Venture #include <map>
91a7c49f9SPatrick Venture #include <regex>
10aadb30ddSPatrick Venture #include <set>
118b4478ccSPatrick Venture #include <string>
121a7c49f9SPatrick Venture #include <unordered_map>
131a7c49f9SPatrick Venture #include <utility>
14aadb30ddSPatrick Venture #include <variant>
151a7c49f9SPatrick Venture #include <vector>
16aadb30ddSPatrick Venture 
17aadb30ddSPatrick Venture using Property = std::string;
18aadb30ddSPatrick Venture using Value = std::variant<int64_t, double, std::string, bool>;
19aadb30ddSPatrick Venture using PropertyMap = std::map<Property, Value>;
20aadb30ddSPatrick Venture 
21aadb30ddSPatrick Venture namespace pid_control
22aadb30ddSPatrick Venture {
23aadb30ddSPatrick Venture 
setZoneIndex(const std::string & name,std::map<std::string,int64_t> & zones,int64_t index)24b8cfc642SPatrick Venture int64_t setZoneIndex(const std::string& name,
25b8cfc642SPatrick Venture                      std::map<std::string, int64_t>& zones, int64_t index)
26b8cfc642SPatrick Venture {
27b8cfc642SPatrick Venture     auto it = zones.find(name);
28b8cfc642SPatrick Venture     if (it != zones.end())
29b8cfc642SPatrick Venture     {
30b8cfc642SPatrick Venture         // Name already allocated, make no change, return existing
31b8cfc642SPatrick Venture         return it->second;
32b8cfc642SPatrick Venture     }
33b8cfc642SPatrick Venture 
34b8cfc642SPatrick Venture     // The zone name is known not to exist yet
35b8cfc642SPatrick Venture     for (;;)
36b8cfc642SPatrick Venture     {
37b8cfc642SPatrick Venture         bool usedIndex = false;
38b8cfc642SPatrick Venture 
39b8cfc642SPatrick Venture         // See if desired index number is free
40b8cfc642SPatrick Venture         for (const auto& zi : zones)
41b8cfc642SPatrick Venture         {
42b8cfc642SPatrick Venture             if (index == zi.second)
43b8cfc642SPatrick Venture             {
44b8cfc642SPatrick Venture                 usedIndex = true;
45b8cfc642SPatrick Venture                 break;
46b8cfc642SPatrick Venture             }
47b8cfc642SPatrick Venture         }
48b8cfc642SPatrick Venture 
49b8cfc642SPatrick Venture         // Increment until a free index number is found
50b8cfc642SPatrick Venture         if (usedIndex)
51b8cfc642SPatrick Venture         {
52b8cfc642SPatrick Venture             ++index;
53b8cfc642SPatrick Venture             continue;
54b8cfc642SPatrick Venture         }
55b8cfc642SPatrick Venture 
56b8cfc642SPatrick Venture         break;
57b8cfc642SPatrick Venture     }
58b8cfc642SPatrick Venture 
59b8cfc642SPatrick Venture     // Allocate and return new zone index number for this name
60b8cfc642SPatrick Venture     zones[name] = index;
61b8cfc642SPatrick Venture     return index;
62b8cfc642SPatrick Venture }
63b8cfc642SPatrick Venture 
getZoneIndex(const std::string & name,std::map<std::string,int64_t> & zones)64b8cfc642SPatrick Venture int64_t getZoneIndex(const std::string& name,
65b8cfc642SPatrick Venture                      std::map<std::string, int64_t>& zones)
66b8cfc642SPatrick Venture {
67b8cfc642SPatrick Venture     auto it = zones.find(name);
68b8cfc642SPatrick Venture     if (it != zones.end())
69b8cfc642SPatrick Venture     {
70b8cfc642SPatrick Venture         return it->second;
71b8cfc642SPatrick Venture     }
72b8cfc642SPatrick Venture 
73b8cfc642SPatrick Venture     // Auto-assign next unused zone number, using 0-based numbering
74b8cfc642SPatrick Venture     return setZoneIndex(name, zones, 0);
75b8cfc642SPatrick Venture }
76b8cfc642SPatrick Venture 
findSensors(const std::unordered_map<std::string,std::string> & sensors,const std::string & search,std::vector<std::pair<std::string,std::string>> & matches)771a7c49f9SPatrick Venture bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
781a7c49f9SPatrick Venture                  const std::string& search,
791a7c49f9SPatrick Venture                  std::vector<std::pair<std::string, std::string>>& matches)
801a7c49f9SPatrick Venture {
811a7c49f9SPatrick Venture     std::smatch match;
827a8d5a17SJae Hyun Yoo     std::regex reg('/' + search + '$');
831a7c49f9SPatrick Venture     for (const auto& sensor : sensors)
841a7c49f9SPatrick Venture     {
851a7c49f9SPatrick Venture         if (std::regex_search(sensor.first, match, reg))
861a7c49f9SPatrick Venture         {
871a7c49f9SPatrick Venture             matches.push_back(sensor);
881a7c49f9SPatrick Venture         }
891a7c49f9SPatrick Venture     }
901a7c49f9SPatrick Venture     return matches.size() > 0;
911a7c49f9SPatrick Venture }
921a7c49f9SPatrick Venture 
getSensorPath(const std::string & type,const std::string & id)93aadb30ddSPatrick Venture std::string getSensorPath(const std::string& type, const std::string& id)
94aadb30ddSPatrick Venture {
9522579ca4SHarvey Wu     std::string layer;
96aadb30ddSPatrick Venture     if (type == "fan")
97aadb30ddSPatrick Venture     {
98aadb30ddSPatrick Venture         layer = "fan_tach";
99aadb30ddSPatrick Venture     }
100aadb30ddSPatrick Venture     else if (type == "temp")
101aadb30ddSPatrick Venture     {
102aadb30ddSPatrick Venture         layer = "temperature";
103aadb30ddSPatrick Venture     }
1043e2f7581SJosh Lehan     else if (type == "margin")
1053e2f7581SJosh Lehan     {
1063e2f7581SJosh Lehan         layer = "temperature";
1073e2f7581SJosh Lehan     }
10823e22b90SJosh Lehan     else if (type == "power")
10923e22b90SJosh Lehan     {
11023e22b90SJosh Lehan         layer = "power";
11123e22b90SJosh Lehan     }
11223e22b90SJosh Lehan     else if (type == "powersum")
11323e22b90SJosh Lehan     {
11423e22b90SJosh Lehan         layer = "power";
11523e22b90SJosh Lehan     }
116aadb30ddSPatrick Venture     else
117aadb30ddSPatrick Venture     {
118aadb30ddSPatrick Venture         layer = "unknown"; // TODO(venture): Need to handle.
119aadb30ddSPatrick Venture     }
120aadb30ddSPatrick Venture 
121aadb30ddSPatrick Venture     return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id);
122aadb30ddSPatrick Venture }
123aadb30ddSPatrick Venture 
getMatch(const std::string & path)124f2efcbbdSHarvey.Wu std::string getMatch(const std::string& path)
125aadb30ddSPatrick Venture {
126*90b0a66dSPatrick Williams     return sdbusplus::bus::match::rules::propertiesChangedNamespace(
127*90b0a66dSPatrick Williams         path, "xyz.openbmc_project");
128aadb30ddSPatrick Venture }
129aadb30ddSPatrick Venture 
validType(const std::string & type)130aadb30ddSPatrick Venture bool validType(const std::string& type)
131aadb30ddSPatrick Venture {
13223e22b90SJosh Lehan     static std::set<std::string> valid = {"fan", "temp", "margin", "power",
13323e22b90SJosh Lehan                                           "powersum"};
134aadb30ddSPatrick Venture     return (valid.find(type) != valid.end());
135aadb30ddSPatrick Venture }
136aadb30ddSPatrick Venture 
scaleSensorReading(const double min,const double max,double & value)137aadb30ddSPatrick Venture void scaleSensorReading(const double min, const double max, double& value)
138aadb30ddSPatrick Venture {
139aadb30ddSPatrick Venture     if (max <= 0 || max <= min)
140aadb30ddSPatrick Venture     {
141aadb30ddSPatrick Venture         return;
142aadb30ddSPatrick Venture     }
14391fe17f6SJosh Lehan     value -= min;
144aadb30ddSPatrick Venture     value /= (max - min);
145aadb30ddSPatrick Venture }
146aadb30ddSPatrick Venture 
147aadb30ddSPatrick Venture } // namespace pid_control
148