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