Changeset 900

Show
Ignore:
Timestamp:
03/02/08 12:15:55 (16 years ago)
Author:
wagi
Message:

Use GUID instead node id for download application. Furthermore, factor out common code from both firmware downloaders

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libffado/support/firmware/bridgeco-downloader.cpp

    r864 r900  
    2222 */ 
    2323 
     24#include "downloader.h" 
     25 
    2426#include "src/bebob/bebob_dl_mgr.h" 
    2527#include "src/bebob/bebob_dl_bcd.h" 
     
    2830#include "libieee1394/ieee1394service.h" 
    2931 
    30 #include <argp.h> 
    3132#include <iostream> 
    3233 
     34using namespace std; 
    3335 
    3436//////////////////////////////////////////////// 
    3537// arg parsing 
    3638//////////////////////////////////////////////// 
    37 const char *argp_program_version = "bridgeco-downloader 0.1"; 
     39const char *argp_program_version = "bridgeco-downloader 0.2"; 
    3840const char *argp_program_bug_address = "<ffado-devel@lists.sf.net>"; 
    39 static char doc[] = "bridgeco-downloader -- firmware downloader application for BridgeCo devices\n\n" 
     41char* doc = "bridgeco-downloader -- firmware downloader application for BridgeCo devices\n\n" 
    4042                    "OPERATION: display\n" 
    4143                    "           setguid GUID\n" 
     
    4345                    "           cne FILE\n" 
    4446                    "           bcd FILE\n"; 
    45 static char args_doc[] = "NODE_ID OPERATION"; 
    46 static struct argp_option options[] = { 
     47static struct argp_option _options[] = { 
    4748    {"verbose",   'v', "level",     0,  "Produce verbose output" }, 
    4849    {"port",      'p', "PORT",      0,  "Set port" }, 
     
    5152    { 0 } 
    5253}; 
    53  
    54 struct arguments 
    55 
    56     arguments() 
    57         : verbose( 0 ) 
    58         , port( 0 ) 
    59         , force( 0 ) 
    60         { 
    61             args[0] = 0; 
    62             args[1] = 0; 
    63             args[2] = 0; 
    64         } 
    65  
    66     char* args[3]; 
    67     short verbose; 
    68     int   port; 
    69     int   force; 
    70     int   no_bootloader_restart; 
    71 } arguments; 
    72  
    73 // Parse a single option. 
    74 static error_t 
    75 parse_opt( int key, char* arg, struct argp_state* state ) 
    76 
    77     // Get the input argument from `argp_parse', which we 
    78     // know is a pointer to our arguments structure. 
    79     struct arguments* arguments = ( struct arguments* ) state->input; 
    80  
    81     char* tail; 
    82     switch (key) { 
    83     case 'v': 
    84         if (arg) { 
    85             arguments->verbose = strtol( arg, &tail, 0 ); 
    86             if ( errno ) { 
    87                 fprintf( stderr,  "Could not parse 'verbose' argument\n" ); 
    88                 return ARGP_ERR_UNKNOWN; 
    89             } 
    90         } 
    91         break; 
    92     case 'p': 
    93         errno = 0; 
    94         arguments->port = strtol(arg, &tail, 0); 
    95         if (errno) { 
    96             perror("argument parsing failed:"); 
    97             return errno; 
    98         } 
    99         break; 
    100     case 'f': 
    101         arguments->force = 1; 
    102         break; 
    103     case 'b': 
    104         arguments->no_bootloader_restart = 1; 
    105         break; 
    106     case ARGP_KEY_ARG: 
    107         if (state->arg_num >= 3) { 
    108             // Too many arguments. 
    109             argp_usage (state); 
    110         } 
    111         arguments->args[state->arg_num] = arg; 
    112         break; 
    113     case ARGP_KEY_END: 
    114         if (state->arg_num < 2) { 
    115             // Not enough arguments. 
    116             argp_usage (state); 
    117         } 
    118         break; 
    119     default: 
    120         return ARGP_ERR_UNKNOWN; 
    121     } 
    122     return 0; 
    123 
    124  
    125 static struct argp argp = { options, parse_opt, args_doc, doc }; 
    126  
     54struct argp_option* options = _options; 
    12755 
    12856int 
    12957main( int argc, char** argv ) 
    13058{ 
    131     using namespace std; 
    13259 
    13360    // arg parsing 
    134     argp_parse (&argp, argc, argv, 0, 0, &arguments); 
     61    argp_parse (argp, argc, argv, 0, 0, args); 
    13562 
    13663    errno = 0; 
    13764    char* tail; 
    138     int node_id = strtol(arguments.args[0], &tail, 0); 
     65    int node_id = -1; 
     66 
     67    fb_octlet_t guid = strtoll(args->args[0], &tail, 0); 
    13968    if (errno) { 
    140     perror("argument parsing failed:"); 
    141     return -1; 
     69        perror("argument parsing failed:"); 
     70        return -1; 
    14271    } 
    14372 
    14473    Ieee1394Service service; 
    145     if ( !service.initialize( arguments.port ) ) { 
     74    if ( !service.initialize( args->port ) ) { 
    14675        cerr << "Could not initialize IEEE 1394 service" << endl; 
    14776        return -1; 
    14877    } 
     78    service.setVerboseLevel( args->verbose ); 
    14979 
    150     service.setVerboseLevel( arguments.verbose ); 
     80    for (int i = 0; i < service.getNodeCount(); i++) { 
     81        ConfigRom configRom(service, i); 
     82        configRom.initialize(); 
     83         
     84        if (configRom.getGuid() == guid) 
     85            node_id = configRom.getNodeId(); 
     86    } 
     87 
     88    if (node_id < 0) { 
     89        cerr << "Could not find device with matching GUID" << endl; 
     90        return -1; 
     91    } 
     92 
     93    service.setVerboseLevel( args->verbose ); 
     94 
    15195    BeBoB::BootloaderManager blMgr( service, node_id ); 
    152     if ( arguments.force == 1 ) { 
     96    if ( args->force == 1 ) { 
    15397        blMgr.setForceOperations( true ); 
    15498    } 
     
    156100    blMgr.printInfoRegisters(); 
    157101 
    158     if ( strcmp( arguments.args[1], "setguid" ) == 0 ) { 
    159         if (!arguments.args[2] ) { 
     102    if ( strcmp( args->args[1], "setguid" ) == 0 ) { 
     103        if (!args->args[2] ) { 
    160104            cerr << "guid argument is missing" << endl; 
    161105            return -1; 
     
    163107 
    164108        char* tail; 
    165         fb_octlet_t guid = strtoll(arguments.args[2], &tail, 0 ); 
     109        fb_octlet_t guid = strtoll(args->args[2], &tail, 0 ); 
    166110 
    167111        if ( !blMgr.programGUID( guid ) ) { 
     
    171115            cout << "new GUID programmed" << endl; 
    172116        } 
    173     } else if ( strcmp( arguments.args[1], "firmware" ) == 0 ) { 
    174         if (!arguments.args[2] ) { 
     117    } else if ( strcmp( args->args[1], "firmware" ) == 0 ) { 
     118        if (!args->args[2] ) { 
    175119            cerr << "FILE argument is missing" << endl; 
    176120            return -1; 
    177121        } 
    178         std::string str( arguments.args[2] ); 
     122        std::string str( args->args[2] ); 
    179123 
    180         blMgr.setStartBootloader( arguments.no_bootloader_restart != 1 ); 
     124        blMgr.setStartBootloader( args->no_bootloader_restart != 1 ); 
    181125        if ( !blMgr.downloadFirmware( str ) ) { 
    182126            cerr << "Failed to download firmware" << endl; 
     
    185129            cout << "Firmware download was successful" << endl; 
    186130        } 
    187     } else if ( strcmp( arguments.args[1], "cne" ) == 0 ) { 
    188         if (!arguments.args[2] ) { 
     131    } else if ( strcmp( args->args[1], "cne" ) == 0 ) { 
     132        if (!args->args[2] ) { 
    189133            cerr << "FILE argument is missing" << endl; 
    190134            return -1; 
    191135        } 
    192         std::string str( arguments.args[2] ); 
     136        std::string str( args->args[2] ); 
    193137 
    194138        if ( !blMgr.downloadCnE( str ) ) { 
     
    198142            cout << "CnE download was successful" << endl; 
    199143        } 
    200     } else if ( strcmp( arguments.args[1], "display" ) == 0 ) { 
     144    } else if ( strcmp( args->args[1], "display" ) == 0 ) { 
    201145        // nothing to do 
    202     } else if ( strcmp( arguments.args[1], "bcd" ) == 0 ) { 
    203         if ( !arguments.args[2] ) { 
     146    } else if ( strcmp( args->args[1], "bcd" ) == 0 ) { 
     147        if ( !args->args[2] ) { 
    204148            cerr << "FILE arguments is missing" << endl; 
    205149            return -1; 
    206150        } 
    207         BeBoB::BCD* bcd = new BeBoB::BCD::BCD( arguments.args[2] ); 
     151        BeBoB::BCD* bcd = new BeBoB::BCD::BCD( args->args[2] ); 
    208152        if ( !bcd ) { 
    209             cerr << "Could no open file " << arguments.args[2] << endl; 
     153            cerr << "Could no open file " << args->args[2] << endl; 
    210154            return -1; 
    211155        } 
    212156        if ( !bcd->parse() ) { 
    213             cerr << "Could not parse file " << arguments.args[2] << endl; 
     157            cerr << "Could not parse file " << args->args[2] << endl; 
    214158            return -1; 
    215159        } 
  • trunk/libffado/support/firmware/fireworks-downloader.cpp

    r864 r900  
    2323 */ 
    2424 
     25#include "downloader.h" 
     26 
    2527#include "src/fireworks/fireworks_device.h" 
    2628#include "src/fireworks/fireworks_firmware.h" 
     
    3335#include "devicemanager.h" 
    3436 
    35 #include <argp.h> 
    3637#include <iostream> 
    3738 
     
    3940#include <string> 
    4041 
     42using namespace FireWorks; 
     43 
    4144DECLARE_GLOBAL_DEBUG_MODULE; 
    4245 
    43 using namespace FireWorks; 
    4446//////////////////////////////////////////////// 
    4547// arg parsing 
    4648//////////////////////////////////////////////// 
    47 const char *argp_program_version = "fireworks-downloader 0.1"; 
     49const char *argp_program_version = "fireworks-downloader 0.2"; 
    4850const 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" 
     51char* doc = "fireworks-downloader -- firmware downloader application for ECHO Fireworks devices\n\n" 
    5052                    "OPERATION: display\n" 
    5153                    "           firmware FILE\n"; 
    52 static char args_doc[] = "NODE_ID OPERATION"; 
    53 static struct argp_option options[] = { 
     54 
     55static struct argp_option _options[] = { 
    5456    {"verbose",   'v', "level",     0,  "Produce verbose output" }, 
    5557    {"port",      'p', "PORT",      0,  "Set port" }, 
    5658    { 0 } 
    5759}; 
    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  
     60struct argp_option* options = _options; 
    12461 
    12562int 
     
    12865    using namespace std; 
    12966 
    130     // arg parsing 
    131     argp_parse (&argp, argc, argv, 0, 0, &arguments); 
     67    argp_parse (argp, argc, argv, 0, 0, args); 
    13268 
    13369    errno = 0; 
    13470    char* tail; 
    135     int node_id = strtol(arguments.args[0], &tail, 0); 
     71    int node_id = -1; 
     72 
     73    fb_octlet_t guid = strtoll(args->args[0], &tail, 0); 
    13674    if (errno) { 
    13775        debugError("argument parsing failed: %s\n", 
     
    14179 
    14280    Ieee1394Service service; 
    143     if ( !service.initialize( arguments.port ) ) { 
     81    if ( !service.initialize( args->port ) ) { 
    14482        debugError("Could not initialize IEEE 1394 service\n"); 
    14583        return -1; 
    14684    } 
    147     service.setVerboseLevel( arguments.verbose ); 
     85    service.setVerboseLevel( args->verbose ); 
     86  
     87    for (int i = 0; i < service.getNodeCount(); i++) { 
     88        ConfigRom configRom(service, i); 
     89        configRom.initialize(); 
     90         
     91        if (configRom.getGuid() == guid) 
     92            node_id = configRom.getNodeId(); 
     93    } 
     94 
     95    if (node_id < 0) { 
     96        cerr << "Could not find device with matching GUID" << endl; 
     97        return -1; 
     98    } 
    14899 
    149100    ConfigRom *configRom = new ConfigRom(service, node_id ); 
     
    152103        return -1; 
    153104    } 
    154     configRom->setVerboseLevel( arguments.verbose ); 
     105    configRom->setVerboseLevel( args->verbose ); 
    155106 
    156107    if ( !configRom->initialize() ) { 
     
    178129    // create the firmware util class 
    179130    FirmwareUtil util = FirmwareUtil(*dev); 
    180     util.setVerboseLevel( arguments.verbose ); 
     131    util.setVerboseLevel( args->verbose ); 
    181132     
    182     if ( strcmp( arguments.args[1], "firmware" ) == 0 ) { 
    183         if (!arguments.args[2] ) { 
     133    if ( strcmp( args->args[1], "firmware" ) == 0 ) { 
     134        if (!args->args[2] ) { 
    184135            cerr << "FILE argument is missing" << endl; 
    185136            delete dev; 
    186137            return -1; 
    187138        } 
    188         std::string str( arguments.args[2] ); 
     139        std::string str( args->args[2] ); 
    189140 
    190141        // load the file 
    191142        Firmware f = Firmware(); 
    192         f.setVerboseLevel( arguments.verbose ); 
     143        f.setVerboseLevel( args->verbose ); 
    193144         
    194145        f.loadFile(str); 
    195146        f.show(); 
    196147 
    197     } else if ( strcmp( arguments.args[1], "display" ) == 0 ) { 
     148    } else if ( strcmp( args->args[1], "display" ) == 0 ) { 
    198149        // nothing to do 
    199150        dev->showDevice(); 
  • trunk/libffado/support/firmware/SConscript

    r864 r900  
    4646 
    4747if env['ENABLE_BEBOB']: 
    48     apps["ffado-bridgeco-downloader"] = "bridgeco-downloader.cpp" 
     48    apps["ffado-bridgeco-downloader"] = "downloader.cpp bridgeco-downloader.cpp" 
    4949    installapps += [ "ffado-bridgeco-downloader" ] 
    5050 
    5151if env['ENABLE_FIREWORKS']: 
    52     apps["ffado-fireworks-downloader"] = "fireworks-downloader.cpp" 
     52    apps["ffado-fireworks-downloader"] = "downloader.cpp fireworks-downloader.cpp" 
    5353    installapps += [ "ffado-fireworks-downloader" ] 
    5454