blob: fd9ad622f0cc2d2bc3fcbde6f29f666f59fd2ef3 [file] [log] [blame]
Matthew Barth17d1fe22017-05-11 15:00:36 -05001#pragma once
2
Matthew Barth3e1bb272020-05-26 11:09:21 -05003#include "types.hpp"
4#include "utility.hpp"
5#include "zone.hpp"
6
Matthew Barth17d1fe22017-05-11 15:00:36 -05007#include <algorithm>
Matthew Barth4af419c2017-06-12 13:39:31 -05008#include <numeric>
Matthew Barth17d1fe22017-05-11 15:00:36 -05009
10namespace phosphor
11{
12namespace fan
13{
14namespace control
15{
16namespace action
17{
18
19/**
Matthew Barth2a646c52017-10-05 17:04:11 -050020 * @brief An action that wraps a list of actions with a timer
21 * @details Sets up a list of actions to be invoked when the defined timer
22 * expires (or for each expiration of a repeating timer).
23 *
24 * @param[in] tConf - Timer configuration parameters
25 * @param[in] action - List of actions to be called when the timer expires
26 *
27 * @return Action lambda function
28 * An Action function that creates a timer
29 */
Matthew Barth3e1bb272020-05-26 11:09:21 -050030Action call_actions_based_on_timer(TimerConf&& tConf,
31 std::vector<Action>&& actions);
Matthew Barth2a646c52017-10-05 17:04:11 -050032
33/**
Matthew Barth98726c42017-10-17 10:35:20 -050034 * @brief An action that sets the floor to the default fan floor speed
35 * @details Sets the fan floor to the defined default fan floor speed when a
36 * service associated to the given group has terminated. Once all services
37 * are functional and providing the sensors again, the fan floor is allowed
38 * to be set normally.
39 *
40 * @param[in] zone - Zone containing fans
41 * @param[in] group - Group of sensors to determine services' states
42 */
43void default_floor_on_missing_owner(Zone& zone, const Group& group);
44
45/**
Matthew Barth0decd1b2017-10-24 15:58:17 -050046 * @brief An action to set a speed when a service owner is missing
47 * @details Sets the fans to the given speed when any service owner associated
48 * to the group is missing. Once all services are functional and providing
49 * the event data again, active fan speed changes are allowed.
50 *
51 * @param[in] speed - Speed to set the zone to
52 *
53 * @return Action lambda function
54 * An Action function that sets the zone to the given speed if any service
55 * owners are missing.
56 */
57Action set_speed_on_missing_owner(uint64_t speed);
58
59/**
Matthew Barthb280bfa2017-09-15 09:56:50 -050060 * @brief An action to set the request speed base
61 * @details A new target speed is determined using a speed delta being added
62 * or subtracted, for increases or decrease respectively, from a base speed.
63 * This base speed defaults to be the current target speed or is set to a
64 * different base speed(i.e. the fans' tach feedback speed) to request a new
65 * target from.
66 *
67 * @param[in] zone - Zone containing fans
68 * @param[in] group - Group of sensors to determine base from
69 */
70void set_request_speed_base_with_max(Zone& zone, const Group& group);
71
72/**
Matthew Barth17d1fe22017-05-11 15:00:36 -050073 * @brief An action to set the speed on a zone
Matthew Barth861d77c2017-05-22 14:18:25 -050074 * @details The zone is held at the given speed when a defined number of
Matthew Barth17d1fe22017-05-11 15:00:36 -050075 * properties in the group are set to the given state
76 *
77 * @param[in] count - Number of properties
78 * @param[in] state - Value the property(s) needed to be set at
79 * @param[in] speed - Speed to set the zone to
80 *
81 * @return Lambda function
82 * A lambda function to set the zone speed when the number of properties
83 * within the group are at a certain value
84 */
Matthew Barth9e741ed2017-06-02 16:29:09 -050085template <typename T>
86auto count_state_before_speed(size_t count, T&& state, uint64_t speed)
Matthew Barth17d1fe22017-05-11 15:00:36 -050087{
Patrick Williamsdfddd642024-08-16 15:21:51 -040088 return [count, speed,
89 state = std::forward<T>(state)](auto& zone, auto& group) {
Matthew Barthe0f67c82018-05-08 15:47:12 -050090 size_t numAtState = 0;
91 for (auto& entry : group)
Matthew Barth17d1fe22017-05-11 15:00:36 -050092 {
Matthew Barthe0f67c82018-05-08 15:47:12 -050093 try
94 {
95 if (zone.template getPropertyValue<T>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050096 std::get<pathPos>(entry), std::get<intfPos>(entry),
Matthew Barth146b7392018-03-08 16:17:58 -060097 std::get<propPos>(entry)) == state)
Matthew Barthe0f67c82018-05-08 15:47:12 -050098 {
99 numAtState++;
100 }
101 }
102 catch (const std::out_of_range& oore)
103 {
104 // Default to property not equal when not found
105 }
106 if (numAtState >= count)
107 {
108 zone.setSpeed(speed);
109 break;
110 }
Matthew Barth17d1fe22017-05-11 15:00:36 -0500111 }
Matthew Barth60b00762017-08-15 13:39:06 -0500112 // Update group's fan control active allowed based on action results
113 zone.setActiveAllow(&group, !(numAtState >= count));
Matthew Barth17d1fe22017-05-11 15:00:36 -0500114 };
115}
116
Matthew Barth4af419c2017-06-12 13:39:31 -0500117/**
118 * @brief An action to set the floor speed on a zone
119 * @details Based on the average of the defined sensor group values, the floor
120 * speed is selected from the first map key entry that the average sensor value
121 * is less than.
122 *
123 * @param[in] val_to_speed - Ordered map of sensor value-to-speed
124 *
Matthew Barthb280bfa2017-09-15 09:56:50 -0500125 * @return Action lambda function
126 * An Action function to set the zone's floor speed when the average of
Matthew Barth4af419c2017-06-12 13:39:31 -0500127 * property values within the group is below the lowest sensor value given
128 */
Matthew Barth9ca60652020-02-10 13:01:08 -0600129template <typename T>
Matthew Barth3e1bb272020-05-26 11:09:21 -0500130Action set_floor_from_average_sensor_value(std::map<T, uint64_t>&& val_to_speed)
Matthew Barth9ca60652020-02-10 13:01:08 -0600131{
132 return [val_to_speed = std::move(val_to_speed)](control::Zone& zone,
Matthew Barth3e1bb272020-05-26 11:09:21 -0500133 const Group& group) {
Matthew Barth9ca60652020-02-10 13:01:08 -0600134 auto speed = zone.getDefFloor();
135 if (group.size() != 0)
136 {
137 auto count = 0;
Patrick Williamsdfddd642024-08-16 15:21:51 -0400138 auto sumValue = std::accumulate(
139 group.begin(), group.end(), 0,
140 [&zone, &count](T sum, const auto& entry) {
141 try
142 {
143 return sum + zone.template getPropertyValue<T>(
144 std::get<pathPos>(entry),
145 std::get<intfPos>(entry),
146 std::get<propPos>(entry));
147 }
148 catch (const std::out_of_range& oore)
149 {
150 count++;
151 return sum;
152 }
153 });
Matthew Barth9ca60652020-02-10 13:01:08 -0600154 if ((group.size() - count) > 0)
155 {
156 auto groupSize = static_cast<int64_t>(group.size());
157 auto avgValue = sumValue / (groupSize - count);
Matthew Barth3e1bb272020-05-26 11:09:21 -0500158 auto it = std::find_if(val_to_speed.begin(), val_to_speed.end(),
Patrick Williamsdfddd642024-08-16 15:21:51 -0400159 [&avgValue](const auto& entry) {
160 return avgValue < entry.first;
161 });
Matthew Barth9ca60652020-02-10 13:01:08 -0600162 if (it != std::end(val_to_speed))
163 {
164 speed = (*it).second;
165 }
166 }
167 }
168 zone.setFloor(speed);
169 };
170}
Matthew Barth4af419c2017-06-12 13:39:31 -0500171
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500172/**
173 * @brief An action to set the ceiling speed on a zone
Matthew Barthb280bfa2017-09-15 09:56:50 -0500174 * @details Based on the average of the defined sensor group values, the
175 * ceiling speed is selected from the map key transition point that the average
176 * sensor value falls within depending on the key values direction from what
177 * was previously read.
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500178 *
179 * @param[in] val_to_speed - Ordered map of sensor value-to-speed transitions
180 *
Matthew Barthb280bfa2017-09-15 09:56:50 -0500181 * @return Action lambda function
182 * An Action function to set the zone's ceiling speed when the average of
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500183 * property values within the group is above(increasing) or
184 * below(decreasing) the key transition point
185 */
Matthew Barth9ca60652020-02-10 13:01:08 -0600186template <typename T>
Matthew Barth3e1bb272020-05-26 11:09:21 -0500187Action
188 set_ceiling_from_average_sensor_value(std::map<T, uint64_t>&& val_to_speed)
Matthew Barth9ca60652020-02-10 13:01:08 -0600189{
Patrick Williamsdfddd642024-08-16 15:21:51 -0400190 return [val_to_speed =
191 std::move(val_to_speed)](Zone& zone, const Group& group) {
Matthew Barth9ca60652020-02-10 13:01:08 -0600192 auto speed = zone.getCeiling();
193 if (group.size() != 0)
194 {
195 auto count = 0;
Patrick Williamsdfddd642024-08-16 15:21:51 -0400196 auto sumValue = std::accumulate(
197 group.begin(), group.end(), 0,
198 [&zone, &count](T sum, const auto& entry) {
199 try
200 {
201 return sum + zone.template getPropertyValue<T>(
202 std::get<pathPos>(entry),
203 std::get<intfPos>(entry),
204 std::get<propPos>(entry));
205 }
206 catch (const std::out_of_range& oore)
207 {
208 count++;
209 return sum;
210 }
211 });
Matthew Barth9ca60652020-02-10 13:01:08 -0600212 if ((group.size() - count) > 0)
213 {
214 auto groupSize = static_cast<int64_t>(group.size());
215 auto avgValue = sumValue / (groupSize - count);
216 auto prevValue = zone.swapCeilingKeyValue(avgValue);
217 if (avgValue != prevValue)
Patrick Williams61b73292023-05-10 07:50:12 -0500218 { // Only check if previous and new values differ
Matthew Barth9ca60652020-02-10 13:01:08 -0600219 if (avgValue < prevValue)
Matthew Barth3e1bb272020-05-26 11:09:21 -0500220 { // Value is decreasing from previous
Matthew Barth9ca60652020-02-10 13:01:08 -0600221 for (auto it = val_to_speed.rbegin();
Matthew Barth3e1bb272020-05-26 11:09:21 -0500222 it != val_to_speed.rend(); ++it)
Matthew Barth9ca60652020-02-10 13:01:08 -0600223 {
224 if (it == val_to_speed.rbegin() &&
225 avgValue >= it->first)
226 {
227 // Value is at/above last map key, set
228 // ceiling speed to the last map key's value
229 speed = it->second;
230 break;
231 }
232 else if (std::next(it, 1) == val_to_speed.rend() &&
233 avgValue <= it->first)
234 {
235 // Value is at/below first map key, set
236 // ceiling speed to the first map key's value
237 speed = it->second;
238 break;
239 }
Matthew Barth3e1bb272020-05-26 11:09:21 -0500240 if (avgValue < it->first && it->first <= prevValue)
Matthew Barth9ca60652020-02-10 13:01:08 -0600241 {
242 // Value decreased & transitioned across
243 // a map key, update ceiling speed to this
244 // map key's value when new value is below
245 // map's key and the key is at/below the
246 // previous value
247 speed = it->second;
248 }
249 }
250 }
251 else
Matthew Barth3e1bb272020-05-26 11:09:21 -0500252 { // Value is increasing from previous
Matthew Barth9ca60652020-02-10 13:01:08 -0600253 for (auto it = val_to_speed.begin();
Matthew Barth3e1bb272020-05-26 11:09:21 -0500254 it != val_to_speed.end(); ++it)
Matthew Barth9ca60652020-02-10 13:01:08 -0600255 {
256 if (it == val_to_speed.begin() &&
257 avgValue <= it->first)
258 {
259 // Value is at/below first map key, set
260 // ceiling speed to the first map key's value
261 speed = it->second;
262 break;
263 }
264 else if (std::next(it, 1) == val_to_speed.end() &&
265 avgValue >= it->first)
266 {
267 // Value is at/above last map key, set
268 // ceiling speed to the last map key's value
269 speed = it->second;
270 break;
271 }
Matthew Barth3e1bb272020-05-26 11:09:21 -0500272 if (avgValue > it->first && it->first >= prevValue)
Matthew Barth9ca60652020-02-10 13:01:08 -0600273 {
274 // Value increased & transitioned across
275 // a map key, update ceiling speed to this
276 // map key's value when new value is above
277 // map's key and the key is at/above the
278 // previous value
279 speed = it->second;
280 }
281 }
282 }
283 }
284 }
285 }
286 zone.setCeiling(speed);
287 };
288}
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500289
Matthew Barth24623522017-06-21 14:09:57 -0500290/**
291 * @brief An action to set the speed increase delta and request speed change
292 * @details Provides the ability to determine what the net increase delta the
293 * zone's fan speeds should be updated by from their current target speed and
294 * request that new target speed.
295 *
296 * @param[in] state - State to compare the group's property value to
Matthew Barth172f3932017-08-14 11:07:46 -0500297 * @param[in] factor - Factor to apply to the calculated net delta
Matthew Barth24623522017-06-21 14:09:57 -0500298 * @param[in] speedDelta - Speed delta of the group
299 *
300 * @return Lambda function
301 * A lambda function that determines the net increase delta and requests
302 * a new target speed with that increase for the zone.
303 */
304template <typename T>
Matthew Barth172f3932017-08-14 11:07:46 -0500305auto set_net_increase_speed(T&& state, T&& factor, uint64_t speedDelta)
Matthew Barth24623522017-06-21 14:09:57 -0500306{
Matthew Barth3e1bb272020-05-26 11:09:21 -0500307 return [speedDelta, factor = std::forward<T>(factor),
308 state = std::forward<T>(state)](auto& zone, auto& group) {
Matthew Barth24623522017-06-21 14:09:57 -0500309 auto netDelta = zone.getIncSpeedDelta();
Patrick Williamsdfddd642024-08-16 15:21:51 -0400310 std::for_each(
311 group.begin(), group.end(),
312 [&zone, &state, &factor, &speedDelta,
313 &netDelta](const auto& entry) {
314 try
Matthew Barth24623522017-06-21 14:09:57 -0500315 {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400316 T value = zone.template getPropertyValue<T>(
317 std::get<pathPos>(entry), std::get<intfPos>(entry),
318 std::get<propPos>(entry));
319 // TODO openbmc/phosphor-fan-presence#7 - Support possible
320 // state types for comparison
321 if (value >= state)
322 {
323 // Increase by at least a single delta(factor)
324 // to attempt bringing under 'state'
325 auto delta = std::max((value - state), factor);
326 // Increase is the factor applied to the
327 // difference times the given speed delta
328 netDelta = std::max(netDelta,
329 static_cast<uint64_t>(
330 (delta / factor) * speedDelta));
331 }
Matthew Barthbc651602017-08-10 16:59:43 -0500332 }
Patrick Williamsdfddd642024-08-16 15:21:51 -0400333 catch (const std::out_of_range& oore)
334 {
335 // Property value not found, netDelta unchanged
336 }
337 });
Matthew Barth240397b2017-06-22 11:23:30 -0500338 // Request speed change for target speed update
339 zone.requestSpeedIncrease(netDelta);
Matthew Barth24623522017-06-21 14:09:57 -0500340 };
341}
342
Matthew Barth0ce99d82017-06-22 15:07:29 -0500343/**
344 * @brief An action to set the speed decrease delta and request speed change
345 * @details Provides the ability to determine what the net decrease delta each
346 * zone's fan speeds should be updated by from their current target speed, and
347 * request that speed change occur on the next decrease interval.
348 *
349 * @param[in] state - State to compare the group's property value to
Matthew Barth172f3932017-08-14 11:07:46 -0500350 * @param[in] factor - Factor to apply to the calculated net delta
Matthew Barth0ce99d82017-06-22 15:07:29 -0500351 * @param[in] speedDelta - Speed delta of the group
352 *
353 * @return Lambda function
354 * A lambda function that determines the net decrease delta and requests
355 * a new target speed with that decrease for the zone.
356 */
357template <typename T>
Matthew Barth172f3932017-08-14 11:07:46 -0500358auto set_net_decrease_speed(T&& state, T&& factor, uint64_t speedDelta)
Matthew Barth0ce99d82017-06-22 15:07:29 -0500359{
Matthew Barth3e1bb272020-05-26 11:09:21 -0500360 return [speedDelta, factor = std::forward<T>(factor),
361 state = std::forward<T>(state)](auto& zone, auto& group) {
Matthew Barth0ce99d82017-06-22 15:07:29 -0500362 auto netDelta = zone.getDecSpeedDelta();
Matthew Barthe4338cd2017-12-14 11:14:30 -0600363 for (auto& entry : group)
364 {
365 try
Matthew Barth0ce99d82017-06-22 15:07:29 -0500366 {
Matthew Barthe4338cd2017-12-14 11:14:30 -0600367 T value = zone.template getPropertyValue<T>(
Matthew Barth3e1bb272020-05-26 11:09:21 -0500368 std::get<pathPos>(entry), std::get<intfPos>(entry),
369 std::get<propPos>(entry));
Matthew Barthe4338cd2017-12-14 11:14:30 -0600370 // TODO openbmc/phosphor-fan-presence#7 - Support possible
371 // state types for comparison
372 if (value < state)
Matthew Barth0ce99d82017-06-22 15:07:29 -0500373 {
Matthew Barthe4338cd2017-12-14 11:14:30 -0600374 if (netDelta == 0)
Matthew Barth0ce99d82017-06-22 15:07:29 -0500375 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500376 netDelta = ((state - value) / factor) * speedDelta;
Matthew Barthe4338cd2017-12-14 11:14:30 -0600377 }
378 else
379 {
380 // Decrease is the factor applied to the
381 // difference times the given speed delta
382 netDelta = std::min(
383 netDelta,
Patrick Williamsdfddd642024-08-16 15:21:51 -0400384 static_cast<uint64_t>(
385 ((state - value) / factor) * speedDelta));
Matthew Barth0ce99d82017-06-22 15:07:29 -0500386 }
Matthew Barthbc651602017-08-10 16:59:43 -0500387 }
Matthew Barthe4338cd2017-12-14 11:14:30 -0600388 else
Matthew Barthbc651602017-08-10 16:59:43 -0500389 {
Matthew Barthe4338cd2017-12-14 11:14:30 -0600390 // No decrease allowed for this group
391 netDelta = 0;
392 break;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500393 }
394 }
Matthew Barthe4338cd2017-12-14 11:14:30 -0600395 catch (const std::out_of_range& oore)
396 {
397 // Property value not found, netDelta unchanged
398 }
399 }
400 // Update group's decrease allowed state
401 zone.setDecreaseAllow(&group, !(netDelta == 0));
Matthew Barth0ce99d82017-06-22 15:07:29 -0500402 // Request speed decrease to occur on decrease interval
403 zone.requestSpeedDecrease(netDelta);
404 };
405}
406
Matthew Barthc410dda2019-01-11 16:31:13 -0600407/**
408 * @brief An action to use an alternate set of events
409 * @details Provides the ability to replace a default set of events with an
410 * alternate set of events based on all members of a group being at a specified
411 * state. When any member of the group no longer matches the provided state,
412 * the alternate set of events are replaced with the defaults.
413 *
414 * @param[in] state - State to compare the group's property value to
415 * @param[in] defEvents - The default set of events
416 * @param[in] altEvents - The alternate set of events
417 *
418 * @return Lambda function
419 * A lambda function that checks all group members are at a specified state
420 * and replacing the default set of events with an alternate set of events.
421 */
422template <typename T>
423auto use_alternate_events_on_state(T&& state,
424 std::vector<SetSpeedEvent>&& defEvents,
425 std::vector<SetSpeedEvent>&& altEvents)
426{
Matthew Barth3e1bb272020-05-26 11:09:21 -0500427 return [state = std::forward<T>(state), defEvents = std::move(defEvents),
428 altEvents = std::move(altEvents)](auto& zone, auto& group) {
Matthew Barthc410dda2019-01-11 16:31:13 -0600429 // Compare all group entries to the state
Patrick Williamsdfddd642024-08-16 15:21:51 -0400430 auto useAlt = std::all_of(
431 group.begin(), group.end(), [&zone, &state](const auto& entry) {
432 try
433 {
434 return zone.template getPropertyValue<T>(
435 std::get<pathPos>(entry),
436 std::get<intfPos>(entry),
437 std::get<propPos>(entry)) == state;
438 }
439 catch (const std::out_of_range& oore)
440 {
441 // Default to property not equal when not found
442 return false;
443 }
444 });
Matthew Barthc410dda2019-01-11 16:31:13 -0600445
Matthew Barth3e1bb272020-05-26 11:09:21 -0500446 const std::vector<SetSpeedEvent>* rmEvents = &altEvents;
447 const std::vector<SetSpeedEvent>* initEvents = &defEvents;
Matthew Barthc410dda2019-01-11 16:31:13 -0600448
449 if (useAlt)
450 {
451 rmEvents = &defEvents;
452 initEvents = &altEvents;
453 }
454
455 // Remove events
Matthew Barth3e1bb272020-05-26 11:09:21 -0500456 std::for_each(rmEvents->begin(), rmEvents->end(),
Patrick Williamsdfddd642024-08-16 15:21:51 -0400457 [&zone](const auto& entry) { zone.removeEvent(entry); });
Matthew Barthc410dda2019-01-11 16:31:13 -0600458 // Init events
Matthew Barth3e1bb272020-05-26 11:09:21 -0500459 std::for_each(initEvents->begin(), initEvents->end(),
Patrick Williamsdfddd642024-08-16 15:21:51 -0400460 [&zone](const auto& entry) { zone.initEvent(entry); });
Matthew Barthc410dda2019-01-11 16:31:13 -0600461 };
462}
463
Matthew Barth014f07c2019-05-30 09:55:42 -0500464/**
465 * @brief An action to set the floor speed on a zone
466 * @details Using sensor group values that are within a defined range, the
467 * floor speed is selected from the first map key entry that the median
468 * sensor value is less than where 3 or more sensor group values are valid.
469 * In the case where less than 3 sensor values are valid, use the highest
470 * sensor group value and default the floor speed when 0 sensor group values
471 * are valid.
472 *
473 * @param[in] lowerBound - Lowest allowed sensor value to be valid
474 * @param[in] upperBound - Highest allowed sensor value to be valid
475 * @param[in] valueToSpeed - Ordered map of sensor value-to-speed
476 *
477 * @return Action lambda function
478 * An Action function to set the zone's floor speed from a resulting group
479 * of valid sensor values based on their highest value or median.
480 */
Matthew Barth9ca60652020-02-10 13:01:08 -0600481template <typename T>
Matthew Barth3e1bb272020-05-26 11:09:21 -0500482Action set_floor_from_median_sensor_value(T&& lowerBound, T&& upperBound,
483 std::map<T, uint64_t>&& valueToSpeed)
Matthew Barth9ca60652020-02-10 13:01:08 -0600484{
485 return [lowerBound = std::forward<T>(lowerBound),
486 upperBound = std::forward<T>(upperBound),
487 valueToSpeed = std::move(valueToSpeed)](control::Zone& zone,
Matthew Barth3e1bb272020-05-26 11:09:21 -0500488 const Group& group) {
Matthew Barth9ca60652020-02-10 13:01:08 -0600489 auto speed = zone.getDefFloor();
490 if (group.size() != 0)
491 {
492 std::vector<T> validValues;
Patrick Williamsdfddd642024-08-16 15:21:51 -0400493 for (const auto& member : group)
Matthew Barth9ca60652020-02-10 13:01:08 -0600494 {
495 try
496 {
497 auto value = zone.template getPropertyValue<T>(
Matthew Barth3e1bb272020-05-26 11:09:21 -0500498 std::get<pathPos>(member), std::get<intfPos>(member),
499 std::get<propPos>(member));
Matthew Barth9ca60652020-02-10 13:01:08 -0600500 if (value == std::clamp(value, lowerBound, upperBound))
501 {
502 // Sensor value is valid
503 validValues.emplace_back(value);
504 }
505 }
506 catch (const std::out_of_range& oore)
507 {
508 continue;
509 }
510 }
511
512 if (!validValues.empty())
513 {
514 auto median = validValues.front();
515 // Get the determined median value
516 if (validValues.size() == 2)
517 {
518 // For 2 values, use the highest instead of the average
519 // for a thermally safe floor
520 median = *std::max_element(validValues.begin(),
521 validValues.end());
522 }
523 else if (validValues.size() > 2)
524 {
525 median = utility::getMedian(validValues);
526 }
527
528 // Use determined median sensor value to find floor speed
Matthew Barth3e1bb272020-05-26 11:09:21 -0500529 auto it = std::find_if(valueToSpeed.begin(), valueToSpeed.end(),
Patrick Williamsdfddd642024-08-16 15:21:51 -0400530 [&median](const auto& entry) {
531 return median < entry.first;
532 });
Matthew Barth9ca60652020-02-10 13:01:08 -0600533 if (it != std::end(valueToSpeed))
534 {
535 speed = (*it).second;
536 }
537 }
538 }
539 zone.setFloor(speed);
540 };
541}
Matthew Barth014f07c2019-05-30 09:55:42 -0500542
Matthew Barth8d06a832019-10-02 14:43:39 -0500543/**
544 * @brief An action to update the default floor speed
545 * @details Provides the ability to update the default fan floor speed when
546 * all of the group members property values match the value given
547 *
548 * @param[in] state - State to compare the group's property value to
549 * @param[in] speed - Speed to set the default fan floor to
550 *
551 * @return Lambda function
552 * A lambda function that checks all group members are at a specified state
553 * and updates the default fan floor speed.
554 */
555template <typename T>
556auto update_default_floor(T&& state, uint64_t speed)
557{
Matthew Barth3e1bb272020-05-26 11:09:21 -0500558 return [speed, state = std::forward<T>(state)](auto& zone, auto& group) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400559 auto updateDefFloor = std::all_of(
560 group.begin(), group.end(), [&zone, &state](const auto& entry) {
561 try
562 {
563 return zone.template getPropertyValue<T>(
564 std::get<pathPos>(entry),
565 std::get<intfPos>(entry),
566 std::get<propPos>(entry)) == state;
567 }
568 catch (const std::out_of_range& oore)
569 {
570 // Default to property not equal when not found
571 return false;
572 }
573 });
Matthew Barth8d06a832019-10-02 14:43:39 -0500574
575 if (!updateDefFloor)
576 {
577 // Do not update the default floor
578 return;
579 }
580
581 // Set/update the default floor of the zone
582 zone.setDefFloor(speed);
583 };
584}
585
Matthew Barth799cdf72019-10-08 15:00:10 -0500586/**
587 * @brief An action to use a set of events
588 * @details Provides the ability to use a set of events when all members of
589 * a group are at a specified state. When any member of the group no longer
590 * matches the provided state the set of events are removed.
591 *
592 * @param[in] state - State to compare the group's property value to
593 * @param[in] events - The set of events
594 *
595 * @return Lambda function
596 * A lambda function that checks all group members are at a specified state
597 * and initializes the set of events, otherwise removes them.
598 */
599template <typename T>
Matthew Barth3e1bb272020-05-26 11:09:21 -0500600auto use_events_on_state(T&& state, std::vector<SetSpeedEvent>&& events)
Matthew Barth799cdf72019-10-08 15:00:10 -0500601{
602 return [state = std::forward<T>(state),
Matthew Barth3e1bb272020-05-26 11:09:21 -0500603 events = std::move(events)](auto& zone, auto& group) {
Matthew Barth799cdf72019-10-08 15:00:10 -0500604 // Compare all group entries to the state
Patrick Williamsdfddd642024-08-16 15:21:51 -0400605 auto useEvents = std::all_of(
606 group.begin(), group.end(), [&zone, &state](const auto& entry) {
607 try
608 {
609 return zone.template getPropertyValue<T>(
610 std::get<pathPos>(entry),
611 std::get<intfPos>(entry),
612 std::get<propPos>(entry)) == state;
613 }
614 catch (const std::out_of_range& oore)
615 {
616 // Default to property not equal when not found
617 return false;
618 }
619 });
Matthew Barth799cdf72019-10-08 15:00:10 -0500620
621 if (useEvents)
622 {
623 // Init events
Patrick Williamsdfddd642024-08-16 15:21:51 -0400624 std::for_each(events.begin(), events.end(),
625 [&zone](const auto& entry) {
626 zone.initEvent(entry);
627 });
Matthew Barth799cdf72019-10-08 15:00:10 -0500628 }
629 else
630 {
631 // Remove events
Patrick Williamsdfddd642024-08-16 15:21:51 -0400632 std::for_each(events.begin(), events.end(),
633 [&zone](const auto& entry) {
634 zone.removeEvent(entry);
635 });
Matthew Barth799cdf72019-10-08 15:00:10 -0500636 }
637 };
638}
639
Matthew Barth17d1fe22017-05-11 15:00:36 -0500640} // namespace action
641} // namespace control
642} // namespace fan
643} // namespace phosphor