blob: c08daf90a4b1afd5eba7f7c9018d78434896a969 [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>
James Feist6714a252018-09-10 15:26:18 -070015#include <regex>
Patrick Venturefd6ba732019-10-31 14:27:39 -070016#include <string>
17#include <tuple>
18#include <utility>
19#include <variant>
20#include <vector>
James Feist6714a252018-09-10 15:26:18 -070021
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070022const constexpr char* jsonStore = "/var/configuration/flattened.json";
23const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
24const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
James Feist58295ad2019-05-30 15:01:41 -070025
26constexpr const char* cpuInventoryPath =
27 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070028const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070029
30using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080031 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
32 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080033using SensorBaseConfigMap =
34 boost::container::flat_map<std::string, BasicVariantType>;
35using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
36using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
37using ManagedObjectType =
38 boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
James Feist6714a252018-09-10 15:26:18 -070039
James Feista5e58722019-04-22 14:43:11 -070040using GetSubTreeType = std::vector<
41 std::pair<std::string,
42 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feistd8bd5622019-06-26 12:09:05 -070043using Association = std::tuple<std::string, std::string, std::string>;
44
Zhikui Renda98f092021-11-01 09:41:08 -070045inline std::string escapeName(const std::string& sensorName)
46{
47 return boost::replace_all_copy(sensorName, " ", "_");
48}
49
Jason Ling100c20b2020-08-11 14:50:33 -070050std::optional<std::string> openAndRead(const std::string& hwmonFile);
51std::optional<std::string>
52 getFullHwmonFilePath(const std::string& directory,
53 const std::string& hwmonBaseName,
54 const std::set<std::string>& permitSet);
55std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
Ed Tanous8a57ec02020-10-09 12:46:52 -070056bool findFiles(const std::filesystem::path& dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070057 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080058 std::vector<std::filesystem::path>& foundPaths,
Ed Tanous8a57ec02020-10-09 12:46:52 -070059 int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080060bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080061bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080062void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070063bool getSensorConfiguration(
64 const std::string& type,
65 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -070066 ManagedObjectType& resp, bool useCache);
67
68bool getSensorConfiguration(
69 const std::string& type,
70 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
71 ManagedObjectType& resp);
James Feist87d713a2018-12-06 16:06:24 -080072
James Feist82bac4c2019-03-11 11:16:53 -070073void createAssociation(
74 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
75 const std::string& path);
76
James Feist87d713a2018-12-06 16:06:24 -080077// replaces limits if MinReading and MaxReading are found.
78void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080079 const SensorBaseConfiguration* data);
80
James Feistfc94b212019-02-06 16:14:51 -080081enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080082{
83 on,
James Feistfc94b212019-02-06 16:14:51 -080084 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080085 always
86};
87
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000088bool readingStateGood(const PowerState& powerState);
89
James Feista5e58722019-04-22 14:43:11 -070090namespace mapper
91{
92constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
93constexpr const char* path = "/xyz/openbmc_project/object_mapper";
94constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
95constexpr const char* subtree = "GetSubTree";
96} // namespace mapper
97
98namespace properties
99{
100constexpr const char* interface = "org.freedesktop.DBus.Properties";
101constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -0700102constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -0700103} // namespace properties
104
James Feist52497fd2019-06-07 13:01:33 -0700105namespace power
106{
107const static constexpr char* busname = "xyz.openbmc_project.State.Host";
108const static constexpr char* interface = "xyz.openbmc_project.State.Host";
109const static constexpr char* path = "/xyz/openbmc_project/state/host0";
110const static constexpr char* property = "CurrentHostState";
111} // namespace power
112namespace post
113{
114const static constexpr char* busname =
115 "xyz.openbmc_project.State.OperatingSystem";
116const static constexpr char* interface =
117 "xyz.openbmc_project.State.OperatingSystem.Status";
118const static constexpr char* path = "/xyz/openbmc_project/state/os";
119const static constexpr char* property = "OperatingSystemState";
120} // namespace post
121
James Feist2adc95c2019-09-30 14:55:28 -0700122namespace association
123{
124const static constexpr char* interface =
125 "xyz.openbmc_project.Association.Definitions";
126} // namespace association
127
James Feist40a72142018-12-21 10:09:53 -0800128template <typename T>
129inline T loadVariant(
130 const boost::container::flat_map<std::string, BasicVariantType>& data,
131 const std::string& key)
132{
133 auto it = data.find(key);
134 if (it == data.end())
135 {
136 std::cerr << "Configuration missing " << key << "\n";
137 throw std::invalid_argument("Key Missing");
138 }
139 if constexpr (std::is_same_v<T, double>)
140 {
James Feist3eb82622019-02-08 13:10:22 -0800141 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800142 }
James Feist6ef20402019-01-07 16:45:08 -0800143 else if constexpr (std::is_unsigned_v<T>)
144 {
James Feist3eb82622019-02-08 13:10:22 -0800145 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800146 }
James Feist40a72142018-12-21 10:09:53 -0800147 else if constexpr (std::is_same_v<T, std::string>)
148 {
James Feist3eb82622019-02-08 13:10:22 -0800149 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800150 }
151 else
152 {
James Feist52497fd2019-06-07 13:01:33 -0700153 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800154 }
155}
James Feistfc94b212019-02-06 16:14:51 -0800156
157inline void setReadState(const std::string& str, PowerState& val)
158{
159
160 if (str == "On")
161 {
162 val = PowerState::on;
163 }
164 else if (str == "BiosPost")
165 {
166 val = PowerState::biosPost;
167 }
168 else if (str == "Always")
169 {
170 val = PowerState::always;
171 }
172}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800173
Ed Tanous8a57ec02020-10-09 12:46:52 -0700174inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist49a8ccd2020-09-16 16:09:52 -0700175 const std::string& name, bool on)
176{
177 conn->async_method_call(
178 [name](const boost::system::error_code ec) {
179 if (ec)
180 {
181 std::cerr << "Failed to set LED " << name << "\n";
182 }
183 },
184 "xyz.openbmc_project.LED.GroupManager",
185 "/xyz/openbmc_project/led/groups/" + name, properties::interface,
186 properties::set, "xyz.openbmc_project.Led.Group", "Asserted",
187 std::variant<bool>(on));
188}
189
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800190void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700191 const std::shared_ptr<sdbusplus::asio::connection>& conn,
192 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800193 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700194
James Feist38fb5982020-05-28 10:09:54 -0700195struct GetSensorConfiguration :
196 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700197{
198 GetSensorConfiguration(
199 std::shared_ptr<sdbusplus::asio::connection> connection,
200 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
Ed Tanous8a57ec02020-10-09 12:46:52 -0700201 dbusConnection(std::move(connection)),
James Feistc71c1192019-09-18 14:31:33 -0700202 callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700203 {}
James Feistf27a55c2020-08-04 14:27:30 -0700204
205 void getPath(const std::string& path, const std::string& interface,
206 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700207 {
James Feistf27a55c2020-08-04 14:27:30 -0700208 if (retries > 5)
209 {
210 retries = 5;
211 }
212 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
213
214 self->dbusConnection->async_method_call(
215 [self, path, interface, owner,
216 retries](const boost::system::error_code ec,
217 boost::container::flat_map<std::string, BasicVariantType>&
218 data) {
219 if (ec)
220 {
221 std::cerr << "Error getting " << path << ": retries left"
222 << retries - 1 << "\n";
223 if (!retries)
224 {
225 return;
226 }
227 auto timer = std::make_shared<boost::asio::steady_timer>(
228 self->dbusConnection->get_io_context());
229 timer->expires_after(std::chrono::seconds(10));
230 timer->async_wait([self, timer, path, interface, owner,
231 retries](boost::system::error_code ec) {
232 if (ec)
233 {
234 std::cerr << "Timer error!\n";
235 return;
236 }
237 self->getPath(path, interface, owner, retries - 1);
238 });
239 return;
240 }
241
242 self->respData[path][interface] = std::move(data);
243 },
244 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
245 interface);
246 }
247
248 void getConfiguration(const std::vector<std::string>& interfaces,
249 size_t retries = 0)
250 {
251 if (retries > 5)
252 {
253 retries = 5;
254 }
255
James Feistc71c1192019-09-18 14:31:33 -0700256 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
257 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700258 [self, interfaces, retries](const boost::system::error_code ec,
259 const GetSubTreeType& ret) {
James Feistc71c1192019-09-18 14:31:33 -0700260 if (ec)
261 {
262 std::cerr << "Error calling mapper\n";
James Feistf27a55c2020-08-04 14:27:30 -0700263 if (!retries)
264 {
265 return;
266 }
267 auto timer = std::make_shared<boost::asio::steady_timer>(
268 self->dbusConnection->get_io_context());
269 timer->expires_after(std::chrono::seconds(10));
270 timer->async_wait([self, timer, interfaces,
271 retries](boost::system::error_code ec) {
272 if (ec)
273 {
274 std::cerr << "Timer error!\n";
275 return;
276 }
277 self->getConfiguration(interfaces, retries - 1);
278 });
279
James Feistc71c1192019-09-18 14:31:33 -0700280 return;
281 }
282 for (const auto& [path, objDict] : ret)
283 {
284 if (objDict.empty())
285 {
286 return;
287 }
288 const std::string& owner = objDict.begin()->first;
289
290 for (const std::string& interface : objDict.begin()->second)
291 {
292 // anything that starts with a requested configuration
293 // is good
294 if (std::find_if(
295 interfaces.begin(), interfaces.end(),
296 [interface](const std::string& possible) {
297 return boost::starts_with(interface,
298 possible);
299 }) == interfaces.end())
300 {
301 continue;
302 }
James Feistf27a55c2020-08-04 14:27:30 -0700303 self->getPath(path, interface, owner);
James Feistc71c1192019-09-18 14:31:33 -0700304 }
305 }
306 },
307 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
308 "/", 0, interfaces);
309 }
310
311 ~GetSensorConfiguration()
312 {
313 callback(respData);
314 }
315
316 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
317 std::function<void(ManagedObjectType& resp)> callback;
318 ManagedObjectType respData;
319};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200320
321// The common scheme for sysfs files naming is: <type><number>_<item>.
322// This function returns optionally these 3 elements as a tuple.
323std::optional<std::tuple<std::string, std::string, std::string>>
324 splitFileName(const std::filesystem::path& filePath);
325std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700326 const double& scaleFactor);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800327void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
328bool getManufacturingMode();