tma: impl helper services, cleanup hostside packets
This commit is contained in:
@@ -190,7 +190,7 @@ class TmaPacket {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
TmaConnResult Read(const T &t) {
|
||||
TmaConnResult Read(T &t) {
|
||||
return Read(&t, sizeof(T));
|
||||
}
|
||||
|
||||
|
||||
@@ -34,5 +34,13 @@ static constexpr u32 HashServiceName(const char *name) {
|
||||
|
||||
enum class TmaService : u32 {
|
||||
Invalid = 0,
|
||||
|
||||
/* Special nodes, for facilitating connection over USB. */
|
||||
UsbQueryTarget = HashServiceName("USBQueryTarget"),
|
||||
UsbSendHostInfo = HashServiceName("USBSendHostInfo"),
|
||||
UsbConnect = HashServiceName("USBConnect"),
|
||||
UsbDisconnect = HashServiceName("USBDisconnect"),
|
||||
|
||||
|
||||
TestService = HashServiceName("AtmosphereTestService"), /* Temporary service, will be used to debug communications. */
|
||||
};
|
||||
|
||||
@@ -76,18 +76,54 @@ void TmaUsbConnection::RecvThreadFunc(void *arg) {
|
||||
this_ptr->SetConnected(true);
|
||||
|
||||
while (res == TmaConnResult::Success) {
|
||||
if (!this_ptr->IsConnected()) {
|
||||
break;
|
||||
}
|
||||
TmaPacket *packet = this_ptr->AllocateRecvPacket();
|
||||
if (packet == nullptr) { std::abort(); }
|
||||
|
||||
res = TmaUsbComms::ReceivePacket(packet);
|
||||
|
||||
if (res == TmaConnResult::Success) {
|
||||
TmaPacket *send_packet = this_ptr->AllocateSendPacket();
|
||||
send_packet->Write<u64>(i++);
|
||||
this_ptr->send_queue.Send(reinterpret_cast<uintptr_t>(send_packet));
|
||||
switch (packet->GetServiceId()) {
|
||||
case TmaService::UsbQueryTarget: {
|
||||
this_ptr->SetConnected(false);
|
||||
|
||||
res = this_ptr->SendQueryReply(packet);
|
||||
|
||||
if (!this_ptr->has_woken_up) {
|
||||
/* TODO: Cancel background work. */
|
||||
}
|
||||
}
|
||||
break;
|
||||
case TmaService::UsbSendHostInfo: {
|
||||
struct {
|
||||
u32 version;
|
||||
u32 sleeping;
|
||||
} host_info;
|
||||
packet->Read<decltype(host_info)>(host_info);
|
||||
|
||||
if (!this_ptr->has_woken_up || !host_info.sleeping) {
|
||||
/* TODO: Cancel background work. */
|
||||
}
|
||||
}
|
||||
break;
|
||||
case TmaService::UsbConnect: {
|
||||
res = this_ptr->SendQueryReply(packet);
|
||||
|
||||
if (res == TmaConnResult::Success) {
|
||||
this_ptr->SetConnected(true);
|
||||
this_ptr->OnConnectionEvent(ConnectionEvent::Connected);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case TmaService::UsbDisconnect: {
|
||||
this_ptr->SetConnected(false);
|
||||
this_ptr->OnDisconnected();
|
||||
|
||||
/* TODO: Cancel background work. */
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
this_ptr->FreePacket(packet);
|
||||
} else {
|
||||
this_ptr->FreePacket(packet);
|
||||
@@ -153,3 +189,13 @@ TmaConnResult TmaUsbConnection::SendPacket(TmaPacket *packet) {
|
||||
return TmaConnResult::Disconnected;
|
||||
}
|
||||
}
|
||||
|
||||
TmaConnResult TmaUsbConnection::SendQueryReply(TmaPacket *packet) {
|
||||
packet->ClearOffset();
|
||||
struct {
|
||||
u32 version;
|
||||
} target_info;
|
||||
target_info.version = 0;
|
||||
packet->Write<decltype(target_info)>(target_info);
|
||||
return TmaUsbComms::SendPacket(packet);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ class TmaUsbConnection : public TmaConnection {
|
||||
static void SendThreadFunc(void *arg);
|
||||
static void RecvThreadFunc(void *arg);
|
||||
static void OnUsbStateChange(void *this_ptr, u32 state);
|
||||
TmaConnResult SendQueryReply(TmaPacket *packet);
|
||||
void ClearSendQueue();
|
||||
void StartThreads();
|
||||
void StopThreads();
|
||||
|
||||
@@ -436,7 +436,7 @@ TmaConnResult TmaUsbComms::SendPacket(TmaPacket *packet) {
|
||||
res = TmaConnResult::GeneralFailure;
|
||||
}
|
||||
|
||||
if (res == TmaConnResult::Success) {
|
||||
if (res == TmaConnResult::Success && 0 < body_len) {
|
||||
/* Copy body to send buffer. */
|
||||
packet->CopyBodyTo(g_send_data_buf);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user