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" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 24 | #include "pid/pidthread.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 25 | #include "pid/zone.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 26 | #include "sensors/builder.hpp" |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 27 | #include "sensors/buildjson.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 28 | #include "sensors/manager.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 29 | #include "threads/busthread.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 | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 32 | #include <getopt.h> |
| 33 | |
| 34 | #include <chrono> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 35 | #include <iostream> |
| 36 | #include <map> |
| 37 | #include <memory> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 38 | #include <sdbusplus/bus.hpp> |
| 39 | #include <thread> |
| 40 | #include <unordered_map> |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 41 | #include <utility> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 42 | #include <vector> |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 43 | |
Patrick Venture | 9f04441 | 2018-09-20 19:49:55 -0700 | [diff] [blame] | 44 | #if CONFIGURE_DBUS |
| 45 | #include "dbus/dbusconfiguration.hpp" |
| 46 | #endif |
| 47 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 48 | /* The YAML converted sensor list. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 49 | std::map<std::string, struct conf::SensorConfig> sensorConfig = {}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 50 | /* The YAML converted PID list. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 51 | std::map<int64_t, conf::PIDConf> zoneConfig = {}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 52 | /* The YAML converted Zone configuration. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 53 | std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 54 | |
Patrick Venture | c19f5d4 | 2019-02-14 10:59:47 -0800 | [diff] [blame] | 55 | /** the swampd daemon will check for the existence of this file. */ |
| 56 | constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; |
| 57 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 58 | int main(int argc, char* argv[]) |
| 59 | { |
| 60 | int rc = 0; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 61 | std::string configPath = ""; |
| 62 | |
| 63 | while (1) |
| 64 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 65 | // clang-format off |
| 66 | static struct option long_options[] = { |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 67 | {"conf", required_argument, 0, 'c'}, |
| 68 | {0, 0, 0, 0} |
| 69 | }; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 70 | // clang-format on |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 71 | |
| 72 | int option_index = 0; |
Patrick Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 73 | int c = getopt_long(argc, argv, "c:", long_options, &option_index); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 74 | |
| 75 | if (c == -1) |
| 76 | { |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | switch (c) |
| 81 | { |
| 82 | case 'c': |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 83 | configPath = std::string{optarg}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 84 | break; |
| 85 | default: |
| 86 | /* skip garbage. */ |
| 87 | continue; |
| 88 | } |
| 89 | } |
| 90 | |
James Feist | 9fa90c1 | 2019-01-11 15:35:22 -0800 | [diff] [blame] | 91 | auto modeControlBus = sdbusplus::bus::new_system(); |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 92 | static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; |
| 93 | // Create a manager for the ModeBus because we own it. |
| 94 | sdbusplus::server::manager::manager(modeControlBus, modeRoot); |
| 95 | |
Patrick Venture | 9f04441 | 2018-09-20 19:49:55 -0700 | [diff] [blame] | 96 | #if CONFIGURE_DBUS |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 97 | { |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 98 | dbus_configuration::init(modeControlBus); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 99 | } |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 100 | #else |
| 101 | const std::string& path = |
| 102 | (configPath.length() > 0) ? configPath : jsonConfigurationPath; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * When building the sensors, if any of the dbus passive ones aren't on the |
| 106 | * bus, it'll fail immediately. |
| 107 | */ |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 108 | try |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 109 | { |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 110 | auto jsonData = parseValidateJson(path); |
| 111 | sensorConfig = buildSensorsFromJson(jsonData); |
| 112 | std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 113 | } |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 114 | catch (const std::exception& e) |
| 115 | { |
| 116 | std::cerr << "Failed during building: " << e.what() << "\n"; |
| 117 | exit(EXIT_FAILURE); /* fatal error. */ |
| 118 | } |
| 119 | #endif |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 120 | |
| 121 | SensorManager mgmr = buildSensors(sensorConfig); |
| 122 | std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones = |
| 123 | buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 124 | |
| 125 | if (0 == zones.size()) |
| 126 | { |
| 127 | std::cerr << "No zones defined, exiting.\n"; |
| 128 | return rc; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * All sensors are managed by one manager, but each zone has a pointer to |
| 133 | * it. |
| 134 | */ |
| 135 | |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 136 | auto& hostSensorBus = mgmr.getHostBus(); |
| 137 | auto& passiveListeningBus = mgmr.getPassiveBus(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 138 | |
| 139 | std::cerr << "Starting threads\n"; |
| 140 | |
| 141 | /* TODO(venture): Ask SensorManager if we have any passive sensors. */ |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 142 | struct ThreadParams p = {std::ref(passiveListeningBus), ""}; |
| 143 | std::thread l(busThread, std::ref(p)); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 144 | |
| 145 | /* TODO(venture): Ask SensorManager if we have any host sensors. */ |
| 146 | static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external"; |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 147 | struct ThreadParams e = {std::ref(hostSensorBus), hostBus}; |
| 148 | std::thread te(busThread, std::ref(e)); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 149 | |
| 150 | static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl"; |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 151 | struct ThreadParams m = {std::ref(modeControlBus), modeBus}; |
| 152 | std::thread tm(busThread, std::ref(m)); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 153 | |
| 154 | std::vector<std::thread> zoneThreads; |
| 155 | |
| 156 | /* TODO(venture): This was designed to have one thread per zone, but really |
| 157 | * it could have one thread for all the zones and iterate through each |
| 158 | * sequentially as it goes -- and it'd probably be fast enough to do that, |
| 159 | * however, a system isn't likely going to have more than a couple zones. |
| 160 | * If it only has a couple zones, then this is fine. |
| 161 | */ |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 162 | for (const auto& i : zones) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 163 | { |
| 164 | std::cerr << "pushing zone" << std::endl; |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 165 | zoneThreads.push_back(std::thread(pidControlThread, i.second.get())); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | l.join(); |
| 169 | te.join(); |
| 170 | tm.join(); |
| 171 | for (auto& t : zoneThreads) |
| 172 | { |
| 173 | t.join(); |
| 174 | } |
| 175 | |
| 176 | return rc; |
| 177 | } |