xref: /openbmc/phosphor-fan-presence/control/json/actions/request_target_base.cpp (revision 64b5ac203518568ec8b7569d0e785352278f2472)
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 
22 #include <nlohmann/json.hpp>
23 #include <phosphor-logging/lg2.hpp>
24 
25 #include <algorithm>
26 
27 namespace phosphor::fan::control::json
28 {
29 
30 using json = nlohmann::json;
31 
RequestTargetBase(const json & jsonObj,const std::vector<Group> & groups)32 RequestTargetBase::RequestTargetBase(const json& jsonObj,
33                                      const std::vector<Group>& groups) :
34     ActionBase(jsonObj, groups)
35 {
36     // There are no JSON configuration parameters for this action
37 }
38 
run(Zone & zone)39 void RequestTargetBase::run(Zone& zone)
40 {
41     uint64_t base = 0;
42     for (const auto& group : _groups)
43     {
44         for (const auto& member : group.getMembers())
45         {
46             try
47             {
48                 auto value = Manager::getObjValueVariant(
49                     member, group.getInterface(), group.getProperty());
50                 if (auto intPtr = std::get_if<int64_t>(&value))
51                 {
52                     // 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));
59                 }
60                 else if (auto dblPtr = std::get_if<double>(&value))
61                 {
62                     // 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));
70                 }
71                 else
72                 {
73                     // Unsupported group member type for this action
74                     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());
80                 }
81             }
82             catch (const std::out_of_range& oore)
83             {
84                 // Property value not found, base request target unchanged
85             }
86         }
87     }
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
94