blob: 91b29986b2d8aa9ad1857f24d19caf53d336d3f5 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Ed Tanous8a57ec02020-10-09 12:46:52 -07002#include <VariantVisitors.hpp>
Zhikui Renda98f092021-11-01 09:41:08 -07003#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -07004#include <boost/asio/steady_timer.hpp>
James Feist6714a252018-09-10 15:26:18 -07005#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -07006#include <sdbusplus/asio/connection.hpp>
7#include <sdbusplus/asio/object_server.hpp>
8#include <sdbusplus/message/types.hpp>
9
James Feist24f02f22019-04-15 11:05:39 -070010#include <filesystem>
Patrick Venturefd6ba732019-10-31 14:27:39 -070011#include <functional>
James Feist6714a252018-09-10 15:26:18 -070012#include <iostream>
Patrick Venturefd6ba732019-10-31 14:27:39 -070013#include <memory>
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050014#include <optional>
James Feist6714a252018-09-10 15:26:18 -070015#include <regex>
Zev Weiss214d9712022-08-12 12:54:31 -070016#include <span>
Patrick Venturefd6ba732019-10-31 14:27:39 -070017#include <string>
18#include <tuple>
19#include <utility>
20#include <variant>
21#include <vector>
James Feist6714a252018-09-10 15:26:18 -070022
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070023const constexpr char* jsonStore = "/var/configuration/flattened.json";
24const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
25const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
James Feist58295ad2019-05-30 15:01:41 -070026
27constexpr const char* cpuInventoryPath =
28 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070029const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070030
31using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080032 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
33 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080034using SensorBaseConfigMap =
35 boost::container::flat_map<std::string, BasicVariantType>;
36using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
37using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
38using ManagedObjectType =
39 boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
James Feist6714a252018-09-10 15:26:18 -070040
James Feista5e58722019-04-22 14:43:11 -070041using GetSubTreeType = std::vector<
42 std::pair<std::string,
43 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feistd8bd5622019-06-26 12:09:05 -070044using Association = std::tuple<std::string, std::string, std::string>;
45
Zhikui Renda98f092021-11-01 09:41:08 -070046inline std::string escapeName(const std::string& sensorName)
47{
48 return boost::replace_all_copy(sensorName, " ", "_");
49}
50
Zev Weiss88cb29d2022-05-09 03:46:15 +000051enum class PowerState
52{
53 on,
54 biosPost,
Thu Nguyen6db8aae2022-10-04 08:12:48 +070055 always,
56 chassisOn
Zev Weiss88cb29d2022-05-09 03:46:15 +000057};
58
Jason Ling100c20b2020-08-11 14:50:33 -070059std::optional<std::string> openAndRead(const std::string& hwmonFile);
60std::optional<std::string>
61 getFullHwmonFilePath(const std::string& directory,
62 const std::string& hwmonBaseName,
63 const std::set<std::string>& permitSet);
64std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
Ed Tanous8a57ec02020-10-09 12:46:52 -070065bool findFiles(const std::filesystem::path& dirPath,
Lei YU6a4e9732021-10-20 13:27:34 +080066 std::string_view matchString,
James Feistcf3bce62019-01-08 10:07:19 -080067 std::vector<std::filesystem::path>& foundPaths,
Ed Tanous8a57ec02020-10-09 12:46:52 -070068 int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080069bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080070bool hasBiosPost(void);
Thu Nguyen6db8aae2022-10-04 08:12:48 +070071bool isChassisOn(void);
Zev Weiss88cb29d2022-05-09 03:46:15 +000072void setupPowerMatchCallback(
73 const std::shared_ptr<sdbusplus::asio::connection>& conn,
74 std::function<void(PowerState type, bool state)>&& callback);
James Feist71d31b22019-01-02 16:57:54 -080075void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070076bool getSensorConfiguration(
77 const std::string& type,
78 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -070079 ManagedObjectType& resp, bool useCache);
80
81bool getSensorConfiguration(
82 const std::string& type,
83 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
84 ManagedObjectType& resp);
James Feist87d713a2018-12-06 16:06:24 -080085
James Feist82bac4c2019-03-11 11:16:53 -070086void createAssociation(
87 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
88 const std::string& path);
89
James Feist87d713a2018-12-06 16:06:24 -080090// replaces limits if MinReading and MaxReading are found.
91void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080092 const SensorBaseConfiguration* data);
93
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000094bool readingStateGood(const PowerState& powerState);
95
Zev Weiss054aad82022-08-18 01:37:34 -070096constexpr const char* configInterfacePrefix =
97 "xyz.openbmc_project.Configuration.";
98
99inline std::string configInterfaceName(const std::string& type)
100{
101 return std::string(configInterfacePrefix) + type;
102}
103
James Feista5e58722019-04-22 14:43:11 -0700104namespace mapper
105{
106constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
107constexpr const char* path = "/xyz/openbmc_project/object_mapper";
108constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
109constexpr const char* subtree = "GetSubTree";
110} // namespace mapper
111
112namespace properties
113{
114constexpr const char* interface = "org.freedesktop.DBus.Properties";
115constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -0700116constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -0700117} // namespace properties
118
James Feist52497fd2019-06-07 13:01:33 -0700119namespace power
120{
121const static constexpr char* busname = "xyz.openbmc_project.State.Host";
122const static constexpr char* interface = "xyz.openbmc_project.State.Host";
123const static constexpr char* path = "/xyz/openbmc_project/state/host0";
124const static constexpr char* property = "CurrentHostState";
125} // namespace power
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700126
127namespace chassis
128{
129const static constexpr char* busname = "xyz.openbmc_project.State.Chassis";
130const static constexpr char* interface = "xyz.openbmc_project.State.Chassis";
131const static constexpr char* path = "/xyz/openbmc_project/state/chassis0";
132const static constexpr char* property = "CurrentPowerState";
133const static constexpr char* sOn = "On";
134} // namespace chassis
135
James Feist52497fd2019-06-07 13:01:33 -0700136namespace post
137{
138const static constexpr char* busname =
139 "xyz.openbmc_project.State.OperatingSystem";
140const static constexpr char* interface =
141 "xyz.openbmc_project.State.OperatingSystem.Status";
142const static constexpr char* path = "/xyz/openbmc_project/state/os";
143const static constexpr char* property = "OperatingSystemState";
144} // namespace post
145
James Feist2adc95c2019-09-30 14:55:28 -0700146namespace association
147{
148const static constexpr char* interface =
149 "xyz.openbmc_project.Association.Definitions";
150} // namespace association
151
James Feist40a72142018-12-21 10:09:53 -0800152template <typename T>
Zev Weissafd15042022-07-18 12:28:40 -0700153inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key)
James Feist40a72142018-12-21 10:09:53 -0800154{
155 auto it = data.find(key);
156 if (it == data.end())
157 {
158 std::cerr << "Configuration missing " << key << "\n";
159 throw std::invalid_argument("Key Missing");
160 }
161 if constexpr (std::is_same_v<T, double>)
162 {
James Feist3eb82622019-02-08 13:10:22 -0800163 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800164 }
James Feist6ef20402019-01-07 16:45:08 -0800165 else if constexpr (std::is_unsigned_v<T>)
166 {
James Feist3eb82622019-02-08 13:10:22 -0800167 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800168 }
James Feist40a72142018-12-21 10:09:53 -0800169 else if constexpr (std::is_same_v<T, std::string>)
170 {
James Feist3eb82622019-02-08 13:10:22 -0800171 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800172 }
173 else
174 {
James Feist52497fd2019-06-07 13:01:33 -0700175 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800176 }
177}
James Feistfc94b212019-02-06 16:14:51 -0800178
179inline void setReadState(const std::string& str, PowerState& val)
180{
181
182 if (str == "On")
183 {
184 val = PowerState::on;
185 }
186 else if (str == "BiosPost")
187 {
188 val = PowerState::biosPost;
189 }
190 else if (str == "Always")
191 {
192 val = PowerState::always;
193 }
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700194 else if (str == "ChassisOn")
195 {
196 val = PowerState::chassisOn;
197 }
James Feistfc94b212019-02-06 16:14:51 -0800198}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800199
Zev Weissa4d27682022-07-19 15:30:36 -0700200inline PowerState getPowerState(const SensorBaseConfigMap& cfg)
201{
202 PowerState state = PowerState::always;
203 auto findPowerState = cfg.find("PowerState");
204 if (findPowerState != cfg.end())
205 {
206 std::string powerState =
207 std::visit(VariantToStringVisitor(), findPowerState->second);
208 setReadState(powerState, state);
209 }
210 return state;
211}
212
Zev Weiss8569bf22022-10-11 15:37:44 -0700213inline float getPollRate(const SensorBaseConfigMap& cfg, float dflt)
214{
215 float pollRate = dflt;
216 auto findPollRate = cfg.find("PollRate");
217 if (findPollRate != cfg.end())
218 {
219 pollRate = std::visit(VariantToFloatVisitor(), findPollRate->second);
220 if (!std::isfinite(pollRate) || pollRate <= 0.0F)
221 {
222 pollRate = dflt; // poll time invalid, fall back to default
223 }
224 }
225 return pollRate;
226}
227
Ed Tanous8a57ec02020-10-09 12:46:52 -0700228inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist49a8ccd2020-09-16 16:09:52 -0700229 const std::string& name, bool on)
230{
231 conn->async_method_call(
232 [name](const boost::system::error_code ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700233 if (ec)
234 {
235 std::cerr << "Failed to set LED " << name << "\n";
236 }
James Feist49a8ccd2020-09-16 16:09:52 -0700237 },
238 "xyz.openbmc_project.LED.GroupManager",
239 "/xyz/openbmc_project/led/groups/" + name, properties::interface,
240 properties::set, "xyz.openbmc_project.Led.Group", "Asserted",
241 std::variant<bool>(on));
242}
243
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800244void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700245 const std::shared_ptr<sdbusplus::asio::connection>& conn,
246 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800247 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700248
James Feist38fb5982020-05-28 10:09:54 -0700249struct GetSensorConfiguration :
250 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700251{
252 GetSensorConfiguration(
253 std::shared_ptr<sdbusplus::asio::connection> connection,
254 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
Ed Tanous8a57ec02020-10-09 12:46:52 -0700255 dbusConnection(std::move(connection)),
James Feistc71c1192019-09-18 14:31:33 -0700256 callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700257 {}
James Feistf27a55c2020-08-04 14:27:30 -0700258
259 void getPath(const std::string& path, const std::string& interface,
260 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700261 {
James Feistf27a55c2020-08-04 14:27:30 -0700262 if (retries > 5)
263 {
264 retries = 5;
265 }
266 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
267
268 self->dbusConnection->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700269 [self, path, interface, owner, retries](
270 const boost::system::error_code ec, SensorBaseConfigMap& data) {
Ed Tanousbb679322022-05-16 16:10:00 -0700271 if (ec)
272 {
273 std::cerr << "Error getting " << path << ": retries left"
274 << retries - 1 << "\n";
Ed Tanous2049bd22022-07-09 07:20:26 -0700275 if (retries == 0U)
James Feistf27a55c2020-08-04 14:27:30 -0700276 {
James Feistf27a55c2020-08-04 14:27:30 -0700277 return;
278 }
Ed Tanousbb679322022-05-16 16:10:00 -0700279 auto timer = std::make_shared<boost::asio::steady_timer>(
280 self->dbusConnection->get_io_context());
281 timer->expires_after(std::chrono::seconds(10));
282 timer->async_wait([self, timer, path, interface, owner,
283 retries](boost::system::error_code ec) {
284 if (ec)
285 {
286 std::cerr << "Timer error!\n";
287 return;
288 }
289 self->getPath(path, interface, owner, retries - 1);
290 });
291 return;
292 }
James Feistf27a55c2020-08-04 14:27:30 -0700293
Ed Tanousbb679322022-05-16 16:10:00 -0700294 self->respData[path][interface] = std::move(data);
James Feistf27a55c2020-08-04 14:27:30 -0700295 },
296 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
297 interface);
298 }
299
Zev Weiss054aad82022-08-18 01:37:34 -0700300 void getConfiguration(const std::vector<std::string>& types,
James Feistf27a55c2020-08-04 14:27:30 -0700301 size_t retries = 0)
302 {
303 if (retries > 5)
304 {
305 retries = 5;
306 }
307
Zev Weiss054aad82022-08-18 01:37:34 -0700308 std::vector<std::string> interfaces(types.size());
309 for (const auto& type : types)
310 {
311 interfaces.push_back(configInterfaceName(type));
312 }
313
James Feistc71c1192019-09-18 14:31:33 -0700314 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
315 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700316 [self, interfaces, retries](const boost::system::error_code ec,
317 const GetSubTreeType& ret) {
Ed Tanousbb679322022-05-16 16:10:00 -0700318 if (ec)
319 {
320 std::cerr << "Error calling mapper\n";
Ed Tanous2049bd22022-07-09 07:20:26 -0700321 if (retries == 0U)
James Feistc71c1192019-09-18 14:31:33 -0700322 {
James Feistc71c1192019-09-18 14:31:33 -0700323 return;
324 }
Ed Tanousbb679322022-05-16 16:10:00 -0700325 auto timer = std::make_shared<boost::asio::steady_timer>(
326 self->dbusConnection->get_io_context());
327 timer->expires_after(std::chrono::seconds(10));
328 timer->async_wait([self, timer, interfaces,
329 retries](boost::system::error_code ec) {
330 if (ec)
James Feistc71c1192019-09-18 14:31:33 -0700331 {
Ed Tanousbb679322022-05-16 16:10:00 -0700332 std::cerr << "Timer error!\n";
James Feistc71c1192019-09-18 14:31:33 -0700333 return;
334 }
Ed Tanousbb679322022-05-16 16:10:00 -0700335 self->getConfiguration(interfaces, retries - 1);
336 });
James Feistc71c1192019-09-18 14:31:33 -0700337
Ed Tanousbb679322022-05-16 16:10:00 -0700338 return;
339 }
340 for (const auto& [path, objDict] : ret)
341 {
342 if (objDict.empty())
343 {
344 return;
James Feistc71c1192019-09-18 14:31:33 -0700345 }
Ed Tanousbb679322022-05-16 16:10:00 -0700346 const std::string& owner = objDict.begin()->first;
347
348 for (const std::string& interface : objDict.begin()->second)
349 {
350 // anything that starts with a requested configuration
351 // is good
352 if (std::find_if(interfaces.begin(), interfaces.end(),
353 [interface](const std::string& possible) {
Zev Weiss6c106d62022-08-17 20:50:00 -0700354 return interface.starts_with(possible);
Ed Tanousbb679322022-05-16 16:10:00 -0700355 }) == interfaces.end())
356 {
357 continue;
358 }
359 self->getPath(path, interface, owner);
360 }
361 }
James Feistc71c1192019-09-18 14:31:33 -0700362 },
363 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
364 "/", 0, interfaces);
365 }
366
367 ~GetSensorConfiguration()
368 {
369 callback(respData);
370 }
371
372 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
373 std::function<void(ManagedObjectType& resp)> callback;
374 ManagedObjectType respData;
375};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200376
377// The common scheme for sysfs files naming is: <type><number>_<item>.
378// This function returns optionally these 3 elements as a tuple.
379std::optional<std::tuple<std::string, std::string, std::string>>
380 splitFileName(const std::filesystem::path& filePath);
381std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700382 const double& scaleFactor);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800383void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
384bool getManufacturingMode();
Zev Weiss214d9712022-08-12 12:54:31 -0700385std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
386 setupPropertiesChangedMatches(
387 sdbusplus::asio::connection& bus, std::span<const char* const> types,
388 const std::function<void(sdbusplus::message_t&)>& handler);