xref: /openbmc/phosphor-net-ipmid/main.cpp (revision afac94d1ffa271c9efb611a40f73a3e238164a9e)
1c35524e7STom Joseph #include "main.hpp"
2c35524e7STom Joseph 
3c35524e7STom Joseph #include "comm_module.hpp"
49e801a2bSVernon Mauery #include "command/guid.hpp"
5c35524e7STom Joseph #include "command_table.hpp"
6c35524e7STom Joseph #include "message.hpp"
7c35524e7STom Joseph #include "message_handler.hpp"
82085ae07SVernon Mauery #include "sd_event_loop.hpp"
92085ae07SVernon Mauery #include "sessions_manager.hpp"
10c35524e7STom Joseph #include "socket_channel.hpp"
1189481cfcSTom Joseph #include "sol_module.hpp"
12c35524e7STom Joseph 
139e801a2bSVernon Mauery #include <assert.h>
149e801a2bSVernon Mauery #include <dirent.h>
159e801a2bSVernon Mauery #include <dlfcn.h>
164f09eaeeSWilliam A. Kennington III #include <ipmid/api.h>
179e801a2bSVernon Mauery #include <systemd/sd-daemon.h>
189e801a2bSVernon Mauery #include <systemd/sd-event.h>
199e801a2bSVernon Mauery #include <unistd.h>
209e801a2bSVernon Mauery 
21d92bc324SVernon Mauery #include <CLI/CLI.hpp>
227b7f25f7SGeorge Liu #include <phosphor-logging/lg2.hpp>
23cbccb05aSVernon Mauery #include <sdbusplus/asio/connection.hpp>
24052b7cf3SVernon Mauery #include <user_channel/channel_layer.hpp>
259e801a2bSVernon Mauery 
26bc8958feSGeorge Liu #include <tuple>
27bc8958feSGeorge Liu 
28cbccb05aSVernon Mauery static auto io = std::make_shared<boost::asio::io_context>();
getIo()292085ae07SVernon Mauery std::shared_ptr<boost::asio::io_context> getIo()
302085ae07SVernon Mauery {
312085ae07SVernon Mauery     return io;
322085ae07SVernon Mauery }
33c35524e7STom Joseph 
34c35524e7STom Joseph sd_bus* bus = nullptr;
353ecf0a1cSMarri Devender Rao 
367b98c072SVernon Mauery std::shared_ptr<sdbusplus::asio::connection> sdbusp;
37d0062edeSVernon Mauery 
38c35524e7STom Joseph /*
39c35524e7STom Joseph  * @brief Required by apphandler IPMI Provider Library
40c35524e7STom Joseph  */
ipmid_get_sd_bus_connection()41c35524e7STom Joseph sd_bus* ipmid_get_sd_bus_connection()
42c35524e7STom Joseph {
43c35524e7STom Joseph     return bus;
44c35524e7STom Joseph }
45c35524e7STom Joseph 
46c35524e7STom Joseph /*
47d0062edeSVernon Mauery  * @brief mechanism to get at sdbusplus object
48d0062edeSVernon Mauery  */
getSdBus()497b98c072SVernon Mauery std::shared_ptr<sdbusplus::asio::connection> getSdBus()
50d0062edeSVernon Mauery {
51d0062edeSVernon Mauery     return sdbusp;
52d0062edeSVernon Mauery }
53d0062edeSVernon Mauery 
54052b7cf3SVernon Mauery static EInterfaceIndex currentInterfaceIndex = interfaceUnknown;
setInterfaceIndex(const std::string & channel)55052b7cf3SVernon Mauery static void setInterfaceIndex(const std::string& channel)
56052b7cf3SVernon Mauery {
57052b7cf3SVernon Mauery     try
58052b7cf3SVernon Mauery     {
59052b7cf3SVernon Mauery         currentInterfaceIndex =
60052b7cf3SVernon Mauery             static_cast<EInterfaceIndex>(ipmi::getChannelByName(channel));
61052b7cf3SVernon Mauery     }
62052b7cf3SVernon Mauery     catch (const std::exception& e)
63052b7cf3SVernon Mauery     {
647b7f25f7SGeorge Liu         lg2::error("Requested {NAME} is not a valid channel name: {ERROR}",
657b7f25f7SGeorge Liu                    "NAME", channel, "ERROR", e);
66052b7cf3SVernon Mauery     }
67052b7cf3SVernon Mauery }
getInterfaceIndex(void)68d6f3f7d7Sssekar EInterfaceIndex getInterfaceIndex(void)
69d6f3f7d7Sssekar {
70052b7cf3SVernon Mauery     return currentInterfaceIndex;
71d6f3f7d7Sssekar }
72d6f3f7d7Sssekar 
main(int argc,char * argv[])73d92bc324SVernon Mauery int main(int argc, char* argv[])
74c35524e7STom Joseph {
75d92bc324SVernon Mauery     CLI::App app("KCS RMCP+ bridge");
76d92bc324SVernon Mauery     std::string channel;
77d92bc324SVernon Mauery     app.add_option("-c,--channel", channel, "channel name. e.g., eth0");
78d92bc324SVernon Mauery     uint16_t port = ipmi::rmcpp::defaultPort;
79d92bc324SVernon Mauery     app.add_option("-p,--port", port, "port number");
80d92bc324SVernon Mauery     bool verbose = false;
81d92bc324SVernon Mauery     app.add_option("-v,--verbose", verbose, "print more verbose output");
82d92bc324SVernon Mauery     CLI11_PARSE(app, argc, argv);
83d92bc324SVernon Mauery 
84c35524e7STom Joseph     // Connect to system bus
8596a1a391SVernon Mauery     auto rc = sd_bus_default_system(&bus);
86c35524e7STom Joseph     if (rc < 0)
87c35524e7STom Joseph     {
887b7f25f7SGeorge Liu         lg2::error("Failed to connect to system bus: {ERROR}", "ERROR",
897b7f25f7SGeorge Liu                    strerror(-rc));
90cbccb05aSVernon Mauery         return rc;
91c35524e7STom Joseph     }
92cbccb05aSVernon Mauery     sdbusp = std::make_shared<sdbusplus::asio::connection>(*io, bus);
93166c71a1SRatan Gupta 
94*afac94d1SGeorge Liu     auto& loop = eventloop::EventLoop::get();
95*afac94d1SGeorge Liu     loop.setupSignal();
96*afac94d1SGeorge Liu 
97052b7cf3SVernon Mauery     ipmi::ipmiChannelInit();
98052b7cf3SVernon Mauery     if (channel.size())
99052b7cf3SVernon Mauery     {
100052b7cf3SVernon Mauery         setInterfaceIndex(channel);
101052b7cf3SVernon Mauery     }
102052b7cf3SVernon Mauery 
1032085ae07SVernon Mauery     session::Manager::get().managerInit(channel);
10483029cb8STom Joseph     // Register callback to update cache for a GUID change and cache the GUID
10583029cb8STom Joseph     command::registerGUIDChangeCallback();
10683029cb8STom Joseph 
107c936ecaaSJian Zhang     // Register callback to update cache for sol conf change
108c936ecaaSJian Zhang     sol::registerSolConfChangeCallbackHandler(channel);
109c936ecaaSJian Zhang 
110c35524e7STom Joseph     // Register the phosphor-net-ipmid session setup commands
111c35524e7STom Joseph     command::sessionSetupCommands();
112c35524e7STom Joseph 
11389481cfcSTom Joseph     // Register the phosphor-net-ipmid SOL commands
11489481cfcSTom Joseph     sol::command::registerCommands();
11589481cfcSTom Joseph 
116d92bc324SVernon Mauery     if (loop.setupSocket(sdbusp, channel))
117d92bc324SVernon Mauery     {
118d92bc324SVernon Mauery         return EXIT_FAILURE;
119d92bc324SVernon Mauery     }
120d92bc324SVernon Mauery 
121c35524e7STom Joseph     // Start Event Loop
122d92bc324SVernon Mauery     return loop.startEventLoop();
123c35524e7STom Joseph }
124