Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

706 lines
17 KiB

  1. #include <stddef.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "windows.h"
  6. HANDLE WaitEvent;
  7. HANDLE StartWaitEvent;
  8. HANDLE StartTransmitEvent;
  9. HANDLE SetBreakEvent;
  10. HANDLE ClrBreakEvent;
  11. HANDLE hFile;
  12. HANDLE IoSemaphore;
  13. DWORD
  14. WaitCommThread(
  15. LPVOID Trash
  16. )
  17. {
  18. DWORD ReasonSatisfied;
  19. DWORD Trash2;
  20. OVERLAPPED Ol;
  21. UNREFERENCED_PARAMETER(Trash);
  22. Ol.hEvent = WaitEvent;
  23. do {
  24. //
  25. // StartWait Event will get pulsed whenever the user
  26. // asks for a wait.
  27. //
  28. WaitForSingleObject(StartWaitEvent,-1);
  29. //
  30. // Now we wait on the comm event.
  31. //
  32. WaitForSingleObject(IoSemaphore,-1);
  33. printf("Waiting for comm event\n");
  34. ReleaseSemaphore(IoSemaphore,1,NULL);
  35. if (!WaitCommEvent(
  36. hFile,
  37. &ReasonSatisfied,
  38. &Ol
  39. )) {
  40. DWORD LastError = GetLastError();
  41. if (LastError == ERROR_IO_PENDING) {
  42. if (!GetOverlappedResult(
  43. hFile,
  44. &Ol,
  45. &Trash2,
  46. TRUE
  47. )) {
  48. WaitForSingleObject(IoSemaphore,-1);
  49. printf("Could not do the getoverlapped on the wait: %d\n",GetLastError());
  50. ReleaseSemaphore(IoSemaphore,1,NULL);
  51. } else {
  52. WaitForSingleObject(IoSemaphore,-1);
  53. printf("Wait satisfied with mask: %x\n",ReasonSatisfied);
  54. printf("Event was%sset\n",((!WaitForSingleObject(WaitEvent,0))?(" "):(" not ")));
  55. ReleaseSemaphore(IoSemaphore,1,NULL);
  56. }
  57. } else {
  58. WaitForSingleObject(IoSemaphore,-1);
  59. printf("Could not start the wait: %d\n",LastError);
  60. ReleaseSemaphore(IoSemaphore,1,NULL);
  61. }
  62. } else {
  63. WaitForSingleObject(IoSemaphore,-1);
  64. printf("Wait satisfied with mask: %x\n",ReasonSatisfied);
  65. printf("Event was%sset\n",((!WaitForSingleObject(WaitEvent,0))?(" "):(" not ")));
  66. ReleaseSemaphore(IoSemaphore,1,NULL);
  67. }
  68. } while (TRUE);
  69. return 1;
  70. }
  71. DWORD
  72. TransmitCommThread(
  73. LPVOID Trash
  74. )
  75. {
  76. UNREFERENCED_PARAMETER(Trash);
  77. do {
  78. //
  79. // StartWait Event will get pulsed whenever the user
  80. // asks for a wait.
  81. //
  82. WaitForSingleObject(StartTransmitEvent,-1);
  83. //
  84. // Now we wait on the comm event.
  85. //
  86. WaitForSingleObject(IoSemaphore,-1);
  87. printf("Starting transmit\n");
  88. ReleaseSemaphore(IoSemaphore,1,NULL);
  89. if (!TransmitCommChar(
  90. hFile,
  91. 'a'
  92. )) {
  93. DWORD LastError = GetLastError();
  94. WaitForSingleObject(IoSemaphore,-1);
  95. printf("Could not initiate the transmit: %d\n",LastError);
  96. ReleaseSemaphore(IoSemaphore,1,NULL);
  97. } else {
  98. COMSTAT LocalStat;
  99. if (!ClearCommError(
  100. hFile,
  101. NULL,
  102. &LocalStat
  103. )) {
  104. WaitForSingleObject(IoSemaphore,-1);
  105. printf("Could not initiate the clear comm error: %d\n",GetLastError());
  106. ReleaseSemaphore(IoSemaphore,1,NULL);
  107. } else {
  108. WaitForSingleObject(IoSemaphore,-1);
  109. printf("Successful transmit - InQueue,OutQueue: %d,%d\n",LocalStat.cbInQue,LocalStat.cbOutQue);
  110. ReleaseSemaphore(IoSemaphore,1,NULL);
  111. }
  112. }
  113. } while (TRUE);
  114. return 1;
  115. }
  116. DWORD
  117. SetBreakThread(
  118. LPVOID Trash
  119. )
  120. {
  121. UNREFERENCED_PARAMETER(Trash);
  122. do {
  123. //
  124. // StartBreakEvent will get pulsed whenever the user
  125. // asks for a Set break.
  126. //
  127. WaitForSingleObject(SetBreakEvent,-1);
  128. //
  129. // Now we wait on the comm event.
  130. //
  131. WaitForSingleObject(IoSemaphore,-1);
  132. printf("Starting Set Break\n");
  133. ReleaseSemaphore(IoSemaphore,1,NULL);
  134. if (!EscapeCommFunction(
  135. hFile,
  136. SETBREAK
  137. )) {
  138. DWORD LastError = GetLastError();
  139. WaitForSingleObject(IoSemaphore,-1);
  140. printf("Set break failed: %d\n",LastError);
  141. ReleaseSemaphore(IoSemaphore,1,NULL);
  142. } else {
  143. WaitForSingleObject(IoSemaphore,-1);
  144. printf("Set escape done\n");
  145. ReleaseSemaphore(IoSemaphore,1,NULL);
  146. }
  147. } while (TRUE);
  148. return 1;
  149. }
  150. DWORD
  151. ClrBreakThread(
  152. LPVOID Trash
  153. )
  154. {
  155. UNREFERENCED_PARAMETER(Trash);
  156. do {
  157. //
  158. // StartWait Event will get pulsed whenever the user
  159. // asks for a clr break.
  160. //
  161. WaitForSingleObject(ClrBreakEvent,-1);
  162. //
  163. // Now we wait on the comm event.
  164. //
  165. WaitForSingleObject(IoSemaphore,-1);
  166. printf("Starting clr break\n");
  167. ReleaseSemaphore(IoSemaphore,1,NULL);
  168. if (!EscapeCommFunction(
  169. hFile,
  170. CLRBREAK
  171. )) {
  172. DWORD LastError = GetLastError();
  173. WaitForSingleObject(IoSemaphore,-1);
  174. printf("Could not initiate the clr break: %d\n",LastError);
  175. ReleaseSemaphore(IoSemaphore,1,NULL);
  176. } else {
  177. WaitForSingleObject(IoSemaphore,-1);
  178. printf("clr break done\n");
  179. ReleaseSemaphore(IoSemaphore,1,NULL);
  180. }
  181. } while (TRUE);
  182. return 1;
  183. }
  184. void main(int argc,char *argv[]) {
  185. char *MyPort = "COM1";
  186. DWORD ValueFromEscape = 0;
  187. DWORD CharFunc;
  188. DWORD ThreadId;
  189. int scanfval;
  190. if (argc > 1) {
  191. MyPort = argv[1];
  192. }
  193. WaitEvent = CreateEvent(
  194. NULL,
  195. TRUE,
  196. FALSE,
  197. NULL
  198. );
  199. if (!WaitEvent) {
  200. printf("Wait Event could not be created\n");
  201. exit(1);
  202. }
  203. StartWaitEvent = CreateEvent(
  204. NULL,
  205. FALSE,
  206. FALSE,
  207. NULL
  208. );
  209. if (!StartWaitEvent) {
  210. printf("StartWait Event could not be created\n");
  211. exit(1);
  212. }
  213. StartTransmitEvent = CreateEvent(
  214. NULL,
  215. FALSE,
  216. FALSE,
  217. NULL
  218. );
  219. if (!StartTransmitEvent) {
  220. printf("StartTransmit Event could not be created\n");
  221. exit(1);
  222. }
  223. SetBreakEvent = CreateEvent(
  224. NULL,
  225. FALSE,
  226. FALSE,
  227. NULL
  228. );
  229. if (!SetBreakEvent) {
  230. printf("SetBreakEvent could not be created\n");
  231. exit(1);
  232. }
  233. ClrBreakEvent = CreateEvent(
  234. NULL,
  235. FALSE,
  236. FALSE,
  237. NULL
  238. );
  239. if (!ClrBreakEvent) {
  240. printf("ClrBreakEvent could not be created\n");
  241. exit(1);
  242. }
  243. IoSemaphore = CreateSemaphore(
  244. NULL,
  245. 1,
  246. 1,
  247. NULL
  248. );
  249. if (!IoSemaphore) {
  250. printf("IoSemaphore could not be created\n");
  251. exit(1);
  252. }
  253. if ((hFile = CreateFile(
  254. MyPort,
  255. GENERIC_READ | GENERIC_WRITE,
  256. 0,
  257. NULL,
  258. CREATE_ALWAYS,
  259. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  260. NULL
  261. )) != ((HANDLE)-1)) {
  262. DCB MyDcb;
  263. COMMTIMEOUTS NewTimeouts;
  264. printf("We successfully opened the %s port.\n",MyPort);
  265. //
  266. // We've successfully opened the file. Set the state of
  267. // the comm device. First we get the old values and
  268. // adjust to our own.
  269. //
  270. if (!GetCommState(
  271. hFile,
  272. &MyDcb
  273. )) {
  274. printf("Couldn't get the comm state: %d\n",GetLastError());
  275. exit(1);
  276. }
  277. MyDcb.BaudRate = 19200;
  278. MyDcb.ByteSize = 8;
  279. MyDcb.Parity = NOPARITY;
  280. MyDcb.StopBits = ONESTOPBIT;
  281. MyDcb.EvtChar = 'a';
  282. NewTimeouts.ReadIntervalTimeout = 0;
  283. NewTimeouts.ReadTotalTimeoutMultiplier = 0;
  284. NewTimeouts.ReadTotalTimeoutConstant = 0;
  285. NewTimeouts.WriteTotalTimeoutMultiplier = 0;
  286. NewTimeouts.WriteTotalTimeoutConstant = 30000; // 30 seconds for immediate char;
  287. if (!SetCommState(
  288. hFile,
  289. &MyDcb
  290. )) {
  291. printf("Couldn't set the comm state: %d\n",GetLastError());
  292. exit(1);
  293. }
  294. if (!SetCommTimeouts(
  295. hFile,
  296. &NewTimeouts
  297. )) {
  298. printf("Couldn't set the comm timeouts: %d\n",GetLastError());
  299. exit(1);
  300. }
  301. } else {
  302. printf("Could not open the comm port: %d\n",GetLastError());
  303. }
  304. //
  305. // Create the thread that will wait for the
  306. // comm events.
  307. //
  308. if (!CreateThread(
  309. NULL,
  310. 0,
  311. WaitCommThread,
  312. NULL,
  313. 0,
  314. &ThreadId
  315. )) {
  316. printf("Could not create the wait thread.\n");
  317. exit(1);
  318. }
  319. //
  320. // Create the thread that will wait for the
  321. // comm events.
  322. //
  323. if (!CreateThread(
  324. NULL,
  325. 0,
  326. TransmitCommThread,
  327. NULL,
  328. 0,
  329. &ThreadId
  330. )) {
  331. printf("Could not create the transmit thread.\n");
  332. exit(1);
  333. }
  334. if (!CreateThread(
  335. NULL,
  336. 0,
  337. SetBreakThread,
  338. NULL,
  339. 0,
  340. &ThreadId
  341. )) {
  342. printf("Could not create the transmit thread.\n");
  343. exit(1);
  344. }
  345. if (!CreateThread(
  346. NULL,
  347. 0,
  348. ClrBreakThread,
  349. NULL,
  350. 0,
  351. &ThreadId
  352. )) {
  353. printf("Could not create the transmit thread.\n");
  354. exit(1);
  355. }
  356. do {
  357. WaitForSingleObject(IoSemaphore,-1);
  358. printf("^z=END 1=GETCOMMMASK 2=WAITCOMMEVENT\n"
  359. " 3=SETCOMMMASK (Will prompt for mask)\n"
  360. " 4=TRANSMITIMMEDIATE\n"
  361. " 5=SETBREAK 6=CLRBREAK:");
  362. if ((scanfval = scanf("%d",&CharFunc)) != EOF) {
  363. if (scanfval != 1) {
  364. printf("Invalid input\n");
  365. ReleaseSemaphore(IoSemaphore,1,NULL);
  366. continue;
  367. }
  368. ReleaseSemaphore(IoSemaphore,1,NULL);
  369. if ((CharFunc >= 1) && (CharFunc <= 6)) {
  370. switch (CharFunc) {
  371. case 1: {
  372. DWORD OldMask = 0;
  373. if (GetCommMask(
  374. hFile,
  375. &OldMask
  376. )) {
  377. WaitForSingleObject(IoSemaphore,-1);
  378. printf("CurrentMask = %x\n",OldMask);
  379. ReleaseSemaphore(IoSemaphore,1,NULL);
  380. } else {
  381. DWORD LastError = GetLastError();
  382. DWORD ErrorWord = 0;
  383. if (ClearCommError(
  384. hFile,
  385. &ErrorWord,
  386. NULL
  387. )) {
  388. if (ErrorWord) {
  389. printf("We had an error word of: %x\n",ErrorWord);
  390. } else {
  391. printf("No error word value on bad getmask call\n");
  392. }
  393. } else {
  394. printf("Could not call clear comm error: %d\n",GetLastError());
  395. }
  396. WaitForSingleObject(IoSemaphore,-1);
  397. printf("Error from GetMask: %d\n",LastError);
  398. ReleaseSemaphore(IoSemaphore,1,NULL);
  399. }
  400. break;
  401. }
  402. case 2: {
  403. if (!SetEvent(StartWaitEvent)) {
  404. DWORD LastError = GetLastError();
  405. WaitForSingleObject(IoSemaphore,-1);
  406. printf("Could not set StartWaitEvent: %d\n",LastError);
  407. ReleaseSemaphore(IoSemaphore,1,NULL);
  408. }
  409. break;
  410. }
  411. case 3: {
  412. DWORD NewMask = 0;
  413. WaitForSingleObject(IoSemaphore,-1);
  414. printf("%.8x EV_RXCHAR %.8x EV_RXFLAG\n"
  415. "%.8x EV_TXEMPTY %.8x EV_CTS \n"
  416. "%.8x EV_DSR %.8x EV_RLSD \n"
  417. "%.8x EV_BREAK %.8x EV_ERR \n"
  418. "%.8x EV_RING %.8x EV_PERR \n"
  419. "%.8x EV_RX80FULL %.8x EV_EVENT1\n"
  420. "%.8x EV_EVENT2 \n",
  421. EV_RXCHAR ,EV_RXFLAG,
  422. EV_TXEMPTY ,EV_CTS,
  423. EV_DSR ,EV_RLSD,
  424. EV_BREAK ,EV_ERR,
  425. EV_RING ,EV_PERR,
  426. EV_RX80FULL ,EV_EVENT1,
  427. EV_EVENT2);
  428. if ((scanfval = scanf("%x",&NewMask)) != EOF) {
  429. if (scanfval != 1) {
  430. printf("Invalid input\n");
  431. ReleaseSemaphore(IoSemaphore,1,NULL);
  432. continue;
  433. }
  434. printf("Using New mask of: %x\n",NewMask);
  435. ReleaseSemaphore(IoSemaphore,1,NULL);
  436. if (!SetCommMask(
  437. hFile,
  438. NewMask
  439. )) {
  440. DWORD LastError = GetLastError();
  441. WaitForSingleObject(IoSemaphore,-1);
  442. printf("SetCommMask unsuccessful: %d\n",LastError);
  443. ReleaseSemaphore(IoSemaphore,1,NULL);
  444. }
  445. } else {
  446. printf("All done\n");
  447. ReleaseSemaphore(IoSemaphore,1,NULL);
  448. exit(1);
  449. }
  450. break;
  451. }
  452. case 4: {
  453. if (!SetEvent(StartTransmitEvent)) {
  454. DWORD LastError = GetLastError();
  455. WaitForSingleObject(IoSemaphore,-1);
  456. printf("Could not set StartTransmitEvent: %d\n",LastError);
  457. ReleaseSemaphore(IoSemaphore,1,NULL);
  458. }
  459. break;
  460. }
  461. case 5: {
  462. if (!SetEvent(SetBreakEvent)) {
  463. DWORD LastError = GetLastError();
  464. WaitForSingleObject(IoSemaphore,-1);
  465. printf("Could not set SetBreakEvent: %d\n",LastError);
  466. ReleaseSemaphore(IoSemaphore,1,NULL);
  467. }
  468. break;
  469. }
  470. case 6: {
  471. if (!SetEvent(ClrBreakEvent)) {
  472. DWORD LastError = GetLastError();
  473. WaitForSingleObject(IoSemaphore,-1);
  474. printf("Could not set ClrBreakEvt: %d\n",LastError);
  475. ReleaseSemaphore(IoSemaphore,1,NULL);
  476. }
  477. break;
  478. }
  479. }
  480. }
  481. } else {
  482. printf("All done\n");
  483. ReleaseSemaphore(IoSemaphore,1,NULL);
  484. break;
  485. }
  486. } while (TRUE);
  487. }