|
| 1 | +#include "interfaces.h" |
| 2 | + |
| 3 | +#include <dbus-c++/dbus.h> // DBus |
| 4 | + |
| 5 | +#include "reboot_interfaces.h" |
| 6 | + |
| 7 | +constexpr char kRebootBusName[] = "org.SONiC.HostService.reboot"; |
| 8 | +constexpr char kRebootPath[] = "/org/SONiC/HostService/reboot"; |
| 9 | + |
| 10 | +constexpr char kContainerShutdownBusName[] = |
| 11 | + "org.SONiC.HostService.container_shutdown"; |
| 12 | +constexpr char kContainerShutdownPath[] = |
| 13 | + "/org/SONiC/HostService/container_shutdown"; |
| 14 | + |
| 15 | +DBus::Connection& HostServiceDbus::getConnection(void) { |
| 16 | + static DBus::Connection* connPtr = nullptr; |
| 17 | + if (connPtr == nullptr) { |
| 18 | + static DBus::BusDispatcher dispatcher; |
| 19 | + DBus::default_dispatcher = &dispatcher; |
| 20 | + |
| 21 | + static DBus::Connection conn = DBus::Connection::SystemBus(); |
| 22 | + connPtr = &conn; |
| 23 | + } |
| 24 | + return *connPtr; |
| 25 | +} |
| 26 | + |
| 27 | +DbusInterface::DbusResponse HostServiceDbus::Reboot( |
| 28 | + const std::string& jsonRebootRequest) { |
| 29 | + int32_t status; |
| 30 | + |
| 31 | + DbusReboot reboot_client(getConnection(), kRebootBusName, kRebootPath); |
| 32 | + std::string retString; |
| 33 | + std::vector<std::string> options; |
| 34 | + options.push_back(jsonRebootRequest); |
| 35 | + try { |
| 36 | + reboot_client.issue_reboot(options, status, retString); |
| 37 | + } catch (DBus::Error& ex) { |
| 38 | + return DbusResponse{ |
| 39 | + DbusStatus::DBUS_FAIL, |
| 40 | + "HostServiceDbus::Reboot: failed to call reboot host service"}; |
| 41 | + } |
| 42 | + |
| 43 | + // reboot.py returns 0 for success, 1 for failure |
| 44 | + if (status == 0) { |
| 45 | + // Successful reboot response is an empty string. |
| 46 | + return DbusResponse{DbusStatus::DBUS_SUCCESS, ""}; |
| 47 | + } |
| 48 | + return DbusResponse{DbusStatus::DBUS_FAIL, retString}; |
| 49 | +} |
| 50 | + |
| 51 | +DbusInterface::DbusResponse HostServiceDbus::RebootStatus( |
| 52 | + const std::string& jsonStatusRequest) { |
| 53 | + DbusReboot reboot_client(getConnection(), kRebootBusName, kRebootPath); |
| 54 | + int32_t status; |
| 55 | + std::string retString; |
| 56 | + |
| 57 | + try { |
| 58 | + reboot_client.get_reboot_status(status, retString); |
| 59 | + } catch (DBus::Error& ex) { |
| 60 | + return DbusResponse{ |
| 61 | + DbusStatus::DBUS_FAIL, |
| 62 | + "HostServiceDbus::RebootStatus: failed to call reboot status " |
| 63 | + "host service"}; |
| 64 | + } |
| 65 | + |
| 66 | + // reboot.py returns 0 for success, 1 for failure |
| 67 | + if (status == 0) { |
| 68 | + return DbusResponse{DbusStatus::DBUS_SUCCESS, retString}; |
| 69 | + } |
| 70 | + return DbusResponse{DbusStatus::DBUS_FAIL, retString}; |
| 71 | +} |
0 commit comments