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