1 | /* |
---|
2 | * buffer.c : generate an executable to test the sockets. |
---|
3 | * it is used to create/test the sent/receive messages |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | */ |
---|
7 | |
---|
8 | #include <stdlib.h> |
---|
9 | #include "buffer.h" |
---|
10 | |
---|
11 | typedef unsigned char BOOLEAN; |
---|
12 | typedef BOOLEAN *PBOOLEAN; |
---|
13 | |
---|
14 | #define TRUE 1 |
---|
15 | #define FALSE 0 |
---|
16 | #define SIZE_INTERVAL 50 |
---|
17 | #define CHAR_BUFFER '0' |
---|
18 | #define CHAR_LIMIT 'L' |
---|
19 | #define CHAR_END 'E' |
---|
20 | |
---|
21 | static unsigned long s_BufferId_ul = 0; |
---|
22 | static unsigned long s_BufferId_ul_rcv = 1; |
---|
23 | |
---|
24 | static int Length = 10; |
---|
25 | static int koYes = 0; |
---|
26 | static int frameAfterKo = 5; |
---|
27 | |
---|
28 | unsigned char * BuildBuffer() { |
---|
29 | unsigned char * Buffer_puc; |
---|
30 | unsigned long * Buffer_pul; |
---|
31 | int i; |
---|
32 | int SizeDataBuffer_i; |
---|
33 | |
---|
34 | |
---|
35 | /* Seed the random-number generator with current time so that |
---|
36 | * the numbers will be different every time we run. |
---|
37 | */ |
---|
38 | /*srand( (unsigned int)(s_BufferId_ul*time(NULL)));*/ |
---|
39 | |
---|
40 | Length++; |
---|
41 | if (Length > 2000) |
---|
42 | Length = 10; |
---|
43 | |
---|
44 | Buffer_puc = (unsigned char *)malloc(Length); |
---|
45 | if( Buffer_puc == NULL ) |
---|
46 | printf( "Insufficient memory available\n" ); |
---|
47 | |
---|
48 | /* Build the buffer |
---|
49 | * The first word = The length of the buffer |
---|
50 | * Every SIZE_INTERVAL char write the CHAR_LIMIT |
---|
51 | * The last cahr is CHAR_END |
---|
52 | * The other char is CHAR_BUFFER */ |
---|
53 | |
---|
54 | |
---|
55 | /* Intialize the length of the buffer in the first word */ |
---|
56 | Buffer_pul = (unsigned long *)Buffer_puc; |
---|
57 | *Buffer_pul = Length; |
---|
58 | |
---|
59 | /* Intialize the length of the buffer in the first word*/ |
---|
60 | Buffer_pul++; |
---|
61 | s_BufferId_ul++; |
---|
62 | *Buffer_pul = s_BufferId_ul; |
---|
63 | |
---|
64 | /* Intialize the buffer with the CHAR_LIMIT and CHAR_BUFFER */ |
---|
65 | SizeDataBuffer_i = Length-8; |
---|
66 | Buffer_puc+=8; |
---|
67 | |
---|
68 | for (i= 0; i<SizeDataBuffer_i; i++){ |
---|
69 | if( (i%SIZE_INTERVAL)==0 ) |
---|
70 | Buffer_puc[i] = CHAR_LIMIT; |
---|
71 | else |
---|
72 | Buffer_puc[i] = CHAR_BUFFER; |
---|
73 | } |
---|
74 | |
---|
75 | Buffer_puc[SizeDataBuffer_i-2] = CHAR_END; |
---|
76 | Buffer_puc[SizeDataBuffer_i-1] = '\0'; |
---|
77 | |
---|
78 | Buffer_puc-=8; |
---|
79 | |
---|
80 | printf("\nBuffer sent ID=<%ld> and length=<%d>/<0x%x>\n", s_BufferId_ul, Length, Length); |
---|
81 | fflush(stdout); |
---|
82 | |
---|
83 | return Buffer_puc; |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | void FreeBuffer(unsigned char * p_Buffer_puc){ |
---|
89 | free(p_Buffer_puc); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | unsigned char * AllocBuffer() |
---|
94 | { |
---|
95 | unsigned char * Buffer_puc; |
---|
96 | |
---|
97 | Buffer_puc = (unsigned char *)malloc(SIZE_MAX_RCV_BUFFER); |
---|
98 | |
---|
99 | if( Buffer_puc == NULL ){ |
---|
100 | printf("Insufficient memory available\n"); |
---|
101 | fflush(stdout); |
---|
102 | } |
---|
103 | return Buffer_puc; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | unsigned long CheckBuffer(unsigned char * p_Buffer_puc) |
---|
109 | { |
---|
110 | |
---|
111 | BOOLEAN CheckBuffer_b = TRUE; |
---|
112 | unsigned long * Buffer_pul; |
---|
113 | unsigned long LenBuffer_ul; |
---|
114 | int i=0; |
---|
115 | unsigned long BufferId_ul, j; |
---|
116 | |
---|
117 | |
---|
118 | /*Retrieve the length of the buffer*/ |
---|
119 | Buffer_pul = (unsigned long *)p_Buffer_puc; |
---|
120 | LenBuffer_ul = *Buffer_pul; |
---|
121 | |
---|
122 | /* Retrieve the Id of the buffer*/ |
---|
123 | Buffer_pul++; |
---|
124 | BufferId_ul = *Buffer_pul; |
---|
125 | |
---|
126 | if (BufferId_ul < s_BufferId_ul_rcv) { |
---|
127 | s_BufferId_ul_rcv = BufferId_ul; |
---|
128 | } |
---|
129 | |
---|
130 | if (BufferId_ul > s_BufferId_ul_rcv) { |
---|
131 | for (j = 0; j < (BufferId_ul - s_BufferId_ul_rcv); j++) { |
---|
132 | printf("!!!!!!!! Buffer ID=<%ld> lost !!!!!!!!\n", s_BufferId_ul_rcv); |
---|
133 | fflush(stdout); |
---|
134 | s_BufferId_ul_rcv++ ; |
---|
135 | } |
---|
136 | /* error */ |
---|
137 | koYes = 0; /* if koYes = 1 here, the prog will stop 5 reception after */ |
---|
138 | } |
---|
139 | |
---|
140 | s_BufferId_ul_rcv++ ; |
---|
141 | |
---|
142 | /* Print the information*/ |
---|
143 | printf("Buffer receive ID=<%ld> and length=<%ld>/<0x%lx> :",BufferId_ul,LenBuffer_ul,LenBuffer_ul); |
---|
144 | fflush(stdout); |
---|
145 | |
---|
146 | /* Check the end character*/ |
---|
147 | if(p_Buffer_puc[LenBuffer_ul-2]!=CHAR_END){ |
---|
148 | CheckBuffer_b = FALSE; |
---|
149 | } |
---|
150 | |
---|
151 | /* Check the sequence characters*/ |
---|
152 | p_Buffer_puc+=8; |
---|
153 | |
---|
154 | while((CheckBuffer_b)&&(i<(int)(LenBuffer_ul-10))) |
---|
155 | { |
---|
156 | |
---|
157 | if( (i%SIZE_INTERVAL)==0 ) |
---|
158 | CheckBuffer_b = (p_Buffer_puc[i]==CHAR_LIMIT); |
---|
159 | else |
---|
160 | CheckBuffer_b = (p_Buffer_puc[i]==CHAR_BUFFER); |
---|
161 | |
---|
162 | i++; |
---|
163 | } |
---|
164 | |
---|
165 | p_Buffer_puc-=8; |
---|
166 | |
---|
167 | if (CheckBuffer_b) { |
---|
168 | printf("OK\n"); |
---|
169 | fflush(stdout); |
---|
170 | } |
---|
171 | else { |
---|
172 | /* error */ |
---|
173 | koYes = 0; /* if koYes = 1 here, the prog will stop 5 receptions after */ |
---|
174 | printf("KO ???\n"); |
---|
175 | fflush(stdout); |
---|
176 | } |
---|
177 | |
---|
178 | if (koYes){ |
---|
179 | frameAfterKo-- ; |
---|
180 | if (frameAfterKo == 0) |
---|
181 | return 0; |
---|
182 | } |
---|
183 | |
---|
184 | return BufferId_ul; |
---|
185 | |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | |
---|