blob: a322c10a7aa42c96cd9f8b094572de0b2bbae1df [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 */
Matt Spinler57352a32017-04-10 14:48:35 -050016#include <algorithm>
Matt Spinleree7f6422017-05-09 11:03:14 -050017#include <phosphor-logging/log.hpp>
Dinesh Chinari618027a2017-06-26 23:26:50 -050018#include <phosphor-logging/elog.hpp>
19#include <phosphor-logging/elog-errors.hpp>
20#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinleree7f6422017-05-09 11:03:14 -050021#include <unistd.h>
Matt Spinlere10416e2017-04-10 14:15:53 -050022#include "manager.hpp"
Gunnar Millsf96b01e2017-06-02 16:32:19 -050023#include "utility.hpp"
Matthew Barth803d35b2018-05-09 12:15:46 -050024#include "sdbusplus.hpp"
Matt Spinlere10416e2017-04-10 14:15:53 -050025
26namespace phosphor
27{
28namespace fan
29{
30namespace control
31{
32
Matt Spinleree7f6422017-05-09 11:03:14 -050033using namespace phosphor::logging;
34
35constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
36constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
37constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
38constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
39
Gunnar Millsf96b01e2017-06-02 16:32:19 -050040/**
41 * Check if a condition is true. Conditions are used to determine
42 * which fan zone to use.
43 *
44 * @param[in] bus - The D-Bus bus object
45 * @param[in] condition - The condition to check if true
46 * @return result - True if the condition is true
47 */
48bool checkCondition(sdbusplus::bus::bus& bus, const auto& c)
49{
50 auto& type = std::get<conditionTypePos>(c);
51 auto& properties = std::get<conditionPropertyListPos>(c);
52
53 for (auto& p : properties)
54 {
Matthew Barth803d35b2018-05-09 12:15:46 -050055 auto value = std::get<propertyValuePos>(p);
Gunnar Millsf96b01e2017-06-02 16:32:19 -050056
57 // TODO openbmc/openbmc#1769: Support more types than just getProperty.
58 if (type.compare("getProperty") == 0)
59 {
Matthew Barth803d35b2018-05-09 12:15:46 -050060 auto propertyValue = util::SDBusPlus::getProperty<decltype(value)>(
61 bus,
62 std::get<propertyPathPos>(p),
63 std::get<propertyInterfacePos>(p),
64 std::get<propertyNamePos>(p));
Gunnar Millsf96b01e2017-06-02 16:32:19 -050065
66 if (value != propertyValue)
67 {
68 return false;
69 }
70 }
71 }
72 return true;
73}
74
75
Matt Spinleree7f6422017-05-09 11:03:14 -050076//Note: Future code will check 'mode' before starting control algorithm
77Manager::Manager(sdbusplus::bus::bus& bus,
Matthew Barth8600d9a2017-06-23 14:38:05 -050078 phosphor::fan::event::EventPtr& events,
Matt Spinleree7f6422017-05-09 11:03:14 -050079 Mode mode) :
Matt Spinlere10416e2017-04-10 14:15:53 -050080 _bus(bus)
81{
Matt Spinler57352a32017-04-10 14:48:35 -050082 //Create the appropriate Zone objects based on the
83 //actual system configuration.
84
85 //Find the 1 ZoneGroup that meets all of its conditions
86 for (auto& group : _zoneLayouts)
87 {
88 auto& conditions = std::get<conditionListPos>(group);
89
90 if (std::all_of(conditions.begin(), conditions.end(),
Gunnar Millsf96b01e2017-06-02 16:32:19 -050091 [&bus](const auto& condition)
92 {
93 return checkCondition(bus, condition);
94 }))
Matt Spinler57352a32017-04-10 14:48:35 -050095 {
96 //Create a Zone object for each zone in this group
97 auto& zones = std::get<zoneListPos>(group);
98
99 for (auto& z : zones)
100 {
101 _zones.emplace(std::get<zoneNumPos>(z),
Matthew Barth8600d9a2017-06-23 14:38:05 -0500102 std::make_unique<Zone>(mode, _bus, events, z));
Matt Spinler57352a32017-04-10 14:48:35 -0500103 }
104
105 break;
106 }
107 }
108
Matt Spinleree7f6422017-05-09 11:03:14 -0500109}
110
111
112void Manager::doInit()
113{
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500114 for (auto& z : _zones)
Matt Spinler57352a32017-04-10 14:48:35 -0500115 {
116 z.second->setFullSpeed();
117 }
Matt Spinleree7f6422017-05-09 11:03:14 -0500118
119 auto delay = _powerOnDelay;
120 while (delay > 0)
121 {
122 delay = sleep(delay);
123 }
124
125 startFanControlReadyTarget();
Matt Spinlere10416e2017-04-10 14:15:53 -0500126}
127
128
Matt Spinleree7f6422017-05-09 11:03:14 -0500129void Manager::startFanControlReadyTarget()
130{
131 auto method = _bus.new_method_call(SYSTEMD_SERVICE,
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500132 SYSTEMD_OBJ_PATH,
133 SYSTEMD_INTERFACE,
134 "StartUnit");
Matt Spinleree7f6422017-05-09 11:03:14 -0500135
136 method.append(FAN_CONTROL_READY_TARGET);
137 method.append("replace");
138
139 auto response = _bus.call(method);
140 if (response.is_method_error())
141 {
Matt Spinleree7f6422017-05-09 11:03:14 -0500142 log<level::ERR>("Failed to start fan control ready target");
Dinesh Chinari618027a2017-06-26 23:26:50 -0500143 elog<InternalFailure>();
Matt Spinleree7f6422017-05-09 11:03:14 -0500144 }
145}
146
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500147} // namespace control
148} // namespace fan
149} // namespace phosphor