xref: /openbmc/phosphor-fan-presence/control/functor.hpp (revision 9f93bd35bd65e0ca6ee65b375175dc6e2705b7d8)
138a93a8aSMatthew Barth #pragma once
238a93a8aSMatthew Barth 
3336f18a5SMatthew Barth #include "sdbusplus.hpp"
43e1bb274SMatthew Barth #include "types.hpp"
53e1bb274SMatthew Barth 
638a93a8aSMatthew Barth #include <phosphor-logging/log.hpp>
738a93a8aSMatthew Barth 
838a93a8aSMatthew Barth namespace phosphor
938a93a8aSMatthew Barth {
1038a93a8aSMatthew Barth namespace fan
1138a93a8aSMatthew Barth {
1238a93a8aSMatthew Barth namespace control
1338a93a8aSMatthew Barth {
1438a93a8aSMatthew Barth class Zone;
1538a93a8aSMatthew Barth 
16336f18a5SMatthew Barth using namespace phosphor::fan;
175a302576SMatthew Barth using namespace sdbusplus::bus::match;
1838a93a8aSMatthew Barth using namespace phosphor::logging;
1938a93a8aSMatthew Barth 
2038a93a8aSMatthew Barth /**
211b3e9602SMatthew Barth  * @brief Create a zone handler function object
221b3e9602SMatthew Barth  *
231b3e9602SMatthew Barth  * @param[in] handler - The handler being created
241b3e9602SMatthew Barth  *
251b3e9602SMatthew Barth  * @return - The created zone handler function object
261b3e9602SMatthew Barth  */
271b3e9602SMatthew Barth template <typename T>
281b3e9602SMatthew Barth auto make_zoneHandler(T&& handler)
291b3e9602SMatthew Barth {
301b3e9602SMatthew Barth     return ZoneHandler(std::forward<T>(handler));
311b3e9602SMatthew Barth }
321b3e9602SMatthew Barth 
331b3e9602SMatthew Barth /**
341b4de26aSMatthew Barth  * @brief Create a trigger function object
351b4de26aSMatthew Barth  *
361b4de26aSMatthew Barth  * @param[in] trigger - The trigger being created
371b4de26aSMatthew Barth  *
381b4de26aSMatthew Barth  * @return - The created trigger function object
391b4de26aSMatthew Barth  */
401b4de26aSMatthew Barth template <typename T>
411b4de26aSMatthew Barth auto make_trigger(T&& trigger)
421b4de26aSMatthew Barth {
431b4de26aSMatthew Barth     return Trigger(std::forward<T>(trigger));
441b4de26aSMatthew Barth }
451b4de26aSMatthew Barth 
461b4de26aSMatthew Barth /**
4738a93a8aSMatthew Barth  * @brief Create a handler function object
4838a93a8aSMatthew Barth  *
4938a93a8aSMatthew Barth  * @param[in] handler - The handler being created
5038a93a8aSMatthew Barth  *
5138a93a8aSMatthew Barth  * @return - The created handler function object
5238a93a8aSMatthew Barth  */
53926df663SMatthew Barth template <typename T, typename U>
54926df663SMatthew Barth auto make_handler(U&& handler)
5538a93a8aSMatthew Barth {
56926df663SMatthew Barth     return T(std::forward<U>(handler));
5738a93a8aSMatthew Barth }
5838a93a8aSMatthew Barth 
5938a93a8aSMatthew Barth /**
6017d1fe23SMatthew Barth  * @brief Create an action function object
6117d1fe23SMatthew Barth  *
6217d1fe23SMatthew Barth  * @param[in] action - The action being created
6317d1fe23SMatthew Barth  *
6417d1fe23SMatthew Barth  * @return - The created action function object
6517d1fe23SMatthew Barth  */
6617d1fe23SMatthew Barth template <typename T>
6717d1fe23SMatthew Barth auto make_action(T&& action)
6817d1fe23SMatthew Barth {
6917d1fe23SMatthew Barth     return Action(std::forward<T>(action));
7017d1fe23SMatthew Barth }
7117d1fe23SMatthew Barth 
7217d1fe23SMatthew Barth /**
73926df663SMatthew Barth  * @struct Properties
74926df663SMatthew Barth  * @brief A set of match filter functors for Dbus property values. Each
75926df663SMatthew Barth  * functor provides an associated process for retrieving the value
76926df663SMatthew Barth  * for a given property and providing it to the given handler function.
7738a93a8aSMatthew Barth  *
7838a93a8aSMatthew Barth  * @tparam T - The type of the property value
7938a93a8aSMatthew Barth  * @tparam U - The type of the handler
8038a93a8aSMatthew Barth  */
8138a93a8aSMatthew Barth template <typename T, typename U>
82926df663SMatthew Barth struct Properties
8338a93a8aSMatthew Barth {
84926df663SMatthew Barth     Properties() = delete;
85926df663SMatthew Barth     ~Properties() = default;
86926df663SMatthew Barth     Properties(const Properties&) = default;
87926df663SMatthew Barth     Properties& operator=(const Properties&) = default;
88926df663SMatthew Barth     Properties(Properties&&) = default;
89926df663SMatthew Barth     Properties& operator=(Properties&&) = default;
90*9f93bd35SMatthew Barth     explicit Properties(U&& handler) :
91*9f93bd35SMatthew Barth         _path(""), _intf(""), _prop(""), _handler(std::forward<U>(handler))
923e1bb274SMatthew Barth     {}
933e1bb274SMatthew Barth     Properties(const char* path, const char* intf, const char* prop,
9438a93a8aSMatthew Barth                U&& handler) :
95336f18a5SMatthew Barth         _path(path),
963e1bb274SMatthew Barth         _intf(intf), _prop(prop), _handler(std::forward<U>(handler))
973e1bb274SMatthew Barth     {}
9838a93a8aSMatthew Barth 
9938a93a8aSMatthew Barth     /** @brief Run signal handler function
10038a93a8aSMatthew Barth      *
10138a93a8aSMatthew Barth      * Extract the property from the PropertiesChanged
102926df663SMatthew Barth      * message and run the handler function.
10338a93a8aSMatthew Barth      */
1043e1bb274SMatthew Barth     void operator()(sdbusplus::bus::bus& bus, sdbusplus::message::message& msg,
10538a93a8aSMatthew Barth                     Zone& zone) const
10638a93a8aSMatthew Barth     {
107336f18a5SMatthew Barth         if (msg)
108336f18a5SMatthew Barth         {
109469d1366SMatthew Barth             std::string intf;
110469d1366SMatthew Barth             msg.read(intf);
111469d1366SMatthew Barth             if (intf != _intf)
11238a93a8aSMatthew Barth             {
113469d1366SMatthew Barth                 // Interface name does not match on object
11438a93a8aSMatthew Barth                 return;
11538a93a8aSMatthew Barth             }
11638a93a8aSMatthew Barth 
1177f4c548dSMatthew Barth             std::map<std::string, PropertyVariantType> props;
118469d1366SMatthew Barth             msg.read(props);
119469d1366SMatthew Barth             auto it = props.find(_prop);
120469d1366SMatthew Barth             if (it == props.cend())
12138a93a8aSMatthew Barth             {
122469d1366SMatthew Barth                 // Property not included in dictionary of properties changed
12338a93a8aSMatthew Barth                 return;
12438a93a8aSMatthew Barth             }
12538a93a8aSMatthew Barth 
1267f4c548dSMatthew Barth             // Retrieve the property's value applying any visitors necessary
1277f4c548dSMatthew Barth             auto value =
1287f4c548dSMatthew Barth                 zone.getPropertyValueVisitor<T>(_intf, _prop, it->second);
1297f4c548dSMatthew Barth 
1307f4c548dSMatthew Barth             _handler(zone, _path, _intf, _prop, std::forward<T>(value));
13138a93a8aSMatthew Barth         }
132336f18a5SMatthew Barth         else
133336f18a5SMatthew Barth         {
134336f18a5SMatthew Barth             try
135336f18a5SMatthew Barth             {
136469d1366SMatthew Barth                 auto val = zone.getPropertyByName<T>(_path, _intf, _prop);
137469d1366SMatthew 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(
1603e1bb274SMatthew Barth             group.begin(), group.end(),
1613e1bb274SMatthew Barth             [&zone, handler = std::move(_handler)](auto const& member) {
162926df663SMatthew Barth                 auto path = std::get<pathPos>(member);
163926df663SMatthew Barth                 auto intf = std::get<intfPos>(member);
164926df663SMatthew Barth                 auto prop = std::get<propPos>(member);
165926df663SMatthew Barth                 try
166926df663SMatthew Barth                 {
167926df663SMatthew Barth                     auto val = zone.getPropertyByName<T>(path, intf, prop);
168469d1366SMatthew Barth                     handler(zone, path, intf, prop, std::forward<T>(val));
169926df663SMatthew Barth                 }
170926df663SMatthew Barth                 catch (const sdbusplus::exception::SdBusError&)
171926df663SMatthew Barth                 {
172926df663SMatthew Barth                     // Property value not sent to handler
173926df663SMatthew Barth                 }
174926df663SMatthew Barth                 catch (const util::DBusError&)
175926df663SMatthew Barth                 {
176926df663SMatthew Barth                     // Property value not sent to handler
177926df663SMatthew Barth                 }
1783e1bb274SMatthew Barth             });
179926df663SMatthew Barth     }
180926df663SMatthew Barth 
18138a93a8aSMatthew Barth   private:
182336f18a5SMatthew Barth     const char* _path;
183469d1366SMatthew Barth     const char* _intf;
184469d1366SMatthew Barth     const char* _prop;
18538a93a8aSMatthew Barth     U _handler;
18638a93a8aSMatthew Barth };
18738a93a8aSMatthew Barth 
18838a93a8aSMatthew Barth /**
189469d1366SMatthew Barth  * @brief Used to process a Dbus properties changed signal event
19038a93a8aSMatthew Barth  *
191336f18a5SMatthew Barth  * @param[in] path - Object path
192469d1366SMatthew Barth  * @param[in] intf - Object interface
193469d1366SMatthew Barth  * @param[in] prop - Object property
19438a93a8aSMatthew Barth  * @param[in] handler - Handler function to perform
19538a93a8aSMatthew Barth  *
19638a93a8aSMatthew Barth  * @tparam T - The type of the property
19738a93a8aSMatthew Barth  * @tparam U - The type of the handler
19838a93a8aSMatthew Barth  */
19938a93a8aSMatthew Barth template <typename T, typename U>
2003e1bb274SMatthew Barth auto propertiesChanged(const char* path, const char* intf, const char* prop,
20138a93a8aSMatthew Barth                        U&& handler)
20238a93a8aSMatthew Barth {
2033e1bb274SMatthew Barth     return Properties<T, U>(path, intf, prop, std::forward<U>(handler));
20438a93a8aSMatthew Barth }
20538a93a8aSMatthew Barth 
206eb639c57SMatthew Barth /**
207469d1366SMatthew Barth  * @brief Used to get the properties of an event's group
208926df663SMatthew Barth  *
209926df663SMatthew Barth  * @param[in] handler - Handler function to perform
210926df663SMatthew Barth  *
211469d1366SMatthew Barth  * @tparam T - The type of all the properties
212926df663SMatthew Barth  * @tparam U - The type of the handler
213926df663SMatthew Barth  */
214926df663SMatthew Barth template <typename T, typename U>
215469d1366SMatthew Barth auto getProperties(U&& handler)
216926df663SMatthew Barth {
217926df663SMatthew Barth     return Properties<T, U>(std::forward<U>(handler));
218926df663SMatthew Barth }
219926df663SMatthew Barth 
220926df663SMatthew Barth /**
221469d1366SMatthew Barth  * @struct Interfaces Added
222469d1366SMatthew Barth  * @brief A match filter functor for Dbus interfaces added signals
223eb639c57SMatthew Barth  *
224eb639c57SMatthew Barth  * @tparam T - The type of the property value
225eb639c57SMatthew Barth  * @tparam U - The type of the handler
226eb639c57SMatthew Barth  */
227eb639c57SMatthew Barth template <typename T, typename U>
228469d1366SMatthew Barth struct InterfacesAdded
229eb639c57SMatthew Barth {
230469d1366SMatthew Barth     InterfacesAdded() = delete;
231469d1366SMatthew Barth     ~InterfacesAdded() = default;
232469d1366SMatthew Barth     InterfacesAdded(const InterfacesAdded&) = default;
233469d1366SMatthew Barth     InterfacesAdded& operator=(const InterfacesAdded&) = default;
234469d1366SMatthew Barth     InterfacesAdded(InterfacesAdded&&) = default;
235469d1366SMatthew Barth     InterfacesAdded& operator=(InterfacesAdded&&) = default;
2363e1bb274SMatthew Barth     InterfacesAdded(const char* path, const char* intf, const char* prop,
237eb639c57SMatthew Barth                     U&& handler) :
238eb639c57SMatthew Barth         _path(path),
2393e1bb274SMatthew Barth         _intf(intf), _prop(prop), _handler(std::forward<U>(handler))
2403e1bb274SMatthew Barth     {}
241eb639c57SMatthew Barth 
242eb639c57SMatthew Barth     /** @brief Run signal handler function
243eb639c57SMatthew Barth      *
244eb639c57SMatthew Barth      * Extract the property from the InterfacesAdded
245eb639c57SMatthew Barth      * message and run the handler function.
246eb639c57SMatthew Barth      */
2473e1bb274SMatthew Barth     void operator()(sdbusplus::bus::bus&, sdbusplus::message::message& msg,
248eb639c57SMatthew Barth                     Zone& zone) const
249eb639c57SMatthew Barth     {
250336f18a5SMatthew Barth         if (msg)
251336f18a5SMatthew Barth         {
252eb639c57SMatthew Barth             sdbusplus::message::object_path op;
253eb639c57SMatthew Barth 
254eb639c57SMatthew Barth             msg.read(op);
255d5cfdbe0SMatthew Barth             if (static_cast<const std::string&>(op) != _path)
256eb639c57SMatthew Barth             {
257eb639c57SMatthew Barth                 // Object path does not match this handler's path
258eb639c57SMatthew Barth                 return;
259eb639c57SMatthew Barth             }
260eb639c57SMatthew Barth 
2613e1bb274SMatthew Barth             std::map<std::string, std::map<std::string, PropertyVariantType>>
2623e1bb274SMatthew Barth                 intfProp;
263eb639c57SMatthew Barth             msg.read(intfProp);
264469d1366SMatthew Barth             auto itIntf = intfProp.find(_intf);
265eb639c57SMatthew Barth             if (itIntf == intfProp.cend())
266eb639c57SMatthew Barth             {
267eb639c57SMatthew Barth                 // Interface not found on this handler's path
268eb639c57SMatthew Barth                 return;
269eb639c57SMatthew Barth             }
270469d1366SMatthew Barth             auto itProp = itIntf->second.find(_prop);
271eb639c57SMatthew Barth             if (itProp == itIntf->second.cend())
272eb639c57SMatthew Barth             {
273eb639c57SMatthew Barth                 // Property not found on this handler's path
274eb639c57SMatthew Barth                 return;
275eb639c57SMatthew Barth             }
276eb639c57SMatthew Barth 
2777f4c548dSMatthew Barth             // Retrieve the property's value applying any visitors necessary
2787f4c548dSMatthew Barth             auto value =
2797f4c548dSMatthew Barth                 zone.getPropertyValueVisitor<T>(_intf, _prop, itProp->second);
2807f4c548dSMatthew Barth 
2817f4c548dSMatthew Barth             _handler(zone, _path, _intf, _prop, std::forward<T>(value));
282eb639c57SMatthew Barth         }
283336f18a5SMatthew Barth     }
284eb639c57SMatthew Barth 
285eb639c57SMatthew Barth   private:
286eb639c57SMatthew Barth     const char* _path;
287469d1366SMatthew Barth     const char* _intf;
288469d1366SMatthew Barth     const char* _prop;
289eb639c57SMatthew Barth     U _handler;
290eb639c57SMatthew Barth };
291eb639c57SMatthew Barth 
292eb639c57SMatthew Barth /**
293926df663SMatthew Barth  * @brief Used to process a Dbus interfaces added signal event
294eb639c57SMatthew Barth  *
295eb639c57SMatthew Barth  * @param[in] path - Object path
296469d1366SMatthew Barth  * @param[in] intf - Object interface
297469d1366SMatthew Barth  * @param[in] prop - Object property
298eb639c57SMatthew Barth  * @param[in] handler - Handler function to perform
299eb639c57SMatthew Barth  *
300eb639c57SMatthew Barth  * @tparam T - The type of the property
301eb639c57SMatthew Barth  * @tparam U - The type of the handler
302eb639c57SMatthew Barth  */
303eb639c57SMatthew Barth template <typename T, typename U>
3043e1bb274SMatthew Barth auto interfacesAdded(const char* path, const char* intf, const char* prop,
305eb639c57SMatthew Barth                      U&& handler)
306eb639c57SMatthew Barth {
3073e1bb274SMatthew Barth     return InterfacesAdded<T, U>(path, intf, prop, std::forward<U>(handler));
308eb639c57SMatthew Barth }
309eb639c57SMatthew Barth 
3108fa02dabSMatthew Barth /**
311469d1366SMatthew Barth  * @struct Interfaces Removed
312469d1366SMatthew Barth  * @brief A match filter functor for Dbus interfaces removed signals
3131499a5c3SMatthew Barth  *
3141499a5c3SMatthew Barth  * @tparam U - The type of the handler
3151499a5c3SMatthew Barth  */
3161499a5c3SMatthew Barth template <typename U>
317469d1366SMatthew Barth struct InterfacesRemoved
3181499a5c3SMatthew Barth {
319469d1366SMatthew Barth     InterfacesRemoved() = delete;
320469d1366SMatthew Barth     ~InterfacesRemoved() = default;
321469d1366SMatthew Barth     InterfacesRemoved(const InterfacesRemoved&) = default;
322469d1366SMatthew Barth     InterfacesRemoved& operator=(const InterfacesRemoved&) = default;
323469d1366SMatthew Barth     InterfacesRemoved(InterfacesRemoved&&) = default;
324469d1366SMatthew Barth     InterfacesRemoved& operator=(InterfacesRemoved&&) = default;
3253e1bb274SMatthew Barth     InterfacesRemoved(const char* path, const char* intf, U&& handler) :
3263e1bb274SMatthew Barth         _path(path), _intf(intf), _handler(std::forward<U>(handler))
3273e1bb274SMatthew Barth     {}
3281499a5c3SMatthew Barth 
3291499a5c3SMatthew Barth     /** @brief Run signal handler function
3301499a5c3SMatthew Barth      *
331469d1366SMatthew Barth      * Extract the interfaces from the InterfacesRemoved
3321499a5c3SMatthew Barth      * message and run the handler function.
3331499a5c3SMatthew Barth      */
3343e1bb274SMatthew Barth     void operator()(sdbusplus::bus::bus&, sdbusplus::message::message& msg,
3351499a5c3SMatthew Barth                     Zone& zone) const
3361499a5c3SMatthew Barth     {
3371499a5c3SMatthew Barth         if (msg)
3381499a5c3SMatthew Barth         {
3391499a5c3SMatthew Barth             std::vector<std::string> intfs;
3401499a5c3SMatthew Barth             sdbusplus::message::object_path op;
3411499a5c3SMatthew Barth 
3421499a5c3SMatthew Barth             msg.read(op);
3431499a5c3SMatthew Barth             if (static_cast<const std::string&>(op) != _path)
3441499a5c3SMatthew Barth             {
3451499a5c3SMatthew Barth                 // Object path does not match this handler's path
3461499a5c3SMatthew Barth                 return;
3471499a5c3SMatthew Barth             }
3481499a5c3SMatthew Barth 
3491499a5c3SMatthew Barth             msg.read(intfs);
350469d1366SMatthew Barth             auto itIntf = std::find(intfs.begin(), intfs.end(), _intf);
3511499a5c3SMatthew Barth             if (itIntf == intfs.cend())
3521499a5c3SMatthew Barth             {
3531499a5c3SMatthew Barth                 // Interface not found on this handler's path
3541499a5c3SMatthew Barth                 return;
3551499a5c3SMatthew Barth             }
3561499a5c3SMatthew Barth 
3571499a5c3SMatthew Barth             _handler(zone);
3581499a5c3SMatthew Barth         }
3591499a5c3SMatthew Barth     }
3601499a5c3SMatthew Barth 
3611499a5c3SMatthew Barth   private:
3621499a5c3SMatthew Barth     const char* _path;
363469d1366SMatthew Barth     const char* _intf;
3641499a5c3SMatthew Barth     U _handler;
3651499a5c3SMatthew Barth };
3661499a5c3SMatthew Barth 
3671499a5c3SMatthew Barth /**
368926df663SMatthew Barth  * @brief Used to process a Dbus interfaces removed signal event
3691499a5c3SMatthew Barth  *
3701499a5c3SMatthew Barth  * @param[in] path - Object path
371469d1366SMatthew Barth  * @param[in] intf - Object interface
3721499a5c3SMatthew Barth  * @param[in] handler - Handler function to perform
3731499a5c3SMatthew Barth  *
3741499a5c3SMatthew Barth  * @tparam U - The type of the handler
3751499a5c3SMatthew Barth  */
3761499a5c3SMatthew Barth template <typename U>
3773e1bb274SMatthew Barth auto interfacesRemoved(const char* path, const char* intf, U&& handler)
3781499a5c3SMatthew Barth {
3793e1bb274SMatthew Barth     return InterfacesRemoved<U>(path, intf, std::forward<U>(handler));
3801499a5c3SMatthew Barth }
3811499a5c3SMatthew Barth 
3821499a5c3SMatthew Barth /**
383926df663SMatthew Barth  * @struct Name Owner
384926df663SMatthew Barth  * @brief A functor for Dbus name owner signals and methods
3858fa02dabSMatthew Barth  *
3868fa02dabSMatthew Barth  * @tparam U - The type of the handler
3878fa02dabSMatthew Barth  */
3888fa02dabSMatthew Barth template <typename U>
389926df663SMatthew Barth struct NameOwner
3908fa02dabSMatthew Barth {
391926df663SMatthew Barth     NameOwner() = delete;
392926df663SMatthew Barth     ~NameOwner() = default;
393926df663SMatthew Barth     NameOwner(const NameOwner&) = default;
394926df663SMatthew Barth     NameOwner& operator=(const NameOwner&) = default;
395926df663SMatthew Barth     NameOwner(NameOwner&&) = default;
396926df663SMatthew Barth     NameOwner& operator=(NameOwner&&) = default;
3973e1bb274SMatthew Barth     explicit NameOwner(U&& handler) : _handler(std::forward<U>(handler))
3983e1bb274SMatthew Barth     {}
3998fa02dabSMatthew Barth 
4008fa02dabSMatthew Barth     /** @brief Run signal handler function
4018fa02dabSMatthew Barth      *
4028fa02dabSMatthew Barth      * Extract the name owner from the NameOwnerChanged
403926df663SMatthew Barth      * message and run the handler function.
4048fa02dabSMatthew Barth      */
4053e1bb274SMatthew Barth     void operator()(sdbusplus::bus::bus& bus, sdbusplus::message::message& msg,
4068fa02dabSMatthew Barth                     Zone& zone) const
4078fa02dabSMatthew Barth     {
4088fa02dabSMatthew Barth         if (msg)
4098fa02dabSMatthew Barth         {
410*9f93bd35SMatthew Barth             std::string name;
411*9f93bd35SMatthew Barth             bool hasOwner = false;
412*9f93bd35SMatthew Barth 
4135a302576SMatthew Barth             // Handle NameOwnerChanged signals
4145a302576SMatthew Barth             msg.read(name);
4155a302576SMatthew Barth 
4165a302576SMatthew Barth             std::string oldOwn;
4175a302576SMatthew Barth             msg.read(oldOwn);
4185a302576SMatthew Barth 
4195a302576SMatthew Barth             std::string newOwn;
4205a302576SMatthew Barth             msg.read(newOwn);
4215a302576SMatthew Barth             if (!newOwn.empty())
4225a302576SMatthew Barth             {
4235a302576SMatthew Barth                 hasOwner = true;
4245a302576SMatthew Barth             }
425926df663SMatthew Barth             _handler(zone, name, hasOwner);
4268fa02dabSMatthew Barth         }
427926df663SMatthew Barth     }
428926df663SMatthew Barth 
4293e1bb274SMatthew Barth     void operator()(Zone& zone, const Group& group) const
4308fa02dabSMatthew Barth     {
431926df663SMatthew Barth         std::string name = "";
432926df663SMatthew Barth         bool hasOwner = false;
433926df663SMatthew Barth         std::for_each(
4343e1bb274SMatthew Barth             group.begin(), group.end(),
4353e1bb274SMatthew Barth             [&zone, &group, &name, &hasOwner,
4363e1bb274SMatthew Barth              handler = std::move(_handler)](auto const& member) {
437926df663SMatthew Barth                 auto path = std::get<pathPos>(member);
438926df663SMatthew Barth                 auto intf = std::get<intfPos>(member);
4395a302576SMatthew Barth                 try
4405a302576SMatthew Barth                 {
441926df663SMatthew Barth                     auto servName = zone.getService(path, intf);
442926df663SMatthew Barth                     if (name != servName)
443926df663SMatthew Barth                     {
444926df663SMatthew Barth                         name = servName;
4455a302576SMatthew Barth                         hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
4463e1bb274SMatthew Barth                             zone.getBus(), "org.freedesktop.DBus",
4473e1bb274SMatthew Barth                             "/org/freedesktop/DBus", "org.freedesktop.DBus",
4483e1bb274SMatthew Barth                             "NameHasOwner", name);
449926df663SMatthew Barth                         // Update service name owner state list of a group
450926df663SMatthew Barth                         handler(zone, name, hasOwner);
451926df663SMatthew Barth                     }
4528fa02dabSMatthew Barth                 }
453ba7b5feaSMatt Spinler                 catch (const util::DBusMethodError& e)
4545a302576SMatthew Barth                 {
4555a302576SMatthew Barth                     // Failed to get service name owner state
456926df663SMatthew Barth                     name = "";
4575a302576SMatthew Barth                     hasOwner = false;
4585a302576SMatthew Barth                 }
4593e1bb274SMatthew Barth             });
4608fa02dabSMatthew Barth     }
4618fa02dabSMatthew Barth 
4628fa02dabSMatthew Barth   private:
4638fa02dabSMatthew Barth     U _handler;
4648fa02dabSMatthew Barth };
4658fa02dabSMatthew Barth 
4668fa02dabSMatthew Barth /**
4678fa02dabSMatthew Barth  * @brief Used to process a Dbus name owner changed signal event
4688fa02dabSMatthew Barth  *
4698fa02dabSMatthew Barth  * @param[in] handler - Handler function to perform
4708fa02dabSMatthew Barth  *
4718fa02dabSMatthew Barth  * @tparam U - The type of the handler
4728fa02dabSMatthew Barth  *
4738fa02dabSMatthew Barth  * @return - The NameOwnerChanged signal struct
4748fa02dabSMatthew Barth  */
4758fa02dabSMatthew Barth template <typename U>
476926df663SMatthew Barth auto nameOwnerChanged(U&& handler)
4778fa02dabSMatthew Barth {
478926df663SMatthew Barth     return NameOwner<U>(std::forward<U>(handler));
479926df663SMatthew Barth }
480926df663SMatthew Barth 
481926df663SMatthew Barth /**
482926df663SMatthew Barth  * @brief Used to process the init of a name owner event
483926df663SMatthew Barth  *
484926df663SMatthew Barth  * @param[in] handler - Handler function to perform
485926df663SMatthew Barth  *
486926df663SMatthew Barth  * @tparam U - The type of the handler
487926df663SMatthew Barth  *
488926df663SMatthew Barth  * @return - The NameOwnerChanged signal struct
489926df663SMatthew Barth  */
490926df663SMatthew Barth template <typename U>
491926df663SMatthew Barth auto nameHasOwner(U&& handler)
492926df663SMatthew Barth {
493926df663SMatthew Barth     return NameOwner<U>(std::forward<U>(handler));
4948fa02dabSMatthew Barth }
4958fa02dabSMatthew Barth 
49638a93a8aSMatthew Barth } // namespace control
49738a93a8aSMatthew Barth } // namespace fan
49838a93a8aSMatthew Barth } // namespace phosphor
499