tma: impl helper services, cleanup hostside packets

This commit is contained in:
Michael Scire
2018-11-06 20:20:07 -08:00
parent 46001263f8
commit 2572ae8378
10 changed files with 252 additions and 47 deletions

View File

@@ -190,7 +190,7 @@ class TmaPacket {
}
template<typename T>
TmaConnResult Read(const T &t) {
TmaConnResult Read(T &t) {
return Read(&t, sizeof(T));
}

View File

@@ -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. */
};

View File

@@ -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);
}

View File

@@ -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();

View File

@@ -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);