xref: /openbmc/phosphor-hwmon/readd.cpp (revision c635e8609f5246224dfba7db57991de0b1c5327f)
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 <iostream>
17 #include <memory>
18 #include "argument.hpp"
19 #include "mainloop.hpp"
20 #include "config.h"
21 #include "sysfs.hpp"
22 
23 static void exit_with_error(const char* err, char** argv)
24 {
25     ArgumentParser::usage(argv);
26     std::cerr << std::endl;
27     std::cerr << "ERROR: " << err << std::endl;
28     exit(-1);
29 }
30 
31 int main(int argc, char** argv)
32 {
33     // Read arguments.
34     auto options = std::make_unique<ArgumentParser>(argc, argv);
35 
36     // Parse out path argument.
37     auto path = (*options)["dev-path"];
38     if (path != ArgumentParser::empty_string)
39     {
40         // This path may either be a device path (starts with
41         // /devices), or an open firmware device tree path.
42         if (path.substr(0, 8) == "/devices")
43         {
44             path = sysfs::findHwmonFromDevPath(path);
45         }
46         else
47         {
48             path = sysfs::findHwmonFromOFPath(path);
49         }
50     }
51 
52     if (path == ArgumentParser::empty_string)
53     {
54         path = (*options)["path"];
55     }
56 
57     if (path == ArgumentParser::empty_string)
58     {
59         exit_with_error("Path not specified or invalid.", argv);
60     }
61 
62     // Finished getting options out, so cleanup the parser.
63     options.reset();
64 
65     // Determine the physical device sysfs path.
66     auto calloutPath = sysfs::findCalloutPath(path);
67     if (calloutPath.empty())
68     {
69         exit_with_error("Unable to determine callout path.", argv);
70     }
71 
72     MainLoop loop(
73         sdbusplus::bus::new_default(),
74         path,
75         calloutPath,
76         BUSNAME_PREFIX,
77         SENSOR_ROOT);
78     loop.run();
79 
80     return 0;
81 }
82 
83 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
84