source: anotherchoice/tags/jsp-1.4.4-full-UTF8/windev/devicemanager/stdafx.cpp@ 26

Last change on this file since 26 was 26, checked in by ykominami, 10 years ago

initial

File size: 15.0 KB
Line 
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: stdafx.cpp,v 1.11 2003/12/24 07:40:42 takayuki Exp $
51 */
52
53/*
54 * デバイスマネージャ 本体(CExeModule)
55 */
56
57#pragma warning(disable:4786) //デバッグ文字列を255文字に切り詰めました
58#pragma warning(disable:4200) //非標準の拡張機能を使っています (長さ不定のé…
59åˆ—:465行目)
60
61#include "stdafx.h"
62
63#ifdef _ATL_STATIC_REGISTRY
64#include <statreg.h>
65#include <statreg.cpp>
66#endif
67
68#include <atlimpl.cpp>
69#include <algorithm>
70
71#include <initguid.h>
72#include "devicemanager.h"
73#include "device.h"
74#include "kernel.h"
75#include "information.h"
76#include "kernellog.h"
77
78using namespace std;
79
80#ifdef _DEBUG
81 DWORD Count;
82 char Buffer[1024];
83 #define DebugOut(x) ::WriteFile(::GetStdHandle(STD_OUTPUT_HANDLE),Buffer, (x), &Count, NULL)
84#else
85 #define DebugOut(x) /##/
86#endif
87
88CExeModule::CExeModule(void) : Kernel(0)
89{
90 ::InitializeCriticalSection(&cs);
91}
92
93
94DeviceID CExeModule::AddDevice(CDevice *device)
95{
96 list<CDevice *>::iterator scope;
97 list<CInformation *>::iterator view;
98
99 ::EnterCriticalSection(&cs);
100
101 scope = find(DeviceList.begin(), DeviceList.end(), device);
102 if(scope != DeviceList.end())
103 {
104 ::LeaveCriticalSection(&cs);
105 return 0;
106 }
107
108 DebugOut(::wsprintfA(Buffer,"AddDevice\n"));
109 view = ViewerList.begin();
110 while(view != ViewerList.end())
111 {
112 (*view)->Fire_OnDeviceChanged(1,reinterpret_cast<long>(device));
113 view ++;
114 }
115
116 DeviceList.push_back(device);
117
118 ::LeaveCriticalSection(&cs);
119 return reinterpret_cast<DeviceID>(device);
120}
121
122bool CExeModule::DeleteDevice(CDevice *device)
123{
124 list<CDevice *>::iterator dev;
125 map<Range,CDevice *>::iterator mem[2];
126
127 ::EnterCriticalSection(&cs);
128
129 DebugOut(::wsprintfA(Buffer,"DelDevice\n"));
130
131 dev = find(DeviceList.begin(), DeviceList.end(), device);
132 if(dev == DeviceList.end())
133 {
134 ::LeaveCriticalSection(&cs);
135 return false;
136 }
137
138 mem[0] = DeviceMap.begin();
139 while(mem[0] != DeviceMap.end())
140 {
141 if((*mem[0]).second == device)
142 {
143 mem[1] = mem[0];
144 mem[0] ++;
145
146 DeviceMap.erase(mem[1]);
147 }else
148 mem[0] ++;
149 }
150
151 DeviceList.erase(dev);
152
153 list<CInformation *>::iterator view;
154 view = ViewerList.begin();
155 while(view != ViewerList.end())
156 {
157 (*view)->Fire_OnDeviceChanged(2,reinterpret_cast<long>(device));
158 view ++;
159 }
160
161 ::LeaveCriticalSection(&cs);
162 return true;
163}
164
165bool CExeModule::Map(CDevice *device, Range area)
166{
167 map<Range,CDevice *>::iterator scope;
168
169 ::EnterCriticalSection(&cs);
170
171
172 scope = DeviceMap.begin();
173 while(scope != DeviceMap.end())
174 {
175 if( area.IsIncludedIn((*scope).first) )
176 {
177 ::LeaveCriticalSection(&cs);
178 return false;
179 }
180 scope ++;
181 }
182
183 DeviceMap[area] = device;
184
185 DebugOut(::wsprintfA(Buffer,"Map\n"));
186 list<CInformation *>::iterator view;
187 view = ViewerList.begin();
188 while(view != ViewerList.end())
189 {
190 (*view)->Fire_OnDeviceChanged(3,reinterpret_cast<long>(device),area.GetBase());
191 view ++;
192 }
193
194 ::LeaveCriticalSection(&cs);
195 return true;
196}
197
198
199bool CExeModule::Unmap(CDevice *device, unsigned long address)
200{
201 map<Range,CDevice *>::iterator scope;
202 Range work(address);
203
204 ::EnterCriticalSection(&cs);
205
206 scope = DeviceMap.begin();
207 while(scope != DeviceMap.end())
208 {
209 if( (*scope).second == device && work.IsIncludedIn((*scope).first) )
210 {
211 DeviceMap.erase(scope);
212
213 DebugOut(::wsprintfA(Buffer,"Unmap\n"));
214 list<CInformation *>::iterator view;
215 view = ViewerList.begin();
216 while(view != ViewerList.end())
217 {
218 (*view)->Fire_OnDeviceChanged(4,reinterpret_cast<long>(device),address);
219 view ++;
220 }
221
222 ::LeaveCriticalSection(&cs);
223
224 return true;
225 }
226 scope ++;
227 }
228 ::LeaveCriticalSection(&cs);
229
230 return false;
231}
232
233void CExeModule::AddViewer(CInformation *view)
234{
235 list<CInformation *>::iterator scope;
236
237 ::EnterCriticalSection(&cs);
238 scope = find(ViewerList.begin(), ViewerList.end(), view);
239 if(scope == ViewerList.end())
240 ViewerList.push_back(view);
241 ::LeaveCriticalSection(&cs);
242}
243
244void CExeModule::RemoveViewer(CInformation *view)
245{
246 list<CInformation *>::iterator scope;
247
248 ::EnterCriticalSection(&cs);
249 scope = find(ViewerList.begin(), ViewerList.end(), view);
250 if(scope != ViewerList.end())
251 ViewerList.erase(scope);
252 ::LeaveCriticalSection(&cs);
253}
254
255bool CExeModule::AttachKernel(CKernel *kernel)
256{
257 bool result = false;
258 list<CDevice *>::iterator scope;
259 list<CInformation *>::iterator view;
260 list<CKernelLog *>::iterator watcher;
261
262 DebugOut(::wsprintfA(Buffer,"AttachKernel\n"));
263
264 ::EnterCriticalSection(&cs);
265 if((Kernel != 0) && (Kernel != kernel))
266 {
267 //割込み-1 => カーネルのKeepAlive
268 if(FAILED(Kernel->Fire_OnInterruptRequest(-1)))
269 {
270 //接続を失ったカーネルのデタッチ
271 DetachKernel(Kernel);
272 }else
273 {
274 //接続済みカーネルがある
275 if(::MessageBox(NULL,_T("新しいカーネルが起動され、デバイスマネージャに接続しようとしています。\n新しいカーネルに接続å…
276ˆã‚’切り替えますか?"),_T("TOPPERS/JSP Windows - DeviceManager"),MB_YESNO) == IDYES)
277 DetachKernel(Kernel);
278 }
279 }
280
281 if(Kernel == 0)
282 {
283 Kernel = kernel;
284
285 scope = DeviceList.begin();
286 while(scope != DeviceList.end())
287 {
288 (*scope)->Fire_OnKernelStart();
289 scope ++;
290 }
291
292 view = ViewerList.begin();
293 while(view != ViewerList.end())
294 {
295 (*view)->Fire_OnKernelChanged(1);
296 view ++;
297 }
298
299 watcher = WatcherList.begin();
300 while(watcher != WatcherList.end())
301 {
302 (*watcher)->Fire_OnKernelStart();
303 watcher ++;
304 }
305
306 DebugOut(::wsprintfA(Buffer,"AttachKernel was performed successfully\n"));
307 result = true;
308 }
309 ::LeaveCriticalSection(&cs);
310
311 return result;
312}
313
314bool CExeModule::DetachKernel(CKernel *kernel)
315{
316 bool result;
317 list<CDevice *>::iterator scope;
318 list<CInformation *>::iterator view;
319 list<CKernelLog *>::iterator watcher;
320
321 DebugOut(::wsprintfA(Buffer,"DetachKernel\n"));
322
323 ::EnterCriticalSection(&cs);
324 if( (result = Kernel == kernel) )
325 {
326 scope = DeviceList.begin();
327 while(scope != DeviceList.end())
328 {
329 (*scope)->Fire_OnKernelExit();
330 scope ++;
331 }
332
333 view = ViewerList.begin();
334 while(view != ViewerList.end())
335 {
336 (*view)->Fire_OnKernelChanged(2);
337 view ++;
338 }
339
340 watcher = WatcherList.begin();
341 while(watcher != WatcherList.end())
342 {
343 (*watcher)->Fire_OnKernelExit();
344 watcher ++;
345 }
346
347 Kernel = 0l;
348 DebugOut(::wsprintfA(Buffer,"DetachKernel was performed successfully\n"));
349 }
350 ::LeaveCriticalSection(&cs);
351
352 return result;
353}
354
355bool CExeModule::Read(CKernel * kernel, unsigned long address, unsigned long sz, unsigned char data[])
356{
357 bool result;
358 map<Range,CDevice *>::iterator scope;
359
360 DebugOut(::wsprintfA(Buffer,"Read\n"));
361
362 ::EnterCriticalSection(&cs);
363
364 if(Kernel != kernel)
365 {
366 ::LeaveCriticalSection(&cs);
367
368 DebugOut(::wsprintfA(Buffer,"Illegal Kernel\n"));
369
370 return false;
371 }
372
373 scope = DeviceMap.find(Range(address,sz));
374
375 result = (scope != DeviceMap.end());
376 if(result)
377 (*scope).second->Fire_OnRead(address, sz, data);
378
379 ::LeaveCriticalSection(&cs);
380
381 return result;
382
383}
384
385bool CExeModule::Write(CKernel *kernel, unsigned long address, unsigned long sz, unsigned char *data)
386{
387 bool result;
388 map<Range,CDevice *>::iterator scope;
389
390 DebugOut(::wsprintfA(Buffer,"Write\n"));
391
392 ::EnterCriticalSection(&cs);
393
394 if(Kernel != kernel)
395 {
396 ::LeaveCriticalSection(&cs);
397 DebugOut(::wsprintfA(Buffer,"Request performed by Illegal Kernel\n"));
398 return false;
399 }
400
401 scope = DeviceMap.find(Range(address,sz));
402
403 result = (scope != DeviceMap.end());
404 if(result)
405 {
406 DebugOut(::wsprintfA(Buffer,"FireWrite\n"));
407 (*scope).second->Fire_OnWrite(address, sz,data);
408 }
409 ::LeaveCriticalSection(&cs);
410
411 return result;
412}
413
414bool CExeModule::RaiseInterrupt(unsigned long inhno)
415{
416 bool result = false;
417
418 ::EnterCriticalSection(&cs);
419
420 if(Kernel != 0 && Kernel->Fire_OnInterruptRequest(inhno) >= 0)
421 result = true;
422
423 ::LeaveCriticalSection(&cs);
424
425 return result;
426}
427
428bool CExeModule::AddLogWatcher(CKernelLog * watcher)
429{
430 if( find(WatcherList.begin(), WatcherList.end(), watcher) != WatcherList.end() )
431 return false; //多重登録
432
433 ::EnterCriticalSection(&cs);
434 WatcherList.push_back(watcher);
435 ::LeaveCriticalSection(&cs);
436
437 DebugOut(::wsprintfA(Buffer,"Log watcher inserted into the queue successfully\n"));
438
439 return true;
440}
441
442
443void CExeModule::RemoveLogWatcher(CKernelLog * watcher)
444{
445 list<CKernelLog *>::iterator scope;
446
447 ::EnterCriticalSection(&cs);
448 scope = find(WatcherList.begin(), WatcherList.end(), watcher);
449 if(scope != WatcherList.end())
450 WatcherList.erase(scope);
451 ::LeaveCriticalSection(&cs);
452
453 DebugOut(::wsprintfA(Buffer,"Log watcher removed from the queue successfully\n"));
454}
455
456
457
458/*
459 * ログ情
460報の種別の定義
461 */
462#define LOG_TYP_INTERRUPT 0x01 /* 割込みハンドラ */
463#define LOG_TYP_ISR 0x02 /* 割込みサービスハンドラ */
464#define LOG_TYP_TIMERHDR 0x03 /* タイムイベントハンドラ */
465#define LOG_TYP_CPUEXC 0x04 /* CPU例外ハンドラ */
466#define LOG_TYP_TSKEXC 0x05 /* タスク例外処理ルーチン */
467#define LOG_TYP_TSKSTAT 0x06 /* タスク状æ…
468‹å¤‰åŒ– */
469#define LOG_TYP_DISPATCH 0x07 /* ディスパッチャ */
470#define LOG_TYP_SVC 0x08 /* サービスコール */
471#define LOG_TYP_COMMENT 0x09 /* コメント(文字列のみのログ) */
472#define LOG_ENTER 0x00 /* å…
473¥å£ï¼é–‹å§‹ */
474#define LOG_LEAVE 0x80 /* 出口/終了 */
475
476
477typedef unsigned int BITMASK;
478
479typedef struct {
480 unsigned int logtype;
481 unsigned int logtim;
482 BITMASK valid;
483 unsigned int bufsz;
484 unsigned char buf[];
485 } DBIFLOG;
486#define DBIFLOG_BUFMARGIN (sizeof(unsigned int)*4)
487
488bool CExeModule::OnLogEvent(CKernel *kernel, long sz, unsigned char *data)
489{
490 list<CKernelLog *>::iterator scope;
491
492#if 0
493
494 DBIFLOG * log = reinterpret_cast<DBIFLOG *>(data);
495
496 DebugOut(::wsprintfA(Buffer,"LOG <%08x> ",log->logtim));
497 switch(log->logtype)
498 {
499 case LOG_TYP_TSKSTAT:
500 DebugOut(::wsprintfA(Buffer,"[STATUS] Tsk:%d ",*(int*)log->buf));
501 switch(*((int*)log->buf+1))
502 {
503 case 0x01:
504 case 0x02:
505 DebugOut(::wsprintfA(Buffer,"<Runnable [Ready+Running]>\n",*(int*)log->buf));
506 break;
507 case 0x04:
508 case 0x0c:
509 if(*((int*)log->buf+1) == 0x04)
510 DebugOut(::wsprintfA(Buffer,"<Waiting : "));
511 else
512 DebugOut(::wsprintfA(Buffer,"<Wait-Suspend : "));
513
514 switch(*((int*)log->buf+2))
515 {
516 case 0x1:
517 DebugOut(::wsprintfA(Buffer,"Sleep>\n"));
518 break;
519 case 0x2:
520 DebugOut(::wsprintfA(Buffer,"Delay>\n"));
521 break;
522 case 0x4:
523 DebugOut(::wsprintfA(Buffer,"Semaphore>\n"));
524 break;
525 default:
526 DebugOut(::wsprintfA(Buffer," complex [%04x]>\n", *((int*)log->buf+2)));
527 break;
528 }
529
530 break;
531 case 0x08:
532 DebugOut(::wsprintfA(Buffer,"<Suspended>\n"));
533 break;
534 case 0x10:
535 DebugOut(::wsprintfA(Buffer,"<Dormant>\n"));
536 break;
537 default:
538 DebugOut(::wsprintfA(Buffer,"<Unknown : %d>",*(int*)log->buf));
539 break;
540 }
541 break;
542
543 case LOG_TYP_TSKEXC:
544 DebugOut(::wsprintfA(Buffer,"[TSKEXC|ENT] Tsk:%d\n",*(int*)log->buf));
545 break;
546
547 case LOG_TYP_TSKEXC | LOG_LEAVE:
548 DebugOut(::wsprintfA(Buffer,"[TSKEXC|LEA] Tsk:%d\n",*(int*)log->buf));
549 break;
550
551 case LOG_TYP_DISPATCH:
552 if(*((int*)log->buf+1) == 0)
553 DebugOut(::wsprintfA(Buffer,"[DSP|ENT] Tsk:%d Task-context\n",*(int*)log->buf));
554 else
555 DebugOut(::wsprintfA(Buffer,"[DSP|ENT] Tsk:%d Nontask-context\n",*(int*)log->buf));
556 break;
557
558 case LOG_TYP_DISPATCH | LOG_LEAVE:
559 DebugOut(::wsprintfA(Buffer,"[DSP|LEA] Tsk:%d\n",*(int*)log->buf));
560 break;
561
562 case LOG_TYP_COMMENT:
563 DebugOut(::wsprintfA(Buffer,"[COMMENT (%d)] : ",log->bufsz));
564 ::WriteFile(::GetStdHandle(STD_OUTPUT_HANDLE), log->buf, log->bufsz-1, &Count, NULL);
565 break;
566
567 case LOG_TYP_INTERRUPT:
568 DebugOut(::wsprintfA(Buffer,"[INT|ENT] %d\n",*(int*)log->buf));
569 break;
570
571 case LOG_TYP_INTERRUPT | LOG_LEAVE:
572 DebugOut(::wsprintfA(Buffer,"[INT|LEA] %d\n",*(int*)log->buf));
573 break;
574
575 case LOG_TYP_CPUEXC:
576 DebugOut(::wsprintfA(Buffer,"[CPUEXC|ENT] Tsk:%d\n",*(int*)log->buf));
577 break;
578
579 case LOG_TYP_CPUEXC | LOG_LEAVE:
580 DebugOut(::wsprintfA(Buffer,"[CPUEXC|LEA] Tsk:%d\n",*(int*)log->buf));
581 break;
582
583 case LOG_TYP_TIMERHDR:
584 case LOG_TYP_TIMERHDR | LOG_LEAVE:
585 if((log->logtype & LOG_LEAVE) != 0)
586 DebugOut(::wsprintfA(Buffer,"[INT|LEA]"));
587 else
588 DebugOut(::wsprintfA(Buffer,"[INT|ENT]"));
589
590 if( *(int*)log->buf == 0x8d )
591 DebugOut(::wsprintfA(Buffer," cyclic "));
592 else
593 DebugOut(::wsprintfA(Buffer," OBJ(%02x) ", *(int*)log->buf));
594
595 DebugOut(::wsprintf(Buffer," ID:%d EXINF:0x%08x\n",*((int*)log->buf+1), *((int*)log->buf+2)));
596 break;
597
598 default:
599 DebugOut(::wsprintfA(Buffer,"[UNKNOWN:0x%x]\n",log->logtype));
600 };
601#endif
602
603 if(Kernel != kernel)
604 {
605 DebugOut(::wsprintfA(Buffer,"Request performed by Illegal Kernel\n"));
606 return false;
607 }
608
609 ::EnterCriticalSection(&cs);
610 scope = WatcherList.begin();
611 while(scope != WatcherList.end())
612 {
613 (*scope)->Fire_OnLogEvent(sz, data);
614 scope ++;
615 }
616 ::LeaveCriticalSection(&cs);
617
618 return true;
619}
620
Note: See TracBrowser for help on using the repository browser.