blob: 91d6a54094b18aa2b9c718b9f0555da5cb83c614 [file] [log] [blame]
Matthew Barth81748b12018-05-02 16:03:48 -05001#include "conditions.hpp"
Matthew Barth177fe982020-05-26 11:05:19 -05002
Matthew Barth81748b12018-05-02 16:03:48 -05003#include "sdbusplus.hpp"
Matthew Barth3ad14342020-06-08 16:17:42 -05004#include "types.hpp"
5
6#include <nlohmann/json.hpp>
Anwaar Hadia00f6832025-03-27 15:03:11 +00007#include <phosphor-logging/lg2.hpp>
Matthew Barth81748b12018-05-02 16:03:48 -05008
Matthew Barth177fe982020-05-26 11:05:19 -05009#include <algorithm>
10
Matthew Barth81748b12018-05-02 16:03:48 -050011namespace phosphor
12{
13namespace fan
14{
15namespace monitor
16{
17namespace condition
18{
19
Matthew Barth3ad14342020-06-08 16:17:42 -050020using json = nlohmann::json;
21using namespace phosphor::logging;
22
Matthew Barth81748b12018-05-02 16:03:48 -050023Condition propertiesMatch(std::vector<PropertyState>&& propStates)
24{
Patrick Williamscb356d42022-07-22 19:26:53 -050025 return [pStates = std::move(propStates)](sdbusplus::bus_t& bus) {
Patrick Williamsdfddd642024-08-16 15:21:51 -040026 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 Barth81748b12018-05-02 16:03:48 -050033 };
34}
35
Matthew Barth3ad14342020-06-08 16:17:42 -050036Condition getPropertiesMatch(const json& condParams)
37{
38 if (!condParams.contains("properties"))
39 {
40 // Log error on missing required parameter
Anwaar Hadia00f6832025-03-27 15:03:11 +000041 lg2::error("Missing fan monitor condition properties {NAME}", "NAME",
42 condParams["name"].get<std::string>());
Matthew Barth3ad14342020-06-08 16:17:42 -050043 throw std::runtime_error("Missing fan monitor condition properties");
44 }
45 std::vector<PropertyState> propStates;
46 for (auto& param : condParams["properties"])
47 {
48 if (!param.contains("object") || !param.contains("interface") ||
49 !param.contains("property"))
50 {
51 // Log error on missing required parameters
Anwaar Hadia00f6832025-03-27 15:03:11 +000052 lg2::error(
53 "Missing properties. Match condition parameters 'object, interface, property'");
Matthew Barth3ad14342020-06-08 16:17:42 -050054 throw std::runtime_error(
55 "Missing propertiesMatch condition parameters");
56 }
57
58 auto propAttrs = param["property"];
59 if (!propAttrs.contains("name") || !propAttrs.contains("value"))
60 {
61 // Log error on missing required parameters
Anwaar Hadia00f6832025-03-27 15:03:11 +000062 lg2::error(
63 "Missing properties. Match condition property attributes 'name, value'");
Matthew Barth3ad14342020-06-08 16:17:42 -050064 throw std::runtime_error(
65 "Missing propertiesMatch condition property attributes");
66 }
67
68 std::string type = "";
69 if (propAttrs.contains("type"))
70 {
71 type = propAttrs["type"].get<std::string>();
72 }
73
74 // Add property for propertiesMatch condition
75 propStates.emplace_back(PropertyState(
76 {param["object"].get<std::string>(),
77 param["interface"].get<std::string>(),
78 propAttrs["name"].get<std::string>()},
79 JsonTypeHandler::getPropValue(propAttrs["value"], type)));
80 }
81
82 return make_condition(condition::propertiesMatch(std::move(propStates)));
83}
84
Matthew Barth81748b12018-05-02 16:03:48 -050085} // namespace condition
86} // namespace monitor
87} // namespace fan
88} // namespace phosphor