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 "provider_registration.hpp" 9 #include "socket_channel.hpp" 10 #include "sol_module.hpp" 11 #include "timer.hpp" 12 13 #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 25 // Tuple of Global Singletons 26 session::Manager manager; 27 command::Table table; 28 eventloop::EventLoop loop; 29 sol::Manager solManager; 30 31 std::tuple<session::Manager&, command::Table&, eventloop::EventLoop&, 32 sol::Manager&> 33 singletonPool(manager, table, loop, solManager); 34 35 sd_bus* bus = nullptr; 36 sd_event* events = nullptr; 37 38 // Global timer for network changes 39 std::unique_ptr<phosphor::ipmi::Timer> networkTimer = nullptr; 40 41 FILE* ipmidbus = nullptr; 42 static unsigned short selReservationID = 0xFFFF; 43 static bool selReservationValid = false; 44 sd_bus_slot* ipmid_slot = nullptr; 45 46 /* 47 * @brief Required by apphandler IPMI Provider Library 48 */ 49 sd_bus* ipmid_get_sd_bus_connection() 50 { 51 return bus; 52 } 53 54 /* 55 * @brief Required by apphandler IPMI Provider Library 56 */ 57 sd_event* ipmid_get_sd_event_connection() 58 { 59 return events; 60 } 61 62 /* 63 * @brief Required by apphandler IPMI Provider Library 64 */ 65 unsigned short reserveSel(void) 66 { 67 // IPMI spec, Reservation ID, the value simply increases against each 68 // execution of the Reserve SEL command. 69 if (++selReservationID == 0) 70 { 71 selReservationID = 1; 72 } 73 selReservationValid = true; 74 return selReservationID; 75 } 76 77 /* 78 * @brief Required by apphandler IPMI Provider Library 79 */ 80 bool checkSELReservation(unsigned short id) 81 { 82 return (selReservationValid && selReservationID == id); 83 } 84 85 /* 86 * @brief Required by apphandler IPMI Provider Library 87 */ 88 void cancelSELReservation(void) 89 { 90 selReservationValid = false; 91 } 92 93 int main(int i_argc, char* i_argv[]) 94 { 95 /* 96 * Required by apphandler IPMI Provider Library for logging. 97 */ 98 ipmidbus = fopen("/dev/null", "w"); 99 100 // Connect to system bus 101 auto rc = sd_bus_open_system(&bus); 102 if (rc < 0) 103 { 104 std::cerr << "Failed to connect to system bus:" << strerror(-rc) 105 << "\n"; 106 goto finish; 107 } 108 109 /* Get an sd event handler */ 110 rc = sd_event_default(&events); 111 if (rc < 0) 112 { 113 std::cerr << "Failure to create sd_event" << strerror(-rc) << "\n"; 114 goto finish; 115 } 116 117 // Register callback to update cache for a GUID change and cache the GUID 118 command::registerGUIDChangeCallback(); 119 cache::guid = command::getSystemGUID(); 120 121 // Register all the IPMI provider libraries applicable for net-ipmid 122 provider::registerCallbackHandlers(NET_IPMID_LIB_PATH); 123 124 // Register the phosphor-net-ipmid session setup commands 125 command::sessionSetupCommands(); 126 127 // Register the phosphor-net-ipmid SOL commands 128 sol::command::registerCommands(); 129 130 // Start Event Loop 131 return std::get<eventloop::EventLoop&>(singletonPool) 132 .startEventLoop(events); 133 134 finish: 135 sd_bus_unref(bus); 136 sd_event_unref(events); 137 138 return 0; 139 } 140