Skip to content

Commit

Permalink
rel: Add support for console access over USB Gecko
Browse files Browse the repository at this point in the history
  • Loading branch information
PistonMiner committed Mar 27, 2021
1 parent 43560d3 commit 2870989
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ttyd-tools/rel/include/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ConsoleSystem
void drawLine(int line, const char *text, gc::color4 color = {0xff,0xff,0xff,0xff});

void updatePrompt();
void updateUsbGecko();
void processCommand(const char *text);
void disp();

Expand Down Expand Up @@ -127,6 +128,9 @@ class ConsoleSystem
char mPromptBuffer[64] = "";
int mBackspaceHoldTimer = 0;
Keyboard mKeyboard;

int mUgBufferSize = 0;
char mUgBuffer[64];
};

}
90 changes: 90 additions & 0 deletions ttyd-tools/rel/source/console.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "console.h"

#include "ug.h"
#include "util.h"

#include <ttyd/mario.h>
Expand Down Expand Up @@ -30,6 +31,10 @@ ConIntVar con_log_fade("con_log_fade", 1);
ConIntVar con_log_fade_start("con_log_fade_start", 3000);
ConIntVar con_log_fade_duration("con_log_fade_duration", 1000);

ConIntVar con_ug_mode("con_ug_mode", 2);
ConIntVar con_ug_chan("con_ug_chan", 1);
ConIntVar con_ug_send_block("con_ug_send_block", 1);

void CC_find(const char *args)
{
char filter[128] = "";
Expand Down Expand Up @@ -95,6 +100,7 @@ void ConsoleSystem::init()

void ConsoleSystem::update()
{
updateUsbGecko();
updatePrompt();

ttyd::dispdrv::CameraId cam_id;
Expand Down Expand Up @@ -180,6 +186,24 @@ void ConsoleSystem::logDebug(const char *fmt, ...)

void ConsoleSystem::logColor(const char *log_text, gc::color4 color)
{
// Send to USB Gecko if enabled
// We really don't want to probe every time we log, so we don't. If the UG
// is detached, the per-frame probe will switch back into standby
if (con_ug_mode.value == 1)
{
int chan = con_ug_chan.value;

const char *data_left = log_text;
int size_left = strlen(log_text);
while (size_left > 0)
{
int got = ugSend(chan, data_left, size_left);
if (got < 0)
break;
size_left -= got;
}
}

// Cut off until it fits
int num_lines = CountLines(log_text);
int max_lines = MOD_ARRAYSIZE(mLogLines);
Expand Down Expand Up @@ -350,6 +374,72 @@ void ConsoleSystem::updatePrompt()
}
}

void ConsoleSystem::updateUsbGecko()
{
if (!con_ug_mode.value)
return;

// Make sure there's a UG inserted
int chan = con_ug_chan.value;
if (!ugProbe(chan))
{
if (con_ug_mode.value == 1)
{
// Switch to standby to avoid sending random commands to EXI
// devices which might be attached after the UG
logWarning(
"Console UG enabled but none attached to slot %c, switching to standby\n",
'A' + chan
);
con_ug_mode.value = 2;
}
return;
}

// Receive as much as able
int size_left = MOD_ARRAYSIZE(mUgBuffer) - mUgBufferSize;
int got = ugRecv(chan, mUgBuffer + mUgBufferSize, size_left);
if (got < 0)
return;
mUgBufferSize += got;

// Process commands
while (true)
{
char *cmd_end = strchr(mUgBuffer, '\n');
if (!cmd_end)
break;

// Switch from standby to enabled when we receive a command
if (con_ug_mode.value == 2)
{
con_ug_mode.value = 1;
}
*cmd_end = '\0';
processCommand(mUgBuffer);

// Move up buffer
int cmd_size = cmd_end + 1 - mUgBuffer;
memmove(
mUgBuffer,
mUgBuffer + cmd_size,
mUgBufferSize - cmd_size
);
mUgBufferSize -= cmd_size;
}

// Check for overflow
if (mUgBufferSize == MOD_ARRAYSIZE(mUgBuffer))
{
// We could execute the truncated command but that seems worse than
// dropping the buffer. The unfortunate side effect is that we will
// probably execute the *back* part of the command as a new one, but
// that's probably better.
logError("Console UG buffer overflow, command dropped\n");
mUgBufferSize = 0;
}
}

void ConsoleSystem::processCommand(const char *text)
{
logInfo("] %s\n", text);
Expand Down

0 comments on commit 2870989

Please sign in to comment.