1 /** 2 * Copyright 2017 Google Inc. 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 */ 16 17 #include "config.h" 18 19 #include "build/buildjson.hpp" 20 #include "conf.hpp" 21 #include "interfaces.hpp" 22 #include "pid/builder.hpp" 23 #include "pid/buildjson.hpp" 24 #include "pid/pidloop.hpp" 25 #include "pid/tuning.hpp" 26 #include "pid/zone.hpp" 27 #include "sensors/builder.hpp" 28 #include "sensors/buildjson.hpp" 29 #include "sensors/manager.hpp" 30 #include "util.hpp" 31 32 #include <CLI/CLI.hpp> 33 #include <boost/asio/io_context.hpp> 34 #include <boost/asio/steady_timer.hpp> 35 #include <chrono> 36 #include <iostream> 37 #include <list> 38 #include <map> 39 #include <memory> 40 #include <sdbusplus/asio/connection.hpp> 41 #include <sdbusplus/bus.hpp> 42 #include <thread> 43 #include <unordered_map> 44 #include <utility> 45 #include <vector> 46 47 #if CONFIGURE_DBUS 48 #include "dbus/dbusconfiguration.hpp" 49 #endif 50 51 /* The YAML converted sensor list. */ 52 std::map<std::string, struct conf::SensorConfig> sensorConfig = {}; 53 /* The YAML converted PID list. */ 54 std::map<int64_t, conf::PIDConf> zoneConfig = {}; 55 /* The YAML converted Zone configuration. */ 56 std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {}; 57 58 /** the swampd daemon will check for the existence of this file. */ 59 constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; 60 61 int main(int argc, char* argv[]) 62 { 63 int rc = 0; 64 std::string configPath = ""; 65 tuningLoggingPath = ""; 66 67 CLI::App app{"OpenBMC Fan Control Daemon"}; 68 69 app.add_option("-c,--conf", configPath, 70 "Optional parameter to specify configuration at run-time") 71 ->check(CLI::ExistingFile); 72 app.add_option("-t,--tuning", tuningLoggingPath, 73 "Optional parameter to specify tuning logging path, and " 74 "enable tuning") 75 ->check(CLI::ExistingFile); 76 77 CLI11_PARSE(app, argc, argv); 78 79 tuningLoggingEnabled = (tuningLoggingPath.length() > 0); 80 81 auto modeControlBus = sdbusplus::bus::new_system(); 82 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; 83 // Create a manager for the ModeBus because we own it. 84 sdbusplus::server::manager::manager(modeControlBus, modeRoot); 85 86 #if CONFIGURE_DBUS 87 { 88 dbus_configuration::init(modeControlBus); 89 } 90 #else 91 const std::string& path = 92 (configPath.length() > 0) ? configPath : jsonConfigurationPath; 93 94 /* 95 * When building the sensors, if any of the dbus passive ones aren't on the 96 * bus, it'll fail immediately. 97 */ 98 try 99 { 100 auto jsonData = parseValidateJson(path); 101 sensorConfig = buildSensorsFromJson(jsonData); 102 std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData); 103 } 104 catch (const std::exception& e) 105 { 106 std::cerr << "Failed during building: " << e.what() << "\n"; 107 exit(EXIT_FAILURE); /* fatal error. */ 108 } 109 #endif 110 111 SensorManager mgmr = buildSensors(sensorConfig); 112 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones = 113 buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); 114 115 if (0 == zones.size()) 116 { 117 std::cerr << "No zones defined, exiting.\n"; 118 return rc; 119 } 120 121 /* 122 * All sensors are managed by one manager, but each zone has a pointer to 123 * it. 124 */ 125 126 auto& hostSensorBus = mgmr.getHostBus(); 127 auto& passiveListeningBus = mgmr.getPassiveBus(); 128 129 boost::asio::io_context io; 130 sdbusplus::asio::connection passiveBus(io, passiveListeningBus.release()); 131 132 sdbusplus::asio::connection hostBus(io, hostSensorBus.release()); 133 hostBus.request_name("xyz.openbmc_project.Hwmon.external"); 134 135 sdbusplus::asio::connection modeBus(io, modeControlBus.release()); 136 modeBus.request_name("xyz.openbmc_project.State.FanCtrl"); 137 138 std::list<boost::asio::steady_timer> timers; 139 140 for (const auto& i : zones) 141 { 142 auto& timer = timers.emplace_back(io); 143 std::cerr << "pushing zone" << std::endl; 144 pidControlLoop(i.second.get(), timer); 145 } 146 147 io.run(); 148 return rc; 149 } 150