Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fan controller plugin #131

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
75 changes: 75 additions & 0 deletions kernel/src/Plugins/FanController/FanController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

#include "FanController.hpp"
#include <Utils/Kdlsym.hpp>
#include <Utils/Logger.hpp>
#include <Utils/SysWrappers.hpp>

using namespace Mira::Plugins;

FanController::FanController()
{
}

FanController::~FanController()
{
}

void FanController::SetFanThreshhold(int fanController_input)
{
if (fanController_input < 59 || fanController_input > 79)
{
WriteLog(LL_Error, "Unsafe fan controller setting: %i°C", fanController_input);
return;
}

auto s_Thread = curthread;
if (s_Thread == nullptr)
{
WriteLog(LL_Error, "could not get current thread.");
return;
}

if (fanController_orig == fanController_input)
{
WriteLog(LL_Info, "Fan controller already set to %i°C", fanController_input);
return;
}

fanController_desired = fanController_input;

int fd = kopen_t("/dev/icc_fan", 0x0000, 0, s_Thread); // O_RDONLY
if (fd <= 0) {
WriteLog(LL_Info, "unable to open \"/dev/icc_fan\"");
return;
}

char data[10] = {0x00, 0x00, 0x00, 0x00, 0x00, (char)fanController_input, 0x00, 0x00, 0x00, 0x00};
kioctl_t(fd, 0xC01C8F07, data, s_Thread);
kclose_t(fd, s_Thread);

WriteLog(LL_Info, "Successfully set fan controller to %i°C", fanController_input);
}

bool FanController::OnLoad()
{
SetFanThreshhold(fanController_desired);
return true;
}

bool FanController::OnUnload()
{
SetFanThreshhold(fanController_orig);
return true;
}

bool FanController::OnSuspend()
{
return true;
}

bool FanController::OnResume()
{
return true;
}
26 changes: 26 additions & 0 deletions kernel/src/Plugins/FanController/FanController.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <Utils/IModule.hpp>
#include <Utils/Types.hpp>

namespace Mira
{
namespace Plugins
{
class FanController : public Mira::Utils::IModule
{
private:
int fanController_orig = 79;
int fanController_desired = 79;
public:
FanController();
virtual ~FanController();

virtual const char* GetName() override { return "FanController"; }
virtual bool OnLoad() override;
virtual bool OnUnload() override;
virtual bool OnSuspend() override;
virtual bool OnResume() override;
void SetFanThreshhold(int fanController_input);
};
}
}
45 changes: 44 additions & 1 deletion kernel/src/Plugins/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <Plugins/RemotePlayEnabler/RemotePlayEnabler.hpp>
#include <Plugins/SyscallGuard/SyscallGuardPlugin.hpp>
#include <Plugins/TTYRedirector/TTYRedirector.hpp>
#include <Plugins/FanController/FanController.hpp>

// Utility functions
#include <Utils/Logger.hpp>
Expand All @@ -39,7 +40,8 @@ PluginManager::PluginManager() :
m_MorpheusEnabler(nullptr),
m_RemotePlayEnabler(nullptr),
m_SyscallGuard(nullptr),
m_TTYRedirector(nullptr)
m_TTYRedirector(nullptr),
m_FanController(nullptr)
{
// Hushes error: private field 'm_FileManager' is not used [-Werror,-Wunused-private-field]
m_Logger = nullptr;
Expand Down Expand Up @@ -150,6 +152,15 @@ bool PluginManager::OnLoad()
break;
}

// Initialize FanController
m_FanController = new Mira::Plugins::FanController();
if (m_FanController == nullptr)
{
WriteLog(LL_Error, "could not allocate fan controller.");
s_Success = false;
break;
}

// Initialize TTYRedirector
m_TTYRedirector = new Mira::Plugins::TTYRedirector();
if (m_TTYRedirector == nullptr)
Expand Down Expand Up @@ -214,6 +225,12 @@ bool PluginManager::OnLoad()
WriteLog(LL_Error, "could not load remote play enabler.");
}

if (m_FanController)
{
if (!m_FanController->OnLoad())
WriteLog(LL_Error, "could not load fan controller.");
}

if (m_TTYRedirector)
{
if (!m_TTYRedirector->OnLoad())
Expand Down Expand Up @@ -376,6 +393,18 @@ bool PluginManager::OnUnload()
m_RemotePlayEnabler = nullptr;
}

// Delete FanController
if (m_FanController)
{
WriteLog(LL_Debug, "unloading fan controller");
if (!m_FanController->OnUnload())
WriteLog(LL_Error, "fan controller could not unload");

// Free FanController
delete m_FanController;
m_FanController = nullptr;
}

// Delete the debugger
// NOTE: Don't unload before the debugger for catch error if something wrong
if (m_Debugger)
Expand Down Expand Up @@ -483,6 +512,13 @@ bool PluginManager::OnSuspend()
WriteLog(LL_Error, "remote play enabler suspend failed");
}

// Suspend FanController (does nothing)
if (m_FanController)
{
if (!m_FanController->OnSuspend())
WriteLog(LL_Error, "fan controller suspend failed");
}

// Nota: Don't suspend before the debugger for catch error if something when wrong
if (m_Debugger)
{
Expand Down Expand Up @@ -557,6 +593,13 @@ bool PluginManager::OnResume()
WriteLog(LL_Error, "remote play enabler resume failed");
}

WriteLog(LL_Debug, "resuming fan controller");
if (m_FanController)
{
if (!m_FanController->OnResume())
WriteLog(LL_Error, "fan controller resume failed");
}

WriteLog(LL_Debug, "resuming tty redirector");
if (m_TTYRedirector)
{
Expand Down
6 changes: 4 additions & 2 deletions kernel/src/Plugins/PluginManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ namespace Mira
virtual bool OnUnload() override;
virtual bool OnSuspend() override;
virtual bool OnResume() override;

virtual bool OnProcessExec(struct proc* p_Process) override;
virtual bool OnProcessExecEnd(struct proc* p_Process) override;
virtual bool OnProcessExit(struct proc* p_Process) override;

private:
Mira::Utils::IModule* m_Logger;
Mira::Utils::IModule* m_Debugger;
Expand All @@ -43,6 +43,7 @@ namespace Mira
Mira::Utils::IModule* m_RemotePlayEnabler;
Mira::Utils::IModule* m_SyscallGuard;
Mira::Utils::IModule* m_TTYRedirector;
Mira::Utils::IModule* m_FanController;

public:
Mira::Utils::IModule* GetDebugger() { return m_Debugger; }
Expand All @@ -53,6 +54,7 @@ namespace Mira
Mira::Utils::IModule* GetMorpheusEnabler() { return m_MorpheusEnabler; }
Mira::Utils::IModule* GetRemotePlayEnabler() { return m_RemotePlayEnabler; }
Mira::Utils::IModule* GetSyscallGuard() { return m_SyscallGuard; }
Mira::Utils::IModule* GetFanController() { return m_FanController; }
};
}
}