htc: free ourselves from the tyranny of numerical enums

This commit is contained in:
Michael Scire 2021-02-08 06:48:30 -08:00 committed by SciresM
parent 2341f18edd
commit e40eece74e
6 changed files with 131 additions and 87 deletions

View file

@ -31,19 +31,22 @@ namespace ams::htclow {
ChannelState_Connectable = 0,
ChannelState_Unconnectable = 1,
ChannelState_Connected = 2,
ChannelState_Shutdown = 3,
ChannelState_Disconnected = 3,
};
constexpr bool IsStateTransitionAllowed(ChannelState from, ChannelState to) {
switch (from) {
case ChannelState_Connectable:
return to == ChannelState_Unconnectable || to == ChannelState_Connected || to == ChannelState_Shutdown;
return to == ChannelState_Unconnectable ||
to == ChannelState_Connected ||
to == ChannelState_Disconnected;
case ChannelState_Unconnectable:
return to == ChannelState_Connectable || to == ChannelState_Shutdown;
return to == ChannelState_Connectable ||
to == ChannelState_Disconnected;
case ChannelState_Connected:
return to == ChannelState_Shutdown;
case ChannelState_Shutdown:
return to == ChannelState_Shutdown;
return to == ChannelState_Disconnected;
case ChannelState_Disconnected:
return to == ChannelState_Disconnected;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}

View file

@ -87,10 +87,10 @@ namespace ams::htclow::ctrl {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
if (m_state_machine->GetHtcctrlState() == HtcctrlState_7) {
R_TRY(this->SetState(HtcctrlState_8));
if (m_state_machine->GetHtcctrlState() == HtcctrlState_Sleep) {
R_TRY(this->SetState(HtcctrlState_ExitSleep));
} else {
R_TRY(this->SetState(HtcctrlState_0));
R_TRY(this->SetState(HtcctrlState_DriverConnected));
}
return ResultSuccess();
@ -100,10 +100,10 @@ namespace ams::htclow::ctrl {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
if (m_state_machine->GetHtcctrlState() == HtcctrlState_6) {
R_TRY(this->SetState(HtcctrlState_7));
if (m_state_machine->GetHtcctrlState() == HtcctrlState_EnterSleep) {
R_TRY(this->SetState(HtcctrlState_Sleep));
} else {
R_TRY(this->SetState(HtcctrlState_11));
R_TRY(this->SetState(HtcctrlState_DriverDisconnected));
}
return ResultSuccess();

View file

@ -19,57 +19,89 @@
namespace ams::htclow::ctrl {
enum HtcctrlState : u32 {
HtcctrlState_0 = 0,
HtcctrlState_1 = 1,
HtcctrlState_2 = 2,
HtcctrlState_3 = 3,
HtcctrlState_4 = 4,
HtcctrlState_5 = 5,
HtcctrlState_6 = 6,
HtcctrlState_7 = 7,
HtcctrlState_8 = 8,
HtcctrlState_9 = 9,
HtcctrlState_10 = 10,
HtcctrlState_11 = 11,
HtcctrlState_12 = 12,
HtcctrlState_DriverConnected = 0,
HtcctrlState_SentConnectFromHost = 1,
HtcctrlState_Connected = 2,
HtcctrlState_SentReadyFromHost = 3,
HtcctrlState_Ready = 4,
HtcctrlState_SentSuspendFromTarget = 5,
HtcctrlState_EnterSleep = 6,
HtcctrlState_Sleep = 7,
HtcctrlState_ExitSleep = 8,
HtcctrlState_SentResumeFromTarget = 9,
HtcctrlState_Disconnected = 10,
HtcctrlState_DriverDisconnected = 11,
HtcctrlState_Error = 12,
};
constexpr bool IsStateTransitionAllowed(HtcctrlState from, HtcctrlState to) {
switch (from) {
case HtcctrlState_0:
return to == HtcctrlState_1 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_1:
return to == HtcctrlState_2 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_2:
return to == HtcctrlState_3 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_3:
return to == HtcctrlState_4 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_4:
return to == HtcctrlState_5 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_5:
return to == HtcctrlState_6 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_6:
return to == HtcctrlState_7 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_7:
return to == HtcctrlState_8;
case HtcctrlState_8:
return to == HtcctrlState_9 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_9:
return to == HtcctrlState_4 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_10:
return to == HtcctrlState_1 || to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_11:
return to == HtcctrlState_0;
case HtcctrlState_12:
return to == HtcctrlState_10 || to == HtcctrlState_11 || to == HtcctrlState_12;
case HtcctrlState_DriverConnected:
return to == HtcctrlState_SentConnectFromHost ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_SentConnectFromHost:
return to == HtcctrlState_Connected ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_Connected:
return to == HtcctrlState_SentReadyFromHost ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_SentReadyFromHost:
return to == HtcctrlState_Ready ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_Ready:
return to == HtcctrlState_SentSuspendFromTarget ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_SentSuspendFromTarget:
return to == HtcctrlState_EnterSleep ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_EnterSleep:
return to == HtcctrlState_Sleep ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_Sleep:
return to == HtcctrlState_ExitSleep;
case HtcctrlState_ExitSleep:
return to == HtcctrlState_SentResumeFromTarget ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_SentResumeFromTarget:
return to == HtcctrlState_Ready ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_Disconnected:
return to == HtcctrlState_SentConnectFromHost ||
to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
case HtcctrlState_DriverDisconnected:
return to == HtcctrlState_DriverConnected;
case HtcctrlState_Error:
return to == HtcctrlState_Disconnected ||
to == HtcctrlState_DriverDisconnected ||
to == HtcctrlState_Error;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
constexpr bool IsDisconnected(HtcctrlState state) {
switch (state) {
case HtcctrlState_10:
case HtcctrlState_11:
case HtcctrlState_Disconnected:
case HtcctrlState_DriverDisconnected:
return true;
default:
return false;
@ -78,9 +110,9 @@ namespace ams::htclow::ctrl {
constexpr bool IsConnecting(HtcctrlState state) {
switch (state) {
case HtcctrlState_0:
case HtcctrlState_1:
case HtcctrlState_10:
case HtcctrlState_DriverConnected:
case HtcctrlState_SentConnectFromHost:
case HtcctrlState_Disconnected:
return true;
default:
return false;
@ -89,14 +121,14 @@ namespace ams::htclow::ctrl {
constexpr bool IsConnected(HtcctrlState state) {
switch (state) {
case HtcctrlState_2:
case HtcctrlState_3:
case HtcctrlState_4:
case HtcctrlState_5:
case HtcctrlState_6:
case HtcctrlState_7:
case HtcctrlState_8:
case HtcctrlState_9:
case HtcctrlState_Connected:
case HtcctrlState_SentReadyFromHost:
case HtcctrlState_Ready:
case HtcctrlState_SentSuspendFromTarget:
case HtcctrlState_EnterSleep:
case HtcctrlState_Sleep:
case HtcctrlState_ExitSleep:
case HtcctrlState_SentResumeFromTarget:
return true;
default:
return false;
@ -105,12 +137,12 @@ namespace ams::htclow::ctrl {
constexpr bool IsReadied(HtcctrlState state) {
switch (state) {
case HtcctrlState_4:
case HtcctrlState_5:
case HtcctrlState_6:
case HtcctrlState_7:
case HtcctrlState_8:
case HtcctrlState_9:
case HtcctrlState_Ready:
case HtcctrlState_SentSuspendFromTarget:
case HtcctrlState_EnterSleep:
case HtcctrlState_Sleep:
case HtcctrlState_ExitSleep:
case HtcctrlState_SentResumeFromTarget:
return true;
default:
return false;
@ -119,11 +151,11 @@ namespace ams::htclow::ctrl {
constexpr bool IsSleeping(HtcctrlState state) {
switch (state) {
case HtcctrlState_5:
case HtcctrlState_6:
case HtcctrlState_7:
case HtcctrlState_8:
case HtcctrlState_9:
case HtcctrlState_SentSuspendFromTarget:
case HtcctrlState_EnterSleep:
case HtcctrlState_Sleep:
case HtcctrlState_ExitSleep:
case HtcctrlState_SentResumeFromTarget:
return true;
default:
return false;

View file

@ -42,7 +42,7 @@ namespace ams::htclow::ctrl {
}
HtcctrlStateMachine::HtcctrlStateMachine() : m_map(), m_state(HtcctrlState_11), m_prev_state(HtcctrlState_11), m_mutex() {
HtcctrlStateMachine::HtcctrlStateMachine() : m_map(), m_state(HtcctrlState_DriverDisconnected), m_prev_state(HtcctrlState_DriverDisconnected), m_mutex() {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
@ -153,28 +153,25 @@ namespace ams::htclow::ctrl {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* TODO: What do these values mean? */
auto it = m_map.find(channel);
return it != m_map.end() && it->second._04 == 2 && it->second._00 == 2;
return it != m_map.end() && it->second.connect == ServiceChannelConnect_ConnectingChecked && it->second.support == ServiceChannelSupport_Unsupported;
}
bool HtcctrlStateMachine::IsConnectable(const impl::ChannelInternalType &channel) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* TODO: What do these values mean? */
auto it = m_map.find(channel);
return ctrl::IsConnected(m_state) && (!(it != m_map.end()) || it->second._04 != 2);
return ctrl::IsConnected(m_state) && (it == m_map.end() || it->second.connect != ServiceChannelConnect_ConnectingChecked);
}
void HtcctrlStateMachine::SetNotConnecting(const impl::ChannelInternalType &channel) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
/* TODO: What do these values mean? */
auto it = m_map.find(channel);
if (it != m_map.end() && it->second._04 != 2) {
it->second._04 = 0;
if (it != m_map.end() && it->second.connect != ServiceChannelConnect_ConnectingChecked) {
it->second.connect = ServiceChannelConnect_NotConnecting;
}
}

View file

@ -21,9 +21,21 @@ namespace ams::htclow::ctrl {
class HtcctrlStateMachine {
private:
enum ServiceChannelSupport {
ServiceChannelSupport_Unknown = 0,
ServiceChannelSupport_Suppported = 1,
ServiceChannelSupport_Unsupported = 2,
};
enum ServiceChannelConnect {
ServiceChannelConnect_NotConnecting = 0,
ServiceChannelConnect_Connecting = 1,
ServiceChannelConnect_ConnectingChecked = 2,
};
struct ServiceChannelState {
u32 _00;
u32 _04;
ServiceChannelSupport support;
ServiceChannelConnect connect;
};
static constexpr int MaxChannelCount = 10;

View file

@ -59,7 +59,7 @@ namespace ams::htclow::mux {
m_send_buffer.Clear();
/* Set our state to shutdown. */
this->SetState(ChannelState_Shutdown);
this->SetState(ChannelState_Disconnected);
}
void ChannelImpl::SetState(ChannelState state) {
@ -78,7 +78,7 @@ namespace ams::htclow::mux {
}
/* If relevant, notify disconnect. */
if (m_state == ChannelState_Shutdown) {
if (m_state == ChannelState_Disconnected) {
m_task_manager->NotifyDisconnect(m_channel);
}
}