Revert "hoc-clk: add live vdd2, live boost clock and basic pwm dimming"

This reverts commit 15b7df8ef1.
This commit is contained in:
souldbminersmwc
2025-11-09 16:14:52 -05:00
parent 22ec140738
commit 21a3f953d7
3804 changed files with 435 additions and 570162 deletions

View File

@@ -1,31 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "htc_i_driver.hpp"
namespace ams::htc::server::driver {
class DriverManager {
private:
IDriver *m_driver;
public:
DriverManager(IDriver *driver) : m_driver(driver) { /* ... */ }
IDriver *GetDriver() { return m_driver; }
};
}

View File

@@ -1,187 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_htclow_driver.hpp"
namespace ams::htc::server::driver {
namespace {
constexpr ALWAYS_INLINE htclow::impl::ChannelInternalType GetHtclowChannel(htclow::ChannelId channel_id, htclow::ModuleId module_id) {
return {
.channel_id = channel_id,
.reserved = 0,
.module_id = module_id,
};
}
}
void HtclowDriver::WaitTask(u32 task_id) {
os::WaitEvent(m_manager->GetTaskEvent(task_id));
}
void HtclowDriver::SetDisconnectionEmulationEnabled(bool en) {
/* NOTE: Nintendo ignores the input, here. */
AMS_UNUSED(en);
m_disconnection_emulation_enabled = false;
}
Result HtclowDriver::Open(htclow::ChannelId channel) {
/* Check the channel/module combination. */
if (m_module_id == htclow::ModuleId::Htcmisc) {
AMS_ABORT_UNLESS(channel == HtcmiscClientChannelId );
} else if (m_module_id == htclow::ModuleId::Htcs) {
AMS_ABORT_UNLESS(channel == 0);
} else {
AMS_ABORT("Unsupported channel");
}
R_RETURN(this->Open(channel, m_default_receive_buffer, sizeof(m_default_receive_buffer), m_default_send_buffer, sizeof(m_default_send_buffer)));
}
Result HtclowDriver::Open(htclow::ChannelId channel, void *receive_buffer, size_t receive_buffer_size, void *send_buffer, size_t send_buffer_size) {
/* Open the channel. */
R_TRY(m_manager->Open(GetHtclowChannel(channel, m_module_id)));
/* Set the send/receive buffers. */
m_manager->SetReceiveBuffer(GetHtclowChannel(channel, m_module_id), receive_buffer, receive_buffer_size);
m_manager->SetSendBuffer(GetHtclowChannel(channel, m_module_id), send_buffer, send_buffer_size);
R_SUCCEED();
}
void HtclowDriver::Close(htclow::ChannelId channel) {
/* Close the channel. */
const auto result = m_manager->Close(GetHtclowChannel(channel, m_module_id));
R_ASSERT(result);
}
Result HtclowDriver::Connect(htclow::ChannelId channel) {
/* Check if we should emulate disconnection. */
R_UNLESS(!m_disconnection_emulation_enabled, htclow::ResultConnectionFailure());
/* Begin connecting. */
u32 task_id{};
R_TRY(m_manager->ConnectBegin(std::addressof(task_id), GetHtclowChannel(channel, m_module_id)));
/* Wait for the task to complete. */
this->WaitTask(task_id);
/* Finish connecting. */
R_TRY(m_manager->ConnectEnd(GetHtclowChannel(channel, m_module_id), task_id));
R_SUCCEED();
}
void HtclowDriver::Shutdown(htclow::ChannelId channel) {
/* Shut down the channel. */
m_manager->Shutdown(GetHtclowChannel(channel, m_module_id));
}
Result HtclowDriver::Send(s64 *out, const void *src, s64 src_size, htclow::ChannelId channel) {
/* Check if we should emulate disconnection. */
R_UNLESS(!m_disconnection_emulation_enabled, htclow::ResultConnectionFailure());
/* Validate that dst_size is okay. */
R_UNLESS(util::IsIntValueRepresentable<size_t>(src_size), htclow::ResultOverflow());
/* Repeatedly send until we're done. */
size_t cur_send;
size_t sent;
for (sent = 0; sent < static_cast<size_t>(src_size); sent += cur_send) {
/* Begin sending. */
u32 task_id{};
R_TRY(m_manager->SendBegin(std::addressof(task_id), std::addressof(cur_send), static_cast<const u8 *>(src) + sent, static_cast<size_t>(src_size) - sent, GetHtclowChannel(channel, m_module_id)));
/* Wait for the task to complete. */
this->WaitTask(task_id);
/* Finish sending. */
R_ABORT_UNLESS(m_manager->SendEnd(task_id));
}
/* Set the output sent size. */
*out = static_cast<s64>(sent);
R_SUCCEED();
}
Result HtclowDriver::ReceiveInternal(size_t *out, void *dst, size_t dst_size, htclow::ChannelId channel, htclow::ReceiveOption option) {
/* Determine whether we're blocking. */
const bool blocking = option != htclow::ReceiveOption_NonBlocking;
/* Begin receiving. */
u32 task_id{};
R_TRY(m_manager->ReceiveBegin(std::addressof(task_id), GetHtclowChannel(channel, m_module_id), blocking ? 1 : 0));
/* Wait for the task to complete. */
this->WaitTask(task_id);
/* Finish receiving. */
R_RETURN(m_manager->ReceiveEnd(out, dst, dst_size, GetHtclowChannel(channel, m_module_id), task_id));
}
Result HtclowDriver::Receive(s64 *out, void *dst, s64 dst_size, htclow::ChannelId channel, htclow::ReceiveOption option) {
/* Check if we should emulate disconnection. */
R_UNLESS(!m_disconnection_emulation_enabled, htclow::ResultConnectionFailure());
/* Validate that dst_size is okay. */
R_UNLESS(util::IsIntValueRepresentable<size_t>(dst_size), htclow::ResultOverflow());
/* Determine the minimum allowable receive size. */
size_t min_size;
switch (option) {
case htclow::ReceiveOption_NonBlocking: min_size = 0; break;
case htclow::ReceiveOption_ReceiveAnyData: min_size = 1; break;
case htclow::ReceiveOption_ReceiveAllData: min_size = dst_size; break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
/* Repeatedly receive. */
size_t received = 0;
do {
size_t cur_received;
const Result result = this->ReceiveInternal(std::addressof(cur_received), static_cast<u8 *>(dst) + received, static_cast<size_t>(dst_size) - received, channel, option);
if (R_FAILED(result)) {
if (htclow::ResultChannelReceiveBufferEmpty::Includes(result)) {
R_UNLESS(option != htclow::ReceiveOption_NonBlocking, htclow::ResultNonBlockingReceiveFailed());
}
if (htclow::ResultChannelNotExist::Includes(result)) {
*out = received;
}
R_RETURN(result);
}
received += cur_received;
} while (received < min_size);
/* Set the output received size. */
*out = static_cast<s64>(received);
R_SUCCEED();
}
htclow::ChannelState HtclowDriver::GetChannelState(htclow::ChannelId channel) {
return m_manager->GetChannelState(GetHtclowChannel(channel, m_module_id));
}
os::EventType *HtclowDriver::GetChannelStateEvent(htclow::ChannelId channel) {
return m_manager->GetChannelStateEvent(GetHtclowChannel(channel, m_module_id));
}
}

View File

@@ -1,50 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "../../../htclow/htclow_manager.hpp"
#include "htc_i_driver.hpp"
namespace ams::htc::server::driver {
class HtclowDriver final : public IDriver {
private:
static constexpr size_t DefaultBufferSize = 128_KB;
private:
u8 m_default_receive_buffer[DefaultBufferSize];
u8 m_default_send_buffer[DefaultBufferSize];
htclow::HtclowManager *m_manager;
bool m_disconnection_emulation_enabled;
htclow::ModuleId m_module_id;
public:
HtclowDriver(htclow::HtclowManager *manager, htclow::ModuleId module_id) : m_manager(manager), m_disconnection_emulation_enabled(false), m_module_id(module_id) { /* ... */ }
private:
void WaitTask(u32 task_id);
Result ReceiveInternal(size_t *out, void *dst, size_t dst_size, htclow::ChannelId channel, htclow::ReceiveOption option);
public:
virtual void SetDisconnectionEmulationEnabled(bool en) override;
virtual Result Open(htclow::ChannelId channel) override;
virtual Result Open(htclow::ChannelId channel, void *receive_buffer, size_t receive_buffer_size, void *send_buffer, size_t send_buffer_size) override;
virtual void Close(htclow::ChannelId channel) override;
virtual Result Connect(htclow::ChannelId channel) override;
virtual void Shutdown(htclow::ChannelId channel) override;
virtual Result Send(s64 *out, const void *src, s64 src_size, htclow::ChannelId channel) override;
virtual Result Receive(s64 *out, void *dst, s64 dst_size, htclow::ChannelId channel, htclow::ReceiveOption option) override;
virtual htclow::ChannelState GetChannelState(htclow::ChannelId channel) override;
virtual os::EventType *GetChannelStateEvent(htclow::ChannelId channel) override;
};
}

View File

@@ -1,35 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::htc::server::driver {
class IDriver {
public:
virtual void SetDisconnectionEmulationEnabled(bool en) = 0;
virtual Result Open(htclow::ChannelId channel) = 0;
virtual Result Open(htclow::ChannelId channel, void *receive_buffer, size_t receive_buffer_size, void *send_buffer, size_t send_buffer_size) = 0;
virtual void Close(htclow::ChannelId channel) = 0;
virtual Result Connect(htclow::ChannelId channel) = 0;
virtual void Shutdown(htclow::ChannelId channel) = 0;
virtual Result Send(s64 *out, const void *src, s64 src_size, htclow::ChannelId channel) = 0;
virtual Result Receive(s64 *out, void *dst, s64 dst_size, htclow::ChannelId channel, htclow::ReceiveOption option) = 0;
virtual htclow::ChannelState GetChannelState(htclow::ChannelId channel) = 0;
virtual os::EventType *GetChannelStateEvent(htclow::ChannelId channel) = 0;
};
}

View File

@@ -1,165 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_htc_service_object.hpp"
#include "../../htcfs/htcfs_working_directory.hpp"
namespace ams::htc::server {
HtcServiceObject::HtcServiceObject(htclow::HtclowManager *htclow_manager) : m_set(), m_misc_impl(htclow_manager), m_observer(m_misc_impl), m_mutex(){
/* Initialize our set. */
m_set.Initialize(MaxSetElements, m_set_memory, sizeof(m_set_memory));
}
HtcmiscImpl *HtcServiceObject::GetHtcmiscImpl() {
return std::addressof(m_misc_impl);
}
Result HtcServiceObject::GetEnvironmentVariable(sf::Out<s32> out_size, const sf::OutBuffer &out, const sf::InBuffer &name) {
/* Get the variable. */
size_t var_size = std::numeric_limits<size_t>::max();
R_TRY(m_misc_impl.GetEnvironmentVariable(std::addressof(var_size), reinterpret_cast<char *>(out.GetPointer()), out.GetSize(), reinterpret_cast<const char *>(name.GetPointer()), name.GetSize()));
/* Check the output size. */
R_UNLESS(util::IsIntValueRepresentable<s32>(var_size), htc::ResultUnknown());
/* Set the output size. */
*out_size = static_cast<s32>(var_size);
R_SUCCEED();
}
Result HtcServiceObject::GetEnvironmentVariableLength(sf::Out<s32> out_size, const sf::InBuffer &name) {
/* Get the variable. */
size_t var_size = std::numeric_limits<size_t>::max();
R_TRY(m_misc_impl.GetEnvironmentVariableLength(std::addressof(var_size), reinterpret_cast<const char *>(name.GetPointer()), name.GetSize()));
/* Check the output size. */
R_UNLESS(util::IsIntValueRepresentable<s32>(var_size), htc::ResultUnknown());
/* Set the output size. */
*out_size = static_cast<s32>(var_size);
R_SUCCEED();
}
Result HtcServiceObject::GetHostConnectionEvent(sf::OutCopyHandle out) {
/* Set the output handle. */
out.SetValue(m_observer.GetConnectEvent()->GetReadableHandle(), false);
R_SUCCEED();
}
Result HtcServiceObject::GetHostDisconnectionEvent(sf::OutCopyHandle out) {
/* Set the output handle. */
out.SetValue(m_observer.GetDisconnectEvent()->GetReadableHandle(), false);
R_SUCCEED();
}
Result HtcServiceObject::GetHostConnectionEventForSystem(sf::OutCopyHandle out) {
/* NOTE: Nintendo presumably reserved this command in case they need it, but they haven't implemented it yet. */
AMS_UNUSED(out);
AMS_ABORT("HostEventForSystem not implemented.");
}
Result HtcServiceObject::GetHostDisconnectionEventForSystem(sf::OutCopyHandle out) {
/* NOTE: Nintendo presumably reserved this command in case they need it, but they haven't implemented it yet. */
AMS_UNUSED(out);
AMS_ABORT("HostEventForSystem not implemented.");
}
Result HtcServiceObject::GetWorkingDirectoryPath(const sf::OutBuffer &out, s32 max_len) {
R_RETURN(htcfs::GetWorkingDirectory(reinterpret_cast<char *>(out.GetPointer()), max_len));
}
Result HtcServiceObject::GetWorkingDirectoryPathSize(sf::Out<s32> out_size) {
R_RETURN(htcfs::GetWorkingDirectorySize(out_size.GetPointer()));
}
Result HtcServiceObject::RunOnHostStart(sf::Out<u32> out_id, sf::OutCopyHandle out, const sf::InBuffer &args) {
/* Begin the run on host task. */
os::NativeHandle event_handle;
R_TRY(m_misc_impl.RunOnHostBegin(out_id.GetPointer(), std::addressof(event_handle), reinterpret_cast<const char *>(args.GetPointer()), args.GetSize()));
/* Add the task id to our set. */
{
std::scoped_lock lk(m_mutex);
m_set.insert(*out_id);
}
/* Set the output event. */
out.SetValue(event_handle, true);
R_SUCCEED();
}
Result HtcServiceObject::RunOnHostResults(sf::Out<s32> out_result, u32 id) {
/* Verify that we have the task. */
{
std::scoped_lock lk(m_mutex);
R_UNLESS(m_set.erase(id), htc::ResultInvalidTaskId());
}
/* Finish the run on host task. */
R_RETURN(m_misc_impl.RunOnHostEnd(out_result.GetPointer(), id));
}
Result HtcServiceObject::GetBridgeIpAddress(const sf::OutBuffer &out) {
/* NOTE: Atmosphere does not support HostBridge, and it's unclear if we ever will. */
AMS_UNUSED(out);
AMS_ABORT("HostBridge currently not supported.");
}
Result HtcServiceObject::GetBridgePort(const sf::OutBuffer &out) {
/* NOTE: Atmosphere does not support HostBridge, and it's unclear if we ever will. */
AMS_UNUSED(out);
AMS_ABORT("HostBridge currently not supported.");
}
Result HtcServiceObject::SetCradleAttached(bool attached) {
/* NOTE: Atmosphere does not support HostBridge, and it's unclear if we ever will. */
AMS_UNUSED(attached);
AMS_ABORT("HostBridge currently not supported.");
}
Result HtcServiceObject::GetBridgeSubnetMask(const sf::OutBuffer &out) {
/* NOTE: Atmosphere does not support HostBridge, and it's unclear if we ever will. */
AMS_UNUSED(out);
AMS_ABORT("HostBridge currently not supported.");
}
Result HtcServiceObject::GetBridgeMacAddress(const sf::OutBuffer &out) {
/* NOTE: Atmosphere does not support HostBridge, and it's unclear if we ever will. */
AMS_UNUSED(out);
AMS_ABORT("HostBridge currently not supported.");
}
Result HtcServiceObject::SetBridgeIpAddress(const sf::InBuffer &arg) {
/* NOTE: Atmosphere does not support HostBridge, and it's unclear if we ever will. */
AMS_UNUSED(arg);
AMS_ABORT("HostBridge currently not supported.");
}
Result HtcServiceObject::SetBridgeSubnetMask(const sf::InBuffer &arg) {
/* NOTE: Atmosphere does not support HostBridge, and it's unclear if we ever will. */
AMS_UNUSED(arg);
AMS_ABORT("HostBridge currently not supported.");
}
Result HtcServiceObject::SetBridgePort(const sf::InBuffer &arg) {
/* NOTE: Atmosphere does not support HostBridge, and it's unclear if we ever will. */
AMS_UNUSED(arg);
AMS_ABORT("HostBridge currently not supported.");
}
}

View File

@@ -1,61 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "../../htclow/htclow_manager.hpp"
#include "htc_htcmisc_impl.hpp"
#include "htc_observer.hpp"
namespace ams::htc::server {
class HtcServiceObject {
private:
static constexpr inline auto MaxSetElements = 0x48;
using Set = util::FixedSet<u32>;
private:
u8 m_set_memory[Set::GetRequiredMemorySize(MaxSetElements)];
Set m_set;
HtcmiscImpl m_misc_impl;
Observer m_observer;
os::SdkMutex m_mutex;
public:
HtcServiceObject(htclow::HtclowManager *htclow_manager);
public:
HtcmiscImpl *GetHtcmiscImpl();
public:
Result GetEnvironmentVariable(sf::Out<s32> out_size, const sf::OutBuffer &out, const sf::InBuffer &name);
Result GetEnvironmentVariableLength(sf::Out<s32> out_size, const sf::InBuffer &name);
Result GetHostConnectionEvent(sf::OutCopyHandle out);
Result GetHostDisconnectionEvent(sf::OutCopyHandle out);
Result GetHostConnectionEventForSystem(sf::OutCopyHandle out);
Result GetHostDisconnectionEventForSystem(sf::OutCopyHandle out);
Result GetBridgeIpAddress(const sf::OutBuffer &out);
Result GetBridgePort(const sf::OutBuffer &out);
Result SetCradleAttached(bool attached);
Result GetBridgeSubnetMask(const sf::OutBuffer &out);
Result GetBridgeMacAddress(const sf::OutBuffer &out);
Result GetWorkingDirectoryPath(const sf::OutBuffer &out, s32 max_len);
Result GetWorkingDirectoryPathSize(sf::Out<s32> out_size);
Result RunOnHostStart(sf::Out<u32> out_id, sf::OutCopyHandle out, const sf::InBuffer &args);
Result RunOnHostResults(sf::Out<s32> out_result, u32 id);
Result SetBridgeIpAddress(const sf::InBuffer &arg);
Result SetBridgeSubnetMask(const sf::InBuffer &arg);
Result SetBridgePort(const sf::InBuffer &arg);
};
static_assert(tma::IsIHtcManager<HtcServiceObject>);
}

View File

@@ -1,86 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_htc_service_object.hpp"
#include "htc_htcmisc_manager.hpp"
namespace ams::htc::server {
namespace {
static constexpr inline size_t NumServers = 1;
static constexpr inline size_t MaxSessions = 30;
static constexpr inline sm::ServiceName ServiceName = sm::ServiceName::Encode("htc");
using ServerOptions = sf::hipc::DefaultServerManagerOptions;
using ServerManager = sf::hipc::ServerManager<NumServers, ServerOptions, MaxSessions>;
constinit util::TypedStorage<ServerManager> g_server_manager_storage = {};
constinit ServerManager *g_server_manager = nullptr;
constinit HtcmiscImpl *g_misc_impl = nullptr;
}
void InitializeHtcmiscServer(htclow::HtclowManager *htclow_manager) {
/* Check that we haven't already initialized. */
AMS_ASSERT(g_server_manager == nullptr);
/* Create/Set the server manager pointer. */
g_server_manager = util::ConstructAt(g_server_manager_storage);
/* Create and register the htc manager object. */
HtcServiceObject *service_object;
R_ABORT_UNLESS(g_server_manager->RegisterObjectForServer(CreateHtcmiscManager(std::addressof(service_object), htclow_manager), ServiceName, MaxSessions));
/* Set the misc impl. */
g_misc_impl = service_object->GetHtcmiscImpl();
/* Start the server. */
g_server_manager->ResumeProcessing();
}
void FinalizeHtcmiscServer() {
/* Check that we've already initialized. */
AMS_ASSERT(g_server_manager != nullptr);
/* Clear the misc impl. */
g_misc_impl = nullptr;
/* Clear and destroy. */
std::destroy_at(g_server_manager);
g_server_manager = nullptr;
}
void LoopHtcmiscServer() {
/* Check that we've already initialized. */
AMS_ASSERT(g_server_manager != nullptr);
g_server_manager->LoopProcess();
}
void RequestStopHtcmiscServer() {
/* Check that we've already initialized. */
AMS_ASSERT(g_server_manager != nullptr);
g_server_manager->RequestStopProcessing();
}
HtcmiscImpl *GetHtcmiscImpl() {
return g_misc_impl;
}
}

View File

@@ -1,254 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_htcmisc_impl.hpp"
namespace ams::htc::server {
namespace {
alignas(os::ThreadStackAlignment) u8 g_client_thread_stack[os::MemoryPageSize];
alignas(os::ThreadStackAlignment) u8 g_server_thread_stack[os::MemoryPageSize];
}
HtcmiscImpl::HtcmiscImpl(htclow::HtclowManager *htclow_manager)
: m_htclow_driver(htclow_manager, htclow::ModuleId::Htcmisc),
m_driver_manager(std::addressof(m_htclow_driver)),
m_rpc_client(std::addressof(m_htclow_driver), HtcmiscClientChannelId),
m_rpc_server(std::addressof(m_htclow_driver), HtcmiscServerChannelId),
m_cancel_event(os::EventClearMode_ManualClear),
m_cancelled(false),
m_connection_event(os::EventClearMode_ManualClear),
m_client_connected(false),
m_server_connected(false),
m_connected(false),
m_connection_mutex()
{
/* Create the client thread. */
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_client_thread), ClientThreadEntry, this, g_client_thread_stack, sizeof(g_client_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(htc, Htcmisc)));
/* Set the client thread name. */
os::SetThreadNamePointer(std::addressof(m_client_thread), AMS_GET_SYSTEM_THREAD_NAME(htc, Htcmisc));
/* Start the client thread. */
os::StartThread(std::addressof(m_client_thread));
/* Create the server thread. */
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_server_thread), ServerThreadEntry, this, g_server_thread_stack, sizeof(g_server_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(htc, Htcmisc)));
/* Set the server thread name. */
os::SetThreadNamePointer(std::addressof(m_server_thread), AMS_GET_SYSTEM_THREAD_NAME(htc, Htcmisc));
/* Start the server thread. */
os::StartThread(std::addressof(m_server_thread));
}
HtcmiscImpl::~HtcmiscImpl() {
/* Cancel ourselves. */
this->Cancel();
/* Wait for our threads to be done, and destroy them. */
os::WaitThread(std::addressof(m_client_thread));
os::DestroyThread(std::addressof(m_client_thread));
os::WaitThread(std::addressof(m_server_thread));
os::DestroyThread(std::addressof(m_server_thread));
}
void HtcmiscImpl::Cancel() {
/* Set ourselves as cancelled. */
m_cancelled = true;
/* Signal our cancel event. */
m_cancel_event.Signal();
}
void HtcmiscImpl::WaitTask(u32 task_id) {
return m_rpc_client.Wait(task_id);
}
Result HtcmiscImpl::GetEnvironmentVariable(size_t *out_size, char *dst, size_t dst_size, const char *name, size_t name_size) {
/* Begin the task. */
u32 task_id{};
R_TRY(m_rpc_client.Begin<rpc::GetEnvironmentVariableTask>(std::addressof(task_id), name, name_size));
/* Wait for the task to complete. */
this->WaitTask(task_id);
/* Finish the task. */
R_TRY(m_rpc_client.End<rpc::GetEnvironmentVariableTask>(task_id, out_size, dst, dst_size));
R_SUCCEED();
}
Result HtcmiscImpl::GetEnvironmentVariableLength(size_t *out_size, const char *name, size_t name_size) {
/* Begin the task. */
u32 task_id{};
R_TRY(m_rpc_client.Begin<rpc::GetEnvironmentVariableLengthTask>(std::addressof(task_id), name, name_size));
/* Wait for the task to complete. */
this->WaitTask(task_id);
/* Finish the task. */
R_TRY(m_rpc_client.End<rpc::GetEnvironmentVariableLengthTask>(task_id, out_size));
R_SUCCEED();
}
Result HtcmiscImpl::RunOnHostBegin(u32 *out_task_id, os::NativeHandle *out_event, const char *args, size_t args_size) {
/* Begin the task. */
u32 task_id{};
R_TRY(m_rpc_client.Begin<rpc::RunOnHostTask>(std::addressof(task_id), args, args_size));
/* Detach the task. */
*out_task_id = task_id;
*out_event = m_rpc_client.DetachReadableHandle(task_id);
R_SUCCEED();
}
Result HtcmiscImpl::RunOnHostEnd(s32 *out_result, u32 task_id) {
/* Finish the task. */
s32 res;
R_TRY(m_rpc_client.End<rpc::RunOnHostTask>(task_id, std::addressof(res)));
/* Set output. */
*out_result = res;
R_SUCCEED();
}
void HtcmiscImpl::ClientThread() {
/* Loop so long as we're not cancelled. */
while (!m_cancelled) {
/* Open the rpc client. */
m_rpc_client.Open();
/* Ensure we close, if something goes wrong. */
auto client_guard = SCOPE_GUARD { m_rpc_client.Close(); };
/* Wait for the rpc client. */
if (m_rpc_client.WaitAny(htclow::ChannelState_Connectable, m_cancel_event.GetBase()) != 0) {
break;
}
/* Start the rpc client. */
if (R_FAILED(m_rpc_client.Start())) {
break;
}
/* We're connected! */
this->SetClientConnectionEvent(true);
client_guard.Cancel();
/* We're connected, so we want to cleanup when we're done. */
ON_SCOPE_EXIT {
m_rpc_client.Close();
m_rpc_client.Cancel();
m_rpc_client.Wait();
this->SetClientConnectionEvent(false);
};
/* Wait to become disconnected. */
if (m_rpc_client.WaitAny(htclow::ChannelState_Disconnected, m_cancel_event.GetBase()) != 0) {
break;
}
}
}
void HtcmiscImpl::ServerThread() {
/* Loop so long as we're not cancelled. */
while (!m_cancelled) {
/* Open the rpc server. */
m_rpc_server.Open();
/* Ensure we close, if something goes wrong. */
auto server_guard = SCOPE_GUARD { m_rpc_server.Close(); };
/* Wait for the rpc server. */
if (m_rpc_server.WaitAny(htclow::ChannelState_Connectable, m_cancel_event.GetBase()) != 0) {
break;
}
/* Start the rpc server. */
if (R_FAILED(m_rpc_server.Start())) {
break;
}
/* We're connected! */
this->SetServerConnectionEvent(true);
server_guard.Cancel();
/* We're connected, so we want to cleanup when we're done. */
ON_SCOPE_EXIT {
m_rpc_server.Close();
m_rpc_server.Cancel();
m_rpc_server.Wait();
this->SetServerConnectionEvent(false);
};
/* Wait to become disconnected. */
if (m_rpc_server.WaitAny(htclow::ChannelState_Disconnected, m_cancel_event.GetBase()) != 0) {
break;
}
}
}
void HtcmiscImpl::SetClientConnectionEvent(bool en) {
/* Lock ourselves. */
std::scoped_lock lk(m_connection_mutex);
/* Update our state. */
if (m_client_connected != en) {
m_client_connected = en;
this->UpdateConnectionEvent();
}
}
void HtcmiscImpl::SetServerConnectionEvent(bool en) {
/* Lock ourselves. */
std::scoped_lock lk(m_connection_mutex);
/* Update our state. */
if (m_server_connected != en) {
m_server_connected = en;
this->UpdateConnectionEvent();
}
}
void HtcmiscImpl::UpdateConnectionEvent() {
/* Determine if we're connected. */
const bool connected = m_client_connected && m_server_connected;
/* Update our state. */
if (m_connected != connected) {
m_connected = connected;
m_connection_event.Signal();
}
}
os::EventType *HtcmiscImpl::GetConnectionEvent() const {
return m_connection_event.GetBase();
}
bool HtcmiscImpl::IsConnected() const {
/* Lock ourselves. */
std::scoped_lock lk(m_connection_mutex);
return m_connected;
}
}

