root/trunk/libffado/support/firmware/fireworks-downloader.cpp

Revision 750, 5.5 kB (checked in by ppalmers, 16 years ago)

Code refactoring. Tries to simplify things and tries to put all code where it belongs.

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  * Copyright (C) 2005-2007 by Daniel Wagner
4  *
5  * This file is part of FFADO
6  * FFADO = Free Firewire (pro-)audio drivers for linux
7  *
8  * FFADO is based upon FreeBoB
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "src/fireworks/fireworks_device.h"
26 #include "src/fireworks/fireworks_firmware.h"
27
28 #include "libieee1394/configrom.h"
29 #include "libieee1394/ieee1394service.h"
30
31 #include "debugmodule/debugmodule.h"
32
33 #include "devicemanager.h"
34
35 #include <argp.h>
36 #include <iostream>
37
38 #include <string.h>
39 #include <string>
40
41 DECLARE_GLOBAL_DEBUG_MODULE;
42
43 using namespace FireWorks;
44 ////////////////////////////////////////////////
45 // arg parsing
46 ////////////////////////////////////////////////
47 const char *argp_program_version = "fireworks-downloader 0.1";
48 const char *argp_program_bug_address = "<ffado-devel@lists.sf.net>";
49 static char doc[] = "fireworks-downloader -- firmware downloader application for ECHO Fireworks devices\n\n"
50                     "OPERATION: display\n"
51                     "           firmware FILE\n";
52 static char args_doc[] = "NODE_ID OPERATION";
53 static struct argp_option options[] = {
54     {"verbose",   'v', "level",     0,  "Produce verbose output" },
55     {"port",      'p', "PORT",      0,  "Set port" },
56     { 0 }
57 };
58
59 struct arguments
60 {
61     arguments()
62         : verbose( 0 )
63         , port( 0 )
64         {
65             args[0] = 0;
66             args[1] = 0;
67             args[2] = 0;
68         }
69
70     char* args[3];
71     short verbose;
72     int   port;
73 } arguments;
74
75 // Parse a single option.
76 static error_t
77 parse_opt( int key, char* arg, struct argp_state* state )
78 {
79     // Get the input argument from `argp_parse', which we
80     // know is a pointer to our arguments structure.
81     struct arguments* arguments = ( struct arguments* ) state->input;
82
83     char* tail;
84     switch (key) {
85     case 'v':
86         if (arg) {
87             arguments->verbose = strtol( arg, &tail, 0 );
88             if ( errno ) {
89                 debugError( "Could not parse 'verbose' argument\n" );
90                 return ARGP_ERR_UNKNOWN;
91             }
92         }
93         break;
94     case 'p':
95         errno = 0;
96         arguments->port = strtol(arg, &tail, 0);
97         if (errno) {
98             debugError("argument parsing failed: %s\n",
99                        strerror(errno));
100             return errno;
101         }
102         break;
103     case ARGP_KEY_ARG:
104         if (state->arg_num >= 3) {
105             // Too many arguments.
106             argp_usage (state);
107         }
108         arguments->args[state->arg_num] = arg;
109         break;
110     case ARGP_KEY_END:
111         if (state->arg_num < 2) {
112             // Not enough arguments.
113             argp_usage (state);
114         }
115         break;
116     default:
117         return ARGP_ERR_UNKNOWN;
118     }
119     return 0;
120 }
121
122 static struct argp argp = { options, parse_opt, args_doc, doc };
123
124
125 int
126 main( int argc, char** argv )
127 {
128     using namespace std;
129
130     // arg parsing
131     argp_parse (&argp, argc, argv, 0, 0, &arguments);
132
133     errno = 0;
134     char* tail;
135     int node_id = strtol(arguments.args[0], &tail, 0);
136     if (errno) {
137         debugError("argument parsing failed: %s\n",
138                     strerror(errno));
139         return -1;
140     }
141
142     Ieee1394Service service;
143     if ( !service.initialize( arguments.port ) ) {
144         debugError("Could not initialize IEEE 1394 service\n");
145         return -1;
146     }
147     service.setVerboseLevel( arguments.verbose );
148
149     ConfigRom *configRom = new ConfigRom(service, node_id );
150     if (configRom == NULL) {
151         debugError("Could not create ConfigRom\n");
152         return -1;
153     }
154     configRom->setVerboseLevel( arguments.verbose );
155
156     if ( !configRom->initialize() ) {
157         debugError( "Could not read config rom from device (node id %d).\n",
158                     node_id );
159         delete configRom;
160         return -1;
161     }
162
163     if ( !Device::probe(*configRom) ) {
164         debugError( "Device with node id %d is not an ECHO FireWorks device.\n",
165                     node_id );
166         delete configRom;
167         return -1;
168     }
169    
170     DeviceManager d = DeviceManager();
171     Device *dev = new Device(d, std::auto_ptr<ConfigRom>(configRom) );
172     if (dev == NULL) {
173         debugError("Could not create FireWorks::Device\n");
174         delete configRom;
175         return -1;
176     }
177
178     // create the firmware util class
179     FirmwareUtil util = FirmwareUtil(*dev);
180     util.setVerboseLevel( arguments.verbose );
181    
182     if ( strcmp( arguments.args[1], "firmware" ) == 0 ) {
183         if (!arguments.args[2] ) {
184             cerr << "FILE argument is missing" << endl;
185             delete dev;
186             return -1;
187         }
188         std::string str( arguments.args[2] );
189
190         // load the file
191         Firmware f = Firmware();
192         f.setVerboseLevel( arguments.verbose );
193        
194         f.loadFile(str);
195         f.show();
196
197     } else if ( strcmp( arguments.args[1], "display" ) == 0 ) {
198         // nothing to do
199         dev->showDevice();
200     } else {
201         cout << "Unknown operation" << endl;
202     }
203
204     delete dev;
205     return 0;
206 }
Note: See TracBrowser for help on using the browser.