blob: c64977bacc07fdffbaa6f33cfb0d07a68cbb8bb5 [file] [log] [blame]
Patrick Ventureaadb30d2020-08-10 09:17:11 -07001
Patrick Williams90b0a662023-07-13 17:01:08 -05002#include <sdbusplus/bus/match.hpp>
3
Patrick Ventureaadb30d2020-08-10 09:17:11 -07004#include <cmath>
Patrick Venture8b4478c2020-10-06 08:30:27 -07005#include <cstdint>
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 Ventureb8cfc642020-10-07 08:30:22 -070022int64_t setZoneIndex(const std::string& name,
23 std::map<std::string, int64_t>& zones, int64_t index)
24{
25 auto it = zones.find(name);
26 if (it != zones.end())
27 {
28 // Name already allocated, make no change, return existing
29 return it->second;
30 }
31
32 // The zone name is known not to exist yet
33 for (;;)
34 {
35 bool usedIndex = false;
36
37 // See if desired index number is free
38 for (const auto& zi : zones)
39 {
40 if (index == zi.second)
41 {
42 usedIndex = true;
43 break;
44 }
45 }
46
47 // Increment until a free index number is found
48 if (usedIndex)
49 {
50 ++index;
51 continue;
52 }
53
54 break;
55 }
56
57 // Allocate and return new zone index number for this name
58 zones[name] = index;
59 return index;
60}
61
62int64_t getZoneIndex(const std::string& name,
63 std::map<std::string, int64_t>& zones)
64{
65 auto it = zones.find(name);
66 if (it != zones.end())
67 {
68 return it->second;
69 }
70
71 // Auto-assign next unused zone number, using 0-based numbering
72 return setZoneIndex(name, zones, 0);
73}
74
Patrick Venture1a7c49f2020-10-06 15:49:27 -070075bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
76 const std::string& search,
77 std::vector<std::pair<std::string, std::string>>& matches)
78{
79 std::smatch match;
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070080 std::regex reg('/' + search + '$');
Patrick Venture1a7c49f2020-10-06 15:49:27 -070081 for (const auto& sensor : sensors)
82 {
83 if (std::regex_search(sensor.first, match, reg))
84 {
Ed Tanousd2768c52025-06-26 11:42:57 -070085 matches.emplace_back(sensor);
Patrick Venture1a7c49f2020-10-06 15:49:27 -070086 }
87 }
88 return matches.size() > 0;
89}
90
Chaul Lya552fe22024-11-15 10:20:28 +000091std::string getSensorUnit(const std::string& type)
92{
93 std::string unit;
94 if (type == "fan")
95 {
96 unit = "xyz.openbmc_project.Sensor.Value.Unit.RPMS";
97 }
98 else if (type == "temp" || type == "margin")
99 {
100 unit = "xyz.openbmc_project.Sensor.Value.Unit.DegreesC";
101 }
102 else if (type == "power" || type == "powersum")
103 {
104 unit = "xyz.openbmc_project.Sensor.Value.Unit.Watts";
105 }
106 else
107 {
108 unit = "unknown";
109 }
110 return unit;
111}
112
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700113std::string getSensorPath(const std::string& type, const std::string& id)
114{
Harvey Wu22579ca2022-11-07 14:53:37 +0800115 std::string layer;
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700116 if (type == "fan")
117 {
118 layer = "fan_tach";
119 }
120 else if (type == "temp")
121 {
122 layer = "temperature";
123 }
Josh Lehan3e2f7582020-09-20 22:06:03 -0700124 else if (type == "margin")
125 {
126 layer = "temperature";
127 }
Josh Lehan23e22b92022-11-12 22:37:58 -0800128 else if (type == "power")
129 {
130 layer = "power";
131 }
132 else if (type == "powersum")
133 {
134 layer = "power";
135 }
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700136 else
137 {
138 layer = "unknown"; // TODO(venture): Need to handle.
139 }
140
141 return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id);
142}
143
Harvey.Wuf2efcbb2022-02-09 10:24:30 +0800144std::string getMatch(const std::string& path)
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700145{
Patrick Williams90b0a662023-07-13 17:01:08 -0500146 return sdbusplus::bus::match::rules::propertiesChangedNamespace(
147 path, "xyz.openbmc_project");
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700148}
149
150bool validType(const std::string& type)
151{
Josh Lehan23e22b92022-11-12 22:37:58 -0800152 static std::set<std::string> valid = {"fan", "temp", "margin", "power",
153 "powersum"};
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700154 return (valid.find(type) != valid.end());
155}
156
157void scaleSensorReading(const double min, const double max, double& value)
158{
159 if (max <= 0 || max <= min)
160 {
161 return;
162 }
Josh Lehan91fe17f2020-09-20 22:50:26 -0700163 value -= min;
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700164 value /= (max - min);
165}
166
167} // namespace pid_control