View File

@@ -1,70 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "../../htclow/htclow_manager.hpp"
#include "driver/htc_htclow_driver.hpp"
#include "driver/htc_driver_manager.hpp"
#include "rpc/htc_rpc_client.hpp"
#include "rpc/htc_htcmisc_rpc_server.hpp"
namespace ams::htc::server {
class HtcmiscImpl {
private:
driver::HtclowDriver m_htclow_driver;
driver::DriverManager m_driver_manager;
rpc::RpcClient m_rpc_client;
rpc::HtcmiscRpcServer m_rpc_server;
os::ThreadType m_client_thread;
os::ThreadType m_server_thread;
os::Event m_cancel_event;
bool m_cancelled;
mutable os::Event m_connection_event;
bool m_client_connected;
bool m_server_connected;
bool m_connected;
mutable os::SdkMutex m_connection_mutex;
private:
static void ClientThreadEntry(void *arg) { static_cast<HtcmiscImpl *>(arg)->ClientThread(); }
static void ServerThreadEntry(void *arg) { static_cast<HtcmiscImpl *>(arg)->ServerThread(); }
void ClientThread();
void ServerThread();
public:
HtcmiscImpl(htclow::HtclowManager *htclow_manager);
~HtcmiscImpl();
os::EventType *GetConnectionEvent() const;
bool IsConnected() const;
private:
void SetClientConnectionEvent(bool en);
void SetServerConnectionEvent(bool en);
void UpdateConnectionEvent();
void WaitTask(u32 task_id);
public:
void Cancel();
Result GetEnvironmentVariable(size_t *out_size, char *dst, size_t dst_size, const char *name, size_t name_size);
Result GetEnvironmentVariableLength(size_t *out_size, const char *name, size_t name_size);
Result RunOnHostBegin(u32 *out_task_id, os::NativeHandle *out_event, const char *args, size_t args_size);
Result RunOnHostEnd(s32 *out_result, u32 task_id);
};
}

