root/branches/streaming-rework/tests/test-cycletimer.cpp

Revision 384, 9.2 kB (checked in by pieterpalmers, 16 years ago)

- temporary commit as backup measure
- rewrote synchronisation code
- receive streaming based on SYT works
- transmit streaming synced to received stream sort of works, still

have to iron out some issues.

NOTE: all devices but the bebob's are disabled in this code,

because they still have to be ported to the new sync
mechanism.

Line 
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     if (failures) {
161         debugOutput(DEBUG_LEVEL_NORMAL, " %d failures\n",failures);
162         return -1;
163     } else {
164         debugOutput(DEBUG_LEVEL_NORMAL, " no failures\n");
165     }
166     return 0;
167 }
168
169 int main(int argc, char *argv[])
170 {
171
172         run=1;
173        
174         IsoHandlerManager *m_isoManager=NULL;
175     PosixThread * m_isoManagerThread=NULL;
176    
177 #ifdef TEST_PORT_0     
178     IsoStream *s=NULL;
179 #endif
180 #ifdef TEST_PORT_1     
181     IsoStream *s2=NULL;
182 #endif
183 #ifdef TEST_PORT_2
184     IsoStream *s3=NULL;
185 #endif
186
187         signal (SIGINT, sighandler);
188         signal (SIGPIPE, sighandler);
189
190         debugOutput(DEBUG_LEVEL_NORMAL, "Freebob Cycle timer test application\n");
191        
192         debugOutput(DEBUG_LEVEL_NORMAL, "Testing cycle timer helper functions & macro's... \n");
193         if(do_cycletimer_test()) {
194            debugOutput(DEBUG_LEVEL_NORMAL, " !!! FAILED !!!\n");
195            exit(1);
196         } else {
197            debugOutput(DEBUG_LEVEL_NORMAL, " !!! PASSED !!!\n");
198         }
199        
200 //      exit(1);
201        
202         m_isoManager=new IsoHandlerManager();
203        
204         if(!m_isoManager) {
205                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoHandlerManager\n");
206                 goto finish;
207         }
208        
209         m_isoManager->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
210                
211     // the thread to iterate the manager
212         m_isoManagerThread=new PosixThread(
213               m_isoManager,
214               true, 80,
215               PTHREAD_CANCEL_DEFERRED);
216              
217         if(!m_isoManagerThread) {
218                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create iso manager thread\n");
219                 goto finish;
220         }
221
222 #ifdef TEST_PORT_0     
223         // add a stream to the manager so that it has something to do
224         s=new IsoStream(IsoStream::EST_Receive, 0);
225        
226         if (!s) {
227                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
228                 goto finish;
229         }       
230        
231         s->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
232        
233         if (!s->init()) {
234                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
235                 goto finish;
236         }
237        
238         s->setChannel(0);
239        
240         if(!m_isoManager->registerStream(s)) {
241                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
242                 goto finish;
243         }
244 #endif
245
246 #ifdef TEST_PORT_1     
247         // add a stream to the manager so that it has something to do
248         s2=new IsoStream(IsoStream::EST_Receive, 1);
249        
250         if (!s2) {
251                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
252                 goto finish;
253         }       
254        
255         s2->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
256        
257         if (!s2->init()) {
258                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
259                 goto finish;
260         }
261        
262         s2->setChannel(0);
263        
264         if(!m_isoManager->registerStream(s2)) {
265                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
266                 goto finish;
267         }
268 #endif
269
270 #ifdef TEST_PORT_2             
271         // add a stream to the manager so that it has something to do
272         s3=new IsoStream(IsoStream::EST_Receive,2);
273        
274         if (!s3) {
275                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
276                 goto finish;
277         }       
278        
279         s3->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
280        
281         if (!s3->init()) {
282                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
283                 goto finish;
284         }
285        
286         s3->setChannel(0);
287        
288         if(!m_isoManager->registerStream(s3)) {
289                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
290                 goto finish;
291         }
292 #endif
293
294         debugOutput(DEBUG_LEVEL_NORMAL,   "Preparing IsoHandlerManager...\n");
295         if (!m_isoManager->prepare()) {
296                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not prepare isoManager\n");
297                 goto finish;
298         }
299
300         debugOutput(DEBUG_LEVEL_NORMAL,   "Starting ISO manager sync update thread...\n");
301         // start the runner thread
302         m_isoManagerThread->Start();
303        
304         debugOutput(DEBUG_LEVEL_NORMAL,   "Starting IsoHandler...\n");
305         if (!m_isoManager->startHandlers(0)) {
306                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not start handlers...\n");
307                 goto finish;
308         }
309        
310         while(run) {
311         sleep(1);
312         m_isoManager->dumpInfo();
313         }
314
315         debugOutput(DEBUG_LEVEL_NORMAL,   "Stopping handlers...\n");
316         if(!m_isoManager->stopHandlers()) {
317            debugOutput(DEBUG_LEVEL_NORMAL, "Could not stop ISO handlers\n");
318            goto finish;
319         }
320        
321         // stop the sync thread
322         debugOutput(DEBUG_LEVEL_NORMAL,   "Stopping ISO manager sync update thread...\n");
323         m_isoManagerThread->Stop();
324        
325 #ifdef TEST_PORT_0     
326         if(!m_isoManager->unregisterStream(s)) {
327                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
328                 goto finish;
329         }
330         delete s;
331 #endif
332
333 #ifdef TEST_PORT_1     
334         if(!m_isoManager->unregisterStream(s1)) {
335                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
336                 goto finish;
337         }
338         delete s1;
339 #endif
340
341 #ifdef TEST_PORT_2     
342         if(!m_isoManager->unregisterStream(s2)) {
343                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
344                 goto finish;
345         }
346         delete s2;
347 #endif
348        
349         delete m_isoManagerThread;
350     delete m_isoManager;
351
352 finish:
353         debugOutput(DEBUG_LEVEL_NORMAL, "Bye...\n");
354
355   return EXIT_SUCCESS;
356 }
Note: See TracBrowser for help on using the browser.