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