xref: /openbmc/phosphor-fan-presence/control/handlers.hpp (revision c7dfb135a0da732f4715befbc40783f01d5b284a)
1 #pragma once
2 
3 #include "types.hpp"
4 #include "zone.hpp"
5 
6 #include <utility>
7 
8 namespace phosphor
9 {
10 namespace fan
11 {
12 namespace control
13 {
14 namespace handler
15 {
16 
17 /**
18  * @brief A handler function to set/update a property on a zone
19  * @details Sets or updates a zone property to the given value using the
20  * provided zone dbus object's set property function
21  *
22  * @param[in] intf - Interface on zone object
23  * @param[in] prop - Property on interface
24  * @param[in] func - Zone set property function pointer
25  * @param[in] value - Value to set property to
26  * @param[in] persist - Persist property value or not
27  *
28  * @return Lambda function
29  *     A lambda function to set/update the zone property
30  */
31 template <typename T>
setZoneProperty(const char * intf,const char * prop,T (Zone::* func)(T),T && value,bool persist)32 auto setZoneProperty(const char* intf, const char* prop, T (Zone::*func)(T),
33                      T&& value, bool persist)
34 {
35     return [=, value = std::forward<T>(value)](auto& zone) {
36         (zone.*func)(value);
37         if (persist)
38         {
39             zone.setPersisted(intf, prop);
40         }
41     };
42 }
43 
44 /**
45  * @brief A handler function to set/update a property
46  * @details Sets or updates a property's value determined by a combination of
47  * an object's path, interface, and property names
48  *
49  * @param[in] path - Object's path name
50  * @param[in] interface - Object's interface name
51  * @param[in] property - Object's property name
52  *
53  * @return Lambda function
54  *     A lambda function to set/update the property value
55  */
56 template <typename T>
setProperty()57 auto setProperty()
58 {
59     return [](auto& zone, auto& path, auto& intf, auto& prop, T&& arg) {
60         zone.setPropertyValue(path, intf, prop, std::forward<T>(arg));
61     };
62 }
63 
64 /**
65  * @brief A handler function to set/update service name owner state
66  * @details Sets or updates service name owner state used by a group where
67  * a service name without an owner represents the service no longer exists
68  *
69  * @param[in] group - Group associated with a service
70  *
71  * @return Lambda function
72  *     A lambda function to set/update the service name owner state
73  */
setService(Group && group)74 inline auto setService(Group&& group)
75 {
76     return [group = std::move(group)](auto& zone, auto& name, bool hasOwner) {
77         // Update service name owner state list of a group
78         zone.setServiceOwner(&group, name, hasOwner);
79     };
80 }
81 
82 /**
83  * @brief A handler function to remove an interface from an object path
84  * @details Removes an interface from an object's path which includes removing
85  * all properties that would be under that interface
86  *
87  * @param[in] path - Object's path name
88  * @param[in] interface - Object's interface name
89  *
90  * @return Lambda function
91  *     A lambda function to remove the interface
92  */
removeInterface(const char * path,const char * interface)93 inline auto removeInterface(const char* path, const char* interface)
94 {
95     return [=](auto& zone) { zone.removeObjectInterface(path, interface); };
96 }
97 
98 } // namespace handler
99 } // namespace control
100 } // namespace fan
101 } // namespace phosphor
102