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