1 |
/* |
---|
2 |
* Copyright (C) 2005-2008 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 2 of the License, or |
---|
12 |
* (at your option) version 3 of the License. |
---|
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 |
#include "debugmodule/debugmodule.h" |
---|
25 |
|
---|
26 |
#include "libutil/IpcRingBuffer.h" |
---|
27 |
#include "libutil/SystemTimeSource.h" |
---|
28 |
|
---|
29 |
#include <argp.h> |
---|
30 |
#include <stdlib.h> |
---|
31 |
#include <iostream> |
---|
32 |
#include <signal.h> |
---|
33 |
|
---|
34 |
using namespace Util; |
---|
35 |
|
---|
36 |
DECLARE_GLOBAL_DEBUG_MODULE; |
---|
37 |
|
---|
38 |
#define MAX_ARGS 2 |
---|
39 |
|
---|
40 |
int run=1; |
---|
41 |
int lastsig=-1; |
---|
42 |
static void sighandler (int sig) |
---|
43 |
{ |
---|
44 |
run = 0; |
---|
45 |
} |
---|
46 |
|
---|
47 |
//////////////////////////////////////////////// |
---|
48 |
// arg parsing |
---|
49 |
//////////////////////////////////////////////// |
---|
50 |
const char *argp_program_version = "test-ipcringbuffer 0.1"; |
---|
51 |
const char *argp_program_bug_address = "<ffado-devel@lists.sf.net>"; |
---|
52 |
static char doc[] = "test-avccmd -- test program to test the ipc ringbuffer class."; |
---|
53 |
static char args_doc[] = "DIRECTION"; |
---|
54 |
static struct argp_option options[] = { |
---|
55 |
{"verbose", 'v', "level", 0, "Produce verbose output" }, |
---|
56 |
{ 0 } |
---|
57 |
}; |
---|
58 |
|
---|
59 |
struct arguments |
---|
60 |
{ |
---|
61 |
arguments() |
---|
62 |
: nargs ( 0 ) |
---|
63 |
, verbose( false ) |
---|
64 |
{ |
---|
65 |
args[0] = 0; |
---|
66 |
} |
---|
67 |
|
---|
68 |
char* args[MAX_ARGS]; |
---|
69 |
int nargs; |
---|
70 |
long int verbose; |
---|
71 |
} arguments; |
---|
72 |
|
---|
73 |
// Parse a single option. |
---|
74 |
static error_t |
---|
75 |
parse_opt( int key, char* arg, struct argp_state* state ) |
---|
76 |
{ |
---|
77 |
// Get the input argument from `argp_parse', which we |
---|
78 |
// know is a pointer to our arguments structure. |
---|
79 |
struct arguments* arguments = ( struct arguments* ) state->input; |
---|
80 |
|
---|
81 |
char* tail; |
---|
82 |
errno = 0; |
---|
83 |
switch (key) { |
---|
84 |
case 'v': |
---|
85 |
if (arg) { |
---|
86 |
arguments->verbose = strtol( arg, &tail, 0 ); |
---|
87 |
if ( errno ) { |
---|
88 |
fprintf( stderr, "Could not parse 'verbose' argument\n" ); |
---|
89 |
return ARGP_ERR_UNKNOWN; |
---|
90 |
} |
---|
91 |
} |
---|
92 |
break; |
---|
93 |
case ARGP_KEY_ARG: |
---|
94 |
if (state->arg_num >= MAX_ARGS) { |
---|
95 |
// Too many arguments. |
---|
96 |
argp_usage (state); |
---|
97 |
} |
---|
98 |
arguments->args[state->arg_num] = arg; |
---|
99 |
arguments->nargs++; |
---|
100 |
break; |
---|
101 |
case ARGP_KEY_END: |
---|
102 |
if(arguments->nargs <= 0) { |
---|
103 |
printMessage("not enough arguments\n"); |
---|
104 |
return -1; |
---|
105 |
} |
---|
106 |
break; |
---|
107 |
default: |
---|
108 |
return ARGP_ERR_UNKNOWN; |
---|
109 |
} |
---|
110 |
return 0; |
---|
111 |
} |
---|
112 |
|
---|
113 |
static struct argp argp = { options, parse_opt, args_doc, doc }; |
---|
114 |
|
---|
115 |
/////////////////////////// |
---|
116 |
// main |
---|
117 |
////////////////////////// |
---|
118 |
int |
---|
119 |
main(int argc, char **argv) |
---|
120 |
{ |
---|
121 |
signal (SIGINT, sighandler); |
---|
122 |
signal (SIGPIPE, sighandler); |
---|
123 |
|
---|
124 |
// arg parsing |
---|
125 |
if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) { |
---|
126 |
fprintf( stderr, "Could not parse command line\n" ); |
---|
127 |
exit(-1); |
---|
128 |
} |
---|
129 |
|
---|
130 |
setDebugLevel(arguments.verbose); |
---|
131 |
|
---|
132 |
errno = 0; |
---|
133 |
char* tail; |
---|
134 |
long int direction = strtol( arguments.args[0], &tail, 0 ); |
---|
135 |
if ( errno ) { |
---|
136 |
fprintf( stderr, "Could not parse direction argument\n" ); |
---|
137 |
exit(-1); |
---|
138 |
} |
---|
139 |
|
---|
140 |
printMessage("Testing shared memory direction %ld\n", direction); |
---|
141 |
|
---|
142 |
#define TEST_SAMPLERATE 44100 |
---|
143 |
#define BUFF_SIZE 64 |
---|
144 |
#define NB_BUFFERS 4 |
---|
145 |
IpcRingBuffer* b = NULL; |
---|
146 |
if(direction == 0) { |
---|
147 |
b = new IpcRingBuffer("testbuff", |
---|
148 |
IpcRingBuffer::eBT_Master, |
---|
149 |
IpcRingBuffer::eD_Outward, |
---|
150 |
IpcRingBuffer::eB_Blocking, |
---|
151 |
NB_BUFFERS, BUFF_SIZE); |
---|
152 |
if(b == NULL) { |
---|
153 |
debugError("Could not create master\n"); |
---|
154 |
exit(-1); |
---|
155 |
} |
---|
156 |
} else { |
---|
157 |
b = new IpcRingBuffer("testbuff", |
---|
158 |
IpcRingBuffer::eBT_Master, |
---|
159 |
IpcRingBuffer::eD_Inward, |
---|
160 |
IpcRingBuffer::eB_Blocking, |
---|
161 |
NB_BUFFERS, BUFF_SIZE); |
---|
162 |
if(b == NULL) { |
---|
163 |
debugError("Could not create master\n"); |
---|
164 |
exit(-1); |
---|
165 |
} |
---|
166 |
} |
---|
167 |
|
---|
168 |
b->setVerboseLevel(arguments.verbose); |
---|
169 |
|
---|
170 |
char buff[BUFF_SIZE]; |
---|
171 |
int cnt = 0; |
---|
172 |
long int time_to_sleep = 1000*1000*BUFF_SIZE/TEST_SAMPLERATE; |
---|
173 |
|
---|
174 |
if(!b->init()) { |
---|
175 |
debugError("Could not init buffer\n"); |
---|
176 |
goto out_err; |
---|
177 |
} |
---|
178 |
|
---|
179 |
run=1; |
---|
180 |
while(run) { |
---|
181 |
if(direction == 0) { |
---|
182 |
snprintf(buff, BUFF_SIZE, "test %d", cnt); |
---|
183 |
if(cnt%1000==0) { |
---|
184 |
printMessage("writing '%s'...\n", buff); |
---|
185 |
} |
---|
186 |
IpcRingBuffer::eResult res = b->Write(buff); |
---|
187 |
if(res != IpcRingBuffer::eR_OK && res != IpcRingBuffer::eR_Again) { |
---|
188 |
debugError("Could not write to segment\n"); |
---|
189 |
goto out_err; |
---|
190 |
} |
---|
191 |
if(res == IpcRingBuffer::eR_Again) { |
---|
192 |
printMessage(" Try again on %d...\n", cnt); |
---|
193 |
} else { |
---|
194 |
cnt++; |
---|
195 |
} |
---|
196 |
usleep(time_to_sleep); |
---|
197 |
} else { |
---|
198 |
if(cnt%1000==0) { |
---|
199 |
printMessage("reading...\n"); |
---|
200 |
} |
---|
201 |
|
---|
202 |
IpcRingBuffer::eResult res = b->Read(buff); |
---|
203 |
if(res != IpcRingBuffer::eR_OK && res != IpcRingBuffer::eR_Again) { |
---|
204 |
debugError("Could not receive from queue\n"); |
---|
205 |
goto out_err; |
---|
206 |
} |
---|
207 |
if(cnt%1000==0) { |
---|
208 |
buff[BUFF_SIZE-1]=0; |
---|
209 |
printMessage(" read: '%s'\n", buff); |
---|
210 |
} |
---|
211 |
if(res == IpcRingBuffer::eR_Again) { |
---|
212 |
printMessage(" Try again on %d...\n", cnt); |
---|
213 |
} else { |
---|
214 |
cnt++; |
---|
215 |
} |
---|
216 |
} |
---|
217 |
} |
---|
218 |
|
---|
219 |
delete b; |
---|
220 |
return 0; |
---|
221 |
|
---|
222 |
out_err: |
---|
223 |
delete b; |
---|
224 |
return -1; |
---|
225 |
} |
---|
226 |
|
---|