blob: 3e1ca52c46c8755c0d24625e787cd1be51eebff4 [file] [log] [blame]
Matt Spinlerafb39132017-08-14 13:53:07 -05001/**
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 */
Matt Spinlerafb39132017-08-14 13:53:07 -050016#include "argument.hpp"
Matt Spinler56d90a82017-08-14 14:05:11 -050017#include "pgood_monitor.hpp"
Matt Spinler70849272017-08-22 09:14:40 -050018#include "runtime_monitor.hpp"
Matt Spinlerb2d72512017-08-22 09:07:01 -050019#include "ucd90160.hpp"
Matt Spinlerafb39132017-08-14 13:53:07 -050020
Matt Spinlerf0f02b92018-10-25 16:12:43 -050021#include <chrono>
22#include <iostream>
23#include <phosphor-logging/log.hpp>
24#include <sdeventplus/event.hpp>
25
Matt Spinlerafb39132017-08-14 13:53:07 -050026using namespace witherspoon::power;
Matt Spinler56d90a82017-08-14 14:05:11 -050027using namespace phosphor::logging;
Matt Spinlerafb39132017-08-14 13:53:07 -050028
29int main(int argc, char** argv)
30{
31 ArgumentParser args{argc, argv};
32 auto action = args["action"];
33
Matt Spinler70849272017-08-22 09:14:40 -050034 if ((action != "pgood-monitor") && (action != "runtime-monitor"))
Matt Spinlerafb39132017-08-14 13:53:07 -050035 {
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
Matt Spinler78c5c2b2017-12-14 10:42:07 -060048 std::chrono::milliseconds interval{i};
Matt Spinlerafb39132017-08-14 13:53:07 -050049
Matt Spinlerf0f02b92018-10-25 16:12:43 -050050 auto event = sdeventplus::Event::get_default();
Matt Spinler56d90a82017-08-14 14:05:11 -050051 auto bus = sdbusplus::bus::new_default();
52 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
53
Matt Spinlera8269652017-09-19 15:13:28 -050054 auto device = std::make_unique<UCD90160>(0, bus);
Matt Spinlerb2d72512017-08-22 09:07:01 -050055
Matt Spinler70849272017-08-22 09:14:40 -050056 std::unique_ptr<DeviceMonitor> monitor;
Matt Spinler56d90a82017-08-14 14:05:11 -050057
Matt Spinler70849272017-08-22 09:14:40 -050058 if (action == "pgood-monitor")
59 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050060 // 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);
Matt Spinler70849272017-08-22 09:14:40 -050064 }
Matt Spinlerf0f02b92018-10-25 16:12:43 -050065 else // runtime-monitor
Matt Spinler70849272017-08-22 09:14:40 -050066 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050067 // 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);
Matt Spinler70849272017-08-22 09:14:40 -050071 }
72
73 return monitor->run();
Matt Spinlerafb39132017-08-14 13:53:07 -050074}