blob: 18d8f368fe906352ba0294186195bbd7522b2355 [file] [log] [blame]
Patrick Ventureaadb30d2020-08-10 09:17:11 -07001#include "util.hpp"
2
Patrick Williams90b0a662023-07-13 17:01:08 -05003#include <sdbusplus/bus/match.hpp>
4
Patrick Ventureaadb30d2020-08-10 09:17:11 -07005#include <cmath>
Patrick Venture8b4478c2020-10-06 08:30:27 -07006#include <cstdint>
Patrick Ventureaadb30d2020-08-10 09:17:11 -07007#include <iostream>
Patrick Venture8b4478c2020-10-06 08:30:27 -07008#include <map>
Patrick Venture1a7c49f2020-10-06 15:49:27 -07009#include <regex>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070010#include <set>
Patrick Venture8b4478c2020-10-06 08:30:27 -070011#include <string>
Patrick Venture1a7c49f2020-10-06 15:49:27 -070012#include <unordered_map>
13#include <utility>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070014#include <variant>
Patrick Venture1a7c49f2020-10-06 15:49:27 -070015#include <vector>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070016
17using Property = std::string;
18using Value = std::variant<int64_t, double, std::string, bool>;
19using PropertyMap = std::map<Property, Value>;
20
21namespace pid_control
22{
23
Patrick Ventureb8cfc642020-10-07 08:30:22 -070024int64_t setZoneIndex(const std::string& name,
25 std::map<std::string, int64_t>& zones, int64_t index)
26{
27 auto it = zones.find(name);
28 if (it != zones.end())
29 {
30 // Name already allocated, make no change, return existing
31 return it->second;
32 }
33
34 // The zone name is known not to exist yet
35 for (;;)
36 {
37 bool usedIndex = false;
38
39 // See if desired index number is free
40 for (const auto& zi : zones)
41 {
42 if (index == zi.second)
43 {
44 usedIndex = true;
45 break;
46 }
47 }
48
49 // Increment until a free index number is found
50 if (usedIndex)
51 {
52 ++index;
53 continue;
54 }
55
56 break;
57 }
58
59 // Allocate and return new zone index number for this name
60 zones[name] = index;
61 return index;
62}
63
64int64_t getZoneIndex(const std::string& name,
65 std::map<std::string, int64_t>& zones)
66{
67 auto it = zones.find(name);
68 if (it != zones.end())
69 {
70 return it->second;
71 }
72
73 // Auto-assign next unused zone number, using 0-based numbering
74 return setZoneIndex(name, zones, 0);
75}
76
Patrick Venture1a7c49f2020-10-06 15:49:27 -070077bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
78 const std::string& search,
79 std::vector<std::pair<std::string, std::string>>& matches)
80{
81 std::smatch match;
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070082 std::regex reg('/' + search + '$');
Patrick Venture1a7c49f2020-10-06 15:49:27 -070083 for (const auto& sensor : sensors)
84 {
85 if (std::regex_search(sensor.first, match, reg))
86 {
87 matches.push_back(sensor);
88 }
89 }
90 return matches.size() > 0;
91}
92
Chaul Lya552fe22024-11-15 10:20:28 +000093std::string getSensorUnit(const std::string& type)
94{
95 std::string unit;
96 if (type == "fan")
97 {
98 unit = "xyz.openbmc_project.Sensor.Value.Unit.RPMS";
99 }
100 else if (type == "temp" || type == "margin")
101 {
102 unit = "xyz.openbmc_project.Sensor.Value.Unit.DegreesC";
103 }
104 else if (type == "power" || type == "powersum")
105 {
106 unit = "xyz.openbmc_project.Sensor.Value.Unit.Watts";
107 }
108 else
109 {
110 unit = "unknown";
111 }
112 return unit;
113}
114
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700115std::string getSensorPath(const std::string& type, const std::string& id)
116{
Harvey Wu22579ca2022-11-07 14:53:37 +0800117 std::string layer;
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700118 if (type == "fan")
119 {
120 layer = "fan_tach";
121 }
122 else if (type == "temp")
123 {
124 layer = "temperature";
125 }
Josh Lehan3e2f7582020-09-20 22:06:03 -0700126 else if (type == "margin")
127 {
128 layer = "temperature";
129 }
Josh Lehan23e22b92022-11-12 22:37:58 -0800130 else if (type == "power")
131 {
132 layer = "power";
133 }
134 else if (type == "powersum")
135 {
136 layer = "power";
137 }
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700138 else
139 {
140 layer = "unknown"; // TODO(venture): Need to handle.
141 }
142
143 return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id);
144}
145
Harvey.Wuf2efcbb2022-02-09 10:24:30 +0800146std::string getMatch(const std::string& path)
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700147{
Patrick Williams90b0a662023-07-13 17:01:08 -0500148 return sdbusplus::bus::match::rules::propertiesChangedNamespace(
149 path, "xyz.openbmc_project");
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700150}
151
152bool validType(const std::string& type)
153{
Josh Lehan23e22b92022-11-12 22:37:58 -0800154 static std::set<std::string> valid = {"fan", "temp", "margin", "power",
155 "powersum"};
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700156 return (valid.find(type) != valid.end());
157}
158
159void scaleSensorReading(const double min, const double max, double& value)
160{
161 if (max <= 0 || max <= min)
162 {
163 return;
164 }
Josh Lehan91fe17f2020-09-20 22:50:26 -0700165 value -= min;
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700166 value /= (max - min);
167}
168
169} // namespace pid_control