blob: 0096debb51b3a279b5e722e31c1cf47d442e1034 [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 Williams5e15c3b2023-10-20 11:18:11 -050016 return [tConf = std::move(tConf)](
17 control::Zone& zone, const std::string& name, const Group& group,
18 const std::vector<Action>& actions) {
Matthew Barth3e1bb272020-05-26 11:09:21 -050019 zone.addTimer(name, group, actions, tConf);
Patrick Williams5e15c3b2023-10-20 11:18:11 -050020 };
Matthew Barth1b4de262018-03-06 13:03:16 -060021}
22
Matthew Barth926df662018-10-09 09:51:12 -050023Trigger signal(const std::string& match, SignalHandler&& handler)
Matthew Barth016bd242018-03-07 16:06:06 -060024{
Matthew Barth3e1bb272020-05-26 11:09:21 -050025 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) {
Matthew Barth016bd242018-03-07 16:06:06 -060028 // Setup signal matches of the property for event
29 std::unique_ptr<EventData> eventData =
Matthew Barth3e1bb272020-05-26 11:09:21 -050030 std::make_unique<EventData>(group, match, handler, actions);
Patrick Williams3ea9ec22021-11-19 12:21:08 -060031 std::unique_ptr<sdbusplus::bus::match_t> mPtr = nullptr;
Matthew Barth016bd242018-03-07 16:06:06 -060032 if (!match.empty())
33 {
34 // Subscribe to signal match
Patrick Williams3ea9ec22021-11-19 12:21:08 -060035 mPtr = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050036 zone.getBus(), match.c_str(),
37 std::bind(std::mem_fn(&Zone::handleEvent), &zone,
38 std::placeholders::_1, eventData.get()));
Matthew Barth016bd242018-03-07 16:06:06 -060039 }
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 {
Matthew Barth146b7392018-03-08 16:17:58 -060046 if (std::get<pathPos>(entry) == zone.getPath())
Matthew Barth016bd242018-03-07 16:06:06 -060047 {
48 auto ifaces = zone.getIfaces();
49 // Group member interface in list owned by zone
50 if (std::find(ifaces.begin(), ifaces.end(),
Matthew Barth3e1bb272020-05-26 11:09:21 -050051 std::get<intfPos>(entry)) != ifaces.end())
Matthew Barth016bd242018-03-07 16:06:06 -060052 {
53 // Store path,interface,property as a managed object
Matthew Barth3e1bb272020-05-26 11:09:21 -050054 zone.setObjectData(
55 std::get<pathPos>(entry), std::get<intfPos>(entry),
56 std::get<propPos>(entry), eventData.get());
Matthew Barth016bd242018-03-07 16:06:06 -060057 }
58 }
59 }
60 }
Matthew Barth79cb8312018-11-14 15:20:31 -060061 zone.addSignal(name, std::move(eventData), std::move(mPtr));
Matthew Barth016bd242018-03-07 16:06:06 -060062 };
63}
64
Matthew Barth926df662018-10-09 09:51:12 -050065Trigger init(MethodHandler&& handler)
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060066{
Matthew Barth3e1bb272020-05-26 11:09:21 -050067 return [handler = std::move(handler)](
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040068 control::Zone& zone, const std::string& /*name*/,
69 const Group& group, const std::vector<Action>& actions) {
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060070 // A handler function is optional
71 if (handler)
72 {
Matthew Barth926df662018-10-09 09:51:12 -050073 handler(zone, group);
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060074 }
Matthew Barth926df662018-10-09 09:51:12 -050075
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060076 // Run action functions for initial event state
Patrick Williamsdfddd642024-08-16 15:21:51 -040077 std::for_each(actions.begin(), actions.end(),
78 [&zone, &group](const auto& action) {
79 action(zone, group);
80 });
Matthew Barthcd3bfbc2018-03-07 16:26:03 -060081 };
82}
83
Matthew Barth1b4de262018-03-06 13:03:16 -060084} // namespace trigger
85} // namespace control
86} // namespace fan
87} // namespace phosphor