blob: 7f529e1d6a712e5ece1701e0b200bf290b53d6a8 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
James Feist87d713a2018-12-06 16:06:24 -08002#include "VariantVisitors.hpp"
3
James Feistc71c1192019-09-18 14:31:33 -07004#include <boost/algorithm/string/predicate.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
James Feistcf3bce62019-01-08 10:07:19 -080045bool findFiles(const std::filesystem::path dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070046 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080047 std::vector<std::filesystem::path>& foundPaths,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070048 unsigned int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080049bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080050bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080051void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070052bool getSensorConfiguration(
53 const std::string& type,
54 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist87d713a2018-12-06 16:06:24 -080055 ManagedObjectType& resp, bool useCache = false);
56
James Feist82bac4c2019-03-11 11:16:53 -070057void createAssociation(
58 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
59 const std::string& path);
60
James Feist87d713a2018-12-06 16:06:24 -080061// replaces limits if MinReading and MaxReading are found.
62void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080063 const SensorBaseConfiguration* data);
64
James Feistfc94b212019-02-06 16:14:51 -080065enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080066{
67 on,
James Feistfc94b212019-02-06 16:14:51 -080068 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080069 always
70};
71
James Feista5e58722019-04-22 14:43:11 -070072namespace mapper
73{
74constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
75constexpr const char* path = "/xyz/openbmc_project/object_mapper";
76constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
77constexpr const char* subtree = "GetSubTree";
78} // namespace mapper
79
80namespace properties
81{
82constexpr const char* interface = "org.freedesktop.DBus.Properties";
83constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -070084constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -070085} // namespace properties
86
James Feist52497fd2019-06-07 13:01:33 -070087namespace power
88{
89const static constexpr char* busname = "xyz.openbmc_project.State.Host";
90const static constexpr char* interface = "xyz.openbmc_project.State.Host";
91const static constexpr char* path = "/xyz/openbmc_project/state/host0";
92const static constexpr char* property = "CurrentHostState";
93} // namespace power
94namespace post
95{
96const static constexpr char* busname =
97 "xyz.openbmc_project.State.OperatingSystem";
98const static constexpr char* interface =
99 "xyz.openbmc_project.State.OperatingSystem.Status";
100const static constexpr char* path = "/xyz/openbmc_project/state/os";
101const static constexpr char* property = "OperatingSystemState";
102} // namespace post
103
James Feist2adc95c2019-09-30 14:55:28 -0700104namespace association
105{
106const static constexpr char* interface =
107 "xyz.openbmc_project.Association.Definitions";
108} // namespace association
109
James Feist40a72142018-12-21 10:09:53 -0800110template <typename T>
111inline T loadVariant(
112 const boost::container::flat_map<std::string, BasicVariantType>& data,
113 const std::string& key)
114{
115 auto it = data.find(key);
116 if (it == data.end())
117 {
118 std::cerr << "Configuration missing " << key << "\n";
119 throw std::invalid_argument("Key Missing");
120 }
121 if constexpr (std::is_same_v<T, double>)
122 {
James Feist3eb82622019-02-08 13:10:22 -0800123 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800124 }
James Feist6ef20402019-01-07 16:45:08 -0800125 else if constexpr (std::is_unsigned_v<T>)
126 {
James Feist3eb82622019-02-08 13:10:22 -0800127 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800128 }
James Feist40a72142018-12-21 10:09:53 -0800129 else if constexpr (std::is_same_v<T, std::string>)
130 {
James Feist3eb82622019-02-08 13:10:22 -0800131 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800132 }
133 else
134 {
James Feist52497fd2019-06-07 13:01:33 -0700135 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800136 }
137}
James Feistfc94b212019-02-06 16:14:51 -0800138
139inline void setReadState(const std::string& str, PowerState& val)
140{
141
142 if (str == "On")
143 {
144 val = PowerState::on;
145 }
146 else if (str == "BiosPost")
147 {
148 val = PowerState::biosPost;
149 }
150 else if (str == "Always")
151 {
152 val = PowerState::always;
153 }
154}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800155
James Feist49a8ccd2020-09-16 16:09:52 -0700156inline void setLed(std::shared_ptr<sdbusplus::asio::connection> conn,
157 const std::string& name, bool on)
158{
159 conn->async_method_call(
160 [name](const boost::system::error_code ec) {
161 if (ec)
162 {
163 std::cerr << "Failed to set LED " << name << "\n";
164 }
165 },
166 "xyz.openbmc_project.LED.GroupManager",
167 "/xyz/openbmc_project/led/groups/" + name, properties::interface,
168 properties::set, "xyz.openbmc_project.Led.Group", "Asserted",
169 std::variant<bool>(on));
170}
171
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800172void createInventoryAssoc(
173 std::shared_ptr<sdbusplus::asio::connection> conn,
174 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
175 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700176
James Feist38fb5982020-05-28 10:09:54 -0700177struct GetSensorConfiguration :
178 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700179{
180 GetSensorConfiguration(
181 std::shared_ptr<sdbusplus::asio::connection> connection,
182 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
183 dbusConnection(connection),
184 callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700185 {}
James Feistf27a55c2020-08-04 14:27:30 -0700186
187 void getPath(const std::string& path, const std::string& interface,
188 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700189 {
James Feistf27a55c2020-08-04 14:27:30 -0700190 if (retries > 5)
191 {
192 retries = 5;
193 }
194 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
195
196 self->dbusConnection->async_method_call(
197 [self, path, interface, owner,
198 retries](const boost::system::error_code ec,
199 boost::container::flat_map<std::string, BasicVariantType>&
200 data) {
201 if (ec)
202 {
203 std::cerr << "Error getting " << path << ": retries left"
204 << retries - 1 << "\n";
205 if (!retries)
206 {
207 return;
208 }
209 auto timer = std::make_shared<boost::asio::steady_timer>(
210 self->dbusConnection->get_io_context());
211 timer->expires_after(std::chrono::seconds(10));
212 timer->async_wait([self, timer, path, interface, owner,
213 retries](boost::system::error_code ec) {
214 if (ec)
215 {
216 std::cerr << "Timer error!\n";
217 return;
218 }
219 self->getPath(path, interface, owner, retries - 1);
220 });
221 return;
222 }
223
224 self->respData[path][interface] = std::move(data);
225 },
226 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
227 interface);
228 }
229
230 void getConfiguration(const std::vector<std::string>& interfaces,
231 size_t retries = 0)
232 {
233 if (retries > 5)
234 {
235 retries = 5;
236 }
237
James Feistc71c1192019-09-18 14:31:33 -0700238 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
239 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700240 [self, interfaces, retries](const boost::system::error_code ec,
241 const GetSubTreeType& ret) {
James Feistc71c1192019-09-18 14:31:33 -0700242 if (ec)
243 {
244 std::cerr << "Error calling mapper\n";
James Feistf27a55c2020-08-04 14:27:30 -0700245 if (!retries)
246 {
247 return;
248 }
249 auto timer = std::make_shared<boost::asio::steady_timer>(
250 self->dbusConnection->get_io_context());
251 timer->expires_after(std::chrono::seconds(10));
252 timer->async_wait([self, timer, interfaces,
253 retries](boost::system::error_code ec) {
254 if (ec)
255 {
256 std::cerr << "Timer error!\n";
257 return;
258 }
259 self->getConfiguration(interfaces, retries - 1);
260 });
261
James Feistc71c1192019-09-18 14:31:33 -0700262 return;
263 }
264 for (const auto& [path, objDict] : ret)
265 {
266 if (objDict.empty())
267 {
268 return;
269 }
270 const std::string& owner = objDict.begin()->first;
271
272 for (const std::string& interface : objDict.begin()->second)
273 {
274 // anything that starts with a requested configuration
275 // is good
276 if (std::find_if(
277 interfaces.begin(), interfaces.end(),
278 [interface](const std::string& possible) {
279 return boost::starts_with(interface,
280 possible);
281 }) == interfaces.end())
282 {
283 continue;
284 }
James Feistf27a55c2020-08-04 14:27:30 -0700285 self->getPath(path, interface, owner);
James Feistc71c1192019-09-18 14:31:33 -0700286 }
287 }
288 },
289 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
290 "/", 0, interfaces);
291 }
292
293 ~GetSensorConfiguration()
294 {
295 callback(respData);
296 }
297
298 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
299 std::function<void(ManagedObjectType& resp)> callback;
300 ManagedObjectType respData;
301};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200302
303// The common scheme for sysfs files naming is: <type><number>_<item>.
304// This function returns optionally these 3 elements as a tuple.
305std::optional<std::tuple<std::string, std::string, std::string>>
306 splitFileName(const std::filesystem::path& filePath);
307std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700308 const double& scaleFactor);