blob: 410732db351b9f42276cc178e5fa56aaf0ffdfe6 [file] [log] [blame]
Matthew Barth1b4de262018-03-06 13:03:16 -06001#include "triggers.hpp"
2
3namespace phosphor
4{
5namespace fan
6{
7namespace control
8{
9namespace trigger
10{
11
12using namespace phosphor::fan;
13
14Trigger timer(TimerConf&& tConf)
15{
16 return [tConf = std::move(tConf)](control::Zone& zone,
Matthew Barth2885ba92018-11-14 15:03:05 -060017 const std::string& name,
Matthew Barth1b4de262018-03-06 13:03:16 -060018 const Group& group,
19 const std::vector<Action>& actions)
20 {
21 zone.addTimer(group,
22 actions,
23 tConf);
24 };
25}
26
Matthew Barth926df662018-10-09 09:51:12 -050027Trigger signal(const std::string& match, SignalHandler&& handler)
Matthew Barth016bd242018-03-07 16:06:06 -060028{
29 return [match = std::move(match),
30 handler = std::move(handler)](control::Zone& zone,
Matthew Barth2885ba92018-11-14 15:03:05 -060031 const std::string& name,
Matthew Barth016bd242018-03-07 16:06:06 -060032 const Group& group,
33 const std::vector<Action>& actions)
34 {
35 // Setup signal matches of the property for event
36 std::unique_ptr<EventData> eventData =
37 std::make_unique<EventData>(
38 group,
39 match,
40 handler,
41 actions
42 );
43 std::unique_ptr<sdbusplus::server::match::match> mPtr = nullptr;
44 if (!match.empty())
45 {
46 // Subscribe to signal match
47 mPtr = std::make_unique<sdbusplus::server::match::match>(
48 zone.getBus(),
49 match.c_str(),
50 std::bind(std::mem_fn(&Zone::handleEvent),
51 &zone,
52 std::placeholders::_1,
53 eventData.get())
54 );
55 }
56 else
57 {
58 // When match is empty, handle if zone object member
59 // Set event data for each host group member
60 for (auto& entry : group)
61 {
Matthew Barth146b7392018-03-08 16:17:58 -060062 if (std::get<pathPos>(entry) == zone.getPath())
Matthew Barth016bd242018-03-07 16:06:06 -060063 {
64 auto ifaces = zone.getIfaces();
65 // Group member interface in list owned by zone
66 if (std::find(ifaces.begin(), ifaces.end(),
Matthew Barth146b7392018-03-08 16:17:58 -060067 std::get<intfPos>(entry)) != ifaces.end())
Matthew Barth016bd242018-03-07 16:06:06 -060068 {
69 // Store path,interface,property as a managed object
Matthew Barth146b7392018-03-08 16:17:58 -060070 zone.setObjectData(std::get<pathPos>(entry),
71 std::get<intfPos>(entry),
72 std::get<propPos>(entry),
Matthew Barth016bd242018-03-07 16:06:06 -060073 eventData.get());
74 }
75 }
76 }
77 }
78 zone.addSignal(std::move(eventData), std::move(mPtr));
79 };
80}
81
Matthew Barth926df662018-10-09 09:51:12 -050082Trigger init(MethodHandler&& handler)
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060083{
84 return [handler = std::move(handler)](control::Zone& zone,
Matthew Barth2885ba92018-11-14 15:03:05 -060085 const std::string& name,
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060086 const Group& group,
87 const std::vector<Action>& actions)
88 {
89 // A handler function is optional
90 if (handler)
91 {
Matthew Barth926df662018-10-09 09:51:12 -050092 handler(zone, group);
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060093 }
Matthew Barth926df662018-10-09 09:51:12 -050094
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060095 // Run action functions for initial event state
96 std::for_each(
97 actions.begin(),
98 actions.end(),
99 [&zone, &group](auto const& action)
100 {
101 action(zone, group);
102 }
103 );
104 };
105}
106
Matthew Barth1b4de262018-03-06 13:03:16 -0600107} // namespace trigger
108} // namespace control
109} // namespace fan
110} // namespace phosphor