Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Core/Build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions Core/Extensions/DBus/Include/RestuneDBusInternal.h
Original file line number Diff line number Diff line change
@@ -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 <systemd/sd-bus.h>
#include <systemd/sd-event.h>

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
96 changes: 96 additions & 0 deletions Core/Extensions/DBus/RestuneDBusInternal.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
20 changes: 3 additions & 17 deletions StateDetector.cpp → Core/Extensions/DBus/StateDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// SPDX-License-Identifier: BSD-3-Clause-Clear

#include <thread>
#include <systemd/sd-bus.h>
#include <systemd/sd-event.h>

#include "RestuneDBusInternal.h"
#include "Logger.h"
#include "ComponentRegistry.h"
#include "ResourceTunerSettings.h"
Expand All @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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");
Expand All @@ -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,
Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions Signals/Build/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading