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 #pragma once
17 
18 #include "config_base.hpp"
19 
20 #include <nlohmann/json.hpp>
21 #include <sdbusplus/bus.hpp>
22 
23 #include <map>
24 
25 namespace phosphor::fan::control::json
26 {
27 
28 using json = nlohmann::json;
29 
30 /**
31  * @class Fan - Represents a configured fan control fan object
32  *
33  * A fan object contains the configured attributes for a fan within the system
34  * that will be controlled by the fan control application. These configuration
35  * attributes include, but are not limited to, the cooling zone in which the
36  * fan is included, what sensors make up the fan, the target interface to be
37  * used in setting a target, and any profiles(OPTIONAL) the fan should be
38  * included in.
39  *
40  * (When no profile for a fan is given, the fan defaults to always be included)
41  *
42  */
43 class Fan : public ConfigBase
44 {
45   public:
46     /* JSON file name for fans */
47     static constexpr auto confFileName = "fans.json";
48 
49     Fan() = delete;
50     Fan(const Fan&) = delete;
51     Fan(Fan&&) = delete;
52     Fan& operator=(const Fan&) = delete;
53     Fan& operator=(Fan&&) = delete;
54     ~Fan() = default;
55 
56     /**
57      * Constructor
58      * Parses and populates a zone fan from JSON object data
59      *
60      * @param[in] jsonObj - JSON object
61      */
62     Fan(const json& jsonObj);
63 
64     /**
65      * @brief Get the zone
66      *
67      * @return Zone this fan belongs in
68      */
69     inline const auto& getZone() const
70     {
71         return _zone;
72     }
73 
74     /**
75      * @brief Get the list of sensors
76      *
77      * @return List of sensors with `Target` property
78      */
79     inline const auto& getSensors() const
80     {
81         return _sensors;
82     }
83 
84     /**
85      * @brief Get the sensors' interface
86      *
87      * @return Interface containing `Target` to use on sensors
88      */
89     inline const auto& getInterface() const
90     {
91         return _interface;
92     }
93 
94     /**
95      * @brief Get the current fan target
96      *
97      * @return - The current target of the fan
98      */
99     inline auto getTarget() const
100     {
101         return _target;
102     }
103 
104     /**
105      * Sets the target value on all contained sensors
106      *
107      * @param[in] target - The value to set
108      */
109     void setTarget(uint64_t target);
110 
111   private:
112     /* The sdbusplus bus object */
113     sdbusplus::bus::bus& _bus;
114 
115     /**
116      * Interface containing the `Target` property
117      * to use in controlling the fan's target
118      */
119     std::string _interface;
120 
121     /* Target for this fan */
122     uint64_t _target;
123 
124     /**
125      * Map of sensors containing the `Target` property on
126      * dbus to the service providing them that make up the fan
127      */
128     std::map<std::string, std::string> _sensors;
129 
130     /* The zone this fan belongs to */
131     std::string _zone;
132 
133     /**
134      * @brief Parse and set the fan's sensor interface
135      *
136      * @param[in] jsonObj - JSON object for the fan
137      *
138      * Sets the sensor interface to use when setting the `Target` property
139      */
140     void setInterface(const json& jsonObj);
141 
142     /**
143      * @brief Parse and set the fan's sensor list
144      *
145      * @param[in] jsonObj - JSON object for the fan
146      *
147      * Sets the list of sensors that contain a `Target` property on dbus
148      * that make up this fan.
149      */
150     void setSensors(const json& jsonObj);
151 
152     /**
153      * @brief Parse and set the fan's zone
154      *
155      * @param[in] jsonObj - JSON object for the fan
156      *
157      * Sets the zone this fan is included in.
158      */
159     void setZone(const json& jsonObj);
160 };
161 
162 } // namespace phosphor::fan::control::json
163