Brad Bishop | 29dbfa6 | 2016-12-19 13:39:57 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 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 | */ |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 18 | #include "hwmonio.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 19 | #include "mainloop.hpp" |
Brad Bishop | 4da0161 | 2017-01-05 21:10:06 -0500 | [diff] [blame] | 20 | #include "sysfs.hpp" |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 21 | |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 22 | #include <CLI/CLI.hpp> |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 23 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 24 | #include <iostream> |
| 25 | #include <memory> |
| 26 | |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 27 | static void exit_with_error(const std::string& help, const char* err) |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 28 | { |
Patrick Venture | 2e476ac | 2019-03-12 16:06:22 -0700 | [diff] [blame] | 29 | std::cerr << "ERROR: " << err << std::endl << help << std::endl; |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 30 | exit(-1); |
| 31 | } |
| 32 | |
| 33 | int main(int argc, char** argv) |
| 34 | { |
| 35 | // Read arguments. |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 36 | std::string syspath = ""; |
| 37 | std::string devpath = ""; |
Anton D. Kachalov | d46d081 | 2021-02-03 23:25:37 +0100 | [diff] [blame] | 38 | std::string sensor_id = ""; |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 39 | |
| 40 | CLI::App app{"OpenBMC Hwmon Daemon"}; |
| 41 | app.add_option("-p,--path", syspath, "sysfs location to monitor"); |
| 42 | app.add_option("-o,--dev-path", devpath, "device path to monitor"); |
Anton D. Kachalov | d46d081 | 2021-02-03 23:25:37 +0100 | [diff] [blame] | 43 | app.add_option("-i,--sensor-id", sensor_id, "dbus sensor instance id"); |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 44 | |
| 45 | CLI11_PARSE(app, argc, argv); |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 46 | |
| 47 | // Parse out path argument. |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 48 | auto path = devpath; |
Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame] | 49 | auto param = path; |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 50 | if (!path.empty()) |
Brad Bishop | 4da0161 | 2017-01-05 21:10:06 -0500 | [diff] [blame] | 51 | { |
Matt Spinler | 147b033 | 2018-03-05 12:07:46 -0600 | [diff] [blame] | 52 | // This path may either be a device path (starts with |
| 53 | // /devices), or an open firmware device tree path. |
| 54 | if (path.substr(0, 8) == "/devices") |
| 55 | { |
| 56 | path = sysfs::findHwmonFromDevPath(path); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | path = sysfs::findHwmonFromOFPath(path); |
| 61 | } |
Brad Bishop | 4da0161 | 2017-01-05 21:10:06 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 64 | if (path.empty()) |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 65 | { |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 66 | path = syspath; |
Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame] | 67 | param = path; |
Brad Bishop | 4da0161 | 2017-01-05 21:10:06 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 70 | if (path.empty()) |
Brad Bishop | 4da0161 | 2017-01-05 21:10:06 -0500 | [diff] [blame] | 71 | { |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 72 | exit_with_error(app.help("", CLI::AppFormatMode::All), |
| 73 | "Path not specified or invalid."); |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 76 | // Determine the physical device sysfs path. |
| 77 | auto calloutPath = sysfs::findCalloutPath(path); |
| 78 | if (calloutPath.empty()) |
| 79 | { |
Patrick Venture | 17cfad6 | 2019-03-11 14:14:30 -0700 | [diff] [blame] | 80 | exit_with_error(app.help("", CLI::AppFormatMode::All), |
| 81 | "Unable to determine callout path."); |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 84 | hwmonio::HwmonIO io(path); |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 85 | MainLoop loop(sdbusplus::bus::new_default(), param, path, calloutPath, |
Anton D. Kachalov | d46d081 | 2021-02-03 23:25:37 +0100 | [diff] [blame] | 86 | BUSNAME_PREFIX, SENSOR_ROOT, sensor_id, &io); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 87 | loop.run(); |
| 88 | |
| 89 | return 0; |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Brad Bishop | 03476f1 | 2016-12-19 13:09:12 -0500 | [diff] [blame] | 92 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |