root/trunk/libffado/src/bebob-sync.cpp

Revision 445, 5.8 kB (checked in by pieterpalmers, 16 years ago)

* name change from FreeBoB to FFADO
* replaced tabs by 4 spaces
* got rid of end-of-line spaces
* made all license and copyrights conform

library becomes LGPL, apps become GPL
explicitly state LGPL v2.1 and GPL v2 (don't like v3 draft)

copyrights are 2005-2007 Daniel & Pieter
except for the MotU stuff (C) Jonathan, Pieter

Line 
1 /*
2  * Copyright (C) 2005-2007 by Daniel Wagner
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 is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2
11  * as published by the Free Software Foundation.
12  *
13  * FFADO is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with FFADO; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA.
22  */
23
24 #include "devicemanager.h"
25 #include "iavdevice.h"
26 #include "bebob/bebob_avdevice.h"
27
28 #include <argp.h>
29 #include <iostream>
30
31
32 ////////////////////////////////////////////////
33 // arg parsing
34 ////////////////////////////////////////////////
35 const char *argp_program_version = "bebob-sync 0.1";
36 const char *argp_program_bug_address = "<ffado-devel@lists.sf.net>";
37 static char doc[] = "bebob-sync -- select sync mode on a BeBoB device\n\n"
38                     "OPERATION:  set <nr>\n";
39 static char args_doc[] = "NODE_ID OPERATION";
40 static struct argp_option options[] = {
41     {"verbose",   'v', "level",     0,  "Produce verbose output" },
42     {"port",      'p', "PORT",      0,  "Set port" },
43     { 0 }
44 };
45
46 // IMPL_GLOBAL_DEBUG_MODULE( bebob-sync, DebugModule::eDL_Normal );
47 DECLARE_GLOBAL_DEBUG_MODULE;
48
49 struct arguments
50 {
51     arguments()
52         : verbose( 0 )
53         , port( 0 )
54         {
55             args[0] = 0;
56             args[1] = 0;
57             args[2] = 0;
58         }
59
60     char* args[3];
61     short verbose;
62     int   port;
63 } arguments;
64
65 // Parse a single option.
66 static error_t
67 parse_opt( int key, char* arg, struct argp_state* state )
68 {
69     // Get the input argument from `argp_parse', which we
70     // know is a pointer to our arguments structure.
71     struct arguments* arguments = ( struct arguments* ) state->input;
72
73     char* tail;
74     switch (key) {
75     case 'v':
76         if (arg) {
77             arguments->verbose = strtol( arg, &tail, 0 );
78             if ( errno ) {
79                 fprintf( stderr,  "Could not parse 'verbose' argument\n" );
80                 return ARGP_ERR_UNKNOWN;
81             }
82         }
83         break;
84     case 'p':
85         errno = 0;
86         arguments->port = strtol(arg, &tail, 0);
87         if (errno) {
88             perror("argument parsing failed:");
89             return errno;
90         }
91         break;
92     case ARGP_KEY_ARG:
93         if (state->arg_num >= 3) {
94             argp_usage (state);
95         }
96         arguments->args[state->arg_num] = arg;
97         break;
98     case ARGP_KEY_END:
99         if (state->arg_num < 1) {
100             argp_usage (state);
101         }
102         break;
103     default:
104         return ARGP_ERR_UNKNOWN;
105     }
106     return 0;
107 }
108
109 static struct argp argp = { options, parse_opt, args_doc, doc };
110
111
112 int
113 main( int argc, char** argv )
114 {
115     using namespace std;
116
117     // arg parsing
118     argp_parse (&argp, argc, argv, 0, 0, &arguments);
119
120     errno = 0;
121     char* tail;
122     int node_id = strtol(arguments.args[0], &tail, 0);
123     if (errno) {
124     perror("argument parsng failed:");
125     return -1;
126     }
127
128     std::auto_ptr<DeviceManager> pDeviceManager
129         = std::auto_ptr<DeviceManager>( new DeviceManager() );
130     if ( !pDeviceManager.get() ) {
131         debugFatal( "Could not allocate DeviceManager\n" );
132         return -1;
133     }
134     if ( !pDeviceManager->initialize( arguments.port ) ) {
135         debugFatal( "Could not initialize device manager\n" );
136         return -1;
137     }
138     if ( arguments.verbose ) {
139         pDeviceManager->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
140     }
141     if ( !pDeviceManager->discover( ) ) {
142         debugError( "Could not discover devices\n" );
143         return -1;
144     }
145
146     IAvDevice* pAvDevice = pDeviceManager->getAvDevice( node_id );
147     if ( !pAvDevice ) {
148         printf( "No recognized device found with id %d\n", node_id );
149         return 0;
150     }
151
152     BeBoB::AvDevice* pBeBoBAvDevice
153         = dynamic_cast<BeBoB::AvDevice*>( pAvDevice );
154     if ( !pBeBoBAvDevice ) {
155         printf( "Not a BeBoB device, cannot set sync\n" );
156         return 0;
157     }
158
159     int i = 0;
160     for ( BeBoB::AvDevice::SyncInfoVector::const_iterator it
161               = pBeBoBAvDevice->getSyncInfos().begin();
162           it != pBeBoBAvDevice->getSyncInfos().end();
163           ++it )
164     {
165         const BeBoB::AvDevice::SyncInfo* pSyncInfo = &*it;
166         printf( "  %2d) %s\n", i, pSyncInfo->m_description.c_str() );
167         ++i;
168     }
169
170     printf( "Active Sync mode:\n" );
171     if ( !pBeBoBAvDevice->getActiveSyncInfo() ) {
172         debugError( "Could not retrieve active sync information\n" );
173         return 0;
174     }
175     printf( "  %s\n", pBeBoBAvDevice->getActiveSyncInfo()->m_description.c_str() );
176
177     if ( argc >= 4 ) {
178         if ( strcmp( "set", arguments.args[1] ) == 0 ) {
179             errno = 0;
180             unsigned int syncId = strtol(arguments.args[2], &tail, 0);
181             if (errno) {
182                 perror("argument parsing failed:");
183                 return -1;
184             }
185
186             if (syncId >= pBeBoBAvDevice->getSyncInfos().size() ) {
187                 printf( "Invalid sync mode id given (%d). "
188                         "Valid range is 0 - %d\n",
189                         syncId, pBeBoBAvDevice->getSyncInfos().size()-1 );
190                 return -1;
191             }
192
193             const BeBoB::AvDevice::SyncInfo& syncInfo
194                 = pBeBoBAvDevice->getSyncInfos()[syncId];
195             printf( "Setting Sync Mode to \"%s\"... ",
196                     syncInfo.m_description.c_str() );
197
198             if ( pBeBoBAvDevice->setActiveSync( syncInfo ) ) {
199                 printf( "done\n" );
200             } else {
201                 printf( "failed\n" );
202             }
203         }
204     }
205
206     return 0;
207 }
208
209
Note: See TracBrowser for help on using the browser.