blob: 78a6ecd106a4e8946fd62704384e68f3add798ca [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>
10#include <sdbusplus/message/types.hpp>
11
James Feist24f02f22019-04-15 11:05:39 -070012#include <filesystem>
Patrick Venturefd6ba732019-10-31 14:27:39 -070013#include <functional>
James Feist6714a252018-09-10 15:26:18 -070014#include <iostream>
Patrick Venturefd6ba732019-10-31 14:27:39 -070015#include <memory>
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050016#include <optional>
James Feist6714a252018-09-10 15:26:18 -070017#include <regex>
Zev Weiss214d9712022-08-12 12:54:31 -070018#include <span>
Patrick Venturefd6ba732019-10-31 14:27:39 -070019#include <string>
20#include <tuple>
21#include <utility>
22#include <variant>
23#include <vector>
James Feist6714a252018-09-10 15:26:18 -070024
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070025const constexpr char* jsonStore = "/var/configuration/flattened.json";
26const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
27const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
James Feist58295ad2019-05-30 15:01:41 -070028
29constexpr const char* cpuInventoryPath =
30 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070031const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070032
33using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080034 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>;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080036using SensorBaseConfigMap =
37 boost::container::flat_map<std::string, BasicVariantType>;
38using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
39using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
40using ManagedObjectType =
41 boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
James Feist6714a252018-09-10 15:26:18 -070042
James Feista5e58722019-04-22 14:43:11 -070043using GetSubTreeType = std::vector<
44 std::pair<std::string,
45 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feistd8bd5622019-06-26 12:09:05 -070046using Association = std::tuple<std::string, std::string, std::string>;
47
Zhikui Renda98f092021-11-01 09:41:08 -070048inline std::string escapeName(const std::string& sensorName)
49{
50 return boost::replace_all_copy(sensorName, " ", "_");
51}
52
Zev Weiss88cb29d2022-05-09 03:46:15 +000053enum class PowerState
54{
55 on,
56 biosPost,
Thu Nguyen6db8aae2022-10-04 08:12:48 +070057 always,
58 chassisOn
Zev Weiss88cb29d2022-05-09 03:46:15 +000059};
60
Jason Ling100c20b2020-08-11 14:50:33 -070061std::optional<std::string> openAndRead(const std::string& hwmonFile);
Patrick Williams2aaf7172024-08-16 15:20:40 -040062std::optional<std::string> getFullHwmonFilePath(
63 const std::string& directory, const std::string& hwmonBaseName,
64 const std::set<std::string>& permitSet);
Jason Ling100c20b2020-08-11 14:50:33 -070065std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
Ed Tanous8a57ec02020-10-09 12:46:52 -070066bool findFiles(const std::filesystem::path& dirPath,
Lei YU6a4e9732021-10-20 13:27:34 +080067 std::string_view matchString,
James Feistcf3bce62019-01-08 10:07:19 -080068 std::vector<std::filesystem::path>& foundPaths,
Ed Tanous8a57ec02020-10-09 12:46:52 -070069 int symlinkDepth = 1);
Ed Tanous201a1012024-04-03 18:07:28 -070070bool isPowerOn();
71bool hasBiosPost();
72bool isChassisOn();
Zev Weiss88cb29d2022-05-09 03:46:15 +000073void setupPowerMatchCallback(
74 const std::shared_ptr<sdbusplus::asio::connection>& conn,
75 std::function<void(PowerState type, bool state)>&& callback);
James Feist71d31b22019-01-02 16:57:54 -080076void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070077bool getSensorConfiguration(
78 const std::string& type,
79 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -070080 ManagedObjectType& resp, bool useCache);
81
James Feist82bac4c2019-03-11 11:16:53 -070082void createAssociation(
83 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
84 const std::string& path);
85
James Feist87d713a2018-12-06 16:06:24 -080086// replaces limits if MinReading and MaxReading are found.
87void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080088 const SensorBaseConfiguration* data);
89
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000090bool readingStateGood(const PowerState& powerState);
91
Zev Weiss054aad82022-08-18 01:37:34 -070092constexpr const char* configInterfacePrefix =
93 "xyz.openbmc_project.Configuration.";
94
95inline std::string configInterfaceName(const std::string& type)
96{
97 return std::string(configInterfacePrefix) + type;
98}
99
James Feista5e58722019-04-22 14:43:11 -0700100namespace mapper
101{
102constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
103constexpr const char* path = "/xyz/openbmc_project/object_mapper";
104constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
105constexpr const char* subtree = "GetSubTree";
106} // namespace mapper
107
108namespace properties
109{
110constexpr const char* interface = "org.freedesktop.DBus.Properties";
111constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -0700112constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -0700113} // namespace properties
114
James Feist52497fd2019-06-07 13:01:33 -0700115namespace power
116{
Potin Laief85e0b2024-02-23 10:30:19 +0800117const static constexpr char* busname = "xyz.openbmc_project.State.Host0";
James Feist52497fd2019-06-07 13:01:33 -0700118const static constexpr char* interface = "xyz.openbmc_project.State.Host";
119const static constexpr char* path = "/xyz/openbmc_project/state/host0";
120const static constexpr char* property = "CurrentHostState";
121} // namespace power
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700122
123namespace chassis
124{
Patrick Williamsea14f142024-01-19 14:23:54 -0600125const static constexpr char* busname = "xyz.openbmc_project.State.Chassis0";
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700126const static constexpr char* interface = "xyz.openbmc_project.State.Chassis";
127const static constexpr char* path = "/xyz/openbmc_project/state/chassis0";
128const static constexpr char* property = "CurrentPowerState";
Thang Tran819eb322023-11-07 13:50:18 +0700129const static constexpr char* sOn = ".On";
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700130} // namespace chassis
131
James Feist52497fd2019-06-07 13:01:33 -0700132namespace post
133{
Potin Laief85e0b2024-02-23 10:30:19 +0800134const static constexpr char* busname = "xyz.openbmc_project.State.Host0";
James Feist52497fd2019-06-07 13:01:33 -0700135const static constexpr char* interface =
136 "xyz.openbmc_project.State.OperatingSystem.Status";
Potin Laief85e0b2024-02-23 10:30:19 +0800137const static constexpr char* path = "/xyz/openbmc_project/state/host0";
James Feist52497fd2019-06-07 13:01:33 -0700138const static constexpr char* property = "OperatingSystemState";
139} // namespace post
140
James Feist2adc95c2019-09-30 14:55:28 -0700141namespace association
142{
143const static constexpr char* interface =
144 "xyz.openbmc_project.Association.Definitions";
145} // namespace association
146
James Feist40a72142018-12-21 10:09:53 -0800147template <typename T>
Zev Weissafd15042022-07-18 12:28:40 -0700148inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key)
James Feist40a72142018-12-21 10:09:53 -0800149{
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 {
James Feist3eb82622019-02-08 13:10:22 -0800158 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800159 }
James Feist6ef20402019-01-07 16:45:08 -0800160 else if constexpr (std::is_unsigned_v<T>)
161 {
James Feist3eb82622019-02-08 13:10:22 -0800162 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800163 }
James Feist40a72142018-12-21 10:09:53 -0800164 else if constexpr (std::is_same_v<T, std::string>)
165 {
James Feist3eb82622019-02-08 13:10:22 -0800166 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800167 }
168 else
169 {
James Feist52497fd2019-06-07 13:01:33 -0700170 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800171 }
172}
James Feistfc94b212019-02-06 16:14:51 -0800173
174inline void setReadState(const std::string& str, PowerState& val)
175{
James Feistfc94b212019-02-06 16:14:51 -0800176 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 }
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700188 else if (str == "ChassisOn")
189 {
190 val = PowerState::chassisOn;
191 }
James Feistfc94b212019-02-06 16:14:51 -0800192}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800193
Zev Weissa4d27682022-07-19 15:30:36 -0700194inline PowerState getPowerState(const SensorBaseConfigMap& cfg)
195{
196 PowerState state = PowerState::always;
197 auto findPowerState = cfg.find("PowerState");
198 if (findPowerState != cfg.end())
199 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400200 std::string powerState =
201 std::visit(VariantToStringVisitor(), findPowerState->second);
Zev Weissa4d27682022-07-19 15:30:36 -0700202 setReadState(powerState, state);
203 }
204 return state;
205}
206
Zev Weiss8569bf22022-10-11 15:37:44 -0700207inline 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
Ed Tanous8a57ec02020-10-09 12:46:52 -0700222inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist49a8ccd2020-09-16 16:09:52 -0700223 const std::string& name, bool on)
224{
225 conn->async_method_call(
226 [name](const boost::system::error_code ec) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400227 if (ec)
228 {
229 std::cerr << "Failed to set LED " << name << "\n";
230 }
231 },
James Feist49a8ccd2020-09-16 16:09:52 -0700232 "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
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800238void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700239 const std::shared_ptr<sdbusplus::asio::connection>& conn,
240 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800241 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700242
James Feist38fb5982020-05-28 10:09:54 -0700243struct GetSensorConfiguration :
244 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700245{
246 GetSensorConfiguration(
247 std::shared_ptr<sdbusplus::asio::connection> connection,
248 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
Patrick Williams2aaf7172024-08-16 15:20:40 -0400249 dbusConnection(std::move(connection)), callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700250 {}
James Feistf27a55c2020-08-04 14:27:30 -0700251
252 void getPath(const std::string& path, const std::string& interface,
253 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700254 {
James Feistf27a55c2020-08-04 14:27:30 -0700255 if (retries > 5)
256 {
257 retries = 5;
258 }
259 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
260
261 self->dbusConnection->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700262 [self, path, interface, owner, retries](
263 const boost::system::error_code ec, SensorBaseConfigMap& data) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400264 if (ec)
James Feistf27a55c2020-08-04 14:27:30 -0700265 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400266 std::cerr << "Error getting " << path << ": retries left"
267 << retries - 1 << "\n";
268 if (retries == 0U)
Ed Tanousbb679322022-05-16 16:10:00 -0700269 {
Ed Tanousbb679322022-05-16 16:10:00 -0700270 return;
271 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400272 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 }
James Feistf27a55c2020-08-04 14:27:30 -0700286
Patrick Williams2aaf7172024-08-16 15:20:40 -0400287 self->respData[path][interface] = std::move(data);
288 },
James Feistf27a55c2020-08-04 14:27:30 -0700289 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
290 interface);
291 }
292
Zev Weiss054aad82022-08-18 01:37:34 -0700293 void getConfiguration(const std::vector<std::string>& types,
James Feistf27a55c2020-08-04 14:27:30 -0700294 size_t retries = 0)
295 {
296 if (retries > 5)
297 {
298 retries = 5;
299 }
300
Zev Weiss054aad82022-08-18 01:37:34 -0700301 std::vector<std::string> interfaces(types.size());
302 for (const auto& type : types)
303 {
304 interfaces.push_back(configInterfaceName(type));
305 }
306
James Feistc71c1192019-09-18 14:31:33 -0700307 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
308 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700309 [self, interfaces, retries](const boost::system::error_code ec,
310 const GetSubTreeType& ret) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400311 if (ec)
James Feistc71c1192019-09-18 14:31:33 -0700312 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400313 std::cerr << "Error calling mapper\n";
314 if (retries == 0U)
James Feistc71c1192019-09-18 14:31:33 -0700315 {
316 return;
317 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400318 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 });
James Feistc71c1192019-09-18 14:31:33 -0700330
Ed Tanousbb679322022-05-16 16:10:00 -0700331 return;
James Feistc71c1192019-09-18 14:31:33 -0700332 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400333 for (const auto& [path, objDict] : ret)
Ed Tanousbb679322022-05-16 16:10:00 -0700334 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400335 if (objDict.empty())
Ed Tanousbb679322022-05-16 16:10:00 -0700336 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400337 return;
Ed Tanousbb679322022-05-16 16:10:00 -0700338 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400339 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 }
Ed Tanousbb679322022-05-16 16:10:00 -0700355 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400356 },
James Feistc71c1192019-09-18 14:31:33 -0700357 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
358 "/", 0, interfaces);
359 }
360
361 ~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};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200370
371// The common scheme for sysfs files naming is: <type><number>_<item>.
372// This function returns optionally these 3 elements as a tuple.
373std::optional<std::tuple<std::string, std::string, std::string>>
374 splitFileName(const std::filesystem::path& filePath);
375std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700376 const double& scaleFactor);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800377void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
378bool getManufacturingMode();
Zev Weiss214d9712022-08-12 12:54:31 -0700379std::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);
Tom Tung278e1772023-12-12 09:20:40 +0800383
384template <typename T>
385bool 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}