libftdi Archives

Subject: ftdi_eeprom_cfg

From: Wilfried Holzke <libftdi@xxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Thu, 10 Jun 2010 15:07:28 +0200
Hi,

I missed the ability to create a config file from an eeprom file. So I
created a small program. Besides I added a procedure to to free the
allocated memory for strings (ftdi_eeprom_free), this could be added to
libftdi to clean up.

If this feature is usefull, please tell me if it could be integrated
into "ftdi_eeprom" or it should stay as a stand alone application.

regards

  Wilfried


---------------------------

/*
 *  Copyright (C) 2010 Wilfried Holzke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  compile: gcc thisfile.c -lftdi -lconfuse -o ftdi_eeprom_cfg
 */

#include <stdio.h>
#include <string.h>
#include <confuse.h>
#include <ftdi.h>

cfg_opt_t opts[] = {
                CFG_INT("vendor_id", 0, 0),
                CFG_INT("product_id", 0, 0),
                CFG_BOOL("self_powered", cfg_true, 0),
                CFG_BOOL("remote_wakeup", cfg_true, 0),
                CFG_BOOL("BM_type_chip", cfg_true, 0),
                CFG_BOOL("in_is_isochronous", cfg_false, 0),
                CFG_BOOL("out_is_isochronous", cfg_false, 0),
                CFG_BOOL("suspend_pull_downs", cfg_false, 0),
                CFG_BOOL("use_serial", cfg_false, 0),
                CFG_BOOL("change_usb_version", cfg_false, 0),
                CFG_INT("usb_version", 0, 0),
                CFG_INT("max_power", 0, 0),
                CFG_STR("manufacturer", "Acme Inc.", 0),
                CFG_STR("product", "USB Serial Converter", 0),
                CFG_STR("serial", "08-15", 0),
                CFG_STR("filename", "", 0),
                CFG_END()
};

void ftdi_eeprom_free(struct ftdi_eeprom *eeprom) {

        if (eeprom->manufacturer != 0) {
                free(eeprom->manufacturer);
                eeprom->manufacturer = 0;
        }

        if (eeprom->product != 0) {
                free(eeprom->product);
                eeprom->product = 0;
        }

        if (eeprom->serial != 0) {
                free(eeprom->serial);
                eeprom->serial = 0;
        }
}

void ftdi_eeprom_print(struct ftdi_eeprom *eeprom) {
        printf("vendor_id:          %#06x\n",eeprom->vendor_id);
        printf("product_id:         %#06x\n",eeprom->product_id);
        printf("self_powered:       %i\n",eeprom->self_powered);
        printf("remote_wakeup:      %i\n",eeprom->remote_wakeup);
        printf("BM_type_chip:       %i\n",eeprom->BM_type_chip);
        printf("in_is_isochronous:  %i\n",eeprom->in_is_isochronous);
        printf("out_is_isochronous: %i\n",eeprom->out_is_isochronous);
        printf("suspend_pull_downs: %i\n",eeprom->suspend_pull_downs);
        printf("use_serial:         %i\n",eeprom->use_serial);
        printf("change_usb_version: %i\n",eeprom->change_usb_version);
        printf("usb_version:        %i\n",eeprom->usb_version);
        printf("max_power:          %i\n",eeprom->max_power);
        printf("manufacturer:       %s\n",eeprom->manufacturer);
        printf("product:            %s\n",eeprom->product);
        printf("serial:             %s\n",eeprom->serial);
}

void ftdi_eeprom_writeconf(char* filename, struct ftdi_eeprom *eeprom) {
        cfg_t *cfg=0;
        cfg = cfg_init(opts, 0);

    cfg_setint(cfg, "vendor_id",eeprom->vendor_id);
    cfg_setint(cfg, "product_id",eeprom->product_id);
    cfg_setbool(cfg, "BM_type_chip",eeprom->BM_type_chip);

    cfg_setbool(cfg, "self_powered",eeprom->self_powered);
    cfg_setbool(cfg, "remote_wakeup",eeprom->remote_wakeup);
    cfg_setint(cfg, "max_power",eeprom->max_power);

    cfg_setbool(cfg, "in_is_isochronous",eeprom->in_is_isochronous);
    cfg_setbool(cfg, "out_is_isochronous",eeprom->out_is_isochronous);
    cfg_setbool(cfg, "suspend_pull_downs",eeprom->suspend_pull_downs);

    cfg_setbool(cfg, "use_serial",eeprom->use_serial);
    cfg_setbool(cfg, "change_usb_version",eeprom->change_usb_version);
    cfg_setint(cfg, "usb_version",eeprom->usb_version);

    cfg_setstr(cfg, "manufacturer",eeprom->manufacturer);
    cfg_setstr(cfg, "product",eeprom->product);
    cfg_setstr(cfg, "serial",eeprom->serial);

        FILE *fp = fopen(filename, "w");
        if (fp != 0) {
                cfg_print(cfg, fp);
                fclose(fp);
        }
}

char* strcon(char *s1, char *s2) {
        char *s = strdup(s1);
        if (s != 0) {
                char *sf = realloc(s,strlen(s1)+strlen(s2)+1);
                if (sf != 0) {
                        s = strcat(sf,s2);
                } else {
                        free(s);
                        s = 0;
                }
        }
        return s;
}

int main(int argc, char** argv) {

        if (argc != 2) {
                printf("usage: ftdi_eeprom_print [eeprom-filename]\n");
        } else {

                FILE *fp = fopen (argv[1], "rb");
                if (fp != 0) {
                        unsigned char eeprom_buf[128];
                        fread (&eeprom_buf, 1, 128, fp);
                        fclose (fp);

                        struct ftdi_eeprom eeprom;
                        if (ftdi_eeprom_decode(&eeprom, eeprom_buf, 128) == 0) {

                                ftdi_eeprom_print(&eeprom);

                                char* filename = strcon(argv[1],".cfg");
                                if (filename != 0) {
                                        ftdi_eeprom_writeconf(filename,&eeprom);
                                        free(filename);
                                }

                                ftdi_eeprom_free(&eeprom);

                        } else {
                                printf("error decoding eeprom buffer!\n");
                        }
                }
        }

        return 0;
}

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread