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" 24da4a5dd1SPatrick Venture #include "pid/pidthread.hpp" 25e620656cSPatrick Venture #include "pid/zone.hpp" 265e929093SPatrick Venture #include "sensors/builder.hpp" 27ba8ffa73SPatrick Venture #include "sensors/buildjson.hpp" 28e620656cSPatrick Venture #include "sensors/manager.hpp" 29e620656cSPatrick Venture #include "threads/busthread.hpp" 30da4a5dd1SPatrick Venture #include "util.hpp" 31e620656cSPatrick Venture 32da4a5dd1SPatrick Venture #include <getopt.h> 33da4a5dd1SPatrick Venture 34da4a5dd1SPatrick Venture #include <chrono> 35da4a5dd1SPatrick Venture #include <iostream> 36da4a5dd1SPatrick Venture #include <map> 37da4a5dd1SPatrick Venture #include <memory> 38da4a5dd1SPatrick Venture #include <sdbusplus/bus.hpp> 39da4a5dd1SPatrick Venture #include <thread> 40da4a5dd1SPatrick Venture #include <unordered_map> 41ba8ffa73SPatrick Venture #include <utility> 42da4a5dd1SPatrick Venture #include <vector> 43e620656cSPatrick Venture 449f044415SPatrick Venture #if CONFIGURE_DBUS 459f044415SPatrick Venture #include "dbus/dbusconfiguration.hpp" 469f044415SPatrick Venture #endif 479f044415SPatrick Venture 48e620656cSPatrick Venture /* The YAML converted sensor list. */ 49c54fbd88SPatrick Venture extern std::map<std::string, struct SensorConfig> sensorConfig; 50e620656cSPatrick Venture /* The YAML converted PID list. */ 51c54fbd88SPatrick Venture extern std::map<int64_t, PIDConf> zoneConfig; 52e620656cSPatrick Venture /* The YAML converted Zone configuration. */ 53c54fbd88SPatrick Venture extern std::map<int64_t, struct ZoneConfig> zoneDetailsConfig; 54e620656cSPatrick Venture 55*c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */ 56*c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; 57*c19f5d4dSPatrick Venture 58e620656cSPatrick Venture int main(int argc, char* argv[]) 59e620656cSPatrick Venture { 60e620656cSPatrick Venture int rc = 0; 61e620656cSPatrick Venture std::string configPath = ""; 62e620656cSPatrick Venture 63e620656cSPatrick Venture while (1) 64e620656cSPatrick Venture { 65da4a5dd1SPatrick Venture // clang-format off 66da4a5dd1SPatrick Venture static struct option long_options[] = { 67e620656cSPatrick Venture {"conf", required_argument, 0, 'c'}, 68e620656cSPatrick Venture {0, 0, 0, 0} 69e620656cSPatrick Venture }; 70da4a5dd1SPatrick Venture // clang-format on 71e620656cSPatrick Venture 72e620656cSPatrick Venture int option_index = 0; 73df766f25SPatrick Venture int c = getopt_long(argc, argv, "c:", long_options, &option_index); 74e620656cSPatrick Venture 75e620656cSPatrick Venture if (c == -1) 76e620656cSPatrick Venture { 77e620656cSPatrick Venture break; 78e620656cSPatrick Venture } 79e620656cSPatrick Venture 80e620656cSPatrick Venture switch (c) 81e620656cSPatrick Venture { 82e620656cSPatrick Venture case 'c': 83e620656cSPatrick Venture configPath = std::string{optarg}; 84e620656cSPatrick Venture break; 85e620656cSPatrick Venture default: 86e620656cSPatrick Venture /* skip garbage. */ 87e620656cSPatrick Venture continue; 88e620656cSPatrick Venture } 89e620656cSPatrick Venture } 90e620656cSPatrick Venture 919fa90c19SJames Feist auto modeControlBus = sdbusplus::bus::new_system(); 929f044415SPatrick Venture #if CONFIGURE_DBUS 937136a5aeSJames Feist { 94c179d406SPatrick Venture dbus_configuration::init(modeControlBus); 957136a5aeSJames Feist } 969f044415SPatrick Venture #endif 97fe75b193SPatrick Venture SensorManager mgmr; 985c7cc545SPatrick Venture std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones; 99e620656cSPatrick Venture 10008afbb25SGunnar Mills // Create a manager for the ModeBus because we own it. 101e620656cSPatrick Venture static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; 102c179d406SPatrick Venture sdbusplus::server::manager::manager(modeControlBus, modeRoot); 103e620656cSPatrick Venture 104e620656cSPatrick Venture /* 105e620656cSPatrick Venture * When building the sensors, if any of the dbus passive ones aren't on the 106e620656cSPatrick Venture * bus, it'll fail immediately. 107e620656cSPatrick Venture */ 108e620656cSPatrick Venture if (configPath.length() > 0) 109e620656cSPatrick Venture { 110e620656cSPatrick Venture try 111e620656cSPatrick Venture { 112ba8ffa73SPatrick Venture auto jsonData = parseValidateJson(configPath); 113ba8ffa73SPatrick Venture mgmr = buildSensors(buildSensorsFromJson(jsonData)); 114ba8ffa73SPatrick Venture 115ba8ffa73SPatrick Venture std::map<int64_t, PIDConf> pidConfig; 116ba8ffa73SPatrick Venture std::map<int64_t, struct ZoneConfig> zoneConfig; 117ba8ffa73SPatrick Venture std::tie(pidConfig, zoneConfig) = buildPIDsFromJson(jsonData); 118ba8ffa73SPatrick Venture 119ba8ffa73SPatrick Venture zones = buildZones(pidConfig, zoneConfig, mgmr, modeControlBus); 120e620656cSPatrick Venture } 121e620656cSPatrick Venture catch (const std::exception& e) 122e620656cSPatrick Venture { 123e620656cSPatrick Venture std::cerr << "Failed during building: " << e.what() << "\n"; 124e620656cSPatrick Venture exit(EXIT_FAILURE); /* fatal error. */ 125e620656cSPatrick Venture } 126e620656cSPatrick Venture } 127e620656cSPatrick Venture else 128e620656cSPatrick Venture { 129c54fbd88SPatrick Venture mgmr = buildSensors(sensorConfig); 130c179d406SPatrick Venture zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); 131e620656cSPatrick Venture } 132e620656cSPatrick Venture 133e620656cSPatrick Venture if (0 == zones.size()) 134e620656cSPatrick Venture { 135e620656cSPatrick Venture std::cerr << "No zones defined, exiting.\n"; 136e620656cSPatrick Venture return rc; 137e620656cSPatrick Venture } 138e620656cSPatrick Venture 139e620656cSPatrick Venture /* 140e620656cSPatrick Venture * All sensors are managed by one manager, but each zone has a pointer to 141e620656cSPatrick Venture * it. 142e620656cSPatrick Venture */ 143e620656cSPatrick Venture 144c179d406SPatrick Venture auto& hostSensorBus = mgmr.getHostBus(); 145c179d406SPatrick Venture auto& passiveListeningBus = mgmr.getPassiveBus(); 146e620656cSPatrick Venture 147e620656cSPatrick Venture std::cerr << "Starting threads\n"; 148e620656cSPatrick Venture 149e620656cSPatrick Venture /* TODO(venture): Ask SensorManager if we have any passive sensors. */ 150c179d406SPatrick Venture struct ThreadParams p = {std::ref(passiveListeningBus), ""}; 151c179d406SPatrick Venture std::thread l(busThread, std::ref(p)); 152e620656cSPatrick Venture 153e620656cSPatrick Venture /* TODO(venture): Ask SensorManager if we have any host sensors. */ 154e620656cSPatrick Venture static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external"; 155c179d406SPatrick Venture struct ThreadParams e = {std::ref(hostSensorBus), hostBus}; 156c179d406SPatrick Venture std::thread te(busThread, std::ref(e)); 157e620656cSPatrick Venture 158e620656cSPatrick Venture static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl"; 159c179d406SPatrick Venture struct ThreadParams m = {std::ref(modeControlBus), modeBus}; 160c179d406SPatrick Venture std::thread tm(busThread, std::ref(m)); 161e620656cSPatrick Venture 162e620656cSPatrick Venture std::vector<std::thread> zoneThreads; 163e620656cSPatrick Venture 164e620656cSPatrick Venture /* TODO(venture): This was designed to have one thread per zone, but really 165e620656cSPatrick Venture * it could have one thread for all the zones and iterate through each 166e620656cSPatrick Venture * sequentially as it goes -- and it'd probably be fast enough to do that, 167e620656cSPatrick Venture * however, a system isn't likely going to have more than a couple zones. 168e620656cSPatrick Venture * If it only has a couple zones, then this is fine. 169e620656cSPatrick Venture */ 1704a2dc4d8SPatrick Venture for (const auto& i : zones) 171e620656cSPatrick Venture { 172e620656cSPatrick Venture std::cerr << "pushing zone" << std::endl; 1737af157b1SPatrick Venture zoneThreads.push_back(std::thread(pidControlThread, i.second.get())); 174e620656cSPatrick Venture } 175e620656cSPatrick Venture 176e620656cSPatrick Venture l.join(); 177e620656cSPatrick Venture te.join(); 178e620656cSPatrick Venture tm.join(); 179e620656cSPatrick Venture for (auto& t : zoneThreads) 180e620656cSPatrick Venture { 181e620656cSPatrick Venture t.join(); 182e620656cSPatrick Venture } 183e620656cSPatrick Venture 184e620656cSPatrick Venture return rc; 185e620656cSPatrick Venture } 186