xref: /openbmc/phosphor-hwmon/readd.cpp (revision b6865fdcc10dbfd5ff023f3b74d9c8b645d319db)
129dbfa6dSBrad Bishop /**
229dbfa6dSBrad Bishop  * Copyright © 2016 IBM Corporation
329dbfa6dSBrad Bishop  *
429dbfa6dSBrad Bishop  * Licensed under the Apache License, Version 2.0 (the "License");
529dbfa6dSBrad Bishop  * you may not use this file except in compliance with the License.
629dbfa6dSBrad Bishop  * You may obtain a copy of the License at
729dbfa6dSBrad Bishop  *
829dbfa6dSBrad Bishop  *     http://www.apache.org/licenses/LICENSE-2.0
929dbfa6dSBrad Bishop  *
1029dbfa6dSBrad Bishop  * Unless required by applicable law or agreed to in writing, software
1129dbfa6dSBrad Bishop  * distributed under the License is distributed on an "AS IS" BASIS,
1229dbfa6dSBrad Bishop  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1329dbfa6dSBrad Bishop  * See the License for the specific language governing permissions and
1429dbfa6dSBrad Bishop  * limitations under the License.
1529dbfa6dSBrad Bishop  */
16043d3230SPatrick Venture #include "config.h"
17043d3230SPatrick Venture 
1816805a6aSPatrick Venture #include "hwmonio.hpp"
19e55ef3d8SBrad Bishop #include "mainloop.hpp"
204da0161bSBrad Bishop #include "sysfs.hpp"
216292aeedSMatthew Barth 
2217cfad6cSPatrick Venture #include <CLI/CLI.hpp>
23*e8771fd4SPatrick Williams 
24043d3230SPatrick Venture #include <iostream>
25043d3230SPatrick Venture #include <memory>
26043d3230SPatrick Venture 
exit_with_error(const std::string & help,const char * err)2717cfad6cSPatrick Venture static void exit_with_error(const std::string& help, const char* err)
286292aeedSMatthew Barth {
292e476ac3SPatrick Venture     std::cerr << "ERROR: " << err << std::endl << help << std::endl;
306292aeedSMatthew Barth     exit(-1);
316292aeedSMatthew Barth }
326292aeedSMatthew Barth 
main(int argc,char ** argv)336292aeedSMatthew Barth int main(int argc, char** argv)
346292aeedSMatthew Barth {
356292aeedSMatthew Barth     // Read arguments.
3617cfad6cSPatrick Venture     std::string syspath = "";
3717cfad6cSPatrick Venture     std::string devpath = "";
38d46d0818SAnton D. Kachalov     std::string sensor_id = "";
3917cfad6cSPatrick Venture 
4017cfad6cSPatrick Venture     CLI::App app{"OpenBMC Hwmon Daemon"};
4117cfad6cSPatrick Venture     app.add_option("-p,--path", syspath, "sysfs location to monitor");
4217cfad6cSPatrick Venture     app.add_option("-o,--dev-path", devpath, "device path to monitor");
43d46d0818SAnton D. Kachalov     app.add_option("-i,--sensor-id", sensor_id, "dbus sensor instance id");
4417cfad6cSPatrick Venture 
4517cfad6cSPatrick Venture     CLI11_PARSE(app, argc, argv);
466292aeedSMatthew Barth 
476292aeedSMatthew Barth     // Parse out path argument.
4817cfad6cSPatrick Venture     auto path = devpath;
49c897d8bbSPatrick Venture     auto param = path;
5017cfad6cSPatrick Venture     if (!path.empty())
514da0161bSBrad Bishop     {
52147b0337SMatt Spinler         // This path may either be a device path (starts with
53147b0337SMatt Spinler         // /devices), or an open firmware device tree path.
54147b0337SMatt Spinler         if (path.substr(0, 8) == "/devices")
55147b0337SMatt Spinler         {
56147b0337SMatt Spinler             path = sysfs::findHwmonFromDevPath(path);
57147b0337SMatt Spinler         }
58147b0337SMatt Spinler         else
59147b0337SMatt Spinler         {
6031dbe062SMatt Spinler             path = sysfs::findHwmonFromOFPath(path);
614da0161bSBrad Bishop         }
62147b0337SMatt Spinler     }
634da0161bSBrad Bishop 
6417cfad6cSPatrick Venture     if (path.empty())
656292aeedSMatthew Barth     {
6617cfad6cSPatrick Venture         path = syspath;
67c897d8bbSPatrick Venture         param = path;
684da0161bSBrad Bishop     }
694da0161bSBrad Bishop 
7017cfad6cSPatrick Venture     if (path.empty())
714da0161bSBrad Bishop     {
7217cfad6cSPatrick Venture         exit_with_error(app.help("", CLI::AppFormatMode::All),
7317cfad6cSPatrick Venture                         "Path not specified or invalid.");
746292aeedSMatthew Barth     }
756292aeedSMatthew Barth 
76f3aa9aefSBrad Bishop     // Determine the physical device sysfs path.
77f3aa9aefSBrad Bishop     auto calloutPath = sysfs::findCalloutPath(path);
78f3aa9aefSBrad Bishop     if (calloutPath.empty())
79f3aa9aefSBrad Bishop     {
8017cfad6cSPatrick Venture         exit_with_error(app.help("", CLI::AppFormatMode::All),
8117cfad6cSPatrick Venture                         "Unable to determine callout path.");
82f3aa9aefSBrad Bishop     }
83f3aa9aefSBrad Bishop 
8416805a6aSPatrick Venture     hwmonio::HwmonIO io(path);
85043d3230SPatrick Venture     MainLoop loop(sdbusplus::bus::new_default(), param, path, calloutPath,
86d46d0818SAnton D. Kachalov                   BUSNAME_PREFIX, SENSOR_ROOT, sensor_id, &io);
87d499ca64SBrad Bishop     loop.run();
88d499ca64SBrad Bishop 
89d499ca64SBrad Bishop     return 0;
906292aeedSMatthew Barth }
91