blob: f580b32efd2b3df6e4b778a01bf866fb3ec63309 [file] [log] [blame]
Tom Joseph807c7e82017-02-09 19:49:38 +05301#pragma once
2
Vernon Mauery2085ae02021-06-10 11:51:00 -07003#include "main.hpp"
Vernon Mauery9e801a22018-10-12 13:20:49 -07004#include "sol/sol_manager.hpp"
5
Tom Joseph807c7e82017-02-09 19:49:38 +05306#include <systemd/sd-event.h>
Vernon Mauery9e801a22018-10-12 13:20:49 -07007
Vernon Mauerycbccb052018-10-24 13:52:22 -07008#include <boost/asio/io_context.hpp>
George Liubc8958f2022-07-04 09:29:49 +08009#include <sdbusplus/asio/connection.hpp>
10
Tom Joseph807c7e82017-02-09 19:49:38 +053011#include <chrono>
12#include <map>
Andrew Geissler7408e762020-05-17 08:56:05 -050013#include <string>
Vernon Maueryd92bc322019-03-15 15:24:30 -070014
15namespace ipmi
16{
17namespace rmcpp
18{
19constexpr uint16_t defaultPort = 623;
20} // namespace rmcpp
21} // namespace ipmi
Tom Joseph807c7e82017-02-09 19:49:38 +053022
23namespace eventloop
24{
Alvin Wang8c0bf982019-10-21 10:30:07 +080025using DbusObjectPath = std::string;
26using DbusService = std::string;
27using DbusInterface = std::string;
28using ObjectTree =
29 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
30using 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
33constexpr uint16_t VLAN_VALUE_MASK = 0x0fff;
34constexpr auto MAPPER_BUS_NAME = "xyz.openbmc_project.ObjectMapper";
35constexpr auto MAPPER_OBJ = "/xyz/openbmc_project/object_mapper";
36constexpr auto MAPPER_INTF = "xyz.openbmc_project.ObjectMapper";
37constexpr auto PATH_ROOT = "/xyz/openbmc_project/network";
38constexpr auto INTF_VLAN = "xyz.openbmc_project.Network.VLAN";
39constexpr auto INTF_ETHERNET = "xyz.openbmc_project.Network.EthernetInterface";
40constexpr auto METHOD_GET = "Get";
41constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties";
Tom Joseph807c7e82017-02-09 19:49:38 +053042
Tom Joseph807c7e82017-02-09 19:49:38 +053043class EventLoop
44{
Vernon Mauery2085ae02021-06-10 11:51:00 -070045 private:
46 struct Private
George Liubc8958f2022-07-04 09:29:49 +080047 {};
Vernon Mauery2085ae02021-06-10 11:51:00 -070048
Vernon Mauery9e801a22018-10-12 13:20:49 -070049 public:
Vernon Mauery2085ae02021-06-10 11:51:00 -070050 EventLoop(std::shared_ptr<boost::asio::io_context>& io, const Private&) :
51 io(io)
George Liubc8958f2022-07-04 09:29:49 +080052 {}
Vernon Mauerycbccb052018-10-24 13:52:22 -070053 EventLoop() = delete;
Vernon Mauery9e801a22018-10-12 13:20:49 -070054 ~EventLoop() = default;
55 EventLoop(const EventLoop&) = delete;
56 EventLoop& operator=(const EventLoop&) = delete;
57 EventLoop(EventLoop&&) = delete;
58 EventLoop& operator=(EventLoop&&) = delete;
Tom Joseph807c7e82017-02-09 19:49:38 +053059
Vernon Mauery2085ae02021-06-10 11:51:00 -070060 /**
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 Mauery9e801a22018-10-12 13:20:49 -070076 /** @brief Initialise the event loop and add the handler for incoming
77 * IPMI packets.
Vernon Mauery9e801a22018-10-12 13:20:49 -070078 *
79 * @return EXIT_SUCCESS on success and EXIT_FAILURE on failure.
80 */
Vernon Mauerycbccb052018-10-24 13:52:22 -070081 int startEventLoop();
Tom Joseph807c7e82017-02-09 19:49:38 +053082
Vernon Maueryd92bc322019-03-15 15:24:30 -070083 /** @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 Mauery9e801a22018-10-12 13:20:49 -070090 private:
Vernon Mauery7a0142c2018-11-09 08:38:16 -080091 /** @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 Wang8c0bf982019-10-21 10:30:07 +080097 /** @brief get vlanid */
98 int getVLANID(const std::string channel);
99
Vernon Mauerycbccb052018-10-24 13:52:22 -0700100 /** @brief boost::asio io context to run with
101 */
102 std::shared_ptr<boost::asio::io_context> io;
103
Vernon Mauery7a0142c2018-11-09 08:38:16 -0800104 /** @brief boost::asio udp socket
Vernon Mauery9e801a22018-10-12 13:20:49 -0700105 */
Vernon Mauery7a0142c2018-11-09 08:38:16 -0800106 std::shared_ptr<boost::asio::ip::udp::socket> udpSocket = nullptr;
Tom Joseph807c7e82017-02-09 19:49:38 +0530107};
108
109} // namespace eventloop