xref: /openbmc/phosphor-fan-presence/monitor/types.hpp (revision a00f683cea5d288e2996a4b20abe3fb20d59f716)
1 #pragma once
2 
3 #include "tach_sensor.hpp"
4 #include "trust_group.hpp"
5 
6 #include <nlohmann/json.hpp>
7 #include <phosphor-logging/lg2.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      */
getPropValue(const json & entry,const std::string & type="")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         lg2::error(
87             "Unsupported data type {GIVEN_ENTRY_TYPE} for JSON entry's value. Supported types are 'bool, int64_t, std::string'",
88             "GIVEN_ENTRY_TYPE", type, "JSON_ENTRY", entry.dump());
89         throw std::runtime_error(
90             "Unsupported data type for JSON entry's value");
91     }
92 };
93 
94 constexpr auto propIdentity = 0;
95 constexpr auto propValue = 1;
96 using PropertyState = std::pair<PropertyIdentity, PropertyValue>;
97 
98 using Condition = std::function<bool(sdbusplus::bus_t&)>;
99 
100 using CreateGroupFunction = std::function<std::unique_ptr<trust::Group>()>;
101 
102 struct SensorDefinition
103 {
104     std::string name;
105     bool hasTarget;
106     std::string targetInterface;
107     std::string targetPath;
108     double factor;
109     int64_t offset;
110     size_t threshold;
111     bool ignoreAboveMax;
112 };
113 
114 struct FanDefinition
115 {
116     std::string name;
117     size_t method;
118     size_t funcDelay;
119     size_t timeout;
120     size_t deviation;
121     size_t upperDeviation;
122     size_t numSensorFailsForNonfunc;
123     size_t monitorStartDelay;
124     size_t countInterval;
125     std::optional<size_t> nonfuncRotorErrDelay;
126     std::optional<size_t> fanMissingErrDelay;
127     std::vector<SensorDefinition> sensorList;
128     std::optional<Condition> condition;
129     bool funcOnPresent;
130 };
131 
132 constexpr auto presentHealthPos = 0;
133 constexpr auto sensorFuncHealthPos = 1;
134 
135 using FanHealthEntry = std::tuple<bool, std::vector<bool>>;
136 using FanHealth = std::map<std::string, FanHealthEntry>;
137 } // namespace monitor
138 } // namespace fan
139 } // namespace phosphor
140