107fecfc6SMatthew Barth /**
207fecfc6SMatthew Barth * Copyright © 2021 IBM Corporation
307fecfc6SMatthew Barth *
407fecfc6SMatthew Barth * Licensed under the Apache License, Version 2.0 (the "License");
507fecfc6SMatthew Barth * you may not use this file except in compliance with the License.
607fecfc6SMatthew Barth * You may obtain a copy of the License at
707fecfc6SMatthew Barth *
807fecfc6SMatthew Barth * http://www.apache.org/licenses/LICENSE-2.0
907fecfc6SMatthew Barth *
1007fecfc6SMatthew Barth * Unless required by applicable law or agreed to in writing, software
1107fecfc6SMatthew Barth * distributed under the License is distributed on an "AS IS" BASIS,
1207fecfc6SMatthew Barth * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1307fecfc6SMatthew Barth * See the License for the specific language governing permissions and
1407fecfc6SMatthew Barth * limitations under the License.
1507fecfc6SMatthew Barth */
1607fecfc6SMatthew Barth #include "request_target_base.hpp"
1707fecfc6SMatthew Barth
1807fecfc6SMatthew Barth #include "../manager.hpp"
1907fecfc6SMatthew Barth #include "../zone.hpp"
2007fecfc6SMatthew Barth #include "group.hpp"
2107fecfc6SMatthew Barth
2207fecfc6SMatthew Barth #include <nlohmann/json.hpp>
23*64b5ac20SAnwaar Hadi #include <phosphor-logging/lg2.hpp>
2407fecfc6SMatthew Barth
2507fecfc6SMatthew Barth #include <algorithm>
2607fecfc6SMatthew Barth
2707fecfc6SMatthew Barth namespace phosphor::fan::control::json
2807fecfc6SMatthew Barth {
2907fecfc6SMatthew Barth
3007fecfc6SMatthew Barth using json = nlohmann::json;
3107fecfc6SMatthew Barth
RequestTargetBase(const json & jsonObj,const std::vector<Group> & groups)3219c77494SMatthew Barth RequestTargetBase::RequestTargetBase(const json& jsonObj,
3319c77494SMatthew Barth const std::vector<Group>& groups) :
3419c77494SMatthew Barth ActionBase(jsonObj, groups)
3507fecfc6SMatthew Barth {
3607fecfc6SMatthew Barth // There are no JSON configuration parameters for this action
3707fecfc6SMatthew Barth }
3807fecfc6SMatthew Barth
run(Zone & zone)396d2476c9SMatthew Barth void RequestTargetBase::run(Zone& zone)
4007fecfc6SMatthew Barth {
4107fecfc6SMatthew Barth uint64_t base = 0;
426d2476c9SMatthew Barth for (const auto& group : _groups)
436d2476c9SMatthew Barth {
4407fecfc6SMatthew Barth for (const auto& member : group.getMembers())
4507fecfc6SMatthew Barth {
4607fecfc6SMatthew Barth try
4707fecfc6SMatthew Barth {
4807fecfc6SMatthew Barth auto value = Manager::getObjValueVariant(
4907fecfc6SMatthew Barth member, group.getInterface(), group.getProperty());
5007fecfc6SMatthew Barth if (auto intPtr = std::get_if<int64_t>(&value))
5107fecfc6SMatthew Barth {
5207fecfc6SMatthew Barth // Throw out any negative values as those are not valid
5307fecfc6SMatthew Barth // to use as a fan target base
5407fecfc6SMatthew Barth if (*intPtr < 0)
5507fecfc6SMatthew Barth {
5607fecfc6SMatthew Barth continue;
5707fecfc6SMatthew Barth }
5807fecfc6SMatthew Barth base = std::max(base, static_cast<uint64_t>(*intPtr));
5907fecfc6SMatthew Barth }
6007fecfc6SMatthew Barth else if (auto dblPtr = std::get_if<double>(&value))
6107fecfc6SMatthew Barth {
6207fecfc6SMatthew Barth // Throw out any negative values as those are not valid
6307fecfc6SMatthew Barth // to use as a fan target base
6407fecfc6SMatthew Barth if (*dblPtr < 0)
6507fecfc6SMatthew Barth {
6607fecfc6SMatthew Barth continue;
6707fecfc6SMatthew Barth }
6807fecfc6SMatthew Barth // Precision of a double not a concern with fan targets
6907fecfc6SMatthew Barth base = std::max(base, static_cast<uint64_t>(*dblPtr));
7007fecfc6SMatthew Barth }
7107fecfc6SMatthew Barth else
7207fecfc6SMatthew Barth {
7307fecfc6SMatthew Barth // Unsupported group member type for this action
74*64b5ac20SAnwaar Hadi lg2::error(
75*64b5ac20SAnwaar Hadi "Action {ACTION_NAME}: Unsupported group member type "
76*64b5ac20SAnwaar Hadi "given. [object = {MEMBER} : {GROUP_INTERFACE} : {GROUP_PROPERTY}]",
77*64b5ac20SAnwaar Hadi "ACTION_NAME", getName(), "MEMBER", member,
78*64b5ac20SAnwaar Hadi "GROUP_INTERFACE", group.getInterface(),
79*64b5ac20SAnwaar Hadi "GROUP_PROPERTY", group.getProperty());
8007fecfc6SMatthew Barth }
8107fecfc6SMatthew Barth }
8207fecfc6SMatthew Barth catch (const std::out_of_range& oore)
8307fecfc6SMatthew Barth {
8407fecfc6SMatthew Barth // Property value not found, base request target unchanged
8507fecfc6SMatthew Barth }
8607fecfc6SMatthew Barth }
876d2476c9SMatthew Barth }
8807fecfc6SMatthew Barth
8907fecfc6SMatthew Barth // A request target base of 0 defaults to the current target
9007fecfc6SMatthew Barth zone.setRequestTargetBase(base);
9107fecfc6SMatthew Barth }
9207fecfc6SMatthew Barth
9307fecfc6SMatthew Barth } // namespace phosphor::fan::control::json
94