1 /**
2  * Copyright © 2017 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 "argument.hpp"
17 #include "pgood_monitor.hpp"
18 #include "runtime_monitor.hpp"
19 #include "ucd90160.hpp"
20 
21 #include <chrono>
22 #include <iostream>
23 #include <phosphor-logging/log.hpp>
24 #include <sdeventplus/event.hpp>
25 
26 using namespace witherspoon::power;
27 using namespace phosphor::logging;
28 
29 int main(int argc, char** argv)
30 {
31     ArgumentParser args{argc, argv};
32     auto action = args["action"];
33 
34     if ((action != "pgood-monitor") && (action != "runtime-monitor"))
35     {
36         std::cerr << "Invalid action\n";
37         args.usage(argv);
38         exit(EXIT_FAILURE);
39     }
40 
41     auto i = strtoul(args["interval"].c_str(), nullptr, 10);
42     if (i == 0)
43     {
44         std::cerr << "Invalid interval value\n";
45         exit(EXIT_FAILURE);
46     }
47 
48     std::chrono::milliseconds interval{i};
49 
50     auto event = sdeventplus::Event::get_default();
51     auto bus = sdbusplus::bus::new_default();
52     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
53 
54     auto device = std::make_unique<UCD90160>(0, bus);
55 
56     std::unique_ptr<DeviceMonitor> monitor;
57 
58     if (action == "pgood-monitor")
59     {
60         // If PGOOD doesn't turn on within a certain
61         // time, analyze the device for errors
62         monitor = std::make_unique<PGOODMonitor>(std::move(device), bus, event,
63                                                  interval);
64     }
65     else // runtime-monitor
66     {
67         // Continuously monitor this device both by polling
68         // and on 'power lost' signals.
69         monitor = std::make_unique<RuntimeMonitor>(std::move(device), bus,
70                                                    event, interval);
71     }
72 
73     return monitor->run();
74 }
75