blob: eb2580ff2a72c8912034b9497f1d14473f3e9267 [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{
Patrick Williams61b73292023-05-10 07:50:12 -050016 return
17 [tConf = std::move(tConf)](control::Zone& zone, const std::string& name,
18 const Group& group,
19 const std::vector<Action>& actions) {
Matthew Barth3e1bb272020-05-26 11:09:21 -050020 zone.addTimer(name, group, actions, tConf);
Patrick Williams61b73292023-05-10 07:50:12 -050021 };
Matthew Barth1b4de262018-03-06 13:03:16 -060022}
23
Matthew Barth926df662018-10-09 09:51:12 -050024Trigger signal(const std::string& match, SignalHandler&& handler)
Matthew Barth016bd242018-03-07 16:06:06 -060025{
Matthew Barth3e1bb272020-05-26 11:09:21 -050026 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) {
Matthew Barth016bd242018-03-07 16:06:06 -060029 // Setup signal matches of the property for event
30 std::unique_ptr<EventData> eventData =
Matthew Barth3e1bb272020-05-26 11:09:21 -050031 std::make_unique<EventData>(group, match, handler, actions);
Patrick Williams3ea9ec22021-11-19 12:21:08 -060032 std::unique_ptr<sdbusplus::bus::match_t> mPtr = nullptr;
Matthew Barth016bd242018-03-07 16:06:06 -060033 if (!match.empty())
34 {
35 // Subscribe to signal match
Patrick Williams3ea9ec22021-11-19 12:21:08 -060036 mPtr = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050037 zone.getBus(), match.c_str(),
38 std::bind(std::mem_fn(&Zone::handleEvent), &zone,
39 std::placeholders::_1, eventData.get()));
Matthew Barth016bd242018-03-07 16:06:06 -060040 }
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 {
Matthew Barth146b7392018-03-08 16:17:58 -060047 if (std::get<pathPos>(entry) == zone.getPath())
Matthew Barth016bd242018-03-07 16:06:06 -060048 {
49 auto ifaces = zone.getIfaces();
50 // Group member interface in list owned by zone
51 if (std::find(ifaces.begin(), ifaces.end(),
Matthew Barth3e1bb272020-05-26 11:09:21 -050052 std::get<intfPos>(entry)) != ifaces.end())
Matthew Barth016bd242018-03-07 16:06:06 -060053 {
54 // Store path,interface,property as a managed object
Matthew Barth3e1bb272020-05-26 11:09:21 -050055 zone.setObjectData(
56 std::get<pathPos>(entry), std::get<intfPos>(entry),
57 std::get<propPos>(entry), eventData.get());
Matthew Barth016bd242018-03-07 16:06:06 -060058 }
59 }
60 }
61 }
Matthew Barth79cb8312018-11-14 15:20:31 -060062 zone.addSignal(name, std::move(eventData), std::move(mPtr));
Matthew Barth016bd242018-03-07 16:06:06 -060063 };
64}
65
Matthew Barth926df662018-10-09 09:51:12 -050066Trigger init(MethodHandler&& handler)
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060067{
Matthew Barth3e1bb272020-05-26 11:09:21 -050068 return [handler = std::move(handler)](
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040069 control::Zone& zone, const std::string& /*name*/,
70 const Group& group, const std::vector<Action>& actions) {
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060071 // A handler function is optional
72 if (handler)
73 {
Matthew Barth926df662018-10-09 09:51:12 -050074 handler(zone, group);
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060075 }
Matthew Barth926df662018-10-09 09:51:12 -050076
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060077 // Run action functions for initial event state
Patrick Williams61b73292023-05-10 07:50:12 -050078 std::for_each(actions.begin(), actions.end(),
79 [&zone, &group](auto const& action) {
80 action(zone, group);
81 });
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060082 };
83}
84
Matthew Barth1b4de262018-03-06 13:03:16 -060085} // namespace trigger
86} // namespace control
87} // namespace fan
88} // namespace phosphor