View File

@@ -1,46 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_htcmisc_manager.hpp"
namespace ams::htc::server {
namespace {
mem::StandardAllocator g_allocator;
sf::StandardAllocatorMemoryResource g_sf_resource(std::addressof(g_allocator));
alignas(os::MemoryPageSize) constinit u8 g_heap[util::AlignUp(sizeof(HtcServiceObject), os::MemoryPageSize) + 12_KB];
class StaticAllocatorInitializer {
public:
StaticAllocatorInitializer() {
g_allocator.Initialize(g_heap, sizeof(g_heap));
}
} g_static_allocator_initializer;
using ObjectFactory = sf::ObjectFactory<sf::MemoryResourceAllocationPolicy>;
}
sf::SharedPointer<tma::IHtcManager> CreateHtcmiscManager(HtcServiceObject **out, htclow::HtclowManager *htclow_manager) {
auto obj = ObjectFactory::CreateSharedEmplaced<tma::IHtcManager, HtcServiceObject>(std::addressof(g_sf_resource), htclow_manager);
*out = std::addressof(obj.GetImpl());
return obj;
}
}

View File

@@ -1,24 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "htc_htc_service_object.hpp"
namespace ams::htc::server {
sf::SharedPointer<tma::IHtcManager> CreateHtcmiscManager(HtcServiceObject **out, htclow::HtclowManager *htclow_manager);
}

View File

