blob: 7c08875fb4e49bae25b496f84b7c70e271e954b5 [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 Barth2331d182020-08-18 15:00:27 -050045 * @brief Load the configuration of a given JSON class object type
46 *
47 * @param[in] bus - The dbus bus object
48 * @param[in] isOptional - JSON configuration file is optional or not
49 * Defaults to false
50 *
51 * @return Map of configuration entries
Matthew Barthce957262020-08-27 10:35:57 -050052 * Map of configuration keys to their corresponding configuration object
Matthew Barth2331d182020-08-18 15:00:27 -050053 */
54template <typename T>
Matthew Barthce957262020-08-27 10:35:57 -050055std::map<configKey, std::unique_ptr<T>> getConfig(sdbusplus::bus::bus& bus,
56 bool isOptional = false)
Matthew Barth2331d182020-08-18 15:00:27 -050057{
Matthew Barthce957262020-08-27 10:35:57 -050058 std::map<configKey, std::unique_ptr<T>> config;
Matthew Barth2331d182020-08-18 15:00:27 -050059
60 auto confFile = fan::JsonConfig::getConfFile(bus, confAppName,
61 T::confFileName, isOptional);
62 if (!confFile.empty())
63 {
64 for (const auto& entry : fan::JsonConfig::load(confFile))
65 {
66 auto obj = std::make_unique<T>(bus, entry);
Matthew Barthce957262020-08-27 10:35:57 -050067 config.emplace(std::make_pair(obj->getName(), obj->getProfiles()),
68 std::move(obj));
Matthew Barth2331d182020-08-18 15:00:27 -050069 }
70 }
Matthew Barth2331d182020-08-18 15:00:27 -050071 return config;
72}
73
74/**
Matthew Barth413e4d02020-09-29 10:44:37 -050075 * @brief Helper function to determine when a configuration entry is included
76 * based on the list of active profiles and its list of profiles
77 *
78 * A configuration entry may include a list of profiles that when any one of
79 * the profiles are active, the entry should be included. When the list of
80 * profiles for a configuration entry is empty, that results in always
81 * including the entry. An empty list of active profiles results in including
82 * only those entries configured without a list of profiles.
83 *
84 * i.e.) No profiles configured results in always being included, whereas
85 * providing a list of profiles on an entry results only in that entry being
86 * included when a profile in the list is active.
87 *
88 * @param[in] activeProfiles - List of active system profiles
89 * @param[in] entryProfiles - List of the configuration entry's profiles
90 *
91 * @return Whether the configuration entry should be included or not
92 */
93bool checkEntry(const std::vector<std::string>& activeProfiles,
94 const std::vector<std::string>& entryProfiles);
95
96/**
Matthew Barth23ac24c2020-08-04 13:55:43 -050097 * @brief Get the configuration definitions for zone groups
98 *
99 * @param[in] bus - The dbus bus object
100 *
101 * @return List of zone group objects
102 * Generated list of zone groups including their control configurations
103 */
104const std::vector<ZoneGroup> getZoneGroups(sdbusplus::bus::bus& bus);
105
Matthew Barth2dc5aba2020-08-04 14:23:34 -0500106/**
107 * @brief Get the delay(in seconds) to allow the fans to ramp up to the defined
108 * power on speed
109 *
110 * @param[in] bus - The dbus bus object
111 *
112 * @return Time to delay in seconds
113 * Amount of time to delay in seconds after a power on
114 */
115const unsigned int getPowerOnDelay(sdbusplus::bus::bus& bus);
116
Matthew Barth167d2dd2020-08-04 12:19:16 -0500117} // namespace phosphor::fan::control