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::object<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::bus&)>; 102 103 using CreateGroupFunction = std::function<std::unique_ptr<trust::Group>()>; 104 105 constexpr auto sensorNameField = 0; 106 constexpr auto hasTargetField = 1; 107 constexpr auto targetInterfaceField = 2; 108 constexpr auto factorField = 3; 109 constexpr auto offsetField = 4; 110 constexpr auto thresholdField = 5; 111 112 using SensorDefinition = 113 std::tuple<std::string, bool, std::string, double, int64_t, size_t>; 114 115 constexpr auto fanNameField = 0; 116 constexpr auto methodField = 1; 117 constexpr auto funcDelay = 2; 118 constexpr auto timeoutField = 3; 119 constexpr auto fanDeviationField = 4; 120 constexpr auto numSensorFailsForNonfuncField = 5; 121 constexpr auto monitorStartDelayField = 6; 122 constexpr auto nonfuncRotorErrDelayField = 7; 123 constexpr auto fanMissingErrDelayField = 8; 124 constexpr auto sensorListField = 9; 125 constexpr auto conditionField = 10; 126 127 using FanDefinition = 128 std::tuple<std::string, size_t, size_t, size_t, size_t, size_t, size_t, 129 std::optional<size_t>, std::optional<size_t>, 130 std::vector<SensorDefinition>, std::optional<Condition>>; 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