@@ -1,120 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_observer.hpp"
#include "../../htcs/impl/htcs_manager.hpp"
namespace ams::htc::server {
Observer::Observer(const HtcmiscImpl &misc_impl)
: m_connect_event(os::EventClearMode_ManualClear, true),
m_disconnect_event(os::EventClearMode_ManualClear, true),
m_stop_event(os::EventClearMode_ManualClear),
m_misc_impl(misc_impl),
m_thread_running(false),
m_stopped(false),
m_connected(false),
m_is_service_available(false)
{
/* Initialize htcs library. */
htcs::impl::HtcsManagerHolder::AddReference();
/* Update our event state. */
this->UpdateEvent();
/* Start. */
R_ABORT_UNLESS(this->Start());
}
Result Observer::Start() {
/* Check that we're not already running. */
AMS_ASSERT(!m_thread_running);
/* Create the thread. */
R_TRY(os::CreateThread(std::addressof(m_observer_thread), ObserverThreadEntry, this, m_observer_thread_stack, sizeof(m_observer_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(htc, HtcObserver)));
/* Set the thread name pointer. */
os::SetThreadNamePointer(std::addressof(m_observer_thread), AMS_GET_SYSTEM_THREAD_NAME(htc, HtcObserver));
/* Mark our thread as running. */
m_thread_running = true;
m_stopped = false;
/* Start our thread. */
os::StartThread(std::addressof(m_observer_thread));
R_SUCCEED();
}
void Observer::UpdateEvent() {
if (m_connected && m_is_service_available) {
m_disconnect_event.Clear();
m_connect_event.Signal();
} else {
m_connect_event.Clear();
m_disconnect_event.Signal();
}
}
void Observer::ObserverThreadBody() {
/* When we're done observing, clear our state. */
ON_SCOPE_EXIT {
m_connected = false;
m_is_service_available = false;
this->UpdateEvent();
};
/* Get the htcs manager. */
auto * const htcs_manager = htcs::impl::HtcsManagerHolder::GetHtcsManager();
/* Get the events we're waiting on. */
os::EventType * const stop_event = m_stop_event.GetBase();
os::EventType * const conn_event = m_misc_impl.GetConnectionEvent();
os::EventType * const htcs_event = htcs_manager->GetServiceAvailabilityEvent();
/* Loop until we're asked to stop. */
while (!m_stopped) {
/* Wait for an event to be signaled. */
const auto index = os::WaitAny(stop_event, conn_event /*, htcs_event */);
switch (index) {
case 0:
/* Stop event, just break out of the loop. */
os::ClearEvent(stop_event);
break;
case 1:
/* Connection event, update our connection status. */
os::ClearEvent(conn_event);
m_connected = m_misc_impl.IsConnected();
break;
case 2:
/* Htcs event, update our service status. */
os::ClearEvent(htcs_event);
m_is_service_available = htcs_manager->IsServiceAvailable();
break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
/* If the event was our stop event, break. */
if (index == 0) {
break;
}
/* Update event status. */
this->UpdateEvent();
}
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "htc_htcmisc_impl.hpp"
namespace ams::htc::server {
class Observer {
private:
os::SystemEvent m_connect_event;
os::SystemEvent m_disconnect_event;
os::Event m_stop_event;
os::ThreadType m_observer_thread;
const HtcmiscImpl &m_misc_impl;
bool m_thread_running;
bool m_stopped;
bool m_connected;
bool m_is_service_available;
alignas(os::ThreadStackAlignment) u8 m_observer_thread_stack[os::MemoryPageSize];
public:
Observer(const HtcmiscImpl &misc_impl);
private:
static void ObserverThreadEntry(void *arg) { static_cast<Observer *>(arg)->ObserverThreadBody(); }
void ObserverThreadBody();
private:
Result Start();
void UpdateEvent();
public:
os::SystemEvent *GetConnectEvent() { return std::addressof(m_connect_event); }
os::SystemEvent *GetDisconnectEvent() { return std::addressof(m_disconnect_event); }
};
}

View File

@@ -1,110 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "../../htclow/htclow_manager.hpp"
namespace ams::htc::server {
namespace {
constinit htclow::HtclowManager *g_htclow_manager = nullptr;
constexpr const psc::PmModuleId PscModuleDependencies[] = { psc::PmModuleId_Pcie, psc::PmModuleId_Usb };
psc::PmModule g_pm_module;
constinit bool g_is_asleep = false;
constinit bool g_is_suspended = false;
}
void InitializePowerStateMonitor(htclow::impl::DriverType driver_type, htclow::HtclowManager *htclow_manager) {
AMS_UNUSED(driver_type);
/* Set the htclow manager. */
g_htclow_manager = htclow_manager;
/* Initialize pm module. */
R_ABORT_UNLESS(g_pm_module.Initialize(psc::PmModuleId_TmaHostIo, PscModuleDependencies, util::size(PscModuleDependencies), os::EventClearMode_AutoClear));
/* We're neither asleep nor suspended. */
g_is_asleep = false;
g_is_suspended = false;
}
void FinalizePowerStateMonitor() {
R_ABORT_UNLESS(g_pm_module.Finalize());
}
void LoopMonitorPowerState() {
/* Get the psc module's event pointer. */
auto *event = g_pm_module.GetEventPointer();
while (true) {
/* Wait for a new power state event. */
event->Wait();
/* Get the power state. */
psc::PmState pm_state;
psc::PmFlagSet pm_flags;
R_ABORT_UNLESS(g_pm_module.GetRequest(std::addressof(pm_state), std::addressof(pm_flags)));
/* Update sleeping state. */
switch (pm_state) {
case psc::PmState_FullAwake:
if (g_is_asleep) {
g_htclow_manager->NotifyAwake();
g_is_asleep = false;
}
break;
case psc::PmState_MinimumAwake:
case psc::PmState_SleepReady:
case psc::PmState_EssentialServicesSleepReady:
case psc::PmState_EssentialServicesAwake:
if (!g_is_asleep) {
g_htclow_manager->NotifyAsleep();
g_is_asleep = true;
}
break;
default:
break;
}
/* Update suspend state. */
switch (pm_state) {
case psc::PmState_FullAwake:
case psc::PmState_MinimumAwake:
if (g_is_suspended) {
g_htclow_manager->Resume();
g_is_suspended = false;
}
break;
case psc::PmState_SleepReady:
case psc::PmState_EssentialServicesSleepReady:
case psc::PmState_EssentialServicesAwake:
if (!g_is_suspended) {
g_htclow_manager->Suspend();
g_is_suspended = true;
}
break;
default:
break;
}
/* Acknowledge the pm request. */
R_ABORT_UNLESS(g_pm_module.Acknowledge(pm_state, ResultSuccess()));
}
}
}

View File

@@ -1,178 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_htcmisc_rpc_server.hpp"
namespace ams::htc::server::rpc {
namespace {
constexpr inline size_t ReceiveThreadStackSize = os::MemoryPageSize;
alignas(os::ThreadStackAlignment) constinit u8 g_receive_thread_stack[ReceiveThreadStackSize];
}
HtcmiscRpcServer::HtcmiscRpcServer(driver::IDriver *driver, htclow::ChannelId channel)
: m_allocator(nullptr),
m_driver(driver),
m_channel_id(channel),
m_receive_thread_stack(g_receive_thread_stack),
m_cancelled(false),
m_thread_running(false)
{
/* ... */
}
void HtcmiscRpcServer::Open() {
R_ABORT_UNLESS(m_driver->Open(m_channel_id, m_driver_receive_buffer, sizeof(m_driver_receive_buffer), m_driver_send_buffer, sizeof(m_driver_send_buffer)));
}
void HtcmiscRpcServer::Close() {
m_driver->Close(m_channel_id);
}
Result HtcmiscRpcServer::Start() {
/* Connect. */
R_TRY(m_driver->Connect(m_channel_id));
/* Create our thread. */
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_receive_thread), ReceiveThreadEntry, this, m_receive_thread_stack, ReceiveThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(htc, HtcmiscReceive)));
/* Set thread name pointer. */
os::SetThreadNamePointer(std::addressof(m_receive_thread), AMS_GET_SYSTEM_THREAD_NAME(htc, HtcmiscReceive));
/* Start thread. */
os::StartThread(std::addressof(m_receive_thread));
/* Set initial state. */
m_cancelled = false;
m_thread_running = true;
R_SUCCEED();
}
void HtcmiscRpcServer::Cancel() {
/* Set cancelled. */
m_cancelled = true;
}
void HtcmiscRpcServer::Wait() {
/* Wait for thread to not be running. */
if (m_thread_running) {
os::WaitThread(std::addressof(m_receive_thread));
os::DestroyThread(std::addressof(m_receive_thread));
}
m_thread_running = false;
}
int HtcmiscRpcServer::WaitAny(htclow::ChannelState state, os::EventType *event) {
/* Check if we're already signaled. */
if (os::TryWaitEvent(event)) {
return 1;
}
/* Wait. */
while (m_driver->GetChannelState(m_channel_id) != state) {
const auto idx = os::WaitAny(m_driver->GetChannelStateEvent(m_channel_id), event);
if (idx != 0) {
return idx;
}
/* Clear the channel state event. */
os::ClearEvent(m_driver->GetChannelStateEvent(m_channel_id));
}
return 0;
}
Result HtcmiscRpcServer::ReceiveThread() {
/* Loop forever. */
auto *header = reinterpret_cast<HtcmiscRpcPacket *>(m_receive_buffer);
while (true) {
/* Try to receive a packet header. */
R_TRY(this->ReceiveHeader(header));
/* Track how much we've received. */
size_t received = sizeof(*header);
/* If the packet has one, receive its body. */
if (header->body_size > 0) {
/* Sanity check the body size. */
AMS_ABORT_UNLESS(util::IsIntValueRepresentable<size_t>(header->body_size));
AMS_ABORT_UNLESS(static_cast<size_t>(header->body_size) <= sizeof(m_receive_buffer) - received);
/* Receive the body. */
R_TRY(this->ReceiveBody(header->data, header->body_size));
/* Note that we received the body. */
received += header->body_size;
}
/* Check that the packet is a request packet. */
R_UNLESS(header->category == HtcmiscPacketCategory::Request, htc::ResultInvalidCategory());
/* Handle specific requests. */
if (header->type == HtcmiscPacketType::SetTargetName) {
R_TRY(this->ProcessSetTargetNameRequest(header->data, header->body_size, header->task_id));
}
}
}
Result HtcmiscRpcServer::ProcessSetTargetNameRequest(const char *name, size_t size, u32 task_id) {
/* TODO: we need to use settings::fwdbg::SetSettingsItemValue here, but this will require ams support for set:fd re-enable? */
/* Needs some thought. */
AMS_UNUSED(name, size, task_id);
AMS_ABORT("HtcmiscRpcServer::ProcessSetTargetNameRequest");
}
Result HtcmiscRpcServer::ReceiveHeader(HtcmiscRpcPacket *header) {
/* Receive. */
s64 received;
R_TRY(m_driver->Receive(std::addressof(received), reinterpret_cast<char *>(header), sizeof(*header), m_channel_id, htclow::ReceiveOption_ReceiveAllData));
/* Check size. */
R_UNLESS(static_cast<size_t>(received) == sizeof(*header), htc::ResultInvalidSize());
R_SUCCEED();
}
Result HtcmiscRpcServer::ReceiveBody(char *dst, size_t size) {
/* Receive. */
s64 received;
R_TRY(m_driver->Receive(std::addressof(received), dst, size, m_channel_id, htclow::ReceiveOption_ReceiveAllData));
/* Check size. */
R_UNLESS(static_cast<size_t>(received) == size, htc::ResultInvalidSize());
R_SUCCEED();
}
Result HtcmiscRpcServer::SendRequest(const char *src, size_t size) {
/* Sanity check our size. */
AMS_ASSERT(util::IsIntValueRepresentable<s64>(size));
/* Send the data. */
s64 sent;
R_TRY(m_driver->Send(std::addressof(sent), src, static_cast<s64>(size), m_channel_id));
/* Check that we sent the right amount. */
R_UNLESS(sent == static_cast<s64>(size), htc::ResultInvalidSize());
R_SUCCEED();
}
}

View File

@@ -1,62 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "../driver/htc_i_driver.hpp"
#include "htc_htcmisc_rpc_tasks.hpp"
namespace ams::htc::server::rpc {
class HtcmiscRpcServer {
private:
/* TODO: where is this value coming from, again? */
static constexpr size_t BufferSize = 1_KB;
private:
mem::StandardAllocator *m_allocator;
driver::IDriver *m_driver;
htclow::ChannelId m_channel_id;
void *m_receive_thread_stack;
os::ThreadType m_receive_thread;
bool m_cancelled;
bool m_thread_running;
char m_receive_buffer[BufferSize];
char m_send_buffer[BufferSize];
u8 m_driver_receive_buffer[4_KB];
u8 m_driver_send_buffer[4_KB];
private:
static void ReceiveThreadEntry(void *arg) { static_cast<HtcmiscRpcServer *>(arg)->ReceiveThread(); }
Result ReceiveThread();
public:
HtcmiscRpcServer(driver::IDriver *driver, htclow::ChannelId channel);
public:
void Open();
void Close();
Result Start();
void Cancel();
void Wait();
int WaitAny(htclow::ChannelState state, os::EventType *event);
private:
Result ProcessSetTargetNameRequest(const char *name, size_t size, u32 task_id);
Result ReceiveHeader(HtcmiscRpcPacket *header);
Result ReceiveBody(char *dst, size_t size);
Result SendRequest(const char *src, size_t size);
};
}

View File

@@ -1,300 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_htcmisc_rpc_tasks.hpp"
namespace ams::htc::server::rpc {
Result GetEnvironmentVariableTask::SetArguments(const char *args, size_t size) {
/* Copy to our name. */
const size_t copied = util::Strlcpy(m_name, args, sizeof(m_name));
m_name_size = copied;
/* Require that the size be correct. */
R_UNLESS(size == copied || size == copied + 1, htc::ResultUnknown());
R_SUCCEED();
}
void GetEnvironmentVariableTask::Complete(HtcmiscResult result, const char *data, size_t size) {
/* Sanity check input. */
if (size < sizeof(m_value)) {
/* Convert the result. */
switch (result) {
case HtcmiscResult::Success:
/* Copy to our value. */
std::memcpy(m_value, data, size);
m_value[size] = '\x00';
m_value_size = size + 1;
m_result = ResultSuccess();
break;
case HtcmiscResult::UnknownError:
m_result = htc::ResultUnknown();
break;
case HtcmiscResult::UnsupportedVersion:
m_result = htc::ResultConnectionFailure();
break;
case HtcmiscResult::InvalidRequest:
m_result = htc::ResultNotFound();
break;
}
} else {
m_result = htc::ResultUnknown();
}
/* Complete the task. */
Task::Complete();
}
Result GetEnvironmentVariableTask::GetResult(size_t *out, char *dst, size_t size) const {
/* Check our task state. */
AMS_ASSERT(this->GetTaskState() == RpcTaskState::Completed);
/* Check that we succeeded. */
R_TRY(m_result);
/* Check that we can convert successfully. */
R_UNLESS(util::IsIntValueRepresentable<int>(size), htc::ResultUnknown());
/* Copy out. */
const auto copied = util::Strlcpy(dst, m_value, size);
R_UNLESS(copied < static_cast<int>(size), htc::ResultNotEnoughBuffer());
/* Set the output size. */
*out = m_value_size;
R_SUCCEED();
}
Result GetEnvironmentVariableTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
/* Validate pre-conditions. */
AMS_ASSERT(size >= sizeof(HtcmiscRpcPacket));
AMS_UNUSED(size);
/* Create the packet. */
auto *packet = reinterpret_cast<HtcmiscRpcPacket *>(data);
*packet = {
.protocol = HtcmiscProtocol,
.version = HtcmiscMaxVersion,
.category = HtcmiscPacketCategory::Request,
.type = HtcmiscPacketType::GetEnvironmentVariable,
.body_size = this->GetNameSize(),
.task_id = task_id,
.params = {
/* ... */
},
};
/* Set the packet body. */
std::memcpy(packet->data, this->GetName(), this->GetNameSize());
/* Set the output size. */
*out = sizeof(*packet) + this->GetNameSize();
R_SUCCEED();
}
Result GetEnvironmentVariableTask::ProcessResponse(const char *data, size_t size) {
/* Convert the input to a packet. */
auto *packet = reinterpret_cast<const HtcmiscRpcPacket *>(data);
/* Process the packet. */
this->Complete(static_cast<HtcmiscResult>(packet->params[0]), data + sizeof(*packet), size - sizeof(*packet));
/* Complete the task. */
Task::Complete();
R_SUCCEED();
}
Result GetEnvironmentVariableLengthTask::SetArguments(const char *args, size_t size) {
/* Copy to our name. */
const size_t copied = util::Strlcpy(m_name, args, sizeof(m_name));
m_name_size = copied;
/* Require that the size be correct. */
R_UNLESS(size == copied || size == copied + 1, htc::ResultUnknown());
R_SUCCEED();
}
void GetEnvironmentVariableLengthTask::Complete(HtcmiscResult result, const char *data, size_t size) {
/* Sanity check input. */
if (size == sizeof(s64)) {
/* Convert the result. */
switch (result) {
case HtcmiscResult::Success:
/* Copy to our value. */
s64 tmp;
std::memcpy(std::addressof(tmp), data, sizeof(tmp));
if (util::IsIntValueRepresentable<size_t>(tmp)) {
m_value_size = static_cast<size_t>(tmp);
}
m_result = ResultSuccess();
break;
case HtcmiscResult::UnknownError:
m_result = htc::ResultUnknown();
break;
case HtcmiscResult::UnsupportedVersion:
m_result = htc::ResultConnectionFailure();
break;
case HtcmiscResult::InvalidRequest:
m_result = htc::ResultNotFound();
break;
}
} else {
m_result = htc::ResultUnknown();
}
/* Complete the task. */
Task::Complete();
}
Result GetEnvironmentVariableLengthTask::GetResult(size_t *out) const {
/* Check our task state. */
AMS_ASSERT(this->GetTaskState() == RpcTaskState::Completed);
/* Check that we succeeded. */
R_TRY(m_result);
/* Set the output size. */
*out = m_value_size;
R_SUCCEED();
}
Result GetEnvironmentVariableLengthTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
/* Validate pre-conditions. */
AMS_ASSERT(size >= sizeof(HtcmiscRpcPacket));
AMS_UNUSED(size);
/* Create the packet. */
auto *packet = reinterpret_cast<HtcmiscRpcPacket *>(data);
*packet = {
.protocol = HtcmiscProtocol,
.version = HtcmiscMaxVersion,
.category = HtcmiscPacketCategory::Request,
.type = HtcmiscPacketType::GetEnvironmentVariableLength,
.body_size = this->GetNameSize(),
.task_id = task_id,
.params = {
/* ... */
},
};
/* Set the packet body. */
std::memcpy(packet->data, this->GetName(), this->GetNameSize());
/* Set the output size. */
*out = sizeof(*packet) + this->GetNameSize();
R_SUCCEED();
}
Result GetEnvironmentVariableLengthTask::ProcessResponse(const char *data, size_t size) {
/* Convert the input to a packet. */
auto *packet = reinterpret_cast<const HtcmiscRpcPacket *>(data);
/* Process the packet. */
this->Complete(static_cast<HtcmiscResult>(packet->params[0]), data + sizeof(*packet), size - sizeof(*packet));
/* Complete the task. */
Task::Complete();
R_SUCCEED();
}
Result RunOnHostTask::SetArguments(const char *args, size_t size) {
/* Verify command fits in our buffer. */
R_UNLESS(size < sizeof(m_command), htc::ResultNotEnoughBuffer());
/* Set our command. */
std::memcpy(m_command, args, size);
m_command_size = size;
R_SUCCEED();
}
void RunOnHostTask::Complete(int host_result) {
/* Set our host result. */
m_host_result = host_result;
/* Signal. */
m_system_event.Signal();
/* Complete the task. */
Task::Complete();
}
Result RunOnHostTask::GetResult(int *out) const {
*out = m_host_result;
R_SUCCEED();
}
void RunOnHostTask::Cancel(RpcTaskCancelReason reason) {
/* Cancel the task. */
Task::Cancel(reason);
/* Signal our event. */
m_system_event.Signal();
}
Result RunOnHostTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
/* Validate pre-conditions. */
AMS_ASSERT(size >= sizeof(HtcmiscRpcPacket));
AMS_UNUSED(size);
/* Create the packet. */
auto *packet = reinterpret_cast<HtcmiscRpcPacket *>(data);
*packet = {
.protocol = HtcmiscProtocol,
.version = HtcmiscMaxVersion,
.category = HtcmiscPacketCategory::Request,
.type = HtcmiscPacketType::RunOnHost,
.body_size = this->GetCommandSize(),
.task_id = task_id,
.params = {
/* ... */
},
};
/* Set the packet body. */
std::memcpy(packet->data, this->GetCommand(), this->GetCommandSize());
/* Set the output size. */
*out = sizeof(*packet) + this->GetCommandSize();
R_SUCCEED();
}
Result RunOnHostTask::ProcessResponse(const char *data, size_t size) {
/* Validate pre-conditions. */
AMS_ASSERT(size >= sizeof(HtcmiscRpcPacket));
AMS_UNUSED(size);
this->Complete(reinterpret_cast<const HtcmiscRpcPacket *>(data)->params[0]);
R_SUCCEED();
}
os::SystemEventType *RunOnHostTask::GetSystemEvent() {
return m_system_event.GetBase();
}
}

