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. */ 49*f81f2886SJames Feist std::map<std::string, struct conf::SensorConfig> sensorConfig = {}; 50e620656cSPatrick Venture /* The YAML converted PID list. */ 51*f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {}; 52e620656cSPatrick Venture /* The YAML converted Zone configuration. */ 53*f81f2886SJames Feist std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {}; 54e620656cSPatrick Venture 55c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */ 56c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; 57c19f5d4dSPatrick 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(); 924cb7c058SPatrick Venture static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; 934cb7c058SPatrick Venture // Create a manager for the ModeBus because we own it. 944cb7c058SPatrick Venture sdbusplus::server::manager::manager(modeControlBus, modeRoot); 954cb7c058SPatrick Venture 969f044415SPatrick Venture #if CONFIGURE_DBUS 977136a5aeSJames Feist { 98c179d406SPatrick Venture dbus_configuration::init(modeControlBus); 997136a5aeSJames Feist } 10018b1311eSPatrick Venture #else 10118b1311eSPatrick Venture const std::string& path = 10218b1311eSPatrick Venture (configPath.length() > 0) ? configPath : jsonConfigurationPath; 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 try 109e620656cSPatrick Venture { 11018b1311eSPatrick Venture auto jsonData = parseValidateJson(path); 1114cb7c058SPatrick Venture sensorConfig = buildSensorsFromJson(jsonData); 11218b1311eSPatrick Venture std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData); 113e620656cSPatrick Venture } 114e620656cSPatrick Venture catch (const std::exception& e) 115e620656cSPatrick Venture { 116e620656cSPatrick Venture std::cerr << "Failed during building: " << e.what() << "\n"; 117e620656cSPatrick Venture exit(EXIT_FAILURE); /* fatal error. */ 118e620656cSPatrick Venture } 11918b1311eSPatrick Venture #endif 1204cb7c058SPatrick Venture 1214cb7c058SPatrick Venture SensorManager mgmr = buildSensors(sensorConfig); 1224cb7c058SPatrick Venture std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones = 1234cb7c058SPatrick Venture buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); 124e620656cSPatrick Venture 125e620656cSPatrick Venture if (0 == zones.size()) 126e620656cSPatrick Venture { 127e620656cSPatrick Venture std::cerr << "No zones defined, exiting.\n"; 128e620656cSPatrick Venture return rc; 129e620656cSPatrick Venture } 130e620656cSPatrick Venture 131e620656cSPatrick Venture /* 132e620656cSPatrick Venture * All sensors are managed by one manager, but each zone has a pointer to 133e620656cSPatrick Venture * it. 134e620656cSPatrick Venture */ 135e620656cSPatrick Venture 136c179d406SPatrick Venture auto& hostSensorBus = mgmr.getHostBus(); 137c179d406SPatrick Venture auto& passiveListeningBus = mgmr.getPassiveBus(); 138e620656cSPatrick Venture 139e620656cSPatrick Venture std::cerr << "Starting threads\n"; 140e620656cSPatrick Venture 141e620656cSPatrick Venture /* TODO(venture): Ask SensorManager if we have any passive sensors. */ 142c179d406SPatrick Venture struct ThreadParams p = {std::ref(passiveListeningBus), ""}; 143c179d406SPatrick Venture std::thread l(busThread, std::ref(p)); 144e620656cSPatrick Venture 145e620656cSPatrick Venture /* TODO(venture): Ask SensorManager if we have any host sensors. */ 146e620656cSPatrick Venture static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external"; 147c179d406SPatrick Venture struct ThreadParams e = {std::ref(hostSensorBus), hostBus}; 148c179d406SPatrick Venture std::thread te(busThread, std::ref(e)); 149e620656cSPatrick Venture 150e620656cSPatrick Venture static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl"; 151c179d406SPatrick Venture struct ThreadParams m = {std::ref(modeControlBus), modeBus}; 152c179d406SPatrick Venture std::thread tm(busThread, std::ref(m)); 153e620656cSPatrick Venture 154e620656cSPatrick Venture std::vector<std::thread> zoneThreads; 155e620656cSPatrick Venture 156e620656cSPatrick Venture /* TODO(venture): This was designed to have one thread per zone, but really 157e620656cSPatrick Venture * it could have one thread for all the zones and iterate through each 158e620656cSPatrick Venture * sequentially as it goes -- and it'd probably be fast enough to do that, 159e620656cSPatrick Venture * however, a system isn't likely going to have more than a couple zones. 160e620656cSPatrick Venture * If it only has a couple zones, then this is fine. 161e620656cSPatrick Venture */ 1624a2dc4d8SPatrick Venture for (const auto& i : zones) 163e620656cSPatrick Venture { 164e620656cSPatrick Venture std::cerr << "pushing zone" << std::endl; 1657af157b1SPatrick Venture zoneThreads.push_back(std::thread(pidControlThread, i.second.get())); 166e620656cSPatrick Venture } 167e620656cSPatrick Venture 168e620656cSPatrick Venture l.join(); 169e620656cSPatrick Venture te.join(); 170e620656cSPatrick Venture tm.join(); 171e620656cSPatrick Venture for (auto& t : zoneThreads) 172e620656cSPatrick Venture { 173e620656cSPatrick Venture t.join(); 174e620656cSPatrick Venture } 175e620656cSPatrick Venture 176e620656cSPatrick Venture return rc; 177e620656cSPatrick Venture } 178