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

Revision 1172, 5.1 kB (checked in by ppalmers, 15 years ago)

lay down the foundations for easy ALSA/Pulse support

Line 
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/PosixSharedMemory.h"
27
28 #include <argp.h>
29 #include <stdlib.h>
30 #include <iostream>
31 #include <signal.h>
32
33 using namespace Util;
34
35 DECLARE_GLOBAL_DEBUG_MODULE;
36
37 #define MAX_ARGS 2
38
39 int run=1;
40 int lastsig=-1;
41 static void sighandler (int sig)
42 {
43     run = 0;
44 }
45
46 ////////////////////////////////////////////////
47 // arg parsing
48 ////////////////////////////////////////////////
49 const char *argp_program_version = "test-shm 0.1";
50 const char *argp_program_bug_address = "<ffado-devel@lists.sf.net>";
51 static char doc[] = "test-avccmd -- test program to test the shared memory class.";
52 static char args_doc[] = "DIRECTION";
53 static struct argp_option options[] = {
54     {"verbose",  'v', "level",    0,  "Produce verbose output" },
55    { 0 }
56 };
57
58 struct arguments
59 {
60     arguments()
61         : nargs ( 0 )
62         , verbose( false )
63         {
64             args[0] = 0;
65         }
66
67     char* args[MAX_ARGS];
68     int   nargs;
69     long int verbose;
70 } arguments;
71
72 // Parse a single option.
73 static error_t
74 parse_opt( int key, char* arg, struct argp_state* state )
75 {
76     // Get the input argument from `argp_parse', which we
77     // know is a pointer to our arguments structure.
78     struct arguments* arguments = ( struct arguments* ) state->input;
79
80     char* tail;
81     errno = 0;
82     switch (key) {
83     case 'v':
84         if (arg) {
85             arguments->verbose = strtol( arg, &tail, 0 );
86             if ( errno ) {
87                 fprintf( stderr,  "Could not parse 'verbose' argument\n" );
88                 return ARGP_ERR_UNKNOWN;
89             }
90         }
91         break;
92     case ARGP_KEY_ARG:
93         if (state->arg_num >= MAX_ARGS) {
94             // Too many arguments.
95             argp_usage (state);
96         }
97         arguments->args[state->arg_num] = arg;
98         arguments->nargs++;
99         break;
100     case ARGP_KEY_END:
101         if(arguments->nargs <= 0) {
102             printMessage("not enough arguments\n");
103             return -1;
104         }
105         break;
106     default:
107         return ARGP_ERR_UNKNOWN;
108     }
109     return 0;
110 }
111
112 static struct argp argp = { options, parse_opt, args_doc, doc };
113
114 ///////////////////////////
115 // main
116 //////////////////////////
117 int
118 main(int argc, char **argv)
119 {
120     signal (SIGINT, sighandler);
121     signal (SIGPIPE, sighandler);
122
123     // arg parsing
124     if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
125         fprintf( stderr, "Could not parse command line\n" );
126         exit(-1);
127     }
128
129     setDebugLevel(arguments.verbose);
130
131     errno = 0;
132     char* tail;
133     long int direction = strtol( arguments.args[0], &tail, 0 );
134     if ( errno ) {
135         fprintf( stderr,  "Could not parse direction argument\n" );
136         exit(-1);
137     }
138
139     printMessage("Testing shared memory direction %ld\n", direction);
140
141     PosixSharedMemory s = PosixSharedMemory("testseg", 1024);
142     s.setVerboseLevel(arguments.verbose);
143
144     if(direction == 0) {
145         if(!s.Create(PosixSharedMemory::eD_ReadWrite)) {
146             debugError("Could not create segment\n");
147             exit(-1);
148         }
149     } else {
150         if(!s.Open(PosixSharedMemory::eD_ReadOnly)) {
151             debugError("Could not open segment\n");
152              exit(-1);
153        }
154     }
155
156     if(!s.LockInMemory(true)) {
157         debugError("Could not memlock segment\n");
158     }
159
160     int offset=0;
161     int len = 64;
162     char buff[len];
163
164     int cnt = 0;
165     run=1;
166     long int time_to_sleep = 1000*1000;
167     while(run) {
168         if(direction == 0) {
169             snprintf(buff, len, "test %d", cnt++);
170             printMessage("writing '%s'...\n", buff);
171             if(s.Write(offset, buff, len) != PosixSharedMemory::eR_OK) {
172                 debugError("Could not write to segment\n");
173                 goto out_err;
174             }
175             usleep(time_to_sleep);
176         } else {
177             printMessage("reading...\n");
178             if(s.Read(offset, buff, len) != PosixSharedMemory::eR_OK) {
179                 debugError("Could not receive from queue\n");
180                 goto out_err;
181             }
182             buff[len-1]=0;
183             printMessage(" read: '%s'\n", buff);
184             usleep(time_to_sleep * 110/100);
185         }
186     }
187
188     if(!s.LockInMemory(false)) {
189         debugError("Could not mem-unlock segment\n");
190     }
191
192     return 0;
193
194 out_err:
195     if(!s.LockInMemory(false)) {
196         debugError("Could not mem-unlock segment\n");
197     }
198     return -1;
199 }
200
Note: See TracBrowser for help on using the browser.