blob: 7c58e2abb01bf3edec2135a15c81a358c1b2cec7 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
James Feist87d713a2018-12-06 16:06:24 -08002#include "VariantVisitors.hpp"
James Feistcf3bce62019-01-08 10:07:19 -08003#include "filesystem.hpp"
James Feist87d713a2018-12-06 16:06:24 -08004
James Feist6714a252018-09-10 15:26:18 -07005#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -07006#include <iostream>
7#include <regex>
8#include <sdbusplus/asio/connection.hpp>
9#include <sdbusplus/message/types.hpp>
10
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070011const constexpr char* jsonStore = "/var/configuration/flattened.json";
12const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
13const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
14const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070015
16using BasicVariantType =
James Feistbc896df2018-11-26 16:28:17 -080017 sdbusplus::message::variant<std::vector<std::string>, std::string, int64_t,
18 uint64_t, double, int32_t, uint32_t, int16_t,
19 uint16_t, uint8_t, bool>;
James Feist6714a252018-09-10 15:26:18 -070020
21using ManagedObjectType = boost::container::flat_map<
22 sdbusplus::message::object_path,
23 boost::container::flat_map<
24 std::string,
25 boost::container::flat_map<std::string, BasicVariantType>>>;
26using SensorData = boost::container::flat_map<
27 std::string, boost::container::flat_map<std::string, BasicVariantType>>;
28
James Feist87d713a2018-12-06 16:06:24 -080029using SensorBaseConfiguration =
30 std::pair<std::string,
31 boost::container::flat_map<std::string, BasicVariantType>>;
32
James Feistcf3bce62019-01-08 10:07:19 -080033bool findFiles(const std::filesystem::path dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070034 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080035 std::vector<std::filesystem::path>& foundPaths,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070036 unsigned int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080037bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080038bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080039void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070040bool getSensorConfiguration(
41 const std::string& type,
42 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist87d713a2018-12-06 16:06:24 -080043 ManagedObjectType& resp, bool useCache = false);
44
45// replaces limits if MinReading and MaxReading are found.
46void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080047 const SensorBaseConfiguration* data);
48
James Feistfc94b212019-02-06 16:14:51 -080049enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080050{
51 on,
James Feistfc94b212019-02-06 16:14:51 -080052 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080053 always
54};
55
James Feist40a72142018-12-21 10:09:53 -080056template <typename T>
57inline T loadVariant(
58 const boost::container::flat_map<std::string, BasicVariantType>& data,
59 const std::string& key)
60{
61 auto it = data.find(key);
62 if (it == data.end())
63 {
64 std::cerr << "Configuration missing " << key << "\n";
65 throw std::invalid_argument("Key Missing");
66 }
67 if constexpr (std::is_same_v<T, double>)
68 {
69 return sdbusplus::message::variant_ns::visit(VariantToDoubleVisitor(),
70 it->second);
71 }
James Feist6ef20402019-01-07 16:45:08 -080072 else if constexpr (std::is_unsigned_v<T>)
73 {
74 return sdbusplus::message::variant_ns::visit(
75 VariantToUnsignedIntVisitor(), it->second);
76 }
James Feist40a72142018-12-21 10:09:53 -080077 else if constexpr (std::is_same_v<T, std::string>)
78 {
79 return sdbusplus::message::variant_ns::visit(VariantToStringVisitor(),
80 it->second);
81 }
82 else
83 {
84 static_assert("Type Not Implemented");
85 }
86}
James Feistfc94b212019-02-06 16:14:51 -080087
88inline void setReadState(const std::string& str, PowerState& val)
89{
90
91 if (str == "On")
92 {
93 val = PowerState::on;
94 }
95 else if (str == "BiosPost")
96 {
97 val = PowerState::biosPost;
98 }
99 else if (str == "Always")
100 {
101 val = PowerState::always;
102 }
103}