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; |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 179 | int listen_fd; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 180 | sigset_t ss; |
| 181 | sd_event_source* source = nullptr; |
| 182 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 183 | sdbusplus::asio::sd_event_wrapper sdEvents(*io); |
| 184 | event = sdEvents.get(); |
Marri Devender Rao | 3ecf0a1 | 2017-07-13 08:07:22 -0500 | [diff] [blame] | 185 | |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 186 | if (sigemptyset(&ss) < 0 || sigaddset(&ss, SIGTERM) < 0 || |
| 187 | sigaddset(&ss, SIGINT) < 0) |
| 188 | { |
| 189 | r = -errno; |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 190 | return EXIT_FAILURE; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* Block SIGTERM first, so that the event loop can handle it */ |
| 194 | if (sigprocmask(SIG_BLOCK, &ss, nullptr) < 0) |
| 195 | { |
| 196 | r = -errno; |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 197 | return EXIT_FAILURE; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* Let's make use of the default handler and "floating" reference features |
| 201 | * of sd_event_add_signal() */ |
| 202 | r = sd_event_add_signal(event, nullptr, SIGTERM, nullptr, nullptr); |
| 203 | if (r < 0) |
| 204 | { |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 205 | return EXIT_FAILURE; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | r = sd_event_add_signal(event, nullptr, SIGINT, nullptr, nullptr); |
| 209 | if (r < 0) |
| 210 | { |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 211 | return EXIT_FAILURE; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 212 | } |
| 213 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 214 | // Create our own socket if SysD did not supply one. |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 215 | listen_fd = sd_listen_fds(0); |
| 216 | if (listen_fd == 1) |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 217 | { |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 218 | fd = SD_LISTEN_FDS_START; |
| 219 | } |
| 220 | else if (listen_fd > 1) |
| 221 | { |
| 222 | log<level::ERR>("Too many file descriptors received"); |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 223 | return 1; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 224 | } |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 225 | else |
| 226 | { |
| 227 | struct sockaddr_in address; |
| 228 | if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == 0) |
| 229 | { |
| 230 | r = -errno; |
| 231 | log<level::ERR>("Unable to manually open socket"); |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 232 | return EXIT_FAILURE; |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 233 | } |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 234 | |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 235 | address.sin_family = AF_INET; |
| 236 | address.sin_addr.s_addr = INADDR_ANY; |
| 237 | address.sin_port = htons(IPMI_STD_PORT); |
| 238 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 239 | if (bind(fd, (struct sockaddr*)&address, sizeof(address)) < 0) |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 240 | { |
| 241 | r = -errno; |
| 242 | log<level::ERR>("Unable to bind socket"); |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 243 | close(fd); |
| 244 | return EXIT_FAILURE; |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 247 | |
| 248 | r = sd_event_add_io(event, &source, fd, EPOLLIN, udp623Handler, nullptr); |
| 249 | if (r < 0) |
| 250 | { |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 251 | close(fd); |
| 252 | return EXIT_FAILURE; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | udpIPMI.reset(source); |
| 256 | source = nullptr; |
| 257 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 258 | io->run(); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 259 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 260 | return EXIT_SUCCESS; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 261 | } |
| 262 | |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 263 | void EventLoop::startHostConsole(const sol::CustomFD& fd) |
| 264 | { |
| 265 | int rc = 0; |
| 266 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 267 | if ((fd() == -1) || hostConsole.get()) |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 268 | { |
| 269 | throw std::runtime_error("Console descriptor already added"); |
| 270 | } |
| 271 | |
| 272 | sd_event_source* source = nullptr; |
| 273 | |
| 274 | // Add the fd to the event loop for EPOLLIN |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 275 | rc = sd_event_add_io(event, &source, fd(), EPOLLIN, consoleInputHandler, |
| 276 | nullptr); |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 277 | if (rc < 0) |
| 278 | { |
| 279 | throw std::runtime_error("Failed to add socket descriptor"); |
| 280 | } |
| 281 | |
| 282 | hostConsole.reset(source); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 283 | source = nullptr; |
Tom Joseph | 56f24e9 | 2017-03-14 16:06:31 +0530 | [diff] [blame] | 284 | } |
| 285 | |
Tom Joseph | cfbd43f | 2017-03-14 16:14:03 +0530 | [diff] [blame] | 286 | void EventLoop::stopHostConsole() |
| 287 | { |
Tom Joseph | cfbd43f | 2017-03-14 16:14:03 +0530 | [diff] [blame] | 288 | if (hostConsole.get()) |
| 289 | { |
| 290 | // Disable the host console payload |
Patrick Venture | a65e30d | 2018-10-26 17:55:08 -0700 | [diff] [blame] | 291 | int rc = sd_event_source_set_enabled(hostConsole.get(), SD_EVENT_OFF); |
Tom Joseph | cfbd43f | 2017-03-14 16:14:03 +0530 | [diff] [blame] | 292 | if (rc < 0) |
| 293 | { |
| 294 | log<level::ERR>("Failed to disable the host console socket", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 295 | entry("RC=%d", rc)); |
Tom Joseph | cfbd43f | 2017-03-14 16:14:03 +0530 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | hostConsole.reset(); |
| 299 | } |
| 300 | } |
| 301 | |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 302 | void EventLoop::startSOLPayloadInstance(uint8_t payloadInst, |
| 303 | IntervalType accumulateInterval, |
| 304 | IntervalType retryInterval) |
| 305 | { |
| 306 | auto instance = payloadInst; |
| 307 | sd_event_source* accTimerSource = nullptr; |
| 308 | sd_event_source* retryTimerSource = nullptr; |
| 309 | int rc = 0; |
| 310 | uint64_t currentTime = 0; |
| 311 | |
| 312 | rc = sd_event_now(event, CLOCK_MONOTONIC, ¤tTime); |
| 313 | if (rc < 0) |
| 314 | { |
| 315 | log<level::ERR>("Failed to get the current timestamp", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 316 | entry("RC=%d", rc)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 317 | throw std::runtime_error("Failed to get current timestamp"); |
| 318 | } |
| 319 | |
| 320 | // Create character accumulate timer |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 321 | rc = sd_event_add_time(event, &accTimerSource, CLOCK_MONOTONIC, |
| 322 | currentTime + accumulateInterval.count(), 0, |
| 323 | charAccTimerHandler, static_cast<void*>(&instance)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 324 | if (rc < 0) |
| 325 | { |
| 326 | log<level::ERR>("Failed to setup the accumulate timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 327 | entry("RC=%d", rc)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 328 | throw std::runtime_error("Failed to setup accumulate timer"); |
| 329 | } |
| 330 | |
| 331 | // Create retry interval timer and add to the event loop |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 332 | rc = sd_event_add_time(event, &retryTimerSource, CLOCK_MONOTONIC, |
| 333 | currentTime + retryInterval.count(), 0, |
| 334 | retryTimerHandler, static_cast<void*>(&instance)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 335 | if (rc < 0) |
| 336 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 337 | 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] | 338 | throw std::runtime_error("Failed to setup retry timer"); |
| 339 | } |
| 340 | |
| 341 | // Enable the Character Accumulate Timer |
| 342 | rc = sd_event_source_set_enabled(accTimerSource, SD_EVENT_ONESHOT); |
| 343 | if (rc < 0) |
| 344 | { |
| 345 | log<level::ERR>("Failed to enable the accumulate timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 346 | entry("RC=%d", rc)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 347 | throw std::runtime_error("Failed to enable accumulate timer"); |
| 348 | } |
| 349 | |
| 350 | // Disable the Retry Interval Timer |
| 351 | rc = sd_event_source_set_enabled(retryTimerSource, SD_EVENT_OFF); |
| 352 | if (rc < 0) |
| 353 | { |
| 354 | log<level::ERR>("Failed to disable the retry timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 355 | entry("RC=%d", rc)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 356 | throw std::runtime_error("Failed to disable retry timer"); |
| 357 | } |
| 358 | |
| 359 | EventSource accEventSource(accTimerSource); |
| 360 | EventSource retryEventSource(retryTimerSource); |
| 361 | accTimerSource = nullptr; |
| 362 | retryTimerSource = nullptr; |
| 363 | |
| 364 | TimerMap timer; |
| 365 | timer.emplace(Timers::ACCUMULATE, std::make_tuple(std::move(accEventSource), |
| 366 | accumulateInterval)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 367 | timer.emplace(Timers::RETRY, |
| 368 | std::make_tuple(std::move(retryEventSource), retryInterval)); |
Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 369 | payloadInfo.emplace(instance, std::move(timer)); |
| 370 | } |
| 371 | |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 372 | void EventLoop::stopSOLPayloadInstance(uint8_t payloadInst) |
| 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 | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 379 | throw std::runtime_error("SOL payload instance not found"); |
| 380 | } |
| 381 | |
| 382 | int rc = 0; |
| 383 | |
| 384 | /* Destroy the character accumulate timer event source */ |
| 385 | rc = sd_event_source_set_enabled( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 386 | (std::get<0>(iter->second.at(Timers::ACCUMULATE))).get(), SD_EVENT_OFF); |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 387 | if (rc < 0) |
| 388 | { |
| 389 | log<level::ERR>("Failed to disable the character accumulate timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 390 | entry("RC=%d", rc)); |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 391 | payloadInfo.erase(payloadInst); |
| 392 | throw std::runtime_error("Failed to disable accumulate timer"); |
| 393 | } |
| 394 | |
| 395 | /* Destroy the retry interval timer event source */ |
| 396 | rc = sd_event_source_set_enabled( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 397 | (std::get<0>(iter->second.at(Timers::RETRY))).get(), SD_EVENT_OFF); |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 398 | if (rc < 0) |
| 399 | { |
| 400 | log<level::ERR>("Failed to disable the retry timer", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 401 | entry("RC=%d", rc)); |
Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 402 | payloadInfo.erase(payloadInst); |
| 403 | throw std::runtime_error("Failed to disable retry timer"); |
| 404 | } |
| 405 | |
| 406 | payloadInfo.erase(payloadInst); |
| 407 | } |
| 408 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 409 | void EventLoop::switchTimer(uint8_t payloadInst, Timers type, bool status) |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 410 | { |
| 411 | auto iter = payloadInfo.find(payloadInst); |
| 412 | if (iter == payloadInfo.end()) |
| 413 | { |
| 414 | log<level::ERR>("SOL Payload instance not found", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 415 | entry("PAYLOADINST=%d", payloadInst)); |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 416 | throw std::runtime_error("SOL Payload instance not found"); |
| 417 | } |
| 418 | |
| 419 | int rc = 0; |
| 420 | auto source = (std::get<0>(iter->second.at(type))).get(); |
| 421 | auto interval = std::get<1>(iter->second.at(type)); |
| 422 | |
| 423 | // Turn OFF the timer |
| 424 | if (!status) |
| 425 | { |
| 426 | rc = sd_event_source_set_enabled(source, SD_EVENT_OFF); |
| 427 | if (rc < 0) |
| 428 | { |
| 429 | log<level::ERR>("Failed to disable the timer", entry("RC=%d", rc)); |
| 430 | throw std::runtime_error("Failed to disable timer"); |
| 431 | } |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | // Turn ON the timer |
| 436 | uint64_t currentTime = 0; |
| 437 | rc = sd_event_now(event, CLOCK_MONOTONIC, ¤tTime); |
| 438 | if (rc < 0) |
| 439 | { |
| 440 | log<level::ERR>("Failed to get the current timestamp", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 441 | entry("RC=%d", rc)); |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 442 | throw std::runtime_error("Failed to get current timestamp"); |
| 443 | } |
| 444 | |
| 445 | rc = sd_event_source_set_time(source, currentTime + interval.count()); |
| 446 | if (rc < 0) |
| 447 | { |
| 448 | log<level::ERR>("sd_event_source_set_time function failed", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 449 | entry("RC=%d", rc)); |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 450 | throw std::runtime_error("sd_event_source_set_time function failed"); |
| 451 | } |
| 452 | |
| 453 | rc = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); |
| 454 | if (rc < 0) |
| 455 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 456 | log<level::ERR>("Failed to enable the timer", entry("RC=%d", rc)); |
Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 457 | throw std::runtime_error("Failed to enable timer"); |
| 458 | } |
| 459 | } |
| 460 | |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 461 | } // namespace eventloop |