blob: 0df2a4ab6c002f1c5ef33bcb4fb8d7721a1d6f17 [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 {
Matthew Barthd7b716a2018-11-16 13:37:57 -060021 zone.addTimer(name,
22 group,
Matthew Barth1b4de262018-03-06 13:03:16 -060023 actions,
24 tConf);
25 };
26}
27
Matthew Barth926df662018-10-09 09:51:12 -050028Trigger signal(const std::string& match, SignalHandler&& handler)
Matthew Barth016bd242018-03-07 16:06:06 -060029{
30 return [match = std::move(match),
31 handler = std::move(handler)](control::Zone& zone,
Matthew Barth2885ba92018-11-14 15:03:05 -060032 const std::string& name,
Matthew Barth016bd242018-03-07 16:06:06 -060033 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 {
Matthew Barth146b7392018-03-08 16:17:58 -060063 if (std::get<pathPos>(entry) == zone.getPath())
Matthew Barth016bd242018-03-07 16:06:06 -060064 {
65 auto ifaces = zone.getIfaces();
66 // Group member interface in list owned by zone
67 if (std::find(ifaces.begin(), ifaces.end(),
Matthew Barth146b7392018-03-08 16:17:58 -060068 std::get<intfPos>(entry)) != ifaces.end())
Matthew Barth016bd242018-03-07 16:06:06 -060069 {
70 // Store path,interface,property as a managed object
Matthew Barth146b7392018-03-08 16:17:58 -060071 zone.setObjectData(std::get<pathPos>(entry),
72 std::get<intfPos>(entry),
73 std::get<propPos>(entry),
Matthew Barth016bd242018-03-07 16:06:06 -060074 eventData.get());
75 }
76 }
77 }
78 }
Matthew Barth79cb8312018-11-14 15:20:31 -060079 zone.addSignal(name, std::move(eventData), std::move(mPtr));
Matthew Barth016bd242018-03-07 16:06:06 -060080 };
81}
82
Matthew Barth926df662018-10-09 09:51:12 -050083Trigger init(MethodHandler&& handler)
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060084{
85 return [handler = std::move(handler)](control::Zone& zone,
Matthew Barth2885ba92018-11-14 15:03:05 -060086 const std::string& name,
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060087 const Group& group,
88 const std::vector<Action>& actions)
89 {
90 // A handler function is optional
91 if (handler)
92 {
Matthew Barth926df662018-10-09 09:51:12 -050093 handler(zone, group);
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060094 }
Matthew Barth926df662018-10-09 09:51:12 -050095
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060096 // 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
Matthew Barth1b4de262018-03-06 13:03:16 -0600108} // namespace trigger
109} // namespace control
110} // namespace fan
111} // namespace phosphor