blob: e40c61e86213129275d83a6b68ce104d22eef4ff [file] [log] [blame]
Mike Capps385b1982021-06-28 10:40:42 -04001/**
2 * Copyright © 2021 IBM Corporation
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
Mike Cappsff968232021-06-30 18:24:39 -040017#include "config.h"
18
Mike Capps385b1982021-06-28 10:40:42 -040019#include "sdbusplus.hpp"
20
21#include <CLI/CLI.hpp>
Matt Spinlerb564e152021-10-29 13:10:18 -050022#include <nlohmann/json.hpp>
Mike Capps385b1982021-06-28 10:40:42 -040023#include <sdbusplus/bus.hpp>
24
Matt Spinlerb564e152021-10-29 13:10:18 -050025#include <chrono>
26#include <filesystem>
Mike Capps385b1982021-06-28 10:40:42 -040027#include <iomanip>
28#include <iostream>
29
30using SDBusPlus = phosphor::fan::util::SDBusPlus;
31
Mike Capps16aab352021-06-30 10:17:21 -040032constexpr auto systemdMgrIface = "org.freedesktop.systemd1.Manager";
33constexpr auto systemdPath = "/org/freedesktop/systemd1";
34constexpr auto systemdService = "org.freedesktop.systemd1";
Mike Cappsff968232021-06-30 18:24:39 -040035constexpr auto phosphorServiceName = "phosphor-fan-control@0.service";
Matt Spinlerb564e152021-10-29 13:10:18 -050036constexpr auto dumpFile = "/tmp/fan_control_dump.json";
Mike Capps16aab352021-06-30 10:17:21 -040037
38enum
39{
40 FAN_NAMES = 0,
41 PATH_MAP = 1,
42 IFACES = 2,
43 METHOD = 3
44};
45
Matt Spinlerb564e152021-10-29 13:10:18 -050046struct DumpQuery
47{
48 std::string section;
49 std::string name;
50 std::vector<std::string> properties;
51};
52
Mike Capps385b1982021-06-28 10:40:42 -040053/**
54 * @function extracts fan name from dbus path string (last token where
55 * delimiter is the / character), with proper bounds checking.
56 * @param[in] path - D-Bus path
57 * @return just the fan name.
58 */
59
60std::string justFanName(std::string const& path)
61{
62 std::string fanName;
63
64 auto itr = path.rfind("/");
65 if (itr != std::string::npos && itr < path.size())
66 {
67 fanName = path.substr(1 + itr);
68 }
69
70 return fanName;
71}
72
73/**
74 * @function produces subtree paths whose names match fan token names.
75 * @param[in] path - D-Bus path to obtain subtree from
76 * @param[in] iface - interface to obtain subTreePaths from
77 * @param[in] fans - label matching tokens to filter by
78 * @param[in] shortPath - flag to shorten fan token
79 * @return map of paths by fan name
80 */
81
82std::map<std::string, std::vector<std::string>>
83 getPathsFromIface(const std::string& path, const std::string& iface,
84 const std::vector<std::string>& fans,
85 bool shortPath = false)
86{
87 std::map<std::string, std::vector<std::string>> dest;
88
89 for (auto& path :
90 SDBusPlus::getSubTreePathsRaw(SDBusPlus::getBus(), path, iface, 0))
91 {
92 for (auto& fan : fans)
93 {
94 if (shortPath)
95 {
96 if (fan == justFanName(path))
97 {
98 dest[fan].push_back(path);
99 }
100 }
101 else if (std::string::npos != path.find(fan + "_"))
102 {
103 dest[fan].push_back(path);
104 }
105 }
106 }
107
108 return dest;
109}
110
111/**
Mike Capps530c6552021-06-29 09:50:38 -0400112 * @function consolidated function to load dbus paths and fan names
113 */
114auto loadDBusData()
115{
116 auto& bus{SDBusPlus::getBus()};
117
118 std::vector<std::string> fanNames;
119
120 // paths by D-bus interface,fan name
121 std::map<std::string, std::map<std::string, std::vector<std::string>>>
122 pathMap;
123
124 std::string method("RPM");
125
126 std::map<const std::string, const std::string> interfaces{
127 {"FanSpeed", "xyz.openbmc_project.Control.FanSpeed"},
128 {"FanPwm", "xyz.openbmc_project.Control.FanPwm"},
129 {"SensorValue", "xyz.openbmc_project.Sensor.Value"},
130 {"Item", "xyz.openbmc_project.Inventory.Item"},
131 {"OpStatus", "xyz.openbmc_project.State.Decorator.OperationalStatus"}};
132
133 std::map<const std::string, const std::string> paths{
134 {"motherboard",
135 "/xyz/openbmc_project/inventory/system/chassis/motherboard"},
136 {"tach", "/xyz/openbmc_project/sensors/fan_tach"}};
137
138 // build a list of all fans
139 for (auto& path : SDBusPlus::getSubTreePathsRaw(bus, paths["tach"],
140 interfaces["FanSpeed"], 0))
141 {
142 // special case where we build the list of fans
143 auto fan = justFanName(path);
144 fan = fan.substr(0, fan.rfind("_"));
145 fanNames.push_back(fan);
146 }
147
148 // retry with PWM mode if none found
149 if (0 == fanNames.size())
150 {
151 method = "PWM";
152
153 for (auto& path : SDBusPlus::getSubTreePathsRaw(
154 bus, paths["tach"], interfaces["FanPwm"], 0))
155 {
156 // special case where we build the list of fans
157 auto fan = justFanName(path);
158 fan = fan.substr(0, fan.rfind("_"));
159 fanNames.push_back(fan);
160 }
161 }
162
163 // load tach sensor paths for each fan
164 pathMap["tach"] =
165 getPathsFromIface(paths["tach"], interfaces["SensorValue"], fanNames);
166
167 // load inventory Item data for each fan
168 pathMap["inventory"] = getPathsFromIface(
169 paths["motherboard"], interfaces["Item"], fanNames, true);
170
171 // load operational status data for each fan
172 pathMap["opstatus"] = getPathsFromIface(
173 paths["motherboard"], interfaces["OpStatus"], fanNames, true);
174
175 return std::make_tuple(fanNames, pathMap, interfaces, method);
176}
177
178/**
Mike Capps385b1982021-06-28 10:40:42 -0400179 * @function gets the states of phosphor-fanctl. equivalent to
180 * "systemctl status phosphor-fan-control@0"
181 * @return a list of several (sub)states of fanctl (loaded,
182 * active, running) as well as D-Bus properties representing
183 * BMC states (bmc state,chassis power state, host state)
184 */
185
186std::array<std::string, 6> getStates()
187{
188 using DBusTuple =
189 std::tuple<std::string, std::string, std::string, std::string,
190 std::string, std::string, sdbusplus::message::object_path,
191 uint32_t, std::string, sdbusplus::message::object_path>;
192
193 std::array<std::string, 6> ret;
194
Mike Capps16aab352021-06-30 10:17:21 -0400195 std::vector<std::string> services{phosphorServiceName};
Mike Capps385b1982021-06-28 10:40:42 -0400196
197 try
198 {
199 auto fields{SDBusPlus::callMethodAndRead<std::vector<DBusTuple>>(
200 systemdService, systemdPath, systemdMgrIface, "ListUnitsByNames",
201 services)};
202
203 if (fields.size() > 0)
204 {
205 ret[0] = std::get<2>(fields[0]);
206 ret[1] = std::get<3>(fields[0]);
207 ret[2] = std::get<4>(fields[0]);
208 }
209 else
210 {
211 std::cout << "No units found for systemd service: " << services[0]
212 << std::endl;
213 }
214 }
Mike Capps530c6552021-06-29 09:50:38 -0400215 catch (const std::exception& e)
Mike Capps385b1982021-06-28 10:40:42 -0400216 {
217 std::cerr << "Failure retrieving phosphor-fan-control states: "
218 << e.what() << std::endl;
219 }
220
221 std::string path("/xyz/openbmc_project/state/bmc0");
222 std::string iface("xyz.openbmc_project.State.BMC");
223 ret[3] =
224 SDBusPlus::getProperty<std::string>(path, iface, "CurrentBMCState");
225
226 path = "/xyz/openbmc_project/state/chassis0";
227 iface = "xyz.openbmc_project.State.Chassis";
228 ret[4] =
229 SDBusPlus::getProperty<std::string>(path, iface, "CurrentPowerState");
230
231 path = "/xyz/openbmc_project/state/host0";
232 iface = "xyz.openbmc_project.State.Host";
233 ret[5] =
234 SDBusPlus::getProperty<std::string>(path, iface, "CurrentHostState");
235
236 return ret;
237}
238
239/**
Mike Capps530c6552021-06-29 09:50:38 -0400240 * @function helper to determine interface type from a given control method
241 */
242std::string ifaceTypeFromMethod(const std::string& method)
243{
244 return (method == "RPM" ? "FanSpeed" : "FanPwm");
245}
246
247/**
Mike Capps385b1982021-06-28 10:40:42 -0400248 * @function performs the "status" command from the cmdline.
249 * get states and sensor data and output to the console
250 */
251void status()
252{
253 using std::cout;
254 using std::endl;
255 using std::setw;
256
Mike Capps530c6552021-06-29 09:50:38 -0400257 auto busData = loadDBusData();
Mike Capps16aab352021-06-30 10:17:21 -0400258 auto& method = std::get<METHOD>(busData);
Mike Capps385b1982021-06-28 10:40:42 -0400259
Mike Capps530c6552021-06-29 09:50:38 -0400260 std::string property;
Mike Capps385b1982021-06-28 10:40:42 -0400261
262 // get the state,substate of fan-control and obmc
263 auto states = getStates();
264
265 // print the header
266 cout << "Fan Control Service State : " << states[0] << ", " << states[1]
267 << "(" << states[2] << ")" << endl;
268 cout << endl;
269 cout << "CurrentBMCState : " << states[3] << endl;
270 cout << "CurrentPowerState : " << states[4] << endl;
271 cout << "CurrentHostState : " << states[5] << endl;
272 cout << endl;
Matthew Barth3943b542022-02-07 14:16:03 -0600273 cout << "FAN "
274 << "TARGET(" << method << ") FEEDBACKS(RPM) PRESENT"
275 << " FUNCTIONAL" << endl;
Mike Capps385b1982021-06-28 10:40:42 -0400276 cout << "==============================================================="
277 << endl;
278
Mike Capps16aab352021-06-30 10:17:21 -0400279 auto& fanNames{std::get<FAN_NAMES>(busData)};
280 auto& pathMap{std::get<PATH_MAP>(busData)};
281 auto& interfaces{std::get<IFACES>(busData)};
Mike Capps530c6552021-06-29 09:50:38 -0400282
Mike Capps385b1982021-06-28 10:40:42 -0400283 for (auto& fan : fanNames)
284 {
Matthew Barth3943b542022-02-07 14:16:03 -0600285 cout << setw(8) << std::left << fan << std::right << setw(13);
Mike Capps385b1982021-06-28 10:40:42 -0400286
287 // get the target RPM
288 property = "Target";
289 cout << SDBusPlus::getProperty<uint64_t>(
Mike Capps530c6552021-06-29 09:50:38 -0400290 pathMap["tach"][fan][0],
291 interfaces[ifaceTypeFromMethod(method)], property)
Matthew Barth3943b542022-02-07 14:16:03 -0600292 << setw(19);
Mike Capps385b1982021-06-28 10:40:42 -0400293
294 // get the sensor RPM
295 property = "Value";
Mike Capps530c6552021-06-29 09:50:38 -0400296
Mike Cappsff968232021-06-30 18:24:39 -0400297 std::ostringstream output;
Mike Capps385b1982021-06-28 10:40:42 -0400298 int numRotors = pathMap["tach"][fan].size();
299 // print tach readings for each rotor
300 for (auto& path : pathMap["tach"][fan])
301 {
Mike Cappsff968232021-06-30 18:24:39 -0400302 output << SDBusPlus::getProperty<double>(
Mike Capps385b1982021-06-28 10:40:42 -0400303 path, interfaces["SensorValue"], property);
304
305 // dont print slash on last rotor
306 if (--numRotors)
Mike Cappsff968232021-06-30 18:24:39 -0400307 output << "/";
Mike Capps385b1982021-06-28 10:40:42 -0400308 }
Matthew Barth3943b542022-02-07 14:16:03 -0600309 cout << output.str() << setw(10);
Mike Capps385b1982021-06-28 10:40:42 -0400310
Mike Capps530c6552021-06-29 09:50:38 -0400311 // print the Present property
Mike Capps385b1982021-06-28 10:40:42 -0400312 property = "Present";
Matthew Barth3943b542022-02-07 14:16:03 -0600313 auto itFan = pathMap["inventory"].find(fan);
314 if (itFan != pathMap["inventory"].end())
Mike Capps385b1982021-06-28 10:40:42 -0400315 {
Matthew Barth3943b542022-02-07 14:16:03 -0600316 for (auto& path : itFan->second)
Mike Capps385b1982021-06-28 10:40:42 -0400317 {
Matthew Barth3943b542022-02-07 14:16:03 -0600318 try
Mike Capps385b1982021-06-28 10:40:42 -0400319 {
Matthew Barth3943b542022-02-07 14:16:03 -0600320 cout << std::boolalpha
321 << SDBusPlus::getProperty<bool>(
322 path, interfaces["Item"], property);
Mike Capps385b1982021-06-28 10:40:42 -0400323 }
Matthew Barth3943b542022-02-07 14:16:03 -0600324 catch (const phosphor::fan::util::DBusError&)
Mike Capps385b1982021-06-28 10:40:42 -0400325 {
Matthew Barth3943b542022-02-07 14:16:03 -0600326 cout << "Unknown";
Mike Capps385b1982021-06-28 10:40:42 -0400327 }
328 }
Matthew Barth3943b542022-02-07 14:16:03 -0600329 }
330 else
331 {
332 cout << "Unknown";
Mike Capps385b1982021-06-28 10:40:42 -0400333 }
334
335 cout << setw(13);
336
Mike Capps530c6552021-06-29 09:50:38 -0400337 // and the functional property
Mike Capps385b1982021-06-28 10:40:42 -0400338 property = "Functional";
Matthew Barth3943b542022-02-07 14:16:03 -0600339 itFan = pathMap["opstatus"].find(fan);
340 if (itFan != pathMap["opstatus"].end())
Mike Capps385b1982021-06-28 10:40:42 -0400341 {
Matthew Barth3943b542022-02-07 14:16:03 -0600342 for (auto& path : itFan->second)
Mike Capps385b1982021-06-28 10:40:42 -0400343 {
Matthew Barth3943b542022-02-07 14:16:03 -0600344 try
Mike Capps385b1982021-06-28 10:40:42 -0400345 {
Matthew Barth3943b542022-02-07 14:16:03 -0600346 cout << std::boolalpha
347 << SDBusPlus::getProperty<bool>(
348 path, interfaces["OpStatus"], property);
Mike Capps385b1982021-06-28 10:40:42 -0400349 }
Matthew Barth3943b542022-02-07 14:16:03 -0600350 catch (const phosphor::fan::util::DBusError&)
Mike Capps385b1982021-06-28 10:40:42 -0400351 {
Matthew Barth3943b542022-02-07 14:16:03 -0600352 cout << "Unknown";
Mike Capps385b1982021-06-28 10:40:42 -0400353 }
354 }
Matthew Barth3943b542022-02-07 14:16:03 -0600355 }
356 else
357 {
358 cout << "Unknown";
Mike Capps385b1982021-06-28 10:40:42 -0400359 }
360
361 cout << endl;
362 }
363}
364
365/**
Mike Capps530c6552021-06-29 09:50:38 -0400366 * @function print target RPM/PWM and tach readings from each fan
367 */
368void get()
369{
370 using std::cout;
371 using std::endl;
372 using std::setw;
373
374 auto busData = loadDBusData();
375
Mike Capps16aab352021-06-30 10:17:21 -0400376 auto& fanNames{std::get<FAN_NAMES>(busData)};
377 auto& pathMap{std::get<PATH_MAP>(busData)};
378 auto& interfaces{std::get<IFACES>(busData)};
379 auto& method = std::get<METHOD>(busData);
Mike Capps530c6552021-06-29 09:50:38 -0400380
381 std::string property;
382
383 // print the header
384 cout << "TARGET SENSOR" << setw(11) << "TARGET(" << method
Mike Cappsff968232021-06-30 18:24:39 -0400385 << ") FEEDBACK SENSOR FEEDBACK(RPM)" << endl;
Mike Capps530c6552021-06-29 09:50:38 -0400386 cout << "==============================================================="
387 << endl;
388
389 for (auto& fan : fanNames)
390 {
391 if (pathMap["tach"][fan].size() == 0)
392 continue;
393 // print just the sensor name
394 auto shortPath = pathMap["tach"][fan][0];
395 shortPath = justFanName(shortPath);
Mike Cappsff968232021-06-30 18:24:39 -0400396 cout << shortPath << setw(18);
Mike Capps530c6552021-06-29 09:50:38 -0400397
398 // print its target RPM/PWM
399 property = "Target";
400 cout << SDBusPlus::getProperty<uint64_t>(
401 pathMap["tach"][fan][0],
402 interfaces[ifaceTypeFromMethod(method)], property)
403 << setw(12) << " ";
404
405 // print readings for each rotor
406 property = "Value";
407
408 auto indent = 0U;
409 for (auto& path : pathMap["tach"][fan])
410 {
411 cout << setw(indent);
Mike Cappsff968232021-06-30 18:24:39 -0400412 cout << justFanName(path) << setw(16)
Mike Capps530c6552021-06-29 09:50:38 -0400413 << SDBusPlus::getProperty<double>(
414 path, interfaces["SensorValue"], property)
415 << endl;
416
417 if (0 == indent)
Mike Cappsff968232021-06-30 18:24:39 -0400418 indent = 42;
Mike Capps530c6552021-06-29 09:50:38 -0400419 }
420 }
421}
422
423/**
Mike Capps16aab352021-06-30 10:17:21 -0400424 * @function set fan[s] to a target RPM
425 */
Mike Cappsff968232021-06-30 18:24:39 -0400426void set(uint64_t target, std::vector<std::string>& fanList)
Mike Capps16aab352021-06-30 10:17:21 -0400427{
428 auto busData = loadDBusData();
Mike Capps16aab352021-06-30 10:17:21 -0400429 auto& bus{SDBusPlus::getBus()};
430 auto& pathMap{std::get<PATH_MAP>(busData)};
431 auto& interfaces{std::get<IFACES>(busData)};
432 auto& method = std::get<METHOD>(busData);
433
434 std::string ifaceType(method == "RPM" ? "FanSpeed" : "FanPwm");
435
436 // stop the fan-control service
Mike Cappsff968232021-06-30 18:24:39 -0400437 SDBusPlus::callMethodAndRead<sdbusplus::message::object_path>(
Mike Capps16aab352021-06-30 10:17:21 -0400438 systemdService, systemdPath, systemdMgrIface, "StopUnit",
439 phosphorServiceName, "replace");
440
441 if (fanList.size() == 0)
442 {
443 fanList = std::get<FAN_NAMES>(busData);
444 }
445
446 for (auto& fan : fanList)
447 {
448 try
449 {
450 auto paths(pathMap["tach"].find(fan));
451
452 if (pathMap["tach"].end() == paths)
453 {
454 // try again, maybe it was a sensor name instead of a fan name
455 for (const auto& [fanName, sensors] : pathMap["tach"])
456 {
457 for (const auto& path : sensors)
458 {
459 std::string sensor(path.substr(path.rfind("/")));
460
461 if (sensor.size() > 0)
462 {
463 sensor = sensor.substr(1);
464
465 if (sensor == fan)
466 {
467 paths = pathMap["tach"].find(fanName);
468
469 break;
470 }
471 }
472 }
473 }
474 }
475
476 if (pathMap["tach"].end() == paths)
477 {
478 std::cout << "Could not find tach path for fan: " << fan
479 << std::endl;
480 continue;
481 }
482
483 // set the target RPM
484 SDBusPlus::setProperty<uint64_t>(bus, paths->second[0],
485 interfaces[ifaceType], "Target",
486 std::move(target));
487 }
488 catch (const phosphor::fan::util::DBusPropertyError& e)
489 {
490 std::cerr << "Cannot set target rpm for " << fan
491 << " caught D-Bus exception: " << e.what() << std::endl;
492 }
493 }
494}
495
496/**
497 * @function restart fan-control to allow it to manage fan speeds
498 */
499void resume()
500{
501 try
502 {
503 auto retval =
504 SDBusPlus::callMethodAndRead<sdbusplus::message::object_path>(
505 systemdService, systemdPath, systemdMgrIface, "StartUnit",
506 phosphorServiceName, "replace");
507 }
508 catch (const phosphor::fan::util::DBusMethodError& e)
509 {
510 std::cerr << "Unable to start fan control: " << e.what() << std::endl;
511 }
512}
513
514/**
Mike Cappsff968232021-06-30 18:24:39 -0400515 * @function force reload of control files by sending HUP signal
516 */
517void reload()
518{
Mike Cappsff968232021-06-30 18:24:39 -0400519 try
520 {
521 SDBusPlus::callMethod(systemdService, systemdPath, systemdMgrIface,
522 "KillUnit", phosphorServiceName, "main", SIGHUP);
523 }
524 catch (const phosphor::fan::util::DBusPropertyError& e)
525 {
526 std::cerr << "Unable to reload configuration files: " << e.what()
527 << std::endl;
528 }
Mike Cappsff968232021-06-30 18:24:39 -0400529}
530
531/**
Mike Capps7c8b97f2021-10-06 14:04:18 -0400532 * @function dump the FlightRecorder log data
533 */
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500534void dumpFanControl()
Mike Capps7c8b97f2021-10-06 14:04:18 -0400535{
Mike Capps28945d42022-01-27 11:22:40 -0500536 namespace fs = std::filesystem;
537
Mike Capps7c8b97f2021-10-06 14:04:18 -0400538 try
539 {
Mike Capps28945d42022-01-27 11:22:40 -0500540 // delete existing file
541 if (fs::exists(dumpFile))
542 {
543 std::filesystem::remove(dumpFile);
544 }
545
Mike Capps7c8b97f2021-10-06 14:04:18 -0400546 SDBusPlus::callMethod(systemdService, systemdPath, systemdMgrIface,
547 "KillUnit", phosphorServiceName, "main", SIGUSR1);
Mike Capps28945d42022-01-27 11:22:40 -0500548
549 bool done = false;
550
551 do
552 {
553 // wait for file to be detected
554 sleep(1);
555
556 if (fs::exists(dumpFile))
557 {
558 try
559 {
560 auto unused{nlohmann::json::parse(std::ifstream{dumpFile})};
561 done = true;
562 }
563 catch (...)
564 {
565 // TODO: maybe have a max-retries counter and fail after N
566 // tries
567 }
568 }
569 } while (!done);
570
Matt Spinlerb564e152021-10-29 13:10:18 -0500571 std::cout << "Fan control dump written to: " << dumpFile << std::endl;
Mike Capps7c8b97f2021-10-06 14:04:18 -0400572 }
573 catch (const phosphor::fan::util::DBusPropertyError& e)
574 {
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500575 std::cerr << "Unable to dump fan control: " << e.what() << std::endl;
Mike Capps7c8b97f2021-10-06 14:04:18 -0400576 }
577}
578
579/**
Matt Spinlerb564e152021-10-29 13:10:18 -0500580 * @function Query items in the dump file
581 */
582void queryDumpFile(const DumpQuery& dq)
583{
584 nlohmann::json output;
585 std::ifstream file{dumpFile};
586
587 if (!file.good())
588 {
589 std::cerr << "Unable to open dump file, please run 'fanctl dump'.\n";
590 return;
591 }
592
593 auto dumpData = nlohmann::json::parse(file);
594
595 if (!dumpData.contains(dq.section))
596 {
597 std::cerr << "Error: Dump file does not contain " << dq.section
598 << " section"
599 << "\n";
600 return;
601 }
602
603 const auto& section = dumpData.at(dq.section);
604
Matt Spinler69f19ff2021-11-04 09:28:25 -0500605 if (section.is_array())
606 {
607 for (const auto& entry : section)
608 {
609 if (!entry.is_string() || dq.name.empty() ||
610 (entry.get<std::string>().find(dq.name) != std::string::npos))
611 {
612 output[dq.section].push_back(entry);
613 }
614 }
615 std::cout << std::setw(4) << output << "\n";
616 return;
617 }
618
Matt Spinlerb564e152021-10-29 13:10:18 -0500619 for (const auto& [key1, values1] : section.items())
620 {
621 if (dq.name.empty() || (key1.find(dq.name) != std::string::npos))
622 {
623 // If no properties specified, print the whole JSON value
624 if (dq.properties.empty())
625 {
626 output[key1] = values1;
627 continue;
628 }
629
630 // Look for properties both one and two levels down.
631 // Future improvement: Use recursion.
632 for (const auto& [key2, values2] : values1.items())
633 {
634 for (const auto& prop : dq.properties)
635 {
636 if (prop == key2)
637 {
638 output[key1][prop] = values2;
639 }
640 }
641
642 for (const auto& [key3, values3] : values2.items())
643 {
644 for (const auto& prop : dq.properties)
645 {
646 if (prop == key3)
647 {
648 output[key1][prop] = values3;
649 }
650 }
651 }
652 }
653 }
654 }
655
656 if (!output.empty())
657 {
658 std::cout << std::setw(4) << output << "\n";
659 }
660}
661
662/**
Mike Cappsff968232021-06-30 18:24:39 -0400663 * @function setup the CLI object to accept all options
664 */
Matt Spinlerb564e152021-10-29 13:10:18 -0500665void initCLI(CLI::App& app, uint64_t& target, std::vector<std::string>& fanList,
666 DumpQuery& dq)
Mike Cappsff968232021-06-30 18:24:39 -0400667{
668 app.set_help_flag("-h,--help", "Print this help page and exit.");
669
670 // App requires only 1 subcommand to be given
671 app.require_subcommand(1);
672
673 // This represents the command given
674 auto commands = app.add_option_group("Commands");
675
676 // status method
677 std::string strHelp("Prints fan target/tach readings, present/functional "
678 "states, and fan-monitor/BMC/Power service status");
Mike Capps49766182021-09-27 22:32:46 -0400679
Mike Cappsff968232021-06-30 18:24:39 -0400680 auto cmdStatus = commands->add_subcommand("status", strHelp);
681 cmdStatus->set_help_flag("-h, --help", strHelp);
682 cmdStatus->require_option(0);
683
684 // get method
685 strHelp = "Get the current fan target and feedback speeds for all rotors";
686 auto cmdGet = commands->add_subcommand("get", strHelp);
687 cmdGet->set_help_flag("-h, --help", strHelp);
688 cmdGet->require_option(0);
689
690 // set method
691 strHelp = "Set target (all rotors) for one-or-more fans";
692 auto cmdSet = commands->add_subcommand("set", strHelp);
693 strHelp = R"(set <TARGET> [TARGET SENSOR(S)]
694 <TARGET>
695 - RPM/PWM target to set the fans
696[TARGET SENSOR LIST]
697- list of target sensors to set)";
698 cmdSet->set_help_flag("-h, --help", strHelp);
699 cmdSet->add_option("target", target, "RPM/PWM target to set the fans");
700 cmdSet->add_option(
701 "fan list", fanList,
702 "[optional] list of 1+ fans to set target RPM/PWM (default: all)");
703 cmdSet->require_option();
704
Matthew Barthad3b6b52021-10-26 15:38:15 -0500705#ifdef CONTROL_USE_JSON
Mike Cappsff968232021-06-30 18:24:39 -0400706 strHelp = "Reload phosphor-fan configuration files";
707 auto cmdReload = commands->add_subcommand("reload", strHelp);
708 cmdReload->set_help_flag("-h, --help", strHelp);
709 cmdReload->require_option(0);
Matthew Barthad3b6b52021-10-26 15:38:15 -0500710#endif
Mike Cappsff968232021-06-30 18:24:39 -0400711
712 strHelp = "Resume running phosphor-fan-control";
713 auto cmdResume = commands->add_subcommand("resume", strHelp);
714 cmdResume->set_help_flag("-h, --help", strHelp);
715 cmdResume->require_option(0);
Mike Capps7c8b97f2021-10-06 14:04:18 -0400716
Matthew Barth1a4dcef2021-10-26 15:35:48 -0500717#ifdef CONTROL_USE_JSON
Mike Capps7c8b97f2021-10-06 14:04:18 -0400718 // Dump method
719 auto cmdDump = commands->add_subcommand(
720 "dump", "Dump the FlightRecorder diagnostic log");
721 cmdDump->set_help_flag("-h, --help",
722 "Dump the FlightRecorder diagnostic log");
723 cmdDump->require_option(0);
Matt Spinlerb564e152021-10-29 13:10:18 -0500724
Matthew Barth1a4dcef2021-10-26 15:35:48 -0500725 // Query dump
Matt Spinlerb564e152021-10-29 13:10:18 -0500726 auto cmdDumpQuery =
727 commands->add_subcommand("query_dump", "Query the dump file");
728
729 cmdDumpQuery->set_help_flag("-h, --help", "Query the dump file");
730 cmdDumpQuery
731 ->add_option("-s, --section", dq.section, "Dump file section name")
732 ->required();
733 cmdDumpQuery->add_option("-n, --name", dq.name,
734 "Optional dump file entry name (or substring)");
735 cmdDumpQuery->add_option("-p, --properties", dq.properties,
736 "Optional list of dump file property names");
737#endif
Mike Cappsff968232021-06-30 18:24:39 -0400738}
739
740/**
Mike Capps385b1982021-06-28 10:40:42 -0400741 * @function main entry point for the application
742 */
743int main(int argc, char* argv[])
744{
745 auto rc = 0;
Mike Capps16aab352021-06-30 10:17:21 -0400746 uint64_t target{0U};
747 std::vector<std::string> fanList;
Matt Spinlerb564e152021-10-29 13:10:18 -0500748 DumpQuery dq;
Mike Capps385b1982021-06-28 10:40:42 -0400749
750 try
751 {
Mike Capps49766182021-09-27 22:32:46 -0400752 CLI::App app{"Manually control, get fan tachs, view status, and resume "
753 "automatic control of all fans within a chassis. Full "
754 "documentation can be found at the readme:\n"
755 "https://github.com/openbmc/phosphor-fan-presence/tree/"
756 "master/docs/control/fanctl"};
Mike Capps385b1982021-06-28 10:40:42 -0400757
Matt Spinlerb564e152021-10-29 13:10:18 -0500758 initCLI(app, target, fanList, dq);
Mike Capps16aab352021-06-30 10:17:21 -0400759
Mike Capps385b1982021-06-28 10:40:42 -0400760 CLI11_PARSE(app, argc, argv);
761
Mike Cappsff968232021-06-30 18:24:39 -0400762 if (app.got_subcommand("get"))
Mike Capps385b1982021-06-28 10:40:42 -0400763 {
Mike Cappsff968232021-06-30 18:24:39 -0400764 get();
Mike Capps385b1982021-06-28 10:40:42 -0400765 }
Mike Capps16aab352021-06-30 10:17:21 -0400766 else if (app.got_subcommand("set"))
767 {
768 set(target, fanList);
769 }
Matthew Barthad3b6b52021-10-26 15:38:15 -0500770#ifdef CONTROL_USE_JSON
Mike Cappsff968232021-06-30 18:24:39 -0400771 else if (app.got_subcommand("reload"))
Mike Capps530c6552021-06-29 09:50:38 -0400772 {
Mike Cappsff968232021-06-30 18:24:39 -0400773 reload();
Mike Capps530c6552021-06-29 09:50:38 -0400774 }
Matthew Barthad3b6b52021-10-26 15:38:15 -0500775#endif
Mike Capps16aab352021-06-30 10:17:21 -0400776 else if (app.got_subcommand("resume"))
777 {
778 resume();
779 }
Mike Cappsff968232021-06-30 18:24:39 -0400780 else if (app.got_subcommand("status"))
781 {
782 status();
783 }
Mike Capps7c8b97f2021-10-06 14:04:18 -0400784 else if (app.got_subcommand("dump"))
785 {
Mike Capps28945d42022-01-27 11:22:40 -0500786#ifdef CONTROL_USE_JSON
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500787 dumpFanControl();
Mike Capps28945d42022-01-27 11:22:40 -0500788#else
789 std::ofstream(dumpFile)
790 << "{\n\"msg\": \"Unable to create dump on "
791 "non-JSON config based system\"\n}";
792#endif
Mike Capps7c8b97f2021-10-06 14:04:18 -0400793 }
Mike Capps28945d42022-01-27 11:22:40 -0500794#ifdef CONTROL_USE_JSON
Matt Spinlerb564e152021-10-29 13:10:18 -0500795 else if (app.got_subcommand("query_dump"))
796 {
797 queryDumpFile(dq);
798 }
799#endif
Mike Capps385b1982021-06-28 10:40:42 -0400800 }
801 catch (const std::exception& e)
802 {
803 rc = -1;
804 std::cerr << argv[0] << " failed: " << e.what() << std::endl;
805 }
806
807 return rc;
808}