View File

@@ -1,147 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "htc_rpc_tasks.hpp"
namespace ams::htc::server::rpc {
enum class HtcmiscTaskType {
GetEnvironmentVariable = 0,
GetEnvironmentVariableLength = 1,
SetTargetStatus = 2,
RunOnHost = 3,
};
enum class HtcmiscResult {
Success = 0,
UnknownError = 1,
UnsupportedVersion = 2,
InvalidRequest = 3,
};
enum class HtcmiscPacketCategory : s16 {
Request = 0,
Response = 1,
};
enum class HtcmiscPacketType : s16 {
GetMaxProtocolVersion = 0,
SetProtocolVersion = 1,
GetEnvironmentVariable = 16,
GetEnvironmentVariableLength = 17,
SetTargetStatus = 18,
RunOnHost = 19,
GetWorkingDirectory = 20,
GetWorkingDirectorySize = 21,
SetTargetName = 22,
};
constexpr inline s16 HtcmiscProtocol = 4;
constexpr inline s16 HtcmiscMaxVersion = 2;
struct HtcmiscRpcPacket {
s16 protocol;
s16 version;
HtcmiscPacketCategory category;
HtcmiscPacketType type;
s64 body_size;
u32 task_id{};
u64 params[5];
char data[];
};
static_assert(sizeof(HtcmiscRpcPacket) == 0x40);
class HtcmiscTask : public Task {
private:
HtcmiscTaskType m_task_type;
public:
HtcmiscTask(HtcmiscTaskType type) : m_task_type(type) { /* ... */ }
HtcmiscTaskType GetTaskType() const { return m_task_type; }
};
class GetEnvironmentVariableTask : public HtcmiscTask {
public:
static constexpr inline HtcmiscTaskType TaskType = HtcmiscTaskType::GetEnvironmentVariable;
private:
char m_name[0x800];
int m_name_size;
Result m_result;
size_t m_value_size;
char m_value[0x8000];
public:
GetEnvironmentVariableTask() : HtcmiscTask(HtcmiscTaskType::GetEnvironmentVariable) { /* ... */ }
Result SetArguments(const char *args, size_t size);
void Complete(HtcmiscResult result, const char *data, size_t size);
Result GetResult(size_t *out, char *dst, size_t size) const;
const char *GetName() const { return m_name; }
int GetNameSize() const { return m_name_size; }
public:
virtual Result ProcessResponse(const char *data, size_t size) override;
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
};
class GetEnvironmentVariableLengthTask : public HtcmiscTask {
public:
static constexpr inline HtcmiscTaskType TaskType = HtcmiscTaskType::GetEnvironmentVariableLength;
private:
char m_name[0x800];
int m_name_size;
Result m_result;
size_t m_value_size;
public:
GetEnvironmentVariableLengthTask() : HtcmiscTask(HtcmiscTaskType::GetEnvironmentVariableLength) { /* ... */ }
Result SetArguments(const char *args, size_t size);
void Complete(HtcmiscResult result, const char *data, size_t size);
Result GetResult(size_t *out) const;
const char *GetName() const { return m_name; }
int GetNameSize() const { return m_name_size; }
public:
virtual Result ProcessResponse(const char *data, size_t size) override;
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
};
class RunOnHostTask : public HtcmiscTask {
public:
static constexpr inline HtcmiscTaskType TaskType = HtcmiscTaskType::RunOnHost;
private:
char m_command[0x2000];
int m_command_size;
Result m_result;
int m_host_result;
os::SystemEvent m_system_event;
public:
RunOnHostTask() : HtcmiscTask(HtcmiscTaskType::RunOnHost), m_system_event(os::EventClearMode_ManualClear, true) { /* ... */ }
Result SetArguments(const char *args, size_t size);
void Complete(int host_result);
Result GetResult(int *out) const;
const char *GetCommand() const { return m_command; }
int GetCommandSize() const { return m_command_size; }
public:
virtual void Cancel(RpcTaskCancelReason reason) override;
virtual Result ProcessResponse(const char *data, size_t size) override;
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
virtual os::SystemEventType *GetSystemEvent() override;
};
}

View File

