blob: d202fea3689c0ad1f6a8691dceed291e40209898 [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"
22
23#include <unistd.h>
24
25#include <phosphor-logging/elog-errors.hpp>
26#include <phosphor-logging/elog.hpp>
27#include <phosphor-logging/log.hpp>
28#include <sdbusplus/bus.hpp>
29#include <xyz/openbmc_project/Common/error.hpp>
30
Matt Spinler57352a32017-04-10 14:48:35 -050031#include <algorithm>
Matthew Barth93af4192019-01-18 09:30:57 -060032#include <experimental/filesystem>
Matt Spinlere10416e2017-04-10 14:15:53 -050033
34namespace phosphor
35{
36namespace fan
37{
38namespace control
39{
40
Matt Spinleree7f6422017-05-09 11:03:14 -050041using namespace phosphor::logging;
Matthew Barth93af4192019-01-18 09:30:57 -060042namespace fs = std::experimental::filesystem;
Matt Spinleree7f6422017-05-09 11:03:14 -050043
Matthew Barth3e1bb272020-05-26 11:09:21 -050044constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
45constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Matt Spinleree7f6422017-05-09 11:03:14 -050046constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
47constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
48
Gunnar Millsf96b01e2017-06-02 16:32:19 -050049/**
50 * Check if a condition is true. Conditions are used to determine
51 * which fan zone to use.
52 *
53 * @param[in] bus - The D-Bus bus object
54 * @param[in] condition - The condition to check if true
55 * @return result - True if the condition is true
56 */
Brad Bishopf0b020f2018-11-21 10:42:34 -050057bool checkCondition(sdbusplus::bus::bus& bus, const Condition& c)
Gunnar Millsf96b01e2017-06-02 16:32:19 -050058{
59 auto& type = std::get<conditionTypePos>(c);
60 auto& properties = std::get<conditionPropertyListPos>(c);
61
62 for (auto& p : properties)
63 {
Matthew Barth803d35b2018-05-09 12:15:46 -050064 auto value = std::get<propertyValuePos>(p);
Gunnar Millsf96b01e2017-06-02 16:32:19 -050065
66 // TODO openbmc/openbmc#1769: Support more types than just getProperty.
67 if (type.compare("getProperty") == 0)
68 {
Matthew Barth803d35b2018-05-09 12:15:46 -050069 auto propertyValue = util::SDBusPlus::getProperty<decltype(value)>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050070 bus, std::get<propertyPathPos>(p),
71 std::get<propertyInterfacePos>(p),
72 std::get<propertyNamePos>(p));
Gunnar Millsf96b01e2017-06-02 16:32:19 -050073
74 if (value != propertyValue)
75 {
76 return false;
77 }
78 }
79 }
80 return true;
81}
82
Matthew Barth3e1bb272020-05-26 11:09:21 -050083// Note: Future code will check 'mode' before starting control algorithm
84Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event,
Matt Spinleree7f6422017-05-09 11:03:14 -050085 Mode mode) :
Matthew Barth93af4192019-01-18 09:30:57 -060086 _bus(bus),
87 _objMgr(bus, CONTROL_OBJPATH)
Matt Spinlere10416e2017-04-10 14:15:53 -050088{
Matthew Barth3e1bb272020-05-26 11:09:21 -050089 // Create the appropriate Zone objects based on the
90 // actual system configuration.
Matt Spinler57352a32017-04-10 14:48:35 -050091
Matthew Barth3e1bb272020-05-26 11:09:21 -050092 // Find the 1 ZoneGroup that meets all of its conditions
Matthew Barthd87f89f2020-07-30 10:41:32 -050093#ifndef CONTROL_USE_JSON
Matt Spinler57352a32017-04-10 14:48:35 -050094 for (auto& group : _zoneLayouts)
95 {
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) {
100 return checkCondition(bus, condition);
101 }))
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 }
Matthew Barthd87f89f2020-07-30 10:41:32 -0500118#endif
Matt Spinler57352a32017-04-10 14:48:35 -0500119
Matthew Barth93af4192019-01-18 09:30:57 -0600120 if (mode == Mode::control)
121 {
122 bus.request_name(CONTROL_BUSNAME);
123 }
Matt Spinleree7f6422017-05-09 11:03:14 -0500124}
125
Matt Spinleree7f6422017-05-09 11:03:14 -0500126void Manager::doInit()
127{
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500128 for (auto& z : _zones)
Matt Spinler57352a32017-04-10 14:48:35 -0500129 {
130 z.second->setFullSpeed();
131 }
Matthew Barthd87f89f2020-07-30 10:41:32 -0500132#ifndef CONTROL_USE_JSON
Matt Spinleree7f6422017-05-09 11:03:14 -0500133 auto delay = _powerOnDelay;
134 while (delay > 0)
135 {
136 delay = sleep(delay);
137 }
Matthew Barthd87f89f2020-07-30 10:41:32 -0500138#endif
Matthew Barth3e1bb272020-05-26 11:09:21 -0500139 util::SDBusPlus::callMethod(_bus, SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
140 SYSTEMD_INTERFACE, "StartUnit",
141 FAN_CONTROL_READY_TARGET, "replace");
Matt Spinleree7f6422017-05-09 11:03:14 -0500142}
143
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500144} // namespace control
145} // namespace fan
146} // namespace phosphor