htc: implement htclow listener thread

This commit is contained in:
Michael Scire
2021-02-08 05:45:23 -08:00
committed by SciresM
parent c9c41e0e8d
commit 2341f18edd
21 changed files with 669 additions and 11 deletions

View File

@@ -15,6 +15,7 @@
*/
#include <stratosphere.hpp>
#include "htclow_mux.hpp"
#include "../ctrl/htclow_ctrl_state_machine.hpp"
namespace ams::htclow::mux {
@@ -37,4 +38,28 @@ namespace ams::htclow::mux {
}
}
void Mux::UpdateChannelState() {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Update the state of all channels in our map. */
/* NOTE: Nintendo does this highly inefficiently... */
for (auto pair : m_channel_impl_map.GetMap()) {
m_channel_impl_map[pair.first].UpdateState();
}
}
void Mux::UpdateMuxState() {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* Update whether we're sleeping. */
if (m_state_machine->IsSleeping()) {
m_is_sleeping = true;
} else {
m_is_sleeping = false;
m_wake_event.Signal();
}
}
}

View File

@@ -36,6 +36,9 @@ namespace ams::htclow::mux {
Mux(PacketFactory *pf, ctrl::HtcctrlStateMachine *sm);
void SetVersion(u16 version);
void UpdateChannelState();
void UpdateMuxState();
};
}

View File

@@ -15,6 +15,7 @@
*/
#include <stratosphere.hpp>
#include "htclow_mux_channel_impl.hpp"
#include "../ctrl/htclow_ctrl_state_machine.hpp"
namespace ams::htclow::mux {
@@ -27,4 +28,59 @@ namespace ams::htclow::mux {
m_send_buffer.SetVersion(version);
}
void ChannelImpl::UpdateState() {
/* Check if shutdown must be forced. */
if (m_state_machine->IsUnsupportedServiceChannelToShutdown(m_channel)) {
this->ShutdownForce();
}
/* Check if we're readied. */
if (m_state_machine->IsReadied()) {
m_task_manager->NotifyConnectReady();
}
/* Update our state transition. */
if (m_state_machine->IsConnectable(m_channel)) {
if (m_state == ChannelState_Unconnectable) {
this->SetState(ChannelState_Connectable);
}
} else if (m_state_machine->IsUnconnectable()) {
if (m_state == ChannelState_Connectable) {
this->SetState(ChannelState_Unconnectable);
m_state_machine->SetNotConnecting(m_channel);
} else if (m_state == ChannelState_Connected) {
this->ShutdownForce();
}
}
}
void ChannelImpl::ShutdownForce() {
/* Clear our send buffer. */
m_send_buffer.Clear();
/* Set our state to shutdown. */
this->SetState(ChannelState_Shutdown);
}
void ChannelImpl::SetState(ChannelState state) {
/* Check that we can perform the transition. */
AMS_ABORT_UNLESS(IsStateTransitionAllowed(m_state, state));
/* Perform the transition. */
this->SetStateWithoutCheck(state);
}
void ChannelImpl::SetStateWithoutCheck(ChannelState state) {
/* Change our state. */
if (m_state != state) {
m_state = state;
m_state_change_event.Signal();
}
/* If relevant, notify disconnect. */
if (m_state == ChannelState_Shutdown) {
m_task_manager->NotifyDisconnect(m_channel);
}
}
}

View File

@@ -45,12 +45,18 @@ namespace ams::htclow::mux {
/* TODO: Channel config */
/* TODO: tracking variables. */
std::optional<u64> m_108;
os::Event m_send_packet_event;
/* TODO: Channel state. */
os::Event m_state_change_event;
ChannelState m_state;
public:
ChannelImpl(impl::ChannelInternalType channel, PacketFactory *pf, ctrl::HtcctrlStateMachine *sm, TaskManager *tm, os::Event *ev);
void SetVersion(s16 version);
void UpdateState();
private:
void ShutdownForce();
void SetState(ChannelState state);
void SetStateWithoutCheck(ChannelState state);
};
}

View File

@@ -15,6 +15,7 @@
*/
#include <stratosphere.hpp>
#include "htclow_mux_channel_impl.hpp"
#include "../htclow_packet_factory.hpp"
namespace ams::htclow::mux {
@@ -23,4 +24,12 @@ namespace ams::htclow::mux {
m_version = version;
}
void SendBuffer::Clear() {
while (!m_packet_list.empty()) {
auto *packet = std::addressof(m_packet_list.front());
m_packet_list.pop_front();
m_packet_factory->Delete(packet);
}
}
}

View File

@@ -41,6 +41,8 @@ namespace ams::htclow::mux {
SendBuffer(impl::ChannelInternalType channel, PacketFactory *pf);
void SetVersion(s16 version);
void Clear();
};
}

View File

@@ -0,0 +1,50 @@
/*
* 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 "htclow_mux_task_manager.hpp"
namespace ams::htclow::mux {
void TaskManager::NotifyDisconnect(impl::ChannelInternalType channel) {
for (auto i = 0; i < MaxTaskCount; ++i) {
if (m_valid[i] && m_tasks[i].channel == channel) {
this->CompleteTask(i, EventTrigger_Disconnect);
}
}
}
void TaskManager::NotifyConnectReady() {
for (auto i = 0; i < MaxTaskCount; ++i) {
if (m_valid[i] && m_tasks[i].type == TaskType_Connect) {
this->CompleteTask(i, EventTrigger_ConnectReady);
}
}
}
void TaskManager::CompleteTask(int index, EventTrigger trigger) {
/* Check pre-conditions. */
AMS_ASSERT(0 <= index && index < MaxTaskCount);
AMS_ASSERT(m_valid[index]);
/* Complete the task. */
if (!m_tasks[index].has_event_trigger) {
m_tasks[index].has_event_trigger = true;
m_tasks[index].event_trigger = trigger;
os::SignalEvent(std::addressof(m_tasks[index].event));
}
}
}

View File

@@ -20,14 +20,26 @@ namespace ams::htclow::mux {
constexpr inline int MaxTaskCount = 0x80;
enum EventTrigger : u8 {
EventTrigger_Disconnect = 1,
EventTrigger_ConnectReady = 11,
};
class TaskManager {
private:
enum TaskType : u8 {
TaskType_Receive = 0,
TaskType_Send = 1,
TaskType_Flush = 6,
TaskType_Connect = 7,
};
struct Task {
impl::ChannelInternalType channel;
os::EventType event;
u8 _30;
u8 _31;
u8 _32;
bool has_event_trigger;
EventTrigger event_trigger;
TaskType type;
u64 _38;
};
private:
@@ -35,6 +47,11 @@ namespace ams::htclow::mux {
Task m_tasks[MaxTaskCount];
public:
TaskManager() : m_valid() { /* ... */ }
void NotifyDisconnect(impl::ChannelInternalType channel);
void NotifyConnectReady();
private:
void CompleteTask(int index, EventTrigger trigger);
};
}