@@ -1,472 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_rpc_client.hpp"
namespace ams::htc::server::rpc {
namespace {
constexpr inline size_t ThreadStackSize = os::MemoryPageSize;
alignas(os::ThreadStackAlignment) constinit u8 g_receive_thread_stack[ThreadStackSize];
alignas(os::ThreadStackAlignment) constinit u8 g_send_thread_stack[ThreadStackSize];
constinit os::SdkMutex g_rpc_mutex;
constinit RpcTaskIdFreeList g_task_id_free_list;
constinit RpcTaskTable g_task_table;
}
RpcClient::RpcClient(driver::IDriver *driver, htclow::ChannelId channel)
: m_allocator(nullptr),
m_driver(driver),
m_channel_id(channel),
m_receive_thread_stack(g_receive_thread_stack),
m_send_thread_stack(g_send_thread_stack),
m_mutex(g_rpc_mutex),
m_task_id_free_list(g_task_id_free_list),
m_task_table(g_task_table),
m_task_active(),
m_is_htcs_task(),
m_task_queue(),
m_cancelled(false),
m_thread_running(false)
{
/* Initialize all events. */
for (size_t i = 0; i < MaxRpcCount; ++i) {
os::InitializeEvent(std::addressof(m_receive_buffer_available_events[i]), false, os::EventClearMode_AutoClear);
os::InitializeEvent(std::addressof(m_send_buffer_available_events[i]), false, os::EventClearMode_AutoClear);
}
}
RpcClient::RpcClient(mem::StandardAllocator *allocator, driver::IDriver *driver, htclow::ChannelId channel)
: m_allocator(allocator),
m_driver(driver),
m_channel_id(channel),
m_receive_thread_stack(m_allocator->Allocate(ThreadStackSize, os::ThreadStackAlignment)),
m_send_thread_stack(m_allocator->Allocate(ThreadStackSize, os::ThreadStackAlignment)),
m_mutex(g_rpc_mutex),
m_task_id_free_list(g_task_id_free_list),
m_task_table(g_task_table),
m_task_active(),
m_is_htcs_task(),
m_task_queue(),
m_cancelled(false),
m_thread_running(false)
{
/* Initialize all events. */
for (size_t i = 0; i < MaxRpcCount; ++i) {
os::InitializeEvent(std::addressof(m_receive_buffer_available_events[i]), false, os::EventClearMode_AutoClear);
os::InitializeEvent(std::addressof(m_send_buffer_available_events[i]), false, os::EventClearMode_AutoClear);
}
}
RpcClient::~RpcClient() {
/* Finalize all events. */
for (size_t i = 0; i < MaxRpcCount; ++i) {
os::FinalizeEvent(std::addressof(m_receive_buffer_available_events[i]));
os::FinalizeEvent(std::addressof(m_send_buffer_available_events[i]));
}
/* Free the thread stacks. */
if (m_allocator != nullptr) {
m_allocator->Free(m_receive_thread_stack);
m_allocator->Free(m_send_thread_stack);
}
m_receive_thread_stack = nullptr;
m_send_thread_stack = nullptr;
/* Free all tasks. */
for (u32 i = 0; i < MaxRpcCount; ++i) {
if (m_task_active[i]) {
std::scoped_lock lk(m_mutex);
m_task_table.Delete(i);
m_task_id_free_list.Free(i);
}
}
}
void RpcClient::Open() {
R_ABORT_UNLESS(m_driver->Open(m_channel_id));
}
void RpcClient::Close() {
m_driver->Close(m_channel_id);
}
Result RpcClient::Start() {
/* Connect. */
R_TRY(m_driver->Connect(m_channel_id));
/* Initialize our task queue. */
m_task_queue.Initialize();
/* Create our threads. */
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_receive_thread), ReceiveThreadEntry, this, m_receive_thread_stack, ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(htc, HtcmiscReceive)));
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_send_thread), SendThreadEntry, this, m_send_thread_stack, ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(htc, HtcmiscSend)));
/* Set thread name pointers. */
os::SetThreadNamePointer(std::addressof(m_receive_thread), AMS_GET_SYSTEM_THREAD_NAME(htc, HtcmiscReceive));
os::SetThreadNamePointer(std::addressof(m_send_thread), AMS_GET_SYSTEM_THREAD_NAME(htc, HtcmiscSend));
/* Start threads. */
os::StartThread(std::addressof(m_receive_thread));
os::StartThread(std::addressof(m_send_thread));
/* Set initial state. */
m_cancelled = false;
m_thread_running = true;
/* Clear events. */
for (size_t i = 0; i < MaxRpcCount; ++i) {
os::ClearEvent(std::addressof(m_receive_buffer_available_events[i]));
os::ClearEvent(std::addressof(m_send_buffer_available_events[i]));
}
R_SUCCEED();
}
void RpcClient::Cancel() {
/* Set cancelled. */
m_cancelled = true;
/* Signal all events. */
for (size_t i = 0; i < MaxRpcCount; ++i) {
os::SignalEvent(std::addressof(m_receive_buffer_available_events[i]));
os::SignalEvent(std::addressof(m_send_buffer_available_events[i]));
}
/* Cancel our queue. */
m_task_queue.Cancel();
}
void RpcClient::Wait() {
/* Wait for thread to not be running. */
if (m_thread_running) {
os::WaitThread(std::addressof(m_receive_thread));
os::WaitThread(std::addressof(m_send_thread));
os::DestroyThread(std::addressof(m_receive_thread));
os::DestroyThread(std::addressof(m_send_thread));
}
m_thread_running = false;
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Finalize the task queue. */
m_task_queue.Finalize();
/* Cancel all tasks. */
for (size_t i = 0; i < MaxRpcCount; ++i) {
if (m_task_active[i]) {
m_task_table.Get<Task>(i)->Cancel(RpcTaskCancelReason::ClientFinalized);
}
}
}
int RpcClient::WaitAny(htclow::ChannelState state, os::EventType *event) {
/* Check if we're already signaled. */
if (os::TryWaitEvent(event)) {
return 1;
}
/* Wait. */
while (m_driver->GetChannelState(m_channel_id) != state) {
const auto idx = os::WaitAny(m_driver->GetChannelStateEvent(m_channel_id), event);
if (idx != 0) {
return idx;
}
/* Clear the channel state event. */
os::ClearEvent(m_driver->GetChannelStateEvent(m_channel_id));
}
return 0;
}
Result RpcClient::ReceiveThread() {
/* Loop forever. */
auto *header = reinterpret_cast<RpcPacket *>(m_receive_buffer);
while (true) {
/* Try to receive a packet header. */
R_TRY(this->ReceiveHeader(header));
/* Track how much we've received. */
size_t received = sizeof(*header);
/* If the packet has one, receive its body. */
if (header->body_size > 0) {
/* Sanity check the task id. */
AMS_ABORT_UNLESS(header->task_id < static_cast<int>(MaxRpcCount));
/* Sanity check the body size. */
AMS_ABORT_UNLESS(util::IsIntValueRepresentable<size_t>(header->body_size));
AMS_ABORT_UNLESS(static_cast<size_t>(header->body_size) <= sizeof(m_receive_buffer) - received);
/* Receive the body. */
R_TRY(this->ReceiveBody(header->data, header->body_size));
/* Note that we received the body. */
received += header->body_size;
}
/* Acquire exclusive access to the task tables. */
std::scoped_lock lk(m_mutex);
/* Get the specified task. */
Task *task = m_task_table.Get<Task>(header->task_id);
R_UNLESS(task != nullptr, htc::ResultInvalidTaskId());
/* If the task is canceled, free it. */
if (task->GetTaskState() == RpcTaskState::Cancelled) {
m_task_active[header->task_id] = false;
m_is_htcs_task[header->task_id] = false;
m_task_table.Delete(header->task_id);
m_task_id_free_list.Free(header->task_id);
continue;
}
/* Handle the packet. */
switch (header->category) {
case PacketCategory::Response:
R_TRY(task->ProcessResponse(m_receive_buffer, received));
break;
case PacketCategory::Notification:
R_TRY(task->ProcessNotification(m_receive_buffer, received));
break;
default:
R_THROW(htc::ResultInvalidCategory());
}
/* If we used the receive buffer, signal that we're done with it. */
if (task->IsReceiveBufferRequired()) {
os::SignalEvent(std::addressof(m_receive_buffer_available_events[header->task_id]));
}
}
}
Result RpcClient::ReceiveHeader(RpcPacket *header) {
/* Receive. */
s64 received;
R_TRY(m_driver->Receive(std::addressof(received), reinterpret_cast<char *>(header), sizeof(*header), m_channel_id, htclow::ReceiveOption_ReceiveAllData));
/* Check size. */
R_UNLESS(static_cast<size_t>(received) == sizeof(*header), htc::ResultInvalidSize());
R_SUCCEED();
}
Result RpcClient::ReceiveBody(char *dst, size_t size) {
/* Receive. */
s64 received;
R_TRY(m_driver->Receive(std::addressof(received), dst, size, m_channel_id, htclow::ReceiveOption_ReceiveAllData));
/* Check size. */
R_UNLESS(static_cast<size_t>(received) == size, htc::ResultInvalidSize());
R_SUCCEED();
}
Result RpcClient::SendThread() {
while (true) {
/* Get a task. */
Task *task;
u32 task_id{};
PacketCategory category{};
do {
/* Dequeue a task. */
R_TRY(m_task_queue.Take(std::addressof(task_id), std::addressof(category)));
/* Get the task from the table. */
std::scoped_lock lk(m_mutex);
task = m_task_table.Get<Task>(task_id);
} while (task == nullptr);
/* If required, wait for the send buffer to become available. */
if (task->IsSendBufferRequired()) {
os::WaitEvent(std::addressof(m_send_buffer_available_events[task_id]));
/* Check if we've been cancelled. */
if (m_cancelled) {
break;
}
}
/* Handle the task. */
size_t packet_size;
switch (category) {
case PacketCategory::Request:
R_TRY(task->CreateRequest(std::addressof(packet_size), m_send_buffer, sizeof(m_send_buffer), task_id));
break;
case PacketCategory::Notification:
R_TRY(task->CreateNotification(std::addressof(packet_size), m_send_buffer, sizeof(m_send_buffer), task_id));
break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
/* Send the request. */
R_TRY(this->SendRequest(m_send_buffer, packet_size));
}
R_THROW(htc::ResultCancelled());
}
Result RpcClient::SendRequest(const char *src, size_t size) {
/* Sanity check our size. */
AMS_ASSERT(util::IsIntValueRepresentable<s64>(size));
/* Send the data. */
s64 sent;
R_TRY(m_driver->Send(std::addressof(sent), src, static_cast<s64>(size), m_channel_id));
/* Check that we sent the right amount. */
R_UNLESS(sent == static_cast<s64>(size), htc::ResultInvalidSize());
R_SUCCEED();
}
void RpcClient::CancelBySocket(s32 handle) {
/* Check if we need to cancel each task. */
for (size_t i = 0; i < MaxRpcCount; ++i) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Check that the task is active and is an htcs task. */
if (!m_task_active[i] || !m_is_htcs_task[i]) {
continue;
}
/* Get the htcs task. */
auto *htcs_task = m_task_table.Get<htcs::impl::rpc::HtcsTask>(i);
/* Handle the case where the task handle is the one we're cancelling. */
if (this->GetTaskHandle(i) == handle) {
/* If the task is complete, free it. */
if (htcs_task->GetTaskState() == RpcTaskState::Completed) {
m_task_active[i] = false;
m_is_htcs_task[i] = false;
m_task_table.Delete(i);
m_task_id_free_list.Free(i);
} else {
/* If the task is a send task, notify. */
if (htcs_task->GetTaskType() == htcs::impl::rpc::HtcsTaskType::Send) {
m_task_queue.Add(i, PacketCategory::Notification);
}
/* Cancel the task. */
htcs_task->Cancel(RpcTaskCancelReason::BySocket);
}
/* The task has been cancelled, so we can move on. */
continue;
}
/* Handle the case where the task is a select task. */
if (htcs_task->GetTaskType() == htcs::impl::rpc::HtcsTaskType::Select) {
/* Get the select task. */
auto *select_task = m_task_table.Get<htcs::impl::rpc::SelectTask>(i);
/* Get the handle counts. */
const auto num_read = select_task->GetReadHandleCount();
const auto num_write = select_task->GetWriteHandleCount();
const auto num_exception = select_task->GetExceptionHandleCount();
const auto total = num_read + num_write + num_exception;
/* Get the handle array. */
const auto *handles = select_task->GetHandles();
/* Check each handle. */
for (auto handle_idx = 0; handle_idx < total; ++handle_idx) {
if (handles[handle_idx] == handle) {
/* If the select is complete, free it. */
if (select_task->GetTaskState() == RpcTaskState::Completed) {
m_task_active[i] = false;
m_is_htcs_task[i] = false;
m_task_table.Delete(i);
m_task_id_free_list.Free(i);
} else {
/* Cancel the task. */
select_task->Cancel(RpcTaskCancelReason::BySocket);
}
}
}
}
}
}
s32 RpcClient::GetTaskHandle(u32 task_id) {
/* TODO: Why is this necessary to avoid a bogus array-bounds warning? */
AMS_ASSUME(task_id < MaxRpcCount);
/* Check pre-conditions. */
AMS_ASSERT(m_task_active[task_id]);
AMS_ASSERT(m_is_htcs_task[task_id]);
/* Get the htcs task. */
auto *task = m_task_table.Get<htcs::impl::rpc::HtcsTask>(task_id);
/* Check that the task has a handle. */
if (!m_task_active[task_id] || !m_is_htcs_task[task_id] || task == nullptr) {
return -1;
}
/* Get the task's type. */
const auto type = task->GetTaskType();
/* Check that the task is new enough. */
if (task->GetVersion() == 3) {
if (type == htcs::impl::rpc::HtcsTaskType::Receive || type == htcs::impl::rpc::HtcsTaskType::Send) {
return -1;
}
}
/* Get the handle from the task. */
switch (type) {
case htcs::impl::rpc::HtcsTaskType::Receive:
return static_cast<htcs::impl::rpc::ReceiveTask *>(task)->GetHandle();
case htcs::impl::rpc::HtcsTaskType::Send:
return static_cast<htcs::impl::rpc::SendTask *>(task)->GetHandle();
case htcs::impl::rpc::HtcsTaskType::Shutdown:
return static_cast<htcs::impl::rpc::ShutdownTask *>(task)->GetHandle();
case htcs::impl::rpc::HtcsTaskType::Close:
return -1;
case htcs::impl::rpc::HtcsTaskType::Connect:
return static_cast<htcs::impl::rpc::ConnectTask *>(task)->GetHandle();
case htcs::impl::rpc::HtcsTaskType::Listen:
return static_cast<htcs::impl::rpc::ListenTask *>(task)->GetHandle();
case htcs::impl::rpc::HtcsTaskType::Accept:
return static_cast<htcs::impl::rpc::AcceptTask *>(task)->GetServerHandle();
case htcs::impl::rpc::HtcsTaskType::Socket:
return -1;
case htcs::impl::rpc::HtcsTaskType::Bind:
return static_cast<htcs::impl::rpc::BindTask *>(task)->GetHandle();
case htcs::impl::rpc::HtcsTaskType::Fcntl:
return static_cast<htcs::impl::rpc::FcntlTask *>(task)->GetHandle();
case htcs::impl::rpc::HtcsTaskType::ReceiveSmall:
return static_cast<htcs::impl::rpc::ReceiveSmallTask *>(task)->GetHandle();
case htcs::impl::rpc::HtcsTaskType::SendSmall:
return static_cast<htcs::impl::rpc::SendSmallTask *>(task)->GetHandle();
case htcs::impl::rpc::HtcsTaskType::Select:
return -1;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
}

View File

@@ -1,356 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "../driver/htc_i_driver.hpp"
#include "htc_rpc_task_table.hpp"
#include "htc_rpc_task_queue.hpp"
#include "htc_rpc_task_id_free_list.hpp"
#include "../../../htcs/impl/rpc/htcs_rpc_tasks.hpp"
namespace ams::htc::server::rpc {
template<typename T>
concept IsRpcTask = std::derived_from<T, Task>;
struct RpcTaskFunctionTraits {
public:
template<typename R, typename C, typename... A>
static std::tuple<A...> GetSetArgumentsImpl(R(C::*)(A...));
template<typename R, typename C, typename... A>
static std::tuple<A...> GetGetResultImpl(R(C::*)(A...) const);
};
template<typename T> requires IsRpcTask<T>
using RpcTaskArgumentsType = decltype(RpcTaskFunctionTraits::GetSetArgumentsImpl(&T::SetArguments));
template<typename T> requires IsRpcTask<T>
using RpcTaskResultsType = decltype(RpcTaskFunctionTraits::GetGetResultImpl(&T::GetResult));
template<typename T, size_t Ix> requires IsRpcTask<T>
using RpcTaskArgumentType = typename std::tuple_element<Ix, RpcTaskArgumentsType<T>>::type;
template<typename T, size_t Ix> requires IsRpcTask<T>
using RpcTaskResultType = typename std::tuple_element<Ix, RpcTaskResultsType<T>>::type;
class RpcClient {
private:
/* TODO: where is this value coming from, again? */
static constexpr size_t BufferSize = 0xE400;
private:
mem::StandardAllocator *m_allocator;
driver::IDriver *m_driver;
htclow::ChannelId m_channel_id;
void *m_receive_thread_stack;
void *m_send_thread_stack;
os::ThreadType m_receive_thread;
os::ThreadType m_send_thread;
os::SdkMutex &m_mutex;
RpcTaskIdFreeList &m_task_id_free_list;
RpcTaskTable &m_task_table;
bool m_task_active[MaxRpcCount];
bool m_is_htcs_task[MaxRpcCount];
RpcTaskQueue m_task_queue;
bool m_cancelled;
bool m_thread_running;
os::EventType m_receive_buffer_available_events[MaxRpcCount];
os::EventType m_send_buffer_available_events[MaxRpcCount];
char m_receive_buffer[BufferSize];
char m_send_buffer[BufferSize];
private:
static void ReceiveThreadEntry(void *arg) { static_cast<RpcClient *>(arg)->ReceiveThread(); }
static void SendThreadEntry(void *arg) { static_cast<RpcClient *>(arg)->SendThread(); }
Result ReceiveThread();
Result SendThread();
public:
RpcClient(driver::IDriver *driver, htclow::ChannelId channel);
RpcClient(mem::StandardAllocator *allocator, driver::IDriver *driver, htclow::ChannelId channel);
~RpcClient();
public:
void Open();
void Close();
Result Start();
void Cancel();
void Wait();
int WaitAny(htclow::ChannelState state, os::EventType *event);
private:
Result ReceiveHeader(RpcPacket *header);
Result ReceiveBody(char *dst, size_t size);
Result SendRequest(const char *src, size_t size);
private:
s32 GetTaskHandle(u32 task_id);
public:
void Wait(u32 task_id) {
os::WaitEvent(m_task_table.Get<Task>(task_id)->GetEvent());
}
os::NativeHandle DetachReadableHandle(u32 task_id) {
return os::DetachReadableHandleOfSystemEvent(m_task_table.Get<Task>(task_id)->GetSystemEvent());
}
void CancelBySocket(s32 handle);
template<typename T, typename... Args> requires (IsRpcTask<T> && sizeof...(Args) == std::tuple_size<RpcTaskArgumentsType<T>>::value)
Result Begin(u32 *out_task_id, Args &&... args) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Allocate a free task id. */
u32 task_id{};
R_TRY(m_task_id_free_list.Allocate(std::addressof(task_id)));
/* Create the new task. */
T *task = m_task_table.New<T>(task_id);
m_task_active[task_id] = true;
m_is_htcs_task[task_id] = htcs::impl::rpc::IsHtcsTask<T>;
/* Ensure we clean up the task, if we fail after this. */
auto task_guard = SCOPE_GUARD {
m_task_active[task_id] = false;
m_is_htcs_task[task_id] = false;
m_task_table.Delete<T>(task_id);
m_task_id_free_list.Free(task_id);
};
/* Set the task arguments. */
R_TRY(task->SetArguments(std::forward<Args>(args)...));
/* Clear the task's events. */
os::ClearEvent(std::addressof(m_receive_buffer_available_events[task_id]));
os::ClearEvent(std::addressof(m_send_buffer_available_events[task_id]));
/* Add the task to our queue if we can, or cancel it. */
if (m_thread_running) {
m_task_queue.Add(task_id, PacketCategory::Request);
} else {
task->Cancel(RpcTaskCancelReason::QueueNotAvailable);
}
/* Set the output task id. */
*out_task_id = task_id;
/* We succeeded. */
task_guard.Cancel();
R_SUCCEED();
}
template<typename T, typename... Args> requires (IsRpcTask<T> && sizeof...(Args) == std::tuple_size<RpcTaskResultsType<T>>::value)
Result GetResult(u32 task_id, Args &&... args) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Get the task. */
T *task = m_task_table.Get<T>(task_id);
R_UNLESS(task != nullptr, htc::ResultInvalidTaskId());
/* Check that the task is completed. */
R_UNLESS(task->GetTaskState() == RpcTaskState::Completed, htc::ResultTaskNotCompleted());
/* Get the task's result. */
R_TRY(task->GetResult(std::forward<Args>(args)...));
R_SUCCEED();
}
template<typename T, typename... Args> requires (IsRpcTask<T> && sizeof...(Args) == std::tuple_size<RpcTaskResultsType<T>>::value)
Result End(u32 task_id, Args &&... args) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Get the task. */
T *task = m_task_table.Get<T>(task_id);
R_UNLESS(task != nullptr, htc::ResultInvalidTaskId());
/* Ensure the task is freed if it needs to be, when we're done. */
auto task_guard = SCOPE_GUARD {
m_task_active[task_id] = false;
m_is_htcs_task[task_id] = false;
m_task_table.Delete<T>(task_id);
m_task_id_free_list.Free(task_id);
};
/* If the task was cancelled, handle that. */
if (task->GetTaskState() == RpcTaskState::Cancelled) {
switch (task->GetTaskCancelReason()) {
case RpcTaskCancelReason::BySocket:
task_guard.Cancel();
R_THROW(htc::ResultTaskCancelled());
case RpcTaskCancelReason::ClientFinalized:
R_THROW(htc::ResultCancelled());
case RpcTaskCancelReason::QueueNotAvailable:
R_THROW(htc::ResultTaskQueueNotAvailable());
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
/* Get the task's result. */
R_TRY(task->GetResult(std::forward<Args>(args)...));
R_SUCCEED();
}
template<typename T> requires IsRpcTask<T>
Result VerifyTaskIdWithHandle(u32 task_id, s32 handle) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Get the task. */
T *task = m_task_table.Get<T>(task_id);
R_UNLESS(task != nullptr, htc::ResultInvalidTaskId());
/* Check the task handle. */
R_UNLESS(task->GetHandle() == handle, htc::ResultInvalidTaskId());
R_SUCCEED();
}
template<typename T> requires IsRpcTask<T>
Result Notify(u32 task_id) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Check that our queue is available. */
R_UNLESS(m_thread_running, htc::ResultTaskQueueNotAvailable());
/* Get the task. */
T *task = m_task_table.Get<T>(task_id);
R_UNLESS(task != nullptr, htc::ResultInvalidTaskId());
/* Add notification to our queue. */
m_task_queue.Add(task_id, PacketCategory::Notification);
R_SUCCEED();
}
template<typename T> requires IsRpcTask<T>
void WaitNotification(u32 task_id) {
/* Get the task from the table, releasing our lock afterwards. */
T *task;
{
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Get the task. */
task = m_task_table.Get<T>(task_id);
}
/* Wait for a notification. */
task->WaitNotification();
}
template<typename T> requires IsRpcTask<T>
bool IsCancelled(u32 task_id) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Get the task. */
T *task = m_task_table.Get<T>(task_id);
/* Check the task state. */
return task != nullptr && task->GetTaskState() == RpcTaskState::Cancelled;
}
template<typename T> requires IsRpcTask<T>
bool IsCompleted(u32 task_id) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Get the task. */
T *task = m_task_table.Get<T>(task_id);
/* Check the task state. */
return task != nullptr && task->GetTaskState() == RpcTaskState::Completed;
}
template<typename T> requires IsRpcTask<T>
Result SendContinue(u32 task_id, const void *buffer, s64 buffer_size) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Get the task. */
T *task = m_task_table.Get<T>(task_id);
R_UNLESS(task != nullptr, htc::ResultInvalidTaskId());
/* If the task was cancelled, handle that. */
if (task->GetTaskState() == RpcTaskState::Cancelled) {
switch (task->GetTaskCancelReason()) {
case RpcTaskCancelReason::QueueNotAvailable:
R_THROW(htc::ResultTaskQueueNotAvailable());
default:
R_THROW(htc::ResultTaskCancelled());
}
}
/* Set the task's buffer. */
if (buffer_size > 0) {
task->SetBuffer(buffer, buffer_size);
os::SignalEvent(std::addressof(m_send_buffer_available_events[task_id]));
}
R_SUCCEED();
}
template<typename T> requires IsRpcTask<T>
Result ReceiveContinue(u32 task_id, void *buffer, s64 buffer_size) {
/* Get the task's buffer, and prepare to receive. */
const void *result_buffer;
s64 result_size;
{
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Get the task. */
T *task = m_task_table.Get<T>(task_id);
R_UNLESS(task != nullptr, htc::ResultInvalidTaskId());
/* If the task was cancelled, handle that. */
if (task->GetTaskState() == RpcTaskState::Cancelled) {
switch (task->GetTaskCancelReason()) {
case RpcTaskCancelReason::QueueNotAvailable:
R_THROW(htc::ResultTaskQueueNotAvailable());
default:
R_THROW(htc::ResultTaskCancelled());
}
}
/* Get the result size. */
result_size = task->GetResultSize();
R_SUCCEED_IF(result_size == 0);
/* Get the result buffer. */
result_buffer = task->GetBuffer();
}
/* Wait for the receive buffer to become available. */
os::WaitEvent(std::addressof(m_receive_buffer_available_events[task_id]));
/* Check that we weren't cancelled. */
R_UNLESS(!m_cancelled, htc::ResultCancelled());
/* Copy the received data. */
AMS_ASSERT(0 <= result_size && result_size <= buffer_size);
AMS_UNUSED(buffer_size);
std::memcpy(buffer, result_buffer, result_size);
R_SUCCEED();
}
};
}

