blob: 5662f3fe5954747e1b49bb8b0fccac5569a2c534 [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,
17 const Group& group,
18 const std::vector<Action>& actions)
19 {
20 zone.addTimer(group,
21 actions,
22 tConf);
23 };
24}
25
Matthew Barth926df662018-10-09 09:51:12 -050026Trigger signal(const std::string& match, SignalHandler&& handler)
Matthew Barth016bd242018-03-07 16:06:06 -060027{
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 {
Matthew Barth146b7392018-03-08 16:17:58 -060060 if (std::get<pathPos>(entry) == zone.getPath())
Matthew Barth016bd242018-03-07 16:06:06 -060061 {
62 auto ifaces = zone.getIfaces();
63 // Group member interface in list owned by zone
64 if (std::find(ifaces.begin(), ifaces.end(),
Matthew Barth146b7392018-03-08 16:17:58 -060065 std::get<intfPos>(entry)) != ifaces.end())
Matthew Barth016bd242018-03-07 16:06:06 -060066 {
67 // Store path,interface,property as a managed object
Matthew Barth146b7392018-03-08 16:17:58 -060068 zone.setObjectData(std::get<pathPos>(entry),
69 std::get<intfPos>(entry),
70 std::get<propPos>(entry),
Matthew Barth016bd242018-03-07 16:06:06 -060071 eventData.get());
72 }
73 }
74 }
75 }
76 zone.addSignal(std::move(eventData), std::move(mPtr));
77 };
78}
79
Matthew Barth926df662018-10-09 09:51:12 -050080Trigger init(MethodHandler&& handler)
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060081{
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 {
Matthew Barth926df662018-10-09 09:51:12 -050089 handler(zone, group);
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060090 }
Matthew Barth926df662018-10-09 09:51:12 -050091
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060092 // 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
Matthew Barth1b4de262018-03-06 13:03:16 -0600104} // namespace trigger
105} // namespace control
106} // namespace fan
107} // namespace phosphor