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