blob: 27c3b476c83112d3f91ecc75dd8e2d203b9cb617 [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
Mike Cappsbf8e56f2022-06-29 14:23:07 -040020#include "dbus_paths.hpp"
Matthew Barth3e1bb272020-05-26 11:09:21 -050021#include "sdbusplus.hpp"
22#include "utility.hpp"
23
24#include <unistd.h>
25
26#include <phosphor-logging/elog-errors.hpp>
27#include <phosphor-logging/elog.hpp>
28#include <phosphor-logging/log.hpp>
29#include <sdbusplus/bus.hpp>
30#include <xyz/openbmc_project/Common/error.hpp>
31
Matt Spinler57352a32017-04-10 14:48:35 -050032#include <algorithm>
Matt Spinler2ea9a592022-04-08 14:36:22 -050033#include <filesystem>
Matt Spinlere10416e2017-04-10 14:15:53 -050034
35namespace phosphor
36{
37namespace fan
38{
39namespace control
40{
41
Matt Spinleree7f6422017-05-09 11:03:14 -050042using namespace phosphor::logging;
Matt Spinler2ea9a592022-04-08 14:36:22 -050043namespace fs = std::filesystem;
Matt Spinleree7f6422017-05-09 11:03:14 -050044
Matthew Barth3e1bb272020-05-26 11:09:21 -050045constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
46constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Matt Spinleree7f6422017-05-09 11:03:14 -050047constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
48constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
49
Gunnar Millsf96b01e2017-06-02 16:32:19 -050050/**
51 * Check if a condition is true. Conditions are used to determine
52 * which fan zone to use.
53 *
54 * @param[in] bus - The D-Bus bus object
55 * @param[in] condition - The condition to check if true
56 * @return result - True if the condition is true
57 */
Patrick Williamscb356d42022-07-22 19:26:53 -050058bool checkCondition(sdbusplus::bus_t& bus, const Condition& c)
Gunnar Millsf96b01e2017-06-02 16:32:19 -050059{
60 auto& type = std::get<conditionTypePos>(c);
61 auto& properties = std::get<conditionPropertyListPos>(c);
62
63 for (auto& p : properties)
64 {
Matthew Barth803d35b2018-05-09 12:15:46 -050065 auto value = std::get<propertyValuePos>(p);
Gunnar Millsf96b01e2017-06-02 16:32:19 -050066
67 // TODO openbmc/openbmc#1769: Support more types than just getProperty.
68 if (type.compare("getProperty") == 0)
69 {
Matthew Barth803d35b2018-05-09 12:15:46 -050070 auto propertyValue = util::SDBusPlus::getProperty<decltype(value)>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050071 bus, std::get<propertyPathPos>(p),
72 std::get<propertyInterfacePos>(p),
73 std::get<propertyNamePos>(p));
Gunnar Millsf96b01e2017-06-02 16:32:19 -050074
75 if (value != propertyValue)
76 {
77 return false;
78 }
79 }
80 }
81 return true;
82}
83
Matthew Barth3e1bb272020-05-26 11:09:21 -050084// Note: Future code will check 'mode' before starting control algorithm
Patrick Williamscb356d42022-07-22 19:26:53 -050085Manager::Manager(sdbusplus::bus_t& bus, const sdeventplus::Event& event,
Matt Spinleree7f6422017-05-09 11:03:14 -050086 Mode mode) :
Matthew Barth93af4192019-01-18 09:30:57 -060087 _bus(bus),
88 _objMgr(bus, CONTROL_OBJPATH)
Matt Spinlere10416e2017-04-10 14:15:53 -050089{
Matthew Barth3e1bb272020-05-26 11:09:21 -050090 // Create the appropriate Zone objects based on the
91 // actual system configuration.
Matthew Barthf8ae7a52021-03-05 10:23:43 -060092
Matthew Barth3e1bb272020-05-26 11:09:21 -050093 // Find the 1 ZoneGroup that meets all of its conditions
Matthew Barth413e4d02020-09-29 10:44:37 -050094 for (auto& group : _zoneLayouts)
Matt Spinler57352a32017-04-10 14:48:35 -050095 {
96 auto& conditions = std::get<conditionListPos>(group);
97
98 if (std::all_of(conditions.begin(), conditions.end(),
Matthew Barth3e1bb272020-05-26 11:09:21 -050099 [&bus](const auto& condition) {
Patrick Williams61b73292023-05-10 07:50:12 -0500100 return checkCondition(bus, condition);
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500101 }))
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500102 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500103 // Create a Zone object for each zone in this group
Matt Spinler57352a32017-04-10 14:48:35 -0500104 auto& zones = std::get<zoneListPos>(group);
105
106 for (auto& z : zones)
107 {
Matthew Barth93af4192019-01-18 09:30:57 -0600108 fs::path path{CONTROL_OBJPATH};
109 path /= std::to_string(std::get<zoneNumPos>(z));
Matt Spinler57352a32017-04-10 14:48:35 -0500110 _zones.emplace(std::get<zoneNumPos>(z),
Matthew Barth3e1bb272020-05-26 11:09:21 -0500111 std::make_unique<Zone>(mode, _bus, path.string(),
112 event, z));
Matt Spinler57352a32017-04-10 14:48:35 -0500113 }
114
115 break;
116 }
117 }
118
Matthew Barth93af4192019-01-18 09:30:57 -0600119 if (mode == Mode::control)
120 {
121 bus.request_name(CONTROL_BUSNAME);
122 }
Matt Spinleree7f6422017-05-09 11:03:14 -0500123}
124
Mike Cappsb2e9a4f2022-06-13 10:15:42 -0400125void Manager::doInit(const sdeventplus::Event& /*event*/)
Matt Spinleree7f6422017-05-09 11:03:14 -0500126{
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500127 for (auto& z : _zones)
Matt Spinler57352a32017-04-10 14:48:35 -0500128 {
129 z.second->setFullSpeed();
130 }
Matt Spinleree7f6422017-05-09 11:03:14 -0500131 auto delay = _powerOnDelay;
132 while (delay > 0)
133 {
134 delay = sleep(delay);
135 }
Matthew Barth2dc5aba2020-08-04 14:23:34 -0500136
Matthew Barth3e1bb272020-05-26 11:09:21 -0500137 util::SDBusPlus::callMethod(_bus, SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
138 SYSTEMD_INTERFACE, "StartUnit",
139 FAN_CONTROL_READY_TARGET, "replace");
Matt Spinleree7f6422017-05-09 11:03:14 -0500140}
141
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500142} // namespace control
143} // namespace fan
144} // namespace phosphor