1e620656cSPatrick Venture /** 2e620656cSPatrick Venture * Copyright 2017 Google Inc. 3e620656cSPatrick Venture * 4e620656cSPatrick Venture * Licensed under the Apache License, Version 2.0 (the "License"); 5e620656cSPatrick Venture * you may not use this file except in compliance with the License. 6e620656cSPatrick Venture * You may obtain a copy of the License at 7e620656cSPatrick Venture * 8e620656cSPatrick Venture * http://www.apache.org/licenses/LICENSE-2.0 9e620656cSPatrick Venture * 10e620656cSPatrick Venture * Unless required by applicable law or agreed to in writing, software 11e620656cSPatrick Venture * distributed under the License is distributed on an "AS IS" BASIS, 12e620656cSPatrick Venture * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e620656cSPatrick Venture * See the License for the specific language governing permissions and 14e620656cSPatrick Venture * limitations under the License. 15e620656cSPatrick Venture */ 16e620656cSPatrick Venture 177136a5aeSJames Feist #include "config.h" 18e620656cSPatrick Venture 19ba8ffa73SPatrick Venture #include "build/buildjson.hpp" 20da4a5dd1SPatrick Venture #include "conf.hpp" 21e620656cSPatrick Venture #include "interfaces.hpp" 225c7cc545SPatrick Venture #include "pid/builder.hpp" 23ba8ffa73SPatrick Venture #include "pid/buildjson.hpp" 24ce6a3f36SJames Feist #include "pid/pidloop.hpp" 25c32e3fc5SPatrick Venture #include "pid/tuning.hpp" 26e620656cSPatrick Venture #include "pid/zone.hpp" 275e929093SPatrick Venture #include "sensors/builder.hpp" 28ba8ffa73SPatrick Venture #include "sensors/buildjson.hpp" 29e620656cSPatrick Venture #include "sensors/manager.hpp" 30da4a5dd1SPatrick Venture #include "util.hpp" 31e620656cSPatrick Venture 32b5cc37ceSPatrick Venture #include <CLI/CLI.hpp> 33ce6a3f36SJames Feist #include <boost/asio/io_context.hpp> 34ce6a3f36SJames Feist #include <boost/asio/steady_timer.hpp> 35da4a5dd1SPatrick Venture #include <chrono> 36da4a5dd1SPatrick Venture #include <iostream> 37ce6a3f36SJames Feist #include <list> 38da4a5dd1SPatrick Venture #include <map> 39da4a5dd1SPatrick Venture #include <memory> 40ce6a3f36SJames Feist #include <sdbusplus/asio/connection.hpp> 41da4a5dd1SPatrick Venture #include <sdbusplus/bus.hpp> 42da4a5dd1SPatrick Venture #include <thread> 43da4a5dd1SPatrick Venture #include <unordered_map> 44ba8ffa73SPatrick Venture #include <utility> 45da4a5dd1SPatrick Venture #include <vector> 46e620656cSPatrick Venture 479f044415SPatrick Venture #if CONFIGURE_DBUS 489f044415SPatrick Venture #include "dbus/dbusconfiguration.hpp" 499f044415SPatrick Venture #endif 509f044415SPatrick Venture 51*1fe08952SJames Feist /* The configuration converted sensor list. */ 52f81f2886SJames Feist std::map<std::string, struct conf::SensorConfig> sensorConfig = {}; 53*1fe08952SJames Feist /* The configuration converted PID list. */ 54f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {}; 55*1fe08952SJames Feist /* The configuration converted Zone configuration. */ 56f81f2886SJames Feist std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {}; 57e620656cSPatrick Venture 58c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */ 59c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; 60e620656cSPatrick Venture std::string configPath = ""; 61e620656cSPatrick Venture 62*1fe08952SJames Feist /* async io context for operation */ 63*1fe08952SJames Feist boost::asio::io_context io; 64e620656cSPatrick Venture 65*1fe08952SJames Feist /* buses for system control */ 66*1fe08952SJames Feist static sdbusplus::asio::connection modeControlBus(io); 67*1fe08952SJames Feist static sdbusplus::asio::connection 68*1fe08952SJames Feist hostBus(io, sdbusplus::bus::new_system().release()); 69*1fe08952SJames Feist static sdbusplus::asio::connection 70*1fe08952SJames Feist passiveBus(io, sdbusplus::bus::new_system().release()); 71de79ee05SPatrick Venture 72*1fe08952SJames Feist void restartControlLoops() 73*1fe08952SJames Feist { 74*1fe08952SJames Feist static SensorManager mgmr; 75*1fe08952SJames Feist static std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones; 76*1fe08952SJames Feist static std::list<boost::asio::steady_timer> timers; 77e620656cSPatrick Venture 78*1fe08952SJames Feist timers.clear(); 794cb7c058SPatrick Venture 809f044415SPatrick Venture #if CONFIGURE_DBUS 81*1fe08952SJames Feist 82*1fe08952SJames Feist static boost::asio::steady_timer reloadTimer(io); 83*1fe08952SJames Feist if (!dbus_configuration::init(modeControlBus, reloadTimer)) 847136a5aeSJames Feist { 85*1fe08952SJames Feist return; // configuration not ready 867136a5aeSJames Feist } 87*1fe08952SJames Feist 8818b1311eSPatrick Venture #else 8918b1311eSPatrick Venture const std::string& path = 9018b1311eSPatrick Venture (configPath.length() > 0) ? configPath : jsonConfigurationPath; 91e620656cSPatrick Venture 92e620656cSPatrick Venture /* 93e620656cSPatrick Venture * When building the sensors, if any of the dbus passive ones aren't on the 94e620656cSPatrick Venture * bus, it'll fail immediately. 95e620656cSPatrick Venture */ 96e620656cSPatrick Venture try 97e620656cSPatrick Venture { 9818b1311eSPatrick Venture auto jsonData = parseValidateJson(path); 994cb7c058SPatrick Venture sensorConfig = buildSensorsFromJson(jsonData); 10018b1311eSPatrick Venture std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData); 101e620656cSPatrick Venture } 102e620656cSPatrick Venture catch (const std::exception& e) 103e620656cSPatrick Venture { 104e620656cSPatrick Venture std::cerr << "Failed during building: " << e.what() << "\n"; 105e620656cSPatrick Venture exit(EXIT_FAILURE); /* fatal error. */ 106e620656cSPatrick Venture } 10718b1311eSPatrick Venture #endif 1084cb7c058SPatrick Venture 109*1fe08952SJames Feist mgmr = buildSensors(sensorConfig, passiveBus, hostBus); 110*1fe08952SJames Feist zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); 111e620656cSPatrick Venture 112e620656cSPatrick Venture if (0 == zones.size()) 113e620656cSPatrick Venture { 114e620656cSPatrick Venture std::cerr << "No zones defined, exiting.\n"; 115*1fe08952SJames Feist std::exit(EXIT_FAILURE); 116e620656cSPatrick Venture } 117e620656cSPatrick Venture 118*1fe08952SJames Feist for (const auto& i : zones) 119*1fe08952SJames Feist { 120*1fe08952SJames Feist auto& timer = timers.emplace_back(io); 121*1fe08952SJames Feist std::cerr << "pushing zone " << i.first << "\n"; 122*1fe08952SJames Feist pidControlLoop(i.second.get(), timer); 123*1fe08952SJames Feist } 124*1fe08952SJames Feist } 125*1fe08952SJames Feist 126*1fe08952SJames Feist int main(int argc, char* argv[]) 127*1fe08952SJames Feist { 128*1fe08952SJames Feist loggingPath = ""; 129*1fe08952SJames Feist loggingEnabled = false; 130*1fe08952SJames Feist tuningEnabled = false; 131*1fe08952SJames Feist 132*1fe08952SJames Feist CLI::App app{"OpenBMC Fan Control Daemon"}; 133*1fe08952SJames Feist 134*1fe08952SJames Feist app.add_option("-c,--conf", configPath, 135*1fe08952SJames Feist "Optional parameter to specify configuration at run-time") 136*1fe08952SJames Feist ->check(CLI::ExistingFile); 137*1fe08952SJames Feist app.add_option("-l,--log", loggingPath, 138*1fe08952SJames Feist "Optional parameter to specify logging folder") 139*1fe08952SJames Feist ->check(CLI::ExistingDirectory); 140*1fe08952SJames Feist app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning"); 141*1fe08952SJames Feist 142*1fe08952SJames Feist loggingEnabled = (!loggingPath.empty()); 143*1fe08952SJames Feist 144*1fe08952SJames Feist CLI11_PARSE(app, argc, argv); 145*1fe08952SJames Feist 146*1fe08952SJames Feist static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; 147*1fe08952SJames Feist // Create a manager for the ModeBus because we own it. 148*1fe08952SJames Feist sdbusplus::server::manager::manager( 149*1fe08952SJames Feist static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot); 150*1fe08952SJames Feist hostBus.request_name("xyz.openbmc_project.Hwmon.external"); 151*1fe08952SJames Feist modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl"); 152*1fe08952SJames Feist 153e620656cSPatrick Venture /* 154e620656cSPatrick Venture * All sensors are managed by one manager, but each zone has a pointer to 155e620656cSPatrick Venture * it. 156e620656cSPatrick Venture */ 157e620656cSPatrick Venture 158*1fe08952SJames Feist restartControlLoops(); 159e620656cSPatrick Venture 160ce6a3f36SJames Feist io.run(); 161*1fe08952SJames Feist return 0; 162e620656cSPatrick Venture } 163