Matthew Barth | 81748b1 | 2018-05-02 16:03:48 -0500 | [diff] [blame] | 1 | #include "conditions.hpp" |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 2 | |
Matthew Barth | 81748b1 | 2018-05-02 16:03:48 -0500 | [diff] [blame] | 3 | #include "sdbusplus.hpp" |
Matthew Barth | 3ad1434 | 2020-06-08 16:17:42 -0500 | [diff] [blame] | 4 | #include "types.hpp" |
| 5 | |
| 6 | #include <nlohmann/json.hpp> |
| 7 | #include <phosphor-logging/log.hpp> |
Matthew Barth | 81748b1 | 2018-05-02 16:03:48 -0500 | [diff] [blame] | 8 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
Matthew Barth | 81748b1 | 2018-05-02 16:03:48 -0500 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace fan |
| 14 | { |
| 15 | namespace monitor |
| 16 | { |
| 17 | namespace condition |
| 18 | { |
| 19 | |
Matthew Barth | 3ad1434 | 2020-06-08 16:17:42 -0500 | [diff] [blame] | 20 | using json = nlohmann::json; |
| 21 | using namespace phosphor::logging; |
| 22 | |
Matthew Barth | 81748b1 | 2018-05-02 16:03:48 -0500 | [diff] [blame] | 23 | Condition propertiesMatch(std::vector<PropertyState>&& propStates) |
| 24 | { |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 25 | return [pStates = std::move(propStates)](sdbusplus::bus_t& bus) { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 26 | return std::all_of( |
| 27 | pStates.begin(), pStates.end(), [&bus](const auto& p) { |
| 28 | return util::SDBusPlus::getPropertyVariant<PropertyValue>( |
| 29 | bus, std::get<propObj>(p.first), |
| 30 | std::get<propIface>(p.first), |
| 31 | std::get<propName>(p.first)) == p.second; |
| 32 | }); |
Matthew Barth | 81748b1 | 2018-05-02 16:03:48 -0500 | [diff] [blame] | 33 | }; |
| 34 | } |
| 35 | |
Matthew Barth | 3ad1434 | 2020-06-08 16:17:42 -0500 | [diff] [blame] | 36 | Condition getPropertiesMatch(const json& condParams) |
| 37 | { |
| 38 | if (!condParams.contains("properties")) |
| 39 | { |
| 40 | // Log error on missing required parameter |
| 41 | log<level::ERR>( |
| 42 | "Missing fan monitor condition properties", |
| 43 | entry("NAME=%s", condParams["name"].get<std::string>().c_str())); |
| 44 | throw std::runtime_error("Missing fan monitor condition properties"); |
| 45 | } |
| 46 | std::vector<PropertyState> propStates; |
| 47 | for (auto& param : condParams["properties"]) |
| 48 | { |
| 49 | if (!param.contains("object") || !param.contains("interface") || |
| 50 | !param.contains("property")) |
| 51 | { |
| 52 | // Log error on missing required parameters |
| 53 | log<level::ERR>("Missing propertiesMatch condition parameters", |
| 54 | entry("REQUIRED_PARAMETERS=%s", |
| 55 | "{object, interface, property}")); |
| 56 | throw std::runtime_error( |
| 57 | "Missing propertiesMatch condition parameters"); |
| 58 | } |
| 59 | |
| 60 | auto propAttrs = param["property"]; |
| 61 | if (!propAttrs.contains("name") || !propAttrs.contains("value")) |
| 62 | { |
| 63 | // Log error on missing required parameters |
| 64 | log<level::ERR>( |
| 65 | "Missing propertiesMatch condition property attributes", |
| 66 | entry("REQUIRED_ATTRIBUTES=%s", "{name, value}")); |
| 67 | throw std::runtime_error( |
| 68 | "Missing propertiesMatch condition property attributes"); |
| 69 | } |
| 70 | |
| 71 | std::string type = ""; |
| 72 | if (propAttrs.contains("type")) |
| 73 | { |
| 74 | type = propAttrs["type"].get<std::string>(); |
| 75 | } |
| 76 | |
| 77 | // Add property for propertiesMatch condition |
| 78 | propStates.emplace_back(PropertyState( |
| 79 | {param["object"].get<std::string>(), |
| 80 | param["interface"].get<std::string>(), |
| 81 | propAttrs["name"].get<std::string>()}, |
| 82 | JsonTypeHandler::getPropValue(propAttrs["value"], type))); |
| 83 | } |
| 84 | |
| 85 | return make_condition(condition::propertiesMatch(std::move(propStates))); |
| 86 | } |
| 87 | |
Matthew Barth | 81748b1 | 2018-05-02 16:03:48 -0500 | [diff] [blame] | 88 | } // namespace condition |
| 89 | } // namespace monitor |
| 90 | } // namespace fan |
| 91 | } // namespace phosphor |