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