blob: 25db5a9ae8535885600d87d108793c205ac42335 [file] [log] [blame]
Matthew Barthb280bfa2017-09-15 09:56:50 -05001#include "actions.hpp"
2
3namespace phosphor
4{
5namespace fan
6{
7namespace control
8{
9namespace action
10{
11
12using namespace phosphor::fan;
13
William A. Kennington III122b8432018-10-30 18:39:21 -070014Action call_actions_based_on_timer(TimerConf&& tConf,
15 std::vector<Action>&& actions)
Matthew Barth2a646c52017-10-05 17:04:11 -050016{
Matthew Barth3e1bb272020-05-26 11:09:21 -050017 return [tConf = std::move(tConf), actions = std::move(actions)](
18 control::Zone& zone, const Group& group) {
Matthew Barth2a646c52017-10-05 17:04:11 -050019 try
20 {
Matthew Barthd7b716a2018-11-16 13:37:57 -060021 auto it = zone.getTimerEvents().find(__func__);
22 if (it != zone.getTimerEvents().end())
Matthew Barth2a646c52017-10-05 17:04:11 -050023 {
Matthew Barthd7b716a2018-11-16 13:37:57 -060024 auto& timers = it->second;
25 auto timerIter = zone.findTimer(group, actions, timers);
Matthew Barthe7d53892018-12-12 10:59:57 -060026 if (timerIter == timers.end())
Matthew Barth2a646c52017-10-05 17:04:11 -050027 {
Matthew Barthd7b716a2018-11-16 13:37:57 -060028 // No timer exists yet for action, add timer
29 zone.addTimer(__func__, group, actions, tConf);
Matthew Barth2a646c52017-10-05 17:04:11 -050030 }
Matthew Barthe7d53892018-12-12 10:59:57 -060031 else if (timerIter != timers.end())
Matthew Barthd7b716a2018-11-16 13:37:57 -060032 {
33 // Remove any timer for this group
34 timers.erase(timerIter);
35 if (timers.empty())
36 {
37 zone.getTimerEvents().erase(it);
38 }
39 }
40 }
Matthew Barthe7d53892018-12-12 10:59:57 -060041 else
Matthew Barthd7b716a2018-11-16 13:37:57 -060042 {
43 // No timer exists yet for event, add timer
44 zone.addTimer(__func__, group, actions, tConf);
Matthew Barth2a646c52017-10-05 17:04:11 -050045 }
46 }
47 catch (const std::out_of_range& oore)
48 {
49 // Group not found, no timers set
50 }
51 };
52}
53
Matthew Barth98726c42017-10-17 10:35:20 -050054void default_floor_on_missing_owner(Zone& zone, const Group& group)
55{
Matthew Barth480787c2017-11-06 11:00:00 -060056 // Set/update the services of the group
57 zone.setServices(&group);
Matthew Barth98726c42017-10-17 10:35:20 -050058 auto services = zone.getGroupServices(&group);
Matthew Barth3e1bb272020-05-26 11:09:21 -050059 auto defFloor =
60 std::any_of(services.begin(), services.end(),
61 [](const auto& s) { return !std::get<hasOwnerPos>(s); });
Matthew Barth98726c42017-10-17 10:35:20 -050062 if (defFloor)
63 {
64 zone.setFloor(zone.getDefFloor());
65 }
66 // Update fan control floor change allowed
67 zone.setFloorChangeAllow(&group, !defFloor);
68}
69
Matthew Barth0decd1b2017-10-24 15:58:17 -050070Action set_speed_on_missing_owner(uint64_t speed)
71{
Matthew Barth3e1bb272020-05-26 11:09:21 -050072 return [speed](control::Zone& zone, const Group& group) {
Matthew Barth480787c2017-11-06 11:00:00 -060073 // Set/update the services of the group
74 zone.setServices(&group);
Matthew Barth0decd1b2017-10-24 15:58:17 -050075 auto services = zone.getGroupServices(&group);
Patrick Williams5e15c3b2023-10-20 11:18:11 -050076 auto missingOwner = std::any_of(
77 services.begin(), services.end(),
78 [](const auto& s) { return !std::get<hasOwnerPos>(s); });
Matthew Barth0decd1b2017-10-24 15:58:17 -050079 if (missingOwner)
80 {
81 zone.setSpeed(speed);
82 }
83 // Update group's fan control active allowed based on action results
84 zone.setActiveAllow(&group, !missingOwner);
85 };
86}
87
Matthew Barth3e1bb272020-05-26 11:09:21 -050088void set_request_speed_base_with_max(control::Zone& zone, const Group& group)
Matthew Barthb280bfa2017-09-15 09:56:50 -050089{
90 int64_t base = 0;
Patrick Williams61b73292023-05-10 07:50:12 -050091 std::for_each(group.begin(), group.end(),
92 [&zone, &base](const auto& entry) {
93 try
94 {
95 auto value = zone.template getPropertyValue<int64_t>(
96 std::get<pathPos>(entry), std::get<intfPos>(entry),
97 std::get<propPos>(entry));
98 base = std::max(base, value);
99 }
100 catch (const std::out_of_range& oore)
101 {
102 // Property value not found, base request speed unchanged
103 }
104 });
Matthew Barthb280bfa2017-09-15 09:56:50 -0500105 // A request speed base of 0 defaults to the current target speed
106 zone.setRequestSpeedBase(base);
107}
108
Matthew Barthb280bfa2017-09-15 09:56:50 -0500109} // namespace action
110} // namespace control
111} // namespace fan
112} // namespace phosphor