blob: c2a090411a820e3e1b0b21bde15f25534cd63821 [file] [log] [blame]
Matthew Bartha227a162020-08-05 10:51:45 -05001/**
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
Matthew Barthd9cb63b2021-03-24 14:36:49 -050018#include "action.hpp"
19#include "group.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060020#include "json_config.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060021#include "profile.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060022#include "zone.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060023
Matthew Bartha227a162020-08-05 10:51:45 -050024#include <nlohmann/json.hpp>
25#include <sdbusplus/bus.hpp>
Matthew Barth06764942021-03-04 09:28:40 -060026#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050027#include <sdeventplus/utility/timer.hpp>
28
29#include <chrono>
30#include <map>
31#include <memory>
32#include <optional>
33#include <tuple>
34#include <utility>
35#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050036
37namespace phosphor::fan::control::json
38{
39
40using json = nlohmann::json;
41
Matthew Barthacd737c2021-03-04 11:04:01 -060042/* Application name to be appended to the path for loading a JSON config file */
43constexpr auto confAppName = "control";
44
45/**
46 * Configuration object key to uniquely map to the configuration object
47 * Pair constructed of:
48 * std::string = Configuration object's name
49 * std::vector<std::string> = List of profiles the configuration object
50 * is included in
51 */
52using configKey = std::pair<std::string, std::vector<std::string>>;
53
Matthew Barthd9cb63b2021-03-24 14:36:49 -050054/* Type of timers supported */
55enum class TimerType
56{
57 oneshot,
58 repeating,
59};
60/**
61 * Package of data required when a timer expires
62 * Tuple constructed of:
63 * std::string = Timer package unique identifier
64 * Zone = Zone object to run the actions against
65 * Group = Group of dbus objects for the actions
66 * std::vector<std::unique_ptr<ActionBase>> = List of pointers to actions
67 * that run when the timer expires
68 */
69using TimerPkg = std::tuple<std::string, Zone&, const Group&,
70 std::vector<std::unique_ptr<ActionBase>>&>;
71/**
72 * Data associated with a running timer that's used when it expires
73 * Pair constructed of:
74 * TimerType = Type of timer to manage expired timer instances
75 * TimerPkg = Package of data required when the timer expires
76 */
77using TimerData = std::pair<TimerType, TimerPkg>;
78/* Dbus event timer */
79using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
80
Matthew Bartha227a162020-08-05 10:51:45 -050081/**
82 * @class Manager - Represents the fan control manager's configuration
83 *
84 * A fan control manager configuration is optional, therefore the "manager.json"
85 * file is also optional. The manager configuration is used to populate
86 * fan control's manager parameters which are used in how the application
87 * operates, not in how the fans are controlled.
88 *
89 * When no manager configuration exists, the fan control application starts,
90 * processes any configured events and then begins controlling fans according
91 * to those events.
92 */
93class Manager
94{
95 public:
96 Manager() = delete;
97 Manager(const Manager&) = delete;
98 Manager(Manager&&) = delete;
99 Manager& operator=(const Manager&) = delete;
100 Manager& operator=(Manager&&) = delete;
101 ~Manager() = default;
102
103 /**
104 * Constructor
105 * Parses and populates the fan control manager attributes from a json file
106 *
107 * @param[in] bus - sdbusplus bus object
Matthew Barth06764942021-03-04 09:28:40 -0600108 * @param[in] event - sdeventplus event loop
Matthew Bartha227a162020-08-05 10:51:45 -0500109 */
Matthew Barth06764942021-03-04 09:28:40 -0600110 Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
Matthew Bartha227a162020-08-05 10:51:45 -0500111
112 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600113 * @brief Get the active profiles of the system where an empty list
114 * represents that only configuration entries without a profile defined will
115 * be loaded.
116 *
117 * @return - The list of active profiles
118 */
119 static const std::vector<std::string>& getActiveProfiles();
120
121 /**
122 * @brief Load the configuration of a given JSON class object based on the
123 * active profiles
124 *
125 * @param[in] bus - The sdbusplus bus object
126 * @param[in] isOptional - JSON configuration file is optional or not
127 * Defaults to false
128 *
129 * @return Map of configuration entries
130 * Map of configuration keys to their corresponding configuration object
131 */
132 template <typename T>
133 static std::map<configKey, std::unique_ptr<T>>
134 getConfig(sdbusplus::bus::bus& bus, bool isOptional = false)
135 {
136 std::map<configKey, std::unique_ptr<T>> config;
137
138 auto confFile = fan::JsonConfig::getConfFile(
139 bus, confAppName, T::confFileName, isOptional);
140 if (!confFile.empty())
141 {
142 for (const auto& entry : fan::JsonConfig::load(confFile))
143 {
144 if (entry.contains("profiles"))
145 {
146 std::vector<std::string> profiles;
147 for (const auto& profile : entry["profiles"])
148 {
149 profiles.emplace_back(
150 profile.template get<std::string>());
151 }
152 // Do not create the object if its profiles are not in the
153 // list of active profiles
154 if (!std::any_of(profiles.begin(), profiles.end(),
155 [](const auto& name) {
156 return std::find(
157 getActiveProfiles().begin(),
158 getActiveProfiles().end(),
159 name) !=
160 getActiveProfiles().end();
161 }))
162 {
163 continue;
164 }
165 }
166 auto obj = std::make_unique<T>(bus, entry);
167 config.emplace(
168 std::make_pair(obj->getName(), obj->getProfiles()),
169 std::move(obj));
170 }
171 }
172 return config;
173 }
174
175 /**
Matthew Barth12cb1252021-03-08 16:47:30 -0600176 * @brief Check if the given path and inteface is owned by a dbus service
177 *
178 * @param[in] path - Dbus object path
179 * @param[in] intf - Dbus object interface
180 *
181 * @return - Whether the service has an owner for the given object path and
182 * interface
183 */
184 static bool hasOwner(const std::string& path, const std::string& intf);
185
186 /**
Matthew Barth07fecfc2021-01-29 09:04:43 -0600187 * @brief Get the object's property value as a variant
188 *
189 * @param[in] path - Path of the object containing the property
190 * @param[in] intf - Interface name containing the property
191 * @param[in] prop - Name of property
192 *
193 * @return - The object's property value as a variant
194 */
195 static inline auto getObjValueVariant(const std::string& path,
196 const std::string& intf,
197 const std::string& prop)
198 {
199 return _objects.at(path).at(intf).at(prop);
200 };
201
202 /**
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500203 * @brief Add a dbus timer
204 *
205 * @param[in] type - Type of timer
206 * @param[in] interval - Timer interval in microseconds
207 * @param[in] pkg - Packaged data for when timer expires
208 */
209 void addTimer(const TimerType type,
210 const std::chrono::microseconds interval,
211 std::unique_ptr<TimerPkg> pkg);
212
213 /**
214 * @brief Callback when a timer expires
215 *
216 * @param[in] data - Data to be used when the timer expired
217 */
218 void timerExpired(TimerData& data);
219
220 /**
Matthew Bartha227a162020-08-05 10:51:45 -0500221 * @brief Get the configured power on delay(OPTIONAL)
222 *
223 * @return Power on delay in seconds
224 * Configured power on delay in seconds, otherwise 0
225 */
226 unsigned int getPowerOnDelay();
227
228 private:
229 /* JSON file name for manager configuration attributes */
230 static constexpr auto confFileName = "manager.json";
231
232 /* The parsed JSON object */
233 json _jsonObj;
Matthew Barth06764942021-03-04 09:28:40 -0600234
Matthew Barthacd737c2021-03-04 11:04:01 -0600235 /**
236 * The sdbusplus bus object to use
237 */
238 sdbusplus::bus::bus& _bus;
239
240 /**
241 * The sdeventplus even loop to use
242 */
243 sdeventplus::Event _event;
244
Matthew Barth06764942021-03-04 09:28:40 -0600245 /* List of profiles configured */
Matthew Barthacd737c2021-03-04 11:04:01 -0600246 std::map<configKey, std::unique_ptr<Profile>> _profiles;
Matthew Barth06764942021-03-04 09:28:40 -0600247
248 /* List of active profiles */
Matthew Barthacd737c2021-03-04 11:04:01 -0600249 static std::vector<std::string> _activeProfiles;
250
Matthew Barth12cb1252021-03-08 16:47:30 -0600251 /* Subtree map of paths to services (with ownership state) of interfaces */
252 static std::map<std::string, std::map<std::pair<std::string, bool>,
253 std::vector<std::string>>>
254 _servTree;
255
Matthew Barth07fecfc2021-01-29 09:04:43 -0600256 /* Object map of paths to interfaces of properties and their values */
257 static std::map<
258 std::string,
259 std::map<std::string, std::map<std::string, PropertyVariantType>>>
260 _objects;
261
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500262 /* List of timers and their data to be processed when expired */
263 std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers;
264
Matthew Barthacd737c2021-03-04 11:04:01 -0600265 /* List of zones configured */
266 std::map<configKey, std::unique_ptr<Zone>> _zones;
267
268 /**
269 * @brief Parse and set the configured profiles from the profiles JSON file
270 *
271 * Retrieves the optional profiles JSON configuration file, parses it, and
272 * creates a list of configured profiles available to the other
273 * configuration files. These profiles can be used to remove or include
274 * entries within the other configuration files.
275 */
276 void setProfiles();
Matthew Bartha227a162020-08-05 10:51:45 -0500277};
278
279} // namespace phosphor::fan::control::json