blob: 7f24e2131d86af1fe81a5359845250d17c9b55a1 [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 Feist6714a252018-09-10 15:26:18 -07004#include <boost/container/flat_map.hpp>
James Feist24f02f22019-04-15 11:05:39 -07005#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -07006#include <iostream>
7#include <regex>
8#include <sdbusplus/asio/connection.hpp>
James Feist82bac4c2019-03-11 11:16:53 -07009#include <sdbusplus/asio/object_server.hpp>
James Feist6714a252018-09-10 15:26:18 -070010#include <sdbusplus/message/types.hpp>
11
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070012const constexpr char* jsonStore = "/var/configuration/flattened.json";
13const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
14const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
15const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070016
17using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080018 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
19 double, int32_t, uint32_t, int16_t, 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
James Feist82bac4c2019-03-11 11:16:53 -070045void createAssociation(
46 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
47 const std::string& path);
48
James Feist87d713a2018-12-06 16:06:24 -080049// replaces limits if MinReading and MaxReading are found.
50void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080051 const SensorBaseConfiguration* data);
52
James Feistfc94b212019-02-06 16:14:51 -080053enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080054{
55 on,
James Feistfc94b212019-02-06 16:14:51 -080056 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080057 always
58};
59
James Feist40a72142018-12-21 10:09:53 -080060template <typename T>
61inline T loadVariant(
62 const boost::container::flat_map<std::string, BasicVariantType>& data,
63 const std::string& key)
64{
65 auto it = data.find(key);
66 if (it == data.end())
67 {
68 std::cerr << "Configuration missing " << key << "\n";
69 throw std::invalid_argument("Key Missing");
70 }
71 if constexpr (std::is_same_v<T, double>)
72 {
James Feist3eb82622019-02-08 13:10:22 -080073 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -080074 }
James Feist6ef20402019-01-07 16:45:08 -080075 else if constexpr (std::is_unsigned_v<T>)
76 {
James Feist3eb82622019-02-08 13:10:22 -080077 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -080078 }
James Feist40a72142018-12-21 10:09:53 -080079 else if constexpr (std::is_same_v<T, std::string>)
80 {
James Feist3eb82622019-02-08 13:10:22 -080081 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -080082 }
83 else
84 {
85 static_assert("Type Not Implemented");
86 }
87}
James Feistfc94b212019-02-06 16:14:51 -080088
89inline void setReadState(const std::string& str, PowerState& val)
90{
91
92 if (str == "On")
93 {
94 val = PowerState::on;
95 }
96 else if (str == "BiosPost")
97 {
98 val = PowerState::biosPost;
99 }
100 else if (str == "Always")
101 {
102 val = PowerState::always;
103 }
104}