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