2 * Copyright (C) 2005-2008 by Pieter Palmers
4 * This file is part of FFADO
5 * FFADO = Free Firewire (pro-)audio drivers for linux
7 * FFADO is based upon FreeBoB
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.
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.
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/>.
24 #include "debugmodule/debugmodule.h"
26 #include "libutil/PosixSharedMemory.h"
36 DECLARE_GLOBAL_DEBUG_MODULE
;
42 static void sighandler (int sig
)
47 ////////////////////////////////////////////////
49 ////////////////////////////////////////////////
50 const char *argp_program_version
= "test-shm 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 shared memory class.";
53 static char args_doc
[] = "DIRECTION";
54 static struct argp_option options
[] = {
55 {"verbose", 'v', "level", 0, "Produce verbose output" },
73 // Parse a single option.
75 parse_opt( int key
, char* arg
, struct argp_state
* state
)
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
;
86 arguments
->verbose
= strtol( arg
, &tail
, 0 );
88 fprintf( stderr
, "Could not parse 'verbose' argument\n" );
89 return ARGP_ERR_UNKNOWN
;
94 if (state
->arg_num
>= MAX_ARGS
) {
95 // Too many arguments.
98 arguments
->args
[state
->arg_num
] = arg
;
102 if(arguments
->nargs
<= 0) {
103 printMessage("not enough arguments\n");
108 return ARGP_ERR_UNKNOWN
;
113 static struct argp argp
= { options
, parse_opt
, args_doc
, doc
};
115 ///////////////////////////
117 //////////////////////////
119 main(int argc
, char **argv
)
121 signal (SIGINT
, sighandler
);
122 signal (SIGPIPE
, sighandler
);
125 if ( argp_parse ( &argp
, argc
, argv
, 0, 0, &arguments
) ) {
126 fprintf( stderr
, "Could not parse command line\n" );
130 setDebugLevel(arguments
.verbose
);
134 long int direction
= strtol( arguments
.args
[0], &tail
, 0 );
136 fprintf( stderr
, "Could not parse direction argument\n" );
140 printMessage("Testing shared memory direction %ld\n", direction
);
142 PosixSharedMemory s
= PosixSharedMemory("testseg", 1024);
143 s
.setVerboseLevel(arguments
.verbose
);
146 if(!s
.Create(PosixSharedMemory::eD_ReadWrite
)) {
147 debugError("Could not create segment\n");
151 if(!s
.Open(PosixSharedMemory::eD_ReadOnly
)) {
152 debugError("Could not open segment\n");
157 if(!s
.LockInMemory(true)) {
158 debugError("Could not memlock segment\n");
167 long int time_to_sleep
= 1000*1000;
170 snprintf(buff
, len
, "test %d", cnt
++);
171 printMessage("writing '%s'...\n", buff
);
172 if(s
.Write(offset
, buff
, len
) != PosixSharedMemory::eR_OK
) {
173 debugError("Could not write to segment\n");
176 usleep(time_to_sleep
);
178 printMessage("reading...\n");
179 if(s
.Read(offset
, buff
, len
) != PosixSharedMemory::eR_OK
) {
180 debugError("Could not receive from queue\n");
184 printMessage(" read: '%s'\n", buff
);
185 usleep(time_to_sleep
* 110/100);
189 if(!s
.LockInMemory(false)) {
190 debugError("Could not mem-unlock segment\n");
196 if(!s
.LockInMemory(false)) {
197 debugError("Could not mem-unlock segment\n");