blob: a1c9d378bf9594c85f35a99b2ab7e87c71b4972e [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 Feist3eb82622019-02-08 13:10:22 -080017 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
18 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
James Feist6714a252018-09-10 15:26:18 -070019
20using ManagedObjectType = boost::container::flat_map<
21 sdbusplus::message::object_path,
22 boost::container::flat_map<
23 std::string,
24 boost::container::flat_map<std::string, BasicVariantType>>>;
25using SensorData = boost::container::flat_map<
26 std::string, boost::container::flat_map<std::string, BasicVariantType>>;
27
James Feist87d713a2018-12-06 16:06:24 -080028using SensorBaseConfiguration =
29 std::pair<std::string,
30 boost::container::flat_map<std::string, BasicVariantType>>;
31
James Feistcf3bce62019-01-08 10:07:19 -080032bool findFiles(const std::filesystem::path dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070033 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080034 std::vector<std::filesystem::path>& foundPaths,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070035 unsigned int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080036bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080037bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080038void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070039bool getSensorConfiguration(
40 const std::string& type,
41 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist87d713a2018-12-06 16:06:24 -080042 ManagedObjectType& resp, bool useCache = false);
43
44// replaces limits if MinReading and MaxReading are found.
45void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080046 const SensorBaseConfiguration* data);
47
James Feistfc94b212019-02-06 16:14:51 -080048enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080049{
50 on,
James Feistfc94b212019-02-06 16:14:51 -080051 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080052 always
53};
54
James Feist40a72142018-12-21 10:09:53 -080055template <typename T>
56inline T loadVariant(
57 const boost::container::flat_map<std::string, BasicVariantType>& data,
58 const std::string& key)
59{
60 auto it = data.find(key);
61 if (it == data.end())
62 {
63 std::cerr << "Configuration missing " << key << "\n";
64 throw std::invalid_argument("Key Missing");
65 }
66 if constexpr (std::is_same_v<T, double>)
67 {
James Feist3eb82622019-02-08 13:10:22 -080068 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -080069 }
James Feist6ef20402019-01-07 16:45:08 -080070 else if constexpr (std::is_unsigned_v<T>)
71 {
James Feist3eb82622019-02-08 13:10:22 -080072 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -080073 }
James Feist40a72142018-12-21 10:09:53 -080074 else if constexpr (std::is_same_v<T, std::string>)
75 {
James Feist3eb82622019-02-08 13:10:22 -080076 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -080077 }
78 else
79 {
80 static_assert("Type Not Implemented");
81 }
82}
James Feistfc94b212019-02-06 16:14:51 -080083
84inline void setReadState(const std::string& str, PowerState& val)
85{
86
87 if (str == "On")
88 {
89 val = PowerState::on;
90 }
91 else if (str == "BiosPost")
92 {
93 val = PowerState::biosPost;
94 }
95 else if (str == "Always")
96 {
97 val = PowerState::always;
98 }
99}