1 |
/*************************************************************************** |
---|
2 |
Copyright (C) 2005 by Pieter Palmers * |
---|
3 |
* |
---|
4 |
This program is free software; you can redistribute it and/or modify * |
---|
5 |
it under the terms of the GNU General Public License as published by * |
---|
6 |
the Free Software Foundation; either version 2 of the License, or * |
---|
7 |
(at your option) any later version. * |
---|
8 |
* |
---|
9 |
This program is distributed in the hope that it will be useful, * |
---|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
12 |
GNU General Public License for more details. * |
---|
13 |
* |
---|
14 |
You should have received a copy of the GNU General Public License * |
---|
15 |
along with this program; if not, write to the * |
---|
16 |
Free Software Foundation, Inc., * |
---|
17 |
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
---|
18 |
***************************************************************************/ |
---|
19 |
|
---|
20 |
#ifdef HAVE_CONFIG_H |
---|
21 |
#include <config.h> |
---|
22 |
#endif |
---|
23 |
|
---|
24 |
#include <stdio.h> |
---|
25 |
#include <stdlib.h> |
---|
26 |
#include <string.h> |
---|
27 |
#include <endian.h> |
---|
28 |
|
---|
29 |
#include <signal.h> |
---|
30 |
#include "src/debugmodule/debugmodule.h" |
---|
31 |
|
---|
32 |
#include <netinet/in.h> |
---|
33 |
|
---|
34 |
#include "src/libstreaming/cycletimer.h" |
---|
35 |
|
---|
36 |
#include "src/libstreaming/IsoHandler.h" |
---|
37 |
#include "src/libstreaming/IsoStream.h" |
---|
38 |
#include "src/libstreaming/IsoHandlerManager.h" |
---|
39 |
#include "src/libutil/PosixThread.h" |
---|
40 |
|
---|
41 |
#define TEST_PORT_0 |
---|
42 |
// #define TEST_PORT_1 |
---|
43 |
// #define TEST_PORT_2 |
---|
44 |
|
---|
45 |
using namespace FreebobStreaming; |
---|
46 |
using namespace FreebobUtil; |
---|
47 |
|
---|
48 |
DECLARE_GLOBAL_DEBUG_MODULE; |
---|
49 |
|
---|
50 |
int run; |
---|
51 |
|
---|
52 |
struct CYCLE_TIMER_REGISTER { |
---|
53 |
uint16_t seconds; |
---|
54 |
uint16_t cycles; |
---|
55 |
uint16_t offset; |
---|
56 |
}; |
---|
57 |
|
---|
58 |
uint64_t ctr_to_quadlet(struct CYCLE_TIMER_REGISTER x) { |
---|
59 |
uint64_t retval=0; |
---|
60 |
|
---|
61 |
x.seconds &= 0x7F; |
---|
62 |
|
---|
63 |
x.cycles &= 0x1FFF; |
---|
64 |
x.cycles %= 8000; |
---|
65 |
|
---|
66 |
x.offset &= 0xFFF; |
---|
67 |
x.offset %= 3072; |
---|
68 |
|
---|
69 |
retval = (x.seconds << 25) + (x.cycles << 12) + (x.offset); |
---|
70 |
return retval & 0xFFFFFFFF; |
---|
71 |
} |
---|
72 |
|
---|
73 |
static void sighandler (int sig) |
---|
74 |
{ |
---|
75 |
run = 0; |
---|
76 |
} |
---|
77 |
|
---|
78 |
int do_cycletimer_test() { |
---|
79 |
|
---|
80 |
struct CYCLE_TIMER_REGISTER cycle_timer; |
---|
81 |
uint32_t *cycle_timer_as_uint=(uint32_t *)&cycle_timer; |
---|
82 |
|
---|
83 |
uint32_t i=0; |
---|
84 |
uint32_t targetval=0; |
---|
85 |
|
---|
86 |
int failures=0; |
---|
87 |
|
---|
88 |
|
---|
89 |
// test 1 |
---|
90 |
// |
---|
91 |
|
---|
92 |
*cycle_timer_as_uint=0; |
---|
93 |
for (i=0;i<3072;i++) { |
---|
94 |
cycle_timer.offset=i; |
---|
95 |
targetval=CYCLE_TIMER_GET_OFFSET(ctr_to_quadlet(cycle_timer)); |
---|
96 |
|
---|
97 |
if(targetval != i) { |
---|
98 |
debugOutput(DEBUG_LEVEL_NORMAL, " test1 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval); |
---|
99 |
failures++; |
---|
100 |
} |
---|
101 |
} |
---|
102 |
|
---|
103 |
for (i=0;i<8000;i++) { |
---|
104 |
cycle_timer.cycles=i; |
---|
105 |
targetval=CYCLE_TIMER_GET_CYCLES(ctr_to_quadlet(cycle_timer)); |
---|
106 |
|
---|
107 |
if(targetval != i) { |
---|
108 |
debugOutput(DEBUG_LEVEL_NORMAL, " test2 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval); |
---|
109 |
failures++; |
---|
110 |
} |
---|
111 |
} |
---|
112 |
|
---|
113 |
for (i=0;i<128;i++) { |
---|
114 |
cycle_timer.seconds=i; |
---|
115 |
targetval=CYCLE_TIMER_GET_SECS(ctr_to_quadlet(cycle_timer)); |
---|
116 |
|
---|
117 |
if(targetval != i) { |
---|
118 |
debugOutput(DEBUG_LEVEL_NORMAL, " test3 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval); |
---|
119 |
failures++; |
---|
120 |
} |
---|
121 |
} |
---|
122 |
|
---|
123 |
|
---|
124 |
// a value in ticks |
---|
125 |
// should be: 10sec, 1380cy, 640ticks |
---|
126 |
targetval=250000000L; |
---|
127 |
cycle_timer.seconds = TICKS_TO_SECS(targetval); |
---|
128 |
cycle_timer.cycles = TICKS_TO_CYCLES(targetval); |
---|
129 |
cycle_timer.offset = TICKS_TO_OFFSET(targetval); |
---|
130 |
|
---|
131 |
if((cycle_timer.seconds != 10) | |
---|
132 |
(cycle_timer.cycles != 1380) | |
---|
133 |
(cycle_timer.offset != 640)) |
---|
134 |
{ |
---|
135 |
debugOutput(DEBUG_LEVEL_NORMAL, " test4 failed: (%u,10)sec (%u,1380)cy (%u,640)ticks\n", |
---|
136 |
cycle_timer.seconds,cycle_timer.cycles,cycle_timer.offset); |
---|
137 |
failures++; |
---|
138 |
} else { |
---|
139 |
debugOutput(DEBUG_LEVEL_NORMAL, " test4 ok\n"); |
---|
140 |
} |
---|
141 |
|
---|
142 |
i=TICKS_TO_CYCLE_TIMER(targetval); |
---|
143 |
if (i != 0x14564280) { |
---|
144 |
debugOutput(DEBUG_LEVEL_NORMAL, " test5 failed: (0x%08X,0x14564280)\n", |
---|
145 |
i); |
---|
146 |
failures++; |
---|
147 |
} else { |
---|
148 |
debugOutput(DEBUG_LEVEL_NORMAL, " test5 ok\n"); |
---|
149 |
} |
---|
150 |
|
---|
151 |
targetval=CYCLE_TIMER_TO_TICKS(i); |
---|
152 |
if (targetval!=250000000L) { |
---|
153 |
debugOutput(DEBUG_LEVEL_NORMAL, " test6 failed: (%u,250000000)\n", |
---|
154 |
targetval); |
---|
155 |
failures++; |
---|
156 |
} else { |
---|
157 |
debugOutput(DEBUG_LEVEL_NORMAL, " test6 ok\n"); |
---|
158 |
} |
---|
159 |
|
---|
160 |
int32_t subs; |
---|
161 |
subs=diffTicks(10, 8); |
---|
162 |
if (subs != 2) { |
---|
163 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(10, 8) != 2 : %ld\n", |
---|
164 |
subs); |
---|
165 |
failures++; |
---|
166 |
} |
---|
167 |
|
---|
168 |
subs=diffTicks(10, 12); |
---|
169 |
if (subs != -2) { |
---|
170 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(10, 12) != -2 : %ld\n", |
---|
171 |
subs); |
---|
172 |
failures++; |
---|
173 |
} |
---|
174 |
|
---|
175 |
subs=diffTicks(TICKS_PER_SECOND*128L + 10, 8); |
---|
176 |
if (subs != 2) { |
---|
177 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(TICKS_PER_SECOND*128L + 10, 8) != 2 : %ld\n", |
---|
178 |
subs); |
---|
179 |
failures++; |
---|
180 |
} |
---|
181 |
|
---|
182 |
subs=diffTicks(TICKS_PER_SECOND*128L + 10, 12); |
---|
183 |
if (subs != -2) { |
---|
184 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(TICKS_PER_SECOND*128L + 10, 12) != -2 : %ld\n", |
---|
185 |
subs); |
---|
186 |
failures++; |
---|
187 |
} |
---|
188 |
|
---|
189 |
subs=diffTicks(10, TICKS_PER_SECOND*128L + 8); |
---|
190 |
if (subs != 2) { |
---|
191 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(10, TICKS_PER_SECOND*128L + 8) != 2 : %ld\n", |
---|
192 |
subs); |
---|
193 |
failures++; |
---|
194 |
} |
---|
195 |
|
---|
196 |
subs=diffTicks(10, TICKS_PER_SECOND*128L + 12); |
---|
197 |
if (subs != -2) { |
---|
198 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(10, TICKS_PER_SECOND*128L + 12) != -2 : %l011llu\n", |
---|
199 |
subs); |
---|
200 |
failures++; |
---|
201 |
} |
---|
202 |
|
---|
203 |
//--------- |
---|
204 |
// now = 10sec, 1380cy, 640ticks |
---|
205 |
|
---|
206 |
uint32_t st=sytRecvToFullTicks(0x1234, 1000, 0x14564280); |
---|
207 |
if (st != 248860212LLU) { |
---|
208 |
debugOutput(DEBUG_LEVEL_NORMAL, " sytToRecvFullTicks(0x1234, 1000, 0x14564280) != 248860212 : %011lu\n", |
---|
209 |
st); |
---|
210 |
failures++; |
---|
211 |
} |
---|
212 |
|
---|
213 |
st=sytRecvToFullTicks(0xB2B6, 7000, TICKS_TO_CYCLE_TIMER(3118082282LU)); |
---|
214 |
if (st != 3118089910LLU) { |
---|
215 |
debugOutput(DEBUG_LEVEL_NORMAL, " sytToRecvFullTicks(0x1234, 1000, %08X) != 3118089910 : %011lu\n", |
---|
216 |
TICKS_TO_CYCLE_TIMER(3118082282LU), st); |
---|
217 |
failures++; |
---|
218 |
} |
---|
219 |
|
---|
220 |
st=sytXmitToFullTicks(0xC4EA, 3000, TICKS_TO_CYCLE_TIMER(2958285668LU)); |
---|
221 |
if (st != 2958349546LLU) { |
---|
222 |
debugOutput(DEBUG_LEVEL_NORMAL, " sytToXmitFullTicks(0x1234, 1000, %08X) != 2958349546 : %011lu\n", |
---|
223 |
TICKS_TO_CYCLE_TIMER(2958285668LU), st); |
---|
224 |
failures++; |
---|
225 |
} |
---|
226 |
|
---|
227 |
if (failures) { |
---|
228 |
debugOutput(DEBUG_LEVEL_NORMAL, " %d failures\n",failures); |
---|
229 |
return -1; |
---|
230 |
} else { |
---|
231 |
debugOutput(DEBUG_LEVEL_NORMAL, " no failures\n"); |
---|
232 |
} |
---|
233 |
return 0; |
---|
234 |
} |
---|
235 |
|
---|
236 |
int main(int argc, char *argv[]) |
---|
237 |
{ |
---|
238 |
|
---|
239 |
run=1; |
---|
240 |
|
---|
241 |
IsoHandlerManager *m_isoManager=NULL; |
---|
242 |
|
---|
243 |
#ifdef TEST_PORT_0 |
---|
244 |
IsoStream *s=NULL; |
---|
245 |
#endif |
---|
246 |
#ifdef TEST_PORT_1 |
---|
247 |
IsoStream *s2=NULL; |
---|
248 |
#endif |
---|
249 |
#ifdef TEST_PORT_2 |
---|
250 |
IsoStream *s3=NULL; |
---|
251 |
#endif |
---|
252 |
|
---|
253 |
signal (SIGINT, sighandler); |
---|
254 |
signal (SIGPIPE, sighandler); |
---|
255 |
|
---|
256 |
debugOutput(DEBUG_LEVEL_NORMAL, "Freebob Cycle timer test application\n"); |
---|
257 |
|
---|
258 |
debugOutput(DEBUG_LEVEL_NORMAL, "Testing cycle timer helper functions & macro's... \n"); |
---|
259 |
if(do_cycletimer_test()) { |
---|
260 |
debugOutput(DEBUG_LEVEL_NORMAL, " !!! FAILED !!!\n"); |
---|
261 |
exit(1); |
---|
262 |
} else { |
---|
263 |
debugOutput(DEBUG_LEVEL_NORMAL, " !!! PASSED !!!\n"); |
---|
264 |
} |
---|
265 |
|
---|
266 |
// exit(1); |
---|
267 |
|
---|
268 |
m_isoManager=new IsoHandlerManager(); |
---|
269 |
|
---|
270 |
if(!m_isoManager) { |
---|
271 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoHandlerManager\n"); |
---|
272 |
goto finish; |
---|
273 |
} |
---|
274 |
|
---|
275 |
m_isoManager->setVerboseLevel(DEBUG_LEVEL_VERBOSE); |
---|
276 |
|
---|
277 |
if(!m_isoManager->init()) { |
---|
278 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not init() IsoHandlerManager\n"); |
---|
279 |
goto finish; |
---|
280 |
} |
---|
281 |
|
---|
282 |
|
---|
283 |
#ifdef TEST_PORT_0 |
---|
284 |
// add a stream to the manager so that it has something to do |
---|
285 |
s=new IsoStream(IsoStream::EST_Receive, 0); |
---|
286 |
|
---|
287 |
if (!s) { |
---|
288 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n"); |
---|
289 |
goto finish; |
---|
290 |
} |
---|
291 |
|
---|
292 |
s->setVerboseLevel(DEBUG_LEVEL_VERBOSE); |
---|
293 |
|
---|
294 |
if (!s->init()) { |
---|
295 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n"); |
---|
296 |
goto finish; |
---|
297 |
} |
---|
298 |
|
---|
299 |
s->setChannel(0); |
---|
300 |
|
---|
301 |
if(!m_isoManager->registerStream(s)) { |
---|
302 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n"); |
---|
303 |
goto finish; |
---|
304 |
} |
---|
305 |
#endif |
---|
306 |
|
---|
307 |
#ifdef TEST_PORT_1 |
---|
308 |
// add a stream to the manager so that it has something to do |
---|
309 |
s2=new IsoStream(IsoStream::EST_Receive, 1); |
---|
310 |
|
---|
311 |
if (!s2) { |
---|
312 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n"); |
---|
313 |
goto finish; |
---|
314 |
} |
---|
315 |
|
---|
316 |
s2->setVerboseLevel(DEBUG_LEVEL_VERBOSE); |
---|
317 |
|
---|
318 |
if (!s2->init()) { |
---|
319 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n"); |
---|
320 |
goto finish; |
---|
321 |
} |
---|
322 |
|
---|
323 |
s2->setChannel(0); |
---|
324 |
|
---|
325 |
if(!m_isoManager->registerStream(s2)) { |
---|
326 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n"); |
---|
327 |
goto finish; |
---|
328 |
} |
---|
329 |
#endif |
---|
330 |
|
---|
331 |
#ifdef TEST_PORT_2 |
---|
332 |
// add a stream to the manager so that it has something to do |
---|
333 |
s3=new IsoStream(IsoStream::EST_Receive,2); |
---|
334 |
|
---|
335 |
if (!s3) { |
---|
336 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n"); |
---|
337 |
goto finish; |
---|
338 |
} |
---|
339 |
|
---|
340 |
s3->setVerboseLevel(DEBUG_LEVEL_VERBOSE); |
---|
341 |
|
---|
342 |
if (!s3->init()) { |
---|
343 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n"); |
---|
344 |
goto finish; |
---|
345 |
} |
---|
346 |
|
---|
347 |
s3->setChannel(0); |
---|
348 |
|
---|
349 |
if(!m_isoManager->registerStream(s3)) { |
---|
350 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n"); |
---|
351 |
goto finish; |
---|
352 |
} |
---|
353 |
#endif |
---|
354 |
|
---|
355 |
debugOutput(DEBUG_LEVEL_NORMAL, "Preparing IsoHandlerManager...\n"); |
---|
356 |
if (!m_isoManager->prepare()) { |
---|
357 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not prepare isoManager\n"); |
---|
358 |
goto finish; |
---|
359 |
} |
---|
360 |
|
---|
361 |
debugOutput(DEBUG_LEVEL_NORMAL, "Starting IsoHandler...\n"); |
---|
362 |
if (!m_isoManager->startHandlers(0)) { |
---|
363 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not start handlers...\n"); |
---|
364 |
goto finish; |
---|
365 |
} |
---|
366 |
|
---|
367 |
while(run) { |
---|
368 |
sleep(1); |
---|
369 |
m_isoManager->dumpInfo(); |
---|
370 |
} |
---|
371 |
|
---|
372 |
debugOutput(DEBUG_LEVEL_NORMAL, "Stopping handlers...\n"); |
---|
373 |
if(!m_isoManager->stopHandlers()) { |
---|
374 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not stop ISO handlers\n"); |
---|
375 |
goto finish; |
---|
376 |
} |
---|
377 |
|
---|
378 |
#ifdef TEST_PORT_0 |
---|
379 |
if(!m_isoManager->unregisterStream(s)) { |
---|
380 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n"); |
---|
381 |
goto finish; |
---|
382 |
} |
---|
383 |
delete s; |
---|
384 |
#endif |
---|
385 |
|
---|
386 |
#ifdef TEST_PORT_1 |
---|
387 |
if(!m_isoManager->unregisterStream(s1)) { |
---|
388 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n"); |
---|
389 |
goto finish; |
---|
390 |
} |
---|
391 |
delete s1; |
---|
392 |
#endif |
---|
393 |
|
---|
394 |
#ifdef TEST_PORT_2 |
---|
395 |
if(!m_isoManager->unregisterStream(s2)) { |
---|
396 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n"); |
---|
397 |
goto finish; |
---|
398 |
} |
---|
399 |
delete s2; |
---|
400 |
#endif |
---|
401 |
|
---|
402 |
delete m_isoManager; |
---|
403 |
|
---|
404 |
finish: |
---|
405 |
debugOutput(DEBUG_LEVEL_NORMAL, "Bye...\n"); |
---|
406 |
|
---|
407 |
return EXIT_SUCCESS; |
---|
408 |
} |
---|