blob: 037c127d897f6b68472f0859cc0db45114a7967d [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10302
3#include "VariantVisitors.hpp"
4
Zhikui Renda98f092021-11-01 09:41:08 -07005#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -07006#include <boost/asio/steady_timer.hpp>
James Feist6714a252018-09-10 15:26:18 -07007#include <boost/container/flat_map.hpp>
George Liu61984352025-02-24 14:47:34 +08008#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -07009#include <sdbusplus/asio/connection.hpp>
10#include <sdbusplus/asio/object_server.hpp>
Ed Tanous18b61862025-01-30 10:56:28 -080011#include <sdbusplus/bus/match.hpp>
12#include <sdbusplus/message.hpp>
13#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070014
Ed Tanous18b61862025-01-30 10:56:28 -080015#include <algorithm>
16#include <charconv>
17#include <chrono>
18#include <cmath>
19#include <cstddef>
20#include <cstdint>
James Feist24f02f22019-04-15 11:05:39 -070021#include <filesystem>
Patrick Venturefd6ba732019-10-31 14:27:39 -070022#include <functional>
Patrick Venturefd6ba732019-10-31 14:27:39 -070023#include <memory>
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050024#include <optional>
James Feist6714a252018-09-10 15:26:18 -070025#include <regex>
Ed Tanous18b61862025-01-30 10:56:28 -080026#include <set>
Zev Weiss214d9712022-08-12 12:54:31 -070027#include <span>
Ed Tanous18b61862025-01-30 10:56:28 -080028#include <stdexcept>
Patrick Venturefd6ba732019-10-31 14:27:39 -070029#include <string>
Ed Tanous18b61862025-01-30 10:56:28 -080030#include <string_view>
31#include <system_error>
Patrick Venturefd6ba732019-10-31 14:27:39 -070032#include <tuple>
33#include <utility>
34#include <variant>
35#include <vector>
James Feist6714a252018-09-10 15:26:18 -070036
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070037const constexpr char* jsonStore = "/var/configuration/flattened.json";
38const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
39const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
James Feist58295ad2019-05-30 15:01:41 -070040
41constexpr const char* cpuInventoryPath =
42 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070043const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070044
45using BasicVariantType =
Harshit Agherad837b562025-04-21 19:50:10 +053046 std::variant<std::vector<std::string>, std::vector<uint8_t>, std::string,
47 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
48 uint16_t, uint8_t, bool>;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080049using SensorBaseConfigMap =
50 boost::container::flat_map<std::string, BasicVariantType>;
51using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
52using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
53using ManagedObjectType =
54 boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
James Feist6714a252018-09-10 15:26:18 -070055
James Feista5e58722019-04-22 14:43:11 -070056using GetSubTreeType = std::vector<
57 std::pair<std::string,
58 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feistd8bd5622019-06-26 12:09:05 -070059using Association = std::tuple<std::string, std::string, std::string>;
60
Zhikui Renda98f092021-11-01 09:41:08 -070061inline std::string escapeName(const std::string& sensorName)
62{
63 return boost::replace_all_copy(sensorName, " ", "_");
64}
65
Zev Weiss88cb29d2022-05-09 03:46:15 +000066enum class PowerState
67{
68 on,
69 biosPost,
Thu Nguyen6db8aae2022-10-04 08:12:48 +070070 always,
71 chassisOn
Zev Weiss88cb29d2022-05-09 03:46:15 +000072};
73
Jason Ling100c20b2020-08-11 14:50:33 -070074std::optional<std::string> openAndRead(const std::string& hwmonFile);
Patrick Williams2aaf7172024-08-16 15:20:40 -040075std::optional<std::string> getFullHwmonFilePath(
76 const std::string& directory, const std::string& hwmonBaseName,
77 const std::set<std::string>& permitSet);
Jason Ling100c20b2020-08-11 14:50:33 -070078std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
Ed Tanous8a57ec02020-10-09 12:46:52 -070079bool findFiles(const std::filesystem::path& dirPath,
Lei YU6a4e9732021-10-20 13:27:34 +080080 std::string_view matchString,
James Feistcf3bce62019-01-08 10:07:19 -080081 std::vector<std::filesystem::path>& foundPaths,
Ed Tanous8a57ec02020-10-09 12:46:52 -070082 int symlinkDepth = 1);
Ed Tanous201a1012024-04-03 18:07:28 -070083bool isPowerOn();
84bool hasBiosPost();
85bool isChassisOn();
Zev Weiss88cb29d2022-05-09 03:46:15 +000086void setupPowerMatchCallback(
87 const std::shared_ptr<sdbusplus::asio::connection>& conn,
88 std::function<void(PowerState type, bool state)>&& callback);
James Feist71d31b22019-01-02 16:57:54 -080089void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070090bool getSensorConfiguration(
91 const std::string& type,
92 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -070093 ManagedObjectType& resp, bool useCache);
94
James Feist82bac4c2019-03-11 11:16:53 -070095void createAssociation(
96 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
97 const std::string& path);
98
James Feist87d713a2018-12-06 16:06:24 -080099// replaces limits if MinReading and MaxReading are found.
100void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -0800101 const SensorBaseConfiguration* data);
102
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000103bool readingStateGood(const PowerState& powerState);
104
Zev Weiss054aad82022-08-18 01:37:34 -0700105constexpr const char* configInterfacePrefix =
106 "xyz.openbmc_project.Configuration.";
107
108inline std::string configInterfaceName(const std::string& type)
109{
110 return std::string(configInterfacePrefix) + type;
111}
112
James Feista5e58722019-04-22 14:43:11 -0700113namespace mapper
114{
115constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
116constexpr const char* path = "/xyz/openbmc_project/object_mapper";
117constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
118constexpr const char* subtree = "GetSubTree";
119} // namespace mapper
120
121namespace properties
122{
123constexpr const char* interface = "org.freedesktop.DBus.Properties";
124constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -0700125constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -0700126} // namespace properties
127
James Feist52497fd2019-06-07 13:01:33 -0700128namespace power
129{
Potin Laief85e0b2024-02-23 10:30:19 +0800130const static constexpr char* busname = "xyz.openbmc_project.State.Host0";
James Feist52497fd2019-06-07 13:01:33 -0700131const static constexpr char* interface = "xyz.openbmc_project.State.Host";
132const static constexpr char* path = "/xyz/openbmc_project/state/host0";
133const static constexpr char* property = "CurrentHostState";
134} // namespace power
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700135
136namespace chassis
137{
Patrick Williamsea14f142024-01-19 14:23:54 -0600138const static constexpr char* busname = "xyz.openbmc_project.State.Chassis0";
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700139const static constexpr char* interface = "xyz.openbmc_project.State.Chassis";
140const static constexpr char* path = "/xyz/openbmc_project/state/chassis0";
141const static constexpr char* property = "CurrentPowerState";
Thang Tran819eb322023-11-07 13:50:18 +0700142const static constexpr char* sOn = ".On";
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700143} // namespace chassis
144
James Feist52497fd2019-06-07 13:01:33 -0700145namespace post
146{
Potin Laief85e0b2024-02-23 10:30:19 +0800147const static constexpr char* busname = "xyz.openbmc_project.State.Host0";
James Feist52497fd2019-06-07 13:01:33 -0700148const static constexpr char* interface =
149 "xyz.openbmc_project.State.OperatingSystem.Status";
Potin Laief85e0b2024-02-23 10:30:19 +0800150const static constexpr char* path = "/xyz/openbmc_project/state/host0";
James Feist52497fd2019-06-07 13:01:33 -0700151const static constexpr char* property = "OperatingSystemState";
152} // namespace post
153
James Feist2adc95c2019-09-30 14:55:28 -0700154namespace association
155{
156const static constexpr char* interface =
157 "xyz.openbmc_project.Association.Definitions";
158} // namespace association
159
James Feist40a72142018-12-21 10:09:53 -0800160template <typename T>
Zev Weissafd15042022-07-18 12:28:40 -0700161inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key)
James Feist40a72142018-12-21 10:09:53 -0800162{
163 auto it = data.find(key);
164 if (it == data.end())
165 {
George Liu61984352025-02-24 14:47:34 +0800166 lg2::error("Configuration missing '{KEY}'", "KEY", key);
James Feist40a72142018-12-21 10:09:53 -0800167 throw std::invalid_argument("Key Missing");
168 }
169 if constexpr (std::is_same_v<T, double>)
170 {
James Feist3eb82622019-02-08 13:10:22 -0800171 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800172 }
James Feist6ef20402019-01-07 16:45:08 -0800173 else if constexpr (std::is_unsigned_v<T>)
174 {
James Feist3eb82622019-02-08 13:10:22 -0800175 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800176 }
James Feist40a72142018-12-21 10:09:53 -0800177 else if constexpr (std::is_same_v<T, std::string>)
178 {
James Feist3eb82622019-02-08 13:10:22 -0800179 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800180 }
181 else
182 {
James Feist52497fd2019-06-07 13:01:33 -0700183 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800184 }
185}
James Feistfc94b212019-02-06 16:14:51 -0800186
187inline void setReadState(const std::string& str, PowerState& val)
188{
James Feistfc94b212019-02-06 16:14:51 -0800189 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 }
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700201 else if (str == "ChassisOn")
202 {
203 val = PowerState::chassisOn;
204 }
James Feistfc94b212019-02-06 16:14:51 -0800205}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800206
Zev Weissa4d27682022-07-19 15:30:36 -0700207inline PowerState getPowerState(const SensorBaseConfigMap& cfg)
208{
209 PowerState state = PowerState::always;
210 auto findPowerState = cfg.find("PowerState");
211 if (findPowerState != cfg.end())
212 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400213 std::string powerState =
214 std::visit(VariantToStringVisitor(), findPowerState->second);
Zev Weissa4d27682022-07-19 15:30:36 -0700215 setReadState(powerState, state);
216 }
217 return state;
218}
219
Zev Weiss8569bf22022-10-11 15:37:44 -0700220inline 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
Ed Tanous8a57ec02020-10-09 12:46:52 -0700235inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist49a8ccd2020-09-16 16:09:52 -0700236 const std::string& name, bool on)
237{
238 conn->async_method_call(
239 [name](const boost::system::error_code ec) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400240 if (ec)
241 {
George Liu61984352025-02-24 14:47:34 +0800242 lg2::error("Failed to set LED '{NAME}'", "NAME", name);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400243 }
244 },
James Feist49a8ccd2020-09-16 16:09:52 -0700245 "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
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800251void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700252 const std::shared_ptr<sdbusplus::asio::connection>& conn,
253 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800254 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700255
James Feist38fb5982020-05-28 10:09:54 -0700256struct GetSensorConfiguration :
257 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700258{
259 GetSensorConfiguration(
260 std::shared_ptr<sdbusplus::asio::connection> connection,
261 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
Patrick Williams2aaf7172024-08-16 15:20:40 -0400262 dbusConnection(std::move(connection)), callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700263 {}
James Feistf27a55c2020-08-04 14:27:30 -0700264
265 void getPath(const std::string& path, const std::string& interface,
266 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700267 {
James Feistf27a55c2020-08-04 14:27:30 -0700268 if (retries > 5)
269 {
270 retries = 5;
271 }
272 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
273
274 self->dbusConnection->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700275 [self, path, interface, owner, retries](
276 const boost::system::error_code ec, SensorBaseConfigMap& data) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400277 if (ec)
James Feistf27a55c2020-08-04 14:27:30 -0700278 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400279 if (retries == 0U)
Ed Tanousbb679322022-05-16 16:10:00 -0700280 {
George Liu61984352025-02-24 14:47:34 +0800281 lg2::error("Error getting '{PATH}': no retries left",
282 "PATH", path);
Ed Tanousbb679322022-05-16 16:10:00 -0700283 return;
284 }
George Liu61984352025-02-24 14:47:34 +0800285 lg2::error(
286 "Error getting '{PATH}': '{RETRIES}' retries left",
287 "PATH", path, "RETRIES", retries - 1);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400288 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 {
George Liu61984352025-02-24 14:47:34 +0800295 lg2::error("Timer error: '{ERROR_MESSAGE}'",
296 "ERROR_MESSAGE", ec.message());
Patrick Williams2aaf7172024-08-16 15:20:40 -0400297 return;
298 }
299 self->getPath(path, interface, owner, retries - 1);
300 });
301 return;
302 }
James Feistf27a55c2020-08-04 14:27:30 -0700303
Patrick Williams2aaf7172024-08-16 15:20:40 -0400304 self->respData[path][interface] = std::move(data);
305 },
James Feistf27a55c2020-08-04 14:27:30 -0700306 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
307 interface);
308 }
309
Zev Weiss054aad82022-08-18 01:37:34 -0700310 void getConfiguration(const std::vector<std::string>& types,
James Feistf27a55c2020-08-04 14:27:30 -0700311 size_t retries = 0)
312 {
313 if (retries > 5)
314 {
315 retries = 5;
316 }
317
Zev Weiss054aad82022-08-18 01:37:34 -0700318 std::vector<std::string> interfaces(types.size());
319 for (const auto& type : types)
320 {
321 interfaces.push_back(configInterfaceName(type));
322 }
323
James Feistc71c1192019-09-18 14:31:33 -0700324 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
325 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700326 [self, interfaces, retries](const boost::system::error_code ec,
327 const GetSubTreeType& ret) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400328 if (ec)
James Feistc71c1192019-09-18 14:31:33 -0700329 {
George Liu61984352025-02-24 14:47:34 +0800330 lg2::error("Error calling mapper: '{ERROR_MESSAGE}'",
331 "ERROR_MESSAGE", ec.message());
Patrick Williams2aaf7172024-08-16 15:20:40 -0400332 if (retries == 0U)
James Feistc71c1192019-09-18 14:31:33 -0700333 {
334 return;
335 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400336 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 {
George Liu61984352025-02-24 14:47:34 +0800343 lg2::error("Timer error: '{ERROR_MESSAGE}'",
344 "ERROR_MESSAGE", ec.message());
Patrick Williams2aaf7172024-08-16 15:20:40 -0400345 return;
346 }
347 self->getConfiguration(interfaces, retries - 1);
348 });
James Feistc71c1192019-09-18 14:31:33 -0700349
Ed Tanousbb679322022-05-16 16:10:00 -0700350 return;
James Feistc71c1192019-09-18 14:31:33 -0700351 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400352 for (const auto& [path, objDict] : ret)
Ed Tanousbb679322022-05-16 16:10:00 -0700353 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400354 if (objDict.empty())
Ed Tanousbb679322022-05-16 16:10:00 -0700355 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400356 return;
Ed Tanousbb679322022-05-16 16:10:00 -0700357 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400358 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 }
Ed Tanousbb679322022-05-16 16:10:00 -0700374 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400375 },
James Feistc71c1192019-09-18 14:31:33 -0700376 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
377 "/", 0, interfaces);
378 }
379
380 ~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};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200389
390// The common scheme for sysfs files naming is: <type><number>_<item>.
391// This function returns optionally these 3 elements as a tuple.
Patrick Williams556e04b2025-02-01 08:22:22 -0500392std::optional<std::tuple<std::string, std::string, std::string>> splitFileName(
393 const std::filesystem::path& filePath);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200394std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700395 const double& scaleFactor);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800396void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
397bool getManufacturingMode();
Zev Weiss214d9712022-08-12 12:54:31 -0700398std::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);
Tom Tung278e1772023-12-12 09:20:40 +0800402
403template <typename T>
404bool getDeviceBusAddr(const std::string& deviceName, T& bus, T& addr)
405{
406 auto findHyphen = deviceName.find('-');
407 if (findHyphen == std::string::npos)
408 {
George Liu61984352025-02-24 14:47:34 +0800409 lg2::error("found bad device '{NAME}'", "NAME", deviceName);
Tom Tung278e1772023-12-12 09:20:40 +0800410 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 {
George Liu61984352025-02-24 14:47:34 +0800419 lg2::error("Error finding bus for '{NAME}'", "NAME", deviceName);
Tom Tung278e1772023-12-12 09:20:40 +0800420 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 {
George Liu61984352025-02-24 14:47:34 +0800425 lg2::error("Error finding addr for '{NAME}'", "NAME", deviceName);
Tom Tung278e1772023-12-12 09:20:40 +0800426 return false;
427 }
428
429 return true;
430}