xref: /openbmc/dbus-sensors/src/Utils.hpp (revision 6198435dc2bc6e07b46e32465876815d4ea86437)
1 #pragma once
2 
3 #include "VariantVisitors.hpp"
4 
5 #include <boost/algorithm/string/replace.hpp>
6 #include <boost/asio/steady_timer.hpp>
7 #include <boost/container/flat_map.hpp>
8 #include <phosphor-logging/lg2.hpp>
9 #include <sdbusplus/asio/connection.hpp>
10 #include <sdbusplus/asio/object_server.hpp>
11 #include <sdbusplus/bus/match.hpp>
12 #include <sdbusplus/message.hpp>
13 #include <sdbusplus/message/native_types.hpp>
14 
15 #include <algorithm>
16 #include <charconv>
17 #include <chrono>
18 #include <cmath>
19 #include <cstddef>
20 #include <cstdint>
21 #include <filesystem>
22 #include <functional>
23 #include <memory>
24 #include <optional>
25 #include <regex>
26 #include <set>
27 #include <span>
28 #include <stdexcept>
29 #include <string>
30 #include <string_view>
31 #include <system_error>
32 #include <tuple>
33 #include <utility>
34 #include <variant>
35 #include <vector>
36 
37 const constexpr char* jsonStore = "/var/configuration/flattened.json";
38 const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
39 const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
40 
41 constexpr const char* cpuInventoryPath =
42     "/xyz/openbmc_project/inventory/system/chassis/motherboard";
43 const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
44 
45 using BasicVariantType =
46     std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
47                  double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
48 using SensorBaseConfigMap =
49     boost::container::flat_map<std::string, BasicVariantType>;
50 using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
51 using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
52 using ManagedObjectType =
53     boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
54 
55 using GetSubTreeType = std::vector<
56     std::pair<std::string,
57               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
58 using Association = std::tuple<std::string, std::string, std::string>;
59 
escapeName(const std::string & sensorName)60 inline std::string escapeName(const std::string& sensorName)
61 {
62     return boost::replace_all_copy(sensorName, " ", "_");
63 }
64 
65 enum class PowerState
66 {
67     on,
68     biosPost,
69     always,
70     chassisOn
71 };
72 
73 std::optional<std::string> openAndRead(const std::string& hwmonFile);
74 std::optional<std::string> getFullHwmonFilePath(
75     const std::string& directory, const std::string& hwmonBaseName,
76     const std::set<std::string>& permitSet);
77 std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
78 bool findFiles(const std::filesystem::path& dirPath,
79                std::string_view matchString,
80                std::vector<std::filesystem::path>& foundPaths,
81                int symlinkDepth = 1);
82 bool isPowerOn();
83 bool hasBiosPost();
84 bool isChassisOn();
85 void setupPowerMatchCallback(
86     const std::shared_ptr<sdbusplus::asio::connection>& conn,
87     std::function<void(PowerState type, bool state)>&& callback);
88 void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
89 bool getSensorConfiguration(
90     const std::string& type,
91     const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
92     ManagedObjectType& resp, bool useCache);
93 
94 void createAssociation(
95     std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
96     const std::string& path);
97 
98 // replaces limits if MinReading and MaxReading are found.
99 void findLimits(std::pair<double, double>& limits,
100                 const SensorBaseConfiguration* data);
101 
102 bool readingStateGood(const PowerState& powerState);
103 
104 constexpr const char* configInterfacePrefix =
105     "xyz.openbmc_project.Configuration.";
106 
configInterfaceName(const std::string & type)107 inline std::string configInterfaceName(const std::string& type)
108 {
109     return std::string(configInterfacePrefix) + type;
110 }
111 
112 namespace mapper
113 {
114 constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
115 constexpr const char* path = "/xyz/openbmc_project/object_mapper";
116 constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
117 constexpr const char* subtree = "GetSubTree";
118 } // namespace mapper
119 
120 namespace properties
121 {
122 constexpr const char* interface = "org.freedesktop.DBus.Properties";
123 constexpr const char* get = "Get";
124 constexpr const char* set = "Set";
125 } // namespace properties
126 
127 namespace power
128 {
129 const static constexpr char* busname = "xyz.openbmc_project.State.Host0";
130 const static constexpr char* interface = "xyz.openbmc_project.State.Host";
131 const static constexpr char* path = "/xyz/openbmc_project/state/host0";
132 const static constexpr char* property = "CurrentHostState";
133 } // namespace power
134 
135 namespace chassis
136 {
137 const static constexpr char* busname = "xyz.openbmc_project.State.Chassis0";
138 const static constexpr char* interface = "xyz.openbmc_project.State.Chassis";
139 const static constexpr char* path = "/xyz/openbmc_project/state/chassis0";
140 const static constexpr char* property = "CurrentPowerState";
141 const static constexpr char* sOn = ".On";
142 } // namespace chassis
143 
144 namespace post
145 {
146 const static constexpr char* busname = "xyz.openbmc_project.State.Host0";
147 const static constexpr char* interface =
148     "xyz.openbmc_project.State.OperatingSystem.Status";
149 const static constexpr char* path = "/xyz/openbmc_project/state/host0";
150 const static constexpr char* property = "OperatingSystemState";
151 } // namespace post
152 
153 namespace association
154 {
155 const static constexpr char* interface =
156     "xyz.openbmc_project.Association.Definitions";
157 } // namespace association
158 
159 template <typename T>
loadVariant(const SensorBaseConfigMap & data,const std::string & key)160 inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key)
161 {
162     auto it = data.find(key);
163     if (it == data.end())
164     {
165         lg2::error("Configuration missing '{KEY}'", "KEY", key);
166         throw std::invalid_argument("Key Missing");
167     }
168     if constexpr (std::is_same_v<T, double>)
169     {
170         return std::visit(VariantToDoubleVisitor(), it->second);
171     }
172     else if constexpr (std::is_unsigned_v<T>)
173     {
174         return std::visit(VariantToUnsignedIntVisitor(), it->second);
175     }
176     else if constexpr (std::is_same_v<T, std::string>)
177     {
178         return std::visit(VariantToStringVisitor(), it->second);
179     }
180     else
181     {
182         static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
183     }
184 }
185 
setReadState(const std::string & str,PowerState & val)186 inline void setReadState(const std::string& str, PowerState& val)
187 {
188     if (str == "On")
189     {
190         val = PowerState::on;
191     }
192     else if (str == "BiosPost")
193     {
194         val = PowerState::biosPost;
195     }
196     else if (str == "Always")
197     {
198         val = PowerState::always;
199     }
200     else if (str == "ChassisOn")
201     {
202         val = PowerState::chassisOn;
203     }
204 }
205 
getPowerState(const SensorBaseConfigMap & cfg)206 inline PowerState getPowerState(const SensorBaseConfigMap& cfg)
207 {
208     PowerState state = PowerState::always;
209     auto findPowerState = cfg.find("PowerState");
210     if (findPowerState != cfg.end())
211     {
212         std::string powerState =
213             std::visit(VariantToStringVisitor(), findPowerState->second);
214         setReadState(powerState, state);
215     }
216     return state;
217 }
218 
getPollRate(const SensorBaseConfigMap & cfg,float dflt)219 inline float getPollRate(const SensorBaseConfigMap& cfg, float dflt)
220 {
221     float pollRate = dflt;
222     auto findPollRate = cfg.find("PollRate");
223     if (findPollRate != cfg.end())
224     {
225         pollRate = std::visit(VariantToFloatVisitor(), findPollRate->second);
226         if (!std::isfinite(pollRate) || pollRate <= 0.0F)
227         {
228             pollRate = dflt; // poll time invalid, fall back to default
229         }
230     }
231     return pollRate;
232 }
233 
setLed(const std::shared_ptr<sdbusplus::asio::connection> & conn,const std::string & name,bool on)234 inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
235                    const std::string& name, bool on)
236 {
237     conn->async_method_call(
238         [name](const boost::system::error_code ec) {
239             if (ec)
240             {
241                 lg2::error("Failed to set LED '{NAME}'", "NAME", name);
242             }
243         },
244         "xyz.openbmc_project.LED.GroupManager",
245         "/xyz/openbmc_project/led/groups/" + name, properties::interface,
246         properties::set, "xyz.openbmc_project.Led.Group", "Asserted",
247         std::variant<bool>(on));
248 }
249 
250 void createInventoryAssoc(
251     const std::shared_ptr<sdbusplus::asio::connection>& conn,
252     const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
253     const std::string& path);
254 
255 struct GetSensorConfiguration :
256     std::enable_shared_from_this<GetSensorConfiguration>
257 {
GetSensorConfigurationGetSensorConfiguration258     GetSensorConfiguration(
259         std::shared_ptr<sdbusplus::asio::connection> connection,
260         std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
261         dbusConnection(std::move(connection)), callback(std::move(callbackFunc))
262     {}
263 
getPathGetSensorConfiguration264     void getPath(const std::string& path, const std::string& interface,
265                  const std::string& owner, size_t retries = 5)
266     {
267         if (retries > 5)
268         {
269             retries = 5;
270         }
271         std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
272 
273         self->dbusConnection->async_method_call(
274             [self, path, interface, owner, retries](
275                 const boost::system::error_code ec, SensorBaseConfigMap& data) {
276                 if (ec)
277                 {
278                     if (retries == 0U)
279                     {
280                         lg2::error("Error getting '{PATH}': no retries left",
281                                    "PATH", path);
282                         return;
283                     }
284                     lg2::error(
285                         "Error getting '{PATH}': '{RETRIES}' retries left",
286                         "PATH", path, "RETRIES", retries - 1);
287                     auto timer = std::make_shared<boost::asio::steady_timer>(
288                         self->dbusConnection->get_io_context());
289                     timer->expires_after(std::chrono::seconds(10));
290                     timer->async_wait([self, timer, path, interface, owner,
291                                        retries](boost::system::error_code ec) {
292                         if (ec)
293                         {
294                             lg2::error("Timer error: '{ERROR_MESSAGE}'",
295                                        "ERROR_MESSAGE", ec.message());
296                             return;
297                         }
298                         self->getPath(path, interface, owner, retries - 1);
299                     });
300                     return;
301                 }
302 
303                 self->respData[path][interface] = std::move(data);
304             },
305             owner, path, "org.freedesktop.DBus.Properties", "GetAll",
306             interface);
307     }
308 
getConfigurationGetSensorConfiguration309     void getConfiguration(const std::vector<std::string>& types,
310                           size_t retries = 0)
311     {
312         if (retries > 5)
313         {
314             retries = 5;
315         }
316 
317         std::vector<std::string> interfaces(types.size());
318         for (const auto& type : types)
319         {
320             interfaces.push_back(configInterfaceName(type));
321         }
322 
323         std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
324         dbusConnection->async_method_call(
325             [self, interfaces, retries](const boost::system::error_code ec,
326                                         const GetSubTreeType& ret) {
327                 if (ec)
328                 {
329                     lg2::error("Error calling mapper: '{ERROR_MESSAGE}'",
330                                "ERROR_MESSAGE", ec.message());
331                     if (retries == 0U)
332                     {
333                         return;
334                     }
335                     auto timer = std::make_shared<boost::asio::steady_timer>(
336                         self->dbusConnection->get_io_context());
337                     timer->expires_after(std::chrono::seconds(10));
338                     timer->async_wait([self, timer, interfaces,
339                                        retries](boost::system::error_code ec) {
340                         if (ec)
341                         {
342                             lg2::error("Timer error: '{ERROR_MESSAGE}'",
343                                        "ERROR_MESSAGE", ec.message());
344                             return;
345                         }
346                         self->getConfiguration(interfaces, retries - 1);
347                     });
348 
349                     return;
350                 }
351                 for (const auto& [path, objDict] : ret)
352                 {
353                     if (objDict.empty())
354                     {
355                         return;
356                     }
357                     const std::string& owner = objDict.begin()->first;
358 
359                     for (const std::string& interface : objDict.begin()->second)
360                     {
361                         // anything that starts with a requested configuration
362                         // is good
363                         if (std::find_if(
364                                 interfaces.begin(), interfaces.end(),
365                                 [interface](const std::string& possible) {
366                                     return interface.starts_with(possible);
367                                 }) == interfaces.end())
368                         {
369                             continue;
370                         }
371                         self->getPath(path, interface, owner);
372                     }
373                 }
374             },
375             mapper::busName, mapper::path, mapper::interface, mapper::subtree,
376             "/", 0, interfaces);
377     }
378 
~GetSensorConfigurationGetSensorConfiguration379     ~GetSensorConfiguration()
380     {
381         callback(respData);
382     }
383 
384     std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
385     std::function<void(ManagedObjectType& resp)> callback;
386     ManagedObjectType respData;
387 };
388 
389 // The common scheme for sysfs files naming is: <type><number>_<item>.
390 // This function returns optionally these 3 elements as a tuple.
391 std::optional<std::tuple<std::string, std::string, std::string>> splitFileName(
392     const std::filesystem::path& filePath);
393 std::optional<double> readFile(const std::string& thresholdFile,
394                                const double& scaleFactor);
395 void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
396 bool getManufacturingMode();
397 std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
398     setupPropertiesChangedMatches(
399         sdbusplus::asio::connection& bus, std::span<const char* const> types,
400         const std::function<void(sdbusplus::message_t&)>& handler);
401 
402 template <typename T>
getDeviceBusAddr(const std::string & deviceName,T & bus,T & addr)403 bool getDeviceBusAddr(const std::string& deviceName, T& bus, T& addr)
404 {
405     auto findHyphen = deviceName.find('-');
406     if (findHyphen == std::string::npos)
407     {
408         lg2::error("found bad device '{NAME}'", "NAME", deviceName);
409         return false;
410     }
411     std::string busStr = deviceName.substr(0, findHyphen);
412     std::string addrStr = deviceName.substr(findHyphen + 1);
413 
414     std::from_chars_result res{};
415     res = std::from_chars(&*busStr.begin(), &*busStr.end(), bus);
416     if (res.ec != std::errc{} || res.ptr != &*busStr.end())
417     {
418         lg2::error("Error finding bus for '{NAME}'", "NAME", deviceName);
419         return false;
420     }
421     res = std::from_chars(&*addrStr.begin(), &*addrStr.end(), addr, 16);
422     if (res.ec != std::errc{} || res.ptr != &*addrStr.end())
423     {
424         lg2::error("Error finding addr for '{NAME}'", "NAME", deviceName);
425         return false;
426     }
427 
428     return true;
429 }
430