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