Source code of Windows XP (NT5)
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.

495 lines
11 KiB

  1. //
  2. // Tests resize of buffer.
  3. //
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include "windows.h"
  9. //
  10. // This program assumes that it is using a loopback connector.
  11. //
  12. #define MAX_CHECK 100000
  13. ULONG CheckValues[MAX_CHECK];
  14. ULONG ReadValues[MAX_CHECK];
  15. void main(int argc,char *argv[]) {
  16. HANDLE hFile;
  17. COMMPROP mp;
  18. DCB MyDcb;
  19. ULONG i;
  20. ULONG actuallyIoed;
  21. ULONG firstReadSize;
  22. ULONG secondReadSize;
  23. ULONG secondWriteSize;
  24. COMMTIMEOUTS cto;
  25. char *MyPort = "COM1";
  26. COMSTAT stat;
  27. DWORD errors;
  28. DWORD increment = 1066;
  29. DWORD baudRate = 19200;
  30. cto.ReadIntervalTimeout = (DWORD)~0;
  31. cto.ReadTotalTimeoutMultiplier = (DWORD)0;
  32. cto.ReadTotalTimeoutConstant = (DWORD)0;
  33. cto.WriteTotalTimeoutMultiplier = (DWORD)1;
  34. cto.WriteTotalTimeoutConstant = (DWORD)1000;
  35. if (argc > 1) {
  36. MyPort = argv[1];
  37. if (argc > 2) {
  38. sscanf(argv[2],"%d",&increment);
  39. if (argc > 3) {
  40. sscanf(argv[3],"%d",&baudRate);
  41. }
  42. }
  43. }
  44. //
  45. // Put a sequence number in each long. As an extra check
  46. // turn the high bit on.
  47. //
  48. for (
  49. i = 0;
  50. i < MAX_CHECK;
  51. i++
  52. ) {
  53. CheckValues[i] = i;
  54. CheckValues[i] |= 0x80000000;
  55. }
  56. if ((hFile = CreateFile(
  57. MyPort,
  58. GENERIC_READ | GENERIC_WRITE,
  59. 0,
  60. NULL,
  61. CREATE_ALWAYS,
  62. FILE_ATTRIBUTE_NORMAL,
  63. NULL
  64. )) == ((HANDLE)-1)) {
  65. printf("Couldn't open the port %s\n",MyPort);
  66. exit(1);
  67. }
  68. if (!GetCommState(
  69. hFile,
  70. &MyDcb
  71. )) {
  72. printf("We couldn't get the comm state\n");
  73. exit(1);
  74. }
  75. //
  76. // Set the baud to 19200 and the data bits to 8
  77. // (We want 8 so that we don't lose any of our data.)
  78. //
  79. MyDcb.BaudRate = baudRate;
  80. MyDcb.ByteSize = 8;
  81. if (!SetCommState(
  82. hFile,
  83. &MyDcb
  84. )) {
  85. printf("Couldn't set the baud/data for port %s\n");
  86. exit(1);
  87. }
  88. //
  89. // All the data should be in memory, we just set a timeout
  90. // so that if we do get hung up, the reads will return.
  91. //
  92. if (!SetCommTimeouts(
  93. hFile,
  94. &cto
  95. )) {
  96. printf("Couldn't set the timeouts.\n");
  97. exit(1);
  98. }
  99. //
  100. // Find out how many bytes are available in the RX buffer.
  101. //
  102. if (!GetCommProperties(
  103. hFile,
  104. &mp
  105. )) {
  106. printf("Couldn't get the properties for port %s\n",MyPort);
  107. exit(1);
  108. }
  109. while (mp.dwCurrentRxQueue <= (MAX_CHECK*sizeof(ULONG))) {
  110. printf("RX buffer size: %d\r",mp.dwCurrentRxQueue);
  111. //
  112. // Write out the number of bytes that the RX buffer can
  113. // hold.
  114. //
  115. // The we read in half (approximately) of those bytes. After
  116. // we read the half, we resize the buffer. We then read in
  117. // the rest of those bytes. We then check the values against
  118. // what they "should" be.
  119. //
  120. if (!WriteFile(
  121. hFile,
  122. &CheckValues[0],
  123. mp.dwCurrentRxQueue,
  124. &actuallyIoed,
  125. NULL
  126. )) {
  127. printf("\nDidn't get all of the characters written %d\n",actuallyIoed);
  128. exit(1);
  129. }
  130. if (actuallyIoed != mp.dwCurrentRxQueue) {
  131. printf("\nfirst write Timed out with IO of %d\n",actuallyIoed);
  132. exit(1);
  133. }
  134. Sleep(2);
  135. //
  136. // Call clear comm error so that we can see how many characters
  137. // are in the typeahead buffer.
  138. //
  139. if (!ClearCommError(
  140. hFile,
  141. &errors,
  142. &stat
  143. )) {
  144. printf("\nCouldn't call clear comm status after first write.\n");
  145. exit(1);
  146. }
  147. if (stat.cbInQue != mp.dwCurrentRxQueue) {
  148. printf("\nAfter first write the typeahead buffer is incorrect %d - %x\n",
  149. stat.cbInQue,errors);
  150. exit(1);
  151. }
  152. firstReadSize = mp.dwCurrentRxQueue / 2;
  153. secondReadSize = mp.dwCurrentRxQueue - firstReadSize;
  154. if (!ReadFile(
  155. hFile,
  156. &ReadValues[0],
  157. firstReadSize,
  158. &actuallyIoed,
  159. NULL
  160. )) {
  161. printf("\nDidn't get all of the characters read %d\n",actuallyIoed);
  162. exit(1);
  163. }
  164. if (actuallyIoed != firstReadSize) {
  165. printf("\nfirst read Timed out with IO of %d\n",actuallyIoed);
  166. exit(1);
  167. }
  168. Sleep(2);
  169. //
  170. // Call clear comm error so that we can see how many characters
  171. // are in the typeahead buffer.
  172. //
  173. if (!ClearCommError(
  174. hFile,
  175. &errors,
  176. &stat
  177. )) {
  178. printf("\nCouldn't call clear comm status after first read.\n");
  179. exit(1);
  180. }
  181. if (stat.cbInQue != secondReadSize) {
  182. printf("\nAfter first read the typeahead buffer is incorrect %d\n",
  183. stat.cbInQue);
  184. exit(1);
  185. }
  186. Sleep(100);
  187. mp.dwCurrentRxQueue += increment;
  188. if (!SetupComm(
  189. hFile,
  190. mp.dwCurrentRxQueue,
  191. mp.dwCurrentTxQueue
  192. )) {
  193. printf("\nCouldn't resize the buffer to %d\n",mp.dwCurrentRxQueue);
  194. exit(1);
  195. }
  196. Sleep(2);
  197. //
  198. // Call clear comm error so that we can see how many characters
  199. // are in the typeahead buffer.
  200. //
  201. if (!ClearCommError(
  202. hFile,
  203. &errors,
  204. &stat
  205. )) {
  206. printf("\nCouldn't call clear comm status after resize.\n");
  207. exit(1);
  208. }
  209. if (stat.cbInQue != secondReadSize) {
  210. printf("\nAfter resize the typeahead buffer is incorrect %d\n",
  211. stat.cbInQue);
  212. exit(1);
  213. }
  214. //
  215. // It's been resized. Fill up the remaining.
  216. //
  217. secondWriteSize = mp.dwCurrentRxQueue - secondReadSize;
  218. if (!WriteFile(
  219. hFile,
  220. &CheckValues[0],
  221. secondWriteSize,
  222. &actuallyIoed,
  223. NULL
  224. )) {
  225. printf("\nDidn't write all of the chars second time %d\n",actuallyIoed);
  226. exit(1);
  227. }
  228. if (actuallyIoed != secondWriteSize) {
  229. printf("\nsecond write Timed out with IO of %d\n",actuallyIoed);
  230. exit(1);
  231. }
  232. Sleep(2);
  233. //
  234. // Call clear comm error so that we can see how many characters
  235. // are in the typeahead buffer.
  236. //
  237. if (!ClearCommError(
  238. hFile,
  239. &errors,
  240. &stat
  241. )) {
  242. printf("\nCouldn't call clear comm status after resize.\n");
  243. exit(1);
  244. }
  245. if (stat.cbInQue != mp.dwCurrentRxQueue) {
  246. printf("\nAfter second write the typeahead buffer is incorrect %d\n",
  247. stat.cbInQue);
  248. exit(1);
  249. }
  250. //
  251. // We resized the buffer, see if we can get the rest of the
  252. // characters from the first write.
  253. //
  254. if (!ReadFile(
  255. hFile,
  256. ((PUCHAR)&ReadValues[0])+firstReadSize,
  257. secondReadSize,
  258. &actuallyIoed,
  259. NULL
  260. )) {
  261. printf("\nDidn't get all of the characters read(2) %d\n",actuallyIoed);
  262. exit(1);
  263. }
  264. if (actuallyIoed != secondReadSize) {
  265. printf("\nsecond read Timed out with IO of %d\n",actuallyIoed);
  266. exit(1);
  267. }
  268. Sleep(2);
  269. //
  270. // Call clear comm error so that we can see how many characters
  271. // are in the typeahead buffer.
  272. //
  273. if (!ClearCommError(
  274. hFile,
  275. &errors,
  276. &stat
  277. )) {
  278. printf("\nCouldn't call clear comm status after resize.\n");
  279. exit(1);
  280. }
  281. if (stat.cbInQue != secondWriteSize) {
  282. printf("\nAfter second read the typeahead buffer is incorrect %d\n",
  283. stat.cbInQue);
  284. exit(1);
  285. }
  286. //
  287. // Now check that what we read was what we sent.
  288. //
  289. for (
  290. i = 0;
  291. i < (((firstReadSize+secondReadSize)+(sizeof(DWORD)-1)) / sizeof(DWORD)) - 1;
  292. i++
  293. ) {
  294. if (ReadValues[i] != CheckValues[i]) {
  295. printf("\nAt index %d - values are read %x - check %x\n",
  296. i,ReadValues[i],CheckValues[i]);
  297. exit(1);
  298. }
  299. }
  300. //
  301. // Get the chars we wrote on the second write and make
  302. // sure that they are good also.
  303. //
  304. if (!ReadFile(
  305. hFile,
  306. &ReadValues[0],
  307. secondWriteSize,
  308. &actuallyIoed,
  309. NULL
  310. )) {
  311. printf("\nDidn't get all of the characters read(3) %d\n",actuallyIoed);
  312. exit(1);
  313. }
  314. if (actuallyIoed != secondWriteSize) {
  315. printf("\nthird read Timed out with IO of %d\n",actuallyIoed);
  316. exit(1);
  317. }
  318. Sleep(2);
  319. //
  320. // Call clear comm error so that we can see how many characters
  321. // are in the typeahead buffer.
  322. //
  323. if (!ClearCommError(
  324. hFile,
  325. &errors,
  326. &stat
  327. )) {
  328. printf("\nCouldn't call clear comm status after resize.\n");
  329. exit(1);
  330. }
  331. if (stat.cbInQue) {
  332. printf("\nAfter second read the typeahead buffer is incorrect %d\n",
  333. stat.cbInQue);
  334. exit(1);
  335. }
  336. //
  337. // Now check that what we read was what we sent.
  338. //
  339. for (
  340. i = 0;
  341. i < ((secondWriteSize+(sizeof(DWORD)-1)) / sizeof(DWORD)) - 1;
  342. i++
  343. ) {
  344. if (ReadValues[i] != CheckValues[i]) {
  345. printf("\nAt on read(3) index %d - values are read %x - check %x\n",
  346. i,ReadValues[i],CheckValues[i]);
  347. exit(1);
  348. }
  349. }
  350. }
  351. }