1 #include "triggers.hpp"
2 
3 namespace phosphor
4 {
5 namespace fan
6 {
7 namespace control
8 {
9 namespace trigger
10 {
11 
12 using namespace phosphor::fan;
13 
14 Trigger timer(TimerConf&& tConf)
15 {
16     return [tConf = std::move(tConf)](control::Zone& zone,
17                                       const std::string& name,
18                                       const Group& group,
19                                       const std::vector<Action>& actions)
20     {
21         zone.addTimer(name,
22                       group,
23                       actions,
24                       tConf);
25     };
26 }
27 
28 Trigger signal(const std::string& match, SignalHandler&& handler)
29 {
30     return [match = std::move(match),
31             handler = std::move(handler)](control::Zone& zone,
32                                           const std::string& name,
33                                           const Group& group,
34                                           const std::vector<Action>& actions)
35     {
36         // Setup signal matches of the property for event
37         std::unique_ptr<EventData> eventData =
38             std::make_unique<EventData>(
39                     group,
40                     match,
41                     handler,
42                     actions
43             );
44         std::unique_ptr<sdbusplus::server::match::match> mPtr = nullptr;
45         if (!match.empty())
46         {
47             // Subscribe to signal match
48             mPtr = std::make_unique<sdbusplus::server::match::match>(
49                     zone.getBus(),
50                     match.c_str(),
51                     std::bind(std::mem_fn(&Zone::handleEvent),
52                               &zone,
53                               std::placeholders::_1,
54                               eventData.get())
55             );
56         }
57         else
58         {
59             // When match is empty, handle if zone object member
60             // Set event data for each host group member
61             for (auto& entry : group)
62             {
63                 if (std::get<pathPos>(entry) == zone.getPath())
64                 {
65                     auto ifaces = zone.getIfaces();
66                     // Group member interface in list owned by zone
67                     if (std::find(ifaces.begin(), ifaces.end(),
68                         std::get<intfPos>(entry)) != ifaces.end())
69                     {
70                         // Store path,interface,property as a managed object
71                         zone.setObjectData(std::get<pathPos>(entry),
72                                            std::get<intfPos>(entry),
73                                            std::get<propPos>(entry),
74                                            eventData.get());
75                     }
76                 }
77             }
78         }
79         zone.addSignal(name, std::move(eventData), std::move(mPtr));
80     };
81 }
82 
83 Trigger init(MethodHandler&& handler)
84 {
85     return [handler = std::move(handler)](control::Zone& zone,
86                                           const std::string& name,
87                                           const Group& group,
88                                           const std::vector<Action>& actions)
89     {
90         // A handler function is optional
91         if (handler)
92         {
93             handler(zone, group);
94         }
95 
96         // Run action functions for initial event state
97         std::for_each(
98             actions.begin(),
99             actions.end(),
100             [&zone, &group](auto const& action)
101             {
102                 action(zone, group);
103             }
104         );
105     };
106 }
107 
108 } // namespace trigger
109 } // namespace control
110 } // namespace fan
111 } // namespace phosphor
112