blob: 186e611c2b06626f216a5322547983dff3d591ac [file] [log] [blame]
Matthew Barth07fecfc2021-01-29 09:04:43 -06001/**
2 * Copyright © 2021 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "request_target_base.hpp"
17
18#include "../manager.hpp"
19#include "../zone.hpp"
20#include "group.hpp"
21
Matthew Barth07fecfc2021-01-29 09:04:43 -060022#include <nlohmann/json.hpp>
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000023#include <phosphor-logging/lg2.hpp>
Matthew Barth07fecfc2021-01-29 09:04:43 -060024
25#include <algorithm>
26
27namespace phosphor::fan::control::json
28{
29
30using json = nlohmann::json;
Matthew Barth07fecfc2021-01-29 09:04:43 -060031
Matthew Barth19c77492021-04-08 10:06:06 -050032RequestTargetBase::RequestTargetBase(const json& jsonObj,
33 const std::vector<Group>& groups) :
34 ActionBase(jsonObj, groups)
Matthew Barth07fecfc2021-01-29 09:04:43 -060035{
36 // There are no JSON configuration parameters for this action
37}
38
Matthew Barth6d2476c2021-04-08 10:48:57 -050039void RequestTargetBase::run(Zone& zone)
Matthew Barth07fecfc2021-01-29 09:04:43 -060040{
41 uint64_t base = 0;
Matthew Barth6d2476c2021-04-08 10:48:57 -050042 for (const auto& group : _groups)
Matthew Barth07fecfc2021-01-29 09:04:43 -060043 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050044 for (const auto& member : group.getMembers())
Matthew Barth07fecfc2021-01-29 09:04:43 -060045 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050046 try
Matthew Barth07fecfc2021-01-29 09:04:43 -060047 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050048 auto value = Manager::getObjValueVariant(
49 member, group.getInterface(), group.getProperty());
50 if (auto intPtr = std::get_if<int64_t>(&value))
Matthew Barth07fecfc2021-01-29 09:04:43 -060051 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050052 // Throw out any negative values as those are not valid
53 // to use as a fan target base
54 if (*intPtr < 0)
55 {
56 continue;
57 }
58 base = std::max(base, static_cast<uint64_t>(*intPtr));
Matthew Barth07fecfc2021-01-29 09:04:43 -060059 }
Matthew Barth6d2476c2021-04-08 10:48:57 -050060 else if (auto dblPtr = std::get_if<double>(&value))
Matthew Barth07fecfc2021-01-29 09:04:43 -060061 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050062 // Throw out any negative values as those are not valid
63 // to use as a fan target base
64 if (*dblPtr < 0)
65 {
66 continue;
67 }
68 // Precision of a double not a concern with fan targets
69 base = std::max(base, static_cast<uint64_t>(*dblPtr));
Matthew Barth07fecfc2021-01-29 09:04:43 -060070 }
Matthew Barth6d2476c2021-04-08 10:48:57 -050071 else
72 {
73 // Unsupported group member type for this action
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000074 lg2::error(
75 "Action {ACTION_NAME}: Unsupported group member type "
76 "given. [object = {MEMBER} : {GROUP_INTERFACE} : {GROUP_PROPERTY}]",
77 "ACTION_NAME", getName(), "MEMBER", member,
78 "GROUP_INTERFACE", group.getInterface(),
79 "GROUP_PROPERTY", group.getProperty());
Matthew Barth6d2476c2021-04-08 10:48:57 -050080 }
Matthew Barth07fecfc2021-01-29 09:04:43 -060081 }
Matthew Barth6d2476c2021-04-08 10:48:57 -050082 catch (const std::out_of_range& oore)
Matthew Barth07fecfc2021-01-29 09:04:43 -060083 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050084 // Property value not found, base request target unchanged
Matthew Barth07fecfc2021-01-29 09:04:43 -060085 }
86 }
Matthew Barth07fecfc2021-01-29 09:04:43 -060087 }
88
89 // A request target base of 0 defaults to the current target
90 zone.setRequestTargetBase(base);
91}
92
93} // namespace phosphor::fan::control::json