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

Revision 442, 5.8 kB (checked in by pieterpalmers, 15 years ago)

rename files & directories

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