xref: /openbmc/phosphor-power/power-sequencer/main.cpp (revision f0f02b9ae21ece3d691c1bbea348b468787ed2ff)
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 
21*f0f02b9aSMatt Spinler #include <chrono>
22*f0f02b9aSMatt Spinler #include <iostream>
23*f0f02b9aSMatt Spinler #include <phosphor-logging/log.hpp>
24*f0f02b9aSMatt Spinler #include <sdeventplus/event.hpp>
25*f0f02b9aSMatt Spinler 
26afb39132SMatt Spinler using namespace witherspoon::power;
2756d90a89SMatt Spinler using namespace phosphor::logging;
28afb39132SMatt Spinler 
29afb39132SMatt Spinler int main(int argc, char** argv)
30afb39132SMatt Spinler {
31afb39132SMatt Spinler     ArgumentParser args{argc, argv};
32afb39132SMatt Spinler     auto action = args["action"];
33afb39132SMatt Spinler 
347084927eSMatt Spinler     if ((action != "pgood-monitor") && (action != "runtime-monitor"))
35afb39132SMatt Spinler     {
36afb39132SMatt Spinler         std::cerr << "Invalid action\n";
37afb39132SMatt Spinler         args.usage(argv);
38afb39132SMatt Spinler         exit(EXIT_FAILURE);
39afb39132SMatt Spinler     }
40afb39132SMatt Spinler 
41afb39132SMatt Spinler     auto i = strtoul(args["interval"].c_str(), nullptr, 10);
42afb39132SMatt Spinler     if (i == 0)
43afb39132SMatt Spinler     {
44afb39132SMatt Spinler         std::cerr << "Invalid interval value\n";
45afb39132SMatt Spinler         exit(EXIT_FAILURE);
46afb39132SMatt Spinler     }
47afb39132SMatt Spinler 
4878c5c2b0SMatt Spinler     std::chrono::milliseconds interval{i};
49afb39132SMatt Spinler 
50e5a8b473SWilliam A. Kennington III     auto event = sdeventplus::Event::get_default();
5156d90a89SMatt Spinler     auto bus = sdbusplus::bus::new_default();
5256d90a89SMatt Spinler     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
5356d90a89SMatt Spinler 
54a8269652SMatt Spinler     auto device = std::make_unique<UCD90160>(0, bus);
55b2d72511SMatt Spinler 
567084927eSMatt Spinler     std::unique_ptr<DeviceMonitor> monitor;
5756d90a89SMatt Spinler 
587084927eSMatt Spinler     if (action == "pgood-monitor")
597084927eSMatt Spinler     {
607084927eSMatt Spinler         // If PGOOD doesn't turn on within a certain
617084927eSMatt Spinler         // time, analyze the device for errors
62*f0f02b9aSMatt Spinler         monitor = std::make_unique<PGOODMonitor>(std::move(device), bus, event,
63*f0f02b9aSMatt Spinler                                                  interval);
647084927eSMatt Spinler     }
657084927eSMatt Spinler     else // runtime-monitor
667084927eSMatt Spinler     {
677084927eSMatt Spinler         // Continuously monitor this device both by polling
687084927eSMatt Spinler         // and on 'power lost' signals.
69*f0f02b9aSMatt Spinler         monitor = std::make_unique<RuntimeMonitor>(std::move(device), bus,
70*f0f02b9aSMatt Spinler                                                    event, interval);
717084927eSMatt Spinler     }
727084927eSMatt Spinler 
737084927eSMatt Spinler     return monitor->run();
74afb39132SMatt Spinler }
75