blob: 61fcdf556d8718653f7112eeb64f1f0185012532 [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>
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>
James Feist6714a252018-09-10 15:26:18 -070014#include <regex>
Patrick Venturefd6ba732019-10-31 14:27:39 -070015#include <string>
16#include <tuple>
17#include <utility>
18#include <variant>
19#include <vector>
James Feist6714a252018-09-10 15:26:18 -070020
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070021const constexpr char* jsonStore = "/var/configuration/flattened.json";
22const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
23const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
James Feist58295ad2019-05-30 15:01:41 -070024
25constexpr const char* cpuInventoryPath =
26 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070027const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070028
29using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080030 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
31 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080032using SensorBaseConfigMap =
33 boost::container::flat_map<std::string, BasicVariantType>;
34using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
35using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
36using ManagedObjectType =
37 boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
James Feist6714a252018-09-10 15:26:18 -070038
James Feista5e58722019-04-22 14:43:11 -070039using GetSubTreeType = std::vector<
40 std::pair<std::string,
41 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feistd8bd5622019-06-26 12:09:05 -070042using Association = std::tuple<std::string, std::string, std::string>;
43
Jason Ling100c20b2020-08-11 14:50:33 -070044std::optional<std::string> openAndRead(const std::string& hwmonFile);
45std::optional<std::string>
46 getFullHwmonFilePath(const std::string& directory,
47 const std::string& hwmonBaseName,
48 const std::set<std::string>& permitSet);
49std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
Ed Tanous8a57ec02020-10-09 12:46:52 -070050bool findFiles(const std::filesystem::path& dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070051 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080052 std::vector<std::filesystem::path>& foundPaths,
Ed Tanous8a57ec02020-10-09 12:46:52 -070053 int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080054bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080055bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080056void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070057bool getSensorConfiguration(
58 const std::string& type,
59 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -070060 ManagedObjectType& resp, bool useCache);
61
62bool getSensorConfiguration(
63 const std::string& type,
64 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
65 ManagedObjectType& resp);
James Feist87d713a2018-12-06 16:06:24 -080066
James Feist82bac4c2019-03-11 11:16:53 -070067void createAssociation(
68 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
69 const std::string& path);
70
James Feist87d713a2018-12-06 16:06:24 -080071// replaces limits if MinReading and MaxReading are found.
72void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080073 const SensorBaseConfiguration* data);
74
James Feistfc94b212019-02-06 16:14:51 -080075enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080076{
77 on,
James Feistfc94b212019-02-06 16:14:51 -080078 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080079 always
80};
81
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000082bool readingStateGood(const PowerState& powerState);
83
James Feista5e58722019-04-22 14:43:11 -070084namespace mapper
85{
86constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
87constexpr const char* path = "/xyz/openbmc_project/object_mapper";
88constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
89constexpr const char* subtree = "GetSubTree";
90} // namespace mapper
91
92namespace properties
93{
94constexpr const char* interface = "org.freedesktop.DBus.Properties";
95constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -070096constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -070097} // namespace properties
98
James Feist52497fd2019-06-07 13:01:33 -070099namespace power
100{
101const static constexpr char* busname = "xyz.openbmc_project.State.Host";
102const static constexpr char* interface = "xyz.openbmc_project.State.Host";
103const static constexpr char* path = "/xyz/openbmc_project/state/host0";
104const static constexpr char* property = "CurrentHostState";
105} // namespace power
106namespace post
107{
108const static constexpr char* busname =
109 "xyz.openbmc_project.State.OperatingSystem";
110const static constexpr char* interface =
111 "xyz.openbmc_project.State.OperatingSystem.Status";
112const static constexpr char* path = "/xyz/openbmc_project/state/os";
113const static constexpr char* property = "OperatingSystemState";
114} // namespace post
115
James Feist2adc95c2019-09-30 14:55:28 -0700116namespace association
117{
118const static constexpr char* interface =
119 "xyz.openbmc_project.Association.Definitions";
120} // namespace association
121
James Feist40a72142018-12-21 10:09:53 -0800122template <typename T>
123inline T loadVariant(
124 const boost::container::flat_map<std::string, BasicVariantType>& data,
125 const std::string& key)
126{
127 auto it = data.find(key);
128 if (it == data.end())
129 {
130 std::cerr << "Configuration missing " << key << "\n";
131 throw std::invalid_argument("Key Missing");
132 }
133 if constexpr (std::is_same_v<T, double>)
134 {
James Feist3eb82622019-02-08 13:10:22 -0800135 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800136 }
James Feist6ef20402019-01-07 16:45:08 -0800137 else if constexpr (std::is_unsigned_v<T>)
138 {
James Feist3eb82622019-02-08 13:10:22 -0800139 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800140 }
James Feist40a72142018-12-21 10:09:53 -0800141 else if constexpr (std::is_same_v<T, std::string>)
142 {
James Feist3eb82622019-02-08 13:10:22 -0800143 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800144 }
145 else
146 {
James Feist52497fd2019-06-07 13:01:33 -0700147 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800148 }
149}
James Feistfc94b212019-02-06 16:14:51 -0800150
151inline void setReadState(const std::string& str, PowerState& val)
152{
153
154 if (str == "On")
155 {
156 val = PowerState::on;
157 }
158 else if (str == "BiosPost")
159 {
160 val = PowerState::biosPost;
161 }
162 else if (str == "Always")
163 {
164 val = PowerState::always;
165 }
166}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800167
Ed Tanous8a57ec02020-10-09 12:46:52 -0700168inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist49a8ccd2020-09-16 16:09:52 -0700169 const std::string& name, bool on)
170{
171 conn->async_method_call(
172 [name](const boost::system::error_code ec) {
173 if (ec)
174 {
175 std::cerr << "Failed to set LED " << name << "\n";
176 }
177 },
178 "xyz.openbmc_project.LED.GroupManager",
179 "/xyz/openbmc_project/led/groups/" + name, properties::interface,
180 properties::set, "xyz.openbmc_project.Led.Group", "Asserted",
181 std::variant<bool>(on));
182}
183
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800184void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700185 const std::shared_ptr<sdbusplus::asio::connection>& conn,
186 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800187 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700188
James Feist38fb5982020-05-28 10:09:54 -0700189struct GetSensorConfiguration :
190 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700191{
192 GetSensorConfiguration(
193 std::shared_ptr<sdbusplus::asio::connection> connection,
194 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
Ed Tanous8a57ec02020-10-09 12:46:52 -0700195 dbusConnection(std::move(connection)),
James Feistc71c1192019-09-18 14:31:33 -0700196 callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700197 {}
James Feistf27a55c2020-08-04 14:27:30 -0700198
199 void getPath(const std::string& path, const std::string& interface,
200 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700201 {
James Feistf27a55c2020-08-04 14:27:30 -0700202 if (retries > 5)
203 {
204 retries = 5;
205 }
206 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
207
208 self->dbusConnection->async_method_call(
209 [self, path, interface, owner,
210 retries](const boost::system::error_code ec,
211 boost::container::flat_map<std::string, BasicVariantType>&
212 data) {
213 if (ec)
214 {
215 std::cerr << "Error getting " << path << ": retries left"
216 << retries - 1 << "\n";
217 if (!retries)
218 {
219 return;
220 }
221 auto timer = std::make_shared<boost::asio::steady_timer>(
222 self->dbusConnection->get_io_context());
223 timer->expires_after(std::chrono::seconds(10));
224 timer->async_wait([self, timer, path, interface, owner,
225 retries](boost::system::error_code ec) {
226 if (ec)
227 {
228 std::cerr << "Timer error!\n";
229 return;
230 }
231 self->getPath(path, interface, owner, retries - 1);
232 });
233 return;
234 }
235
236 self->respData[path][interface] = std::move(data);
237 },
238 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
239 interface);
240 }
241
242 void getConfiguration(const std::vector<std::string>& interfaces,
243 size_t retries = 0)
244 {
245 if (retries > 5)
246 {
247 retries = 5;
248 }
249
James Feistc71c1192019-09-18 14:31:33 -0700250 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
251 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700252 [self, interfaces, retries](const boost::system::error_code ec,
253 const GetSubTreeType& ret) {
James Feistc71c1192019-09-18 14:31:33 -0700254 if (ec)
255 {
256 std::cerr << "Error calling mapper\n";
James Feistf27a55c2020-08-04 14:27:30 -0700257 if (!retries)
258 {
259 return;
260 }
261 auto timer = std::make_shared<boost::asio::steady_timer>(
262 self->dbusConnection->get_io_context());
263 timer->expires_after(std::chrono::seconds(10));
264 timer->async_wait([self, timer, interfaces,
265 retries](boost::system::error_code ec) {
266 if (ec)
267 {
268 std::cerr << "Timer error!\n";
269 return;
270 }
271 self->getConfiguration(interfaces, retries - 1);
272 });
273
James Feistc71c1192019-09-18 14:31:33 -0700274 return;
275 }
276 for (const auto& [path, objDict] : ret)
277 {
278 if (objDict.empty())
279 {
280 return;
281 }
282 const std::string& owner = objDict.begin()->first;
283
284 for (const std::string& interface : objDict.begin()->second)
285 {
286 // anything that starts with a requested configuration
287 // is good
288 if (std::find_if(
289 interfaces.begin(), interfaces.end(),
290 [interface](const std::string& possible) {
291 return boost::starts_with(interface,
292 possible);
293 }) == interfaces.end())
294 {
295 continue;
296 }
James Feistf27a55c2020-08-04 14:27:30 -0700297 self->getPath(path, interface, owner);
James Feistc71c1192019-09-18 14:31:33 -0700298 }
299 }
300 },
301 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
302 "/", 0, interfaces);
303 }
304
305 ~GetSensorConfiguration()
306 {
307 callback(respData);
308 }
309
310 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
311 std::function<void(ManagedObjectType& resp)> callback;
312 ManagedObjectType respData;
313};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200314
315// The common scheme for sysfs files naming is: <type><number>_<item>.
316// This function returns optionally these 3 elements as a tuple.
317std::optional<std::tuple<std::string, std::string, std::string>>
318 splitFileName(const std::filesystem::path& filePath);
319std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700320 const double& scaleFactor);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800321void setupManufacturingModeMatch(sdbusplus::asio::connection& conn);
322bool getManufacturingMode();