Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 3 | #include "main.hpp" |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 4 | #include "sol/sol_manager.hpp" |
| 5 | |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 6 | #include <systemd/sd-event.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 7 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 8 | #include <boost/asio/io_context.hpp> |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 9 | #include <sdbusplus/asio/connection.hpp> |
| 10 | |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 11 | #include <chrono> |
| 12 | #include <map> |
Andrew Geissler | 7408e76 | 2020-05-17 08:56:05 -0500 | [diff] [blame] | 13 | #include <string> |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ipmi |
| 16 | { |
| 17 | namespace rmcpp |
| 18 | { |
| 19 | constexpr uint16_t defaultPort = 623; |
| 20 | } // namespace rmcpp |
| 21 | } // namespace ipmi |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 22 | |
| 23 | namespace eventloop |
| 24 | { |
Alvin Wang | 8c0bf98 | 2019-10-21 10:30:07 +0800 | [diff] [blame] | 25 | using DbusObjectPath = std::string; |
| 26 | using DbusService = std::string; |
| 27 | using DbusInterface = std::string; |
| 28 | using ObjectTree = |
| 29 | std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>; |
| 30 | using Value = std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, |
| 31 | int64_t, uint64_t, double, std::string>; |
| 32 | // VLANs are a 12-bit value |
| 33 | constexpr uint16_t VLAN_VALUE_MASK = 0x0fff; |
| 34 | constexpr auto MAPPER_BUS_NAME = "xyz.openbmc_project.ObjectMapper"; |
| 35 | constexpr auto MAPPER_OBJ = "/xyz/openbmc_project/object_mapper"; |
| 36 | constexpr auto MAPPER_INTF = "xyz.openbmc_project.ObjectMapper"; |
| 37 | constexpr auto PATH_ROOT = "/xyz/openbmc_project/network"; |
| 38 | constexpr auto INTF_VLAN = "xyz.openbmc_project.Network.VLAN"; |
| 39 | constexpr auto INTF_ETHERNET = "xyz.openbmc_project.Network.EthernetInterface"; |
| 40 | constexpr auto METHOD_GET = "Get"; |
| 41 | constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties"; |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 42 | |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 43 | class EventLoop |
| 44 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 45 | private: |
| 46 | struct Private |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 47 | {}; |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 48 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 49 | public: |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 50 | EventLoop(std::shared_ptr<boost::asio::io_context>& io, const Private&) : |
| 51 | io(io) |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 52 | {} |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 53 | EventLoop() = delete; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 54 | ~EventLoop() = default; |
| 55 | EventLoop(const EventLoop&) = delete; |
| 56 | EventLoop& operator=(const EventLoop&) = delete; |
| 57 | EventLoop(EventLoop&&) = delete; |
| 58 | EventLoop& operator=(EventLoop&&) = delete; |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 59 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 60 | /** |
| 61 | * @brief Get a reference to the singleton EventLoop |
| 62 | * |
| 63 | * @return EventLoop reference |
| 64 | */ |
| 65 | static EventLoop& get() |
| 66 | { |
| 67 | static std::shared_ptr<EventLoop> ptr = nullptr; |
| 68 | if (!ptr) |
| 69 | { |
| 70 | std::shared_ptr<boost::asio::io_context> io = getIo(); |
| 71 | ptr = std::make_shared<EventLoop>(io, Private()); |
| 72 | } |
| 73 | return *ptr; |
| 74 | } |
| 75 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 76 | /** @brief Initialise the event loop and add the handler for incoming |
| 77 | * IPMI packets. |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 78 | * |
| 79 | * @return EXIT_SUCCESS on success and EXIT_FAILURE on failure. |
| 80 | */ |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 81 | int startEventLoop(); |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 82 | |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 83 | /** @brief Set up the socket (if systemd has not already) and |
| 84 | * make sure that the bus name matches the specified channel |
| 85 | */ |
| 86 | int setupSocket(std::shared_ptr<sdbusplus::asio::connection>& bus, |
| 87 | std::string iface, |
| 88 | uint16_t reqPort = ipmi::rmcpp::defaultPort); |
| 89 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 90 | private: |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 91 | /** @brief async handler for incoming udp packets */ |
| 92 | void handleRmcpPacket(); |
| 93 | |
| 94 | /** @brief register the async handler for incoming udp packets */ |
| 95 | void startRmcpReceive(); |
| 96 | |
Alvin Wang | 8c0bf98 | 2019-10-21 10:30:07 +0800 | [diff] [blame] | 97 | /** @brief get vlanid */ |
| 98 | int getVLANID(const std::string channel); |
| 99 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 100 | /** @brief boost::asio io context to run with |
| 101 | */ |
| 102 | std::shared_ptr<boost::asio::io_context> io; |
| 103 | |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 104 | /** @brief boost::asio udp socket |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 105 | */ |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 106 | std::shared_ptr<boost::asio::ip::udp::socket> udpSocket = nullptr; |
Tom Joseph | 807c7e8 | 2017-02-09 19:49:38 +0530 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace eventloop |