diff --git a/Core/Build/CMakeLists.txt b/Core/Build/CMakeLists.txt index 31498b92f..7f4912376 100644 --- a/Core/Build/CMakeLists.txt +++ b/Core/Build/CMakeLists.txt @@ -14,8 +14,6 @@ target_link_libraries(RestuneExtAPIs RestuneAuxUtils) target_include_directories(RestuneExtAPIs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../Extensions/Include) # FRAMEWORK -find_package(PkgConfig REQUIRED) -pkg_check_modules(LIBYAML REQUIRED yaml-0.1) file(GLOB SOURCES "../Framework/*.cpp") add_library(RestuneServer ${SOURCES}) set_target_properties(RestuneServer PROPERTIES VERSION 1.0.0 SOVERSION 1) @@ -24,8 +22,6 @@ target_include_directories(RestuneServer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../F target_link_libraries(RestuneServer PRIVATE RestuneAuxUtils RestuneSockEnd RestuneExtAPIs ${LIBYAML_LIBRARIES}) # MODULA -find_package(PkgConfig REQUIRED) -pkg_check_modules(LIBYAML REQUIRED yaml-0.1) file(GLOB SOURCES "../Modula/Components/*.cpp" "../Modula/Common/*.cpp" "../Modula/CoreModules/*.cpp") add_library(RestuneAuxUtils ${SOURCES}) set_target_properties(RestuneAuxUtils PROPERTIES VERSION 1.0.0 SOVERSION 1) diff --git a/Core/Extensions/DBus/Include/RestuneDBusInternal.h b/Core/Extensions/DBus/Include/RestuneDBusInternal.h new file mode 100644 index 000000000..98c31eaf8 --- /dev/null +++ b/Core/Extensions/DBus/Include/RestuneDBusInternal.h @@ -0,0 +1,28 @@ +// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. +// SPDX-License-Identifier: BSD-3-Clause-Clear + +#ifndef RESTUNE_DBUS_INTERNAL_H +#define RESTUNE_DBUS_INTERNAL_H + +#include +#include + +class RestuneSDBus { +private: + sd_bus* mBus; + + RestuneSDBus(); + +public: + ~RestuneSDBus(); + + ErrCode stopService(const std::string& unitName); + ErrCode startService(const std::string& unitName); + ErrCode restartService(const std::string& unitName); + + // For custom use-cases, like StateDetector while still keeping + // a single open connection to system-bus. + sd_bus* getBus(); +}; + +#endif diff --git a/Core/Extensions/DBus/RestuneDBusInternal.cpp b/Core/Extensions/DBus/RestuneDBusInternal.cpp new file mode 100644 index 000000000..48cb7a6fc --- /dev/null +++ b/Core/Extensions/DBus/RestuneDBusInternal.cpp @@ -0,0 +1,96 @@ +// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. +// SPDX-License-Identifier: BSD-3-Clause-Clear + +#include "RestuneDBusInternal.h" + +#define SYSTEMD_DBUS_NAME "org.freedesktop.systemd1" +#define SYSTEMD_DBUS_PATH "/org/freedesktop/systemd1" +#define SYSTEMD_DBUS_IF "org.freedesktop.systemd1.Manager" + +RestuneSDBus::RestuneSDBus() { + this->mBus = nullptr; + + // Connect to the system bus + int32_t rc; + if((rc = sd_bus_default_system(&this->mBus)) < 0) { + TYPELOGV(SYSTEM_BUS_CONN_FAILED, strerror(-rc)); + } +} + +ErrCode startService(const std::string& unitName) { + if(this->mBus == nullptr) return RC_MODULE_INIT_FAILURE; + + // Start irqbalance + int32_t rc = sd_bus_call_method( + this->mBus, + SYSTEMD_DBUS_NAME, + SYSTEMD_DBUS_PATH, + SYSTEMD_DBUS_IF, + "StartUnit", + &error, + &reply, + "ss", + unitName.c_str(), + "replace" + ); + + if(rc < 0) { + LOGE("RESTUNE_COCO_TABLE", "Failed to start irqbalanced: " + std::string(error.message)); + return RC_DBUS_COMM_FAIL; + } + + sd_bus_message_unref(reply); + sd_bus_error_free(&error); + + return RC_SUCCESS; +} + +ErrCode stopService(const std::string& unitName) { + if(this->mBus == nullptr) return RC_DBUS_COMM_FAIL; + + rc = sd_bus_call_method( + this->mBus, + SYSTEMD_DBUS_NAME, + SYSTEMD_DBUS_PATH, + SYSTEMD_DBUS_IF, + "StopUnit", + &error, + &reply, + "ss", + unitName.c_str(), + "replace" + ); + + if(rc < 0) { + LOGE("RESTUNE_COCO_TABLE", "Failed to stop irqbalanced: " + std::string(error.message)); + return RC_DBUS_COMM_FAIL; + } + + sd_bus_message_unref(reply); + sd_bus_error_free(&error); + + return RC_SUCCESS; +} + +ErrCode restartService(const std::string& unitName) { + if(RC_IS_NOTOK(stopService(unitName))) { + return RC_DBUS_COMM_FAIL; + } + + if(RC_IS_NOTOK(stopService(unitName))) { + return RC_DBUS_COMM_FAIL; + } + + return RC_SUCCESS; +} + +sd_bus* RestuneSDBus::getBus() { + return this->mBus; +} + +RestuneSDBus::~RestuneSDBus() { + if(this->mBus != nullptr) { + sd_bus_unref(this->mBus); + this->mBus = nullptr; + } +} diff --git a/StateDetector.cpp b/Core/Extensions/DBus/StateDetector.cpp similarity index 90% rename from StateDetector.cpp rename to Core/Extensions/DBus/StateDetector.cpp index 2979e2d0c..10ee88533 100644 --- a/StateDetector.cpp +++ b/Core/Extensions/DBus/StateDetector.cpp @@ -2,9 +2,8 @@ // SPDX-License-Identifier: BSD-3-Clause-Clear #include -#include -#include +#include "RestuneDBusInternal.h" #include "Logger.h" #include "ComponentRegistry.h" #include "ResourceTunerSettings.h" @@ -15,7 +14,6 @@ #define DBUS_SIGNAL_INTERFACE "org.freedesktop.login1.Manager" #define DBUS_SIGNAL_NAME "PrepareForSleep" -static sd_bus* bus = nullptr; static sd_bus_slot* slot = nullptr; static sd_event* event = nullptr; @@ -31,13 +29,8 @@ static void cleanup() { sd_bus_slot_unref(slot); } - if(bus != nullptr) { - sd_bus_unref(bus); - } - event = nullptr; slot = nullptr; - bus = nullptr; } static int32_t onSdBusMessageReceived(sd_bus_message* message, @@ -92,13 +85,6 @@ static int32_t eventLoopTerminator(sd_event_source *s, } static void initHelper() { - // Connect to the system bus - if(sd_bus_default_system(&bus) < 0) { - LOGE("RESTUNE_DISPLAY_AWARE_OPS", "Failed to establish connection with system bus"); - cleanup(); - return; - } - // Create the Event Loop object if(sd_event_default(&event) < 0) { LOGE("RESTUNE_DISPLAY_AWARE_OPS", "Failed to create event-loop"); @@ -107,7 +93,7 @@ static void initHelper() { } // Subscribe to D-Bus signal (PrepareForSleep) - if(sd_bus_match_signal(bus, + if(sd_bus_match_signal(RestuneDBusInternal::getInstance()->getBus(), &slot, nullptr, DBUS_SIGNAL_SENDER_PATH, @@ -121,7 +107,7 @@ static void initHelper() { } // Listen for D-Bus events - if(sd_bus_attach_event(bus, event, 0) < 0) { + if(sd_bus_attach_event(RestuneDBusInternal::getInstance()->getBus(), event, 0) < 0) { LOGE("RESTUNE_DISPLAY_AWARE_OPS", "Failed to start event-loop"); cleanup(); return; diff --git a/Core/Extensions/Extensions.cpp b/Core/Extensions/Extensions/Extensions.cpp similarity index 100% rename from Core/Extensions/Extensions.cpp rename to Core/Extensions/Extensions/Extensions.cpp diff --git a/Core/Extensions/Include/Extensions.h b/Core/Extensions/Extensions/Include/Extensions.h similarity index 100% rename from Core/Extensions/Include/Extensions.h rename to Core/Extensions/Extensions/Include/Extensions.h diff --git a/Signals/Build/CMakeLists.txt b/Signals/Build/CMakeLists.txt index 3bc42d300..be72ffe4d 100644 --- a/Signals/Build/CMakeLists.txt +++ b/Signals/Build/CMakeLists.txt @@ -1,7 +1,5 @@ file(GLOB SOURCES "../*.cpp") -find_package(PkgConfig REQUIRED) -pkg_check_modules(LIBYAML REQUIRED yaml-0.1) add_library(RestuneSignals ${SOURCES}) set_target_properties(RestuneSignals PROPERTIES VERSION 1.0.0 SOVERSION 1) target_include_directories(RestuneSignals PRIVATE ${CMAKE_SOURCE_DIR}/Extensions/Include)