View File

@@ -1,61 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "htc_rpc_tasks.hpp"
#include "htc_htcmisc_rpc_tasks.hpp"
namespace ams::htc::server::rpc {
class RpcTaskIdFreeList {
private:
u32 m_task_ids[MaxRpcCount];
u32 m_offset;
u32 m_free_count;
public:
constexpr RpcTaskIdFreeList() : m_task_ids(), m_offset(0), m_free_count(MaxRpcCount) {
for (auto i = 0; i < static_cast<int>(MaxRpcCount); ++i) {
m_task_ids[i] = i;
}
}
Result Allocate(u32 *out) {
/* Check that we have free tasks. */
R_UNLESS(m_free_count > 0, htc::ResultOutOfRpcTask());
/* Get index. */
const auto index = m_offset;
m_offset = (m_offset + 1) % MaxRpcCount;
--m_free_count;
/* Get the task id. */
*out = m_task_ids[index];
R_SUCCEED();
}
void Free(u32 task_id) {
/* Check pre-conditions. */
AMS_ASSERT(m_free_count < static_cast<int>(MaxRpcCount));
/* Determine index. */
const auto index = ((m_free_count++) + m_offset) % MaxRpcCount;
/* Set the task id. */
m_task_ids[index] = task_id;
}
};
}

View File

@@ -1,103 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "htc_rpc_tasks.hpp"
#include "htc_htcmisc_rpc_tasks.hpp"
namespace ams::htc::server::rpc {
class RpcTaskQueue {
private:
u32 m_task_ids[MaxRpcCount];
PacketCategory m_task_categories[MaxRpcCount];
int m_offset;
int m_count;
os::SdkConditionVariable m_cv;
os::SdkMutex m_mutex;
bool m_cancelled;
public:
constexpr RpcTaskQueue() = default;
void Initialize() {
m_offset = 0;
m_count = 0;
m_cancelled = false;
}
void Finalize() {
m_offset = 0;
m_count = 0;
}
void Cancel() {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Cancel ourselves. */
m_cancelled = true;
/* Signal to consumers/producers. */
m_cv.Signal();
}
void Add(u32 task_id, PacketCategory category) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Check pre-conditions. */
AMS_ASSERT(m_count < static_cast<int>(MaxRpcCount));
/* Determine index. */
const auto index = ((m_count++) + m_offset) % MaxRpcCount;
/* Set task. */
m_task_ids[index] = task_id;
m_task_categories[index] = category;
/* Signal. */
if (m_count > 0) {
m_cv.Signal();
}
}
Result Take(u32 *out_id, PacketCategory *out_category) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Wait until we can take. */
while (m_count == 0 && !m_cancelled) {
m_cv.Wait(m_mutex);
}
/* Check that we're not cancelled. */
R_UNLESS(!m_cancelled, htc::ResultCancelled());
/* Determine index. */
const auto index = m_offset;
/* Advance the queue. */
m_offset = (m_offset + 1) % MaxRpcCount;
--m_count;
/* Return the task info. */
*out_id = m_task_ids[index];
*out_category = m_task_categories[index];
R_SUCCEED();
}
};
}

