xref: /openbmc/phosphor-power/power-sequencer/main.cpp (revision 78c5c2b0942d56ae051b242b2eed1afaf640d54a)
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 <chrono>
17afb39132SMatt Spinler #include <iostream>
1856d90a89SMatt Spinler #include <phosphor-logging/log.hpp>
19afb39132SMatt Spinler #include "argument.hpp"
2056d90a89SMatt Spinler #include "pgood_monitor.hpp"
217084927eSMatt Spinler #include "runtime_monitor.hpp"
22b2d72511SMatt Spinler #include "ucd90160.hpp"
23afb39132SMatt Spinler 
24afb39132SMatt Spinler using namespace witherspoon::power;
2556d90a89SMatt Spinler using namespace phosphor::logging;
26afb39132SMatt Spinler 
27afb39132SMatt Spinler int main(int argc, char** argv)
28afb39132SMatt Spinler {
29afb39132SMatt Spinler     ArgumentParser args{argc, argv};
30afb39132SMatt Spinler     auto action = args["action"];
31afb39132SMatt Spinler 
327084927eSMatt Spinler     if ((action != "pgood-monitor") && (action != "runtime-monitor"))
33afb39132SMatt Spinler     {
34afb39132SMatt Spinler         std::cerr << "Invalid action\n";
35afb39132SMatt Spinler         args.usage(argv);
36afb39132SMatt Spinler         exit(EXIT_FAILURE);
37afb39132SMatt Spinler     }
38afb39132SMatt Spinler 
39afb39132SMatt Spinler     auto i = strtoul(args["interval"].c_str(), nullptr, 10);
40afb39132SMatt Spinler     if (i == 0)
41afb39132SMatt Spinler     {
42afb39132SMatt Spinler         std::cerr << "Invalid interval value\n";
43afb39132SMatt Spinler         exit(EXIT_FAILURE);
44afb39132SMatt Spinler     }
45afb39132SMatt Spinler 
46*78c5c2b0SMatt Spinler     std::chrono::milliseconds interval{i};
47afb39132SMatt Spinler 
4856d90a89SMatt Spinler     sd_event* e = nullptr;
4956d90a89SMatt Spinler     auto r = sd_event_default(&e);
5056d90a89SMatt Spinler     if (r < 0)
5156d90a89SMatt Spinler     {
5256d90a89SMatt Spinler         log<level::ERR>("sd_event_default() failed",
5356d90a89SMatt Spinler                         entry("ERROR=%s", strerror(-r)));
5456d90a89SMatt Spinler         exit(EXIT_FAILURE);
5556d90a89SMatt Spinler     }
5656d90a89SMatt Spinler 
5756d90a89SMatt Spinler     event::Event event{e};
5856d90a89SMatt Spinler     auto bus = sdbusplus::bus::new_default();
5956d90a89SMatt Spinler     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
6056d90a89SMatt Spinler 
61a8269652SMatt Spinler     auto device = std::make_unique<UCD90160>(0, bus);
62b2d72511SMatt Spinler 
637084927eSMatt Spinler     std::unique_ptr<DeviceMonitor> monitor;
6456d90a89SMatt Spinler 
657084927eSMatt Spinler     if (action == "pgood-monitor")
667084927eSMatt Spinler     {
677084927eSMatt Spinler         //If PGOOD doesn't turn on within a certain
687084927eSMatt Spinler         //time, analyze the device for errors
697084927eSMatt Spinler         monitor = std::make_unique<PGOODMonitor>(
707084927eSMatt Spinler                 std::move(device), bus, event, interval);
717084927eSMatt Spinler     }
727084927eSMatt Spinler     else //runtime-monitor
737084927eSMatt Spinler     {
747084927eSMatt Spinler         //Continuously monitor this device both by polling
757084927eSMatt Spinler         //and on 'power lost' signals.
767084927eSMatt Spinler         monitor = std::make_unique<RuntimeMonitor>(
777084927eSMatt Spinler                 std::move(device), bus, event, interval);
787084927eSMatt Spinler     }
797084927eSMatt Spinler 
807084927eSMatt Spinler     return monitor->run();
81afb39132SMatt Spinler }
82