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