#include <hpcmenu.h>
#include <console.h>
Console *Console::_instance = 0;
Console *
Console::Instance()
{
if (_instance == 0) {
_instance = new Console;
}
return (_instance);
}
Console::Console()
{
setBootConsole(BI_CNUSE_BUILTIN);
}
void
Console::Destroy()
{
if (_instance)
delete _instance;
_instance = 0;
}
void
Console::print(const TCHAR *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
wvsprintf(_bufw, fmt, ap);
va_end(ap);
HPC_MENU.print(_bufw);
}
SerialConsole::SerialConsole()
{
_handle = INVALID_HANDLE_VALUE;
setBootConsole(BI_CNUSE_SERIAL);
}
BOOL
SerialConsole::init()
{
if (_handle == INVALID_HANDLE_VALUE)
_handle = OpenCOM1();
if (_handle == INVALID_HANDLE_VALUE) {
Console::print(TEXT("couldn't open COM1\n"));
return (FALSE);
}
DCB dcb;
GetCommState(_handle, &dcb);
Console::print(
TEXT("BaudRate %d, ByteSize %#x, Parity %#x, StopBits %#x\n"),
dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
return (TRUE);
}
BOOL
SerialConsole::setupMultibyteBuffer()
{
size_t len = WideCharToMultiByte(CP_ACP, 0, _bufw, wcslen(_bufw),
0, 0, 0, 0);
if (len + 1 > CONSOLE_BUFSIZE)
return FALSE;
if (!WideCharToMultiByte(CP_ACP, 0, _bufw, len, _bufm, len, 0, 0))
return FALSE;
_bufm[len] = '\0';
return TRUE;
}
void
SerialConsole::print(const TCHAR *fmt, ...)
{
SETUP_WIDECHAR_BUFFER();
if (!setupMultibyteBuffer())
return;
genericPrint(_bufm);
}
HANDLE
SerialConsole::OpenCOM1()
{
static HANDLE COM1handle = INVALID_HANDLE_VALUE;
const char msg[] = "\r\n--------HPCBOOT--------\r\n";
unsigned long wrote;
int speed = HPC_PREFERENCE.serial_speed;
HANDLE h;
if (COM1handle != INVALID_HANDLE_VALUE)
return (COM1handle);
h = CreateFile(TEXT("COM1:"),
GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0,
NULL);
if (h == INVALID_HANDLE_VALUE)
return (h);
DCB dcb;
if (!GetCommState(h, &dcb))
goto bad;
dcb.BaudRate = speed;
if (!SetCommState(h, &dcb))
goto bad;
WriteFile(h, msg, sizeof msg, &wrote, 0);
COM1handle = h;
return (h);
bad:
CloseHandle(h);
return (INVALID_HANDLE_VALUE);
}
void
SerialConsole::genericPrint(const char *buf)
{
unsigned long wrote;
int i;
for (i = 0; *buf != '\0'; buf++) {
char c = *buf;
if (c == '\n')
WriteFile(_handle, "\r", 1, &wrote, 0);
WriteFile(_handle, &c, 1, &wrote, 0);
}
}