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