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 19da4a5dd1SPatrick Venture #include "conf.hpp" 20e620656cSPatrick Venture #include "interfaces.hpp" 215c7cc545SPatrick Venture #include "pid/builder.hpp" 225c7cc545SPatrick Venture #include "pid/builderconfig.hpp" 23da4a5dd1SPatrick Venture #include "pid/pidthread.hpp" 24e620656cSPatrick Venture #include "pid/zone.hpp" 255e929093SPatrick Venture #include "sensors/builder.hpp" 265e929093SPatrick Venture #include "sensors/builderconfig.hpp" 27e620656cSPatrick Venture #include "sensors/manager.hpp" 28e620656cSPatrick Venture #include "threads/busthread.hpp" 29da4a5dd1SPatrick Venture #include "util.hpp" 30e620656cSPatrick Venture 31da4a5dd1SPatrick Venture #include <getopt.h> 32da4a5dd1SPatrick Venture 33da4a5dd1SPatrick Venture #include <chrono> 34da4a5dd1SPatrick Venture #include <experimental/any> 35da4a5dd1SPatrick Venture #include <iostream> 36da4a5dd1SPatrick Venture #include <map> 37da4a5dd1SPatrick Venture #include <memory> 38da4a5dd1SPatrick Venture #include <mutex> /* not yet used. */ 39da4a5dd1SPatrick Venture #include <sdbusplus/bus.hpp> 40da4a5dd1SPatrick Venture #include <thread> 41da4a5dd1SPatrick Venture #include <unordered_map> 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. */ 49f3252315SPatrick Venture extern std::map<std::string, struct SensorConfig> SensorConfig; 50e620656cSPatrick Venture /* The YAML converted PID list. */ 51e620656cSPatrick Venture extern std::map<int64_t, PIDConf> ZoneConfig; 52e620656cSPatrick Venture /* The YAML converted Zone configuration. */ 53f3252315SPatrick Venture extern std::map<int64_t, struct ZoneConfig> ZoneDetailsConfig; 54e620656cSPatrick Venture 55e620656cSPatrick Venture int main(int argc, char* argv[]) 56e620656cSPatrick Venture { 57e620656cSPatrick Venture int rc = 0; 58e620656cSPatrick Venture std::string configPath = ""; 59e620656cSPatrick Venture 60e620656cSPatrick Venture while (1) 61e620656cSPatrick Venture { 62da4a5dd1SPatrick Venture // clang-format off 63da4a5dd1SPatrick Venture static struct option long_options[] = { 64e620656cSPatrick Venture {"conf", required_argument, 0, 'c'}, 65e620656cSPatrick Venture {0, 0, 0, 0} 66e620656cSPatrick Venture }; 67da4a5dd1SPatrick Venture // clang-format on 68e620656cSPatrick Venture 69e620656cSPatrick Venture int option_index = 0; 70df766f25SPatrick Venture int c = getopt_long(argc, argv, "c:", long_options, &option_index); 71e620656cSPatrick Venture 72e620656cSPatrick Venture if (c == -1) 73e620656cSPatrick Venture { 74e620656cSPatrick Venture break; 75e620656cSPatrick Venture } 76e620656cSPatrick Venture 77e620656cSPatrick Venture switch (c) 78e620656cSPatrick Venture { 79e620656cSPatrick Venture case 'c': 80e620656cSPatrick Venture configPath = std::string{optarg}; 81e620656cSPatrick Venture break; 82e620656cSPatrick Venture default: 83e620656cSPatrick Venture /* skip garbage. */ 84e620656cSPatrick Venture continue; 85e620656cSPatrick Venture } 86e620656cSPatrick Venture } 87e620656cSPatrick Venture 88e620656cSPatrick Venture auto ModeControlBus = sdbusplus::bus::new_default(); 899f044415SPatrick Venture #if CONFIGURE_DBUS 907136a5aeSJames Feist { 917136a5aeSJames Feist dbus_configuration::init(ModeControlBus); 927136a5aeSJames Feist } 939f044415SPatrick Venture #endif 94fe75b193SPatrick Venture SensorManager mgmr; 955c7cc545SPatrick Venture std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones; 96e620656cSPatrick Venture 9708afbb25SGunnar Mills // Create a manager for the ModeBus because we own it. 98e620656cSPatrick Venture static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; 99e620656cSPatrick Venture sdbusplus::server::manager::manager(ModeControlBus, modeRoot); 100e620656cSPatrick Venture 101e620656cSPatrick Venture /* 102e620656cSPatrick Venture * When building the sensors, if any of the dbus passive ones aren't on the 103e620656cSPatrick Venture * bus, it'll fail immediately. 104e620656cSPatrick Venture */ 105e620656cSPatrick Venture if (configPath.length() > 0) 106e620656cSPatrick Venture { 107e620656cSPatrick Venture try 108e620656cSPatrick Venture { 109*7af157b1SPatrick Venture mgmr = buildSensorsFromConfig(configPath); 110*7af157b1SPatrick Venture zones = buildZonesFromConfig(configPath, mgmr, ModeControlBus); 111e620656cSPatrick Venture } 112e620656cSPatrick Venture catch (const std::exception& e) 113e620656cSPatrick Venture { 114e620656cSPatrick Venture std::cerr << "Failed during building: " << e.what() << "\n"; 115e620656cSPatrick Venture exit(EXIT_FAILURE); /* fatal error. */ 116e620656cSPatrick Venture } 117e620656cSPatrick Venture } 118e620656cSPatrick Venture else 119e620656cSPatrick Venture { 120*7af157b1SPatrick Venture mgmr = buildSensors(SensorConfig); 121*7af157b1SPatrick Venture zones = buildZones(ZoneConfig, ZoneDetailsConfig, mgmr, ModeControlBus); 122e620656cSPatrick Venture } 123e620656cSPatrick Venture 124e620656cSPatrick Venture if (0 == zones.size()) 125e620656cSPatrick Venture { 126e620656cSPatrick Venture std::cerr << "No zones defined, exiting.\n"; 127e620656cSPatrick Venture return rc; 128e620656cSPatrick Venture } 129e620656cSPatrick Venture 130e620656cSPatrick Venture /* 131e620656cSPatrick Venture * All sensors are managed by one manager, but each zone has a pointer to 132e620656cSPatrick Venture * it. 133e620656cSPatrick Venture */ 134e620656cSPatrick Venture 135fe75b193SPatrick Venture auto& HostSensorBus = mgmr.getHostBus(); 136fe75b193SPatrick Venture auto& PassiveListeningBus = mgmr.getPassiveBus(); 137e620656cSPatrick Venture 138e620656cSPatrick Venture std::cerr << "Starting threads\n"; 139e620656cSPatrick Venture 140e620656cSPatrick Venture /* TODO(venture): Ask SensorManager if we have any passive sensors. */ 141da4a5dd1SPatrick Venture struct ThreadParams p = {std::ref(PassiveListeningBus), ""}; 142e620656cSPatrick Venture std::thread l(BusThread, std::ref(p)); 143e620656cSPatrick Venture 144e620656cSPatrick Venture /* TODO(venture): Ask SensorManager if we have any host sensors. */ 145e620656cSPatrick Venture static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external"; 146da4a5dd1SPatrick Venture struct ThreadParams e = {std::ref(HostSensorBus), hostBus}; 147e620656cSPatrick Venture std::thread te(BusThread, std::ref(e)); 148e620656cSPatrick Venture 149e620656cSPatrick Venture static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl"; 150da4a5dd1SPatrick Venture struct ThreadParams m = {std::ref(ModeControlBus), modeBus}; 151e620656cSPatrick Venture std::thread tm(BusThread, std::ref(m)); 152e620656cSPatrick Venture 153e620656cSPatrick Venture std::vector<std::thread> zoneThreads; 154e620656cSPatrick Venture 155e620656cSPatrick Venture /* TODO(venture): This was designed to have one thread per zone, but really 156e620656cSPatrick Venture * it could have one thread for all the zones and iterate through each 157e620656cSPatrick Venture * sequentially as it goes -- and it'd probably be fast enough to do that, 158e620656cSPatrick Venture * however, a system isn't likely going to have more than a couple zones. 159e620656cSPatrick Venture * If it only has a couple zones, then this is fine. 160e620656cSPatrick Venture */ 1614a2dc4d8SPatrick Venture for (const auto& i : zones) 162e620656cSPatrick Venture { 163e620656cSPatrick Venture std::cerr << "pushing zone" << std::endl; 164*7af157b1SPatrick Venture zoneThreads.push_back(std::thread(pidControlThread, i.second.get())); 165e620656cSPatrick Venture } 166e620656cSPatrick Venture 167e620656cSPatrick Venture l.join(); 168e620656cSPatrick Venture te.join(); 169e620656cSPatrick Venture tm.join(); 170e620656cSPatrick Venture for (auto& t : zoneThreads) 171e620656cSPatrick Venture { 172e620656cSPatrick Venture t.join(); 173e620656cSPatrick Venture } 174e620656cSPatrick Venture 175e620656cSPatrick Venture return rc; 176e620656cSPatrick Venture } 177