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