blob: ff8afefb653e028d30ca4110093d57099df4171a [file] [log] [blame]
Matt Spinlere10416e2017-04-10 14:15:53 -05001/**
2 * Copyright © 2017 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 */
Matthew Barth3e1bb272020-05-26 11:09:21 -050016#include "config.h"
17
18#include "manager.hpp"
19
20#include "sdbusplus.hpp"
21#include "utility.hpp"
Matthew Barth23ac24c2020-08-04 13:55:43 -050022#ifdef CONTROL_USE_JSON
23#include "json_parser.hpp"
24#endif
Matthew Barth3e1bb272020-05-26 11:09:21 -050025
26#include <unistd.h>
27
28#include <phosphor-logging/elog-errors.hpp>
29#include <phosphor-logging/elog.hpp>
30#include <phosphor-logging/log.hpp>
31#include <sdbusplus/bus.hpp>
32#include <xyz/openbmc_project/Common/error.hpp>
33
Matt Spinler57352a32017-04-10 14:48:35 -050034#include <algorithm>
Matthew Barth93af4192019-01-18 09:30:57 -060035#include <experimental/filesystem>
Matt Spinlere10416e2017-04-10 14:15:53 -050036
37namespace phosphor
38{
39namespace fan
40{
41namespace control
42{
43
Matt Spinleree7f6422017-05-09 11:03:14 -050044using namespace phosphor::logging;
Matthew Barth93af4192019-01-18 09:30:57 -060045namespace fs = std::experimental::filesystem;
Matt Spinleree7f6422017-05-09 11:03:14 -050046
Matthew Barth3e1bb272020-05-26 11:09:21 -050047constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
48constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Matt Spinleree7f6422017-05-09 11:03:14 -050049constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
50constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
51
Gunnar Millsf96b01e2017-06-02 16:32:19 -050052/**
53 * Check if a condition is true. Conditions are used to determine
54 * which fan zone to use.
55 *
56 * @param[in] bus - The D-Bus bus object
57 * @param[in] condition - The condition to check if true
58 * @return result - True if the condition is true
59 */
Brad Bishopf0b020f2018-11-21 10:42:34 -050060bool checkCondition(sdbusplus::bus::bus& bus, const Condition& c)
Gunnar Millsf96b01e2017-06-02 16:32:19 -050061{
62 auto& type = std::get<conditionTypePos>(c);
63 auto& properties = std::get<conditionPropertyListPos>(c);
64
65 for (auto& p : properties)
66 {
Matthew Barth803d35b2018-05-09 12:15:46 -050067 auto value = std::get<propertyValuePos>(p);
Gunnar Millsf96b01e2017-06-02 16:32:19 -050068
69 // TODO openbmc/openbmc#1769: Support more types than just getProperty.
70 if (type.compare("getProperty") == 0)
71 {
Matthew Barth803d35b2018-05-09 12:15:46 -050072 auto propertyValue = util::SDBusPlus::getProperty<decltype(value)>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050073 bus, std::get<propertyPathPos>(p),
74 std::get<propertyInterfacePos>(p),
75 std::get<propertyNamePos>(p));
Gunnar Millsf96b01e2017-06-02 16:32:19 -050076
77 if (value != propertyValue)
78 {
79 return false;
80 }
81 }
82 }
83 return true;
84}
85
Matthew Barth3e1bb272020-05-26 11:09:21 -050086// Note: Future code will check 'mode' before starting control algorithm
87Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event,
Matt Spinleree7f6422017-05-09 11:03:14 -050088 Mode mode) :
Matthew Barth93af4192019-01-18 09:30:57 -060089 _bus(bus),
90 _objMgr(bus, CONTROL_OBJPATH)
Matt Spinlere10416e2017-04-10 14:15:53 -050091{
Matthew Barth3e1bb272020-05-26 11:09:21 -050092 // Create the appropriate Zone objects based on the
93 // actual system configuration.
Matthew Barth23ac24c2020-08-04 13:55:43 -050094#ifdef CONTROL_USE_JSON
95 auto zoneLayouts = getZoneGroups(bus);
96#else
97 auto zoneLayouts = _zoneLayouts;
98#endif
Matt Spinler57352a32017-04-10 14:48:35 -050099
Matthew Barth3e1bb272020-05-26 11:09:21 -0500100 // Find the 1 ZoneGroup that meets all of its conditions
Matthew Barth23ac24c2020-08-04 13:55:43 -0500101 for (auto& group : zoneLayouts)
Matt Spinler57352a32017-04-10 14:48:35 -0500102 {
103 auto& conditions = std::get<conditionListPos>(group);
104
105 if (std::all_of(conditions.begin(), conditions.end(),
Matthew Barth3e1bb272020-05-26 11:09:21 -0500106 [&bus](const auto& condition) {
107 return checkCondition(bus, condition);
108 }))
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500109 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500110 // Create a Zone object for each zone in this group
Matt Spinler57352a32017-04-10 14:48:35 -0500111 auto& zones = std::get<zoneListPos>(group);
112
113 for (auto& z : zones)
114 {
Matthew Barth93af4192019-01-18 09:30:57 -0600115 fs::path path{CONTROL_OBJPATH};
116 path /= std::to_string(std::get<zoneNumPos>(z));
Matt Spinler57352a32017-04-10 14:48:35 -0500117 _zones.emplace(std::get<zoneNumPos>(z),
Matthew Barth3e1bb272020-05-26 11:09:21 -0500118 std::make_unique<Zone>(mode, _bus, path.string(),
119 event, z));
Matt Spinler57352a32017-04-10 14:48:35 -0500120 }
121
122 break;
123 }
124 }
125
Matthew Barth93af4192019-01-18 09:30:57 -0600126 if (mode == Mode::control)
127 {
128 bus.request_name(CONTROL_BUSNAME);
129 }
Matt Spinleree7f6422017-05-09 11:03:14 -0500130}
131
Matt Spinleree7f6422017-05-09 11:03:14 -0500132void Manager::doInit()
133{
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500134 for (auto& z : _zones)
Matt Spinler57352a32017-04-10 14:48:35 -0500135 {
136 z.second->setFullSpeed();
137 }
Matthew Barth2dc5aba2020-08-04 14:23:34 -0500138#ifdef CONTROL_USE_JSON
139 auto delay = getPowerOnDelay(_bus);
140#else
Matt Spinleree7f6422017-05-09 11:03:14 -0500141 auto delay = _powerOnDelay;
Matthew Barth2dc5aba2020-08-04 14:23:34 -0500142#endif
Matt Spinleree7f6422017-05-09 11:03:14 -0500143 while (delay > 0)
144 {
145 delay = sleep(delay);
146 }
Matthew Barth2dc5aba2020-08-04 14:23:34 -0500147
Matthew Barth3e1bb272020-05-26 11:09:21 -0500148 util::SDBusPlus::callMethod(_bus, SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
149 SYSTEMD_INTERFACE, "StartUnit",
150 FAN_CONTROL_READY_TARGET, "replace");
Matt Spinleree7f6422017-05-09 11:03:14 -0500151}
152
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500153} // namespace control
154} // namespace fan
155} // namespace phosphor