1 /**
2  * Copyright © 2020 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 "config.h"
17 
18 #include "system.hpp"
19 
20 #include "fan.hpp"
21 #include "fan_defs.hpp"
22 #include "tach_sensor.hpp"
23 #include "trust_manager.hpp"
24 #include "types.hpp"
25 #ifdef MONITOR_USE_JSON
26 #include "json_parser.hpp"
27 #endif
28 
29 #include <nlohmann/json.hpp>
30 #include <sdbusplus/bus.hpp>
31 #include <sdeventplus/event.hpp>
32 
33 namespace phosphor::fan::monitor
34 {
35 
36 using json = nlohmann::json;
37 
38 System::System(Mode mode, sdbusplus::bus::bus& bus,
39                const sdeventplus::Event& event) :
40     _mode(mode),
41     _bus(bus), _event(event)
42 {
43     json jsonObj = json::object();
44 #ifdef MONITOR_USE_JSON
45     jsonObj = getJsonObj(bus);
46 #endif
47     // Retrieve and set trust groups within the trust manager
48     _trust = std::make_unique<trust::Manager>(getTrustGroups(jsonObj));
49 
50     // Retrieve fan definitions and create fan objects to be monitored
51     for (const auto& fanDef : getFanDefinitions(jsonObj))
52     {
53         // Check if a condition exists on the fan
54         auto condition = std::get<conditionField>(fanDef);
55         if (condition)
56         {
57             // Condition exists, skip adding fan if it fails
58             if (!(*condition)(bus))
59             {
60                 continue;
61             }
62         }
63         _fans.emplace_back(
64             std::make_unique<Fan>(mode, bus, event, _trust, fanDef));
65     }
66 }
67 
68 const std::vector<CreateGroupFunction>
69     System::getTrustGroups(const json& jsonObj)
70 {
71 #ifdef MONITOR_USE_JSON
72     return getTrustGrps(jsonObj);
73 #else
74     return trustGroups;
75 #endif
76 }
77 
78 const std::vector<FanDefinition> System::getFanDefinitions(const json& jsonObj)
79 {
80 #ifdef MONITOR_USE_JSON
81     return getFanDefs(jsonObj);
82 #else
83     return fanDefinitions;
84 #endif
85 }
86 
87 } // namespace phosphor::fan::monitor
88