xref: /openbmc/phosphor-net-ipmid/main.cpp (revision afac94d1ffa271c9efb611a40f73a3e238164a9e)
1 #include "main.hpp"
2 
3 #include "comm_module.hpp"
4 #include "command/guid.hpp"
5 #include "command_table.hpp"
6 #include "message.hpp"
7 #include "message_handler.hpp"
8 #include "sd_event_loop.hpp"
9 #include "sessions_manager.hpp"
10 #include "socket_channel.hpp"
11 #include "sol_module.hpp"
12 
13 #include <assert.h>
14 #include <dirent.h>
15 #include <dlfcn.h>
16 #include <ipmid/api.h>
17 #include <systemd/sd-daemon.h>
18 #include <systemd/sd-event.h>
19 #include <unistd.h>
20 
21 #include <CLI/CLI.hpp>
22 #include <phosphor-logging/lg2.hpp>
23 #include <sdbusplus/asio/connection.hpp>
24 #include <user_channel/channel_layer.hpp>
25 
26 #include <tuple>
27 
28 static auto io = std::make_shared<boost::asio::io_context>();
getIo()29 std::shared_ptr<boost::asio::io_context> getIo()
30 {
31     return io;
32 }
33 
34 sd_bus* bus = nullptr;
35 
36 std::shared_ptr<sdbusplus::asio::connection> sdbusp;
37 
38 /*
39  * @brief Required by apphandler IPMI Provider Library
40  */
ipmid_get_sd_bus_connection()41 sd_bus* ipmid_get_sd_bus_connection()
42 {
43     return bus;
44 }
45 
46 /*
47  * @brief mechanism to get at sdbusplus object
48  */
getSdBus()49 std::shared_ptr<sdbusplus::asio::connection> getSdBus()
50 {
51     return sdbusp;
52 }
53 
54 static EInterfaceIndex currentInterfaceIndex = interfaceUnknown;
setInterfaceIndex(const std::string & channel)55 static void setInterfaceIndex(const std::string& channel)
56 {
57     try
58     {
59         currentInterfaceIndex =
60             static_cast<EInterfaceIndex>(ipmi::getChannelByName(channel));
61     }
62     catch (const std::exception& e)
63     {
64         lg2::error("Requested {NAME} is not a valid channel name: {ERROR}",
65                    "NAME", channel, "ERROR", e);
66     }
67 }
getInterfaceIndex(void)68 EInterfaceIndex getInterfaceIndex(void)
69 {
70     return currentInterfaceIndex;
71 }
72 
main(int argc,char * argv[])73 int main(int argc, char* argv[])
74 {
75     CLI::App app("KCS RMCP+ bridge");
76     std::string channel;
77     app.add_option("-c,--channel", channel, "channel name. e.g., eth0");
78     uint16_t port = ipmi::rmcpp::defaultPort;
79     app.add_option("-p,--port", port, "port number");
80     bool verbose = false;
81     app.add_option("-v,--verbose", verbose, "print more verbose output");
82     CLI11_PARSE(app, argc, argv);
83 
84     // Connect to system bus
85     auto rc = sd_bus_default_system(&bus);
86     if (rc < 0)
87     {
88         lg2::error("Failed to connect to system bus: {ERROR}", "ERROR",
89                    strerror(-rc));
90         return rc;
91     }
92     sdbusp = std::make_shared<sdbusplus::asio::connection>(*io, bus);
93 
94     auto& loop = eventloop::EventLoop::get();
95     loop.setupSignal();
96 
97     ipmi::ipmiChannelInit();
98     if (channel.size())
99     {
100         setInterfaceIndex(channel);
101     }
102 
103     session::Manager::get().managerInit(channel);
104     // Register callback to update cache for a GUID change and cache the GUID
105     command::registerGUIDChangeCallback();
106 
107     // Register callback to update cache for sol conf change
108     sol::registerSolConfChangeCallbackHandler(channel);
109 
110     // Register the phosphor-net-ipmid session setup commands
111     command::sessionSetupCommands();
112 
113     // Register the phosphor-net-ipmid SOL commands
114     sol::command::registerCommands();
115 
116     if (loop.setupSocket(sdbusp, channel))
117     {
118         return EXIT_FAILURE;
119     }
120 
121     // Start Event Loop
122     return loop.startEventLoop();
123 }
124