1afb39132SMatt Spinler /**
2afb39132SMatt Spinler  * Copyright © 2017 IBM Corporation
3afb39132SMatt Spinler  *
4afb39132SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5afb39132SMatt Spinler  * you may not use this file except in compliance with the License.
6afb39132SMatt Spinler  * You may obtain a copy of the License at
7afb39132SMatt Spinler  *
8afb39132SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9afb39132SMatt Spinler  *
10afb39132SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11afb39132SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12afb39132SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13afb39132SMatt Spinler  * See the License for the specific language governing permissions and
14afb39132SMatt Spinler  * limitations under the License.
15afb39132SMatt Spinler  */
16afb39132SMatt Spinler #include "argument.hpp"
1756d90a89SMatt Spinler #include "pgood_monitor.hpp"
187084927eSMatt Spinler #include "runtime_monitor.hpp"
19b2d72511SMatt Spinler #include "ucd90160.hpp"
20afb39132SMatt Spinler 
21f0f02b9aSMatt Spinler #include <phosphor-logging/log.hpp>
22f0f02b9aSMatt Spinler #include <sdeventplus/event.hpp>
23f0f02b9aSMatt Spinler 
24*2c4fbc4cSPatrick Williams #include <chrono>
25*2c4fbc4cSPatrick Williams #include <iostream>
26*2c4fbc4cSPatrick Williams 
27afb39132SMatt Spinler using namespace witherspoon::power;
2856d90a89SMatt Spinler using namespace phosphor::logging;
29afb39132SMatt Spinler 
main(int argc,char ** argv)30afb39132SMatt Spinler int main(int argc, char** argv)
31afb39132SMatt Spinler {
32afb39132SMatt Spinler     ArgumentParser args{argc, argv};
33afb39132SMatt Spinler     auto action = args["action"];
34afb39132SMatt Spinler 
357084927eSMatt Spinler     if ((action != "pgood-monitor") && (action != "runtime-monitor"))
36afb39132SMatt Spinler     {
37afb39132SMatt Spinler         std::cerr << "Invalid action\n";
38afb39132SMatt Spinler         args.usage(argv);
39afb39132SMatt Spinler         exit(EXIT_FAILURE);
40afb39132SMatt Spinler     }
41afb39132SMatt Spinler 
42afb39132SMatt Spinler     auto i = strtoul(args["interval"].c_str(), nullptr, 10);
43afb39132SMatt Spinler     if (i == 0)
44afb39132SMatt Spinler     {
45afb39132SMatt Spinler         std::cerr << "Invalid interval value\n";
46afb39132SMatt Spinler         exit(EXIT_FAILURE);
47afb39132SMatt Spinler     }
48afb39132SMatt Spinler 
4978c5c2b0SMatt Spinler     std::chrono::milliseconds interval{i};
50afb39132SMatt Spinler 
51e5a8b473SWilliam A. Kennington III     auto event = sdeventplus::Event::get_default();
5256d90a89SMatt Spinler     auto bus = sdbusplus::bus::new_default();
5356d90a89SMatt Spinler     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
5456d90a89SMatt Spinler 
55a8269652SMatt Spinler     auto device = std::make_unique<UCD90160>(0, bus);
56b2d72511SMatt Spinler 
577084927eSMatt Spinler     std::unique_ptr<DeviceMonitor> monitor;
5856d90a89SMatt Spinler 
597084927eSMatt Spinler     if (action == "pgood-monitor")
607084927eSMatt Spinler     {
617084927eSMatt Spinler         // If PGOOD doesn't turn on within a certain
627084927eSMatt Spinler         // time, analyze the device for errors
63f0f02b9aSMatt Spinler         monitor = std::make_unique<PGOODMonitor>(std::move(device), bus, event,
64f0f02b9aSMatt Spinler                                                  interval);
657084927eSMatt Spinler     }
667084927eSMatt Spinler     else // runtime-monitor
677084927eSMatt Spinler     {
687084927eSMatt Spinler         // Continuously monitor this device both by polling
697084927eSMatt Spinler         // and on 'power lost' signals.
70f0f02b9aSMatt Spinler         monitor = std::make_unique<RuntimeMonitor>(std::move(device), bus,
71f0f02b9aSMatt Spinler                                                    event, interval);
727084927eSMatt Spinler     }
737084927eSMatt Spinler 
747084927eSMatt Spinler     return monitor->run();
75afb39132SMatt Spinler }
76