1e10416ecSMatt Spinler /** 2e10416ecSMatt Spinler * Copyright © 2017 IBM Corporation 3e10416ecSMatt Spinler * 4e10416ecSMatt Spinler * Licensed under the Apache License, Version 2.0 (the "License"); 5e10416ecSMatt Spinler * you may not use this file except in compliance with the License. 6e10416ecSMatt Spinler * You may obtain a copy of the License at 7e10416ecSMatt Spinler * 8e10416ecSMatt Spinler * http://www.apache.org/licenses/LICENSE-2.0 9e10416ecSMatt Spinler * 10e10416ecSMatt Spinler * Unless required by applicable law or agreed to in writing, software 11e10416ecSMatt Spinler * distributed under the License is distributed on an "AS IS" BASIS, 12e10416ecSMatt Spinler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e10416ecSMatt Spinler * See the License for the specific language governing permissions and 14e10416ecSMatt Spinler * limitations under the License. 15e10416ecSMatt Spinler */ 1657352a31SMatt Spinler #include <algorithm> 17*93af4194SMatthew Barth #include <experimental/filesystem> 18*93af4194SMatthew Barth #include <sdbusplus/bus.hpp> 19ee7f6428SMatt Spinler #include <phosphor-logging/log.hpp> 20618027abSDinesh Chinari #include <phosphor-logging/elog.hpp> 21618027abSDinesh Chinari #include <phosphor-logging/elog-errors.hpp> 22618027abSDinesh Chinari #include <xyz/openbmc_project/Common/error.hpp> 23ee7f6428SMatt Spinler #include <unistd.h> 2414cc043fSMatthew Barth #include "config.h" 25e10416ecSMatt Spinler #include "manager.hpp" 26f96b01e2SGunnar Mills #include "utility.hpp" 27803d35bcSMatthew Barth #include "sdbusplus.hpp" 28e10416ecSMatt Spinler 29e10416ecSMatt Spinler namespace phosphor 30e10416ecSMatt Spinler { 31e10416ecSMatt Spinler namespace fan 32e10416ecSMatt Spinler { 33e10416ecSMatt Spinler namespace control 34e10416ecSMatt Spinler { 35e10416ecSMatt Spinler 36ee7f6428SMatt Spinler using namespace phosphor::logging; 37*93af4194SMatthew Barth namespace fs = std::experimental::filesystem; 38ee7f6428SMatt Spinler 39ee7f6428SMatt Spinler constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; 40ee7f6428SMatt Spinler constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; 41ee7f6428SMatt Spinler constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; 42ee7f6428SMatt Spinler constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target"; 43ee7f6428SMatt Spinler 44f96b01e2SGunnar Mills /** 45f96b01e2SGunnar Mills * Check if a condition is true. Conditions are used to determine 46f96b01e2SGunnar Mills * which fan zone to use. 47f96b01e2SGunnar Mills * 48f96b01e2SGunnar Mills * @param[in] bus - The D-Bus bus object 49f96b01e2SGunnar Mills * @param[in] condition - The condition to check if true 50f96b01e2SGunnar Mills * @return result - True if the condition is true 51f96b01e2SGunnar Mills */ 52f0b020fbSBrad Bishop bool checkCondition(sdbusplus::bus::bus& bus, const Condition& c) 53f96b01e2SGunnar Mills { 54f96b01e2SGunnar Mills auto& type = std::get<conditionTypePos>(c); 55f96b01e2SGunnar Mills auto& properties = std::get<conditionPropertyListPos>(c); 56f96b01e2SGunnar Mills 57f96b01e2SGunnar Mills for (auto& p : properties) 58f96b01e2SGunnar Mills { 59803d35bcSMatthew Barth auto value = std::get<propertyValuePos>(p); 60f96b01e2SGunnar Mills 61f96b01e2SGunnar Mills // TODO openbmc/openbmc#1769: Support more types than just getProperty. 62f96b01e2SGunnar Mills if (type.compare("getProperty") == 0) 63f96b01e2SGunnar Mills { 64803d35bcSMatthew Barth auto propertyValue = util::SDBusPlus::getProperty<decltype(value)>( 65803d35bcSMatthew Barth bus, 66f96b01e2SGunnar Mills std::get<propertyPathPos>(p), 67f96b01e2SGunnar Mills std::get<propertyInterfacePos>(p), 68803d35bcSMatthew Barth std::get<propertyNamePos>(p)); 69f96b01e2SGunnar Mills 70f96b01e2SGunnar Mills if (value != propertyValue) 71f96b01e2SGunnar Mills { 72f96b01e2SGunnar Mills return false; 73f96b01e2SGunnar Mills } 74f96b01e2SGunnar Mills } 75f96b01e2SGunnar Mills } 76f96b01e2SGunnar Mills return true; 77f96b01e2SGunnar Mills } 78f96b01e2SGunnar Mills 79f96b01e2SGunnar Mills 80ee7f6428SMatt Spinler //Note: Future code will check 'mode' before starting control algorithm 81ee7f6428SMatt Spinler Manager::Manager(sdbusplus::bus::bus& bus, 821cfc2f11SWilliam A. Kennington III const sdeventplus::Event& event, 83ee7f6428SMatt Spinler Mode mode) : 84*93af4194SMatthew Barth _bus(bus), 85*93af4194SMatthew Barth _objMgr(bus, CONTROL_OBJPATH) 86e10416ecSMatt Spinler { 8757352a31SMatt Spinler //Create the appropriate Zone objects based on the 8857352a31SMatt Spinler //actual system configuration. 8957352a31SMatt Spinler 9057352a31SMatt Spinler //Find the 1 ZoneGroup that meets all of its conditions 9157352a31SMatt Spinler for (auto& group : _zoneLayouts) 9257352a31SMatt Spinler { 9357352a31SMatt Spinler auto& conditions = std::get<conditionListPos>(group); 9457352a31SMatt Spinler 9557352a31SMatt Spinler if (std::all_of(conditions.begin(), conditions.end(), 96f96b01e2SGunnar Mills [&bus](const auto& condition) 9757352a31SMatt Spinler { 98f96b01e2SGunnar Mills return checkCondition(bus, condition); 9957352a31SMatt Spinler })) 10057352a31SMatt Spinler { 10157352a31SMatt Spinler //Create a Zone object for each zone in this group 10257352a31SMatt Spinler auto& zones = std::get<zoneListPos>(group); 10357352a31SMatt Spinler 10457352a31SMatt Spinler for (auto& z : zones) 10557352a31SMatt Spinler { 106*93af4194SMatthew Barth fs::path path{CONTROL_OBJPATH}; 107*93af4194SMatthew Barth path /= std::to_string(std::get<zoneNumPos>(z)); 10857352a31SMatt Spinler _zones.emplace(std::get<zoneNumPos>(z), 109*93af4194SMatthew Barth std::make_unique<Zone>(mode, 110*93af4194SMatthew Barth _bus, 111*93af4194SMatthew Barth path.string(), 112*93af4194SMatthew Barth event, 113*93af4194SMatthew Barth z)); 11457352a31SMatt Spinler } 11557352a31SMatt Spinler 11657352a31SMatt Spinler break; 11757352a31SMatt Spinler } 11857352a31SMatt Spinler } 11957352a31SMatt Spinler 120*93af4194SMatthew Barth if (mode == Mode::control) 121*93af4194SMatthew Barth { 12214cc043fSMatthew Barth bus.request_name(CONTROL_BUSNAME); 123ee7f6428SMatt Spinler } 124*93af4194SMatthew Barth } 125ee7f6428SMatt Spinler 126ee7f6428SMatt Spinler 127ee7f6428SMatt Spinler void Manager::doInit() 128ee7f6428SMatt Spinler { 12957352a31SMatt Spinler for (auto& z : _zones) 13057352a31SMatt Spinler { 13157352a31SMatt Spinler z.second->setFullSpeed(); 13257352a31SMatt Spinler } 133ee7f6428SMatt Spinler 134ee7f6428SMatt Spinler auto delay = _powerOnDelay; 135ee7f6428SMatt Spinler while (delay > 0) 136ee7f6428SMatt Spinler { 137ee7f6428SMatt Spinler delay = sleep(delay); 138e10416ecSMatt Spinler } 139e10416ecSMatt Spinler 1402029106aSMatthew Barth util::SDBusPlus::callMethod( 1412029106aSMatthew Barth _bus, 1422029106aSMatthew Barth SYSTEMD_SERVICE, 143ee7f6428SMatt Spinler SYSTEMD_OBJ_PATH, 144ee7f6428SMatt Spinler SYSTEMD_INTERFACE, 1452029106aSMatthew Barth "StartUnit", 1462029106aSMatthew Barth FAN_CONTROL_READY_TARGET, 1472029106aSMatthew Barth "replace"); 148ee7f6428SMatt Spinler } 149e10416ecSMatt Spinler 150f96b01e2SGunnar Mills } // namespace control 151f96b01e2SGunnar Mills } // namespace fan 152f96b01e2SGunnar Mills } // namespace phosphor 153