1 #pragma once
2 
3 #include "tach_sensor.hpp"
4 #include "trust_group.hpp"
5 
6 #include <nlohmann/json.hpp>
7 #include <phosphor-logging/log.hpp>
8 #include <xyz/openbmc_project/Object/Enable/server.hpp>
9 
10 #include <functional>
11 #include <optional>
12 #include <string>
13 #include <tuple>
14 #include <vector>
15 
16 namespace phosphor
17 {
18 namespace fan
19 {
20 namespace monitor
21 {
22 
23 template <typename... T>
24 using ServerObject = typename sdbusplus::server::object_t<T...>;
25 
26 using ObjectEnableInterface =
27     sdbusplus::xyz::openbmc_project::Object::server::Enable;
28 
29 using ThermalAlertObject = ServerObject<ObjectEnableInterface>;
30 
31 constexpr auto propObj = 0;
32 constexpr auto propIface = 1;
33 constexpr auto propName = 2;
34 using PropertyIdentity = std::tuple<std::string, std::string, std::string>;
35 
36 using PropertyValue = std::variant<bool, int64_t, std::string>;
37 class JsonTypeHandler
38 {
39     using json = nlohmann::json;
40 
41   public:
42     /**
43      * @brief Determines the data type of a JSON configured parameter that is
44      * used as a variant within the fan monitor application and returns the
45      * value as that variant.
46      * @details Retrieves a JSON entry by the first derived data type that
47      * is not null. Expected data types should appear in a logical order of
48      * conversion. i.e.) uint and int could both be uint Alternatively, the
49      * expected data type can be given to force which supported data type
50      * the JSON entry should be retrieved as.
51      *
52      * @param[in] entry - A single JSON entry
53      * @param[in] type - (OPTIONAL) The preferred data type of the entry
54      *
55      * @return A `PropertyValue` variant containing the JSON entry's value
56      */
57     static const PropertyValue getPropValue(const json& entry,
58                                             const std::string& type = "")
59     {
60         PropertyValue value;
61         if (auto boolPtr = entry.get_ptr<const bool*>())
62         {
63             if (type.empty() || type == "bool")
64             {
65                 value = *boolPtr;
66                 return value;
67             }
68         }
69         if (auto int64Ptr = entry.get_ptr<const int64_t*>())
70         {
71             if (type.empty() || type == "int64_t")
72             {
73                 value = *int64Ptr;
74                 return value;
75             }
76         }
77         if (auto stringPtr = entry.get_ptr<const std::string*>())
78         {
79             if (type.empty() || type == "std::string")
80             {
81                 value = *stringPtr;
82                 return value;
83             }
84         }
85 
86         phosphor::logging::log<phosphor::logging::level::ERR>(
87             "Unsupported data type for JSON entry's value",
88             phosphor::logging::entry("GIVEN_ENTRY_TYPE=%s", type.c_str()),
89             phosphor::logging::entry("JSON_ENTRY=%s", entry.dump().c_str()),
90             phosphor::logging::entry("SUPPORTED_TYPES=%s",
91                                      "{bool, int64_t, std::string}"));
92         throw std::runtime_error(
93             "Unsupported data type for JSON entry's value");
94     }
95 };
96 
97 constexpr auto propIdentity = 0;
98 constexpr auto propValue = 1;
99 using PropertyState = std::pair<PropertyIdentity, PropertyValue>;
100 
101 using Condition = std::function<bool(sdbusplus::bus_t&)>;
102 
103 using CreateGroupFunction = std::function<std::unique_ptr<trust::Group>()>;
104 
105 struct SensorDefinition
106 {
107     std::string name;
108     bool hasTarget;
109     std::string targetInterface;
110     std::string targetPath;
111     double factor;
112     int64_t offset;
113     size_t threshold;
114     bool ignoreAboveMax;
115 };
116 
117 struct FanDefinition
118 {
119     std::string name;
120     size_t method;
121     size_t funcDelay;
122     size_t timeout;
123     size_t deviation;
124     size_t upperDeviation;
125     size_t numSensorFailsForNonfunc;
126     size_t monitorStartDelay;
127     size_t countInterval;
128     std::optional<size_t> nonfuncRotorErrDelay;
129     std::optional<size_t> fanMissingErrDelay;
130     std::vector<SensorDefinition> sensorList;
131     std::optional<Condition> condition;
132     bool funcOnPresent;
133 };
134 
135 constexpr auto presentHealthPos = 0;
136 constexpr auto sensorFuncHealthPos = 1;
137 
138 using FanHealthEntry = std::tuple<bool, std::vector<bool>>;
139 using FanHealth = std::map<std::string, FanHealthEntry>;
140 } // namespace monitor
141 } // namespace fan
142 } // namespace phosphor
143