1*ce099b40Smartin /* -*-C++-*- $NetBSD: console.cpp,v 1.11 2008/04/28 20:23:20 martin Exp $ */
29173eae7Such
39173eae7Such /*-
42a2cc9f4Such * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
59173eae7Such * All rights reserved.
69173eae7Such *
79173eae7Such * This code is derived from software contributed to The NetBSD Foundation
89173eae7Such * by UCHIYAMA Yasushi.
99173eae7Such *
109173eae7Such * Redistribution and use in source and binary forms, with or without
119173eae7Such * modification, are permitted provided that the following conditions
129173eae7Such * are met:
139173eae7Such * 1. Redistributions of source code must retain the above copyright
149173eae7Such * notice, this list of conditions and the following disclaimer.
159173eae7Such * 2. Redistributions in binary form must reproduce the above copyright
169173eae7Such * notice, this list of conditions and the following disclaimer in the
179173eae7Such * documentation and/or other materials provided with the distribution.
189173eae7Such *
199173eae7Such * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209173eae7Such * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219173eae7Such * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229173eae7Such * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239173eae7Such * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249173eae7Such * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259173eae7Such * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269173eae7Such * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279173eae7Such * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289173eae7Such * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299173eae7Such * POSSIBILITY OF SUCH DAMAGE.
309173eae7Such */
319173eae7Such
329173eae7Such #include <hpcmenu.h>
339173eae7Such #include <console.h>
349173eae7Such
359173eae7Such Console *Console::_instance = 0;
369173eae7Such
37bd926f64Such //
38bd926f64Such // Display console
39bd926f64Such //
409173eae7Such Console *
Instance()419173eae7Such Console::Instance()
429173eae7Such {
432a2cc9f4Such
442a2cc9f4Such if (_instance == 0) {
459173eae7Such _instance = new Console;
462a2cc9f4Such }
472a2cc9f4Such
482a2cc9f4Such return (_instance);
499173eae7Such }
509173eae7Such
Console()51ed95b7ebSuch Console::Console()
52ed95b7ebSuch {
53ed95b7ebSuch // set default builtin console. (bicons)
54ed95b7ebSuch setBootConsole(BI_CNUSE_BUILTIN);
55ed95b7ebSuch }
56ed95b7ebSuch
579173eae7Such void
Destroy()589173eae7Such Console::Destroy()
599173eae7Such {
602a2cc9f4Such
619173eae7Such if (_instance)
629173eae7Such delete _instance;
639173eae7Such _instance = 0;
649173eae7Such }
659173eae7Such
669173eae7Such void
print(const TCHAR * fmt,...)679173eae7Such Console::print(const TCHAR *fmt, ...)
689173eae7Such {
699173eae7Such va_list ap;
702a2cc9f4Such
719173eae7Such va_start(ap, fmt);
72bd926f64Such wvsprintf(_bufw, fmt, ap);
739173eae7Such va_end(ap);
74bd926f64Such
75bd926f64Such // print to `Console Tab Window'
76bd926f64Such HPC_MENU.print(_bufw);
77bd926f64Such }
78bd926f64Such
79bd926f64Such //
80bd926f64Such // Serial console.
81bd926f64Such //
SerialConsole()82a934bba3Such SerialConsole::SerialConsole()
83a934bba3Such {
842a2cc9f4Such
85a934bba3Such _handle = INVALID_HANDLE_VALUE;
86a934bba3Such // set default serial console.
87a934bba3Such setBootConsole(BI_CNUSE_SERIAL);
88a934bba3Such }
89a934bba3Such
90bd926f64Such BOOL
init()91bd926f64Such SerialConsole::init()
92bd926f64Such {
93bd926f64Such // always open COM1 to supply clock and power for the
94bd926f64Such // sake of kernel serial driver
952a2cc9f4Such if (_handle == INVALID_HANDLE_VALUE)
962a2cc9f4Such _handle = OpenCOM1();
972a2cc9f4Such
982a2cc9f4Such if (_handle == INVALID_HANDLE_VALUE) {
992a2cc9f4Such Console::print(TEXT("couldn't open COM1\n"));
1002a2cc9f4Such return (FALSE);
1012a2cc9f4Such }
1022a2cc9f4Such
1032a2cc9f4Such // Print serial console status on LCD.
1042a2cc9f4Such DCB dcb;
1052a2cc9f4Such GetCommState(_handle, &dcb);
1062a2cc9f4Such Console::print(
1072a2cc9f4Such TEXT("BaudRate %d, ByteSize %#x, Parity %#x, StopBits %#x\n"),
1082a2cc9f4Such dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
1092a2cc9f4Such
1102a2cc9f4Such return (TRUE);
1119173eae7Such }
1129173eae7Such
1139173eae7Such BOOL
setupMultibyteBuffer()114bd926f64Such SerialConsole::setupMultibyteBuffer()
1159173eae7Such {
1169173eae7Such size_t len = WideCharToMultiByte(CP_ACP, 0, _bufw, wcslen(_bufw),
1179173eae7Such 0, 0, 0, 0);
1182a2cc9f4Such
1199173eae7Such if (len + 1 > CONSOLE_BUFSIZE)
1209173eae7Such return FALSE;
1219173eae7Such if (!WideCharToMultiByte(CP_ACP, 0, _bufw, len, _bufm, len, 0, 0))
1229173eae7Such return FALSE;
1239173eae7Such _bufm[len] = '\0';
1249173eae7Such
1259173eae7Such return TRUE;
1269173eae7Such }
1279173eae7Such
128bd926f64Such void
print(const TCHAR * fmt,...)129bd926f64Such SerialConsole::print(const TCHAR *fmt, ...)
130bd926f64Such {
1312a2cc9f4Such
132bd926f64Such SETUP_WIDECHAR_BUFFER();
133bd926f64Such
134bd926f64Such if (!setupMultibyteBuffer())
135bd926f64Such return;
136bd926f64Such
137bd926f64Such genericPrint(_bufm);
138bd926f64Such }
139bd926f64Such
1402a2cc9f4Such HANDLE
OpenCOM1()1412a2cc9f4Such SerialConsole::OpenCOM1()
1429173eae7Such {
1432a2cc9f4Such static HANDLE COM1handle = INVALID_HANDLE_VALUE;
1442a2cc9f4Such const char msg[] = "\r\n--------HPCBOOT--------\r\n";
1452a2cc9f4Such unsigned long wrote;
146bd926f64Such int speed = HPC_PREFERENCE.serial_speed;
1472a2cc9f4Such HANDLE h;
148c31816f7Such
1492a2cc9f4Such if (COM1handle != INVALID_HANDLE_VALUE)
1502a2cc9f4Such return (COM1handle);
1512a2cc9f4Such
1522a2cc9f4Such h = CreateFile(TEXT("COM1:"),
153fae3e8e7Such GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0,
154fae3e8e7Such NULL);
1552a2cc9f4Such if (h == INVALID_HANDLE_VALUE)
1562a2cc9f4Such return (h);
1579173eae7Such
1589173eae7Such DCB dcb;
1592a2cc9f4Such if (!GetCommState(h, &dcb))
1609173eae7Such goto bad;
1619173eae7Such
162c31816f7Such dcb.BaudRate = speed;
1632a2cc9f4Such if (!SetCommState(h, &dcb))
1649173eae7Such goto bad;
1659173eae7Such
1662a2cc9f4Such // Print banner on serial console.
1672a2cc9f4Such WriteFile(h, msg, sizeof msg, &wrote, 0);
1689173eae7Such
1692a2cc9f4Such COM1handle = h;
1702a2cc9f4Such
1712a2cc9f4Such return (h);
1729173eae7Such bad:
1732a2cc9f4Such CloseHandle(h);
1742a2cc9f4Such return (INVALID_HANDLE_VALUE);
1759173eae7Such }
176f0190d35Such
177f0190d35Such void
genericPrint(const char * buf)178f0190d35Such SerialConsole::genericPrint(const char *buf)
179f0190d35Such {
180f0190d35Such unsigned long wrote;
181f0190d35Such int i;
182f0190d35Such
183f0190d35Such for (i = 0; *buf != '\0'; buf++) {
184f0190d35Such char c = *buf;
185f0190d35Such if (c == '\n')
186f0190d35Such WriteFile(_handle, "\r", 1, &wrote, 0);
187f0190d35Such WriteFile(_handle, &c, 1, &wrote, 0);
188f0190d35Such }
189f0190d35Such }
190