blob: 5f53de3c4db39312c7d33718015ae0f79fa57077 [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
22#include <fmt/format.h>
23
24#include <nlohmann/json.hpp>
25#include <phosphor-logging/log.hpp>
26
27#include <algorithm>
28
29namespace phosphor::fan::control::json
30{
31
32using json = nlohmann::json;
33using namespace phosphor::logging;
34
Matthew Barthbfd7e1b2021-02-04 14:25:47 -060035RequestTargetBase::RequestTargetBase(const json& jsonObj) : ActionBase(jsonObj)
Matthew Barth07fecfc2021-01-29 09:04:43 -060036{
37 // There are no JSON configuration parameters for this action
38}
39
40void RequestTargetBase::run(Zone& zone, const Group& group)
41{
42 uint64_t base = 0;
43 for (const auto& member : group.getMembers())
44 {
45 try
46 {
47 auto value = Manager::getObjValueVariant(
48 member, group.getInterface(), group.getProperty());
49 if (auto intPtr = std::get_if<int64_t>(&value))
50 {
51 // Throw out any negative values as those are not valid
52 // to use as a fan target base
53 if (*intPtr < 0)
54 {
55 continue;
56 }
57 base = std::max(base, static_cast<uint64_t>(*intPtr));
58 }
59 else if (auto dblPtr = std::get_if<double>(&value))
60 {
61 // Throw out any negative values as those are not valid
62 // to use as a fan target base
63 if (*dblPtr < 0)
64 {
65 continue;
66 }
67 // Precision of a double not a concern with fan targets
68 base = std::max(base, static_cast<uint64_t>(*dblPtr));
69 }
70 else
71 {
72 // Unsupported group member type for this action
73 log<level::ERR>(
74 fmt::format("Action {}: Unsupported group member type "
75 "given. [object = {} : {} : {}]",
76 getName(), member, group.getInterface(),
77 group.getProperty())
78 .c_str());
79 }
80 }
81 catch (const std::out_of_range& oore)
82 {
83 // Property value not found, base request target unchanged
84 }
85 }
86
87 // A request target base of 0 defaults to the current target
88 zone.setRequestTargetBase(base);
89}
90
91} // namespace phosphor::fan::control::json