blob: b823e5b226a4b88f2d9691ebfb3e7cd8a78b42f0 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Ed Tanous8a57ec02020-10-09 12:46:52 -07002#include <VariantVisitors.hpp>
James Feistc71c1192019-09-18 14:31:33 -07003#include <boost/algorithm/string/predicate.hpp>
Zhikui Renda98f092021-11-01 09:41:08 -07004#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -07005#include <boost/asio/steady_timer.hpp>
James Feist6714a252018-09-10 15:26:18 -07006#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -07007#include <sdbusplus/asio/connection.hpp>
8#include <sdbusplus/asio/object_server.hpp>
9#include <sdbusplus/message/types.hpp>
10
James Feist24f02f22019-04-15 11:05:39 -070011#include <filesystem>
Patrick Venturefd6ba732019-10-31 14:27:39 -070012#include <functional>
James Feist6714a252018-09-10 15:26:18 -070013#include <iostream>
Patrick Venturefd6ba732019-10-31 14:27:39 -070014#include <memory>
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050015#include <optional>
James Feist6714a252018-09-10 15:26:18 -070016#include <regex>
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
Jason Ling100c20b2020-08-11 14:50:33 -070051std::optional<std::string> openAndRead(const std::string& hwmonFile);
52std::optional<std::string>
53 getFullHwmonFilePath(const std::string& directory,
54 const std::string& hwmonBaseName,
55 const std::set<std::string>& permitSet);
56std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
Ed Tanous8a57ec02020-10-09 12:46:52 -070057bool findFiles(const std::filesystem::path& dirPath,
Lei YU6a4e9732021-10-20 13:27:34 +080058 std::string_view matchString,
James Feistcf3bce62019-01-08 10:07:19 -080059 std::vector<std::filesystem::path>& foundPaths,
Ed Tanous8a57ec02020-10-09 12:46:52 -070060 int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080061bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080062bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080063void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070064bool getSensorConfiguration(
65 const std::string& type,
66 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -070067 ManagedObjectType& resp, bool useCache);
68
69bool getSensorConfiguration(
70 const std::string& type,
71 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
72 ManagedObjectType& resp);
James Feist87d713a2018-12-06 16:06:24 -080073
James Feist82bac4c2019-03-11 11:16:53 -070074void createAssociation(
75 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
76 const std::string& path);
77
James Feist87d713a2018-12-06 16:06:24 -080078// replaces limits if MinReading and MaxReading are found.
79void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080080 const SensorBaseConfiguration* data);
81
James Feistfc94b212019-02-06 16:14:51 -080082enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080083{
84 on,
James Feistfc94b212019-02-06 16:14:51 -080085 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080086 always
87};
88
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000089bool readingStateGood(const PowerState& powerState);
90
James Feista5e58722019-04-22 14:43:11 -070091namespace mapper
92{
93constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
94constexpr const char* path = "/xyz/openbmc_project/object_mapper";
95constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
96constexpr const char* subtree = "GetSubTree";
97} // namespace mapper
98
99namespace properties
100{
101constexpr const char* interface = "org.freedesktop.DBus.Properties";
102constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -0700103constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -0700104} // namespace properties
105
James Feist52497fd2019-06-07 13:01:33 -0700106namespace power
107{
108const static constexpr char* busname = "xyz.openbmc_project.State.Host";
109const static constexpr char* interface = "xyz.openbmc_project.State.Host";
110const static constexpr char* path = "/xyz/openbmc_project/state/host0";
111const static constexpr char* property = "CurrentHostState";
112} // namespace power
113namespace post
114{
115const static constexpr char* busname =
116 "xyz.openbmc_project.State.OperatingSystem";
117const static constexpr char* interface =
118 "xyz.openbmc_project.State.OperatingSystem.Status";
119const static constexpr char* path = "/xyz/openbmc_project/state/os";
120const static constexpr char* property = "OperatingSystemState";
121} // namespace post
122
James Feist2adc95c2019-09-30 14:55:28 -0700123namespace association
124{
125const static constexpr char* interface =
126 "xyz.openbmc_project.Association.Definitions";
127} // namespace association
128
James Feist40a72142018-12-21 10:09:53 -0800129template <typename T>
Zev Weissafd15042022-07-18 12:28:40 -0700130inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key)
James Feist40a72142018-12-21 10:09:53 -0800131{
132 auto it = data.find(key);
133 if (it == data.end())
134 {
135 std::cerr << "Configuration missing " << key << "\n";
136 throw std::invalid_argument("Key Missing");
137 }
138 if constexpr (std::is_same_v<T, double>)
139 {
James Feist3eb82622019-02-08 13:10:22 -0800140 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800141 }
James Feist6ef20402019-01-07 16:45:08 -0800142 else if constexpr (std::is_unsigned_v<T>)
143 {
James Feist3eb82622019-02-08 13:10:22 -0800144 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800145 }
James Feist40a72142018-12-21 10:09:53 -0800146 else if constexpr (std::is_same_v<T, std::string>)
147 {
James Feist3eb82622019-02-08 13:10:22 -0800148 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800149 }
150 else
151 {
James Feist52497fd2019-06-07 13:01:33 -0700152 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800153 }
154}
James Feistfc94b212019-02-06 16:14:51 -0800155
156inline void setReadState(const std::string& str, PowerState& val)
157{
158
159 if (str == "On")
160 {
161 val = PowerState::on;
162 }
163 else if (str == "BiosPost")
164 {
165 val = PowerState::biosPost;
166 }
167 else if (str == "Always")
168 {
169 val = PowerState::always;
170 }
171}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800172
Zev Weissa4d27682022-07-19 15:30:36 -0700173inline PowerState getPowerState(const SensorBaseConfigMap& cfg)
174{
175 PowerState state = PowerState::always;
176 auto findPowerState = cfg.find("PowerState");
177 if (findPowerState != cfg.end())
178 {
179 std::string powerState =
180 std::visit(VariantToStringVisitor(), findPowerState->second);
181 setReadState(powerState, state);
182 }
183 return state;
184}
185
Ed Tanous8a57ec02020-10-09 12:46:52 -0700186inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist49a8ccd2020-09-16 16:09:52 -0700187 const std::string& name, bool on)
188{
189 conn->async_method_call(
190 [name](const boost::system::error_code ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700191 if (ec)
192 {
193 std::cerr << "Failed to set LED " << name << "\n";
194 }
James Feist49a8ccd2020-09-16 16:09:52 -0700195 },
196 "xyz.openbmc_project.LED.GroupManager",
197 "/xyz/openbmc_project/led/groups/" + name, properties::interface,
198 properties::set, "xyz.openbmc_project.Led.Group", "Asserted",
199 std::variant<bool>(on));
200}
201
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800202void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700203 const std::shared_ptr<sdbusplus::asio::connection>& conn,
204 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800205 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700206
James Feist38fb5982020-05-28 10:09:54 -0700207struct GetSensorConfiguration :
208 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700209{
210 GetSensorConfiguration(
211 std::shared_ptr<sdbusplus::asio::connection> connection,
212 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
Ed Tanous8a57ec02020-10-09 12:46:52 -0700213 dbusConnection(std::move(connection)),
James Feistc71c1192019-09-18 14:31:33 -0700214 callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700215 {}
James Feistf27a55c2020-08-04 14:27:30 -0700216
217 void getPath(const std::string& path, const std::string& interface,
218 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700219 {
James Feistf27a55c2020-08-04 14:27:30 -0700220 if (retries > 5)
221 {
222 retries = 5;
223 }
224 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
225
226 self->dbusConnection->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700227 [self, path, interface, owner, retries](
228 const boost::system::error_code ec, SensorBaseConfigMap& data) {
Ed Tanousbb679322022-05-16 16:10:00 -0700229 if (ec)
230 {
231 std::cerr << "Error getting " << path << ": retries left"
232 << retries - 1 << "\n";
Ed Tanous2049bd22022-07-09 07:20:26 -0700233 if (retries == 0U)
James Feistf27a55c2020-08-04 14:27:30 -0700234 {
James Feistf27a55c2020-08-04 14:27:30 -0700235 return;
236 }
Ed Tanousbb679322022-05-16 16:10:00 -0700237 auto timer = std::make_shared<boost::asio::steady_timer>(
238 self->dbusConnection->get_io_context());
239 timer->expires_after(std::chrono::seconds(10));
240 timer->async_wait([self, timer, path, interface, owner,
241 retries](boost::system::error_code ec) {
242 if (ec)
243 {
244 std::cerr << "Timer error!\n";
245 return;
246 }
247 self->getPath(path, interface, owner, retries - 1);
248 });
249 return;
250 }
James Feistf27a55c2020-08-04 14:27:30 -0700251
Ed Tanousbb679322022-05-16 16:10:00 -0700252 self->respData[path][interface] = std::move(data);
James Feistf27a55c2020-08-04 14:27:30 -0700253 },
254 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
255 interface);
256 }
257
258 void getConfiguration(const std::vector<std::string>& interfaces,
259 size_t retries = 0)
260 {
261 if (retries > 5)
262 {
263 retries = 5;
264 }
265
James Feistc71c1192019-09-18 14:31:33 -0700266 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
267 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700268 [self, interfaces, retries](const boost::system::error_code ec,
269 const GetSubTreeType& ret) {
Ed Tanousbb679322022-05-16 16:10:00 -0700270 if (ec)
271 {
272 std::cerr << "Error calling mapper\n";
Ed Tanous2049bd22022-07-09 07:20:26 -0700273 if (retries == 0U)
James Feistc71c1192019-09-18 14:31:33 -0700274 {
James Feistc71c1192019-09-18 14:31:33 -0700275 return;
276 }
Ed Tanousbb679322022-05-16 16:10:00 -0700277 auto timer = std::make_shared<boost::asio::steady_timer>(
278 self->dbusConnection->get_io_context());
279 timer->expires_after(std::chrono::seconds(10));
280 timer->async_wait([self, timer, interfaces,
281 retries](boost::system::error_code ec) {
282 if (ec)
James Feistc71c1192019-09-18 14:31:33 -0700283 {
Ed Tanousbb679322022-05-16 16:10:00 -0700284 std::cerr << "Timer error!\n";
James Feistc71c1192019-09-18 14:31:33 -0700285 return;
286 }
Ed Tanousbb679322022-05-16 16:10:00 -0700287 self->getConfiguration(interfaces, retries - 1);
288 });
James Feistc71c1192019-09-18 14:31:33 -0700289
Ed Tanousbb679322022-05-16 16:10:00 -0700290 return;
291 }
292 for (const auto& [path, objDict] : ret)
293 {
294 if (objDict.empty())
295 {
296 return;
James Feistc71c1192019-09-18 14:31:33 -0700297 }
Ed Tanousbb679322022-05-16 16:10:00 -0700298 const std::string& owner = objDict.begin()->first;
299
300 for (const std::string& interface : objDict.begin()->second)
301 {
302 // anything that starts with a requested configuration
303 // is good
304 if (std::find_if(interfaces.begin(), interfaces.end(),
305 [interface](const std::string& possible) {
306 return boost::starts_with(interface, possible);
307 }) == interfaces.end())
308 {
309 continue;
310 }
311 self->getPath(path, interface, owner);
312 }
313 }
James Feistc71c1192019-09-18 14:31:33 -0700314 },
315 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
316 "/", 0, interfaces);
317 }
318
319 ~GetSensorConfiguration()
320 {
321 callback(respData);
322 }
323
324 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
325 std::function<void(ManagedObjectType& resp)> callback;
326 ManagedObjectType respData;
327};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200328
329// The common scheme for sysfs files naming is: <type><number>_<item>.
330// This function returns optionally these 3 elements as a tuple.
331std::optional<std::tuple<std::string, std::string, std::string>>
332 splitFileName(const std::filesystem::path& filePath);
333std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700334 const double& scaleFactor);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800335void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
336bool getManufacturingMode();