Matt Spinler | afb3913 | 2017-08-14 13:53:07 -0500 | [diff] [blame] | 1 | /** |
| 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 Spinler | afb3913 | 2017-08-14 13:53:07 -0500 | [diff] [blame] | 16 | #include "argument.hpp" |
Matt Spinler | 56d90a8 | 2017-08-14 14:05:11 -0500 | [diff] [blame] | 17 | #include "pgood_monitor.hpp" |
Matt Spinler | 7084927 | 2017-08-22 09:14:40 -0500 | [diff] [blame] | 18 | #include "runtime_monitor.hpp" |
Matt Spinler | b2d7251 | 2017-08-22 09:07:01 -0500 | [diff] [blame] | 19 | #include "ucd90160.hpp" |
Matt Spinler | afb3913 | 2017-08-14 13:53:07 -0500 | [diff] [blame] | 20 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 21 | #include <phosphor-logging/log.hpp> |
| 22 | #include <sdeventplus/event.hpp> |
| 23 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 24 | #include <chrono> |
| 25 | #include <iostream> |
| 26 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 27 | using namespace phosphor::power; |
Matt Spinler | 56d90a8 | 2017-08-14 14:05:11 -0500 | [diff] [blame] | 28 | using namespace phosphor::logging; |
Matt Spinler | afb3913 | 2017-08-14 13:53:07 -0500 | [diff] [blame] | 29 | |
| 30 | int main(int argc, char** argv) |
| 31 | { |
| 32 | ArgumentParser args{argc, argv}; |
| 33 | auto action = args["action"]; |
| 34 | |
Matt Spinler | 7084927 | 2017-08-22 09:14:40 -0500 | [diff] [blame] | 35 | if ((action != "pgood-monitor") && (action != "runtime-monitor")) |
Matt Spinler | afb3913 | 2017-08-14 13:53:07 -0500 | [diff] [blame] | 36 | { |
| 37 | std::cerr << "Invalid action\n"; |
| 38 | args.usage(argv); |
| 39 | exit(EXIT_FAILURE); |
| 40 | } |
| 41 | |
| 42 | auto i = strtoul(args["interval"].c_str(), nullptr, 10); |
| 43 | if (i == 0) |
| 44 | { |
| 45 | std::cerr << "Invalid interval value\n"; |
| 46 | exit(EXIT_FAILURE); |
| 47 | } |
| 48 | |
Matt Spinler | 78c5c2b | 2017-12-14 10:42:07 -0600 | [diff] [blame] | 49 | std::chrono::milliseconds interval{i}; |
Matt Spinler | afb3913 | 2017-08-14 13:53:07 -0500 | [diff] [blame] | 50 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 51 | auto event = sdeventplus::Event::get_default(); |
Matt Spinler | 56d90a8 | 2017-08-14 14:05:11 -0500 | [diff] [blame] | 52 | auto bus = sdbusplus::bus::new_default(); |
| 53 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 54 | |
Matt Spinler | a826965 | 2017-09-19 15:13:28 -0500 | [diff] [blame] | 55 | auto device = std::make_unique<UCD90160>(0, bus); |
Matt Spinler | b2d7251 | 2017-08-22 09:07:01 -0500 | [diff] [blame] | 56 | |
Matt Spinler | 7084927 | 2017-08-22 09:14:40 -0500 | [diff] [blame] | 57 | std::unique_ptr<DeviceMonitor> monitor; |
Matt Spinler | 56d90a8 | 2017-08-14 14:05:11 -0500 | [diff] [blame] | 58 | |
Matt Spinler | 7084927 | 2017-08-22 09:14:40 -0500 | [diff] [blame] | 59 | if (action == "pgood-monitor") |
| 60 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 61 | // If PGOOD doesn't turn on within a certain |
| 62 | // time, analyze the device for errors |
| 63 | monitor = std::make_unique<PGOODMonitor>(std::move(device), bus, event, |
| 64 | interval); |
Matt Spinler | 7084927 | 2017-08-22 09:14:40 -0500 | [diff] [blame] | 65 | } |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 66 | else // runtime-monitor |
Matt Spinler | 7084927 | 2017-08-22 09:14:40 -0500 | [diff] [blame] | 67 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 68 | // Continuously monitor this device both by polling |
| 69 | // and on 'power lost' signals. |
| 70 | monitor = std::make_unique<RuntimeMonitor>(std::move(device), bus, |
| 71 | event, interval); |
Matt Spinler | 7084927 | 2017-08-22 09:14:40 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return monitor->run(); |
Matt Spinler | afb3913 | 2017-08-14 13:53:07 -0500 | [diff] [blame] | 75 | } |