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