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> 19*e5a8b473SWilliam A. Kennington III #include <sdeventplus/event.hpp> 20afb39132SMatt Spinler #include "argument.hpp" 2156d90a89SMatt Spinler #include "pgood_monitor.hpp" 227084927eSMatt Spinler #include "runtime_monitor.hpp" 23b2d72511SMatt Spinler #include "ucd90160.hpp" 24afb39132SMatt Spinler 25afb39132SMatt Spinler using namespace witherspoon::power; 2656d90a89SMatt Spinler using namespace phosphor::logging; 27afb39132SMatt Spinler 28afb39132SMatt Spinler int main(int argc, char** argv) 29afb39132SMatt Spinler { 30afb39132SMatt Spinler ArgumentParser args{argc, argv}; 31afb39132SMatt Spinler auto action = args["action"]; 32afb39132SMatt Spinler 337084927eSMatt Spinler if ((action != "pgood-monitor") && (action != "runtime-monitor")) 34afb39132SMatt Spinler { 35afb39132SMatt Spinler std::cerr << "Invalid action\n"; 36afb39132SMatt Spinler args.usage(argv); 37afb39132SMatt Spinler exit(EXIT_FAILURE); 38afb39132SMatt Spinler } 39afb39132SMatt Spinler 40afb39132SMatt Spinler auto i = strtoul(args["interval"].c_str(), nullptr, 10); 41afb39132SMatt Spinler if (i == 0) 42afb39132SMatt Spinler { 43afb39132SMatt Spinler std::cerr << "Invalid interval value\n"; 44afb39132SMatt Spinler exit(EXIT_FAILURE); 45afb39132SMatt Spinler } 46afb39132SMatt Spinler 4778c5c2b0SMatt Spinler std::chrono::milliseconds interval{i}; 48afb39132SMatt Spinler 49*e5a8b473SWilliam A. Kennington III auto event = sdeventplus::Event::get_default(); 5056d90a89SMatt Spinler auto bus = sdbusplus::bus::new_default(); 5156d90a89SMatt Spinler bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 5256d90a89SMatt Spinler 53a8269652SMatt Spinler auto device = std::make_unique<UCD90160>(0, bus); 54b2d72511SMatt Spinler 557084927eSMatt Spinler std::unique_ptr<DeviceMonitor> monitor; 5656d90a89SMatt Spinler 577084927eSMatt Spinler if (action == "pgood-monitor") 587084927eSMatt Spinler { 597084927eSMatt Spinler //If PGOOD doesn't turn on within a certain 607084927eSMatt Spinler //time, analyze the device for errors 617084927eSMatt Spinler monitor = std::make_unique<PGOODMonitor>( 627084927eSMatt Spinler std::move(device), bus, event, interval); 637084927eSMatt Spinler } 647084927eSMatt Spinler else //runtime-monitor 657084927eSMatt Spinler { 667084927eSMatt Spinler //Continuously monitor this device both by polling 677084927eSMatt Spinler //and on 'power lost' signals. 687084927eSMatt Spinler monitor = std::make_unique<RuntimeMonitor>( 697084927eSMatt Spinler std::move(device), bus, event, interval); 707084927eSMatt Spinler } 717084927eSMatt Spinler 727084927eSMatt Spinler return monitor->run(); 73afb39132SMatt Spinler } 74