mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2025-01-03 11:11:14 +00:00
htc: implement mux side of connecting (and more)
This commit is contained in:
parent
70aae4e27a
commit
42cf3f50d7
24 changed files with 474 additions and 12 deletions
|
@ -37,6 +37,7 @@ namespace ams::htclow {
|
||||||
struct ChannelConfig {
|
struct ChannelConfig {
|
||||||
bool flow_control_enabled;
|
bool flow_control_enabled;
|
||||||
bool handshake_enabled;
|
bool handshake_enabled;
|
||||||
|
u64 initial_counter_max_data;
|
||||||
size_t max_packet_size;
|
size_t max_packet_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ namespace ams::htclow::ctrl {
|
||||||
}
|
}
|
||||||
|
|
||||||
HtcctrlService::HtcctrlService(HtcctrlPacketFactory *pf, HtcctrlStateMachine *sm, mux::Mux *mux)
|
HtcctrlService::HtcctrlService(HtcctrlPacketFactory *pf, HtcctrlStateMachine *sm, mux::Mux *mux)
|
||||||
: m_settings_holder(), m_beacon_response(), m_1100(), m_packet_factory(pf), m_state_machine(sm), m_mux(mux), m_event(os::EventClearMode_ManualClear),
|
: m_settings_holder(), m_beacon_response(), m_information_body(), m_packet_factory(pf), m_state_machine(sm), m_mux(mux), m_event(os::EventClearMode_ManualClear),
|
||||||
m_send_buffer(pf), m_mutex(), m_condvar(), m_service_channels_packet(), m_version(ProtocolVersion)
|
m_send_buffer(pf), m_mutex(), m_condvar(), m_service_channels_packet(), m_version(ProtocolVersion)
|
||||||
{
|
{
|
||||||
/* Lock ourselves. */
|
/* Lock ourselves. */
|
||||||
|
@ -78,6 +78,10 @@ namespace ams::htclow::ctrl {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HtcctrlService::UpdateInformationBody(const char *status) {
|
||||||
|
util::SNPrintf(m_information_body, sizeof(m_information_body), "{\r\n \"Status\" : \"%s\"\r\n}\r\n", status);
|
||||||
|
}
|
||||||
|
|
||||||
void HtcctrlService::SetDriverType(impl::DriverType driver_type) {
|
void HtcctrlService::SetDriverType(impl::DriverType driver_type) {
|
||||||
/* Lock ourselves. */
|
/* Lock ourselves. */
|
||||||
std::scoped_lock lk(m_mutex);
|
std::scoped_lock lk(m_mutex);
|
||||||
|
@ -333,6 +337,89 @@ namespace ams::htclow::ctrl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HtcctrlService::TryReady() {
|
||||||
|
/* Lock ourselves. */
|
||||||
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
||||||
|
this->TryReadyInternal();
|
||||||
|
}
|
||||||
|
|
||||||
|
void HtcctrlService::Disconnect() {
|
||||||
|
/* Lock ourselves. */
|
||||||
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
||||||
|
this->DisconnectInternal();
|
||||||
|
}
|
||||||
|
|
||||||
|
void HtcctrlService::Resume() {
|
||||||
|
/* Lock ourselves. */
|
||||||
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
||||||
|
/* Send resume packet, if we can. */
|
||||||
|
if (const auto state = m_state_machine->GetHtcctrlState(); state == HtcctrlState_Sleep || state == HtcctrlState_ExitSleep) {
|
||||||
|
/* Send a resume packet. */
|
||||||
|
m_send_buffer.AddPacket(m_packet_factory->MakeResumePacket());
|
||||||
|
|
||||||
|
/* Signal our event. */
|
||||||
|
m_event.Signal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void HtcctrlService::Suspend() {
|
||||||
|
/* Lock ourselves. */
|
||||||
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
||||||
|
/* If we can, perform a suspend. */
|
||||||
|
if (m_state_machine->GetHtcctrlState() == HtcctrlState_Ready) {
|
||||||
|
/* Send a suspend packet. */
|
||||||
|
m_send_buffer.AddPacket(m_packet_factory->MakeSuspendPacket());
|
||||||
|
|
||||||
|
/* Signal our event. */
|
||||||
|
m_event.Signal();
|
||||||
|
|
||||||
|
/* Wait for our state to transition. */
|
||||||
|
for (auto state = m_state_machine->GetHtcctrlState(); state == HtcctrlState_Ready || state == HtcctrlState_SentSuspendFromTarget; state = m_state_machine->GetHtcctrlState()) {
|
||||||
|
m_condvar.Wait(m_mutex);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* Otherwise, just disconnect. */
|
||||||
|
this->DisconnectInternal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void HtcctrlService::NotifyAwake() {
|
||||||
|
/* Lock ourselves. */
|
||||||
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
||||||
|
/* Update our information. */
|
||||||
|
this->UpdateInformationBody("Awake");
|
||||||
|
|
||||||
|
/* Send information to host. */
|
||||||
|
this->SendInformation();
|
||||||
|
}
|
||||||
|
|
||||||
|
void HtcctrlService::NotifyAsleep() {
|
||||||
|
/* Lock ourselves. */
|
||||||
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
||||||
|
/* Update our information. */
|
||||||
|
this->UpdateInformationBody("Asleep");
|
||||||
|
|
||||||
|
/* Send information to host. */
|
||||||
|
this->SendInformation();
|
||||||
|
}
|
||||||
|
|
||||||
|
void HtcctrlService::SendInformation() {
|
||||||
|
/* If we need information, send information. */
|
||||||
|
if (m_state_machine->IsInformationNeeded()) {
|
||||||
|
/* Send an information packet. */
|
||||||
|
m_send_buffer.AddPacket(m_packet_factory->MakeInformationPacket(m_information_body, util::Strnlen(m_information_body, sizeof(m_information_body)) + 1));
|
||||||
|
|
||||||
|
/* Signal our event. */
|
||||||
|
m_event.Signal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Result HtcctrlService::NotifyDriverConnected() {
|
Result HtcctrlService::NotifyDriverConnected() {
|
||||||
/* Lock ourselves. */
|
/* Lock ourselves. */
|
||||||
std::scoped_lock lk(m_mutex);
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
|
@ -42,7 +42,7 @@ namespace ams::htclow::ctrl {
|
||||||
private:
|
private:
|
||||||
SettingsHolder m_settings_holder;
|
SettingsHolder m_settings_holder;
|
||||||
char m_beacon_response[0x1000];
|
char m_beacon_response[0x1000];
|
||||||
u8 m_1100[0x1000];
|
char m_information_body[0x1000];
|
||||||
HtcctrlPacketFactory *m_packet_factory;
|
HtcctrlPacketFactory *m_packet_factory;
|
||||||
HtcctrlStateMachine *m_state_machine;
|
HtcctrlStateMachine *m_state_machine;
|
||||||
mux::Mux *m_mux;
|
mux::Mux *m_mux;
|
||||||
|
@ -56,6 +56,9 @@ namespace ams::htclow::ctrl {
|
||||||
const char *GetConnectionType(impl::DriverType driver_type) const;
|
const char *GetConnectionType(impl::DriverType driver_type) const;
|
||||||
|
|
||||||
void UpdateBeaconResponse(const char *connection);
|
void UpdateBeaconResponse(const char *connection);
|
||||||
|
void UpdateInformationBody(const char *status);
|
||||||
|
|
||||||
|
void SendInformation();
|
||||||
|
|
||||||
Result ProcessReceiveConnectPacket();
|
Result ProcessReceiveConnectPacket();
|
||||||
Result ProcessReceiveReadyPacket(const void *body, size_t body_size);
|
Result ProcessReceiveReadyPacket(const void *body, size_t body_size);
|
||||||
|
@ -72,10 +75,12 @@ namespace ams::htclow::ctrl {
|
||||||
void ProcessSendDisconnectPacket();
|
void ProcessSendDisconnectPacket();
|
||||||
|
|
||||||
void UpdateServiceChannels(const void *body, size_t body_size);
|
void UpdateServiceChannels(const void *body, size_t body_size);
|
||||||
void TryReadyInternal();
|
|
||||||
|
|
||||||
void PrintServiceChannels(char *dst, size_t dst_size);
|
void PrintServiceChannels(char *dst, size_t dst_size);
|
||||||
|
|
||||||
|
void TryReadyInternal();
|
||||||
|
void DisconnectInternal();
|
||||||
|
|
||||||
Result SetState(HtcctrlState state);
|
Result SetState(HtcctrlState state);
|
||||||
void ReflectState();
|
void ReflectState();
|
||||||
public:
|
public:
|
||||||
|
@ -91,6 +96,15 @@ namespace ams::htclow::ctrl {
|
||||||
bool QuerySendPacket(HtcctrlPacketHeader *header, HtcctrlPacketBody *body, int *out_body_size);
|
bool QuerySendPacket(HtcctrlPacketHeader *header, HtcctrlPacketBody *body, int *out_body_size);
|
||||||
void RemovePacket(const HtcctrlPacketHeader &header);
|
void RemovePacket(const HtcctrlPacketHeader &header);
|
||||||
|
|
||||||
|
void TryReady();
|
||||||
|
void Disconnect();
|
||||||
|
|
||||||
|
void Resume();
|
||||||
|
void Suspend();
|
||||||
|
|
||||||
|
void NotifyAwake();
|
||||||
|
void NotifyAsleep();
|
||||||
|
|
||||||
Result NotifyDriverConnected();
|
Result NotifyDriverConnected();
|
||||||
Result NotifyDriverDisconnected();
|
Result NotifyDriverDisconnected();
|
||||||
};
|
};
|
||||||
|
|
|
@ -57,6 +57,13 @@ namespace ams::htclow::ctrl {
|
||||||
return ResultSuccess();
|
return ResultSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool HtcctrlStateMachine::IsInformationNeeded() {
|
||||||
|
/* Lock ourselves. */
|
||||||
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
||||||
|
return !ctrl::IsDisconnected(m_state) && m_state != HtcctrlState_DriverConnected;
|
||||||
|
}
|
||||||
|
|
||||||
bool HtcctrlStateMachine::IsConnected() {
|
bool HtcctrlStateMachine::IsConnected() {
|
||||||
/* Lock ourselves. */
|
/* Lock ourselves. */
|
||||||
std::scoped_lock lk(m_mutex);
|
std::scoped_lock lk(m_mutex);
|
||||||
|
@ -149,6 +156,16 @@ namespace ams::htclow::ctrl {
|
||||||
return ctrl::IsConnected(m_state) && (it == m_map.end() || it->second.connect != ServiceChannelConnect_ConnectingChecked);
|
return ctrl::IsConnected(m_state) && (it == m_map.end() || it->second.connect != ServiceChannelConnect_ConnectingChecked);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HtcctrlStateMachine::SetConnecting(const impl::ChannelInternalType &channel) {
|
||||||
|
/* Lock ourselves. */
|
||||||
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
||||||
|
auto it = m_map.find(channel);
|
||||||
|
if (it != m_map.end() && it->second.connect != ServiceChannelConnect_ConnectingChecked) {
|
||||||
|
it->second.connect = ServiceChannelConnect_Connecting;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void HtcctrlStateMachine::SetNotConnecting(const impl::ChannelInternalType &channel) {
|
void HtcctrlStateMachine::SetNotConnecting(const impl::ChannelInternalType &channel) {
|
||||||
/* Lock ourselves. */
|
/* Lock ourselves. */
|
||||||
std::scoped_lock lk(m_mutex);
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
|
@ -58,6 +58,8 @@ namespace ams::htclow::ctrl {
|
||||||
bool IsConnectedStatusChanged();
|
bool IsConnectedStatusChanged();
|
||||||
bool IsSleepingStatusChanged();
|
bool IsSleepingStatusChanged();
|
||||||
|
|
||||||
|
bool IsInformationNeeded();
|
||||||
|
|
||||||
bool IsConnected();
|
bool IsConnected();
|
||||||
bool IsReadied();
|
bool IsReadied();
|
||||||
bool IsUnconnectable();
|
bool IsUnconnectable();
|
||||||
|
@ -68,6 +70,7 @@ namespace ams::htclow::ctrl {
|
||||||
bool IsUnsupportedServiceChannelToShutdown(const impl::ChannelInternalType &channel);
|
bool IsUnsupportedServiceChannelToShutdown(const impl::ChannelInternalType &channel);
|
||||||
bool IsConnectable(const impl::ChannelInternalType &channel);
|
bool IsConnectable(const impl::ChannelInternalType &channel);
|
||||||
|
|
||||||
|
void SetConnecting(const impl::ChannelInternalType &channel);
|
||||||
void SetNotConnecting(const impl::ChannelInternalType &channel);
|
void SetNotConnecting(const impl::ChannelInternalType &channel);
|
||||||
void SetConnectingChecked();
|
void SetConnectingChecked();
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,20 @@ namespace ams::htclow::driver {
|
||||||
return ResultSuccess();
|
return ResultSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DriverManager::CloseDriver() {
|
||||||
|
/* Lock ourselves. */
|
||||||
|
std::scoped_lock lk(m_mutex);
|
||||||
|
|
||||||
|
/* Clear our driver type. */
|
||||||
|
m_driver_type = std::nullopt;
|
||||||
|
|
||||||
|
/* Close our driver. */
|
||||||
|
if (m_open_driver != nullptr) {
|
||||||
|
m_open_driver->Close();
|
||||||
|
m_open_driver = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl::DriverType DriverManager::GetDriverType() {
|
impl::DriverType DriverManager::GetDriverType() {
|
||||||
/* Lock ourselves. */
|
/* Lock ourselves. */
|
||||||
std::scoped_lock lk(m_mutex);
|
std::scoped_lock lk(m_mutex);
|
||||||
|
@ -69,6 +83,10 @@ namespace ams::htclow::driver {
|
||||||
return m_open_driver;
|
return m_open_driver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DriverManager::Cancel() {
|
||||||
|
m_open_driver->CancelSendReceive();
|
||||||
|
}
|
||||||
|
|
||||||
void DriverManager::SetDebugDriver(IDriver *driver) {
|
void DriverManager::SetDebugDriver(IDriver *driver) {
|
||||||
m_debug_driver = driver;
|
m_debug_driver = driver;
|
||||||
m_driver_type = impl::DriverType::Debug;
|
m_driver_type = impl::DriverType::Debug;
|
||||||
|
|
|
@ -33,11 +33,14 @@ namespace ams::htclow::driver {
|
||||||
DriverManager() = default;
|
DriverManager() = default;
|
||||||
|
|
||||||
Result OpenDriver(impl::DriverType driver_type);
|
Result OpenDriver(impl::DriverType driver_type);
|
||||||
|
void CloseDriver();
|
||||||
|
|
||||||
impl::DriverType GetDriverType();
|
impl::DriverType GetDriverType();
|
||||||
|
|
||||||
IDriver *GetCurrentDriver();
|
IDriver *GetCurrentDriver();
|
||||||
|
|
||||||
|
void Cancel();
|
||||||
|
|
||||||
void SetDebugDriver(IDriver *driver);
|
void SetDebugDriver(IDriver *driver);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -20,9 +20,10 @@
|
||||||
namespace ams::htclow {
|
namespace ams::htclow {
|
||||||
|
|
||||||
constexpr inline const ChannelConfig DefaultChannelConfig = {
|
constexpr inline const ChannelConfig DefaultChannelConfig = {
|
||||||
.flow_control_enabled = true,
|
.flow_control_enabled = true,
|
||||||
.handshake_enabled = true,
|
.handshake_enabled = true,
|
||||||
.max_packet_size = 0xE000 + sizeof(PacketHeader),
|
.initial_counter_max_data = 0,
|
||||||
|
.max_packet_size = 0xE000 + sizeof(PacketHeader),
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,34 @@ namespace ams::htclow {
|
||||||
os::StartThread(std::addressof(m_listen_thread));
|
os::StartThread(std::addressof(m_listen_thread));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Listener::Cancel() {
|
||||||
|
/* Mark ourselves as cancelled. */
|
||||||
|
m_cancelled = true;
|
||||||
|
|
||||||
|
/* Cancel our worker. */
|
||||||
|
m_worker->Cancel();
|
||||||
|
|
||||||
|
/* Signal our event. */
|
||||||
|
m_event.Signal();
|
||||||
|
|
||||||
|
/* Cancel our driver. */
|
||||||
|
m_driver->CancelSendReceive();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Listener::Wait() {
|
||||||
|
/* Wait for our listen thread to exit. */
|
||||||
|
os::WaitThread(std::addressof(m_listen_thread));
|
||||||
|
|
||||||
|
/* Destroy our listen thread. */
|
||||||
|
os::DestroyThread(std::addressof(m_listen_thread));
|
||||||
|
|
||||||
|
/* Clear our driver. */
|
||||||
|
m_driver = nullptr;
|
||||||
|
|
||||||
|
/* Mark our thread as not running. */
|
||||||
|
m_thread_running = false;
|
||||||
|
}
|
||||||
|
|
||||||
void Listener::ListenThread() {
|
void Listener::ListenThread() {
|
||||||
/* Check pre-conditions. */
|
/* Check pre-conditions. */
|
||||||
AMS_ASSERT(m_driver != nullptr);
|
AMS_ASSERT(m_driver != nullptr);
|
||||||
|
|
|
@ -42,6 +42,8 @@ namespace ams::htclow {
|
||||||
Listener(mem::StandardAllocator *allocator, mux::Mux *mux, ctrl::HtcctrlService *ctrl_srv, Worker *worker);
|
Listener(mem::StandardAllocator *allocator, mux::Mux *mux, ctrl::HtcctrlService *ctrl_srv, Worker *worker);
|
||||||
|
|
||||||
void Start(driver::IDriver *driver);
|
void Start(driver::IDriver *driver);
|
||||||
|
void Cancel();
|
||||||
|
void Wait();
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,10 +56,9 @@ namespace ams::htclow {
|
||||||
void SetDriver(driver::IDriver *driver);
|
void SetDriver(driver::IDriver *driver);
|
||||||
|
|
||||||
void Start();
|
void Start();
|
||||||
|
void Cancel();
|
||||||
void Wait();
|
void Wait();
|
||||||
private:
|
private:
|
||||||
void Cancel();
|
|
||||||
|
|
||||||
Result ProcessReceive();
|
Result ProcessReceive();
|
||||||
Result ProcessSend();
|
Result ProcessSend();
|
||||||
|
|
||||||
|
|
|
@ -182,6 +182,58 @@ namespace ams::htclow::mux {
|
||||||
return ResultSuccess();
|
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) {
|
os::EventType *Mux::GetTaskEvent(u32 task_id) {
|
||||||
/* Lock ourselves. */
|
/* Lock ourselves. */
|
||||||
std::scoped_lock lk(m_mutex);
|
std::scoped_lock lk(m_mutex);
|
||||||
|
@ -189,6 +241,12 @@ namespace ams::htclow::mux {
|
||||||
return m_task_manager.GetTaskEvent(task_id);
|
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) {
|
void Mux::SetSendBuffer(impl::ChannelInternalType channel, void *buf, size_t buf_size, size_t max_packet_size) {
|
||||||
/* Lock ourselves. */
|
/* Lock ourselves. */
|
||||||
std::scoped_lock lk(m_mutex);
|
std::scoped_lock lk(m_mutex);
|
||||||
|
@ -225,4 +283,6 @@ namespace ams::htclow::mux {
|
||||||
m_channel_impl_map[it->second].SetReceiveBuffer(buf, buf_size);
|
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();
|
void UpdateMuxState();
|
||||||
public:
|
public:
|
||||||
Result Open(impl::ChannelInternalType channel);
|
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);
|
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 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 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);
|
void SetSendBufferWithData(impl::ChannelInternalType channel, const void *buf, size_t buf_size, size_t max_packet_size);
|
||||||
|
|
||||||
|
Result Shutdown(impl::ChannelInternalType channel);
|
||||||
private:
|
private:
|
||||||
Result CheckChannelExist(impl::ChannelInternalType channel);
|
Result CheckChannelExist(impl::ChannelInternalType channel);
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include "htclow_mux_channel_impl.hpp"
|
#include "htclow_mux_channel_impl.hpp"
|
||||||
#include "../ctrl/htclow_ctrl_state_machine.hpp"
|
#include "../ctrl/htclow_ctrl_state_machine.hpp"
|
||||||
#include "../htclow_default_channel_config.hpp"
|
#include "../htclow_default_channel_config.hpp"
|
||||||
|
#include "../htclow_packet_factory.hpp"
|
||||||
|
|
||||||
namespace ams::htclow::mux {
|
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) {
|
void ChannelImpl::SetSendBuffer(void *buf, size_t buf_size, size_t max_packet_size) {
|
||||||
/* Set buffer. */
|
/* Set buffer. */
|
||||||
m_send_buffer.SetBuffer(buf, buf_size);
|
m_send_buffer.SetBuffer(buf, buf_size);
|
||||||
|
|
|
@ -61,13 +61,17 @@ namespace ams::htclow::mux {
|
||||||
|
|
||||||
void RemovePacket(const PacketHeader &header);
|
void RemovePacket(const PacketHeader &header);
|
||||||
|
|
||||||
|
void ShutdownForce();
|
||||||
|
|
||||||
void UpdateState();
|
void UpdateState();
|
||||||
public:
|
public:
|
||||||
|
Result DoConnectBegin(u32 *out_task_id);
|
||||||
|
Result DoConnectEnd();
|
||||||
|
|
||||||
void SetSendBuffer(void *buf, size_t buf_size, size_t max_packet_size);
|
void SetSendBuffer(void *buf, size_t buf_size, size_t max_packet_size);
|
||||||
void SetReceiveBuffer(void *buf, size_t buf_size);
|
void SetReceiveBuffer(void *buf, size_t buf_size);
|
||||||
void SetSendBufferWithData(const void *buf, size_t buf_size, size_t max_packet_size);
|
void SetSendBufferWithData(const void *buf, size_t buf_size, size_t max_packet_size);
|
||||||
private:
|
private:
|
||||||
void ShutdownForce();
|
|
||||||
void SetState(ChannelState state);
|
void SetState(ChannelState state);
|
||||||
void SetStateWithoutCheck(ChannelState state);
|
void SetStateWithoutCheck(ChannelState state);
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ namespace ams::htclow::mux {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Validate that the storage is free. */
|
/* Validate that the storage is free. */
|
||||||
R_UNLESS(idx < MaxChannelCount, htclow::ResultOutOfResource());
|
R_UNLESS(idx < MaxChannelCount, htclow::ResultOutOfChannel());
|
||||||
|
|
||||||
/* Create the channel impl. */
|
/* Create the channel impl. */
|
||||||
std::construct_at(GetPointer(m_channel_storage[idx]), channel, m_packet_factory, m_state_machine, m_task_manager, m_event);
|
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();
|
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 AddChannel(impl::ChannelInternalType channel);
|
||||||
|
Result RemoveChannel(impl::ChannelInternalType channel);
|
||||||
private:
|
private:
|
||||||
public:
|
public:
|
||||||
MapType &GetMap() {
|
MapType &GetMap() {
|
||||||
|
|
|
@ -41,6 +41,12 @@ namespace ams::htclow::mux {
|
||||||
m_is_read_only = true;
|
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) {
|
Result RingBuffer::Write(const void *data, size_t size) {
|
||||||
/* Validate pre-conditions. */
|
/* Validate pre-conditions. */
|
||||||
AMS_ASSERT(!m_is_read_only);
|
AMS_ASSERT(!m_is_read_only);
|
||||||
|
|
|
@ -33,6 +33,9 @@ namespace ams::htclow::mux {
|
||||||
void Initialize(void *buffer, size_t buffer_size);
|
void Initialize(void *buffer, size_t buffer_size);
|
||||||
void InitializeForReadOnly(const 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; }
|
size_t GetDataSize() { return m_data_size; }
|
||||||
|
|
||||||
Result Write(const void *data, size_t size);
|
Result Write(const void *data, size_t size);
|
||||||
|
|
|
@ -19,6 +19,11 @@
|
||||||
|
|
||||||
namespace ams::htclow::mux {
|
namespace ams::htclow::mux {
|
||||||
|
|
||||||
|
SendBuffer::~SendBuffer() {
|
||||||
|
m_ring_buffer.Clear();
|
||||||
|
this->Clear();
|
||||||
|
}
|
||||||
|
|
||||||
bool SendBuffer::IsPriorPacket(PacketType packet_type) const {
|
bool SendBuffer::IsPriorPacket(PacketType packet_type) const {
|
||||||
return packet_type == PacketType_MaxData;
|
return packet_type == PacketType_MaxData;
|
||||||
}
|
}
|
||||||
|
@ -94,6 +99,17 @@ namespace ams::htclow::mux {
|
||||||
return true;
|
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) {
|
void SendBuffer::RemovePacket(const PacketHeader &header) {
|
||||||
/* Get the packet type. */
|
/* Get the packet type. */
|
||||||
const auto packet_type = header.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);
|
void CopyPacket(PacketHeader *header, PacketBody *body, int *out_body_size, const Packet &packet);
|
||||||
public:
|
public:
|
||||||
SendBuffer(impl::ChannelInternalType channel, PacketFactory *pf);
|
SendBuffer(impl::ChannelInternalType channel, PacketFactory *pf);
|
||||||
|
~SendBuffer();
|
||||||
|
|
||||||
void SetVersion(s16 version);
|
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);
|
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 RemovePacket(const PacketHeader &header);
|
||||||
|
|
||||||
void SetBuffer(void *buffer, size_t buffer_size);
|
void SetBuffer(void *buffer, size_t buffer_size);
|
||||||
|
|
|
@ -26,6 +26,89 @@ namespace ams::htclow::mux {
|
||||||
return std::addressof(m_tasks[task_id].event);
|
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) {
|
void TaskManager::NotifyDisconnect(impl::ChannelInternalType channel) {
|
||||||
for (auto i = 0; i < MaxTaskCount; ++i) {
|
for (auto i = 0; i < MaxTaskCount; ++i) {
|
||||||
if (m_valid[i] && m_tasks[i].channel == channel) {
|
if (m_valid[i] && m_tasks[i].channel == channel) {
|
||||||
|
|
|
@ -51,14 +51,23 @@ namespace ams::htclow::mux {
|
||||||
public:
|
public:
|
||||||
TaskManager() : m_valid() { /* ... */ }
|
TaskManager() : m_valid() { /* ... */ }
|
||||||
|
|
||||||
|
Result AllocateTask(u32 *out_task_id, impl::ChannelInternalType channel);
|
||||||
|
void FreeTask(u32 task_id);
|
||||||
|
|
||||||
os::EventType *GetTaskEvent(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 NotifyDisconnect(impl::ChannelInternalType channel);
|
||||||
void NotifyReceiveData(impl::ChannelInternalType channel, size_t size);
|
void NotifyReceiveData(impl::ChannelInternalType channel, size_t size);
|
||||||
void NotifySendReady();
|
void NotifySendReady();
|
||||||
void NotifySendBufferEmpty(impl::ChannelInternalType channel);
|
void NotifySendBufferEmpty(impl::ChannelInternalType channel);
|
||||||
void NotifyConnectReady();
|
void NotifyConnectReady();
|
||||||
private:
|
|
||||||
void CompleteTask(int index, EventTrigger trigger);
|
void CompleteTask(int index, EventTrigger trigger);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,8 @@ namespace ams::htclow {
|
||||||
R_DEFINE_ERROR_RESULT(ChannelAlreadyExist, 9);
|
R_DEFINE_ERROR_RESULT(ChannelAlreadyExist, 9);
|
||||||
R_DEFINE_ERROR_RESULT(ChannelNotExist, 10);
|
R_DEFINE_ERROR_RESULT(ChannelNotExist, 10);
|
||||||
|
|
||||||
R_DEFINE_ERROR_RESULT(OutOfResource, 151);
|
R_DEFINE_ERROR_RESULT(OutOfChannel, 151);
|
||||||
|
R_DEFINE_ERROR_RESULT(OutOfTask, 151);
|
||||||
|
|
||||||
R_DEFINE_ERROR_RESULT(InvalidChannelState, 200);
|
R_DEFINE_ERROR_RESULT(InvalidChannelState, 200);
|
||||||
R_DEFINE_ERROR_RESULT(InvalidChannelStateDisconnected, 201);
|
R_DEFINE_ERROR_RESULT(InvalidChannelStateDisconnected, 201);
|
||||||
|
|
Loading…
Reference in a new issue