Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 1 | #include "sd_event_loop.hpp" |
| 2 | |
| 3 | #include "main.hpp" |
| 4 | #include "message_handler.hpp" |
| 5 | |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 6 | #include <netinet/in.h> |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 7 | #include <sys/ioctl.h> |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 8 | #include <sys/socket.h> |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 9 | #include <systemd/sd-daemon.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 10 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 11 | #include <boost/asio/io_context.hpp> |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 12 | #include <phosphor-logging/log.hpp> |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 13 | #include <sdbusplus/asio/sd_event.hpp> |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 14 | |
| 15 | namespace eventloop |
| 16 | { |
| 17 | using namespace phosphor::logging; |
| 18 | |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 19 | void EventLoop::handleRmcpPacket() |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 20 | { |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 21 | try |
| 22 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 23 | auto channelPtr = std::make_shared<udpsocket::Channel>(udpSocket); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 24 | |
| 25 | // Initialize the Message Handler with the socket channel |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 26 | auto msgHandler = std::make_shared<message::Handler>(channelPtr); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 27 | |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 28 | // Read the incoming IPMI packet |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 29 | std::shared_ptr<message::Message> inMessage(msgHandler->receive()); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 30 | if (inMessage == nullptr) |
| 31 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 32 | return; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Execute the Command |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 36 | std::shared_ptr<message::Message> outMessage = |
| 37 | msgHandler->executeCommand(inMessage); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 38 | if (outMessage == nullptr) |
| 39 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 40 | return; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 41 | } |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 42 | // Send the response IPMI Message |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 43 | msgHandler->send(outMessage); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 44 | } |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 45 | catch (const std::exception& e) |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 46 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 47 | log<level::ERR>("Executing the IPMI message failed", |
| 48 | entry("EXCEPTION=%s", e.what())); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 49 | } |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 50 | } |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 51 | |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 52 | void EventLoop::startRmcpReceive() |
| 53 | { |
| 54 | udpSocket->async_wait(boost::asio::socket_base::wait_read, |
| 55 | [this](const boost::system::error_code& ec) { |
| 56 | if (!ec) |
| 57 | { |
| 58 | io->post([this]() { startRmcpReceive(); }); |
| 59 | handleRmcpPacket(); |
| 60 | } |
| 61 | }); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 62 | } |
| 63 | |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 64 | static int consoleInputHandler(sd_event_source* es, int fd, uint32_t revents, |
| 65 | void* userdata) |
| 66 | { |
| 67 | try |
| 68 | { |
| 69 | int readSize = 0; |
| 70 | |
| 71 | if (ioctl(fd, FIONREAD, &readSize) < 0) |
| 72 | { |
| 73 | log<level::ERR>("ioctl failed for FIONREAD:", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 74 | entry("ERRNO=%d", errno)); |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | std::vector<uint8_t> buffer(readSize); |
| 79 | auto bufferSize = buffer.size(); |
| 80 | ssize_t readDataLen = 0; |
| 81 | |
| 82 | readDataLen = read(fd, buffer.data(), bufferSize); |
| 83 | |
| 84 | // Update the Console buffer with data read from the socket |
| 85 | if (readDataLen > 0) |
| 86 | { |
| 87 | buffer.resize(readDataLen); |
| 88 | std::get<sol::Manager&>(singletonPool).dataBuffer.write(buffer); |
| 89 | } |
| 90 | else if (readDataLen == 0) |
| 91 | { |
| 92 | log<level::ERR>("Connection Closed for host console socket"); |
| 93 | } |
| 94 | else if (readDataLen < 0) // Error |
| 95 | { |
| 96 | log<level::ERR>("Reading from host console socket failed:", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 97 | entry("ERRNO=%d", errno)); |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | catch (std::exception& e) |
| 101 | { |
| 102 | log<level::ERR>(e.what()); |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 108 | static int charAccTimerHandler(sd_event_source* s, uint64_t usec, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 109 | void* userdata) |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 110 | { |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 111 | auto bufferSize = std::get<sol::Manager&>(singletonPool).dataBuffer.size(); |
| 112 | |
| 113 | try |
| 114 | { |
Patrick Venture | a65e30d | 2018-10-26 17:55:08 -0700 | [diff] [blame] | 115 | // The instance is hardcoded to 1, in the case of supporting multiple |
| 116 | // payload instances we would need to populate it from userdata |
| 117 | uint8_t instance = 1; |
| 118 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 119 | if (bufferSize > 0) |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 120 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 121 | auto& context = |
| 122 | std::get<sol::Manager&>(singletonPool).getContext(instance); |
Tom Joseph | 7306314 | 2017-03-14 18:20:20 +0530 | [diff] [blame] | 123 | |
Patrick Venture | a65e30d | 2018-10-26 17:55:08 -0700 | [diff] [blame] | 124 | int rc = context.sendOutboundPayload(); |
Tom Joseph | 7306314 | 2017-03-14 18:20:20 +0530 | [diff] [blame] | 125 | |
| 126 | if (rc == 0) |
| 127 | { |
| 128 | return 0; |
| 129 | } |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 130 | } |
| 131 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 132 | std::get<eventloop::EventLoop&>(singletonPool) |
| 133 | .switchTimer(instance, Timers::ACCUMULATE, true); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 134 | } |
| 135 | catch (std::exception& e) |
| 136 | { |
| 137 | log<level::ERR>(e.what()); |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 143 | static int retryTimerHandler(sd_event_source* s, uint64_t usec, void* userdata) |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 144 | { |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 145 | try |
| 146 | { |
Patrick Venture | a65e30d | 2018-10-26 17:55:08 -0700 | [diff] [blame] | 147 | // The instance is hardcoded to 1, in the case of supporting multiple |
| 148 | // payload instances we would need to populate it from userdata |
| 149 | uint8_t instance = 1; |
| 150 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 151 | auto& context = |
| 152 | std::get<sol::Manager&>(singletonPool).getContext(instance); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 153 | |
| 154 | if (context.retryCounter) |
| 155 | { |
| 156 | --context.retryCounter; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 157 | std::get<eventloop::EventLoop&>(singletonPool) |
| 158 | .switchTimer(instance, Timers::RETRY, true); |
Tom Joseph | 2fd466f | 2017-03-30 08:16:47 +0530 | [diff] [blame] | 159 | context.resendPayload(sol::Context::noClear); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 160 | } |
| 161 | else |
| 162 | { |
| 163 | context.retryCounter = context.maxRetryCount; |
Tom Joseph | 2fd466f | 2017-03-30 08:16:47 +0530 | [diff] [blame] | 164 | context.resendPayload(sol::Context::clear); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 165 | std::get<eventloop::EventLoop&>(singletonPool) |
| 166 | .switchTimer(instance, Timers::RETRY, false); |
| 167 | std::get<eventloop::EventLoop&>(singletonPool) |
| 168 | .switchTimer(instance, Timers::ACCUMULATE, true); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | catch (std::exception& e) |
| 172 | { |
| 173 | log<level::ERR>(e.what()); |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 179 | int EventLoop::startEventLoop() |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 180 | { |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 181 | sdbusplus::asio::sd_event_wrapper sdEvents(*io); |
| 182 | event = sdEvents.get(); |
Marri Devender Rao | 3ecf0a1 | 2017-07-13 08:07:22 -0500 | [diff] [blame] | 183 | |
Vernon Mauery | 22c8a21 | 2018-10-24 14:51:23 -0700 | [diff] [blame] | 184 | // set up boost::asio signal handling |
| 185 | boost::asio::signal_set signals(*io, SIGINT, SIGTERM); |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 186 | signals.async_wait( |
| 187 | [this](const boost::system::error_code& error, int signalNumber) { |
| 188 | udpSocket->cancel(); |
| 189 | udpSocket->close(); |
| 190 | io->stop(); |
| 191 | }); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 192 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 193 | // Create our own socket if SysD did not supply one. |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 194 | int listensFdCount = sd_listen_fds(0); |
| 195 | if (listensFdCount == 1) |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 196 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 197 | if (sd_is_socket(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_DGRAM, -1)) |
| 198 | { |
| 199 | udpSocket = std::make_shared<boost::asio::ip::udp::socket>( |
| 200 | *io, boost::asio::ip::udp::v6(), SD_LISTEN_FDS_START); |
| 201 | } |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 202 | } |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 203 | else if (listensFdCount > 1) |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 204 | { |
| 205 | log<level::ERR>("Too many file descriptors received"); |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 206 | return EXIT_FAILURE; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 207 | } |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 208 | if (!udpSocket) |
| 209 | { |
| 210 | udpSocket = std::make_shared<boost::asio::ip::udp::socket>( |
| 211 | *io, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v6(), |
| 212 | IPMI_STD_PORT)); |
| 213 | if (!udpSocket) |
| 214 | { |
| 215 | log<level::ERR>("Failed to start listening on RMCP socket"); |
| 216 | return EXIT_FAILURE; |
| 217 | } |
| 218 | } |
| 219 | startRmcpReceive(); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 220 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 221 | io->run(); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 222 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 223 | return EXIT_SUCCESS; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 224 | } |
| 225 | |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 226 | void EventLoop::startHostConsole(const sol::CustomFD& fd) |
| 227 | { |
| 228 | int rc = 0; |
| 229 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 230 | if ((fd() == -1) || hostConsole.get()) |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 231 | { |
| 232 | throw std::runtime_error("Console descriptor already added"); |
| 233 | } |
| 234 | |
| 235 | sd_event_source* source = nullptr; |
| 236 | |
| 237 | // Add the fd to the event loop for EPOLLIN |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 238 | rc = sd_event_add_io(event, &source, fd(), EPOLLIN, consoleInputHandler, |
| 239 | nullptr); |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 240 | if (rc < 0) |
| 241 | { |
| 242 | throw std::runtime_error("Failed to add socket descriptor"); |
| 243 | } |
| 244 | |
| 245 | hostConsole.reset(source); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 246 | source = nullptr; |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 247 | } |
| 248 | |
Tom Joseph | cfbd43f | 2017-03-14 16:14:03 +0530 | [diff] [blame] | 249 | void EventLoop::stopHostConsole() |
| 250 | { |
Tom Joseph | cfbd43f | 2017-03-14 16:14:03 +0530 | [diff] [blame] | 251 | if (hostConsole.get()) |
| 252 | { |
| 253 | // Disable the host console payload |
Patrick Venture | a65e30d | 2018-10-26 17:55:08 -0700 | [diff] [blame] | 254 | int rc = sd_event_source_set_enabled(hostConsole.get(), SD_EVENT_OFF); |
Tom Joseph | cfbd43f | 2017-03-14 16:14:03 +0530 | [diff] [blame] | 255 | if (rc < 0) |
| 256 | { |
| 257 | log<level::ERR>("Failed to disable the host console socket", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 258 | entry("RC=%d", rc)); |
Tom Joseph | cfbd43f | 2017-03-14 16:14:03 +0530 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | hostConsole.reset(); |
| 262 | } |
| 263 | } |
| 264 | |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 265 | void EventLoop::startSOLPayloadInstance(uint8_t payloadInst, |
| 266 | IntervalType accumulateInterval, |
| 267 | IntervalType retryInterval) |
| 268 | { |
| 269 | auto instance = payloadInst; |
| 270 | sd_event_source* accTimerSource = nullptr; |
| 271 | sd_event_source* retryTimerSource = nullptr; |
| 272 | int rc = 0; |
| 273 | uint64_t currentTime = 0; |
| 274 | |
| 275 | rc = sd_event_now(event, CLOCK_MONOTONIC, ¤tTime); |
| 276 | if (rc < 0) |
| 277 | { |
| 278 | log<level::ERR>("Failed to get the current timestamp", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 279 | entry("RC=%d", rc)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 280 | throw std::runtime_error("Failed to get current timestamp"); |
| 281 | } |
| 282 | |
| 283 | // Create character accumulate timer |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 284 | rc = sd_event_add_time(event, &accTimerSource, CLOCK_MONOTONIC, |
| 285 | currentTime + accumulateInterval.count(), 0, |
| 286 | charAccTimerHandler, static_cast<void*>(&instance)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 287 | if (rc < 0) |
| 288 | { |
| 289 | log<level::ERR>("Failed to setup the accumulate timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 290 | entry("RC=%d", rc)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 291 | throw std::runtime_error("Failed to setup accumulate timer"); |
| 292 | } |
| 293 | |
| 294 | // Create retry interval timer and add to the event loop |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 295 | rc = sd_event_add_time(event, &retryTimerSource, CLOCK_MONOTONIC, |
| 296 | currentTime + retryInterval.count(), 0, |
| 297 | retryTimerHandler, static_cast<void*>(&instance)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 298 | if (rc < 0) |
| 299 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 300 | log<level::ERR>("Failed to setup the retry timer", entry("RC=%d", rc)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 301 | throw std::runtime_error("Failed to setup retry timer"); |
| 302 | } |
| 303 | |
| 304 | // Enable the Character Accumulate Timer |
| 305 | rc = sd_event_source_set_enabled(accTimerSource, SD_EVENT_ONESHOT); |
| 306 | if (rc < 0) |
| 307 | { |
| 308 | log<level::ERR>("Failed to enable the accumulate timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 309 | entry("RC=%d", rc)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 310 | throw std::runtime_error("Failed to enable accumulate timer"); |
| 311 | } |
| 312 | |
| 313 | // Disable the Retry Interval Timer |
| 314 | rc = sd_event_source_set_enabled(retryTimerSource, SD_EVENT_OFF); |
| 315 | if (rc < 0) |
| 316 | { |
| 317 | log<level::ERR>("Failed to disable the retry timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 318 | entry("RC=%d", rc)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 319 | throw std::runtime_error("Failed to disable retry timer"); |
| 320 | } |
| 321 | |
| 322 | EventSource accEventSource(accTimerSource); |
| 323 | EventSource retryEventSource(retryTimerSource); |
| 324 | accTimerSource = nullptr; |
| 325 | retryTimerSource = nullptr; |
| 326 | |
| 327 | TimerMap timer; |
| 328 | timer.emplace(Timers::ACCUMULATE, std::make_tuple(std::move(accEventSource), |
| 329 | accumulateInterval)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 330 | timer.emplace(Timers::RETRY, |
| 331 | std::make_tuple(std::move(retryEventSource), retryInterval)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 332 | payloadInfo.emplace(instance, std::move(timer)); |
| 333 | } |
| 334 | |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 335 | void EventLoop::stopSOLPayloadInstance(uint8_t payloadInst) |
| 336 | { |
| 337 | auto iter = payloadInfo.find(payloadInst); |
| 338 | if (iter == payloadInfo.end()) |
| 339 | { |
| 340 | log<level::ERR>("SOL Payload instance not found", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 341 | entry("PAYLOADINST=%d", payloadInst)); |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 342 | throw std::runtime_error("SOL payload instance not found"); |
| 343 | } |
| 344 | |
| 345 | int rc = 0; |
| 346 | |
| 347 | /* Destroy the character accumulate timer event source */ |
| 348 | rc = sd_event_source_set_enabled( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 349 | (std::get<0>(iter->second.at(Timers::ACCUMULATE))).get(), SD_EVENT_OFF); |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 350 | if (rc < 0) |
| 351 | { |
| 352 | log<level::ERR>("Failed to disable the character accumulate timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 353 | entry("RC=%d", rc)); |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 354 | payloadInfo.erase(payloadInst); |
| 355 | throw std::runtime_error("Failed to disable accumulate timer"); |
| 356 | } |
| 357 | |
| 358 | /* Destroy the retry interval timer event source */ |
| 359 | rc = sd_event_source_set_enabled( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 360 | (std::get<0>(iter->second.at(Timers::RETRY))).get(), SD_EVENT_OFF); |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 361 | if (rc < 0) |
| 362 | { |
| 363 | log<level::ERR>("Failed to disable the retry timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 364 | entry("RC=%d", rc)); |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 365 | payloadInfo.erase(payloadInst); |
| 366 | throw std::runtime_error("Failed to disable retry timer"); |
| 367 | } |
| 368 | |
| 369 | payloadInfo.erase(payloadInst); |
| 370 | } |
| 371 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 372 | void EventLoop::switchTimer(uint8_t payloadInst, Timers type, bool status) |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 373 | { |
| 374 | auto iter = payloadInfo.find(payloadInst); |
| 375 | if (iter == payloadInfo.end()) |
| 376 | { |
| 377 | log<level::ERR>("SOL Payload instance not found", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 378 | entry("PAYLOADINST=%d", payloadInst)); |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 379 | throw std::runtime_error("SOL Payload instance not found"); |
| 380 | } |
| 381 | |
| 382 | int rc = 0; |
| 383 | auto source = (std::get<0>(iter->second.at(type))).get(); |
| 384 | auto interval = std::get<1>(iter->second.at(type)); |
| 385 | |
| 386 | // Turn OFF the timer |
| 387 | if (!status) |
| 388 | { |
| 389 | rc = sd_event_source_set_enabled(source, SD_EVENT_OFF); |
| 390 | if (rc < 0) |
| 391 | { |
| 392 | log<level::ERR>("Failed to disable the timer", entry("RC=%d", rc)); |
| 393 | throw std::runtime_error("Failed to disable timer"); |
| 394 | } |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | // Turn ON the timer |
| 399 | uint64_t currentTime = 0; |
| 400 | rc = sd_event_now(event, CLOCK_MONOTONIC, ¤tTime); |
| 401 | if (rc < 0) |
| 402 | { |
| 403 | log<level::ERR>("Failed to get the current timestamp", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 404 | entry("RC=%d", rc)); |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 405 | throw std::runtime_error("Failed to get current timestamp"); |
| 406 | } |
| 407 | |
| 408 | rc = sd_event_source_set_time(source, currentTime + interval.count()); |
| 409 | if (rc < 0) |
| 410 | { |
| 411 | log<level::ERR>("sd_event_source_set_time function failed", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 412 | entry("RC=%d", rc)); |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 413 | throw std::runtime_error("sd_event_source_set_time function failed"); |
| 414 | } |
| 415 | |
| 416 | rc = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); |
| 417 | if (rc < 0) |
| 418 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 419 | log<level::ERR>("Failed to enable the timer", entry("RC=%d", rc)); |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 420 | throw std::runtime_error("Failed to enable timer"); |
| 421 | } |
| 422 | } |
| 423 | |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 424 | } // namespace eventloop |