blob: b142ed7f49ffffe9cc430b791d96aa847bc51e44 [file] [log] [blame]
Tom Josephc35524e2016-08-29 08:17:59 -05001#include "main.hpp"
Tom Josephc35524e2016-08-29 08:17:59 -05002
Tom Josephc35524e2016-08-29 08:17:59 -05003#include "comm_module.hpp"
Vernon Mauery9e801a22018-10-12 13:20:49 -07004#include "command/guid.hpp"
Tom Josephc35524e2016-08-29 08:17:59 -05005#include "command_table.hpp"
6#include "message.hpp"
7#include "message_handler.hpp"
Tom485038e2016-12-02 13:44:45 +05308#include "provider_registration.hpp"
Tom Josephc35524e2016-08-29 08:17:59 -05009#include "socket_channel.hpp"
Tom Joseph89481cf2017-04-03 02:11:34 +053010#include "sol_module.hpp"
Tom Josephc35524e2016-08-29 08:17:59 -050011
Vernon Mauery9e801a22018-10-12 13:20:49 -070012#include <assert.h>
13#include <dirent.h>
14#include <dlfcn.h>
15#include <host-ipmid/ipmid-api.h>
16#include <systemd/sd-bus.h>
17#include <systemd/sd-daemon.h>
18#include <systemd/sd-event.h>
19#include <unistd.h>
20
21#include <iostream>
Vernon Maueryb1088062018-10-24 12:49:30 -070022#include <sdbusplus/timer.hpp>
Vernon Mauery9e801a22018-10-12 13:20:49 -070023#include <tuple>
24
Tom Josephc35524e2016-08-29 08:17:59 -050025// Tuple of Global Singletons
26session::Manager manager;
27command::Table table;
Tom Joseph52f53d72017-03-14 15:20:19 +053028eventloop::EventLoop loop;
29sol::Manager solManager;
30
31std::tuple<session::Manager&, command::Table&, eventloop::EventLoop&,
Vernon Mauery9e801a22018-10-12 13:20:49 -070032 sol::Manager&>
33 singletonPool(manager, table, loop, solManager);
Tom Josephc35524e2016-08-29 08:17:59 -050034
35sd_bus* bus = nullptr;
Ratan Gupta166c71a2018-03-23 23:05:17 +053036sd_event* events = nullptr;
Marri Devender Rao3ecf0a12017-07-13 08:07:22 -050037
Ratan Gupta40801362018-03-12 16:21:37 +053038// Global timer for network changes
Vernon Maueryb1088062018-10-24 12:49:30 -070039std::unique_ptr<phosphor::Timer> networkTimer = nullptr;
Ratan Gupta40801362018-03-12 16:21:37 +053040
Tom069f01e2016-12-02 14:11:37 +053041FILE* ipmidbus = nullptr;
Jason M. Billsb00f8f72018-09-10 15:14:10 -070042static unsigned short selReservationID = 0xFFFF;
43static bool selReservationValid = false;
Tom069f01e2016-12-02 14:11:37 +053044sd_bus_slot* ipmid_slot = nullptr;
Tom Josephc35524e2016-08-29 08:17:59 -050045
Vernon Maueryd0062ed2018-10-25 09:36:06 -070046std::shared_ptr<sdbusplus::bus::bus> sdbusp;
47
Tom Josephc35524e2016-08-29 08:17:59 -050048/*
49 * @brief Required by apphandler IPMI Provider Library
50 */
51sd_bus* ipmid_get_sd_bus_connection()
52{
53 return bus;
54}
55
56/*
Vernon Maueryd0062ed2018-10-25 09:36:06 -070057 * @brief mechanism to get at sdbusplus object
58 */
59std::shared_ptr<sdbusplus::bus::bus> getSdBus()
60{
61 return sdbusp;
62}
63
64/*
Tom069f01e2016-12-02 14:11:37 +053065 * @brief Required by apphandler IPMI Provider Library
66 */
Ratan Gupta04edb9b2018-03-20 23:06:06 +053067sd_event* ipmid_get_sd_event_connection()
68{
Ratan Gupta166c71a2018-03-23 23:05:17 +053069 return events;
Ratan Gupta04edb9b2018-03-20 23:06:06 +053070}
71
72/*
73 * @brief Required by apphandler IPMI Provider Library
74 */
Jason M. Billsb00f8f72018-09-10 15:14:10 -070075unsigned short reserveSel(void)
Tom069f01e2016-12-02 14:11:37 +053076{
Jason M. Billsb00f8f72018-09-10 15:14:10 -070077 // IPMI spec, Reservation ID, the value simply increases against each
78 // execution of the Reserve SEL command.
Vernon Mauery9e801a22018-10-12 13:20:49 -070079 if (++selReservationID == 0)
80 {
Jason M. Billsb00f8f72018-09-10 15:14:10 -070081 selReservationID = 1;
82 }
83 selReservationValid = true;
84 return selReservationID;
85}
86
87/*
88 * @brief Required by apphandler IPMI Provider Library
89 */
90bool checkSELReservation(unsigned short id)
91{
92 return (selReservationValid && selReservationID == id);
93}
94
95/*
96 * @brief Required by apphandler IPMI Provider Library
97 */
98void cancelSELReservation(void)
99{
100 selReservationValid = false;
Tom069f01e2016-12-02 14:11:37 +0530101}
102
ssekard6f3f7d2018-08-16 17:30:28 +0530103EInterfaceIndex getInterfaceIndex(void)
104{
105 return interfaceLAN1;
106}
107
Vernon Mauerycc7b1cb2018-10-24 13:13:46 -0700108int main()
Tom Josephc35524e2016-08-29 08:17:59 -0500109{
Tom069f01e2016-12-02 14:11:37 +0530110 /*
111 * Required by apphandler IPMI Provider Library for logging.
112 */
Vernon Mauery9e801a22018-10-12 13:20:49 -0700113 ipmidbus = fopen("/dev/null", "w");
Tom069f01e2016-12-02 14:11:37 +0530114
Tom Josephc35524e2016-08-29 08:17:59 -0500115 // Connect to system bus
Vernon Mauery96a1a392018-11-08 16:54:23 -0800116 auto rc = sd_bus_default_system(&bus);
Tom Josephc35524e2016-08-29 08:17:59 -0500117 if (rc < 0)
118 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700119 std::cerr << "Failed to connect to system bus:" << strerror(-rc)
120 << "\n";
Tom Josephc35524e2016-08-29 08:17:59 -0500121 goto finish;
122 }
Vernon Maueryd0062ed2018-10-25 09:36:06 -0700123 sdbusp = std::make_shared<sdbusplus::bus::bus>(bus);
Tom Josephc35524e2016-08-29 08:17:59 -0500124
Ratan Gupta166c71a2018-03-23 23:05:17 +0530125 /* Get an sd event handler */
126 rc = sd_event_default(&events);
127 if (rc < 0)
128 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700129 std::cerr << "Failure to create sd_event" << strerror(-rc) << "\n";
Vernon Maueryd0062ed2018-10-25 09:36:06 -0700130 return EXIT_FAILURE;
Ratan Gupta166c71a2018-03-23 23:05:17 +0530131 }
132
Tom Joseph83029cb2017-09-01 16:37:31 +0530133 // Register callback to update cache for a GUID change and cache the GUID
134 command::registerGUIDChangeCallback();
135 cache::guid = command::getSystemGUID();
136
Tom485038e2016-12-02 13:44:45 +0530137 // Register all the IPMI provider libraries applicable for net-ipmid
138 provider::registerCallbackHandlers(NET_IPMID_LIB_PATH);
139
Tom Josephc35524e2016-08-29 08:17:59 -0500140 // Register the phosphor-net-ipmid session setup commands
141 command::sessionSetupCommands();
142
Tom Joseph89481cf2017-04-03 02:11:34 +0530143 // Register the phosphor-net-ipmid SOL commands
144 sol::command::registerCommands();
145
Tom Josephc35524e2016-08-29 08:17:59 -0500146 // Start Event Loop
Vernon Mauery9e801a22018-10-12 13:20:49 -0700147 return std::get<eventloop::EventLoop&>(singletonPool)
148 .startEventLoop(events);
Tom Josephc35524e2016-08-29 08:17:59 -0500149
150finish:
Ratan Gupta166c71a2018-03-23 23:05:17 +0530151 sd_event_unref(events);
Tom Josephc35524e2016-08-29 08:17:59 -0500152
153 return 0;
154}