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 "dbus/dbusconfiguration.hpp" 22 #include "interfaces.hpp" 23 #include "pid/builder.hpp" 24 #include "pid/buildjson.hpp" 25 #include "pid/pidloop.hpp" 26 #include "pid/tuning.hpp" 27 #include "pid/zone.hpp" 28 #include "sensors/builder.hpp" 29 #include "sensors/buildjson.hpp" 30 #include "sensors/manager.hpp" 31 #include "util.hpp" 32 33 #include <CLI/CLI.hpp> 34 #include <boost/asio/io_context.hpp> 35 #include <boost/asio/steady_timer.hpp> 36 #include <sdbusplus/asio/connection.hpp> 37 #include <sdbusplus/bus.hpp> 38 39 #include <chrono> 40 #include <filesystem> 41 #include <iostream> 42 #include <list> 43 #include <map> 44 #include <memory> 45 #include <thread> 46 #include <unordered_map> 47 #include <utility> 48 #include <vector> 49 50 namespace pid_control 51 { 52 53 /* The configuration converted sensor list. */ 54 std::map<std::string, struct conf::SensorConfig> sensorConfig = {}; 55 /* The configuration converted PID list. */ 56 std::map<int64_t, conf::PIDConf> zoneConfig = {}; 57 /* The configuration converted Zone configuration. */ 58 std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {}; 59 60 } // namespace pid_control 61 62 /** the swampd daemon will check for the existence of this file. */ 63 constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; 64 std::string configPath = ""; 65 66 /* async io context for operation */ 67 boost::asio::io_context io; 68 69 /* buses for system control */ 70 static sdbusplus::asio::connection modeControlBus(io); 71 static sdbusplus::asio::connection 72 hostBus(io, sdbusplus::bus::new_system().release()); 73 static sdbusplus::asio::connection 74 passiveBus(io, sdbusplus::bus::new_system().release()); 75 76 namespace pid_control 77 { 78 79 void restartControlLoops() 80 { 81 static SensorManager mgmr; 82 static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; 83 static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers; 84 85 for (const auto timer : timers) 86 { 87 timer->cancel(); 88 } 89 timers.clear(); 90 zones.clear(); 91 92 const std::string& path = 93 (configPath.length() > 0) ? configPath : jsonConfigurationPath; 94 95 if (std::filesystem::exists(path)) 96 { 97 /* 98 * When building the sensors, if any of the dbus passive ones aren't on 99 * the bus, it'll fail immediately. 100 */ 101 try 102 { 103 auto jsonData = parseValidateJson(path); 104 sensorConfig = buildSensorsFromJson(jsonData); 105 std::tie(zoneConfig, zoneDetailsConfig) = 106 buildPIDsFromJson(jsonData); 107 } 108 catch (const std::exception& e) 109 { 110 std::cerr << "Failed during building: " << e.what() << "\n"; 111 exit(EXIT_FAILURE); /* fatal error. */ 112 } 113 } 114 else 115 { 116 static boost::asio::steady_timer reloadTimer(io); 117 if (!dbus_configuration::init(modeControlBus, reloadTimer)) 118 { 119 return; // configuration not ready 120 } 121 } 122 123 mgmr = buildSensors(sensorConfig, passiveBus, hostBus); 124 zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); 125 126 if (0 == zones.size()) 127 { 128 std::cerr << "No zones defined, exiting.\n"; 129 std::exit(EXIT_FAILURE); 130 } 131 132 for (const auto& i : zones) 133 { 134 std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back( 135 std::make_shared<boost::asio::steady_timer>(io)); 136 std::cerr << "pushing zone " << i.first << "\n"; 137 pidControlLoop(i.second, timer); 138 } 139 } 140 141 void tryRestartControlLoops() 142 { 143 int count = 0; 144 for (count = 0; count <= 5; count++) 145 { 146 try 147 { 148 restartControlLoops(); 149 break; 150 } 151 catch (const std::exception& e) 152 { 153 std::cerr << count 154 << " Failed during restartControlLoops, try again: " 155 << e.what() << "\n"; 156 if (count >= 5) 157 { 158 throw std::runtime_error(e.what()); 159 } 160 } 161 std::this_thread::sleep_for(std::chrono::seconds(10)); 162 } 163 164 return; 165 } 166 167 } // namespace pid_control 168 169 int main(int argc, char* argv[]) 170 { 171 loggingPath = ""; 172 loggingEnabled = false; 173 tuningEnabled = false; 174 175 CLI::App app{"OpenBMC Fan Control Daemon"}; 176 177 app.add_option("-c,--conf", configPath, 178 "Optional parameter to specify configuration at run-time") 179 ->check(CLI::ExistingFile); 180 app.add_option("-l,--log", loggingPath, 181 "Optional parameter to specify logging folder") 182 ->check(CLI::ExistingDirectory); 183 app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning"); 184 185 CLI11_PARSE(app, argc, argv); 186 187 loggingEnabled = (!loggingPath.empty()); 188 189 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; 190 // Create a manager for the ModeBus because we own it. 191 sdbusplus::server::manager::manager( 192 static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot); 193 hostBus.request_name("xyz.openbmc_project.Hwmon.external"); 194 modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl"); 195 196 /* 197 * All sensors are managed by one manager, but each zone has a pointer to 198 * it. 199 */ 200 201 pid_control::tryRestartControlLoops(); 202 203 io.run(); 204 return 0; 205 } 206