blob: 4aa07929e796f4e846725ba89e1b56c84a501347 [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"
Ratan Gupta40801362018-03-12 16:21:37 +053011#include "timer.hpp"
Tom Josephc35524e2016-08-29 08:17:59 -050012
Vernon Mauery9e801a22018-10-12 13:20:49 -070013#include <assert.h>
14#include <dirent.h>
15#include <dlfcn.h>
16#include <host-ipmid/ipmid-api.h>
17#include <systemd/sd-bus.h>
18#include <systemd/sd-daemon.h>
19#include <systemd/sd-event.h>
20#include <unistd.h>
21
22#include <iostream>
23#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
39std::unique_ptr<phosphor::ipmi::Timer> networkTimer = nullptr;
40
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
46/*
47 * @brief Required by apphandler IPMI Provider Library
48 */
49sd_bus* ipmid_get_sd_bus_connection()
50{
51 return bus;
52}
53
54/*
Tom069f01e2016-12-02 14:11:37 +053055 * @brief Required by apphandler IPMI Provider Library
56 */
Ratan Gupta04edb9b2018-03-20 23:06:06 +053057sd_event* ipmid_get_sd_event_connection()
58{
Ratan Gupta166c71a2018-03-23 23:05:17 +053059 return events;
Ratan Gupta04edb9b2018-03-20 23:06:06 +053060}
61
62/*
63 * @brief Required by apphandler IPMI Provider Library
64 */
Jason M. Billsb00f8f72018-09-10 15:14:10 -070065unsigned short reserveSel(void)
Tom069f01e2016-12-02 14:11:37 +053066{
Jason M. Billsb00f8f72018-09-10 15:14:10 -070067 // IPMI spec, Reservation ID, the value simply increases against each
68 // execution of the Reserve SEL command.
Vernon Mauery9e801a22018-10-12 13:20:49 -070069 if (++selReservationID == 0)
70 {
Jason M. Billsb00f8f72018-09-10 15:14:10 -070071 selReservationID = 1;
72 }
73 selReservationValid = true;
74 return selReservationID;
75}
76
77/*
78 * @brief Required by apphandler IPMI Provider Library
79 */
80bool checkSELReservation(unsigned short id)
81{
82 return (selReservationValid && selReservationID == id);
83}
84
85/*
86 * @brief Required by apphandler IPMI Provider Library
87 */
88void cancelSELReservation(void)
89{
90 selReservationValid = false;
Tom069f01e2016-12-02 14:11:37 +053091}
92
Tom Josephc35524e2016-08-29 08:17:59 -050093int main(int i_argc, char* i_argv[])
94{
Tom069f01e2016-12-02 14:11:37 +053095 /*
96 * Required by apphandler IPMI Provider Library for logging.
97 */
Vernon Mauery9e801a22018-10-12 13:20:49 -070098 ipmidbus = fopen("/dev/null", "w");
Tom069f01e2016-12-02 14:11:37 +053099
Tom Josephc35524e2016-08-29 08:17:59 -0500100 // Connect to system bus
101 auto rc = sd_bus_open_system(&bus);
102 if (rc < 0)
103 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700104 std::cerr << "Failed to connect to system bus:" << strerror(-rc)
105 << "\n";
Tom Josephc35524e2016-08-29 08:17:59 -0500106 goto finish;
107 }
108
Ratan Gupta166c71a2018-03-23 23:05:17 +0530109 /* Get an sd event handler */
110 rc = sd_event_default(&events);
111 if (rc < 0)
112 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700113 std::cerr << "Failure to create sd_event" << strerror(-rc) << "\n";
Ratan Gupta166c71a2018-03-23 23:05:17 +0530114 goto finish;
115 }
116
Tom Joseph83029cb2017-09-01 16:37:31 +0530117 // Register callback to update cache for a GUID change and cache the GUID
118 command::registerGUIDChangeCallback();
119 cache::guid = command::getSystemGUID();
120
Tom485038e2016-12-02 13:44:45 +0530121 // Register all the IPMI provider libraries applicable for net-ipmid
122 provider::registerCallbackHandlers(NET_IPMID_LIB_PATH);
123
Tom Josephc35524e2016-08-29 08:17:59 -0500124 // Register the phosphor-net-ipmid session setup commands
125 command::sessionSetupCommands();
126
Tom Joseph89481cf2017-04-03 02:11:34 +0530127 // Register the phosphor-net-ipmid SOL commands
128 sol::command::registerCommands();
129
Tom Josephc35524e2016-08-29 08:17:59 -0500130 // Start Event Loop
Vernon Mauery9e801a22018-10-12 13:20:49 -0700131 return std::get<eventloop::EventLoop&>(singletonPool)
132 .startEventLoop(events);
Tom Josephc35524e2016-08-29 08:17:59 -0500133
134finish:
135 sd_bus_unref(bus);
Ratan Gupta166c71a2018-03-23 23:05:17 +0530136 sd_event_unref(events);
Tom Josephc35524e2016-08-29 08:17:59 -0500137
138 return 0;
139}