blob: be96877e5e0cabc493573c80f4c0635ea383fdbc [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>
James Feist38fb5982020-05-28 10:09:54 -07008#include <sdbusplus/asio/connection.hpp>
9#include <sdbusplus/asio/object_server.hpp>
Ed Tanous18b61862025-01-30 10:56:28 -080010#include <sdbusplus/bus/match.hpp>
11#include <sdbusplus/message.hpp>
12#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070013
Ed Tanous18b61862025-01-30 10:56:28 -080014#include <algorithm>
15#include <charconv>
16#include <chrono>
17#include <cmath>
18#include <cstddef>
19#include <cstdint>
James Feist24f02f22019-04-15 11:05:39 -070020#include <filesystem>
Patrick Venturefd6ba732019-10-31 14:27:39 -070021#include <functional>
James Feist6714a252018-09-10 15:26:18 -070022#include <iostream>
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 =
James Feist3eb82622019-02-08 13:10:22 -080046 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>;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080048using SensorBaseConfigMap =
49 boost::container::flat_map<std::string, BasicVariantType>;
50using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
51using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
52using ManagedObjectType =
53 boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
James Feist6714a252018-09-10 15:26:18 -070054
James Feista5e58722019-04-22 14:43:11 -070055using GetSubTreeType = std::vector<
56 std::pair<std::string,
57 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feistd8bd5622019-06-26 12:09:05 -070058using Association = std::tuple<std::string, std::string, std::string>;
59
Zhikui Renda98f092021-11-01 09:41:08 -070060inline std::string escapeName(const std::string& sensorName)
61{
62 return boost::replace_all_copy(sensorName, " ", "_");
63}
64
Zev Weiss88cb29d2022-05-09 03:46:15 +000065enum class PowerState
66{
67 on,
68 biosPost,
Thu Nguyen6db8aae2022-10-04 08:12:48 +070069 always,
70 chassisOn
Zev Weiss88cb29d2022-05-09 03:46:15 +000071};
72
Jason Ling100c20b2020-08-11 14:50:33 -070073std::optional<std::string> openAndRead(const std::string& hwmonFile);
Patrick Williams2aaf7172024-08-16 15:20:40 -040074std::optional<std::string> getFullHwmonFilePath(
75 const std::string& directory, const std::string& hwmonBaseName,
76 const std::set<std::string>& permitSet);
Jason Ling100c20b2020-08-11 14:50:33 -070077std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
Ed Tanous8a57ec02020-10-09 12:46:52 -070078bool findFiles(const std::filesystem::path& dirPath,
Lei YU6a4e9732021-10-20 13:27:34 +080079 std::string_view matchString,
James Feistcf3bce62019-01-08 10:07:19 -080080 std::vector<std::filesystem::path>& foundPaths,
Ed Tanous8a57ec02020-10-09 12:46:52 -070081 int symlinkDepth = 1);
Ed Tanous201a1012024-04-03 18:07:28 -070082bool isPowerOn();
83bool hasBiosPost();
84bool isChassisOn();
Zev Weiss88cb29d2022-05-09 03:46:15 +000085void setupPowerMatchCallback(
86 const std::shared_ptr<sdbusplus::asio::connection>& conn,
87 std::function<void(PowerState type, bool state)>&& callback);
James Feist71d31b22019-01-02 16:57:54 -080088void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070089bool getSensorConfiguration(
90 const std::string& type,
91 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -070092 ManagedObjectType& resp, bool useCache);
93
James Feist82bac4c2019-03-11 11:16:53 -070094void createAssociation(
95 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
96 const std::string& path);
97
James Feist87d713a2018-12-06 16:06:24 -080098// replaces limits if MinReading and MaxReading are found.
99void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -0800100 const SensorBaseConfiguration* data);
101
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000102bool readingStateGood(const PowerState& powerState);
103
Zev Weiss054aad82022-08-18 01:37:34 -0700104constexpr const char* configInterfacePrefix =
105 "xyz.openbmc_project.Configuration.";
106
107inline std::string configInterfaceName(const std::string& type)
108{
109 return std::string(configInterfacePrefix) + type;
110}
111
James Feista5e58722019-04-22 14:43:11 -0700112namespace mapper
113{
114constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
115constexpr const char* path = "/xyz/openbmc_project/object_mapper";
116constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
117constexpr const char* subtree = "GetSubTree";
118} // namespace mapper
119
120namespace properties
121{
122constexpr const char* interface = "org.freedesktop.DBus.Properties";
123constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -0700124constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -0700125} // namespace properties
126
James Feist52497fd2019-06-07 13:01:33 -0700127namespace power
128{
Potin Laief85e0b2024-02-23 10:30:19 +0800129const static constexpr char* busname = "xyz.openbmc_project.State.Host0";
James Feist52497fd2019-06-07 13:01:33 -0700130const static constexpr char* interface = "xyz.openbmc_project.State.Host";
131const static constexpr char* path = "/xyz/openbmc_project/state/host0";
132const static constexpr char* property = "CurrentHostState";
133} // namespace power
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700134
135namespace chassis
136{
Patrick Williamsea14f142024-01-19 14:23:54 -0600137const static constexpr char* busname = "xyz.openbmc_project.State.Chassis0";
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700138const static constexpr char* interface = "xyz.openbmc_project.State.Chassis";
139const static constexpr char* path = "/xyz/openbmc_project/state/chassis0";
140const static constexpr char* property = "CurrentPowerState";
Thang Tran819eb322023-11-07 13:50:18 +0700141const static constexpr char* sOn = ".On";
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700142} // namespace chassis
143
James Feist52497fd2019-06-07 13:01:33 -0700144namespace post
145{
Potin Laief85e0b2024-02-23 10:30:19 +0800146const static constexpr char* busname = "xyz.openbmc_project.State.Host0";
James Feist52497fd2019-06-07 13:01:33 -0700147const static constexpr char* interface =
148 "xyz.openbmc_project.State.OperatingSystem.Status";
Potin Laief85e0b2024-02-23 10:30:19 +0800149const static constexpr char* path = "/xyz/openbmc_project/state/host0";
James Feist52497fd2019-06-07 13:01:33 -0700150const static constexpr char* property = "OperatingSystemState";
151} // namespace post
152
James Feist2adc95c2019-09-30 14:55:28 -0700153namespace association
154{
155const static constexpr char* interface =
156 "xyz.openbmc_project.Association.Definitions";
157} // namespace association
158
James Feist40a72142018-12-21 10:09:53 -0800159template <typename T>
Zev Weissafd15042022-07-18 12:28:40 -0700160inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key)
James Feist40a72142018-12-21 10:09:53 -0800161{
162 auto it = data.find(key);
163 if (it == data.end())
164 {
165 std::cerr << "Configuration missing " << key << "\n";
166 throw std::invalid_argument("Key Missing");
167 }
168 if constexpr (std::is_same_v<T, double>)
169 {
James Feist3eb82622019-02-08 13:10:22 -0800170 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800171 }
James Feist6ef20402019-01-07 16:45:08 -0800172 else if constexpr (std::is_unsigned_v<T>)
173 {
James Feist3eb82622019-02-08 13:10:22 -0800174 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800175 }
James Feist40a72142018-12-21 10:09:53 -0800176 else if constexpr (std::is_same_v<T, std::string>)
177 {
James Feist3eb82622019-02-08 13:10:22 -0800178 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800179 }
180 else
181 {
James Feist52497fd2019-06-07 13:01:33 -0700182 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800183 }
184}
James Feistfc94b212019-02-06 16:14:51 -0800185
186inline void setReadState(const std::string& str, PowerState& val)
187{
James Feistfc94b212019-02-06 16:14:51 -0800188 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 }
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700200 else if (str == "ChassisOn")
201 {
202 val = PowerState::chassisOn;
203 }
James Feistfc94b212019-02-06 16:14:51 -0800204}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800205
Zev Weissa4d27682022-07-19 15:30:36 -0700206inline PowerState getPowerState(const SensorBaseConfigMap& cfg)
207{
208 PowerState state = PowerState::always;
209 auto findPowerState = cfg.find("PowerState");
210 if (findPowerState != cfg.end())
211 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400212 std::string powerState =
213 std::visit(VariantToStringVisitor(), findPowerState->second);
Zev Weissa4d27682022-07-19 15:30:36 -0700214 setReadState(powerState, state);
215 }
216 return state;
217}
218
Zev Weiss8569bf22022-10-11 15:37:44 -0700219inline 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
Ed Tanous8a57ec02020-10-09 12:46:52 -0700234inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist49a8ccd2020-09-16 16:09:52 -0700235 const std::string& name, bool on)
236{
237 conn->async_method_call(
238 [name](const boost::system::error_code ec) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400239 if (ec)
240 {
241 std::cerr << "Failed to set LED " << name << "\n";
242 }
243 },
James Feist49a8ccd2020-09-16 16:09:52 -0700244 "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
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800250void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700251 const std::shared_ptr<sdbusplus::asio::connection>& conn,
252 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800253 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700254
James Feist38fb5982020-05-28 10:09:54 -0700255struct GetSensorConfiguration :
256 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700257{
258 GetSensorConfiguration(
259 std::shared_ptr<sdbusplus::asio::connection> connection,
260 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
Patrick Williams2aaf7172024-08-16 15:20:40 -0400261 dbusConnection(std::move(connection)), callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700262 {}
James Feistf27a55c2020-08-04 14:27:30 -0700263
264 void getPath(const std::string& path, const std::string& interface,
265 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700266 {
James Feistf27a55c2020-08-04 14:27:30 -0700267 if (retries > 5)
268 {
269 retries = 5;
270 }
271 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
272
273 self->dbusConnection->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700274 [self, path, interface, owner, retries](
275 const boost::system::error_code ec, SensorBaseConfigMap& data) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400276 if (ec)
James Feistf27a55c2020-08-04 14:27:30 -0700277 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400278 if (retries == 0U)
Ed Tanousbb679322022-05-16 16:10:00 -0700279 {
Patrick Rudolph291c2952025-01-14 13:49:32 +0100280 std::cerr << "Error getting " << path
281 << ": no retries left\n";
Ed Tanousbb679322022-05-16 16:10:00 -0700282 return;
283 }
Patrick Rudolph291c2952025-01-14 13:49:32 +0100284 std::cerr << "Error getting " << path << ": " << retries - 1
285 << " retries left\n";
Patrick Williams2aaf7172024-08-16 15:20:40 -0400286 auto timer = std::make_shared<boost::asio::steady_timer>(
287 self->dbusConnection->get_io_context());
288 timer->expires_after(std::chrono::seconds(10));
289 timer->async_wait([self, timer, path, interface, owner,
290 retries](boost::system::error_code ec) {
291 if (ec)
292 {
293 std::cerr << "Timer error!\n";
294 return;
295 }
296 self->getPath(path, interface, owner, retries - 1);
297 });
298 return;
299 }
James Feistf27a55c2020-08-04 14:27:30 -0700300
Patrick Williams2aaf7172024-08-16 15:20:40 -0400301 self->respData[path][interface] = std::move(data);
302 },
James Feistf27a55c2020-08-04 14:27:30 -0700303 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
304 interface);
305 }
306
Zev Weiss054aad82022-08-18 01:37:34 -0700307 void getConfiguration(const std::vector<std::string>& types,
James Feistf27a55c2020-08-04 14:27:30 -0700308 size_t retries = 0)
309 {
310 if (retries > 5)
311 {
312 retries = 5;
313 }
314
Zev Weiss054aad82022-08-18 01:37:34 -0700315 std::vector<std::string> interfaces(types.size());
316 for (const auto& type : types)
317 {
318 interfaces.push_back(configInterfaceName(type));
319 }
320
James Feistc71c1192019-09-18 14:31:33 -0700321 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
322 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700323 [self, interfaces, retries](const boost::system::error_code ec,
324 const GetSubTreeType& ret) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400325 if (ec)
James Feistc71c1192019-09-18 14:31:33 -0700326 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400327 std::cerr << "Error calling mapper\n";
328 if (retries == 0U)
James Feistc71c1192019-09-18 14:31:33 -0700329 {
330 return;
331 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400332 auto timer = std::make_shared<boost::asio::steady_timer>(
333 self->dbusConnection->get_io_context());
334 timer->expires_after(std::chrono::seconds(10));
335 timer->async_wait([self, timer, interfaces,
336 retries](boost::system::error_code ec) {
337 if (ec)
338 {
339 std::cerr << "Timer error!\n";
340 return;
341 }
342 self->getConfiguration(interfaces, retries - 1);
343 });
James Feistc71c1192019-09-18 14:31:33 -0700344
Ed Tanousbb679322022-05-16 16:10:00 -0700345 return;
James Feistc71c1192019-09-18 14:31:33 -0700346 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400347 for (const auto& [path, objDict] : ret)
Ed Tanousbb679322022-05-16 16:10:00 -0700348 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400349 if (objDict.empty())
Ed Tanousbb679322022-05-16 16:10:00 -0700350 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400351 return;
Ed Tanousbb679322022-05-16 16:10:00 -0700352 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400353 const std::string& owner = objDict.begin()->first;
354
355 for (const std::string& interface : objDict.begin()->second)
356 {
357 // anything that starts with a requested configuration
358 // is good
359 if (std::find_if(
360 interfaces.begin(), interfaces.end(),
361 [interface](const std::string& possible) {
362 return interface.starts_with(possible);
363 }) == interfaces.end())
364 {
365 continue;
366 }
367 self->getPath(path, interface, owner);
368 }
Ed Tanousbb679322022-05-16 16:10:00 -0700369 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400370 },
James Feistc71c1192019-09-18 14:31:33 -0700371 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
372 "/", 0, interfaces);
373 }
374
375 ~GetSensorConfiguration()
376 {
377 callback(respData);
378 }
379
380 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
381 std::function<void(ManagedObjectType& resp)> callback;
382 ManagedObjectType respData;
383};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200384
385// The common scheme for sysfs files naming is: <type><number>_<item>.
386// This function returns optionally these 3 elements as a tuple.
Patrick Williams556e04b2025-02-01 08:22:22 -0500387std::optional<std::tuple<std::string, std::string, std::string>> splitFileName(
388 const std::filesystem::path& filePath);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200389std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700390 const double& scaleFactor);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800391void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
392bool getManufacturingMode();
Zev Weiss214d9712022-08-12 12:54:31 -0700393std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
394 setupPropertiesChangedMatches(
395 sdbusplus::asio::connection& bus, std::span<const char* const> types,
396 const std::function<void(sdbusplus::message_t&)>& handler);
Tom Tung278e1772023-12-12 09:20:40 +0800397
398template <typename T>
399bool getDeviceBusAddr(const std::string& deviceName, T& bus, T& addr)
400{
401 auto findHyphen = deviceName.find('-');
402 if (findHyphen == std::string::npos)
403 {
404 std::cerr << "found bad device " << deviceName << "\n";
405 return false;
406 }
407 std::string busStr = deviceName.substr(0, findHyphen);
408 std::string addrStr = deviceName.substr(findHyphen + 1);
409
410 std::from_chars_result res{};
411 res = std::from_chars(&*busStr.begin(), &*busStr.end(), bus);
412 if (res.ec != std::errc{} || res.ptr != &*busStr.end())
413 {
414 std::cerr << "Error finding bus for " << deviceName << "\n";
415 return false;
416 }
417 res = std::from_chars(&*addrStr.begin(), &*addrStr.end(), addr, 16);
418 if (res.ec != std::errc{} || res.ptr != &*addrStr.end())
419 {
420 std::cerr << "Error finding addr for " << deviceName << "\n";
421 return false;
422 }
423
424 return true;
425}