1 | /*
|
---|
2 | * TOPPERS/JSP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Just Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
8 | *
|
---|
9 | * ä¸è¨è使¨©è
|
---|
10 | ã¯ï¼ä»¥ä¸ã® (1)ã(4) ã®æ¡ä»¶ãï¼Free Software Foundation
|
---|
11 | * ã«ãã£ã¦å
|
---|
12 | ¬è¡¨ããã¦ãã GNU General Public License ã® Version 2 ã«è¨
|
---|
13 | * è¿°ããã¦ããæ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§ã¢ï¼æ¬ã½ããã¦ã§ã¢
|
---|
14 | * ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹å¤ã»åé
|
---|
15 | å¸ï¼ä»¥ä¸ï¼
|
---|
16 | * å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
17 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
18 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
19 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
20 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
21 | * ç¨ã§ããå½¢ã§åé
|
---|
22 | å¸ããå ´åã«ã¯ï¼åé
|
---|
23 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
24 | * è
|
---|
25 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è使¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
26 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
27 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
28 | * ç¨ã§ããªãå½¢ã§åé
|
---|
29 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
30 | * ã¨ï¼
|
---|
31 | * (a) åé
|
---|
32 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
33 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
34 | * 使¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
35 | * (b) åé
|
---|
36 | å¸ã®å½¢æ
|
---|
37 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
38 | * å ±åãããã¨ï¼
|
---|
39 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
40 | * 害ãããï¼ä¸è¨è使¨©è
|
---|
41 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
42 | 責ãããã¨ï¼
|
---|
43 | *
|
---|
44 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨è使¨©è
|
---|
45 | ã
|
---|
46 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ãã®é©ç¨å¯è½æ§ã
|
---|
47 | * å«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´
|
---|
48 | * æ¥çã¾ãã¯éæ¥çã«çãããããªãæå®³ã«é¢ãã¦ãï¼ãã®è²¬ä»»ãè² ããªãï¼
|
---|
49 | *
|
---|
50 | * @(#) $Id: hw_serial.c,v 1.13 2003/12/15 07:19:22 takayuki Exp $
|
---|
51 | */
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 | #include <hw_serial.h>
|
---|
56 | #include <hal_msg.h>
|
---|
57 | #include <t_services.h>
|
---|
58 |
|
---|
59 | #include <resource.h>
|
---|
60 | #include <debugout.h>
|
---|
61 |
|
---|
62 | #define BUFSZ_UPPERBOUND 24*1024 /* ã³ã³ã½ã¼ã«ãããã¡ã®å·»æ»ãåºæºãµã¤ãº (ãããè¶
|
---|
63 | ããã¨å·»ãæ»ã) (ã©ããã30000ãè¶
|
---|
64 | ããããªãããã)*/
|
---|
65 | #define BUFSZ_LOWERBOUND 8*1024 /* å·»ãæ»ããã¨ãã«ã©ã®ãããå·»ãæ»ãã (UPPERBOUNDãããå°ããæ°)*/
|
---|
66 |
|
---|
67 | #define ID_PORT(x) ((x) + 1)
|
---|
68 | #define INDEX_PORT(x) ((x) - 1)
|
---|
69 | #define GET_SIOPCB(x) (&siopcb_table[INDEX_PORT(x)])
|
---|
70 |
|
---|
71 | #define BITTEST(x,y) ( ( (x) & (y) ) != 0)
|
---|
72 | #define BITSET(x,y) InterlockedExchange( &(x), (x) | (y) )
|
---|
73 | #define BITCLEAR(x,y) InterlockedExchange( &(x), (x) & ~(y) )
|
---|
74 |
|
---|
75 | extern HINSTANCE ProcessInstance;
|
---|
76 | extern HANDLE PrimaryThreadHandle;
|
---|
77 |
|
---|
78 | /* ã·ãªã¢ã«å¶å¾¡ããã㯠*/
|
---|
79 | SIOPCB siopcb_table[TNUM_PORT];
|
---|
80 |
|
---|
81 |
|
---|
82 | /* è´å½çãªã¨ã©ã¼çºçæç¨ã¢ãµã¼ã */
|
---|
83 | extern void FatalAssertion(int exp, LPCSTR format, ... );
|
---|
84 |
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * ã·ãªã¢ã«I/Oå
|
---|
88 | ±éé¨ã¤ã³ã¿ãã§ã¼ã¹
|
---|
89 | */
|
---|
90 |
|
---|
91 | /*===========================================================================*/
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * ã³ã³ã½ã¼ã«åã·ãªã¢ã«
|
---|
95 | */
|
---|
96 |
|
---|
97 | #define MAX_CONSOLE_BUFSZ 2048
|
---|
98 | #define SERMSG_CREATE WM_APP
|
---|
99 | #define SERMSG_UPDATE (WM_APP+1)
|
---|
100 |
|
---|
101 | struct tagSerialConsoleParameters
|
---|
102 | {
|
---|
103 | CRITICAL_SECTION cs;
|
---|
104 | unsigned int position;
|
---|
105 | char buffer[MAX_CONSOLE_BUFSZ];
|
---|
106 | };
|
---|
107 |
|
---|
108 |
|
---|
109 | static LRESULT CALLBACK KeyEventTrapper(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
|
---|
110 | {
|
---|
111 | SIOPCB * scope;
|
---|
112 |
|
---|
113 | /* WM_CHARãããã¯ãã */
|
---|
114 | if(Msg == WM_CHAR)
|
---|
115 | {
|
---|
116 | scope = (SIOPCB *)GetWindowLong(GetParent(hWnd),GWL_USERDATA);
|
---|
117 | PostMessage(scope->Handle,WM_CHAR,wParam,lParam);
|
---|
118 | return TRUE;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* ãã¨ãã¨ã®WndProcãå¼ã³ãªãã */
|
---|
122 | return CallWindowProc((void *)GetWindowLong(hWnd,GWL_USERDATA),hWnd,Msg,wParam,lParam);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static void SelectConsoleFont(HWND console, UINT pixel)
|
---|
126 | {
|
---|
127 | HANDLE oldfont;
|
---|
128 | HANDLE newfont;
|
---|
129 | LOGFONT logfont;
|
---|
130 | HDC hDC;
|
---|
131 |
|
---|
132 | /* DCåå¾ */
|
---|
133 | hDC = GetDC(console);
|
---|
134 |
|
---|
135 | /* ä»ã®ãã©ã³ãæ
|
---|
136 | å ±ãåå¾ãã */
|
---|
137 | oldfont = (HANDLE) SendMessage(console, WM_GETFONT, 0, 0);
|
---|
138 | GetObject(oldfont, sizeof(LOGFONT), &logfont);
|
---|
139 |
|
---|
140 | /* ãã¤ã³ãã夿´ */
|
---|
141 | logfont.lfHeight = -MulDiv(pixel, GetDeviceCaps(hDC, LOGPIXELSY), 72);
|
---|
142 |
|
---|
143 | /* æ´æ°ãããã©ã³ãæ
|
---|
144 | å ±ãå
|
---|
145 | ã«ãæ°ãããã©ã³ããçæãã¦é¸æ */
|
---|
146 | newfont = CreateFontIndirect(&logfont);
|
---|
147 | SendMessage(console, WM_SETFONT, (WPARAM)newfont, MAKELPARAM(TRUE,0));
|
---|
148 |
|
---|
149 | /* åã®ãã©ã³ããç ´æ£ */
|
---|
150 | DeleteObject(oldfont);
|
---|
151 |
|
---|
152 | /* DCè§£æ¾ */
|
---|
153 | ReleaseDC(console, hDC);
|
---|
154 | }
|
---|
155 |
|
---|
156 | static LRESULT ConsoleCommandHandler(HWND hDlg, UINT wID, UINT wNotifyCode, LPARAM lParam)
|
---|
157 | {
|
---|
158 | BOOL result;
|
---|
159 | HANDLE console;
|
---|
160 |
|
---|
161 | result = TRUE;
|
---|
162 | console = GetDlgItem(hDlg, IDC_CONSOLE);
|
---|
163 |
|
---|
164 | switch(wID)
|
---|
165 | {
|
---|
166 | /*
|
---|
167 | * ãã©ã³ãã®å¤§ããã夿´ãã
|
---|
168 | */
|
---|
169 | case ID_FONT_BIG:
|
---|
170 | SelectConsoleFont(console, 16);
|
---|
171 | break;
|
---|
172 | case ID_FONT_NORMAL:
|
---|
173 | SelectConsoleFont(console, 9);
|
---|
174 | break;
|
---|
175 | case ID_FONT_SMALL:
|
---|
176 | SelectConsoleFont(console, 4);
|
---|
177 | break;
|
---|
178 |
|
---|
179 | default:
|
---|
180 | result = FALSE;
|
---|
181 | }
|
---|
182 |
|
---|
183 | return result;
|
---|
184 | }
|
---|
185 |
|
---|
186 | static BOOL CALLBACK ConsoleProc(HWND hDlg,UINT Msg,WPARAM wParam,LPARAM lParam)
|
---|
187 | {
|
---|
188 | switch(Msg)
|
---|
189 | {
|
---|
190 | case SERMSG_CREATE:
|
---|
191 | {
|
---|
192 | SIOPCB * work;
|
---|
193 | void * DefWndProc;
|
---|
194 | HWND hConsole;
|
---|
195 |
|
---|
196 | hConsole = GetDlgItem(hDlg, IDC_CONSOLE);
|
---|
197 |
|
---|
198 | /* ããã¹ãããã¯ã¹ã®WndProcã«ããã¯ãããã */
|
---|
199 | DefWndProc = (void *)GetWindowLong(hConsole,GWL_WNDPROC);
|
---|
200 | SetWindowLong(hConsole, GWL_USERDATA, (LONG) DefWndProc);
|
---|
201 |
|
---|
202 | work = (SIOPCB *)lParam;
|
---|
203 | SetWindowLong(hDlg,GWL_USERDATA,lParam);
|
---|
204 | SetWindowLong(hConsole, GWL_WNDPROC, (long)KeyEventTrapper);
|
---|
205 |
|
---|
206 | SetTimer(hDlg, 100, 300, NULL);
|
---|
207 |
|
---|
208 | ShowWindow(hDlg,SW_SHOWNA);
|
---|
209 | break;
|
---|
210 | }
|
---|
211 |
|
---|
212 | case WM_DESTROY:
|
---|
213 | {
|
---|
214 | SIOPCB * scope;
|
---|
215 | KillTimer(hDlg, 100);
|
---|
216 | scope = (SIOPCB *)GetWindowLong(hDlg,GWL_USERDATA);
|
---|
217 | if(scope != 0)
|
---|
218 | scope->Handle = NULL;
|
---|
219 | break;
|
---|
220 | }
|
---|
221 |
|
---|
222 | case WM_CLOSE: /* ã¦ã¼ã¶ã«ããã¦ã£ã³ãã¦ã¯ãã¼ãºãææ¢ */
|
---|
223 | break;
|
---|
224 |
|
---|
225 | case WM_CHAR:
|
---|
226 | {
|
---|
227 | SIOPCB * scope;
|
---|
228 | scope = (SIOPCB *)GetWindowLong(hDlg,GWL_USERDATA);
|
---|
229 | if(scope != 0)
|
---|
230 | {
|
---|
231 | scope->ReceiveBuffer = (char)wParam;
|
---|
232 | BITSET(scope->Flag, SIO_STA_INTRCV);
|
---|
233 | HALInterruptRequest(INHNO_SERIAL);
|
---|
234 | }
|
---|
235 | break;
|
---|
236 | }
|
---|
237 |
|
---|
238 | case WM_SETFONT:
|
---|
239 | return TRUE;
|
---|
240 |
|
---|
241 | case WM_INITDIALOG:
|
---|
242 | case WM_SIZE:
|
---|
243 | {
|
---|
244 | RECT client;
|
---|
245 | GetClientRect(hDlg,&client);
|
---|
246 | MoveWindow(GetDlgItem(hDlg,IDC_CONSOLE),0,0,client.right,client.bottom,TRUE);
|
---|
247 | break;
|
---|
248 | }
|
---|
249 |
|
---|
250 | case WM_COMMAND:
|
---|
251 | return ConsoleCommandHandler(hDlg, LOWORD(wParam), HIWORD(wParam), lParam);
|
---|
252 |
|
---|
253 | //ä¸å®æéãã£ããæ¹è¡ãããªãã¦ãåºåãã
|
---|
254 | case WM_TIMER:
|
---|
255 | if(wParam == 100)
|
---|
256 | {
|
---|
257 | SIOPCB * scope;
|
---|
258 | struct tagSerialConsoleParameters * param;
|
---|
259 |
|
---|
260 | scope = (SIOPCB *)GetWindowLong(hDlg, GWL_USERDATA);
|
---|
261 | if(scope != 0) {
|
---|
262 | param = (struct tagSerialConsoleParameters *)scope->versatile;
|
---|
263 | if(param->position == 0)
|
---|
264 | break;
|
---|
265 |
|
---|
266 | lParam = TRUE;
|
---|
267 | }
|
---|
268 | else
|
---|
269 | break; //ã¾ã åæåãçµãã£ã¦ãªãã®ã§ä½ãããªã
|
---|
270 | }
|
---|
271 |
|
---|
272 | //lParam : éä¿¡ãçµãã£ãå¾ã«å²è¾¼ã¿ãããããã©ãã (FALSE:ãããªã TRUE:ããã)
|
---|
273 | case SERMSG_UPDATE:
|
---|
274 | {
|
---|
275 | LRESULT result;
|
---|
276 | int textlength;
|
---|
277 | HANDLE console;
|
---|
278 | SIOPCB * scope;
|
---|
279 | struct tagSerialConsoleParameters * param;
|
---|
280 |
|
---|
281 | scope = (SIOPCB *)GetWindowLong(hDlg, GWL_USERDATA);
|
---|
282 | if(scope != 0) {
|
---|
283 | param = (struct tagSerialConsoleParameters *)scope->versatile;
|
---|
284 |
|
---|
285 | console = GetDlgItem(hDlg, IDC_CONSOLE);
|
---|
286 |
|
---|
287 | textlength = GetWindowTextLength(console);
|
---|
288 | if(textlength > BUFSZ_UPPERBOUND)
|
---|
289 | {
|
---|
290 | /* å¤ãæ
|
---|
291 | å ±ãæ¶ã */
|
---|
292 | SendMessage(console,EM_SETSEL,0,textlength - BUFSZ_LOWERBOUND);
|
---|
293 | SendMessage(console,EM_REPLACESEL,(WPARAM)FALSE,(LPARAM)"");
|
---|
294 | textlength = GetWindowTextLength(console);
|
---|
295 | }
|
---|
296 | /* æ«å°¾ã«æåãç½®ã */
|
---|
297 | result = SendMessage(console,EM_SETSEL,textlength,textlength);
|
---|
298 |
|
---|
299 | EnterCriticalSection(¶m->cs);
|
---|
300 | param->buffer[param->position] = '\x0';
|
---|
301 | result = SendMessage(console,EM_REPLACESEL,(WPARAM)FALSE,(LPARAM)param->buffer);
|
---|
302 | param->position = 0;
|
---|
303 | LeaveCriticalSection(¶m->cs);
|
---|
304 |
|
---|
305 | if(lParam == TRUE)
|
---|
306 | {
|
---|
307 | BITSET(scope->Flag, SIO_STA_INTSND);
|
---|
308 | HALInterruptRequest(INHNO_SERIAL);
|
---|
309 | }
|
---|
310 | }
|
---|
311 | else
|
---|
312 | PostMessage(hDlg, Msg, wParam, lParam); //ã¾ã åæåãçµãã£ã¦ãªãã®ã§æ«å°¾ã«ã¤ããªãã
|
---|
313 |
|
---|
314 | break;
|
---|
315 | }
|
---|
316 |
|
---|
317 | default:
|
---|
318 | return FALSE;
|
---|
319 | }
|
---|
320 |
|
---|
321 | return TRUE;
|
---|
322 | }
|
---|
323 |
|
---|
324 | static void SerialConsole_FinalRelease(void * param)
|
---|
325 | {
|
---|
326 | SIOPCB * cb = (SIOPCB *)param;
|
---|
327 | struct tagSerialConsoleParameters * versatile;
|
---|
328 |
|
---|
329 | if(cb->Handle != 0l && cb->Handle != NULL)
|
---|
330 | DestroyWindow(cb->Handle);
|
---|
331 | cb->Handle = NULL;
|
---|
332 |
|
---|
333 | versatile = cb->versatile;
|
---|
334 | DeleteCriticalSection(&versatile->cs);
|
---|
335 | GlobalFree((HGLOBAL)versatile);
|
---|
336 | }
|
---|
337 |
|
---|
338 | static void CreateSerialConsole(SIOPCB * cb)
|
---|
339 | {
|
---|
340 | struct tagSerialConsoleParameters * param;
|
---|
341 |
|
---|
342 | param = GlobalAlloc(GMEM_FIXED, sizeof(struct tagSerialConsoleParameters));
|
---|
343 | FatalAssertion(param != NULL, "CreateSerialConsole: GlobalAlloc reported NULL.");
|
---|
344 | param->position = 0;
|
---|
345 | InitializeCriticalSection(¶m->cs);
|
---|
346 | cb->versatile = param;
|
---|
347 |
|
---|
348 | cb->Handle = CreateDialog(ProcessInstance, MAKEINTRESOURCE(CONSOLEDIALOG), 0, ConsoleProc);
|
---|
349 | FatalAssertion(cb->Handle != 0, "CreateSerialConsole could not create its dialog.");
|
---|
350 | PostMessage(cb->Handle,SERMSG_CREATE,0,(LPARAM)cb);
|
---|
351 | UpdateWindow(cb->Handle);
|
---|
352 | HALAddDestructionProcedure(SerialConsole_FinalRelease,cb);
|
---|
353 | }
|
---|
354 |
|
---|
355 | static BOOL SerialConsole_PutChar(SIOPCB * cb, INT chr, BOOL rasint)
|
---|
356 | {
|
---|
357 | char buffer[2];
|
---|
358 | int textlength;
|
---|
359 | HANDLE console;
|
---|
360 |
|
---|
361 | assert(cb != NULL);
|
---|
362 | assert(BITTEST(cb->Flag, SIO_TYP_CONSOLE));
|
---|
363 | assert(cb->Handle != NULL);
|
---|
364 |
|
---|
365 | console = GetDlgItem(cb->Handle, IDC_CONSOLE);
|
---|
366 | buffer[0] = (char) chr;
|
---|
367 | buffer[1] = '\x0';
|
---|
368 |
|
---|
369 | textlength = GetWindowTextLength(console);
|
---|
370 | if(textlength > BUFSZ_UPPERBOUND)
|
---|
371 | {
|
---|
372 | /* å¤ãæ
|
---|
373 | å ±ãæ¶ã */
|
---|
374 | SendMessage(console,EM_SETSEL,0,textlength - BUFSZ_LOWERBOUND);
|
---|
375 | SendMessage(console,EM_REPLACESEL,(WPARAM)FALSE,(LPARAM)"");
|
---|
376 | textlength = GetWindowTextLength(console);
|
---|
377 | }
|
---|
378 | /* æ«å°¾ã«æåãç½®ã */
|
---|
379 | SendMessage(console,EM_SETSEL,textlength,textlength);
|
---|
380 | SendMessage(console,EM_REPLACESEL,(WPARAM)FALSE,(LPARAM)buffer);
|
---|
381 |
|
---|
382 | if(rasint == TRUE)
|
---|
383 | {
|
---|
384 | BITSET(cb->Flag, SIO_STA_INTSND);
|
---|
385 | HALInterruptRequest(INHNO_SERIAL);
|
---|
386 | }
|
---|
387 |
|
---|
388 | return TRUE;
|
---|
389 | }
|
---|
390 |
|
---|
391 | static BOOL SerialConsole_PushChar(SIOPCB * cb, INT chr)
|
---|
392 | {
|
---|
393 | BOOL success;
|
---|
394 | BOOL update;
|
---|
395 | struct tagSerialConsoleParameters * param;
|
---|
396 |
|
---|
397 | param = (struct tagSerialConsoleParameters *)cb->versatile;
|
---|
398 |
|
---|
399 | do {
|
---|
400 | success = FALSE;
|
---|
401 | update = FALSE;
|
---|
402 |
|
---|
403 | EnterCriticalSection(¶m->cs);
|
---|
404 |
|
---|
405 | if(param->position < MAX_CONSOLE_BUFSZ)
|
---|
406 | {
|
---|
407 | param->buffer[param->position] = (char)chr;
|
---|
408 | param->position ++;
|
---|
409 |
|
---|
410 | if(chr == '\n')
|
---|
411 | update = TRUE;
|
---|
412 | }else
|
---|
413 | update = TRUE;
|
---|
414 |
|
---|
415 | LeaveCriticalSection(¶m->cs);
|
---|
416 |
|
---|
417 | if(update == TRUE)
|
---|
418 | SendMessage(cb->Handle, SERMSG_UPDATE, 0, 1);
|
---|
419 | } while(success == TRUE);
|
---|
420 |
|
---|
421 | return TRUE;
|
---|
422 | }
|
---|
423 |
|
---|
424 | /*===========================================================================*/
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * Windowsã®ã³ã³ã½ã¼ã«ã使ç¨ããã·ãªã¢ã«å
|
---|
428 | ¥åºå
|
---|
429 | */
|
---|
430 |
|
---|
431 | static DWORD WINAPI WinConsole_ReceiverThread(LPVOID param)
|
---|
432 | {
|
---|
433 | SIOPCB * cb = (SIOPCB *)param;
|
---|
434 | DWORD work;
|
---|
435 | HANDLE stdin;
|
---|
436 | INPUT_RECORD input_record;
|
---|
437 |
|
---|
438 | assert(cb != NULL);
|
---|
439 | assert(cb->Handle != NULL && cb->Handle != 0);
|
---|
440 |
|
---|
441 | stdin = GetStdHandle(STD_INPUT_HANDLE);
|
---|
442 | FatalAssertion(stdin != 0, "WinConsole_ReceiverThread failed to acquire the handle of standard input.");
|
---|
443 |
|
---|
444 | while((work = WaitForSingleObject(cb->Handle, INFINITE)) != WAIT_FAILED)
|
---|
445 | {
|
---|
446 | assert(work == WAIT_OBJECT_0); /* ãªãã¸ã§ã¯ãã¯ã·ã°ãã«ç¶æ
|
---|
447 | ã«ãªã£ã */
|
---|
448 |
|
---|
449 | ReadConsoleInput(stdin, &input_record, 1, &work);
|
---|
450 |
|
---|
451 | if(input_record.EventType == KEY_EVENT && input_record.Event.KeyEvent.bKeyDown == TRUE)
|
---|
452 | {
|
---|
453 | cb->ReceiveBuffer = (char)input_record.Event.KeyEvent.uChar.AsciiChar;
|
---|
454 |
|
---|
455 | BITSET(cb->Flag, SIO_STA_INTRCV);
|
---|
456 | HALInterruptRequest(INHNO_SERIAL);
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | return 0;
|
---|
461 | }
|
---|
462 |
|
---|
463 | static void WinConsole_CreatePort(SIOPCB * cb)
|
---|
464 | {
|
---|
465 | BOOL result;
|
---|
466 | result = AllocConsole();
|
---|
467 | FatalAssertion(result != 0, "WinConsole_CreatePort failed to allocate its own console.");
|
---|
468 | cb->Handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
---|
469 | SetConsoleTitle("TOPPERS/JSP SerialConsole");
|
---|
470 |
|
---|
471 | /* åä¿¡ãããã¡ç£è¦ç¨ã¹ã¬ããã使 */
|
---|
472 | CreateThread(NULL,0,WinConsole_ReceiverThread,(LPVOID)cb,0,NULL);
|
---|
473 | }
|
---|
474 |
|
---|
475 | static BOOL WinConsole_PutChar(SIOPCB * cb, INT chr, BOOL rasint)
|
---|
476 | {
|
---|
477 | BOOL result;
|
---|
478 | DWORD written;
|
---|
479 | char word;
|
---|
480 |
|
---|
481 | assert(cb != NULL);
|
---|
482 | assert(BITTEST(cb->Flag, SIO_TYP_TTY));
|
---|
483 | assert(cb->Handle != NULL && cb->Handle != 0);
|
---|
484 |
|
---|
485 | /* æ¨æºåºåã«ä¸æåéä¿¡ */
|
---|
486 | word = (char)chr;
|
---|
487 | result = WriteFile(cb->Handle, &word, 1, &written, NULL);
|
---|
488 |
|
---|
489 | /* å²è¾¼ã¿çºçãã©ã°ãç«ã£ã¦ããããéä¿¡å®äºå²è¾¼ã¿è¦æ±ãèµ·ãã */
|
---|
490 | if(result != 0 && rasint == TRUE)
|
---|
491 | {
|
---|
492 | BITSET(cb->Flag, SIO_STA_INTSND);
|
---|
493 | HALInterruptRequest(INHNO_SERIAL);
|
---|
494 | }
|
---|
495 |
|
---|
496 | return result != 0 ? TRUE : FALSE;
|
---|
497 | }
|
---|
498 |
|
---|
499 | static void WinConsole_ClosePort(SIOPCB * cb)
|
---|
500 | {
|
---|
501 | assert(cb != NULL);
|
---|
502 | assert(BITTEST(cb->Flag, SIO_TYP_TTY));
|
---|
503 |
|
---|
504 | if(cb->Handle != NULL && cb->Handle != 0)
|
---|
505 | {
|
---|
506 | FreeConsole();
|
---|
507 | cb->Handle = NULL;
|
---|
508 | }
|
---|
509 | }
|
---|
510 |
|
---|
511 | /*===========================================================================*/
|
---|
512 |
|
---|
513 | /*
|
---|
514 | * Windowsã®ã³ã³ã½ã¼ã«ã使ç¨ããã·ãªã¢ã«å
|
---|
515 | ¥åºå
|
---|
516 | */
|
---|
517 |
|
---|
518 | /* 注 : CreateConsoleScreenBufferã¯å¤±ææã«INVALID_HANDLE_VALUEãè¿ããã
|
---|
519 | ä»ã®ã·ãªã¢ã«å
|
---|
520 | ¥åºåé¨ã¨å
|
---|
521 | ±éãããããã«NULLãç¨ããã */
|
---|
522 |
|
---|
523 | static DWORD WINAPI ScreenBuffer_ReceiverThread(LPVOID param)
|
---|
524 | {
|
---|
525 | SIOPCB * cb = (SIOPCB *)param;
|
---|
526 | DWORD work;
|
---|
527 | INPUT_RECORD input_record;
|
---|
528 |
|
---|
529 | assert(cb != NULL);
|
---|
530 | assert(cb->Handle != NULL && cb->Handle != 0);
|
---|
531 |
|
---|
532 | while((work = WaitForSingleObject(cb->Handle, INFINITE)) != WAIT_FAILED)
|
---|
533 | {
|
---|
534 | assert(work == WAIT_OBJECT_0); /* ãªãã¸ã§ã¯ãã¯ã·ã°ãã«ç¶æ
|
---|
535 | ã«ãªã£ã */
|
---|
536 |
|
---|
537 | ReadConsoleInput(cb->Handle, &input_record, 1, &work);
|
---|
538 |
|
---|
539 | if(input_record.EventType == KEY_EVENT && input_record.Event.KeyEvent.bKeyDown == TRUE)
|
---|
540 | {
|
---|
541 | cb->ReceiveBuffer = (char)input_record.Event.KeyEvent.uChar.AsciiChar;
|
---|
542 |
|
---|
543 | BITSET(cb->Flag, SIO_STA_INTRCV);
|
---|
544 | HALInterruptRequest(INHNO_SERIAL);
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | return 0;
|
---|
549 | }
|
---|
550 |
|
---|
551 | static void ScreenBuffer_CreatePort(SIOPCB * cb)
|
---|
552 | {
|
---|
553 | cb->Handle = CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
|
---|
554 |
|
---|
555 | /* cb->Handle should not be compared with NULL because CreateConsoleScreenBuffer returns INVALID_HANDLE_VALUE if it failed */
|
---|
556 | FatalAssertion(cb->Handle != INVALID_HANDLE_VALUE && cb->Handle != NULL,
|
---|
557 | "ScreenBuffer_CreatePort failed to allocate its own console.");
|
---|
558 |
|
---|
559 | SetConsoleActiveScreenBuffer(cb->Handle);
|
---|
560 | SetConsoleTitle("TOPPERS/JSP SerialConsole");
|
---|
561 |
|
---|
562 | /* åä¿¡ãããã¡ç£è¦ç¨ã¹ã¬ããã使 */
|
---|
563 | CreateThread(NULL,0,ScreenBuffer_ReceiverThread,(LPVOID)cb,0,NULL);
|
---|
564 | }
|
---|
565 |
|
---|
566 | static BOOL ScreenBuffer_PutChar(SIOPCB * cb, INT chr, BOOL rasint)
|
---|
567 | {
|
---|
568 | BOOL result;
|
---|
569 | DWORD written;
|
---|
570 | char word;
|
---|
571 |
|
---|
572 | assert(cb != NULL);
|
---|
573 | assert(BITTEST(cb->Flag, SIO_TYP_SCRBUF));
|
---|
574 | assert(cb->Handle != NULL && cb->Handle != 0);
|
---|
575 |
|
---|
576 | /* æ¨æºåºåã«ä¸æåéä¿¡ */
|
---|
577 | word = (char)chr;
|
---|
578 | result = WriteFile(cb->Handle, &word, 1, &written, NULL);
|
---|
579 |
|
---|
580 | /* å²è¾¼ã¿çºçãã©ã°ãç«ã£ã¦ããããéä¿¡å®äºå²è¾¼ã¿è¦æ±ãèµ·ãã */
|
---|
581 | if(result != 0 && rasint == TRUE)
|
---|
582 | {
|
---|
583 | BITSET(cb->Flag, SIO_STA_INTSND);
|
---|
584 | HALInterruptRequest(INHNO_SERIAL);
|
---|
585 | }
|
---|
586 |
|
---|
587 | return result != 0 ? TRUE : FALSE;
|
---|
588 | }
|
---|
589 |
|
---|
590 | static void ScreenBuffer_ClosePort(SIOPCB * cb)
|
---|
591 | {
|
---|
592 | assert(cb != NULL);
|
---|
593 | assert(BITTEST(cb->Flag, SIO_TYP_SCRBUF));
|
---|
594 |
|
---|
595 | if(cb->Handle != NULL && cb->Handle != 0)
|
---|
596 | {
|
---|
597 | CloseHandle(cb->Handle);
|
---|
598 | cb->Handle = NULL;
|
---|
599 | }
|
---|
600 | }
|
---|
601 |
|
---|
602 | /*===========================================================================*/
|
---|
603 |
|
---|
604 | /*
|
---|
605 | * ã»ãã¨ã«ã·ãªã¢ã«åºå
|
---|
606 | */
|
---|
607 |
|
---|
608 | static DWORD WINAPI RawSerial_ReceiverThread(LPVOID param)
|
---|
609 | {
|
---|
610 | SIOPCB * cb = (SIOPCB *)param;
|
---|
611 | DWORD work;
|
---|
612 | char buffer;
|
---|
613 |
|
---|
614 | assert(cb != NULL);
|
---|
615 | assert(cb->Handle != NULL && cb->Handle != 0);
|
---|
616 |
|
---|
617 | while((work = WaitForSingleObject(cb->Handle, INFINITE)) != WAIT_FAILED)
|
---|
618 | {
|
---|
619 | assert(work == WAIT_OBJECT_0); /* ãªãã¸ã§ã¯ãã¯ã·ã°ãã«ç¶æ
|
---|
620 | ã«ãªã£ã */
|
---|
621 |
|
---|
622 | ReadFile(cb->Handle, &buffer, 1, &work, NULL);
|
---|
623 |
|
---|
624 | cb->ReceiveBuffer = (char)buffer;
|
---|
625 | BITSET(cb->Flag, SIO_STA_INTRCV);
|
---|
626 | HALInterruptRequest(INHNO_SERIAL);
|
---|
627 | }
|
---|
628 |
|
---|
629 | return 0;
|
---|
630 | }
|
---|
631 |
|
---|
632 | static void RawSerial_CreatePort(SIOPCB * cb)
|
---|
633 | {
|
---|
634 | cb->Handle = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
|
---|
635 | FatalAssertion( cb->Handle != INVALID_HANDLE_VALUE && cb->Handle != NULL,
|
---|
636 | "RawSerial_CreatePort failed to open the port 'COM1'.");
|
---|
637 |
|
---|
638 | /* åä¿¡ãããã¡ç£è¦ç¨ã¹ã¬ããã使 */
|
---|
639 | CreateThread(NULL, 0, RawSerial_ReceiverThread, (LPVOID)cb, 0, NULL);
|
---|
640 | }
|
---|
641 |
|
---|
642 | static BOOL RawSerial_PutChar(SIOPCB * cb, INT chr, BOOL rasint)
|
---|
643 | {
|
---|
644 | BOOL result;
|
---|
645 | DWORD written;
|
---|
646 | char word;
|
---|
647 |
|
---|
648 | assert(cb != NULL);
|
---|
649 | assert(BITTEST(cb->Flag, SIO_TYP_RAWSIO));
|
---|
650 | assert(cb->Handle != NULL && cb->Handle != 0);
|
---|
651 |
|
---|
652 | /* æ¨æºåºåã«ä¸æåéä¿¡ */
|
---|
653 | word = (char)chr;
|
---|
654 | result = WriteFile(cb->Handle, &word, 1, &written, NULL);
|
---|
655 |
|
---|
656 | /* å²è¾¼ã¿çºçãã©ã°ãç«ã£ã¦ããããéä¿¡å®äºå²è¾¼ã¿è¦æ±ãèµ·ãã */
|
---|
657 | if(result != 0 && rasint == TRUE)
|
---|
658 | {
|
---|
659 | BITSET(cb->Flag, SIO_STA_INTSND);
|
---|
660 | HALInterruptRequest(INHNO_SERIAL);
|
---|
661 | }
|
---|
662 |
|
---|
663 | return result != 0 ? TRUE : FALSE;
|
---|
664 | }
|
---|
665 |
|
---|
666 | static void RawSerial_ClosePort(SIOPCB * cb)
|
---|
667 | {
|
---|
668 | assert(cb != NULL);
|
---|
669 | assert(BITTEST(cb->Flag, SIO_TYP_RAWSIO));
|
---|
670 |
|
---|
671 | if(cb->Handle != NULL && cb->Handle != 0)
|
---|
672 | {
|
---|
673 | CloseHandle(cb->Handle);
|
---|
674 | cb->Handle = NULL;
|
---|
675 | }
|
---|
676 | }
|
---|
677 |
|
---|
678 |
|
---|
679 | /*
|
---|
680 | * SIOãã©ã¤ãã®åæåã«ã¼ãã³
|
---|
681 | */
|
---|
682 | void sio_initialize(void)
|
---|
683 | {
|
---|
684 | int i;
|
---|
685 |
|
---|
686 | for(i=0; i<TNUM_PORT; ++i)
|
---|
687 | {
|
---|
688 | siopcb_table[i].Flag = SIO_TYP_CONSOLE;
|
---|
689 | siopcb_table[i].Handle = NULL;
|
---|
690 | siopcb_table[i].ReceiveBuffer = -1;
|
---|
691 | siopcb_table[i].versatile = NULL;
|
---|
692 | }
|
---|
693 | }
|
---|
694 |
|
---|
695 | /*
|
---|
696 | * ã·ãªã¢ã«I/Oãã¼ãã®ãªã¼ãã³
|
---|
697 | */
|
---|
698 | SIOPCB * sio_opn_por(ID siopid, VP_INT exinf)
|
---|
699 | {
|
---|
700 | BOOL success;
|
---|
701 | SIOPCB * siopcb = GET_SIOPCB(siopid);
|
---|
702 |
|
---|
703 | assert(siopcb != NULL);
|
---|
704 |
|
---|
705 | siopcb->exinf = exinf;
|
---|
706 |
|
---|
707 | success = TRUE;
|
---|
708 | switch(SIO_TYP(siopcb->Flag))
|
---|
709 | {
|
---|
710 | case SIO_TYP_CONSOLE:
|
---|
711 | HALExecuteProcedure(CreateSerialConsole,siopcb);
|
---|
712 | break;
|
---|
713 | case SIO_TYP_TTY:
|
---|
714 | WinConsole_CreatePort(siopcb);
|
---|
715 | break;
|
---|
716 | case SIO_TYP_SCRBUF:
|
---|
717 | HALExecuteProcedure(ScreenBuffer_CreatePort,siopcb);
|
---|
718 | break;
|
---|
719 | case SIO_TYP_RAWSIO:
|
---|
720 | HALExecuteProcedure(RawSerial_CreatePort,siopcb);
|
---|
721 | break;
|
---|
722 | default:
|
---|
723 | success = FALSE;
|
---|
724 | }
|
---|
725 |
|
---|
726 | if(success == TRUE)
|
---|
727 | BITSET(siopcb->Flag, SIO_STA_OPEN);
|
---|
728 |
|
---|
729 | return siopcb;
|
---|
730 | }
|
---|
731 |
|
---|
732 | /*
|
---|
733 | * ã·ãªã¢ã«I/Oãã¼ãã®ã¯ãã¼ãº
|
---|
734 | */
|
---|
735 | void sio_cls_por(SIOPCB *siopcb)
|
---|
736 | {
|
---|
737 | assert(siopcb != NULL);
|
---|
738 |
|
---|
739 | switch(SIO_TYP(siopcb->Flag))
|
---|
740 | {
|
---|
741 | case SIO_TYP_CONSOLE:
|
---|
742 | HALExecuteProcedure(SerialConsole_FinalRelease,siopcb);
|
---|
743 | break;
|
---|
744 | case SIO_TYP_TTY:
|
---|
745 | WinConsole_ClosePort(siopcb);
|
---|
746 | break;
|
---|
747 | case SIO_TYP_SCRBUF:
|
---|
748 | HALExecuteProcedure(ScreenBuffer_ClosePort,siopcb);
|
---|
749 | break;
|
---|
750 | case SIO_TYP_RAWSIO:
|
---|
751 | HALExecuteProcedure(RawSerial_ClosePort,siopcb);
|
---|
752 | break;
|
---|
753 | }
|
---|
754 | }
|
---|
755 |
|
---|
756 | /*
|
---|
757 | * ã·ãªã¢ã«I/Oãã¼ãã¸ã®æåéä¿¡
|
---|
758 | */
|
---|
759 | BOOL sio_snd_chr(SIOPCB *siopcb, INT chr)
|
---|
760 | {
|
---|
761 | BOOL result;
|
---|
762 |
|
---|
763 | assert(siopcb != NULL);
|
---|
764 |
|
---|
765 | switch(SIO_TYP(siopcb->Flag))
|
---|
766 | {
|
---|
767 | case SIO_TYP_CONSOLE:
|
---|
768 | result = SerialConsole_PushChar(siopcb, chr);
|
---|
769 | break;
|
---|
770 | case SIO_TYP_TTY:
|
---|
771 | result = WinConsole_PutChar(siopcb, chr, TRUE);
|
---|
772 | break;
|
---|
773 | case SIO_TYP_SCRBUF:
|
---|
774 | result = ScreenBuffer_PutChar(siopcb, chr, TRUE);
|
---|
775 | break;
|
---|
776 | case SIO_TYP_RAWSIO:
|
---|
777 | result = RawSerial_PutChar(siopcb, chr, TRUE);
|
---|
778 | break;
|
---|
779 | }
|
---|
780 |
|
---|
781 | return result;
|
---|
782 | }
|
---|
783 |
|
---|
784 | /*
|
---|
785 | * ã·ãªã¢ã«I/Oãã¼ãããã®æååä¿¡
|
---|
786 | */
|
---|
787 | INT sio_rcv_chr(SIOPCB * siopcb)
|
---|
788 | {
|
---|
789 | INT result;
|
---|
790 |
|
---|
791 | result = siopcb->ReceiveBuffer;
|
---|
792 | siopcb->ReceiveBuffer = -1;
|
---|
793 |
|
---|
794 | return result;
|
---|
795 | }
|
---|
796 |
|
---|
797 |
|
---|
798 | /*
|
---|
799 | * ã·ãªã¢ã«I/Oãã¼ãããã®ã³ã¼ã«ããã¯è¨±å¯
|
---|
800 | */
|
---|
801 | void sio_ena_cbr(SIOPCB * siopcb, UINT cbrtn)
|
---|
802 | {}
|
---|
803 |
|
---|
804 | /*
|
---|
805 | * ã·ãªã¢ã«I/Oãã¼ãããã®ã³ã¼ã«ããã¯ç¦æ¢
|
---|
806 | */
|
---|
807 | void sio_dis_cbr(SIOPCB * siopcb, UINT cbrtn)
|
---|
808 | {}
|
---|
809 |
|
---|
810 |
|
---|
811 | /*
|
---|
812 | * ã·ãªã¢ã«I/Oãã¼ãå²è¾¼ã¿ãã³ãã©
|
---|
813 | */
|
---|
814 | void sio_handler(void)
|
---|
815 | {
|
---|
816 | int port;
|
---|
817 |
|
---|
818 | for(port = 0; port < TNUM_PORT; ++ port)
|
---|
819 | {
|
---|
820 | if(BITTEST(siopcb_table[port].Flag, SIO_STA_OPEN))
|
---|
821 | {
|
---|
822 | /* åä¿¡å®äºå²è¾¼ã¿ */
|
---|
823 | if(BITTEST(siopcb_table[port].Flag, SIO_STA_INTRCV))
|
---|
824 | {
|
---|
825 | /* åä¿¡ããæåãåãåºã */
|
---|
826 | BITCLEAR(siopcb_table[port].Flag, SIO_STA_INTRCV);
|
---|
827 | sio_ierdy_rcv(GET_SIOPCB(ID_PORT(port))->exinf);
|
---|
828 |
|
---|
829 | }
|
---|
830 |
|
---|
831 | /* éä¿¡å®äºå²è¾¼ã¿ */
|
---|
832 | if(BITTEST(siopcb_table[port].Flag, SIO_STA_INTSND))
|
---|
833 | {
|
---|
834 | BITCLEAR(siopcb_table[port].Flag, SIO_STA_INTSND);
|
---|
835 | sio_ierdy_snd(GET_SIOPCB(ID_PORT(port))->exinf);
|
---|
836 | }
|
---|
837 | }
|
---|
838 | }
|
---|
839 | }
|
---|
840 |
|
---|
841 | /*
|
---|
842 | * ã³ã³ã½ã¼ã«ãã¼ãã¸ã®å¼·å¶ä¸æååºå (å²è¾¼ã¿ãªã)
|
---|
843 | */
|
---|
844 | void SerialRawPutc(INT chr)
|
---|
845 | {
|
---|
846 | SIOPCB * siopcb = &siopcb_table[CONSOLE_PORTID - 1];
|
---|
847 |
|
---|
848 | assert(siopcb != NULL);
|
---|
849 |
|
---|
850 | if(!BITTEST(siopcb->Flag, SIO_STA_OPEN))
|
---|
851 | return;
|
---|
852 |
|
---|
853 | switch(SIO_TYP(siopcb->Flag))
|
---|
854 | {
|
---|
855 | case SIO_TYP_CONSOLE:
|
---|
856 | SerialConsole_PutChar(siopcb, chr, FALSE);
|
---|
857 | break;
|
---|
858 | case SIO_TYP_TTY:
|
---|
859 | WinConsole_PutChar(siopcb, chr, FALSE);
|
---|
860 | break;
|
---|
861 | case SIO_TYP_SCRBUF:
|
---|
862 | ScreenBuffer_PutChar(siopcb, chr, FALSE);
|
---|
863 | break;
|
---|
864 | case SIO_TYP_RAWSIO:
|
---|
865 | RawSerial_PutChar(siopcb, chr, FALSE);
|
---|
866 | break;
|
---|
867 | }
|
---|
868 | }
|
---|