blob: e2672e415ee87607ecbabd50ed7956c388d6eb99 [file] [log] [blame]
Patrick Venturee6206562018-03-08 15:36:53 -08001/**
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 Feist7136a5a2018-07-19 09:52:05 -070017#include "config.h"
Patrick Venturee6206562018-03-08 15:36:53 -080018
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070019#include "conf.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080020#include "interfaces.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070021#include "pid/builder.hpp"
22#include "pid/builderconfig.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023#include "pid/pidthread.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080024#include "pid/zone.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070025#include "sensors/builder.hpp"
26#include "sensors/builderconfig.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080027#include "sensors/manager.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080028#include "threads/busthread.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029#include "util.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080030
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031#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 Venturee6206562018-03-08 15:36:53 -080043
Patrick Venture9f044412018-09-20 19:49:55 -070044#if CONFIGURE_DBUS
45#include "dbus/dbusconfiguration.hpp"
46#endif
47
Patrick Venturee6206562018-03-08 15:36:53 -080048/* The YAML converted sensor list. */
49extern std::map<std::string, struct sensor> SensorConfig;
50/* The YAML converted PID list. */
51extern std::map<int64_t, PIDConf> ZoneConfig;
52/* The YAML converted Zone configuration. */
53extern std::map<int64_t, struct zone> ZoneDetailsConfig;
54
55int main(int argc, char* argv[])
56{
57 int rc = 0;
58 int c;
59 std::string configPath = "";
60
61 while (1)
62 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070063 // clang-format off
64 static struct option long_options[] = {
Patrick Venturee6206562018-03-08 15:36:53 -080065 {"conf", required_argument, 0, 'c'},
66 {0, 0, 0, 0}
67 };
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070068 // clang-format on
Patrick Venturee6206562018-03-08 15:36:53 -080069
70 int option_index = 0;
71 c = getopt_long(argc, argv, "c:", long_options, &option_index);
72
73 if (c == -1)
74 {
75 break;
76 }
77
78 switch (c)
79 {
80 case 'c':
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070081 configPath = std::string{optarg};
Patrick Venturee6206562018-03-08 15:36:53 -080082 break;
83 default:
84 /* skip garbage. */
85 continue;
86 }
87 }
88
89 auto ModeControlBus = sdbusplus::bus::new_default();
Patrick Venture9f044412018-09-20 19:49:55 -070090#if CONFIGURE_DBUS
James Feist7136a5a2018-07-19 09:52:05 -070091 {
92 dbus_configuration::init(ModeControlBus);
93 }
Patrick Venture9f044412018-09-20 19:49:55 -070094#endif
Patrick Venturefe75b192018-06-08 11:19:43 -070095 SensorManager mgmr;
Patrick Venture5c7cc542018-06-11 14:29:38 -070096 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
Patrick Venturee6206562018-03-08 15:36:53 -080097
Gunnar Mills08afbb22018-06-14 08:50:53 -050098 // Create a manager for the ModeBus because we own it.
Patrick Venturee6206562018-03-08 15:36:53 -080099 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
100 sdbusplus::server::manager::manager(ModeControlBus, modeRoot);
101
102 /*
103 * When building the sensors, if any of the dbus passive ones aren't on the
104 * bus, it'll fail immediately.
105 */
106 if (configPath.length() > 0)
107 {
108 try
109 {
110 mgmr = BuildSensorsFromConfig(configPath);
111 zones = BuildZonesFromConfig(configPath, mgmr, ModeControlBus);
112 }
113 catch (const std::exception& e)
114 {
115 std::cerr << "Failed during building: " << e.what() << "\n";
116 exit(EXIT_FAILURE); /* fatal error. */
117 }
118 }
119 else
120 {
121 mgmr = BuildSensors(SensorConfig);
122 zones = BuildZones(ZoneConfig, ZoneDetailsConfig, mgmr, ModeControlBus);
123 }
124
125 if (0 == zones.size())
126 {
127 std::cerr << "No zones defined, exiting.\n";
128 return rc;
129 }
130
131 /*
132 * All sensors are managed by one manager, but each zone has a pointer to
133 * it.
134 */
135
Patrick Venturefe75b192018-06-08 11:19:43 -0700136 auto& HostSensorBus = mgmr.getHostBus();
137 auto& PassiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800138
139 std::cerr << "Starting threads\n";
140
141 /* TODO(venture): Ask SensorManager if we have any passive sensors. */
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700142 struct ThreadParams p = {std::ref(PassiveListeningBus), ""};
Patrick Venturee6206562018-03-08 15:36:53 -0800143 std::thread l(BusThread, std::ref(p));
144
145 /* TODO(venture): Ask SensorManager if we have any host sensors. */
146 static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700147 struct ThreadParams e = {std::ref(HostSensorBus), hostBus};
Patrick Venturee6206562018-03-08 15:36:53 -0800148 std::thread te(BusThread, std::ref(e));
149
150 static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700151 struct ThreadParams m = {std::ref(ModeControlBus), modeBus};
Patrick Venturee6206562018-03-08 15:36:53 -0800152 std::thread tm(BusThread, std::ref(m));
153
154 std::vector<std::thread> zoneThreads;
155
156 /* TODO(venture): This was designed to have one thread per zone, but really
157 * it could have one thread for all the zones and iterate through each
158 * sequentially as it goes -- and it'd probably be fast enough to do that,
159 * however, a system isn't likely going to have more than a couple zones.
160 * If it only has a couple zones, then this is fine.
161 */
162 for (auto& i : zones)
163 {
164 std::cerr << "pushing zone" << std::endl;
Patrick Venture5c7cc542018-06-11 14:29:38 -0700165 zoneThreads.push_back(std::thread(PIDControlThread, i.second.get()));
Patrick Venturee6206562018-03-08 15:36:53 -0800166 }
167
168 l.join();
169 te.join();
170 tm.join();
171 for (auto& t : zoneThreads)
172 {
173 t.join();
174 }
175
176 return rc;
177}