blob: 2faa7ad287e86f7f24b9967664790a59701cb357 [file] [log] [blame]
Tom Joseph807c7e82017-02-09 19:49:38 +05301#pragma once
2
Vernon Mauery9e801a22018-10-12 13:20:49 -07003#include "sol/sol_manager.hpp"
4
Tom Joseph807c7e82017-02-09 19:49:38 +05305#include <systemd/sd-event.h>
Vernon Mauery9e801a22018-10-12 13:20:49 -07006
Tom Joseph807c7e82017-02-09 19:49:38 +05307#include <chrono>
8#include <map>
9
10namespace eventloop
11{
12
13/** @struct EventSourceDeleter
14 *
15 * Custom deleter for the sd_event_source*.
16 */
17struct EventSourceDeleter
18{
19 void operator()(sd_event_source* event) const
20 {
21 event = sd_event_source_unref(event);
22 }
23};
24
25using EventSource = std::unique_ptr<sd_event_source, EventSourceDeleter>;
26using 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 */
36enum class Timers
37{
Vernon Mauery9e801a22018-10-12 13:20:49 -070038 ACCUMULATE, /**< Character Accumulate Timer */
39 RETRY, /**< Retry Interval Timer */
Tom Joseph807c7e82017-02-09 19:49:38 +053040};
41
42class EventLoop
43{
Vernon Mauery9e801a22018-10-12 13:20:49 -070044 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 Joseph807c7e82017-02-09 19:49:38 +053051
Vernon Mauery9e801a22018-10-12 13:20:49 -070052 /** @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 Joseph807c7e82017-02-09 19:49:38 +053060
Vernon Mauery9e801a22018-10-12 13:20:49 -070061 /** @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 Joseph807c7e82017-02-09 19:49:38 +053067
Vernon Mauery9e801a22018-10-12 13:20:49 -070068 /** @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 Joseph807c7e82017-02-09 19:49:38 +053075
Vernon Mauery9e801a22018-10-12 13:20:49 -070076 /** @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 Joseph807c7e82017-02-09 19:49:38 +053081
Vernon Mauery9e801a22018-10-12 13:20:49 -070082 /** @brief Remove host console I/O event source. */
83 void stopHostConsole();
Tom Joseph28d993a2017-04-21 20:47:53 +053084
Vernon Mauery9e801a22018-10-12 13:20:49 -070085 /** @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 Joseph807c7e82017-02-09 19:49:38 +053098
Vernon Mauery9e801a22018-10-12 13:20:49 -070099 /** @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 Joseph807c7e82017-02-09 19:49:38 +0530108
Vernon Mauery9e801a22018-10-12 13:20:49 -0700109 /** @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 Joseph807c7e82017-02-09 19:49:38 +0530121
Vernon Mauery9e801a22018-10-12 13:20:49 -0700122 /** @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 Joseph807c7e82017-02-09 19:49:38 +0530134
Vernon Mauery9e801a22018-10-12 13:20:49 -0700135 /** @brief Event loop object. */
136 sd_event* event = nullptr;
Tom Joseph807c7e82017-02-09 19:49:38 +0530137
Vernon Mauery9e801a22018-10-12 13:20:49 -0700138 private:
139 /** @brief Event source object for host console. */
140 EventSource hostConsole = nullptr;
Ratan Gupta04edb9b2018-03-20 23:06:06 +0530141
Vernon Mauery9e801a22018-10-12 13:20:49 -0700142 /** @brief Event source for the UDP socket listening on IPMI standard
143 * port.
144 */
145 EventSource udpIPMI = nullptr;
Tom Joseph807c7e82017-02-09 19:49:38 +0530146
Vernon Mauery9e801a22018-10-12 13:20:49 -0700147 /** @brief Map to keep information regarding IPMI payload instance and
148 * timers for character accumulate interval and retry interval.
149 */
150 PayloadMap payloadInfo;
Tom Joseph807c7e82017-02-09 19:49:38 +0530151};
152
153} // namespace eventloop