blob: fb543f4691126a0cac8195fbac62efecf212a40b [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>
Zev Weiss214d9712022-08-12 12:54:31 -070017#include <span>
Patrick Venturefd6ba732019-10-31 14:27:39 -070018#include <string>
19#include <tuple>
20#include <utility>
21#include <variant>
22#include <vector>
James Feist6714a252018-09-10 15:26:18 -070023
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070024const constexpr char* jsonStore = "/var/configuration/flattened.json";
25const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
26const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
James Feist58295ad2019-05-30 15:01:41 -070027
28constexpr const char* cpuInventoryPath =
29 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070030const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070031
32using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080033 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
34 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080035using SensorBaseConfigMap =
36 boost::container::flat_map<std::string, BasicVariantType>;
37using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
38using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
39using ManagedObjectType =
40 boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
James Feist6714a252018-09-10 15:26:18 -070041
James Feista5e58722019-04-22 14:43:11 -070042using GetSubTreeType = std::vector<
43 std::pair<std::string,
44 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feistd8bd5622019-06-26 12:09:05 -070045using Association = std::tuple<std::string, std::string, std::string>;
46
Zhikui Renda98f092021-11-01 09:41:08 -070047inline std::string escapeName(const std::string& sensorName)
48{
49 return boost::replace_all_copy(sensorName, " ", "_");
50}
51
Zev Weiss88cb29d2022-05-09 03:46:15 +000052enum class PowerState
53{
54 on,
55 biosPost,
56 always
57};
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);
Zev Weiss88cb29d2022-05-09 03:46:15 +000071void setupPowerMatchCallback(
72 const std::shared_ptr<sdbusplus::asio::connection>& conn,
73 std::function<void(PowerState type, bool state)>&& callback);
James Feist71d31b22019-01-02 16:57:54 -080074void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070075bool getSensorConfiguration(
76 const std::string& type,
77 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -070078 ManagedObjectType& resp, bool useCache);
79
80bool getSensorConfiguration(
81 const std::string& type,
82 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
83 ManagedObjectType& resp);
James Feist87d713a2018-12-06 16:06:24 -080084
James Feist82bac4c2019-03-11 11:16:53 -070085void createAssociation(
86 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
87 const std::string& path);
88
James Feist87d713a2018-12-06 16:06:24 -080089// replaces limits if MinReading and MaxReading are found.
90void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080091 const SensorBaseConfiguration* data);
92
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000093bool readingStateGood(const PowerState& powerState);
94
James Feista5e58722019-04-22 14:43:11 -070095namespace mapper
96{
97constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
98constexpr const char* path = "/xyz/openbmc_project/object_mapper";
99constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
100constexpr const char* subtree = "GetSubTree";
101} // namespace mapper
102
103namespace properties
104{
105constexpr const char* interface = "org.freedesktop.DBus.Properties";
106constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -0700107constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -0700108} // namespace properties
109
James Feist52497fd2019-06-07 13:01:33 -0700110namespace power
111{
112const static constexpr char* busname = "xyz.openbmc_project.State.Host";
113const static constexpr char* interface = "xyz.openbmc_project.State.Host";
114const static constexpr char* path = "/xyz/openbmc_project/state/host0";
115const static constexpr char* property = "CurrentHostState";
116} // namespace power
117namespace post
118{
119const static constexpr char* busname =
120 "xyz.openbmc_project.State.OperatingSystem";
121const static constexpr char* interface =
122 "xyz.openbmc_project.State.OperatingSystem.Status";
123const static constexpr char* path = "/xyz/openbmc_project/state/os";
124const static constexpr char* property = "OperatingSystemState";
125} // namespace post
126
James Feist2adc95c2019-09-30 14:55:28 -0700127namespace association
128{
129const static constexpr char* interface =
130 "xyz.openbmc_project.Association.Definitions";
131} // namespace association
132
James Feist40a72142018-12-21 10:09:53 -0800133template <typename T>
Zev Weissafd15042022-07-18 12:28:40 -0700134inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key)
James Feist40a72142018-12-21 10:09:53 -0800135{
136 auto it = data.find(key);
137 if (it == data.end())
138 {
139 std::cerr << "Configuration missing " << key << "\n";
140 throw std::invalid_argument("Key Missing");
141 }
142 if constexpr (std::is_same_v<T, double>)
143 {
James Feist3eb82622019-02-08 13:10:22 -0800144 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800145 }
James Feist6ef20402019-01-07 16:45:08 -0800146 else if constexpr (std::is_unsigned_v<T>)
147 {
James Feist3eb82622019-02-08 13:10:22 -0800148 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800149 }
James Feist40a72142018-12-21 10:09:53 -0800150 else if constexpr (std::is_same_v<T, std::string>)
151 {
James Feist3eb82622019-02-08 13:10:22 -0800152 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800153 }
154 else
155 {
James Feist52497fd2019-06-07 13:01:33 -0700156 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800157 }
158}
James Feistfc94b212019-02-06 16:14:51 -0800159
160inline void setReadState(const std::string& str, PowerState& val)
161{
162
163 if (str == "On")
164 {
165 val = PowerState::on;
166 }
167 else if (str == "BiosPost")
168 {
169 val = PowerState::biosPost;
170 }
171 else if (str == "Always")
172 {
173 val = PowerState::always;
174 }
175}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800176
Zev Weissa4d27682022-07-19 15:30:36 -0700177inline PowerState getPowerState(const SensorBaseConfigMap& cfg)
178{
179 PowerState state = PowerState::always;
180 auto findPowerState = cfg.find("PowerState");
181 if (findPowerState != cfg.end())
182 {
183 std::string powerState =
184 std::visit(VariantToStringVisitor(), findPowerState->second);
185 setReadState(powerState, state);
186 }
187 return state;
188}
189
Ed Tanous8a57ec02020-10-09 12:46:52 -0700190inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist49a8ccd2020-09-16 16:09:52 -0700191 const std::string& name, bool on)
192{
193 conn->async_method_call(
194 [name](const boost::system::error_code ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700195 if (ec)
196 {
197 std::cerr << "Failed to set LED " << name << "\n";
198 }
James Feist49a8ccd2020-09-16 16:09:52 -0700199 },
200 "xyz.openbmc_project.LED.GroupManager",
201 "/xyz/openbmc_project/led/groups/" + name, properties::interface,
202 properties::set, "xyz.openbmc_project.Led.Group", "Asserted",
203 std::variant<bool>(on));
204}
205
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800206void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700207 const std::shared_ptr<sdbusplus::asio::connection>& conn,
208 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800209 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700210
James Feist38fb5982020-05-28 10:09:54 -0700211struct GetSensorConfiguration :
212 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700213{
214 GetSensorConfiguration(
215 std::shared_ptr<sdbusplus::asio::connection> connection,
216 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
Ed Tanous8a57ec02020-10-09 12:46:52 -0700217 dbusConnection(std::move(connection)),
James Feistc71c1192019-09-18 14:31:33 -0700218 callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700219 {}
James Feistf27a55c2020-08-04 14:27:30 -0700220
221 void getPath(const std::string& path, const std::string& interface,
222 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700223 {
James Feistf27a55c2020-08-04 14:27:30 -0700224 if (retries > 5)
225 {
226 retries = 5;
227 }
228 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
229
230 self->dbusConnection->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700231 [self, path, interface, owner, retries](
232 const boost::system::error_code ec, SensorBaseConfigMap& data) {
Ed Tanousbb679322022-05-16 16:10:00 -0700233 if (ec)
234 {
235 std::cerr << "Error getting " << path << ": retries left"
236 << retries - 1 << "\n";
Ed Tanous2049bd22022-07-09 07:20:26 -0700237 if (retries == 0U)
James Feistf27a55c2020-08-04 14:27:30 -0700238 {
James Feistf27a55c2020-08-04 14:27:30 -0700239 return;
240 }
Ed Tanousbb679322022-05-16 16:10:00 -0700241 auto timer = std::make_shared<boost::asio::steady_timer>(
242 self->dbusConnection->get_io_context());
243 timer->expires_after(std::chrono::seconds(10));
244 timer->async_wait([self, timer, path, interface, owner,
245 retries](boost::system::error_code ec) {
246 if (ec)
247 {
248 std::cerr << "Timer error!\n";
249 return;
250 }
251 self->getPath(path, interface, owner, retries - 1);
252 });
253 return;
254 }
James Feistf27a55c2020-08-04 14:27:30 -0700255
Ed Tanousbb679322022-05-16 16:10:00 -0700256 self->respData[path][interface] = std::move(data);
James Feistf27a55c2020-08-04 14:27:30 -0700257 },
258 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
259 interface);
260 }
261
262 void getConfiguration(const std::vector<std::string>& interfaces,
263 size_t retries = 0)
264 {
265 if (retries > 5)
266 {
267 retries = 5;
268 }
269
James Feistc71c1192019-09-18 14:31:33 -0700270 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
271 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700272 [self, interfaces, retries](const boost::system::error_code ec,
273 const GetSubTreeType& ret) {
Ed Tanousbb679322022-05-16 16:10:00 -0700274 if (ec)
275 {
276 std::cerr << "Error calling mapper\n";
Ed Tanous2049bd22022-07-09 07:20:26 -0700277 if (retries == 0U)
James Feistc71c1192019-09-18 14:31:33 -0700278 {
James Feistc71c1192019-09-18 14:31:33 -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, interfaces,
285 retries](boost::system::error_code ec) {
286 if (ec)
James Feistc71c1192019-09-18 14:31:33 -0700287 {
Ed Tanousbb679322022-05-16 16:10:00 -0700288 std::cerr << "Timer error!\n";
James Feistc71c1192019-09-18 14:31:33 -0700289 return;
290 }
Ed Tanousbb679322022-05-16 16:10:00 -0700291 self->getConfiguration(interfaces, retries - 1);
292 });
James Feistc71c1192019-09-18 14:31:33 -0700293
Ed Tanousbb679322022-05-16 16:10:00 -0700294 return;
295 }
296 for (const auto& [path, objDict] : ret)
297 {
298 if (objDict.empty())
299 {
300 return;
James Feistc71c1192019-09-18 14:31:33 -0700301 }
Ed Tanousbb679322022-05-16 16:10:00 -0700302 const std::string& owner = objDict.begin()->first;
303
304 for (const std::string& interface : objDict.begin()->second)
305 {
306 // anything that starts with a requested configuration
307 // is good
308 if (std::find_if(interfaces.begin(), interfaces.end(),
309 [interface](const std::string& possible) {
310 return boost::starts_with(interface, possible);
311 }) == interfaces.end())
312 {
313 continue;
314 }
315 self->getPath(path, interface, owner);
316 }
317 }
James Feistc71c1192019-09-18 14:31:33 -0700318 },
319 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
320 "/", 0, interfaces);
321 }
322
323 ~GetSensorConfiguration()
324 {
325 callback(respData);
326 }
327
328 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
329 std::function<void(ManagedObjectType& resp)> callback;
330 ManagedObjectType respData;
331};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200332
333// The common scheme for sysfs files naming is: <type><number>_<item>.
334// This function returns optionally these 3 elements as a tuple.
335std::optional<std::tuple<std::string, std::string, std::string>>
336 splitFileName(const std::filesystem::path& filePath);
337std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700338 const double& scaleFactor);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800339void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
340bool getManufacturingMode();
Zev Weiss214d9712022-08-12 12:54:31 -0700341std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
342 setupPropertiesChangedMatches(
343 sdbusplus::asio::connection& bus, std::span<const char* const> types,
344 const std::function<void(sdbusplus::message_t&)>& handler);