Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 1 | #include "main.hpp" |
| 2 | #include <assert.h> |
| 3 | #include <dlfcn.h> |
| 4 | #include <dirent.h> |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #include <iostream> |
| 8 | #include <tuple> |
| 9 | |
| 10 | #include <systemd/sd-bus.h> |
| 11 | #include <systemd/sd-daemon.h> |
| 12 | #include <systemd/sd-event.h> |
| 13 | |
| 14 | #include <host-ipmid/ipmid-api.h> |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 15 | #include "command/guid.hpp" |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 16 | #include "comm_module.hpp" |
| 17 | #include "command_table.hpp" |
| 18 | #include "message.hpp" |
| 19 | #include "message_handler.hpp" |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 20 | #include "provider_registration.hpp" |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 21 | #include "socket_channel.hpp" |
Tom Joseph | 89481cf | 2017-04-03 02:11:34 +0530 | [diff] [blame] | 22 | #include "sol_module.hpp" |
Ratan Gupta | 4080136 | 2018-03-12 16:21:37 +0530 | [diff] [blame] | 23 | #include "timer.hpp" |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 24 | |
| 25 | // Tuple of Global Singletons |
| 26 | session::Manager manager; |
| 27 | command::Table table; |
Tom Joseph | 52f53d7 | 2017-03-14 15:20:19 +0530 | [diff] [blame] | 28 | eventloop::EventLoop loop; |
| 29 | sol::Manager solManager; |
| 30 | |
| 31 | std::tuple<session::Manager&, command::Table&, eventloop::EventLoop&, |
| 32 | sol::Manager&> singletonPool(manager, table, loop, solManager); |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 33 | |
| 34 | sd_bus* bus = nullptr; |
Ratan Gupta | 166c71a | 2018-03-23 23:05:17 +0530 | [diff] [blame] | 35 | sd_event* events = nullptr; |
Marri Devender Rao | 3ecf0a1 | 2017-07-13 08:07:22 -0500 | [diff] [blame] | 36 | |
Ratan Gupta | 4080136 | 2018-03-12 16:21:37 +0530 | [diff] [blame] | 37 | // Global timer for network changes |
| 38 | std::unique_ptr<phosphor::ipmi::Timer> networkTimer = nullptr; |
| 39 | |
Tom | 069f01e | 2016-12-02 14:11:37 +0530 | [diff] [blame] | 40 | FILE* ipmidbus = nullptr; |
Jason M. Bills | b00f8f7 | 2018-09-10 15:14:10 -0700 | [diff] [blame] | 41 | static unsigned short selReservationID = 0xFFFF; |
| 42 | static bool selReservationValid = false; |
Tom | 069f01e | 2016-12-02 14:11:37 +0530 | [diff] [blame] | 43 | sd_bus_slot* ipmid_slot = nullptr; |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * @brief Required by apphandler IPMI Provider Library |
| 47 | */ |
| 48 | sd_bus* ipmid_get_sd_bus_connection() |
| 49 | { |
| 50 | return bus; |
| 51 | } |
| 52 | |
| 53 | /* |
Tom | 069f01e | 2016-12-02 14:11:37 +0530 | [diff] [blame] | 54 | * @brief Required by apphandler IPMI Provider Library |
| 55 | */ |
Ratan Gupta | 04edb9b | 2018-03-20 23:06:06 +0530 | [diff] [blame] | 56 | sd_event* ipmid_get_sd_event_connection() |
| 57 | { |
Ratan Gupta | 166c71a | 2018-03-23 23:05:17 +0530 | [diff] [blame] | 58 | return events; |
Ratan Gupta | 04edb9b | 2018-03-20 23:06:06 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* |
| 62 | * @brief Required by apphandler IPMI Provider Library |
| 63 | */ |
Jason M. Bills | b00f8f7 | 2018-09-10 15:14:10 -0700 | [diff] [blame] | 64 | unsigned short reserveSel(void) |
Tom | 069f01e | 2016-12-02 14:11:37 +0530 | [diff] [blame] | 65 | { |
Jason M. Bills | b00f8f7 | 2018-09-10 15:14:10 -0700 | [diff] [blame] | 66 | // IPMI spec, Reservation ID, the value simply increases against each |
| 67 | // execution of the Reserve SEL command. |
| 68 | if (++selReservationID == 0) { |
| 69 | selReservationID = 1; |
| 70 | } |
| 71 | selReservationValid = true; |
| 72 | return selReservationID; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * @brief Required by apphandler IPMI Provider Library |
| 77 | */ |
| 78 | bool checkSELReservation(unsigned short id) |
| 79 | { |
| 80 | return (selReservationValid && selReservationID == id); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * @brief Required by apphandler IPMI Provider Library |
| 85 | */ |
| 86 | void cancelSELReservation(void) |
| 87 | { |
| 88 | selReservationValid = false; |
Tom | 069f01e | 2016-12-02 14:11:37 +0530 | [diff] [blame] | 89 | } |
| 90 | |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 91 | int main(int i_argc, char* i_argv[]) |
| 92 | { |
Tom | 069f01e | 2016-12-02 14:11:37 +0530 | [diff] [blame] | 93 | /* |
| 94 | * Required by apphandler IPMI Provider Library for logging. |
| 95 | */ |
| 96 | ipmidbus = fopen("/dev/null", "w"); |
| 97 | |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 98 | // Connect to system bus |
| 99 | auto rc = sd_bus_open_system(&bus); |
| 100 | if (rc < 0) |
| 101 | { |
| 102 | std::cerr << "Failed to connect to system bus:" << strerror(-rc) <<"\n"; |
| 103 | goto finish; |
| 104 | } |
| 105 | |
Ratan Gupta | 166c71a | 2018-03-23 23:05:17 +0530 | [diff] [blame] | 106 | /* Get an sd event handler */ |
| 107 | rc = sd_event_default(&events); |
| 108 | if (rc < 0) |
| 109 | { |
| 110 | std::cerr << "Failure to create sd_event" << strerror(-rc) <<"\n"; |
| 111 | goto finish; |
| 112 | } |
| 113 | |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 114 | // Register callback to update cache for a GUID change and cache the GUID |
| 115 | command::registerGUIDChangeCallback(); |
| 116 | cache::guid = command::getSystemGUID(); |
| 117 | |
| 118 | |
Tom | 485038e | 2016-12-02 13:44:45 +0530 | [diff] [blame] | 119 | // Register all the IPMI provider libraries applicable for net-ipmid |
| 120 | provider::registerCallbackHandlers(NET_IPMID_LIB_PATH); |
| 121 | |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 122 | // Register the phosphor-net-ipmid session setup commands |
| 123 | command::sessionSetupCommands(); |
| 124 | |
Tom Joseph | 89481cf | 2017-04-03 02:11:34 +0530 | [diff] [blame] | 125 | // Register the phosphor-net-ipmid SOL commands |
| 126 | sol::command::registerCommands(); |
| 127 | |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 128 | // Start Event Loop |
Ratan Gupta | 166c71a | 2018-03-23 23:05:17 +0530 | [diff] [blame] | 129 | return std::get<eventloop::EventLoop&>(singletonPool).startEventLoop(events); |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 130 | |
| 131 | finish: |
| 132 | sd_bus_unref(bus); |
Ratan Gupta | 166c71a | 2018-03-23 23:05:17 +0530 | [diff] [blame] | 133 | sd_event_unref(events); |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |