htc: implement mux side of connecting (and more)
This commit is contained in:
@@ -182,6 +182,58 @@ namespace ams::htclow::mux {
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result Mux::Close(impl::ChannelInternalType channel) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* If we have the channel, close it. */
|
||||
if (auto it = m_channel_impl_map.GetMap().find(channel); it != m_channel_impl_map.GetMap().end()) {
|
||||
/* Shut down the channel. */
|
||||
m_channel_impl_map[it->second].ShutdownForce();
|
||||
|
||||
/* Remove the channel. */
|
||||
R_ABORT_UNLESS(m_channel_impl_map.RemoveChannel(channel));
|
||||
}
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result Mux::ConnectBegin(u32 *out_task_id, impl::ChannelInternalType channel) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Find the channel. */
|
||||
auto it = m_channel_impl_map.GetMap().find(channel);
|
||||
R_UNLESS(it != m_channel_impl_map.GetMap().end(), htclow::ResultChannelNotExist());
|
||||
|
||||
/* Perform the connection. */
|
||||
return m_channel_impl_map[it->second].DoConnectBegin(out_task_id);
|
||||
}
|
||||
|
||||
Result Mux::ConnectEnd(impl::ChannelInternalType channel, u32 task_id) {
|
||||
/* Get the trigger for the task. */
|
||||
const auto trigger = m_task_manager.GetTrigger(task_id);
|
||||
|
||||
/* Free the task. */
|
||||
m_task_manager.FreeTask(task_id);
|
||||
|
||||
/* Check that we didn't hit a disconnect. */
|
||||
R_UNLESS(trigger != EventTrigger_Disconnect, htclow::ResultInvalidChannelStateDisconnected());
|
||||
|
||||
/* Find the channel. */
|
||||
auto it = m_channel_impl_map.GetMap().find(channel);
|
||||
R_UNLESS(it != m_channel_impl_map.GetMap().end(), htclow::ResultChannelNotExist());
|
||||
|
||||
/* Perform the disconnection. */
|
||||
return m_channel_impl_map[it->second].DoConnectEnd();
|
||||
}
|
||||
|
||||
ChannelState Mux::GetChannelState(impl::ChannelInternalType channel);
|
||||
os::EventType *Mux::GetChannelStateEvent(impl::ChannelInternalType channel);
|
||||
|
||||
Result Mux::FlushBegin(u32 *out_task_id, impl::ChannelInternalType channel);
|
||||
Result Mux::FlushEnd(u32 task_id);
|
||||
|
||||
os::EventType *Mux::GetTaskEvent(u32 task_id) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
@@ -189,6 +241,12 @@ namespace ams::htclow::mux {
|
||||
return m_task_manager.GetTaskEvent(task_id);
|
||||
}
|
||||
|
||||
Result Mux::ReceiveBegin(u32 *out_task_id, impl::ChannelInternalType channel, bool blocking);
|
||||
Result Mux::ReceiveEnd(size_t *out, void *dst, size_t dst_size, impl::ChannelInternalType channel, u32 task_id);
|
||||
|
||||
Result Mux::SendBegin(u32 *out_task_id, size_t *out, const void *src, size_t src_size, impl::ChannelInternalType channel);
|
||||
Result Mux::SendEnd(u32 task_id);
|
||||
|
||||
void Mux::SetSendBuffer(impl::ChannelInternalType channel, void *buf, size_t buf_size, size_t max_packet_size) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
@@ -225,4 +283,6 @@ namespace ams::htclow::mux {
|
||||
m_channel_impl_map[it->second].SetReceiveBuffer(buf, buf_size);
|
||||
}
|
||||
|
||||
Result Mux::Shutdown(impl::ChannelInternalType channel);
|
||||
|
||||
}
|
||||
|
||||
@@ -55,12 +55,30 @@ namespace ams::htclow::mux {
|
||||
void UpdateMuxState();
|
||||
public:
|
||||
Result Open(impl::ChannelInternalType channel);
|
||||
Result Close(impl::ChannelInternalType channel);
|
||||
|
||||
Result ConnectBegin(u32 *out_task_id, impl::ChannelInternalType channel);
|
||||
Result ConnectEnd(impl::ChannelInternalType channel, u32 task_id);
|
||||
|
||||
ChannelState GetChannelState(impl::ChannelInternalType channel);
|
||||
os::EventType *GetChannelStateEvent(impl::ChannelInternalType channel);
|
||||
|
||||
Result FlushBegin(u32 *out_task_id, impl::ChannelInternalType channel);
|
||||
Result FlushEnd(u32 task_id);
|
||||
|
||||
os::EventType *GetTaskEvent(u32 task_id);
|
||||
|
||||
Result ReceiveBegin(u32 *out_task_id, impl::ChannelInternalType channel, bool blocking);
|
||||
Result ReceiveEnd(size_t *out, void *dst, size_t dst_size, impl::ChannelInternalType channel, u32 task_id);
|
||||
|
||||
Result SendBegin(u32 *out_task_id, size_t *out, const void *src, size_t src_size, impl::ChannelInternalType channel);
|
||||
Result SendEnd(u32 task_id);
|
||||
|
||||
void SetSendBuffer(impl::ChannelInternalType channel, void *buf, size_t buf_size, size_t max_packet_size);
|
||||
void SetReceiveBuffer(impl::ChannelInternalType channel, void *buf, size_t buf_size);
|
||||
void SetSendBufferWithData(impl::ChannelInternalType channel, const void *buf, size_t buf_size, size_t max_packet_size);
|
||||
|
||||
Result Shutdown(impl::ChannelInternalType channel);
|
||||
private:
|
||||
Result CheckChannelExist(impl::ChannelInternalType channel);
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "htclow_mux_channel_impl.hpp"
|
||||
#include "../ctrl/htclow_ctrl_state_machine.hpp"
|
||||
#include "../htclow_default_channel_config.hpp"
|
||||
#include "../htclow_packet_factory.hpp"
|
||||
|
||||
namespace ams::htclow::mux {
|
||||
|
||||
@@ -228,6 +229,67 @@ namespace ams::htclow::mux {
|
||||
}
|
||||
}
|
||||
|
||||
Result ChannelImpl::DoConnectBegin(u32 *out_task_id) {
|
||||
/* Check our state. */
|
||||
R_TRY(this->CheckState({ChannelState_Connectable}));
|
||||
|
||||
/* Set ourselves as connecting. */
|
||||
m_state_machine->SetConnecting(m_channel);
|
||||
|
||||
/* Allocate a task. */
|
||||
u32 task_id;
|
||||
R_TRY(m_task_manager->AllocateTask(std::addressof(task_id), m_channel));
|
||||
|
||||
/* Configure the task. */
|
||||
m_task_manager->ConfigureConnectTask(task_id);
|
||||
|
||||
/* If we're ready, complete the task immediately. */
|
||||
if (m_state_machine->IsReadied()) {
|
||||
m_task_manager->CompleteTask(task_id, EventTrigger_ConnectReady);
|
||||
}
|
||||
|
||||
/* Set the output task id. */
|
||||
*out_task_id = task_id;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result ChannelImpl::DoConnectEnd() {
|
||||
/* Check our state. */
|
||||
R_TRY(this->CheckState({ChannelState_Connectable}));
|
||||
|
||||
/* Perform handshake, if we should. */
|
||||
if (m_config.handshake_enabled) {
|
||||
/* Set our next max data. */
|
||||
m_next_max_data = m_receive_buffer.GetBufferSize();
|
||||
|
||||
/* Make a max data packet. */
|
||||
auto packet = m_packet_factory->MakeMaxDataPacket(m_channel, m_version, m_next_max_data);
|
||||
R_UNLESS(packet, htclow::ResultOutOfMemory());
|
||||
|
||||
/* Send the packet. */
|
||||
m_send_buffer.AddPacket(std::move(packet));
|
||||
|
||||
/* Signal that we have an packet to send. */
|
||||
this->SignalSendPacketEvent();
|
||||
|
||||
/* Set our current max data. */
|
||||
m_cur_max_data = m_next_max_data;
|
||||
} else {
|
||||
/* Set our share. */
|
||||
m_share = m_config.initial_counter_max_data;
|
||||
|
||||
/* If we're not empty, signal. */
|
||||
if (!m_send_buffer.Empty()) {
|
||||
this->SignalSendPacketEvent();
|
||||
}
|
||||
}
|
||||
|
||||
/* Set our state as connected. */
|
||||
this->SetState(ChannelState_Connected);
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
void ChannelImpl::SetSendBuffer(void *buf, size_t buf_size, size_t max_packet_size) {
|
||||
/* Set buffer. */
|
||||
m_send_buffer.SetBuffer(buf, buf_size);
|
||||
|
||||
@@ -61,13 +61,17 @@ namespace ams::htclow::mux {
|
||||
|
||||
void RemovePacket(const PacketHeader &header);
|
||||
|
||||
void ShutdownForce();
|
||||
|
||||
void UpdateState();
|
||||
public:
|
||||
Result DoConnectBegin(u32 *out_task_id);
|
||||
Result DoConnectEnd();
|
||||
|
||||
void SetSendBuffer(void *buf, size_t buf_size, size_t max_packet_size);
|
||||
void SetReceiveBuffer(void *buf, size_t buf_size);
|
||||
void SetSendBufferWithData(const void *buf, size_t buf_size, size_t max_packet_size);
|
||||
private:
|
||||
void ShutdownForce();
|
||||
void SetState(ChannelState state);
|
||||
void SetStateWithoutCheck(ChannelState state);
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace ams::htclow::mux {
|
||||
}
|
||||
|
||||
/* Validate that the storage is free. */
|
||||
R_UNLESS(idx < MaxChannelCount, htclow::ResultOutOfResource());
|
||||
R_UNLESS(idx < MaxChannelCount, htclow::ResultOutOfChannel());
|
||||
|
||||
/* Create the channel impl. */
|
||||
std::construct_at(GetPointer(m_channel_storage[idx]), channel, m_packet_factory, m_state_machine, m_task_manager, m_event);
|
||||
@@ -67,4 +67,28 @@ namespace ams::htclow::mux {
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result ChannelImplMap::RemoveChannel(impl::ChannelInternalType channel) {
|
||||
/* Find the storage. */
|
||||
auto it = m_map.find(channel);
|
||||
AMS_ASSERT(it != m_map.end());
|
||||
|
||||
/* Get the channel index. */
|
||||
const auto index = it->second;
|
||||
AMS_ASSERT(0 <= index && index < MaxChannelCount);
|
||||
|
||||
/* Get the channel impl. */
|
||||
auto *channel_impl = GetPointer(m_channel_storage[index]);
|
||||
|
||||
/* Mark the storage as invalid. */
|
||||
m_storage_valid[index] = false;
|
||||
|
||||
/* Erase the channel from the map. */
|
||||
m_map.erase(channel);
|
||||
|
||||
/* Destroy the channel. */
|
||||
std::destroy_at(channel_impl);
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ namespace ams::htclow::mux {
|
||||
}
|
||||
|
||||
Result AddChannel(impl::ChannelInternalType channel);
|
||||
Result RemoveChannel(impl::ChannelInternalType channel);
|
||||
private:
|
||||
public:
|
||||
MapType &GetMap() {
|
||||
|
||||
@@ -41,6 +41,12 @@ namespace ams::htclow::mux {
|
||||
m_is_read_only = true;
|
||||
}
|
||||
|
||||
void RingBuffer::Clear() {
|
||||
m_data_size = 0;
|
||||
m_offset = 0;
|
||||
m_can_discard = false;
|
||||
}
|
||||
|
||||
Result RingBuffer::Write(const void *data, size_t size) {
|
||||
/* Validate pre-conditions. */
|
||||
AMS_ASSERT(!m_is_read_only);
|
||||
|
||||
@@ -33,6 +33,9 @@ namespace ams::htclow::mux {
|
||||
void Initialize(void *buffer, size_t buffer_size);
|
||||
void InitializeForReadOnly(const void *buffer, size_t buffer_size);
|
||||
|
||||
void Clear();
|
||||
|
||||
size_t GetBufferSize() { return m_buffer_size; }
|
||||
size_t GetDataSize() { return m_data_size; }
|
||||
|
||||
Result Write(const void *data, size_t size);
|
||||
|
||||
@@ -19,6 +19,11 @@
|
||||
|
||||
namespace ams::htclow::mux {
|
||||
|
||||
SendBuffer::~SendBuffer() {
|
||||
m_ring_buffer.Clear();
|
||||
this->Clear();
|
||||
}
|
||||
|
||||
bool SendBuffer::IsPriorPacket(PacketType packet_type) const {
|
||||
return packet_type == PacketType_MaxData;
|
||||
}
|
||||
@@ -94,6 +99,17 @@ namespace ams::htclow::mux {
|
||||
return true;
|
||||
}
|
||||
|
||||
void SendBuffer::AddPacket(std::unique_ptr<Packet, PacketDeleter> ptr) {
|
||||
/* Get the packet. */
|
||||
auto *packet = ptr.release();
|
||||
|
||||
/* Check the packet type. */
|
||||
AMS_ABORT_UNLESS(this->IsPriorPacket(packet->GetHeader()->packet_type));
|
||||
|
||||
/* Add the packet. */
|
||||
m_packet_list.push_back(*packet);
|
||||
}
|
||||
|
||||
void SendBuffer::RemovePacket(const PacketHeader &header) {
|
||||
/* Get the packet type. */
|
||||
const auto packet_type = header.packet_type;
|
||||
|
||||
@@ -45,11 +45,13 @@ namespace ams::htclow::mux {
|
||||
void CopyPacket(PacketHeader *header, PacketBody *body, int *out_body_size, const Packet &packet);
|
||||
public:
|
||||
SendBuffer(impl::ChannelInternalType channel, PacketFactory *pf);
|
||||
~SendBuffer();
|
||||
|
||||
void SetVersion(s16 version);
|
||||
|
||||
bool QueryNextPacket(PacketHeader *header, PacketBody *body, int *out_body_size, u64 max_data, u64 total_send_size, bool has_share, u64 share);
|
||||
|
||||
void AddPacket(std::unique_ptr<Packet, PacketDeleter> ptr);
|
||||
void RemovePacket(const PacketHeader &header);
|
||||
|
||||
void SetBuffer(void *buffer, size_t buffer_size);
|
||||
|
||||
@@ -26,6 +26,89 @@ namespace ams::htclow::mux {
|
||||
return std::addressof(m_tasks[task_id].event);
|
||||
}
|
||||
|
||||
EventTrigger TaskManager::GetTrigger(u32 task_id) {
|
||||
/* Check pre-conditions. */
|
||||
AMS_ASSERT(0 <= task_id && task_id < MaxTaskCount);
|
||||
AMS_ASSERT(m_valid[task_id]);
|
||||
|
||||
return m_tasks[task_id].event_trigger;
|
||||
}
|
||||
|
||||
Result TaskManager::AllocateTask(u32 *out_task_id, impl::ChannelInternalType channel) {
|
||||
/* Find a free task. */
|
||||
u32 task_id = 0;
|
||||
for (task_id = 0; task_id < util::size(m_tasks); ++task_id) {
|
||||
if (!m_valid[task_id]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Verify the task is free. */
|
||||
R_UNLESS(!m_valid[task_id], htclow::ResultOutOfTask());
|
||||
|
||||
/* Mark the task as allocated. */
|
||||
m_valid[task_id] = true;
|
||||
|
||||
/* Setup the task. */
|
||||
m_tasks[task_id].channel = channel;
|
||||
m_tasks[task_id].has_event_trigger = false;
|
||||
os::InitializeEvent(std::addressof(m_tasks[task_id].event), false, os::EventClearMode_ManualClear);
|
||||
|
||||
/* Return the task id. */
|
||||
*out_task_id = task_id;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
void TaskManager::FreeTask(u32 task_id) {
|
||||
/* Check pre-conditions. */
|
||||
AMS_ASSERT(0 <= task_id && task_id < MaxTaskCount);
|
||||
|
||||
/* Invalidate the task. */
|
||||
if (m_valid[task_id]) {
|
||||
os::FinalizeEvent(std::addressof(m_tasks[task_id].event));
|
||||
m_valid[task_id] = false;
|
||||
}
|
||||
}
|
||||
|
||||
void TaskManager::ConfigureConnectTask(u32 task_id) {
|
||||
/* Check pre-conditions. */
|
||||
AMS_ASSERT(0 <= task_id && task_id < MaxTaskCount);
|
||||
AMS_ASSERT(m_valid[task_id]);
|
||||
|
||||
/* Set the task type. */
|
||||
m_tasks[task_id].type = TaskType_Connect;
|
||||
}
|
||||
|
||||
void TaskManager::ConfigureFlushTask(u32 task_id) {
|
||||
/* Check pre-conditions. */
|
||||
AMS_ASSERT(0 <= task_id && task_id < MaxTaskCount);
|
||||
AMS_ASSERT(m_valid[task_id]);
|
||||
|
||||
/* Set the task type. */
|
||||
m_tasks[task_id].type = TaskType_Flush;
|
||||
}
|
||||
|
||||
void TaskManager::ConfigureReceiveTask(u32 task_id, size_t size) {
|
||||
/* Check pre-conditions. */
|
||||
AMS_ASSERT(0 <= task_id && task_id < MaxTaskCount);
|
||||
AMS_ASSERT(m_valid[task_id]);
|
||||
|
||||
/* Set the task type. */
|
||||
m_tasks[task_id].type = TaskType_Receive;
|
||||
|
||||
/* Set the task size. */
|
||||
m_tasks[task_id].size = size;
|
||||
}
|
||||
|
||||
void TaskManager::ConfigureSendTask(u32 task_id) {
|
||||
/* Check pre-conditions. */
|
||||
AMS_ASSERT(0 <= task_id && task_id < MaxTaskCount);
|
||||
AMS_ASSERT(m_valid[task_id]);
|
||||
|
||||
/* Set the task type. */
|
||||
m_tasks[task_id].type = TaskType_Send;
|
||||
}
|
||||
|
||||
void TaskManager::NotifyDisconnect(impl::ChannelInternalType channel) {
|
||||
for (auto i = 0; i < MaxTaskCount; ++i) {
|
||||
if (m_valid[i] && m_tasks[i].channel == channel) {
|
||||
|
||||
@@ -51,14 +51,23 @@ namespace ams::htclow::mux {
|
||||
public:
|
||||
TaskManager() : m_valid() { /* ... */ }
|
||||
|
||||
Result AllocateTask(u32 *out_task_id, impl::ChannelInternalType channel);
|
||||
void FreeTask(u32 task_id);
|
||||
|
||||
os::EventType *GetTaskEvent(u32 task_id);
|
||||
EventTrigger GetTrigger(u32 task_id);
|
||||
|
||||
void ConfigureConnectTask(u32 task_id);
|
||||
void ConfigureFlushTask(u32 task_id);
|
||||
void ConfigureReceiveTask(u32 task_id, size_t size);
|
||||
void ConfigureSendTask(u32 task_id);
|
||||
|
||||
void NotifyDisconnect(impl::ChannelInternalType channel);
|
||||
void NotifyReceiveData(impl::ChannelInternalType channel, size_t size);
|
||||
void NotifySendReady();
|
||||
void NotifySendBufferEmpty(impl::ChannelInternalType channel);
|
||||
void NotifyConnectReady();
|
||||
private:
|
||||
|
||||
void CompleteTask(int index, EventTrigger trigger);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user