root/trunk/libffado/tests/test-ieee1394service.cpp

Revision 783, 5.4 kB (checked in by ppalmers, 15 years ago)

cleanup time/wait/sleep code

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <endian.h>
32
33 #include <signal.h>
34 #include "src/debugmodule/debugmodule.h"
35
36 #include <netinet/in.h>
37
38 #include "src/threads.h"
39
40 #include "src/libieee1394/cycletimer.h"
41 #include "src/libieee1394/configrom.h"
42 #include "src/libieee1394/ieee1394service.h"
43 #include "src/libieee1394/ARMHandler.h"
44
45 #include <libraw1394/raw1394.h>
46 #include "libutil/Time.h"
47
48 DECLARE_GLOBAL_DEBUG_MODULE;
49
50 int run=1;
51 static void sighandler (int sig)
52 {
53     run = 0;
54 }
55
56 class MyFunctor : public Functor
57 {
58 public:
59     MyFunctor() {}
60     virtual ~MyFunctor() {}
61
62     void operator() () {
63         printf("hello from the functor (%p)\n", this);
64     };
65 };
66
67 int main(int argc, char *argv[])
68 {
69     setDebugLevel(DEBUG_LEVEL_VERBOSE);
70     signal (SIGINT, sighandler);
71     signal (SIGPIPE, sighandler);
72
73     const int PORT_TO_USE = 0;
74
75     printf("FFADO Ieee1394Service test application\n");
76
77     Ieee1394Service *m_service=NULL;
78
79     m_service=new Ieee1394Service();
80     m_service->setVerboseLevel(DEBUG_LEVEL_VERBOSE );
81     m_service->initialize(PORT_TO_USE);
82     m_service->setThreadParameters(true, 20);
83
84     MyFunctor *test_busreset=new MyFunctor();
85
86     printf(" adding (%p) as busreset handler\n", test_busreset);
87
88     m_service->addBusResetHandler(test_busreset);
89
90     nodeaddr_t addr =  m_service->findFreeARMBlock(0x0000FFFFE0000000ULL, 4, 4 );
91
92     ARMHandler *test_arm=new ARMHandler(addr,
93                          4,
94                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
95                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
96                          0);
97
98     printf(" adding (%p) as arm handler\n", test_arm);
99
100     if (!m_service->registerARMHandler(test_arm)) {
101         printf("  failed\n");
102     }
103
104     addr =  m_service->findFreeARMBlock(0x0000FFFFE0000000ULL, 4, 4 );
105
106     ARMHandler *test_arm2=new ARMHandler(addr,
107                          4,
108                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
109                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
110                          0);
111
112     printf(" adding (%p) as arm handler\n", test_arm2);
113
114     if (!m_service->registerARMHandler(test_arm2)) {
115         printf("  failed\n");
116     }
117
118     raw1394handle_t m_handle = raw1394_new_handle_on_port( PORT_TO_USE );
119     if ( !m_handle ) {
120         if ( !errno ) {
121             debugFatal("libraw1394 not compatible\n");
122         } else {
123             debugFatal("Ieee1394Service::initialize: Could not get 1394 handle: %s\n",
124                 strerror(errno) );
125             debugFatal("Is ieee1394 and raw1394 driver loaded?\n");
126         }
127         exit(-1);
128     }
129
130     while(run) {
131         fflush(stderr);
132         fflush(stdout);
133         SleepRelativeUsec(900*1000);
134
135         uint32_t cycle_timer;
136         uint64_t local_time;
137         // read the CTR 'raw' from a handle
138         // and read it from the 1394 service, which uses a DLL
139         int err = raw1394_read_cycle_timer(m_handle, &cycle_timer, &local_time);
140         uint64_t ctr = CYCLE_TIMER_TO_TICKS( cycle_timer );
141         uint64_t ctr_dll = m_service->getCycleTimerTicks();
142
143         if(err) {
144             debugError("CTR read error\n");
145         }
146         debugOutput ( DEBUG_LEVEL_VERBOSE,
147                     "Cycle timer: %011llu (%03us %04ucy %04uticks)\n",
148                     ctr, (unsigned int)TICKS_TO_SECS( ctr ),
149                     (unsigned int)TICKS_TO_CYCLES( ctr ), (unsigned int)TICKS_TO_OFFSET( ctr ) );
150         debugOutput ( DEBUG_LEVEL_VERBOSE,
151                     "   from DLL: %011llu (%03us %04ucy %04uticks)\n",
152                     ctr_dll, (unsigned int)TICKS_TO_SECS( ctr_dll ),
153                     (unsigned int)TICKS_TO_CYCLES( ctr_dll ), (unsigned int)TICKS_TO_OFFSET( ctr_dll ) );
154         int64_t diff = diffTicks(ctr, ctr_dll);
155         uint64_t abs_diff;
156         if (diff < 0) {
157             abs_diff = -diff;
158         } else {
159             abs_diff = diff;
160         }
161         debugOutput ( DEBUG_LEVEL_VERBOSE,
162                     "      diff: %s%011llu (%03us %04ucy %04uticks)\n",
163                     ((int64_t)abs_diff==diff?" ":"-"), abs_diff, (unsigned int)TICKS_TO_SECS( abs_diff ),
164                     (unsigned int)TICKS_TO_CYCLES( abs_diff ), (unsigned int)TICKS_TO_OFFSET( abs_diff ) );
165         if (abs_diff > 1000) {
166             debugWarning("Alert, large diff: %lld\n", diff);
167         }
168         debugOutput ( DEBUG_LEVEL_VERBOSE,
169                     " wait...\n");
170     }
171
172     delete m_service;
173     delete test_busreset;
174     delete test_arm;
175     delete test_arm2;
176
177     printf("Bye...\n");
178
179     return EXIT_SUCCESS;
180 }
Note: See TracBrowser for help on using the browser.