| 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 | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 64 | static int charAccTimerHandler(sd_event_source* s, uint64_t usec, | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 65 |                                void* userdata) | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 66 | { | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 67 |     auto bufferSize = std::get<sol::Manager&>(singletonPool).dataBuffer.size(); | 
 | 68 |  | 
 | 69 |     try | 
 | 70 |     { | 
| Patrick Venture | a65e30d | 2018-10-26 17:55:08 -0700 | [diff] [blame] | 71 |         // The instance is hardcoded to 1, in the case of supporting multiple | 
 | 72 |         // payload instances we would need to populate it from userdata | 
 | 73 |         uint8_t instance = 1; | 
 | 74 |  | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 75 |         if (bufferSize > 0) | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 76 |         { | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 77 |             auto& context = | 
 | 78 |                 std::get<sol::Manager&>(singletonPool).getContext(instance); | 
| Tom Joseph | 7306314 | 2017-03-14 18:20:20 +0530 | [diff] [blame] | 79 |  | 
| Patrick Venture | a65e30d | 2018-10-26 17:55:08 -0700 | [diff] [blame] | 80 |             int rc = context.sendOutboundPayload(); | 
| Tom Joseph | 7306314 | 2017-03-14 18:20:20 +0530 | [diff] [blame] | 81 |  | 
 | 82 |             if (rc == 0) | 
 | 83 |             { | 
 | 84 |                 return 0; | 
 | 85 |             } | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 86 |         } | 
 | 87 |  | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 88 |         std::get<eventloop::EventLoop&>(singletonPool) | 
 | 89 |             .switchTimer(instance, Timers::ACCUMULATE, true); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 90 |     } | 
 | 91 |     catch (std::exception& e) | 
 | 92 |     { | 
 | 93 |         log<level::ERR>(e.what()); | 
 | 94 |     } | 
 | 95 |  | 
 | 96 |     return 0; | 
 | 97 | } | 
 | 98 |  | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 99 | static int retryTimerHandler(sd_event_source* s, uint64_t usec, void* userdata) | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 100 | { | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 101 |     try | 
 | 102 |     { | 
| Patrick Venture | a65e30d | 2018-10-26 17:55:08 -0700 | [diff] [blame] | 103 |         // The instance is hardcoded to 1, in the case of supporting multiple | 
 | 104 |         // payload instances we would need to populate it from userdata | 
 | 105 |         uint8_t instance = 1; | 
 | 106 |  | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 107 |         auto& context = | 
 | 108 |             std::get<sol::Manager&>(singletonPool).getContext(instance); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 109 |  | 
 | 110 |         if (context.retryCounter) | 
 | 111 |         { | 
 | 112 |             --context.retryCounter; | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 113 |             std::get<eventloop::EventLoop&>(singletonPool) | 
 | 114 |                 .switchTimer(instance, Timers::RETRY, true); | 
| Tom Joseph | 2fd466f | 2017-03-30 08:16:47 +0530 | [diff] [blame] | 115 |             context.resendPayload(sol::Context::noClear); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 116 |         } | 
 | 117 |         else | 
 | 118 |         { | 
 | 119 |             context.retryCounter = context.maxRetryCount; | 
| Tom Joseph | 2fd466f | 2017-03-30 08:16:47 +0530 | [diff] [blame] | 120 |             context.resendPayload(sol::Context::clear); | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 121 |             std::get<eventloop::EventLoop&>(singletonPool) | 
 | 122 |                 .switchTimer(instance, Timers::RETRY, false); | 
 | 123 |             std::get<eventloop::EventLoop&>(singletonPool) | 
 | 124 |                 .switchTimer(instance, Timers::ACCUMULATE, true); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 125 |         } | 
 | 126 |     } | 
 | 127 |     catch (std::exception& e) | 
 | 128 |     { | 
 | 129 |         log<level::ERR>(e.what()); | 
 | 130 |     } | 
 | 131 |  | 
 | 132 |     return 0; | 
 | 133 | } | 
 | 134 |  | 
| Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 135 | int EventLoop::startEventLoop() | 
| Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 136 | { | 
| Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 137 |     sdbusplus::asio::sd_event_wrapper sdEvents(*io); | 
 | 138 |     event = sdEvents.get(); | 
| Marri Devender Rao | 3ecf0a1 | 2017-07-13 08:07:22 -0500 | [diff] [blame] | 139 |  | 
| Vernon Mauery | 22c8a21 | 2018-10-24 14:51:23 -0700 | [diff] [blame] | 140 |     // set up boost::asio signal handling | 
 | 141 |     boost::asio::signal_set signals(*io, SIGINT, SIGTERM); | 
| Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 142 |     signals.async_wait( | 
 | 143 |         [this](const boost::system::error_code& error, int signalNumber) { | 
 | 144 |             udpSocket->cancel(); | 
 | 145 |             udpSocket->close(); | 
 | 146 |             io->stop(); | 
 | 147 |         }); | 
| Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 148 |  | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 149 |     // Create our own socket if SysD did not supply one. | 
| Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 150 |     int listensFdCount = sd_listen_fds(0); | 
 | 151 |     if (listensFdCount == 1) | 
| Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 152 |     { | 
| Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 153 |         if (sd_is_socket(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_DGRAM, -1)) | 
 | 154 |         { | 
 | 155 |             udpSocket = std::make_shared<boost::asio::ip::udp::socket>( | 
 | 156 |                 *io, boost::asio::ip::udp::v6(), SD_LISTEN_FDS_START); | 
 | 157 |         } | 
| Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 158 |     } | 
| Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 159 |     else if (listensFdCount > 1) | 
| Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 160 |     { | 
 | 161 |         log<level::ERR>("Too many file descriptors received"); | 
| Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 162 |         return EXIT_FAILURE; | 
| Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 163 |     } | 
| Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 164 |     if (!udpSocket) | 
 | 165 |     { | 
 | 166 |         udpSocket = std::make_shared<boost::asio::ip::udp::socket>( | 
 | 167 |             *io, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v6(), | 
 | 168 |                                                 IPMI_STD_PORT)); | 
 | 169 |         if (!udpSocket) | 
 | 170 |         { | 
 | 171 |             log<level::ERR>("Failed to start listening on RMCP socket"); | 
 | 172 |             return EXIT_FAILURE; | 
 | 173 |         } | 
 | 174 |     } | 
 | 175 |     startRmcpReceive(); | 
| Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 176 |  | 
| Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 177 |     io->run(); | 
| Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 178 |  | 
| Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 179 |     return EXIT_SUCCESS; | 
| Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 180 | } | 
 | 181 |  | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 182 | void EventLoop::startSOLPayloadInstance(uint8_t payloadInst, | 
 | 183 |                                         IntervalType accumulateInterval, | 
 | 184 |                                         IntervalType retryInterval) | 
 | 185 | { | 
 | 186 |     auto instance = payloadInst; | 
 | 187 |     sd_event_source* accTimerSource = nullptr; | 
 | 188 |     sd_event_source* retryTimerSource = nullptr; | 
 | 189 |     int rc = 0; | 
 | 190 |     uint64_t currentTime = 0; | 
 | 191 |  | 
 | 192 |     rc = sd_event_now(event, CLOCK_MONOTONIC, ¤tTime); | 
 | 193 |     if (rc < 0) | 
 | 194 |     { | 
 | 195 |         log<level::ERR>("Failed to get the current timestamp", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 196 |                         entry("RC=%d", rc)); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 197 |         throw std::runtime_error("Failed to get current timestamp"); | 
 | 198 |     } | 
 | 199 |  | 
 | 200 |     // Create character accumulate timer | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 201 |     rc = sd_event_add_time(event, &accTimerSource, CLOCK_MONOTONIC, | 
 | 202 |                            currentTime + accumulateInterval.count(), 0, | 
 | 203 |                            charAccTimerHandler, static_cast<void*>(&instance)); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 204 |     if (rc < 0) | 
 | 205 |     { | 
 | 206 |         log<level::ERR>("Failed to setup the accumulate timer", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 207 |                         entry("RC=%d", rc)); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 208 |         throw std::runtime_error("Failed to setup accumulate timer"); | 
 | 209 |     } | 
 | 210 |  | 
 | 211 |     // Create retry interval timer and add to the event loop | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 212 |     rc = sd_event_add_time(event, &retryTimerSource, CLOCK_MONOTONIC, | 
 | 213 |                            currentTime + retryInterval.count(), 0, | 
 | 214 |                            retryTimerHandler, static_cast<void*>(&instance)); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 215 |     if (rc < 0) | 
 | 216 |     { | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 217 |         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] | 218 |         throw std::runtime_error("Failed to setup retry timer"); | 
 | 219 |     } | 
 | 220 |  | 
 | 221 |     // Enable the Character Accumulate Timer | 
 | 222 |     rc = sd_event_source_set_enabled(accTimerSource, SD_EVENT_ONESHOT); | 
 | 223 |     if (rc < 0) | 
 | 224 |     { | 
 | 225 |         log<level::ERR>("Failed to enable the accumulate timer", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 226 |                         entry("RC=%d", rc)); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 227 |         throw std::runtime_error("Failed to enable accumulate timer"); | 
 | 228 |     } | 
 | 229 |  | 
 | 230 |     // Disable the Retry Interval Timer | 
 | 231 |     rc = sd_event_source_set_enabled(retryTimerSource, SD_EVENT_OFF); | 
 | 232 |     if (rc < 0) | 
 | 233 |     { | 
 | 234 |         log<level::ERR>("Failed to disable the retry timer", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 235 |                         entry("RC=%d", rc)); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 236 |         throw std::runtime_error("Failed to disable retry timer"); | 
 | 237 |     } | 
 | 238 |  | 
 | 239 |     EventSource accEventSource(accTimerSource); | 
 | 240 |     EventSource retryEventSource(retryTimerSource); | 
 | 241 |     accTimerSource = nullptr; | 
 | 242 |     retryTimerSource = nullptr; | 
 | 243 |  | 
 | 244 |     TimerMap timer; | 
 | 245 |     timer.emplace(Timers::ACCUMULATE, std::make_tuple(std::move(accEventSource), | 
 | 246 |                                                       accumulateInterval)); | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 247 |     timer.emplace(Timers::RETRY, | 
 | 248 |                   std::make_tuple(std::move(retryEventSource), retryInterval)); | 
| Tom Joseph | e989c36 | 2017-03-14 17:16:50 +0530 | [diff] [blame] | 249 |     payloadInfo.emplace(instance, std::move(timer)); | 
 | 250 | } | 
 | 251 |  | 
| Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 252 | void EventLoop::stopSOLPayloadInstance(uint8_t payloadInst) | 
 | 253 | { | 
 | 254 |     auto iter = payloadInfo.find(payloadInst); | 
 | 255 |     if (iter == payloadInfo.end()) | 
 | 256 |     { | 
 | 257 |         log<level::ERR>("SOL Payload instance not found", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 258 |                         entry("PAYLOADINST=%d", payloadInst)); | 
| Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 259 |         throw std::runtime_error("SOL payload instance not found"); | 
 | 260 |     } | 
 | 261 |  | 
 | 262 |     int rc = 0; | 
 | 263 |  | 
 | 264 |     /* Destroy the character accumulate timer event source */ | 
 | 265 |     rc = sd_event_source_set_enabled( | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 266 |         (std::get<0>(iter->second.at(Timers::ACCUMULATE))).get(), SD_EVENT_OFF); | 
| Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 267 |     if (rc < 0) | 
 | 268 |     { | 
 | 269 |         log<level::ERR>("Failed to disable the character accumulate timer", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 270 |                         entry("RC=%d", rc)); | 
| Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 271 |         payloadInfo.erase(payloadInst); | 
 | 272 |         throw std::runtime_error("Failed to disable accumulate timer"); | 
 | 273 |     } | 
 | 274 |  | 
 | 275 |     /* Destroy the retry interval timer event source */ | 
 | 276 |     rc = sd_event_source_set_enabled( | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 277 |         (std::get<0>(iter->second.at(Timers::RETRY))).get(), SD_EVENT_OFF); | 
| Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 278 |     if (rc < 0) | 
 | 279 |     { | 
 | 280 |         log<level::ERR>("Failed to disable the retry timer", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 281 |                         entry("RC=%d", rc)); | 
| Tom Joseph | a891eb7 | 2017-03-14 17:25:01 +0530 | [diff] [blame] | 282 |         payloadInfo.erase(payloadInst); | 
 | 283 |         throw std::runtime_error("Failed to disable retry timer"); | 
 | 284 |     } | 
 | 285 |  | 
 | 286 |     payloadInfo.erase(payloadInst); | 
 | 287 | } | 
 | 288 |  | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 289 | void EventLoop::switchTimer(uint8_t payloadInst, Timers type, bool status) | 
| Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 290 | { | 
 | 291 |     auto iter = payloadInfo.find(payloadInst); | 
 | 292 |     if (iter == payloadInfo.end()) | 
 | 293 |     { | 
 | 294 |         log<level::ERR>("SOL Payload instance not found", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 295 |                         entry("PAYLOADINST=%d", payloadInst)); | 
| Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 296 |         throw std::runtime_error("SOL Payload instance not found"); | 
 | 297 |     } | 
 | 298 |  | 
 | 299 |     int rc = 0; | 
 | 300 |     auto source = (std::get<0>(iter->second.at(type))).get(); | 
 | 301 |     auto interval = std::get<1>(iter->second.at(type)); | 
 | 302 |  | 
 | 303 |     // Turn OFF the timer | 
 | 304 |     if (!status) | 
 | 305 |     { | 
 | 306 |         rc = sd_event_source_set_enabled(source, SD_EVENT_OFF); | 
 | 307 |         if (rc < 0) | 
 | 308 |         { | 
 | 309 |             log<level::ERR>("Failed to disable the timer", entry("RC=%d", rc)); | 
 | 310 |             throw std::runtime_error("Failed to disable timer"); | 
 | 311 |         } | 
 | 312 |         return; | 
 | 313 |     } | 
 | 314 |  | 
 | 315 |     // Turn ON the timer | 
 | 316 |     uint64_t currentTime = 0; | 
 | 317 |     rc = sd_event_now(event, CLOCK_MONOTONIC, ¤tTime); | 
 | 318 |     if (rc < 0) | 
 | 319 |     { | 
 | 320 |         log<level::ERR>("Failed to get the current timestamp", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 321 |                         entry("RC=%d", rc)); | 
| Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 322 |         throw std::runtime_error("Failed to get current timestamp"); | 
 | 323 |     } | 
 | 324 |  | 
 | 325 |     rc = sd_event_source_set_time(source, currentTime + interval.count()); | 
 | 326 |     if (rc < 0) | 
 | 327 |     { | 
 | 328 |         log<level::ERR>("sd_event_source_set_time function failed", | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 329 |                         entry("RC=%d", rc)); | 
| Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 330 |         throw std::runtime_error("sd_event_source_set_time function failed"); | 
 | 331 |     } | 
 | 332 |  | 
 | 333 |     rc = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); | 
 | 334 |     if (rc < 0) | 
 | 335 |     { | 
| Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 336 |         log<level::ERR>("Failed to enable the timer", entry("RC=%d", rc)); | 
| Tom Joseph | 0a0d8f6 | 2017-03-14 17:04:58 +0530 | [diff] [blame] | 337 |         throw std::runtime_error("Failed to enable timer"); | 
 | 338 |     } | 
 | 339 | } | 
 | 340 |  | 
| Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 341 | } // namespace eventloop |