xref: /openbmc/phosphor-hwmon/readd.cpp (revision 0bbd07c0)
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  */
16 #include "config.h"
17 
18 #include "hwmonio.hpp"
19 #include "mainloop.hpp"
20 #include "sysfs.hpp"
21 
22 #include <CLI/CLI.hpp>
23 #include <iostream>
24 #include <memory>
25 
26 static void exit_with_error(const std::string& help, const char* err)
27 {
28     std::cerr << "ERROR: " << err << std::endl << help << std::endl;
29     exit(-1);
30 }
31 
32 int main(int argc, char** argv)
33 {
34     // Read arguments.
35     std::string syspath = "";
36     std::string devpath = "";
37     std::string sensor_id = "";
38 
39     CLI::App app{"OpenBMC Hwmon Daemon"};
40     app.add_option("-p,--path", syspath, "sysfs location to monitor");
41     app.add_option("-o,--dev-path", devpath, "device path to monitor");
42     app.add_option("-i,--sensor-id", sensor_id, "dbus sensor instance id");
43 
44     CLI11_PARSE(app, argc, argv);
45 
46     // Parse out path argument.
47     auto path = devpath;
48     auto param = path;
49     if (!path.empty())
50     {
51         // This path may either be a device path (starts with
52         // /devices), or an open firmware device tree path.
53         if (path.substr(0, 8) == "/devices")
54         {
55             path = sysfs::findHwmonFromDevPath(path);
56         }
57         else
58         {
59             path = sysfs::findHwmonFromOFPath(path);
60         }
61     }
62 
63     if (path.empty())
64     {
65         path = syspath;
66         param = path;
67     }
68 
69     if (path.empty())
70     {
71         exit_with_error(app.help("", CLI::AppFormatMode::All),
72                         "Path not specified or invalid.");
73     }
74 
75     // Determine the physical device sysfs path.
76     auto calloutPath = sysfs::findCalloutPath(path);
77     if (calloutPath.empty())
78     {
79         exit_with_error(app.help("", CLI::AppFormatMode::All),
80                         "Unable to determine callout path.");
81     }
82 
83     hwmonio::HwmonIO io(path);
84     MainLoop loop(sdbusplus::bus::new_default(), param, path, calloutPath,
85                   BUSNAME_PREFIX, SENSOR_ROOT, sensor_id, &io);
86     loop.run();
87 
88     return 0;
89 }
90 
91 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
92