1 /**
2  * Copyright © 2020 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 
17 #include "utility.hpp"
18 
19 #include <CLI/CLI.hpp>
20 
21 #include <algorithm>
22 #include <iostream>
23 #include <string>
24 
25 using namespace phosphor::power::regulators::control;
26 
27 int main(int argc, char* argv[])
28 {
29     auto rc = 0;
30 
31     bool monitorEnable = false;
32     bool monitorDisable = false;
33 
34     CLI::App app{"Regulators control app for OpenBMC phosphor-regulators"};
35 
36     // Add dbus methods group
37     auto methods = app.add_option_group("Methods");
38     // Configure method
39     CLI::App* config =
40         methods->add_subcommand("config", "Configure regulators");
41     config->set_help_flag("-h,--help", "Configure regulators method help");
42     // Monitor method
43     CLI::App* monitor =
44         methods->add_subcommand("monitor", "Monitor regulators");
45     monitor->set_help_flag("-h,--help", "Monitor regulators method help");
46     monitor->add_flag("-e,--enable", monitorEnable,
47                       "Enable regulator monitoring");
48     monitor->add_flag("-d,--disable", monitorDisable,
49                       "Disable regulator monitoring");
50     // Monitor subcommand requires only 1 option be provided
51     monitor->require_option(1);
52     // Methods group requires only 1 subcommand to be given
53     methods->require_subcommand(1);
54 
55     CLI11_PARSE(app, argc, argv);
56 
57     if (app.got_subcommand("config"))
58     {
59         auto resp = callMethod("Configure");
60         if (!resp)
61         {
62             rc = -1;
63             std::cerr << "regsctl: Failed to begin configuring regulators"
64                       << std::endl;
65         }
66     }
67     if (app.got_subcommand("monitor"))
68     {
69         if (monitorEnable)
70         {
71             auto resp = callMethod("Monitor", true);
72             if (!resp)
73             {
74                 rc = -1;
75                 std::cerr << "regsctl: Failed to enable "
76                              "monitoring of regulators"
77                           << std::endl;
78             }
79         }
80         if (monitorDisable)
81         {
82             auto resp = callMethod("Monitor", false);
83             if (!resp)
84             {
85                 rc = -1;
86                 std::cerr << "regsctl: Failed to disable "
87                              "monitoring of regulators"
88                           << std::endl;
89             }
90         }
91     }
92 
93     return rc;
94 }
95