blob: 444030c148cd9a952ff95a5557e5c3ae33aa74cd [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);
62std::optional<std::string>
63 getFullHwmonFilePath(const std::string& directory,
64 const std::string& hwmonBaseName,
65 const std::set<std::string>& permitSet);
66std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
Ed Tanous8a57ec02020-10-09 12:46:52 -070067bool findFiles(const std::filesystem::path& dirPath,
Lei YU6a4e9732021-10-20 13:27:34 +080068 std::string_view matchString,
James Feistcf3bce62019-01-08 10:07:19 -080069 std::vector<std::filesystem::path>& foundPaths,
Ed Tanous8a57ec02020-10-09 12:46:52 -070070 int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080071bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080072bool hasBiosPost(void);
Thu Nguyen6db8aae2022-10-04 08:12:48 +070073bool isChassisOn(void);
Zev Weiss88cb29d2022-05-09 03:46:15 +000074void setupPowerMatchCallback(
75 const std::shared_ptr<sdbusplus::asio::connection>& conn,
76 std::function<void(PowerState type, bool state)>&& callback);
James Feist71d31b22019-01-02 16:57:54 -080077void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070078bool getSensorConfiguration(
79 const std::string& type,
80 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -070081 ManagedObjectType& resp, bool useCache);
82
83bool getSensorConfiguration(
84 const std::string& type,
85 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
86 ManagedObjectType& resp);
James Feist87d713a2018-12-06 16:06:24 -080087
James Feist82bac4c2019-03-11 11:16:53 -070088void createAssociation(
89 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
90 const std::string& path);
91
James Feist87d713a2018-12-06 16:06:24 -080092// replaces limits if MinReading and MaxReading are found.
93void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080094 const SensorBaseConfiguration* data);
95
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000096bool readingStateGood(const PowerState& powerState);
97
Zev Weiss054aad82022-08-18 01:37:34 -070098constexpr const char* configInterfacePrefix =
99 "xyz.openbmc_project.Configuration.";
100
101inline std::string configInterfaceName(const std::string& type)
102{
103 return std::string(configInterfacePrefix) + type;
104}
105
James Feista5e58722019-04-22 14:43:11 -0700106namespace mapper
107{
108constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
109constexpr const char* path = "/xyz/openbmc_project/object_mapper";
110constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
111constexpr const char* subtree = "GetSubTree";
112} // namespace mapper
113
114namespace properties
115{
116constexpr const char* interface = "org.freedesktop.DBus.Properties";
117constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -0700118constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -0700119} // namespace properties
120
James Feist52497fd2019-06-07 13:01:33 -0700121namespace power
122{
123const static constexpr char* busname = "xyz.openbmc_project.State.Host";
124const static constexpr char* interface = "xyz.openbmc_project.State.Host";
125const static constexpr char* path = "/xyz/openbmc_project/state/host0";
126const static constexpr char* property = "CurrentHostState";
127} // namespace power
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700128
129namespace chassis
130{
131const static constexpr char* busname = "xyz.openbmc_project.State.Chassis";
132const static constexpr char* interface = "xyz.openbmc_project.State.Chassis";
133const static constexpr char* path = "/xyz/openbmc_project/state/chassis0";
134const static constexpr char* property = "CurrentPowerState";
135const static constexpr char* sOn = "On";
136} // namespace chassis
137
James Feist52497fd2019-06-07 13:01:33 -0700138namespace post
139{
140const static constexpr char* busname =
141 "xyz.openbmc_project.State.OperatingSystem";
142const static constexpr char* interface =
143 "xyz.openbmc_project.State.OperatingSystem.Status";
144const static constexpr char* path = "/xyz/openbmc_project/state/os";
145const static constexpr char* property = "OperatingSystemState";
146} // namespace post
147
James Feist2adc95c2019-09-30 14:55:28 -0700148namespace association
149{
150const static constexpr char* interface =
151 "xyz.openbmc_project.Association.Definitions";
152} // namespace association
153
James Feist40a72142018-12-21 10:09:53 -0800154template <typename T>
Zev Weissafd15042022-07-18 12:28:40 -0700155inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key)
James Feist40a72142018-12-21 10:09:53 -0800156{
157 auto it = data.find(key);
158 if (it == data.end())
159 {
160 std::cerr << "Configuration missing " << key << "\n";
161 throw std::invalid_argument("Key Missing");
162 }
163 if constexpr (std::is_same_v<T, double>)
164 {
James Feist3eb82622019-02-08 13:10:22 -0800165 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800166 }
James Feist6ef20402019-01-07 16:45:08 -0800167 else if constexpr (std::is_unsigned_v<T>)
168 {
James Feist3eb82622019-02-08 13:10:22 -0800169 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800170 }
James Feist40a72142018-12-21 10:09:53 -0800171 else if constexpr (std::is_same_v<T, std::string>)
172 {
James Feist3eb82622019-02-08 13:10:22 -0800173 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800174 }
175 else
176 {
James Feist52497fd2019-06-07 13:01:33 -0700177 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800178 }
179}
James Feistfc94b212019-02-06 16:14:51 -0800180
181inline void setReadState(const std::string& str, PowerState& val)
182{
183
184 if (str == "On")
185 {
186 val = PowerState::on;
187 }
188 else if (str == "BiosPost")
189 {
190 val = PowerState::biosPost;
191 }
192 else if (str == "Always")
193 {
194 val = PowerState::always;
195 }
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700196 else if (str == "ChassisOn")
197 {
198 val = PowerState::chassisOn;
199 }
James Feistfc94b212019-02-06 16:14:51 -0800200}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800201
Zev Weissa4d27682022-07-19 15:30:36 -0700202inline PowerState getPowerState(const SensorBaseConfigMap& cfg)
203{
204 PowerState state = PowerState::always;
205 auto findPowerState = cfg.find("PowerState");
206 if (findPowerState != cfg.end())
207 {
208 std::string powerState =
209 std::visit(VariantToStringVisitor(), findPowerState->second);
210 setReadState(powerState, state);
211 }
212 return state;
213}
214
Zev Weiss8569bf22022-10-11 15:37:44 -0700215inline float getPollRate(const SensorBaseConfigMap& cfg, float dflt)
216{
217 float pollRate = dflt;
218 auto findPollRate = cfg.find("PollRate");
219 if (findPollRate != cfg.end())
220 {
221 pollRate = std::visit(VariantToFloatVisitor(), findPollRate->second);
222 if (!std::isfinite(pollRate) || pollRate <= 0.0F)
223 {
224 pollRate = dflt; // poll time invalid, fall back to default
225 }
226 }
227 return pollRate;
228}
229
Ed Tanous8a57ec02020-10-09 12:46:52 -0700230inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist49a8ccd2020-09-16 16:09:52 -0700231 const std::string& name, bool on)
232{
233 conn->async_method_call(
234 [name](const boost::system::error_code ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700235 if (ec)
236 {
237 std::cerr << "Failed to set LED " << name << "\n";
238 }
James Feist49a8ccd2020-09-16 16:09:52 -0700239 },
240 "xyz.openbmc_project.LED.GroupManager",
241 "/xyz/openbmc_project/led/groups/" + name, properties::interface,
242 properties::set, "xyz.openbmc_project.Led.Group", "Asserted",
243 std::variant<bool>(on));
244}
245
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800246void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700247 const std::shared_ptr<sdbusplus::asio::connection>& conn,
248 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800249 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700250
James Feist38fb5982020-05-28 10:09:54 -0700251struct GetSensorConfiguration :
252 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700253{
254 GetSensorConfiguration(
255 std::shared_ptr<sdbusplus::asio::connection> connection,
256 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
Ed Tanous8a57ec02020-10-09 12:46:52 -0700257 dbusConnection(std::move(connection)),
James Feistc71c1192019-09-18 14:31:33 -0700258 callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700259 {}
James Feistf27a55c2020-08-04 14:27:30 -0700260
261 void getPath(const std::string& path, const std::string& interface,
262 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700263 {
James Feistf27a55c2020-08-04 14:27:30 -0700264 if (retries > 5)
265 {
266 retries = 5;
267 }
268 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
269
270 self->dbusConnection->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700271 [self, path, interface, owner, retries](
272 const boost::system::error_code ec, SensorBaseConfigMap& data) {
Ed Tanousbb679322022-05-16 16:10:00 -0700273 if (ec)
274 {
275 std::cerr << "Error getting " << path << ": retries left"
276 << retries - 1 << "\n";
Ed Tanous2049bd22022-07-09 07:20:26 -0700277 if (retries == 0U)
James Feistf27a55c2020-08-04 14:27:30 -0700278 {
James Feistf27a55c2020-08-04 14:27:30 -0700279 return;
280 }
Ed Tanousbb679322022-05-16 16:10:00 -0700281 auto timer = std::make_shared<boost::asio::steady_timer>(
282 self->dbusConnection->get_io_context());
283 timer->expires_after(std::chrono::seconds(10));
284 timer->async_wait([self, timer, path, interface, owner,
285 retries](boost::system::error_code ec) {
286 if (ec)
287 {
288 std::cerr << "Timer error!\n";
289 return;
290 }
291 self->getPath(path, interface, owner, retries - 1);
292 });
293 return;
294 }
James Feistf27a55c2020-08-04 14:27:30 -0700295
Ed Tanousbb679322022-05-16 16:10:00 -0700296 self->respData[path][interface] = std::move(data);
James Feistf27a55c2020-08-04 14:27:30 -0700297 },
298 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
299 interface);
300 }
301
Zev Weiss054aad82022-08-18 01:37:34 -0700302 void getConfiguration(const std::vector<std::string>& types,
James Feistf27a55c2020-08-04 14:27:30 -0700303 size_t retries = 0)
304 {
305 if (retries > 5)
306 {
307 retries = 5;
308 }
309
Zev Weiss054aad82022-08-18 01:37:34 -0700310 std::vector<std::string> interfaces(types.size());
311 for (const auto& type : types)
312 {
313 interfaces.push_back(configInterfaceName(type));
314 }
315
James Feistc71c1192019-09-18 14:31:33 -0700316 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
317 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700318 [self, interfaces, retries](const boost::system::error_code ec,
319 const GetSubTreeType& ret) {
Ed Tanousbb679322022-05-16 16:10:00 -0700320 if (ec)
321 {
322 std::cerr << "Error calling mapper\n";
Ed Tanous2049bd22022-07-09 07:20:26 -0700323 if (retries == 0U)
James Feistc71c1192019-09-18 14:31:33 -0700324 {
James Feistc71c1192019-09-18 14:31:33 -0700325 return;
326 }
Ed Tanousbb679322022-05-16 16:10:00 -0700327 auto timer = std::make_shared<boost::asio::steady_timer>(
328 self->dbusConnection->get_io_context());
329 timer->expires_after(std::chrono::seconds(10));
330 timer->async_wait([self, timer, interfaces,
331 retries](boost::system::error_code ec) {
332 if (ec)
James Feistc71c1192019-09-18 14:31:33 -0700333 {
Ed Tanousbb679322022-05-16 16:10:00 -0700334 std::cerr << "Timer error!\n";
James Feistc71c1192019-09-18 14:31:33 -0700335 return;
336 }
Ed Tanousbb679322022-05-16 16:10:00 -0700337 self->getConfiguration(interfaces, retries - 1);
338 });
James Feistc71c1192019-09-18 14:31:33 -0700339
Ed Tanousbb679322022-05-16 16:10:00 -0700340 return;
341 }
342 for (const auto& [path, objDict] : ret)
343 {
344 if (objDict.empty())
345 {
346 return;
James Feistc71c1192019-09-18 14:31:33 -0700347 }
Ed Tanousbb679322022-05-16 16:10:00 -0700348 const std::string& owner = objDict.begin()->first;
349
350 for (const std::string& interface : objDict.begin()->second)
351 {
352 // anything that starts with a requested configuration
353 // is good
354 if (std::find_if(interfaces.begin(), interfaces.end(),
355 [interface](const std::string& possible) {
Zev Weiss6c106d62022-08-17 20:50:00 -0700356 return interface.starts_with(possible);
Ed Tanousbb679322022-05-16 16:10:00 -0700357 }) == interfaces.end())
358 {
359 continue;
360 }
361 self->getPath(path, interface, owner);
362 }
363 }
James Feistc71c1192019-09-18 14:31:33 -0700364 },
365 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
366 "/", 0, interfaces);
367 }
368
369 ~GetSensorConfiguration()
370 {
371 callback(respData);
372 }
373
374 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
375 std::function<void(ManagedObjectType& resp)> callback;
376 ManagedObjectType respData;
377};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200378
379// The common scheme for sysfs files naming is: <type><number>_<item>.
380// This function returns optionally these 3 elements as a tuple.
381std::optional<std::tuple<std::string, std::string, std::string>>
382 splitFileName(const std::filesystem::path& filePath);
383std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700384 const double& scaleFactor);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800385void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
386bool getManufacturingMode();
Zev Weiss214d9712022-08-12 12:54:31 -0700387std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
388 setupPropertiesChangedMatches(
389 sdbusplus::asio::connection& bus, std::span<const char* const> types,
390 const std::function<void(sdbusplus::message_t&)>& handler);