xref: /openbmc/phosphor-fan-presence/control/json/triggers/handlers.hpp (revision dfddd648cb81b27492afead4e2346f5fcd1397cb)
1baeeb8f1SMatthew Barth #pragma once
2baeeb8f1SMatthew Barth 
3baeeb8f1SMatthew Barth #include "../manager.hpp"
4baeeb8f1SMatthew Barth 
5baeeb8f1SMatthew Barth #include <sdbusplus/message.hpp>
6baeeb8f1SMatthew Barth 
7baeeb8f1SMatthew Barth #include <algorithm>
8baeeb8f1SMatthew Barth #include <map>
9baeeb8f1SMatthew Barth #include <tuple>
10baeeb8f1SMatthew Barth #include <vector>
11baeeb8f1SMatthew Barth 
12baeeb8f1SMatthew Barth namespace phosphor::fan::control::json::trigger::signal
13baeeb8f1SMatthew Barth {
14baeeb8f1SMatthew Barth 
15baeeb8f1SMatthew Barth using namespace sdbusplus::message;
16baeeb8f1SMatthew Barth 
17baeeb8f1SMatthew Barth struct Handlers
18baeeb8f1SMatthew Barth {
19baeeb8f1SMatthew Barth   public:
20baeeb8f1SMatthew Barth     /**
21baeeb8f1SMatthew Barth      * @brief Processes a properties changed signal and updates the property's
22baeeb8f1SMatthew Barth      * value in the manager's object cache
23baeeb8f1SMatthew Barth      *
24baeeb8f1SMatthew Barth      * @param[in] msg - The sdbusplus signal message
25baeeb8f1SMatthew Barth      * @param[in] obj - Object data associated with the signal
26baeeb8f1SMatthew Barth      * @param[in] mgr - Manager that stores the object cache
27baeeb8f1SMatthew Barth      */
propertiesChangedphosphor::fan::control::json::trigger::signal::Handlers28baeeb8f1SMatthew Barth     static bool propertiesChanged(message& msg, const SignalObject& obj,
29baeeb8f1SMatthew Barth                                   Manager& mgr)
30baeeb8f1SMatthew Barth     {
31baeeb8f1SMatthew Barth         std::string intf;
32baeeb8f1SMatthew Barth         msg.read(intf);
33baeeb8f1SMatthew Barth         if (intf != std::get<Intf>(obj))
34baeeb8f1SMatthew Barth         {
35baeeb8f1SMatthew Barth             // Interface name does not match object's interface
36baeeb8f1SMatthew Barth             return false;
37baeeb8f1SMatthew Barth         }
38baeeb8f1SMatthew Barth 
39baeeb8f1SMatthew Barth         std::map<std::string, PropertyVariantType> props;
40baeeb8f1SMatthew Barth         msg.read(props);
41baeeb8f1SMatthew Barth         auto itProp = props.find(std::get<Prop>(obj));
42baeeb8f1SMatthew Barth         if (itProp == props.cend())
43baeeb8f1SMatthew Barth         {
44baeeb8f1SMatthew Barth             // Object's property not in dictionary of properties changed
45baeeb8f1SMatthew Barth             return false;
46baeeb8f1SMatthew Barth         }
47baeeb8f1SMatthew Barth 
48baeeb8f1SMatthew Barth         mgr.setProperty(std::get<Path>(obj), std::get<Intf>(obj),
49baeeb8f1SMatthew Barth                         std::get<Prop>(obj), itProp->second);
50baeeb8f1SMatthew Barth         return true;
51baeeb8f1SMatthew Barth     }
52599afcefSMatthew Barth 
53599afcefSMatthew Barth     /**
54599afcefSMatthew Barth      * @brief Processes an interfaces added signal and adds the interface
55599afcefSMatthew Barth      * (including property & property value) to the manager's object cache
56599afcefSMatthew Barth      *
57599afcefSMatthew Barth      * @param[in] msg - The sdbusplus signal message
58599afcefSMatthew Barth      * @param[in] obj - Object data associated with the signal
59599afcefSMatthew Barth      * @param[in] mgr - Manager that stores the object cache
60599afcefSMatthew Barth      */
interfacesAddedphosphor::fan::control::json::trigger::signal::Handlers61599afcefSMatthew Barth     static bool interfacesAdded(message& msg, const SignalObject& obj,
62599afcefSMatthew Barth                                 Manager& mgr)
63599afcefSMatthew Barth     {
64599afcefSMatthew Barth         sdbusplus::message::object_path op;
65599afcefSMatthew Barth         msg.read(op);
66599afcefSMatthew Barth         if (static_cast<const std::string&>(op) != std::get<Path>(obj))
67599afcefSMatthew Barth         {
68599afcefSMatthew Barth             // Path name does not match object's path
69599afcefSMatthew Barth             return false;
70599afcefSMatthew Barth         }
71599afcefSMatthew Barth 
72599afcefSMatthew Barth         std::map<std::string, std::map<std::string, PropertyVariantType>>
73599afcefSMatthew Barth             intfProps;
74599afcefSMatthew Barth         msg.read(intfProps);
75599afcefSMatthew Barth         auto itIntf = intfProps.find(std::get<Intf>(obj));
76599afcefSMatthew Barth         if (itIntf == intfProps.cend())
77599afcefSMatthew Barth         {
78599afcefSMatthew Barth             // Object's interface not in dictionary of interfaces added
79599afcefSMatthew Barth             return false;
80599afcefSMatthew Barth         }
81599afcefSMatthew Barth 
82599afcefSMatthew Barth         auto itProp = itIntf->second.find(std::get<Prop>(obj));
83599afcefSMatthew Barth         if (itProp == itIntf->second.cend())
84599afcefSMatthew Barth         {
85599afcefSMatthew Barth             // Object's property not in dictionary of properties of interface
86599afcefSMatthew Barth             return false;
87599afcefSMatthew Barth         }
88599afcefSMatthew Barth 
89599afcefSMatthew Barth         mgr.setProperty(std::get<Path>(obj), std::get<Intf>(obj),
90599afcefSMatthew Barth                         std::get<Prop>(obj), itProp->second);
91599afcefSMatthew Barth         return true;
92599afcefSMatthew Barth     }
93c2691407SMatthew Barth 
94c2691407SMatthew Barth     /**
95c2691407SMatthew Barth      * @brief Processes an interfaces removed signal and removes the interface
96c2691407SMatthew Barth      * (including its properties) from the object cache on the manager
97c2691407SMatthew Barth      *
98c2691407SMatthew Barth      * @param[in] msg - The sdbusplus signal message
99c2691407SMatthew Barth      * @param[in] obj - Object data associated with the signal
100c2691407SMatthew Barth      * @param[in] mgr - Manager that stores the object cache
101c2691407SMatthew Barth      */
interfacesRemovedphosphor::fan::control::json::trigger::signal::Handlers102c2691407SMatthew Barth     static bool interfacesRemoved(message& msg, const SignalObject& obj,
103c2691407SMatthew Barth                                   Manager& mgr)
104c2691407SMatthew Barth     {
105c2691407SMatthew Barth         sdbusplus::message::object_path op;
106c2691407SMatthew Barth         msg.read(op);
107c2691407SMatthew Barth         if (static_cast<const std::string&>(op) != std::get<Path>(obj))
108c2691407SMatthew Barth         {
109c2691407SMatthew Barth             // Path name does not match object's path
110c2691407SMatthew Barth             return false;
111c2691407SMatthew Barth         }
112c2691407SMatthew Barth 
113c2691407SMatthew Barth         std::vector<std::string> intfs;
114c2691407SMatthew Barth         msg.read(intfs);
115*dfddd648SPatrick Williams         auto itIntf =
116*dfddd648SPatrick Williams             std::find(intfs.begin(), intfs.end(), std::get<Intf>(obj));
117c2691407SMatthew Barth         if (itIntf == intfs.cend())
118c2691407SMatthew Barth         {
119c2691407SMatthew Barth             // Object's interface not in list of interfaces removed
120c2691407SMatthew Barth             return false;
121c2691407SMatthew Barth         }
122c2691407SMatthew Barth 
123c2691407SMatthew Barth         mgr.removeInterface(std::get<Path>(obj), std::get<Intf>(obj));
124c2691407SMatthew Barth         return true;
125c2691407SMatthew Barth     }
126b03f6bb0SMatthew Barth 
127b03f6bb0SMatthew Barth     /**
128b03f6bb0SMatthew Barth      * @brief Processes a name owner changed signal and updates the service's
1296d8e2d3eSMatthew Barth      * owner state for all objects/interfaces associated in the cache
130b03f6bb0SMatthew Barth      *
131b03f6bb0SMatthew Barth      * @param[in] msg - The sdbusplus signal message
132b03f6bb0SMatthew Barth      * @param[in] mgr - Manager that stores the service's owner state
133b03f6bb0SMatthew Barth      */
nameOwnerChangedphosphor::fan::control::json::trigger::signal::Handlers1346d8e2d3eSMatthew Barth     static bool nameOwnerChanged(message& msg, const SignalObject&,
135b03f6bb0SMatthew Barth                                  Manager& mgr)
136b03f6bb0SMatthew Barth     {
137b03f6bb0SMatthew Barth         bool hasOwner = false;
138b03f6bb0SMatthew Barth 
139b03f6bb0SMatthew Barth         std::string serv;
140b03f6bb0SMatthew Barth         msg.read(serv);
141b03f6bb0SMatthew Barth 
142b03f6bb0SMatthew Barth         std::string oldOwner;
143b03f6bb0SMatthew Barth         msg.read(oldOwner);
144b03f6bb0SMatthew Barth 
145b03f6bb0SMatthew Barth         std::string newOwner;
146b03f6bb0SMatthew Barth         msg.read(newOwner);
147b03f6bb0SMatthew Barth         if (!newOwner.empty())
148b03f6bb0SMatthew Barth         {
149b03f6bb0SMatthew Barth             hasOwner = true;
150b03f6bb0SMatthew Barth         }
1517a656e2cSMatthew Barth 
1526d8e2d3eSMatthew Barth         mgr.setOwner(serv, hasOwner);
153b03f6bb0SMatthew Barth         return true;
154b03f6bb0SMatthew Barth     }
15516861797SMatthew Barth 
15616861797SMatthew Barth     /**
15716861797SMatthew Barth      * @brief Processes a dbus member signal, there is nothing associated or
15816861797SMatthew Barth      * any cache to update when this signal is received
15916861797SMatthew Barth      */
memberphosphor::fan::control::json::trigger::signal::Handlers16016861797SMatthew Barth     static bool member(message&, const SignalObject&, Manager&)
16116861797SMatthew Barth     {
16216861797SMatthew Barth         return true;
16316861797SMatthew Barth     }
164baeeb8f1SMatthew Barth };
165baeeb8f1SMatthew Barth 
166baeeb8f1SMatthew Barth } // namespace phosphor::fan::control::json::trigger::signal
167