View File

@@ -1,122 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "htc_rpc_tasks.hpp"
#include "htc_htcmisc_rpc_tasks.hpp"
namespace ams::htc::server::rpc {
/* For convenience. */
template<typename T>
concept IsTypeCheckableTask = std::derived_from<T, Task> && requires (T &t) {
{ t.GetTaskType() } -> std::convertible_to<decltype(T::TaskType)>;
};
static_assert(!IsTypeCheckableTask<Task>);
static_assert(IsTypeCheckableTask<GetEnvironmentVariableTask>);
class RpcTaskTable {
private:
/* htcs::ReceiveSmallTask/htcs::ReceiveSendTask are the largest tasks, containing an inline 0xE000 buffer. */
/* We allow for ~0x100 task overhead from the additional events those contain. */
/* NOTE: Nintendo hardcodes a maximum size of 0xE1D8, despite SendSmallTask being 0xE098 as of latest check. */
#if defined(ATMOSPHERE_OS_HORIZON)
static constexpr size_t MaxTaskSize = 0xE100;
#elif defined(ATMOSPHERE_OS_MACOS)
static constexpr size_t MaxTaskSize = 0xE400;
#else
static constexpr size_t MaxTaskSize = 0xE1D8;
#endif
struct TaskStorage { alignas(alignof(void *)) std::byte _storage[MaxTaskSize]; };
private:
bool m_valid[MaxRpcCount]{};
TaskStorage m_storages[MaxRpcCount]{};
private:
template<typename T>
ALWAYS_INLINE T *GetPointer(u32 index) {
static_assert(alignof(T) <= alignof(TaskStorage));
static_assert(sizeof(T) <= sizeof(TaskStorage));
return reinterpret_cast<T *>(std::addressof(m_storages[index]));
}
ALWAYS_INLINE bool IsValid(u32 index) {
return index < MaxRpcCount && m_valid[index];
}
public:
constexpr RpcTaskTable() = default;
template<typename T> requires std::derived_from<T, Task>
T *New(u32 index) {
/* Sanity check input. */
AMS_ASSERT(!this->IsValid(index));
/* Set valid. */
m_valid[index] = true;
/* Allocate the task. */
T *task = this->GetPointer<T>(index);
/* Create the task. */
std::construct_at(task);
/* Return the task. */
return task;
}
template<typename T> requires std::derived_from<T, Task>
T *Get(u32 index) {
/* Check that the task is valid. */
if (!this->IsValid(index)) {
return nullptr;
}
/* Get the task pointer. */
T *task = this->GetPointer<T>(index);
/* Type check the task. */
if constexpr (IsTypeCheckableTask<T>) {
if (task->GetTaskType() != T::TaskType) {
task = nullptr;
}
}
/* Return the task. */
return task;
}
template<typename T> requires std::derived_from<T, Task>
void Delete(u32 index) {
/* Check that the task is valid. */
if (!this->IsValid(index)) {
return;
}
/* Delete the task. */
std::destroy_at(this->GetPointer<T>(index));
/* Mark the task as invalid. */
m_valid[index] = false;
}
void Delete(u32 index) {
if (this->IsValid(index)) {
this->Delete<Task>(index);
}
}
};
}

View File

@@ -1,123 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::htc::server::rpc {
constexpr inline size_t MaxRpcCount = 0x48;
enum class PacketCategory : s16 {
Request = 0,
Response = 1,
Notification = 2,
};
struct RpcPacket {
s16 protocol;
s16 version;
PacketCategory category;
u16 type;
s64 body_size;
u32 task_id{};
u64 params[5];
char data[];
};
static_assert(sizeof(RpcPacket) == 0x40);
enum class RpcTaskCancelReason {
None = 0,
BySocket = 1,
ClientFinalized = 2,
QueueNotAvailable = 3,
};
enum class RpcTaskState {
Started = 0,
Completed = 1,
Cancelled = 2,
Notified = 3,
};
class Task {
private:
RpcTaskState m_state;
RpcTaskCancelReason m_cancel_reason;
os::Event m_event;
public:
Task() : m_state(RpcTaskState::Started), m_cancel_reason(RpcTaskCancelReason::None), m_event(os::EventClearMode_AutoClear) { /* ... */ }
virtual ~Task() { /* ... */ }
public:
void SetTaskState(RpcTaskState state) { m_state = state; }
RpcTaskState GetTaskState() const { return m_state; }
RpcTaskCancelReason GetTaskCancelReason() const { return m_cancel_reason; }
os::EventType *GetEvent() { return m_event.GetBase(); }
void Notify() {
AMS_ASSERT(m_state == RpcTaskState::Started);
m_state = RpcTaskState::Notified;
}
void Complete() {
AMS_ASSERT(m_state == RpcTaskState::Started || m_state == RpcTaskState::Notified);
m_state = RpcTaskState::Completed;
m_event.Signal();
}
public:
virtual void Cancel(RpcTaskCancelReason reason) {
m_state = RpcTaskState::Cancelled;
m_cancel_reason = reason;
m_event.Signal();
}
virtual Result ProcessResponse(const char *data, size_t size) {
AMS_UNUSED(data, size);
R_SUCCEED();
}
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
AMS_UNUSED(out, data, size, task_id);
R_SUCCEED();
}
virtual Result ProcessNotification(const char *data, size_t size) {
AMS_UNUSED(data, size);
R_SUCCEED();
}
virtual Result CreateNotification(size_t *out, char *data, size_t size, u32 task_id) {
AMS_UNUSED(out, data, size, task_id);
R_SUCCEED();
}
virtual bool IsReceiveBufferRequired() {
return false;
}
virtual bool IsSendBufferRequired() {
return false;
}
virtual os::SystemEventType *GetSystemEvent() {
return nullptr;
}
};
}

View File

@@ -1,31 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "impl/htc_tenv_allocator.hpp"
#include "impl/htc_tenv_impl.hpp"
namespace ams::htc::tenv {
void Initialize(AllocateFunction allocate, DeallocateFunction deallocate) {
/* Initialize the library allocator. */
impl::InitializeAllocator(allocate, deallocate);
}
void UnregisterDefinitionFilePath(os::ProcessId process_id) {
return impl::UnregisterDefinitionFilePath(process_id.value);
}
}

View File

@@ -1,39 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_tenv_service.hpp"
namespace ams::htc::tenv {
Result Service::GetVariable(sf::Out<s64> out_size, const sf::OutBuffer &out_buffer, const htc::tenv::VariableName &name) {
/* TODO */
AMS_UNUSED(out_size, out_buffer, name);
AMS_ABORT("Service::GetVariable");
}
Result Service::GetVariableLength(sf::Out<s64> out_size, const htc::tenv::VariableName &name) {
/* TODO */
AMS_UNUSED(out_size, name);
AMS_ABORT("Service::GetVariableLength");
}
Result Service::WaitUntilVariableAvailable(s64 timeout_ms) {
/* TODO */
AMS_UNUSED(timeout_ms);
AMS_ABORT("Service::WaitUntilVariableAvailable");
}
}

View File

@@ -1,33 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::htc::tenv {
class Service {
private:
os::ProcessId m_process_id;
public:
constexpr Service(os::ProcessId pid) : m_process_id(pid) { /* ... */ }
public:
Result GetVariable(sf::Out<s64> out_size, const sf::OutBuffer &out_buffer, const htc::tenv::VariableName &name);
Result GetVariableLength(sf::Out<s64> out_size,const htc::tenv::VariableName &name);
Result WaitUntilVariableAvailable(s64 timeout_ms);
};
static_assert(htc::tenv::IsIService<Service>);
}

View File

@@ -1,27 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "impl/htc_tenv_allocator.hpp"
#include "htc_tenv_service.hpp"
namespace ams::htc::tenv {
Result ServiceManager::GetServiceInterface(sf::Out<sf::SharedPointer<htc::tenv::IService>> out, const sf::ClientProcessId &process_id) {
*out = impl::SfObjectFactory::CreateSharedEmplaced<htc::tenv::IService, Service>(process_id.GetValue());
R_SUCCEED();
}
}

View File

@@ -1,56 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_tenv_allocator.hpp"
namespace ams::htc::tenv::impl {
namespace {
constinit AllocateFunction g_allocate = nullptr;
constinit DeallocateFunction g_deallocate = nullptr;
}
void InitializeAllocator(AllocateFunction allocate, DeallocateFunction deallocate) {
/* Check that we don't already have allocator functions. */
AMS_ASSERT(g_allocate == nullptr);
AMS_ASSERT(g_deallocate == nullptr);
/* Set our allocator functions. */
g_allocate = allocate;
g_deallocate = deallocate;
/* Check that we have allocator functions. */
AMS_ASSERT(g_allocate != nullptr);
AMS_ASSERT(g_deallocate != nullptr);
}
void *Allocate(size_t size) {
/* Check that we have an allocator. */
AMS_ASSERT(g_allocate != nullptr);
return g_allocate(size);
}
void Deallocate(void *p, size_t size) {
/* Check that we have a deallocator. */
AMS_ASSERT(g_deallocate != nullptr);
return g_deallocate(p, size);
}
}

View File

@@ -1,42 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::htc::tenv::impl {
void InitializeAllocator(AllocateFunction allocate, DeallocateFunction deallocate);
void *Allocate(size_t size);
void Deallocate(void *p, size_t size);
class SfAllocator {
public:
constexpr ALWAYS_INLINE SfAllocator() { /* ... */ }
void *Allocate(size_t size) {
return ::ams::htc::tenv::impl::Allocate(size);
}
void Deallocate(void *p, size_t size) {
return ::ams::htc::tenv::impl::Deallocate(p, size);
}
};
using SfPolicy = sf::StatelessAllocationPolicy<SfAllocator>;
using SfObjectFactory = sf::ObjectFactory<SfPolicy>;
}

View File

@@ -1,40 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "htc_tenv_allocator.hpp"
namespace ams::htc::tenv::impl {
struct DefinitionFileInfo : public util::IntrusiveListBaseNode<DefinitionFileInfo> {
u64 process_id;
Path path;
DefinitionFileInfo(u64 pid, Path *p) : process_id(pid) {
AMS_ASSERT(p != nullptr);
util::Strlcpy(this->path.str, p->str, PathLengthMax);
}
static void *operator new(size_t size) {
return Allocate(size);
}
static void operator delete(void *p, size_t size) {
Deallocate(p, size);
}
};
}

View File

@@ -1,81 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "htc_tenv_impl.hpp"
#include "htc_tenv_definition_file_info.hpp"
namespace ams::htc::tenv::impl {
namespace {
class DefinitionFileInfoManager {
private:
using DefinitionFileInfoList = util::IntrusiveListBaseTraits<DefinitionFileInfo>::ListType;
private:
DefinitionFileInfoList m_list;
os::SdkMutex m_mutex;
public:
constexpr DefinitionFileInfoManager() = default;
~DefinitionFileInfoManager() {
while (!m_list.empty()) {
auto *p = std::addressof(*m_list.rbegin());
m_list.erase(m_list.iterator_to(*p));
delete p;
}
}
void Remove(DefinitionFileInfo *info) {
std::scoped_lock lk(m_mutex);
m_list.erase(m_list.iterator_to(*info));
delete info;
}
DefinitionFileInfo *GetInfo(u64 process_id) {
std::scoped_lock lk(m_mutex);
for (auto &info : m_list) {
if (info.process_id == process_id) {
return std::addressof(info);
}
}
return nullptr;
}
};
constinit DefinitionFileInfoManager g_definition_file_info_manager;
ALWAYS_INLINE DefinitionFileInfoManager &GetDefinitionFileInfoManager() {
return g_definition_file_info_manager;
}
}
void UnregisterDefinitionFilePath(u64 process_id) {
/* Require the process id to be valid. */
if (process_id == 0) {
return;
}
/* Remove the definition file info, if we have one. */
if (auto *info = GetDefinitionFileInfoManager().GetInfo(process_id); info != nullptr) {
GetDefinitionFileInfoManager().Remove(info);
}
}
}

View File

@@ -1,23 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::htc::tenv::impl {
void UnregisterDefinitionFilePath(u64 process_id);
}