blob: f49d4c7b13dbb42173fca13b4d9be2dae9ec3f00 [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
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
ssekard6f3f7d2018-08-16 17:30:28 +053093EInterfaceIndex getInterfaceIndex(void)
94{
95 return interfaceLAN1;
96}
97
Vernon Mauerycc7b1cb2018-10-24 13:13:46 -070098int main()
Tom Josephc35524e2016-08-29 08:17:59 -050099{
Tom069f01e2016-12-02 14:11:37 +0530100 /*
101 * Required by apphandler IPMI Provider Library for logging.
102 */
Vernon Mauery9e801a22018-10-12 13:20:49 -0700103 ipmidbus = fopen("/dev/null", "w");
Tom069f01e2016-12-02 14:11:37 +0530104
Tom Josephc35524e2016-08-29 08:17:59 -0500105 // Connect to system bus
Vernon Mauery96a1a392018-11-08 16:54:23 -0800106 auto rc = sd_bus_default_system(&bus);
Tom Josephc35524e2016-08-29 08:17:59 -0500107 if (rc < 0)
108 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700109 std::cerr << "Failed to connect to system bus:" << strerror(-rc)
110 << "\n";
Tom Josephc35524e2016-08-29 08:17:59 -0500111 goto finish;
112 }
113
Ratan Gupta166c71a2018-03-23 23:05:17 +0530114 /* Get an sd event handler */
115 rc = sd_event_default(&events);
116 if (rc < 0)
117 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700118 std::cerr << "Failure to create sd_event" << strerror(-rc) << "\n";
Ratan Gupta166c71a2018-03-23 23:05:17 +0530119 goto finish;
120 }
121
Tom Joseph83029cb2017-09-01 16:37:31 +0530122 // Register callback to update cache for a GUID change and cache the GUID
123 command::registerGUIDChangeCallback();
124 cache::guid = command::getSystemGUID();
125
Tom485038e2016-12-02 13:44:45 +0530126 // Register all the IPMI provider libraries applicable for net-ipmid
127 provider::registerCallbackHandlers(NET_IPMID_LIB_PATH);
128
Tom Josephc35524e2016-08-29 08:17:59 -0500129 // Register the phosphor-net-ipmid session setup commands
130 command::sessionSetupCommands();
131
Tom Joseph89481cf2017-04-03 02:11:34 +0530132 // Register the phosphor-net-ipmid SOL commands
133 sol::command::registerCommands();
134
Tom Josephc35524e2016-08-29 08:17:59 -0500135 // Start Event Loop
Vernon Mauery9e801a22018-10-12 13:20:49 -0700136 return std::get<eventloop::EventLoop&>(singletonPool)
137 .startEventLoop(events);
Tom Josephc35524e2016-08-29 08:17:59 -0500138
139finish:
140 sd_bus_unref(bus);
Ratan Gupta166c71a2018-03-23 23:05:17 +0530141 sd_event_unref(events);
Tom Josephc35524e2016-08-29 08:17:59 -0500142
143 return 0;
144}