blob: a703ab6e1dac7e0e654e786435117c45bc117b6f [file] [log] [blame]
Matthew Barthd87f89f2020-07-30 10:41:32 -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 Barth2331d182020-08-18 15:00:27 -050018#include "json_config.hpp"
Matthew Barth23ac24c2020-08-04 13:55:43 -050019#include "types.hpp"
20
Matthew Barth2331d182020-08-18 15:00:27 -050021#include <nlohmann/json.hpp>
Matthew Barth23ac24c2020-08-04 13:55:43 -050022#include <sdbusplus/bus.hpp>
23
Matthew Barth2331d182020-08-18 15:00:27 -050024#include <map>
25#include <memory>
Matthew Barthce957262020-08-27 10:35:57 -050026#include <utility>
27#include <vector>
Matthew Barth2331d182020-08-18 15:00:27 -050028
Matthew Barthd87f89f2020-07-30 10:41:32 -050029namespace phosphor::fan::control
Matthew Barth167d2dd2020-08-04 12:19:16 -050030{
31
Matthew Barth2331d182020-08-18 15:00:27 -050032/* Application name to be appended to the path for loading a JSON config file */
Matthew Barth167d2dd2020-08-04 12:19:16 -050033constexpr auto confAppName = "control";
34
Matthew Barth23ac24c2020-08-04 13:55:43 -050035/**
Matthew Barthce957262020-08-27 10:35:57 -050036 * Configuration object key to uniquely map to the configuration object
37 * Pair constructed of:
38 * std::string = Configuration object's name
39 * std::vector<std::string> = List of profiles the configuration object
40 * is included in
41 */
42using configKey = std::pair<std::string, std::vector<std::string>>;
43
44/**
Matthew Barth391ade02021-01-15 14:33:21 -060045 * @brief Load the configuration of a given JSON class object type that requires
46 * a dbus object
Matthew Barth2331d182020-08-18 15:00:27 -050047 *
48 * @param[in] bus - The dbus bus object
49 * @param[in] isOptional - JSON configuration file is optional or not
50 * Defaults to false
51 *
52 * @return Map of configuration entries
Matthew Barthce957262020-08-27 10:35:57 -050053 * Map of configuration keys to their corresponding configuration object
Matthew Barth2331d182020-08-18 15:00:27 -050054 */
55template <typename T>
Matthew Barthce957262020-08-27 10:35:57 -050056std::map<configKey, std::unique_ptr<T>> getConfig(sdbusplus::bus::bus& bus,
57 bool isOptional = false)
Matthew Barth2331d182020-08-18 15:00:27 -050058{
Matthew Barthce957262020-08-27 10:35:57 -050059 std::map<configKey, std::unique_ptr<T>> config;
Matthew Barth2331d182020-08-18 15:00:27 -050060
61 auto confFile = fan::JsonConfig::getConfFile(bus, confAppName,
62 T::confFileName, isOptional);
63 if (!confFile.empty())
64 {
65 for (const auto& entry : fan::JsonConfig::load(confFile))
66 {
67 auto obj = std::make_unique<T>(bus, entry);
Matthew Barthce957262020-08-27 10:35:57 -050068 config.emplace(std::make_pair(obj->getName(), obj->getProfiles()),
69 std::move(obj));
Matthew Barth2331d182020-08-18 15:00:27 -050070 }
71 }
Matthew Barth2331d182020-08-18 15:00:27 -050072 return config;
73}
74
75/**
Matthew Barth391ade02021-01-15 14:33:21 -060076 * @brief Load the configuration of a given JSON class object type that does not
77 * require a dbus object
78 *
79 * @param[in] isOptional - JSON configuration file is optional or not
80 * Defaults to false
81 *
82 * @return Map of configuration entries
83 * Map of configuration keys to their corresponding configuration object
84 */
85template <typename T>
86std::map<configKey, std::unique_ptr<T>> getConfig(bool isOptional = false)
87{
88 // Dbus object is needed to retrieve the JSON configuration file
89 auto bus = sdbusplus::bus::new_default();
90 std::map<configKey, std::unique_ptr<T>> config;
91
92 auto confFile = fan::JsonConfig::getConfFile(bus, confAppName,
93 T::confFileName, isOptional);
94 if (!confFile.empty())
95 {
96 for (const auto& entry : fan::JsonConfig::load(confFile))
97 {
98 auto obj = std::make_unique<T>(entry);
99 config.emplace(std::make_pair(obj->getName(), obj->getProfiles()),
100 std::move(obj));
101 }
102 }
103 return config;
104}
105
106/**
Matthew Barth413e4d02020-09-29 10:44:37 -0500107 * @brief Helper function to determine when a configuration entry is included
108 * based on the list of active profiles and its list of profiles
109 *
110 * A configuration entry may include a list of profiles that when any one of
111 * the profiles are active, the entry should be included. When the list of
112 * profiles for a configuration entry is empty, that results in always
113 * including the entry. An empty list of active profiles results in including
114 * only those entries configured without a list of profiles.
115 *
116 * i.e.) No profiles configured results in always being included, whereas
117 * providing a list of profiles on an entry results only in that entry being
118 * included when a profile in the list is active.
119 *
120 * @param[in] activeProfiles - List of active system profiles
121 * @param[in] entryProfiles - List of the configuration entry's profiles
122 *
123 * @return Whether the configuration entry should be included or not
124 */
125bool checkEntry(const std::vector<std::string>& activeProfiles,
126 const std::vector<std::string>& entryProfiles);
127
128/**
Matthew Barth2dc5aba2020-08-04 14:23:34 -0500129 * @brief Get the delay(in seconds) to allow the fans to ramp up to the defined
130 * power on speed
131 *
132 * @param[in] bus - The dbus bus object
133 *
134 * @return Time to delay in seconds
135 * Amount of time to delay in seconds after a power on
136 */
Matthew Barth06764942021-03-04 09:28:40 -0600137const unsigned int getPowerOnDelay(sdbusplus::bus::bus& bus,
138 const sdeventplus::Event& event);
Matthew Barth2dc5aba2020-08-04 14:23:34 -0500139
Matthew Barth167d2dd2020-08-04 12:19:16 -0500140} // namespace phosphor::fan::control