xref: /openbmc/phosphor-hwmon/readd.cpp (revision c897d8bb66ff98882a5f5c4df5afb94d678f61d4)
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     auto param = path;
39     if (path != ArgumentParser::empty_string)
40     {
41         // This path may either be a device path (starts with
42         // /devices), or an open firmware device tree path.
43         if (path.substr(0, 8) == "/devices")
44         {
45             path = sysfs::findHwmonFromDevPath(path);
46         }
47         else
48         {
49             path = sysfs::findHwmonFromOFPath(path);
50         }
51     }
52 
53     if (path == ArgumentParser::empty_string)
54     {
55         path = (*options)["path"];
56         param = path;
57     }
58 
59     if (path == ArgumentParser::empty_string)
60     {
61         exit_with_error("Path not specified or invalid.", argv);
62     }
63 
64     // Finished getting options out, so cleanup the parser.
65     options.reset();
66 
67     // Determine the physical device sysfs path.
68     auto calloutPath = sysfs::findCalloutPath(path);
69     if (calloutPath.empty())
70     {
71         exit_with_error("Unable to determine callout path.", argv);
72     }
73 
74     MainLoop loop(
75         sdbusplus::bus::new_default(),
76         param,
77         path,
78         calloutPath,
79         BUSNAME_PREFIX,
80         SENSOR_ROOT);
81     loop.run();
82 
83     return 0;
84 }
85 
86 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
87