hocclk: major code refactor

move everything into its own directory, clean codebase up a lot
This commit is contained in:
souldbminersmwc
2026-05-08 22:43:14 -04:00
parent 65dfa8f48a
commit 3ca1f17e4d
78 changed files with 138 additions and 263 deletions

View File

@@ -0,0 +1,221 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include "ipc_server.h"
#include <string.h>
Result ipcServerInit(IpcServer* server, const char* name, u32 max_sessions)
{
if(max_sessions < 1 || max_sessions > (MAX_WAIT_OBJECTS - 1))
{
return MAKERESULT(Module_Libnx, LibnxError_BadInput);
}
server->srvName = smEncodeName(name);
server->max = max_sessions + 1;
server->count = 0;
Result rc = smRegisterService(&server->handles[0], server->srvName, false, max_sessions);
if(R_SUCCEEDED(rc))
{
server->count = 1;
}
return rc;
}
Result ipcServerExit(IpcServer* server)
{
for(u32 i = 0; i < server->count; i++)
{
svcCloseHandle(server->handles[i]);
}
server->count = 0;
return smUnregisterService(server->srvName);
}
static Result _ipcServerAddSession(IpcServer* server, Handle session)
{
if(server->count >= server->max)
{
return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
}
server->handles[server->count] = session;
server->count++;
return 0;
}
static Result _ipcServerDeleteSession(IpcServer* server, u32 index)
{
if(!index || index >= server->count)
{
return MAKERESULT(Module_Libnx, LibnxError_BadInput);
}
svcCloseHandle(server->handles[index]);
for(u32 j = index; j < (server->count - 1); j++)
{
server->handles[j] = server->handles[j + 1];
}
server->count--;
return 0;
}
static Result _ipcServerParseRequest(IpcServerRequest* r)
{
u8* base = armGetTls();
r->hipc = hipcParseRequest(base);
r->data.cmdId = 0;
r->data.size = 0;
r->data.ptr = NULL;
if(r->hipc.meta.type == CmifCommandType_Request)
{
IpcServerRawHeader* header = cmifGetAlignedDataStart(r->hipc.data.data_words, base);
size_t dataSize = r->hipc.meta.num_data_words * 4;
if(!header || dataSize < sizeof(IpcServerRawHeader) || header->magic != CMIF_IN_HEADER_MAGIC)
{
return MAKERESULT(Module_Libnx, LibnxError_BadInput);
}
r->data.cmdId = header->cmdId;
if(dataSize > sizeof(IpcServerRawHeader))
{
r->data.size = dataSize - sizeof(IpcServerRawHeader);
r->data.ptr = ((u8*)header) + sizeof(IpcServerRawHeader);
}
}
return 0;
}
static void _ipcServerPrepareResponse(Result rc, void* data, size_t dataSize)
{
u8* base = armGetTls();
HipcRequest hipc = hipcMakeRequestInline(base,
.type = CmifCommandType_Request,
.num_data_words = (sizeof(IpcServerRawHeader) + dataSize + 0x10) / 4,
);
IpcServerRawHeader* rawHeader = cmifGetAlignedDataStart(hipc.data_words, base);
rawHeader->magic = CMIF_OUT_HEADER_MAGIC;
rawHeader->result = rc;
if(R_SUCCEEDED(rc))
{
memcpy(((u8*)rawHeader) + sizeof(IpcServerRawHeader), data, dataSize);
}
}
static Result _ipcServerProcessNewSession(IpcServer* server)
{
Handle session;
Result rc = svcAcceptSession(&session, server->handles[0]);
if(R_SUCCEEDED(rc) && R_FAILED(rc = _ipcServerAddSession(server, session)))
{
svcCloseHandle(session);
}
return rc;
}
static Result _ipcServerProcessSession(IpcServer* server, IpcServerRequestHandler handler, void* userdata, u32 handleIndex)
{
s32 unusedIndex;
IpcServerRequest r;
size_t dataSize = 0;
u8 data[IPC_SERVER_EXT_RESPONSE_MAX_DATA_SIZE];
bool close = false;
Result rc = svcReplyAndReceive(&unusedIndex, &server->handles[handleIndex], 1, 0, UINT64_MAX);
if(R_SUCCEEDED(rc))
{
rc = _ipcServerParseRequest(&r);
}
if(R_SUCCEEDED(rc))
{
switch(r.hipc.meta.type)
{
case CmifCommandType_Request:
_ipcServerPrepareResponse(
handler(userdata, &r, data, &dataSize),
data,
dataSize
);
break;
case CmifCommandType_Close:
_ipcServerPrepareResponse(0, NULL, 0);
close = true;
break;
default:
_ipcServerPrepareResponse(MAKERESULT(11, 403), NULL, 0);
break;
}
rc = svcReplyAndReceive(&unusedIndex, &server->handles[handleIndex], 0, server->handles[handleIndex], 0);
if(rc == KERNELRESULT(TimedOut))
{
rc = 0;
}
}
if(R_FAILED(rc) || close)
{
_ipcServerDeleteSession(server, handleIndex);
}
return rc;
}
Result ipcServerProcess(IpcServer* server, IpcServerRequestHandler handler, void* userdata)
{
s32 handleIndex = -1;
Result rc = svcWaitSynchronization(&handleIndex, server->handles, server->count, UINT64_MAX);
if(R_SUCCEEDED(rc) && (handleIndex < 0 || handleIndex >= server->count))
{
rc = MAKERESULT(Module_Libnx, LibnxError_NotFound);
}
if(R_SUCCEEDED(rc))
{
if(handleIndex)
{
rc = _ipcServerProcessSession(server, handler, userdata, handleIndex);
}
else
{
rc = _ipcServerProcessNewSession(server);
}
}
return rc;
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <switch.h>
#define IPC_SERVER_EXT_RESPONSE_MAX_DATA_SIZE (0x100 - 0x10 - sizeof(IpcServerRawHeader))
typedef struct
{
u64 magic;
union
{
u64 cmdId;
u64 result;
};
} IpcServerRawHeader;
typedef struct
{
SmServiceName srvName;
Handle handles[MAX_WAIT_OBJECTS];
u32 max;
u32 count;
} IpcServer;
typedef struct
{
u64 cmdId;
void* ptr;
size_t size;
} IpcServerRequestData;
typedef struct
{
HipcParsedRequest hipc;
IpcServerRequestData data;
} IpcServerRequest;
typedef Result (*IpcServerRequestHandler)(void* userdata, const IpcServerRequest* r, u8* out_data, size_t* out_dataSize);
Result ipcServerInit(IpcServer* server, const char* name, u32 max_sessions);
Result ipcServerExit(IpcServer* server);
Result ipcServerProcess(IpcServer* server, IpcServerRequestHandler handler, void* userdata);
Result ipcServerParseCommand(const IpcServerRequest* r, size_t* out_datasize, void** out_data, u64* out_cmd);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,300 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include "ipc_service.hpp"
#include <cstring>
#include <switch.h>
#include "../hos/apm_ext.h"
#include <i2c.h>
#include <t210.h>
#include <max17050.h>
#include <tmp451.h>
#include <ipc_server.h>
#include <lockable_mutex.h>
#include "../file/file_utils.hpp"
#include "../file/errors.hpp"
#include "../mgr/clock_manager.hpp"
#include "../file/config.hpp"
#include "../file/kip.hpp"
namespace ipcService {
namespace {
bool gRunning = false;
Thread gThread;
LockableMutex gThreadMutex;
IpcServer gServer;
Result GetApiVersion(u32* out_version) {
*out_version = HOCCLK_IPC_API_VERSION;
return 0;
}
Result GetVersionString(char* out_buf, size_t bufSize) {
if (bufSize) {
strncpy(out_buf, TARGET_VERSION, bufSize-1);
}
return 0;
}
Result GetCurrentContext(HocClkContext* out_ctx) {
*out_ctx = clockManager::GetCurrentContext();
return 0;
}
Result ExitHandler() {
clockManager::SetRunning(false);
return 0;
}
Result GetProfileCount(std::uint64_t* tid, std::uint8_t* out_count) {
if (!config::HasProfilesLoaded()) {
return HOCCLK_ERROR(ConfigNotLoaded);
}
*out_count = config::GetProfileCount(*tid);
return 0;
}
Result GetProfiles(std::uint64_t* tid, HocClkTitleProfileList* out_profiles) {
if (!config::HasProfilesLoaded()) {
return HOCCLK_ERROR(ConfigNotLoaded);
}
config::GetProfiles(*tid, out_profiles);
return 0;
}
Result SetProfiles(HocClkIpc_SetProfiles_Args* args) {
if (!config::HasProfilesLoaded()) {
return HOCCLK_ERROR(ConfigNotLoaded);
}
HocClkTitleProfileList profiles = args->profiles;
if (!config::SetProfiles(args->tid, &profiles, true)) {
return HOCCLK_ERROR(ConfigSaveFailed);
}
return 0;
}
Result SetEnabled(std::uint8_t* enabled) {
config::SetEnabled(*enabled);
return 0;
}
Result SetOverride(HocClkIpc_SetOverride_Args* args) {
if (!HOCCLK_ENUM_VALID(HocClkModule, args->module)) {
return HOCCLK_ERROR(Generic);
}
config::SetOverrideHz(args->module, args->hz);
return 0;
}
Result GetConfigValuesHandler(HocClkConfigValueList* out_configValues) {
if (!config::HasProfilesLoaded()) {
return HOCCLK_ERROR(ConfigNotLoaded);
}
config::GetConfigValues(out_configValues);
return 0;
}
Result SetConfigValuesHandler(HocClkConfigValueList* configValues) {
if (!config::HasProfilesLoaded()) {
return HOCCLK_ERROR(ConfigNotLoaded);
}
HocClkConfigValueList copy = *configValues;
if (!config::SetConfigValues(&copy, true)) {
return HOCCLK_ERROR(ConfigSaveFailed);
}
return 0;
}
Result GetFreqList(HocClkIpc_GetFreqList_Args* args, std::uint32_t* out_list, std::size_t size, std::uint32_t* out_count) {
if (!HOCCLK_ENUM_VALID(HocClkModule, args->module)) {
return HOCCLK_ERROR(Generic);
}
if (args->maxCount != size/sizeof(*out_list)) {
return HOCCLK_ERROR(Generic);
}
clockManager::GetFreqList(args->module, out_list, args->maxCount, out_count);
return 0;
}
Result ServiceHandlerFunc(void* arg, const IpcServerRequest* r, u8* out_data, size_t* out_dataSize) {
(void)arg;
switch (r->data.cmdId) {
case HocClkIpcCmd_GetApiVersion:
*out_dataSize = sizeof(u32);
return GetApiVersion((u32*)out_data);
case HocClkIpcCmd_GetVersionString:
if (r->hipc.meta.num_recv_buffers >= 1) {
return GetVersionString(
(char*)hipcGetBufferAddress(r->hipc.data.recv_buffers),
hipcGetBufferSize(r->hipc.data.recv_buffers)
);
}
break;
case HocClkIpcCmd_GetCurrentContext:
if (r->data.size >= sizeof(std::uint64_t) && r->hipc.meta.num_recv_buffers >= 1) {
size_t bufSize = hipcGetBufferSize(r->hipc.data.recv_buffers);
if (bufSize >= sizeof(HocClkContext)) {
return GetCurrentContext((HocClkContext*)hipcGetBufferAddress(r->hipc.data.recv_buffers));
}
}
break;
case HocClkIpcCmd_Exit:
return ExitHandler();
case HocClkIpcCmd_GetProfileCount:
if (r->data.size >= sizeof(std::uint64_t)) {
*out_dataSize = sizeof(std::uint8_t);
return GetProfileCount((std::uint64_t*)r->data.ptr, (std::uint8_t*)out_data);
}
break;
case HocClkIpcCmd_GetProfiles:
if (r->data.size >= sizeof(std::uint64_t) && r->hipc.meta.num_recv_buffers >= 1) {
size_t bufSize = hipcGetBufferSize(r->hipc.data.recv_buffers);
if (bufSize >= sizeof(HocClkTitleProfileList)) {
return GetProfiles((std::uint64_t*)r->data.ptr, (HocClkTitleProfileList*)hipcGetBufferAddress(r->hipc.data.recv_buffers));
}
}
break;
case HocClkIpcCmd_SetProfiles:
if (r->data.size >= sizeof(HocClkIpc_SetProfiles_Args)) {
return SetProfiles((HocClkIpc_SetProfiles_Args*)r->data.ptr);
}
break;
case HocClkIpcCmd_SetEnabled:
if (r->data.size >= sizeof(std::uint8_t)) {
return SetEnabled((std::uint8_t*)r->data.ptr);
}
break;
case HocClkIpcCmd_SetOverride:
if (r->data.size >= sizeof(HocClkIpc_SetOverride_Args)) {
return SetOverride((HocClkIpc_SetOverride_Args*)r->data.ptr);
}
break;
case HocClkIpcCmd_GetConfigValues:
if (r->hipc.meta.num_recv_buffers >= 1) {
size_t bufSize = hipcGetBufferSize(r->hipc.data.recv_buffers);
if (bufSize >= sizeof(HocClkConfigValueList)) {
return GetConfigValuesHandler((HocClkConfigValueList*)hipcGetBufferAddress(r->hipc.data.recv_buffers));
}
}
break;
case HocClkIpcCmd_SetConfigValues:
if (r->hipc.meta.num_send_buffers >= 1) {
size_t bufSize = hipcGetBufferSize(r->hipc.data.send_buffers);
if (bufSize >= sizeof(HocClkConfigValueList)) {
return SetConfigValuesHandler((HocClkConfigValueList*)hipcGetBufferAddress(r->hipc.data.send_buffers));
}
}
break;
case HocClkIpcCmd_GetFreqList:
if (r->data.size >= sizeof(HocClkIpc_GetFreqList_Args) && r->hipc.meta.num_recv_buffers >= 1) {
*out_dataSize = sizeof(std::uint32_t);
return GetFreqList(
(HocClkIpc_GetFreqList_Args*)r->data.ptr,
(std::uint32_t*)hipcGetBufferAddress(r->hipc.data.recv_buffers),
hipcGetBufferSize(r->hipc.data.recv_buffers),
(std::uint32_t*)out_data
);
}
break;
case HocClkIpcCmd_SetKipData:
if (r->data.size >= 0) {
kip::SetKipData();
return 0;
}
break;
}
return HOCCLK_ERROR(Generic);
}
void ProcessThreadFunc(void* arg) {
(void)arg;
Result rc;
while (true) {
rc = ipcServerProcess(&gServer, &ServiceHandlerFunc, nullptr);
if (R_FAILED(rc)) {
if (rc == KERNELRESULT(Cancelled)) {
return;
}
if (rc != KERNELRESULT(ConnectionClosed)) {
fileUtils::LogLine("[ipc] ipcServerProcess: [0x%x] %04d-%04d", rc, R_MODULE(rc), R_DESCRIPTION(rc));
}
}
}
}
}
void Initialize() {
std::int32_t priority;
Result rc = svcGetThreadPriority(&priority, CUR_THREAD_HANDLE);
ASSERT_RESULT_OK(rc, "svcGetThreadPriority");
rc = ipcServerInit(&gServer, HOCCLK_IPC_SERVICE_NAME, 42);
ASSERT_RESULT_OK(rc, "ipcServerInit");
rc = threadCreate(&gThread, &ProcessThreadFunc, nullptr, NULL, 0x4000, priority, -2);
ASSERT_RESULT_OK(rc, "threadCreate");
gRunning = false;
}
void Exit() {
SetRunning(false);
Result rc = threadClose(&gThread);
ASSERT_RESULT_OK(rc, "threadClose");
rc = ipcServerExit(&gServer);
ASSERT_RESULT_OK(rc, "ipcServerExit");
}
void SetRunning(bool running) {
std::scoped_lock lock{gThreadMutex};
if (gRunning == running) {
return;
}
gRunning = running;
if (running) {
Result rc = threadStart(&gThread);
ASSERT_RESULT_OK(rc, "threadStart");
} else {
svcCancelSynchronization(gThread.handle);
threadWaitForExit(&gThread);
}
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <hocclk.h>
#include <switch.h>
namespace ipcService {
void Initialize();
void Exit();
void SetRunning(bool running);
}