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

Revision 336, 6.4 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-downloader.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 "bebob/bebob_dl_mgr.h"
22 #include "bebob/bebob_dl_bcd.h"
23
24 #include "configrom.h"
25
26 #include "libfreebobavc/ieee1394service.h"
27
28 #include <argp.h>
29 #include <iostream>
30
31
32 ////////////////////////////////////////////////
33 // arg parsing
34 ////////////////////////////////////////////////
35 const char *argp_program_version = "freebob_downloader 0.1";
36 const char *argp_program_bug_address = "<freebob-devel@lists.sf.net>";
37 static char doc[] = "freebob_downloader -- firmware downloader application\n\n"
38                     "OPERATION: display\n"
39                     "           setguid GUID\n"
40                     "           firmware FILE\n"
41                     "           cne FILE\n"
42                     "           bcd FILE\n";
43 static char args_doc[] = "NODE_ID OPERATION";
44 static struct argp_option options[] = {
45     {"verbose",   'v', "level",     0,  "Produce verbose output" },
46     {"port",      'p', "PORT",      0,  "Set port" },
47     {"force",     'f', 0,           0,  "Force firmware download" },
48     {"noboot",    'b', 0,           0,  "Do no start bootloader (bootloader is already running)" },
49     { 0 }
50 };
51
52 struct arguments
53 {
54     arguments()
55         : verbose( 0 )
56         , port( 0 )
57         , force( 0 )
58         {
59             args[0] = 0;
60             args[1] = 0;
61             args[2] = 0;
62         }
63
64     char* args[3];
65     short verbose;
66     int   port;
67     int   force;
68     int   no_bootloader_restart;
69 } arguments;
70
71 // Parse a single option.
72 static error_t
73 parse_opt( int key, char* arg, struct argp_state* state )
74 {
75     // Get the input argument from `argp_parse', which we
76     // know is a pointer to our arguments structure.
77     struct arguments* arguments = ( struct arguments* ) state->input;
78
79     char* tail;
80     switch (key) {
81     case 'v':
82         if (arg) {
83             arguments->verbose = strtol( arg, &tail, 0 );
84             if ( errno ) {
85                 fprintf( stderr,  "Could not parse 'verbose' argument\n" );
86                 return ARGP_ERR_UNKNOWN;
87             }
88         }
89         break;
90     case 'p':
91         errno = 0;
92         arguments->port = strtol(arg, &tail, 0);
93         if (errno) {
94             perror("argument parsing failed:");
95             return errno;
96         }
97         break;
98     case 'f':
99         arguments->force = 1;
100         break;
101     case 'b':
102         arguments->no_bootloader_restart = 1;
103         break;
104     case ARGP_KEY_ARG:
105         if (state->arg_num >= 3) {
106             // Too many arguments.
107             argp_usage (state);
108         }
109         arguments->args[state->arg_num] = arg;
110         break;
111     case ARGP_KEY_END:
112         if (state->arg_num < 2) {
113             // Not enough arguments.
114             argp_usage (state);
115         }
116         break;
117     default:
118         return ARGP_ERR_UNKNOWN;
119     }
120     return 0;
121 }
122
123 static struct argp argp = { options, parse_opt, args_doc, doc };
124
125
126 int
127 main( int argc, char** argv )
128 {
129     using namespace std;
130
131     // arg parsing
132     argp_parse (&argp, argc, argv, 0, 0, &arguments);
133
134     errno = 0;
135     char* tail;
136     int node_id = strtol(arguments.args[0], &tail, 0);
137     if (errno) {
138         perror("argument parsing failed:");
139         return -1;
140     }
141
142     Ieee1394Service service;
143     if ( !service.initialize( arguments.port ) ) {
144         cerr << "Could not initialize IEEE 1394 service" << endl;
145         return -1;
146     }
147
148     service.setVerbose( arguments.verbose > 0 );
149     BeBoB::BootloaderManager blMgr( service, node_id );
150     if ( arguments.force == 1 ) {
151         blMgr.setForceOperations( true );
152     }
153     blMgr.getConfigRom()->printConfigRom();
154     blMgr.printInfoRegisters();
155
156     if ( strcmp( arguments.args[1], "setguid" ) == 0 ) {
157         if (!arguments.args[2] ) {
158             cerr << "guid argument is missing" << endl;
159             return -1;
160         }
161
162         char* tail;
163         fb_octlet_t guid = strtoll(arguments.args[2], &tail, 0 );
164
165         if ( !blMgr.programGUID( guid ) ) {
166             cerr << "Failed to set GUID" << endl;
167             return -1;
168         } else {
169             cout << "new GUID programmed" << endl;
170         }
171     } else if ( strcmp( arguments.args[1], "firmware" ) == 0 ) {
172         if (!arguments.args[2] ) {
173             cerr << "FILE argument is missing" << endl;
174             return -1;
175         }
176         std::string str( arguments.args[2] );
177
178         blMgr.setStartBootloader( arguments.no_bootloader_restart != 1 );
179         if ( !blMgr.downloadFirmware( str ) ) {
180             cerr << "Failed to download firmware" << endl;
181             return -1;
182         } else {
183             cout << "Firmware download was successful" << endl;
184         }
185     } else if ( strcmp( arguments.args[1], "cne" ) == 0 ) {
186         if (!arguments.args[2] ) {
187             cerr << "FILE argument is missing" << endl;
188             return -1;
189         }
190         std::string str( arguments.args[2] );
191
192         if ( !blMgr.downloadCnE( str ) ) {
193             cerr << "Failed to download CnE" << endl;
194             return -1;
195         } else {
196             cout << "CnE download was successful" << endl;
197         }
198     } else if ( strcmp( arguments.args[1], "display" ) == 0 ) {
199         // nothing to do
200     } else if ( strcmp( arguments.args[1], "bcd" ) == 0 ) {
201         if ( !arguments.args[2] ) {
202             cerr << "FILE arguments is missing" << endl;
203             return -1;
204         }
205         BeBoB::BCD* bcd = new BeBoB::BCD::BCD( arguments.args[2] );
206         if ( !bcd ) {
207             cerr << "Could no open file " << arguments.args[2] << endl;
208             return -1;
209         }
210         if ( !bcd->parse() ) {
211             cerr << "Could not parse file " << arguments.args[2] << endl;
212             return -1;
213         }
214
215         bcd->displayInfo();
216         delete bcd;
217     } else {
218         cout << "Unknown operation" << endl;
219     }
220
221     return 0;
222 }
223
224
Note: See TracBrowser for help on using the browser.