blob: 360239c60ae124b4e3fcb202d759d44cd8fe5ec6 [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>
Matthew Barth93af4192019-01-18 09:30:57 -060017#include <experimental/filesystem>
18#include <sdbusplus/bus.hpp>
Matt Spinleree7f6422017-05-09 11:03:14 -050019#include <phosphor-logging/log.hpp>
Dinesh Chinari618027a2017-06-26 23:26:50 -050020#include <phosphor-logging/elog.hpp>
21#include <phosphor-logging/elog-errors.hpp>
22#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinleree7f6422017-05-09 11:03:14 -050023#include <unistd.h>
Matthew Barth14cc0432019-01-16 15:06:41 -060024#include "config.h"
Matt Spinlere10416e2017-04-10 14:15:53 -050025#include "manager.hpp"
Gunnar Millsf96b01e2017-06-02 16:32:19 -050026#include "utility.hpp"
Matthew Barth803d35b2018-05-09 12:15:46 -050027#include "sdbusplus.hpp"
Matt Spinlere10416e2017-04-10 14:15:53 -050028
29namespace phosphor
30{
31namespace fan
32{
33namespace control
34{
35
Matt Spinleree7f6422017-05-09 11:03:14 -050036using namespace phosphor::logging;
Matthew Barth93af4192019-01-18 09:30:57 -060037namespace fs = std::experimental::filesystem;
Matt Spinleree7f6422017-05-09 11:03:14 -050038
39constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
40constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
41constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
42constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
43
Gunnar Millsf96b01e2017-06-02 16:32:19 -050044/**
45 * Check if a condition is true. Conditions are used to determine
46 * which fan zone to use.
47 *
48 * @param[in] bus - The D-Bus bus object
49 * @param[in] condition - The condition to check if true
50 * @return result - True if the condition is true
51 */
Brad Bishopf0b020f2018-11-21 10:42:34 -050052bool checkCondition(sdbusplus::bus::bus& bus, const Condition& c)
Gunnar Millsf96b01e2017-06-02 16:32:19 -050053{
54 auto& type = std::get<conditionTypePos>(c);
55 auto& properties = std::get<conditionPropertyListPos>(c);
56
57 for (auto& p : properties)
58 {
Matthew Barth803d35b2018-05-09 12:15:46 -050059 auto value = std::get<propertyValuePos>(p);
Gunnar Millsf96b01e2017-06-02 16:32:19 -050060
61 // TODO openbmc/openbmc#1769: Support more types than just getProperty.
62 if (type.compare("getProperty") == 0)
63 {
Matthew Barth803d35b2018-05-09 12:15:46 -050064 auto propertyValue = util::SDBusPlus::getProperty<decltype(value)>(
65 bus,
66 std::get<propertyPathPos>(p),
67 std::get<propertyInterfacePos>(p),
68 std::get<propertyNamePos>(p));
Gunnar Millsf96b01e2017-06-02 16:32:19 -050069
70 if (value != propertyValue)
71 {
72 return false;
73 }
74 }
75 }
76 return true;
77}
78
79
Matt Spinleree7f6422017-05-09 11:03:14 -050080//Note: Future code will check 'mode' before starting control algorithm
81Manager::Manager(sdbusplus::bus::bus& bus,
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070082 const sdeventplus::Event& event,
Matt Spinleree7f6422017-05-09 11:03:14 -050083 Mode mode) :
Matthew Barth93af4192019-01-18 09:30:57 -060084 _bus(bus),
85 _objMgr(bus, CONTROL_OBJPATH)
Matt Spinlere10416e2017-04-10 14:15:53 -050086{
Matt Spinler57352a32017-04-10 14:48:35 -050087 //Create the appropriate Zone objects based on the
88 //actual system configuration.
89
90 //Find the 1 ZoneGroup that meets all of its conditions
91 for (auto& group : _zoneLayouts)
92 {
93 auto& conditions = std::get<conditionListPos>(group);
94
95 if (std::all_of(conditions.begin(), conditions.end(),
Gunnar Millsf96b01e2017-06-02 16:32:19 -050096 [&bus](const auto& condition)
97 {
98 return checkCondition(bus, condition);
99 }))
Matt Spinler57352a32017-04-10 14:48:35 -0500100 {
101 //Create a Zone object for each zone in this group
102 auto& zones = std::get<zoneListPos>(group);
103
104 for (auto& z : zones)
105 {
Matthew Barth93af4192019-01-18 09:30:57 -0600106 fs::path path{CONTROL_OBJPATH};
107 path /= std::to_string(std::get<zoneNumPos>(z));
Matt Spinler57352a32017-04-10 14:48:35 -0500108 _zones.emplace(std::get<zoneNumPos>(z),
Matthew Barth93af4192019-01-18 09:30:57 -0600109 std::make_unique<Zone>(mode,
110 _bus,
111 path.string(),
112 event,
113 z));
Matt Spinler57352a32017-04-10 14:48:35 -0500114 }
115
116 break;
117 }
118 }
119
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
126
127void Manager::doInit()
128{
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500129 for (auto& z : _zones)
Matt Spinler57352a32017-04-10 14:48:35 -0500130 {
131 z.second->setFullSpeed();
132 }
Matt Spinleree7f6422017-05-09 11:03:14 -0500133
134 auto delay = _powerOnDelay;
135 while (delay > 0)
136 {
137 delay = sleep(delay);
138 }
139
Matthew Barth20291062018-05-09 13:06:28 -0500140 util::SDBusPlus::callMethod(
141 _bus,
142 SYSTEMD_SERVICE,
143 SYSTEMD_OBJ_PATH,
144 SYSTEMD_INTERFACE,
145 "StartUnit",
146 FAN_CONTROL_READY_TARGET,
147 "replace");
Matt Spinleree7f6422017-05-09 11:03:14 -0500148}
149
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500150} // namespace control
151} // namespace fan
152} // namespace phosphor