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