Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 1 | #pragma once |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 2 | #include "device.hpp" |
| 3 | |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
| 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <sdbusplus/server.hpp> |
William A. Kennington III | 1a0c917 | 2018-10-18 17:57:49 -0700 | [diff] [blame] | 7 | #include <sdeventplus/clock.hpp> |
William A. Kennington III | e5a8b47 | 2018-10-18 00:40:04 -0700 | [diff] [blame] | 8 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 1a0c917 | 2018-10-18 17:57:49 -0700 | [diff] [blame] | 9 | #include <sdeventplus/utility/timer.hpp> |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 10 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 11 | namespace phosphor |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 12 | { |
| 13 | namespace power |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | |
| 18 | /** |
| 19 | * @class DeviceMonitor |
| 20 | * |
| 21 | * Monitors a power device for faults by calling Device::analyze() |
| 22 | * on an interval. Do the monitoring by calling run(). |
| 23 | * May be overridden to provide more functionality. |
| 24 | */ |
| 25 | class DeviceMonitor |
| 26 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 27 | public: |
| 28 | DeviceMonitor() = delete; |
Brad Bishop | 11cb672 | 2019-09-03 14:30:06 -0400 | [diff] [blame] | 29 | virtual ~DeviceMonitor() = default; |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 30 | DeviceMonitor(const DeviceMonitor&) = delete; |
| 31 | DeviceMonitor& operator=(const DeviceMonitor&) = delete; |
| 32 | DeviceMonitor(DeviceMonitor&&) = delete; |
| 33 | DeviceMonitor& operator=(DeviceMonitor&&) = delete; |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 34 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * @param[in] d - device to monitor |
| 39 | * @param[in] e - event object |
| 40 | * @param[in] i - polling interval in ms |
| 41 | */ |
| 42 | DeviceMonitor(std::unique_ptr<Device>&& d, const sdeventplus::Event& e, |
| 43 | std::chrono::milliseconds i) : |
| 44 | device(std::move(d)), |
| 45 | timer(e, std::bind(&DeviceMonitor::analyze, this), i) |
Adriana Kobylak | 0c9a33d | 2021-09-13 18:05:09 +0000 | [diff] [blame] | 46 | {} |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 47 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 48 | /** |
| 49 | * Starts the timer to monitor the device on an interval. |
| 50 | */ |
| 51 | virtual int run() |
| 52 | { |
| 53 | return timer.get_event().loop(); |
| 54 | } |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 55 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 56 | protected: |
| 57 | /** |
| 58 | * Analyzes the device for faults |
| 59 | * |
| 60 | * Runs in the timer callback |
| 61 | * |
| 62 | * Override if desired |
| 63 | */ |
| 64 | virtual void analyze() |
| 65 | { |
| 66 | device->analyze(); |
| 67 | } |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 68 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 69 | /** |
| 70 | * The device to run the analysis on |
| 71 | */ |
| 72 | std::unique_ptr<Device> device; |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 73 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 74 | /** |
| 75 | * The timer that runs fault check polls. |
| 76 | */ |
| 77 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 80 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 81 | } // namespace phosphor |