root/branches/streaming-rework/src/freebob-sync.cpp

Revision 336, 5.7 kB (checked in by pieterpalmers, 17 years ago)

- Merged the developments on trunk since branch-off:

branch occurred at rev 194
svn merge -r 194:HEAD https://svn.sourceforge.net/svnroot/freebob/trunk/libfreebob

- Modified libfreebobavc to use the messagebuffer for debug info.
- This should compile and run

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 ( !pDeviceManager->discover( arguments.verbose ) ) {
135         debugError( "Could not discover devices\n" );
136         return -1;
137     }
138
139     IAvDevice* pAvDevice = pDeviceManager->getAvDevice( node_id );
140     if ( !pAvDevice ) {
141         printf( "No recognized device found with id %d\n", node_id );
142         return 0;
143     }
144
145     BeBoB::AvDevice* pBeBoBAvDevice
146         = dynamic_cast<BeBoB::AvDevice*>( pAvDevice );
147     if ( !pBeBoBAvDevice ) {
148         printf( "Not a BeBoB device, cannot set sync\n" );
149         return 0;
150     }
151
152     int i = 0;
153     for ( BeBoB::AvDevice::SyncInfoVector::const_iterator it
154               = pBeBoBAvDevice->getSyncInfos().begin();
155           it != pBeBoBAvDevice->getSyncInfos().end();
156           ++it )
157     {
158         const BeBoB::AvDevice::SyncInfo* pSyncInfo = &*it;
159         printf( "  %2d) %s\n", i, pSyncInfo->m_description.c_str() );
160         ++i;
161     }
162
163     printf( "Active Sync mode:\n" );
164     if ( !pBeBoBAvDevice->getActiveSyncInfo() ) {
165         debugError( "Could not retrieve active sync information\n" );
166         return 0;
167     }
168     printf( "  %s\n", pBeBoBAvDevice->getActiveSyncInfo()->m_description.c_str() );
169
170     if ( argc >= 4 ) {
171         if ( strcmp( "set", arguments.args[1] ) == 0 ) {
172             errno = 0;
173             unsigned int syncId = strtol(arguments.args[2], &tail, 0);
174             if (errno) {
175                 perror("argument parsing failed:");
176                 return -1;
177             }
178
179             if (syncId >= pBeBoBAvDevice->getSyncInfos().size() ) {
180                 printf( "Invalid sync mode id given (%d). "
181                         "Valid range is 0 - %d\n",
182                         syncId, pBeBoBAvDevice->getSyncInfos().size()-1 );
183                 return -1;
184             }
185
186             const BeBoB::AvDevice::SyncInfo& syncInfo
187                 = pBeBoBAvDevice->getSyncInfos()[syncId];
188             printf( "Setting Sync Mode to \"%s\"... ",
189                     syncInfo.m_description.c_str() );
190
191             if ( pBeBoBAvDevice->setActiveSync( syncInfo ) ) {
192                 printf( "done\n" );
193             } else {
194                 printf( "failed\n" );
195             }
196         }
197     }
198
199     return 0;
200 }
201
202
Note: See TracBrowser for help on using the browser.