blob: 91e555f322ec5877745a8a811cd1e9a1f895e18a [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"
20#include "dbus/dbusconfiguration.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080021#include "interfaces.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070022#include "pid/builder.hpp"
23#include "pid/builderconfig.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070024#include "pid/pidthread.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080025#include "pid/zone.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070026#include "sensors/builder.hpp"
27#include "sensors/builderconfig.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080028#include "sensors/manager.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080029#include "threads/busthread.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070030#include "util.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080031
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070032#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>
Patrick Venturee6206562018-03-08 15:36:53 -080044
45/* The YAML converted sensor list. */
46extern std::map<std::string, struct sensor> SensorConfig;
47/* The YAML converted PID list. */
48extern std::map<int64_t, PIDConf> ZoneConfig;
49/* The YAML converted Zone configuration. */
50extern std::map<int64_t, struct zone> ZoneDetailsConfig;
51
52int main(int argc, char* argv[])
53{
54 int rc = 0;
55 int c;
56 std::string configPath = "";
57
58 while (1)
59 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 // clang-format off
61 static struct option long_options[] = {
Patrick Venturee6206562018-03-08 15:36:53 -080062 {"conf", required_argument, 0, 'c'},
63 {0, 0, 0, 0}
64 };
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070065 // clang-format on
Patrick Venturee6206562018-03-08 15:36:53 -080066
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':
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070078 configPath = std::string{optarg};
Patrick Venturee6206562018-03-08 15:36:53 -080079 break;
80 default:
81 /* skip garbage. */
82 continue;
83 }
84 }
85
86 auto ModeControlBus = sdbusplus::bus::new_default();
James Feist7136a5a2018-07-19 09:52:05 -070087 if (configureDbus)
88 {
89 dbus_configuration::init(ModeControlBus);
90 }
Patrick Venturefe75b192018-06-08 11:19:43 -070091 SensorManager mgmr;
Patrick Venture5c7cc542018-06-11 14:29:38 -070092 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
Patrick Venturee6206562018-03-08 15:36:53 -080093
Gunnar Mills08afbb22018-06-14 08:50:53 -050094 // Create a manager for the ModeBus because we own it.
Patrick Venturee6206562018-03-08 15:36:53 -080095 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
Patrick Venturefe75b192018-06-08 11:19:43 -0700132 auto& HostSensorBus = mgmr.getHostBus();
133 auto& PassiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800134
135 std::cerr << "Starting threads\n";
136
137 /* TODO(venture): Ask SensorManager if we have any passive sensors. */
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700138 struct ThreadParams p = {std::ref(PassiveListeningBus), ""};
Patrick Venturee6206562018-03-08 15:36:53 -0800139 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";
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700143 struct ThreadParams e = {std::ref(HostSensorBus), hostBus};
Patrick Venturee6206562018-03-08 15:36:53 -0800144 std::thread te(BusThread, std::ref(e));
145
146 static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700147 struct ThreadParams m = {std::ref(ModeControlBus), modeBus};
Patrick Venturee6206562018-03-08 15:36:53 -0800148 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;
Patrick Venture5c7cc542018-06-11 14:29:38 -0700161 zoneThreads.push_back(std::thread(PIDControlThread, i.second.get()));
Patrick Venturee6206562018-03-08 15:36:53 -0800162 }
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}