htc: skeleton HtcsManagerImpl, implement HtcsMonitor

This commit is contained in:
Michael Scire
2021-02-10 18:54:40 -08:00
committed by SciresM
parent cb5a706659
commit 10255f7f51
20 changed files with 652 additions and 17 deletions

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2018-2020 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 "htcs_manager.hpp"
#include "htcs_manager_impl.hpp"
namespace ams::htcs::impl {
HtcsManager::HtcsManager(mem::StandardAllocator *allocator, htclow::HtclowManager *htclow_manager) : m_allocator(allocator), m_impl(static_cast<HtcsManagerImpl *>(allocator->Allocate(sizeof(HtcsManagerImpl), alignof(HtcsManagerImpl)))) {
std::construct_at(m_impl, m_allocator, htclow_manager);
}
HtcsManager::~HtcsManager() {
std::destroy_at(m_impl);
m_allocator->Free(m_impl);
}
os::EventType *HtcsManager::GetServiceAvailabilityEvent() {
return m_impl->GetServiceAvailabilityEvent();
}
bool HtcsManager::IsServiceAvailable() {
return m_impl->IsServiceAvailable();
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2018-2020 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"
namespace ams::htcs::impl {
class HtcsManagerImpl;
class HtcsManager {
private:
mem::StandardAllocator *m_allocator;
HtcsManagerImpl *m_impl;
public:
HtcsManager(mem::StandardAllocator *allocator, htclow::HtclowManager *htclow_manager);
~HtcsManager();
public:
os::EventType *GetServiceAvailabilityEvent();
bool IsServiceAvailable();
};
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2018-2020 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 "htcs_manager.hpp"
namespace ams::htcs::impl::HtcsManagerHolder {
namespace {
constinit os::SdkMutex g_holder_mutex;
constinit int g_holder_reference_count = 0;
mem::StandardAllocator g_allocator;
constinit HtcsManager *g_manager = nullptr;
alignas(os::MemoryPageSize) u8 g_heap_buffer[416_KB];
}
void AddReference() {
std::scoped_lock lk(g_holder_mutex);
if ((g_holder_reference_count++) == 0) {
/* Add reference to the htclow manager. */
htclow::HtclowManagerHolder::AddReference();
/* Initialize the allocator for the manager. */
g_allocator.Initialize(g_heap_buffer, sizeof(g_heap_buffer));
/* Allocate the manager. */
g_manager = static_cast<HtcsManager *>(g_allocator.Allocate(sizeof(HtcsManager), alignof(HtcsManager)));
/* Construct the manager. */
std::construct_at(g_manager, std::addressof(g_allocator), htclow::HtclowManagerHolder::GetHtclowManager());
}
AMS_ASSERT(g_holder_reference_count > 0);
}
void Release() {
std::scoped_lock lk(g_holder_mutex);
AMS_ASSERT(g_holder_reference_count > 0);
if ((--g_holder_reference_count) == 0) {
/* Destroy the manager. */
std::destroy_at(g_manager);
g_allocator.Free(g_manager);
g_manager = nullptr;
/* Finalize the allocator. */
g_allocator.Finalize();
/* Release reference to the htclow manager. */
htclow::HtclowManagerHolder::Release();
}
}
HtcsManager *GetHtcsManager() {
std::scoped_lock lk(g_holder_mutex);
return g_manager;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2018-2020 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 "htcs_manager.hpp"
#include "htcs_manager_impl.hpp"
namespace ams::htcs::impl {
HtcsManagerImpl::HtcsManagerImpl(mem::StandardAllocator *allocator, htclow::HtclowManager *htclow_manager)
: m_allocator(allocator),
m_driver(htclow_manager, htclow::ModuleId::Htcs),
m_driver_manager(std::addressof(m_driver)),
m_rpc_client(m_allocator, std::addressof(m_driver), HtcsClientChannelId),
m_data_channel_manager(std::addressof(m_rpc_client), htclow_manager),
m_service(m_allocator, m_driver_manager.GetDriver(), std::addressof(m_rpc_client), std::addressof(m_data_channel_manager)),
m_monitor(m_allocator, m_driver_manager.GetDriver(), std::addressof(m_rpc_client), std::addressof(m_service))
{
/* Start the monitor. */
m_monitor.Start();
}
HtcsManagerImpl::~HtcsManagerImpl() {
/* Cancel our monitor. */
m_monitor.Cancel();
m_monitor.Wait();
}
os::EventType *HtcsManagerImpl::GetServiceAvailabilityEvent() {
return m_monitor.GetServiceAvailabilityEvent();
}
bool HtcsManagerImpl::IsServiceAvailable() {
return m_monitor.IsServiceAvailable();
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (c) 2018-2020 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/server/driver/htc_htclow_driver.hpp"
#include "../../htc/server/driver/htc_driver_manager.hpp"
#include "../../htc/server/rpc/htc_rpc_client.hpp"
#include "rpc/htcs_data_channel_manager.hpp"
#include "htcs_service.hpp"
#include "htcs_monitor.hpp"
namespace ams::htcs::impl {
class HtcsManagerImpl {
private:
mem::StandardAllocator *m_allocator;
htc::server::driver::HtclowDriver m_driver;
htc::server::driver::DriverManager m_driver_manager;
htc::server::rpc::RpcClient m_rpc_client;
rpc::DataChannelManager m_data_channel_manager;
HtcsService m_service;
HtcsMonitor m_monitor;
public:
HtcsManagerImpl(mem::StandardAllocator *allocator, htclow::HtclowManager *htclow_manager);
~HtcsManagerImpl();
public:
os::EventType *GetServiceAvailabilityEvent();
bool IsServiceAvailable();
};
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright (c) 2018-2020 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 "htcs_manager.hpp"
#include "htcs_manager_impl.hpp"
namespace ams::htcs::impl {
HtcsMonitor::HtcsMonitor(mem::StandardAllocator *allocator, htc::server::driver::IDriver *drv, htc::server::rpc::RpcClient *rc, HtcsService *srv)
: m_allocator(allocator),
m_driver(drv),
m_rpc_client(rc),
m_service(srv),
m_monitor_thread_stack(m_allocator->Allocate(os::MemoryPageSize, os::ThreadStackAlignment)),
m_mutex(),
m_cancel_event(os::EventClearMode_ManualClear),
m_service_availability_event(os::EventClearMode_ManualClear),
m_cancelled(false),
m_is_service_available(false)
{
/* ... */
}
HtcsMonitor::~HtcsMonitor() {
/* Free thread stack. */
m_allocator->Free(m_monitor_thread_stack);
}
void HtcsMonitor::Start() {
/* Create the monitor thread. */
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_monitor_thread), ThreadEntry, this, m_monitor_thread_stack, os::MemoryPageSize, AMS_GET_SYSTEM_THREAD_PRIORITY(htc, HtcsMonitor)));
/* Set thread name. */
os::SetThreadNamePointer(std::addressof(m_monitor_thread), AMS_GET_SYSTEM_THREAD_NAME(htc, HtcsMonitor));
/* Start the monitor thread. */
os::StartThread(std::addressof(m_monitor_thread));
}
void HtcsMonitor::Cancel() {
/* Cancel, and signal. */
m_cancelled = true;
m_cancel_event.Signal();
}
void HtcsMonitor::Wait() {
/* Wait for the thread. */
os::WaitThread(std::addressof(m_monitor_thread));
os::DestroyThread(std::addressof(m_monitor_thread));
}
void HtcsMonitor::ThreadBody() {
/* 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 server. */
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())) {
continue;
}
/* We're available! */
this->SetServiceAvailability(true);
client_guard.Cancel();
/* We're available, 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->SetServiceAvailability(false);
};
/* Wait to become disconnected. */
if (m_rpc_client->WaitAny(htclow::ChannelState_Disconnected, m_cancel_event.GetBase()) != 0) {
break;
}
}
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2018-2020 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/server/driver/htc_i_driver.hpp"
#include "../../htc/server/rpc/htc_rpc_client.hpp"
#include "htcs_service.hpp"
namespace ams::htcs::impl {
class HtcsMonitor {
private:
mem::StandardAllocator *m_allocator;
htc::server::driver::IDriver *m_driver;
htc::server::rpc::RpcClient *m_rpc_client;
HtcsService *m_service;
void *m_monitor_thread_stack;
os::ThreadType m_monitor_thread;
os::SdkMutex m_mutex;
os::Event m_cancel_event;
os::Event m_service_availability_event;
bool m_cancelled;
bool m_is_service_available;
private:
static void ThreadEntry(void *arg) {
static_cast<HtcsMonitor *>(arg)->ThreadBody();
}
void ThreadBody();
public:
HtcsMonitor(mem::StandardAllocator *allocator, htc::server::driver::IDriver *drv, htc::server::rpc::RpcClient *rc, HtcsService *srv);
~HtcsMonitor();
public:
void Start();
void Cancel();
void Wait();
os::EventType *GetServiceAvailabilityEvent() { return m_service_availability_event.GetBase(); }
bool IsServiceAvailable() {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Get availability. */
return m_is_service_available;
}
private:
void SetServiceAvailability(bool available) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Set availability. */
m_is_service_available = available;
/* Signal availability change. */
m_service_availability_event.Signal();
}
};
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2018-2020 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/server/driver/htc_i_driver.hpp"
#include "../../htc/server/rpc/htc_rpc_client.hpp"
#include "rpc/htcs_data_channel_manager.hpp"
namespace ams::htcs::impl {
class HtcsService {
private:
mem::StandardAllocator *m_allocator;
htc::server::driver::IDriver *m_driver;
htc::server::rpc::RpcClient *m_rpc_client;
rpc::DataChannelManager *m_data_channel_manager;
public:
HtcsService(mem::StandardAllocator *allocator, htc::server::driver::IDriver *drv, htc::server::rpc::RpcClient *rc, rpc::DataChannelManager *dcm)
: m_allocator(allocator), m_driver(drv), m_rpc_client(rc), m_data_channel_manager(dcm) { /* ... */ }
public:
/* TODO */
};
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2018-2020 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/server/rpc/htc_rpc_client.hpp"
namespace ams::htcs::impl::rpc {
class DataChannelManager {
private:
htc::server::rpc::RpcClient* m_rpc_client;
htclow::HtclowManager *m_htclow_manager;
htclow::Module m_module;
public:
DataChannelManager(htc::server::rpc::RpcClient *client, htclow::HtclowManager *htclow_manager) : m_rpc_client(client), m_htclow_manager(htclow_manager), m_module(htclow::ModuleId::Htcs) { /* ... */ }
public:
/* TODO */
};
}