Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 3 | #include "sol/sol_manager.hpp" |
| 4 | |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 5 | #include <systemd/sd-event.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 6 | |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 7 | #include <chrono> |
| 8 | #include <map> |
| 9 | |
| 10 | namespace eventloop |
| 11 | { |
| 12 | |
| 13 | /** @struct EventSourceDeleter |
| 14 | * |
| 15 | * Custom deleter for the sd_event_source*. |
| 16 | */ |
| 17 | struct EventSourceDeleter |
| 18 | { |
| 19 | void operator()(sd_event_source* event) const |
| 20 | { |
| 21 | event = sd_event_source_unref(event); |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | using EventSource = std::unique_ptr<sd_event_source, EventSourceDeleter>; |
| 26 | using IntervalType = std::chrono::microseconds; |
| 27 | |
| 28 | /** @enum Timers |
| 29 | * |
| 30 | * For SOL functioning, there are two timers involved. The character accumulate |
| 31 | * interval timer is the amount of time that the BMC will wait before |
| 32 | * transmitting a partial SOL packet. The retry interval timer is the time that |
| 33 | * BMC will wait before the first retry and the time between retries when |
| 34 | * sending SOL packets to the remote console. |
| 35 | */ |
| 36 | enum class Timers |
| 37 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 38 | ACCUMULATE, /**< Character Accumulate Timer */ |
| 39 | RETRY, /**< Retry Interval Timer */ |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | class EventLoop |
| 43 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 44 | public: |
| 45 | EventLoop() = default; |
| 46 | ~EventLoop() = default; |
| 47 | EventLoop(const EventLoop&) = delete; |
| 48 | EventLoop& operator=(const EventLoop&) = delete; |
| 49 | EventLoop(EventLoop&&) = delete; |
| 50 | EventLoop& operator=(EventLoop&&) = delete; |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 51 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 52 | /** @brief Timer Map |
| 53 | * |
| 54 | * The key for the timer map is the timer type. There are two types of |
| 55 | * timers, character accumulate timer and retry interval timer. The |
| 56 | * entries in the values is the event source for the timer and the |
| 57 | * interval. |
| 58 | */ |
| 59 | using TimerMap = std::map<Timers, std::tuple<EventSource, IntervalType>>; |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 60 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 61 | /** @brief SOL Payload Map. |
| 62 | * |
| 63 | * The key for the payload map is the payload instance, the entries in |
| 64 | * the value are a map of timers. |
| 65 | */ |
| 66 | using PayloadMap = std::map<uint8_t, TimerMap>; |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 67 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 68 | /** @brief Initialise the event loop and add the handler for incoming |
| 69 | * IPMI packets. |
| 70 | * @param[in] events- sd bus event; |
| 71 | * |
| 72 | * @return EXIT_SUCCESS on success and EXIT_FAILURE on failure. |
| 73 | */ |
| 74 | int startEventLoop(sd_event* events); |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 75 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 76 | /** @brief Add host console I/O event source to the event loop. |
| 77 | * |
| 78 | * @param[in] fd - File descriptor for host console socket. |
| 79 | */ |
| 80 | void startHostConsole(const sol::CustomFD& fd); |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 81 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 82 | /** @brief Remove host console I/O event source. */ |
| 83 | void stopHostConsole(); |
Tom Joseph | 28d993a | 2017-04-21 20:47:53 +0530 | [diff] [blame] | 84 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 85 | /** @brief Initialize the timers for the SOL payload instance |
| 86 | * |
| 87 | * This API would add the Character accumulate interval timer event |
| 88 | * source and the retry interval timer event source for the SOL payload |
| 89 | * instance to the event loop. |
| 90 | * |
| 91 | * @param[in] payloadInst - SOL payload instance. |
| 92 | * @param[in] accumulateInterval - Character accumulate interval. |
| 93 | * @param[in] retryInterval - Retry interval. |
| 94 | */ |
| 95 | void startSOLPayloadInstance(uint8_t payloadInst, |
| 96 | IntervalType accumulateInterval, |
| 97 | IntervalType retryInterval); |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 98 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 99 | /** @brief Stop the timers for the SOL payload instance. |
| 100 | * |
| 101 | * This would remove the character accumulate interval timer event |
| 102 | * source and the retry interval timer event source from the event |
| 103 | * loop. |
| 104 | * |
| 105 | * @param[in] payloadInst - SOL payload instance |
| 106 | */ |
| 107 | void stopSOLPayloadInstance(uint8_t payloadInst); |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 108 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 109 | /** @brief Modify the timer event source to enable/disable. |
| 110 | * |
| 111 | * When the timer is enabled, the timer it set to fire again at |
| 112 | * timer's interval for the instance added to the event loop iteration |
| 113 | * timestamp. When the timer is disabled, the event source for the |
| 114 | * timer is disabled. |
| 115 | * |
| 116 | * @param[in] payloadInst - SOL payload instance. |
| 117 | * @param[in] type - Timer type. |
| 118 | * @param[in] status - on/off the event source. |
| 119 | */ |
| 120 | void switchTimer(uint8_t payloadInst, Timers type, bool status); |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 121 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 122 | /** @brief Modify the retry interval timer event source to enable/ |
| 123 | * disable |
| 124 | * |
| 125 | * When the timer is enabled, the timer it set to fire again at |
| 126 | * retry interval for the instance added to the event loop iteration |
| 127 | * timestamp. When the timer is disabled the event source for the |
| 128 | * retry interval timer is disabled. |
| 129 | * |
| 130 | * @param[in] payloadInst - SOL payload instance. |
| 131 | * @param[in] status - on/off the event source. |
| 132 | */ |
| 133 | void switchRetryTimer(uint8_t payloadInst, bool status); |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 134 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 135 | /** @brief Event loop object. */ |
| 136 | sd_event* event = nullptr; |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 137 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 138 | private: |
| 139 | /** @brief Event source object for host console. */ |
| 140 | EventSource hostConsole = nullptr; |
Ratan Gupta | 04edb9b | 2018-03-20 23:06:06 +0530 | [diff] [blame] | 141 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 142 | /** @brief Event source for the UDP socket listening on IPMI standard |
| 143 | * port. |
| 144 | */ |
| 145 | EventSource udpIPMI = nullptr; |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 146 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 147 | /** @brief Map to keep information regarding IPMI payload instance and |
| 148 | * timers for character accumulate interval and retry interval. |
| 149 | */ |
| 150 | PayloadMap payloadInfo; |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | } // namespace eventloop |