blob: 7d3704d85c7624131f30097c4f11c4f014651895 [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
Patrick Ventureaadb30d2020-08-10 09:17:11 -070093std::string getSensorPath(const std::string& type, const std::string& id)
94{
Harvey Wu22579ca2022-11-07 14:53:37 +080095 std::string layer;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070096 if (type == "fan")
97 {
98 layer = "fan_tach";
99 }
100 else if (type == "temp")
101 {
102 layer = "temperature";
103 }
Josh Lehan3e2f7582020-09-20 22:06:03 -0700104 else if (type == "margin")
105 {
106 layer = "temperature";
107 }
Josh Lehan23e22b92022-11-12 22:37:58 -0800108 else if (type == "power")
109 {
110 layer = "power";
111 }
112 else if (type == "powersum")
113 {
114 layer = "power";
115 }
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700116 else
117 {
118 layer = "unknown"; // TODO(venture): Need to handle.
119 }
120
121 return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id);
122}
123
Harvey.Wuf2efcbb2022-02-09 10:24:30 +0800124std::string getMatch(const std::string& path)
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700125{
Patrick Williams90b0a662023-07-13 17:01:08 -0500126 return sdbusplus::bus::match::rules::propertiesChangedNamespace(
127 path, "xyz.openbmc_project");
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700128}
129
130bool validType(const std::string& type)
131{
Josh Lehan23e22b92022-11-12 22:37:58 -0800132 static std::set<std::string> valid = {"fan", "temp", "margin", "power",
133 "powersum"};
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700134 return (valid.find(type) != valid.end());
135}
136
137void scaleSensorReading(const double min, const double max, double& value)
138{
139 if (max <= 0 || max <= min)
140 {
141 return;
142 }
Josh Lehan91fe17f2020-09-20 22:50:26 -0700143 value -= min;
Patrick Ventureaadb30d2020-08-10 09:17:11 -0700144 value /= (max - min);
145}
146
147} // namespace pid_control