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