xref: /openbmc/phosphor-fan-presence/control/functor.hpp (revision 469d136669e47e7004916b905eccbb388e607828)
138a93a8aSMatthew Barth #pragma once
238a93a8aSMatthew Barth 
338a93a8aSMatthew Barth #include "types.hpp"
4336f18a5SMatthew Barth #include "sdbusplus.hpp"
538a93a8aSMatthew Barth #include <phosphor-logging/log.hpp>
638a93a8aSMatthew Barth 
738a93a8aSMatthew Barth namespace phosphor
838a93a8aSMatthew Barth {
938a93a8aSMatthew Barth namespace fan
1038a93a8aSMatthew Barth {
1138a93a8aSMatthew Barth namespace control
1238a93a8aSMatthew Barth {
1338a93a8aSMatthew Barth class Zone;
1438a93a8aSMatthew Barth 
15336f18a5SMatthew Barth using namespace phosphor::fan;
165a302576SMatthew Barth using namespace sdbusplus::bus::match;
1738a93a8aSMatthew Barth using namespace phosphor::logging;
1838a93a8aSMatthew Barth 
1938a93a8aSMatthew Barth /**
201b3e9602SMatthew Barth  * @brief Create a zone handler function object
211b3e9602SMatthew Barth  *
221b3e9602SMatthew Barth  * @param[in] handler - The handler being created
231b3e9602SMatthew Barth  *
241b3e9602SMatthew Barth  * @return - The created zone handler function object
251b3e9602SMatthew Barth  */
261b3e9602SMatthew Barth template <typename T>
271b3e9602SMatthew Barth auto make_zoneHandler(T&& handler)
281b3e9602SMatthew Barth {
291b3e9602SMatthew Barth     return ZoneHandler(std::forward<T>(handler));
301b3e9602SMatthew Barth }
311b3e9602SMatthew Barth 
321b3e9602SMatthew Barth /**
331b4de26aSMatthew Barth  * @brief Create a trigger function object
341b4de26aSMatthew Barth  *
351b4de26aSMatthew Barth  * @param[in] trigger - The trigger being created
361b4de26aSMatthew Barth  *
371b4de26aSMatthew Barth  * @return - The created trigger function object
381b4de26aSMatthew Barth  */
391b4de26aSMatthew Barth template <typename T>
401b4de26aSMatthew Barth auto make_trigger(T&& trigger)
411b4de26aSMatthew Barth {
421b4de26aSMatthew Barth    return Trigger(std::forward<T>(trigger));
431b4de26aSMatthew Barth }
441b4de26aSMatthew Barth 
451b4de26aSMatthew Barth /**
4638a93a8aSMatthew Barth  * @brief Create a handler function object
4738a93a8aSMatthew Barth  *
4838a93a8aSMatthew Barth  * @param[in] handler - The handler being created
4938a93a8aSMatthew Barth  *
5038a93a8aSMatthew Barth  * @return - The created handler function object
5138a93a8aSMatthew Barth  */
52926df663SMatthew Barth template <typename T, typename U>
53926df663SMatthew Barth auto make_handler(U&& handler)
5438a93a8aSMatthew Barth {
55926df663SMatthew Barth     return T(std::forward<U>(handler));
5638a93a8aSMatthew Barth }
5738a93a8aSMatthew Barth 
5838a93a8aSMatthew Barth /**
5917d1fe23SMatthew Barth  * @brief Create an action function object
6017d1fe23SMatthew Barth  *
6117d1fe23SMatthew Barth  * @param[in] action - The action being created
6217d1fe23SMatthew Barth  *
6317d1fe23SMatthew Barth  * @return - The created action function object
6417d1fe23SMatthew Barth  */
6517d1fe23SMatthew Barth template <typename T>
6617d1fe23SMatthew Barth auto make_action(T&& action)
6717d1fe23SMatthew Barth {
6817d1fe23SMatthew Barth     return Action(std::forward<T>(action));
6917d1fe23SMatthew Barth }
7017d1fe23SMatthew Barth 
7117d1fe23SMatthew Barth /**
72926df663SMatthew Barth  * @struct Properties
73926df663SMatthew Barth  * @brief A set of match filter functors for Dbus property values. Each
74926df663SMatthew Barth  * functor provides an associated process for retrieving the value
75926df663SMatthew Barth  * for a given property and providing it to the given handler function.
7638a93a8aSMatthew Barth  *
7738a93a8aSMatthew Barth  * @tparam T - The type of the property value
7838a93a8aSMatthew Barth  * @tparam U - The type of the handler
7938a93a8aSMatthew Barth  */
8038a93a8aSMatthew Barth template <typename T, typename U>
81926df663SMatthew Barth struct Properties
8238a93a8aSMatthew Barth {
83926df663SMatthew Barth     Properties() = delete;
84926df663SMatthew Barth     ~Properties() = default;
85926df663SMatthew Barth     Properties(const Properties&) = default;
86926df663SMatthew Barth     Properties& operator=(const Properties&) = default;
87926df663SMatthew Barth     Properties(Properties&&) = default;
88926df663SMatthew Barth     Properties& operator=(Properties&&) = default;
89926df663SMatthew Barth     explicit Properties(U&& handler) :
90926df663SMatthew Barth         _handler(std::forward<U>(handler)) { }
91926df663SMatthew Barth     Properties(const char* path,
92*469d1366SMatthew Barth                const char* intf,
93*469d1366SMatthew Barth                const char* prop,
9438a93a8aSMatthew Barth                U&& handler) :
95336f18a5SMatthew Barth         _path(path),
96*469d1366SMatthew Barth         _intf(intf),
97*469d1366SMatthew Barth         _prop(prop),
9838a93a8aSMatthew Barth         _handler(std::forward<U>(handler)) { }
9938a93a8aSMatthew Barth 
10038a93a8aSMatthew Barth     /** @brief Run signal handler function
10138a93a8aSMatthew Barth      *
10238a93a8aSMatthew Barth      * Extract the property from the PropertiesChanged
103926df663SMatthew Barth      * message and run the handler function.
10438a93a8aSMatthew Barth      */
105336f18a5SMatthew Barth     void operator()(sdbusplus::bus::bus& bus,
10638a93a8aSMatthew Barth                     sdbusplus::message::message& msg,
10738a93a8aSMatthew Barth                     Zone& zone) const
10838a93a8aSMatthew Barth     {
109336f18a5SMatthew Barth         if (msg)
110336f18a5SMatthew Barth         {
111*469d1366SMatthew Barth             std::string intf;
112*469d1366SMatthew Barth             std::map<std::string, sdbusplus::message::variant<T>> props;
11338a93a8aSMatthew Barth 
114*469d1366SMatthew Barth             msg.read(intf);
115*469d1366SMatthew Barth             if (intf != _intf)
11638a93a8aSMatthew Barth             {
117*469d1366SMatthew Barth                 // Interface name does not match on object
11838a93a8aSMatthew Barth                 return;
11938a93a8aSMatthew Barth             }
12038a93a8aSMatthew Barth 
121*469d1366SMatthew Barth             msg.read(props);
122*469d1366SMatthew Barth             auto it = props.find(_prop);
123*469d1366SMatthew Barth             if (it == props.cend())
12438a93a8aSMatthew Barth             {
125*469d1366SMatthew Barth                 // Property not included in dictionary of properties changed
12638a93a8aSMatthew Barth                 return;
12738a93a8aSMatthew Barth             }
12838a93a8aSMatthew Barth 
129*469d1366SMatthew Barth             _handler(zone, _path, _intf, _prop, std::forward<T>(
1304978e06cSWilliam A. Kennington III                 sdbusplus::message::variant_ns::get<T>(it->second)));
13138a93a8aSMatthew Barth         }
132336f18a5SMatthew Barth         else
133336f18a5SMatthew Barth         {
134336f18a5SMatthew Barth             try
135336f18a5SMatthew Barth             {
136*469d1366SMatthew Barth                 auto val = zone.getPropertyByName<T>(_path, _intf, _prop);
137*469d1366SMatthew Barth                 _handler(zone, _path, _intf, _prop, std::forward<T>(val));
138336f18a5SMatthew Barth             }
13986be476bSMatthew Barth             catch (const sdbusplus::exception::SdBusError&)
14086be476bSMatthew Barth             {
14186be476bSMatthew Barth                 // Property will not be used unless a property changed
14286be476bSMatthew Barth                 // signal message is received for this property.
14386be476bSMatthew Barth             }
14486be476bSMatthew Barth             catch (const util::DBusError&)
145336f18a5SMatthew Barth             {
146336f18a5SMatthew Barth                 // Property will not be used unless a property changed
147336f18a5SMatthew Barth                 // signal message is received for this property.
148336f18a5SMatthew Barth             }
149336f18a5SMatthew Barth         }
150336f18a5SMatthew Barth     }
15138a93a8aSMatthew Barth 
152926df663SMatthew Barth     /** @brief Run init handler function
153926df663SMatthew Barth      *
154926df663SMatthew Barth      * Get the property from each member object of the group
155926df663SMatthew Barth      * and run the handler function.
156926df663SMatthew Barth      */
157926df663SMatthew Barth     void operator()(Zone& zone, const Group& group) const
158926df663SMatthew Barth     {
159926df663SMatthew Barth         std::for_each(
160926df663SMatthew Barth             group.begin(),
161926df663SMatthew Barth             group.end(),
162*469d1366SMatthew Barth             [&zone, handler = std::move(_handler)](auto const& member)
163926df663SMatthew Barth             {
164926df663SMatthew Barth                 auto path = std::get<pathPos>(member);
165926df663SMatthew Barth                 auto intf = std::get<intfPos>(member);
166926df663SMatthew Barth                 auto prop = std::get<propPos>(member);
167926df663SMatthew Barth                 try
168926df663SMatthew Barth                 {
169926df663SMatthew Barth                     auto val = zone.getPropertyByName<T>(path, intf, prop);
170*469d1366SMatthew Barth                     handler(zone, path, intf, prop, std::forward<T>(val));
171926df663SMatthew Barth                 }
172926df663SMatthew Barth                 catch (const sdbusplus::exception::SdBusError&)
173926df663SMatthew Barth                 {
174926df663SMatthew Barth                     // Property value not sent to handler
175926df663SMatthew Barth                 }
176926df663SMatthew Barth                 catch (const util::DBusError&)
177926df663SMatthew Barth                 {
178926df663SMatthew Barth                     // Property value not sent to handler
179926df663SMatthew Barth                 }
180926df663SMatthew Barth             }
181926df663SMatthew Barth         );
182926df663SMatthew Barth     }
183926df663SMatthew Barth 
18438a93a8aSMatthew Barth private:
185336f18a5SMatthew Barth     const char* _path;
186*469d1366SMatthew Barth     const char* _intf;
187*469d1366SMatthew Barth     const char* _prop;
18838a93a8aSMatthew Barth     U _handler;
18938a93a8aSMatthew Barth };
19038a93a8aSMatthew Barth 
19138a93a8aSMatthew Barth /**
192*469d1366SMatthew Barth  * @brief Used to process a Dbus properties changed signal event
19338a93a8aSMatthew Barth  *
194336f18a5SMatthew Barth  * @param[in] path - Object path
195*469d1366SMatthew Barth  * @param[in] intf - Object interface
196*469d1366SMatthew Barth  * @param[in] prop - Object property
19738a93a8aSMatthew Barth  * @param[in] handler - Handler function to perform
19838a93a8aSMatthew Barth  *
19938a93a8aSMatthew Barth  * @tparam T - The type of the property
20038a93a8aSMatthew Barth  * @tparam U - The type of the handler
20138a93a8aSMatthew Barth  */
20238a93a8aSMatthew Barth template <typename T, typename U>
203926df663SMatthew Barth auto propertiesChanged(const char* path,
204*469d1366SMatthew Barth                        const char* intf,
205*469d1366SMatthew Barth                        const char* prop,
20638a93a8aSMatthew Barth                        U&& handler)
20738a93a8aSMatthew Barth {
208926df663SMatthew Barth     return Properties<T, U>(path,
209*469d1366SMatthew Barth                             intf,
210*469d1366SMatthew Barth                             prop,
211336f18a5SMatthew Barth                             std::forward<U>(handler));
21238a93a8aSMatthew Barth }
21338a93a8aSMatthew Barth 
214eb639c57SMatthew Barth /**
215*469d1366SMatthew Barth  * @brief Used to get the properties of an event's group
216926df663SMatthew Barth  *
217926df663SMatthew Barth  * @param[in] handler - Handler function to perform
218926df663SMatthew Barth  *
219*469d1366SMatthew Barth  * @tparam T - The type of all the properties
220926df663SMatthew Barth  * @tparam U - The type of the handler
221926df663SMatthew Barth  */
222926df663SMatthew Barth template <typename T, typename U>
223*469d1366SMatthew Barth auto getProperties(U&& handler)
224926df663SMatthew Barth {
225926df663SMatthew Barth     return Properties<T, U>(std::forward<U>(handler));
226926df663SMatthew Barth }
227926df663SMatthew Barth 
228926df663SMatthew Barth /**
229*469d1366SMatthew Barth  * @struct Interfaces Added
230*469d1366SMatthew Barth  * @brief A match filter functor for Dbus interfaces added signals
231eb639c57SMatthew Barth  *
232eb639c57SMatthew Barth  * @tparam T - The type of the property value
233eb639c57SMatthew Barth  * @tparam U - The type of the handler
234eb639c57SMatthew Barth  */
235eb639c57SMatthew Barth template <typename T, typename U>
236*469d1366SMatthew Barth struct InterfacesAdded
237eb639c57SMatthew Barth {
238*469d1366SMatthew Barth     InterfacesAdded() = delete;
239*469d1366SMatthew Barth     ~InterfacesAdded() = default;
240*469d1366SMatthew Barth     InterfacesAdded(const InterfacesAdded&) = default;
241*469d1366SMatthew Barth     InterfacesAdded& operator=(const InterfacesAdded&) = default;
242*469d1366SMatthew Barth     InterfacesAdded(InterfacesAdded&&) = default;
243*469d1366SMatthew Barth     InterfacesAdded& operator=(InterfacesAdded&&) = default;
244*469d1366SMatthew Barth     InterfacesAdded(const char* path,
245*469d1366SMatthew Barth                     const char* intf,
246*469d1366SMatthew Barth                     const char* prop,
247eb639c57SMatthew Barth                     U&& handler) :
248eb639c57SMatthew Barth         _path(path),
249*469d1366SMatthew Barth         _intf(intf),
250*469d1366SMatthew Barth         _prop(prop),
251eb639c57SMatthew Barth         _handler(std::forward<U>(handler)) { }
252eb639c57SMatthew Barth 
253eb639c57SMatthew Barth     /** @brief Run signal handler function
254eb639c57SMatthew Barth      *
255eb639c57SMatthew Barth      * Extract the property from the InterfacesAdded
256eb639c57SMatthew Barth      * message and run the handler function.
257eb639c57SMatthew Barth      */
258eb639c57SMatthew Barth     void operator()(sdbusplus::bus::bus&,
259eb639c57SMatthew Barth                     sdbusplus::message::message& msg,
260eb639c57SMatthew Barth                     Zone& zone) const
261eb639c57SMatthew Barth     {
262336f18a5SMatthew Barth         if (msg)
263336f18a5SMatthew Barth         {
264eb639c57SMatthew Barth             std::map<std::string,
265eb639c57SMatthew Barth                      std::map<std::string,
266eb639c57SMatthew Barth                               sdbusplus::message::variant<T>>> intfProp;
267eb639c57SMatthew Barth             sdbusplus::message::object_path op;
268eb639c57SMatthew Barth 
269eb639c57SMatthew Barth             msg.read(op);
270d5cfdbe0SMatthew Barth             if (static_cast<const std::string&>(op) != _path)
271eb639c57SMatthew Barth             {
272eb639c57SMatthew Barth                 // Object path does not match this handler's path
273eb639c57SMatthew Barth                 return;
274eb639c57SMatthew Barth             }
275eb639c57SMatthew Barth 
276eb639c57SMatthew Barth             msg.read(intfProp);
277*469d1366SMatthew Barth             auto itIntf = intfProp.find(_intf);
278eb639c57SMatthew Barth             if (itIntf == intfProp.cend())
279eb639c57SMatthew Barth             {
280eb639c57SMatthew Barth                 // Interface not found on this handler's path
281eb639c57SMatthew Barth                 return;
282eb639c57SMatthew Barth             }
283*469d1366SMatthew Barth             auto itProp = itIntf->second.find(_prop);
284eb639c57SMatthew Barth             if (itProp == itIntf->second.cend())
285eb639c57SMatthew Barth             {
286eb639c57SMatthew Barth                 // Property not found on this handler's path
287eb639c57SMatthew Barth                 return;
288eb639c57SMatthew Barth             }
289eb639c57SMatthew Barth 
290*469d1366SMatthew Barth             _handler(zone, _path, _intf, _prop, std::forward<T>(
2914978e06cSWilliam A. Kennington III                 sdbusplus::message::variant_ns::get<T>(itProp->second)));
292eb639c57SMatthew Barth         }
293336f18a5SMatthew Barth     }
294eb639c57SMatthew Barth 
295eb639c57SMatthew Barth private:
296eb639c57SMatthew Barth     const char* _path;
297*469d1366SMatthew Barth     const char* _intf;
298*469d1366SMatthew Barth     const char* _prop;
299eb639c57SMatthew Barth     U _handler;
300eb639c57SMatthew Barth };
301eb639c57SMatthew Barth 
302eb639c57SMatthew Barth /**
303926df663SMatthew Barth  * @brief Used to process a Dbus interfaces added signal event
304eb639c57SMatthew Barth  *
305eb639c57SMatthew Barth  * @param[in] path - Object path
306*469d1366SMatthew Barth  * @param[in] intf - Object interface
307*469d1366SMatthew Barth  * @param[in] prop - Object property
308eb639c57SMatthew Barth  * @param[in] handler - Handler function to perform
309eb639c57SMatthew Barth  *
310eb639c57SMatthew Barth  * @tparam T - The type of the property
311eb639c57SMatthew Barth  * @tparam U - The type of the handler
312eb639c57SMatthew Barth  */
313eb639c57SMatthew Barth template <typename T, typename U>
314926df663SMatthew Barth auto interfacesAdded(const char* path,
315*469d1366SMatthew Barth                      const char* intf,
316*469d1366SMatthew Barth                      const char* prop,
317eb639c57SMatthew Barth                      U&& handler)
318eb639c57SMatthew Barth {
319*469d1366SMatthew Barth     return InterfacesAdded<T, U>(path,
320*469d1366SMatthew Barth                                  intf,
321*469d1366SMatthew Barth                                  prop,
322eb639c57SMatthew Barth                                  std::forward<U>(handler));
323eb639c57SMatthew Barth }
324eb639c57SMatthew Barth 
3258fa02dabSMatthew Barth /**
326*469d1366SMatthew Barth  * @struct Interfaces Removed
327*469d1366SMatthew Barth  * @brief A match filter functor for Dbus interfaces removed signals
3281499a5c3SMatthew Barth  *
3291499a5c3SMatthew Barth  * @tparam U - The type of the handler
3301499a5c3SMatthew Barth  */
3311499a5c3SMatthew Barth template <typename U>
332*469d1366SMatthew Barth struct InterfacesRemoved
3331499a5c3SMatthew Barth {
334*469d1366SMatthew Barth     InterfacesRemoved() = delete;
335*469d1366SMatthew Barth     ~InterfacesRemoved() = default;
336*469d1366SMatthew Barth     InterfacesRemoved(const InterfacesRemoved&) = default;
337*469d1366SMatthew Barth     InterfacesRemoved& operator=(const InterfacesRemoved&) = default;
338*469d1366SMatthew Barth     InterfacesRemoved(InterfacesRemoved&&) = default;
339*469d1366SMatthew Barth     InterfacesRemoved& operator=(InterfacesRemoved&&) = default;
340*469d1366SMatthew Barth     InterfacesRemoved(const char* path,
341*469d1366SMatthew Barth                       const char* intf,
3421499a5c3SMatthew Barth                       U&& handler) :
3431499a5c3SMatthew Barth         _path(path),
344*469d1366SMatthew Barth         _intf(intf),
3451499a5c3SMatthew Barth         _handler(std::forward<U>(handler)) { }
3461499a5c3SMatthew Barth 
3471499a5c3SMatthew Barth     /** @brief Run signal handler function
3481499a5c3SMatthew Barth      *
349*469d1366SMatthew Barth      * Extract the interfaces from the InterfacesRemoved
3501499a5c3SMatthew Barth      * message and run the handler function.
3511499a5c3SMatthew Barth      */
3521499a5c3SMatthew Barth     void operator()(sdbusplus::bus::bus&,
3531499a5c3SMatthew Barth                     sdbusplus::message::message& msg,
3541499a5c3SMatthew Barth                     Zone& zone) const
3551499a5c3SMatthew Barth     {
3561499a5c3SMatthew Barth         if (msg)
3571499a5c3SMatthew Barth         {
3581499a5c3SMatthew Barth             std::vector<std::string> intfs;
3591499a5c3SMatthew Barth             sdbusplus::message::object_path op;
3601499a5c3SMatthew Barth 
3611499a5c3SMatthew Barth             msg.read(op);
3621499a5c3SMatthew Barth             if (static_cast<const std::string&>(op) != _path)
3631499a5c3SMatthew Barth             {
3641499a5c3SMatthew Barth                 // Object path does not match this handler's path
3651499a5c3SMatthew Barth                 return;
3661499a5c3SMatthew Barth             }
3671499a5c3SMatthew Barth 
3681499a5c3SMatthew Barth             msg.read(intfs);
369*469d1366SMatthew Barth             auto itIntf = std::find(intfs.begin(), intfs.end(), _intf);
3701499a5c3SMatthew Barth             if (itIntf == intfs.cend())
3711499a5c3SMatthew Barth             {
3721499a5c3SMatthew Barth                 // Interface not found on this handler's path
3731499a5c3SMatthew Barth                 return;
3741499a5c3SMatthew Barth             }
3751499a5c3SMatthew Barth 
3761499a5c3SMatthew Barth             _handler(zone);
3771499a5c3SMatthew Barth         }
3781499a5c3SMatthew Barth     }
3791499a5c3SMatthew Barth 
3801499a5c3SMatthew Barth private:
3811499a5c3SMatthew Barth     const char* _path;
382*469d1366SMatthew Barth     const char* _intf;
3831499a5c3SMatthew Barth     U _handler;
3841499a5c3SMatthew Barth };
3851499a5c3SMatthew Barth 
3861499a5c3SMatthew Barth /**
387926df663SMatthew Barth  * @brief Used to process a Dbus interfaces removed signal event
3881499a5c3SMatthew Barth  *
3891499a5c3SMatthew Barth  * @param[in] path - Object path
390*469d1366SMatthew Barth  * @param[in] intf - Object interface
3911499a5c3SMatthew Barth  * @param[in] handler - Handler function to perform
3921499a5c3SMatthew Barth  *
3931499a5c3SMatthew Barth  * @tparam U - The type of the handler
3941499a5c3SMatthew Barth  */
3951499a5c3SMatthew Barth template <typename U>
396926df663SMatthew Barth auto interfacesRemoved(const char* path,
397*469d1366SMatthew Barth                        const char* intf,
3981499a5c3SMatthew Barth                        U&& handler)
3991499a5c3SMatthew Barth {
400*469d1366SMatthew Barth     return InterfacesRemoved<U>(path,
401*469d1366SMatthew Barth                                 intf,
4021499a5c3SMatthew Barth                                 std::forward<U>(handler));
4031499a5c3SMatthew Barth }
4041499a5c3SMatthew Barth 
4051499a5c3SMatthew Barth /**
406926df663SMatthew Barth  * @struct Name Owner
407926df663SMatthew Barth  * @brief A functor for Dbus name owner signals and methods
4088fa02dabSMatthew Barth  *
4098fa02dabSMatthew Barth  * @tparam U - The type of the handler
4108fa02dabSMatthew Barth  */
4118fa02dabSMatthew Barth template <typename U>
412926df663SMatthew Barth struct NameOwner
4138fa02dabSMatthew Barth {
414926df663SMatthew Barth     NameOwner() = delete;
415926df663SMatthew Barth     ~NameOwner() = default;
416926df663SMatthew Barth     NameOwner(const NameOwner&) = default;
417926df663SMatthew Barth     NameOwner& operator=(const NameOwner&) = default;
418926df663SMatthew Barth     NameOwner(NameOwner&&) = default;
419926df663SMatthew Barth     NameOwner& operator=(NameOwner&&) = default;
420926df663SMatthew Barth     explicit NameOwner(U&& handler) :
4218fa02dabSMatthew Barth         _handler(std::forward<U>(handler)) { }
4228fa02dabSMatthew Barth 
4238fa02dabSMatthew Barth     /** @brief Run signal handler function
4248fa02dabSMatthew Barth      *
4258fa02dabSMatthew Barth      * Extract the name owner from the NameOwnerChanged
426926df663SMatthew Barth      * message and run the handler function.
4278fa02dabSMatthew Barth      */
4288fa02dabSMatthew Barth     void operator()(sdbusplus::bus::bus& bus,
4298fa02dabSMatthew Barth                     sdbusplus::message::message& msg,
4308fa02dabSMatthew Barth                     Zone& zone) const
4318fa02dabSMatthew Barth     {
4325a302576SMatthew Barth         std::string name;
4335a302576SMatthew Barth         bool hasOwner = false;
4348fa02dabSMatthew Barth         if (msg)
4358fa02dabSMatthew Barth         {
4365a302576SMatthew Barth             // Handle NameOwnerChanged signals
4375a302576SMatthew Barth             msg.read(name);
4385a302576SMatthew Barth 
4395a302576SMatthew Barth             std::string oldOwn;
4405a302576SMatthew Barth             msg.read(oldOwn);
4415a302576SMatthew Barth 
4425a302576SMatthew Barth             std::string newOwn;
4435a302576SMatthew Barth             msg.read(newOwn);
4445a302576SMatthew Barth             if (!newOwn.empty())
4455a302576SMatthew Barth             {
4465a302576SMatthew Barth                 hasOwner = true;
4475a302576SMatthew Barth             }
448926df663SMatthew Barth             _handler(zone, name, hasOwner);
4498fa02dabSMatthew Barth         }
450926df663SMatthew Barth     }
451926df663SMatthew Barth 
452926df663SMatthew Barth     void operator()(Zone& zone,
453926df663SMatthew Barth                     const Group& group) const
4548fa02dabSMatthew Barth     {
455926df663SMatthew Barth         std::string name = "";
456926df663SMatthew Barth         bool hasOwner = false;
457926df663SMatthew Barth         std::for_each(
458926df663SMatthew Barth             group.begin(),
459926df663SMatthew Barth             group.end(),
460926df663SMatthew Barth             [&zone, &group, &name, &hasOwner, handler = std::move(_handler)](
461926df663SMatthew Barth                 auto const& member)
462926df663SMatthew Barth             {
463926df663SMatthew Barth                 auto path = std::get<pathPos>(member);
464926df663SMatthew Barth                 auto intf = std::get<intfPos>(member);
4655a302576SMatthew Barth                 try
4665a302576SMatthew Barth                 {
467926df663SMatthew Barth                     auto servName = zone.getService(path, intf);
468926df663SMatthew Barth                     if (name != servName)
469926df663SMatthew Barth                     {
470926df663SMatthew Barth                         name = servName;
4715a302576SMatthew Barth                         hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
472926df663SMatthew Barth                                 zone.getBus(),
4735a302576SMatthew Barth                                 "org.freedesktop.DBus",
4745a302576SMatthew Barth                                 "/org/freedesktop/DBus",
4755a302576SMatthew Barth                                 "org.freedesktop.DBus",
4765a302576SMatthew Barth                                 "NameHasOwner",
4775a302576SMatthew Barth                                 name);
478926df663SMatthew Barth                         // Update service name owner state list of a group
479926df663SMatthew Barth                         handler(zone, name, hasOwner);
480926df663SMatthew Barth                     }
4818fa02dabSMatthew Barth                 }
482ba7b5feaSMatt Spinler                 catch (const util::DBusMethodError& e)
4835a302576SMatthew Barth                 {
4845a302576SMatthew Barth                     // Failed to get service name owner state
485926df663SMatthew Barth                     name = "";
4865a302576SMatthew Barth                     hasOwner = false;
4875a302576SMatthew Barth                 }
488926df663SMatthew Barth             }
489926df663SMatthew Barth         );
4908fa02dabSMatthew Barth     }
4918fa02dabSMatthew Barth 
4928fa02dabSMatthew Barth private:
4938fa02dabSMatthew Barth     U _handler;
4948fa02dabSMatthew Barth };
4958fa02dabSMatthew Barth 
4968fa02dabSMatthew Barth /**
4978fa02dabSMatthew Barth  * @brief Used to process a Dbus name owner changed signal event
4988fa02dabSMatthew Barth  *
4998fa02dabSMatthew Barth  * @param[in] handler - Handler function to perform
5008fa02dabSMatthew Barth  *
5018fa02dabSMatthew Barth  * @tparam U - The type of the handler
5028fa02dabSMatthew Barth  *
5038fa02dabSMatthew Barth  * @return - The NameOwnerChanged signal struct
5048fa02dabSMatthew Barth  */
5058fa02dabSMatthew Barth template <typename U>
506926df663SMatthew Barth auto nameOwnerChanged(U&& handler)
5078fa02dabSMatthew Barth {
508926df663SMatthew Barth     return NameOwner<U>(std::forward<U>(handler));
509926df663SMatthew Barth }
510926df663SMatthew Barth 
511926df663SMatthew Barth /**
512926df663SMatthew Barth  * @brief Used to process the init of a name owner event
513926df663SMatthew Barth  *
514926df663SMatthew Barth  * @param[in] handler - Handler function to perform
515926df663SMatthew Barth  *
516926df663SMatthew Barth  * @tparam U - The type of the handler
517926df663SMatthew Barth  *
518926df663SMatthew Barth  * @return - The NameOwnerChanged signal struct
519926df663SMatthew Barth  */
520926df663SMatthew Barth template <typename U>
521926df663SMatthew Barth auto nameHasOwner(U&& handler)
522926df663SMatthew Barth {
523926df663SMatthew Barth     return NameOwner<U>(std::forward<U>(handler));
5248fa02dabSMatthew Barth }
5258fa02dabSMatthew Barth 
52638a93a8aSMatthew Barth } // namespace control
52738a93a8aSMatthew Barth } // namespace fan
52838a93a8aSMatthew Barth } // namespace phosphor
529