Add new function ftdi_eeprom_get_strings()
[libftdi] / src / ftdi.c
index fdf93fe..777733a 100644 (file)
@@ -469,11 +469,12 @@ int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev,
                           char *serial, int serial_len)
 {
     struct libusb_device_descriptor desc;
+    char need_open;
 
     if ((ftdi==NULL) || (dev==NULL))
         return -1;
 
-    char need_open = (ftdi->usb_dev == NULL);
+    need_open = (ftdi->usb_dev == NULL);
     if (need_open && libusb_open(dev, &ftdi->usb_dev) < 0)
         ftdi_error_return(-4, "libusb_open() failed");
 
@@ -1252,7 +1253,7 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
         else
             best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
     }
-    else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R ))
+    else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
     {
         best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
     }
@@ -2441,7 +2442,7 @@ int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
     eeprom->manufacturer = NULL;
     if (manufacturer)
     {
-        eeprom->manufacturer = malloc(strlen(manufacturer)+1);
+        eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
         if (eeprom->manufacturer)
             strcpy(eeprom->manufacturer, manufacturer);
     }
@@ -2451,7 +2452,7 @@ int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
     eeprom->product = NULL;
     if(product)
     {
-        eeprom->product = malloc(strlen(product)+1);
+        eeprom->product = (char *)malloc(strlen(product)+1);
         if (eeprom->product)
             strcpy(eeprom->product, product);
     }
@@ -2471,7 +2472,7 @@ int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
             default:
                 ftdi_error_return(-3, "Unknown chip type");
         }
-        eeprom->product = malloc(strlen(default_product) +1);
+        eeprom->product = (char *)malloc(strlen(default_product) +1);
         if (eeprom->product)
             strcpy(eeprom->product, default_product);
     }
@@ -2481,7 +2482,7 @@ int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
     eeprom->serial = NULL;
     if (serial)
     {
-        eeprom->serial = malloc(strlen(serial)+1);
+        eeprom->serial = (char *)malloc(strlen(serial)+1);
         if (eeprom->serial)
             strcpy(eeprom->serial, serial);
     }
@@ -2567,7 +2568,7 @@ int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
     {
         if (eeprom->manufacturer)
             free (eeprom->manufacturer);
-        eeprom->manufacturer = malloc(strlen(manufacturer)+1);
+        eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
         if (eeprom->manufacturer)
             strcpy(eeprom->manufacturer, manufacturer);
     }
@@ -2576,7 +2577,7 @@ int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
     {
         if (eeprom->product)
             free (eeprom->product);
-        eeprom->product = malloc(strlen(product)+1);
+        eeprom->product = (char *)malloc(strlen(product)+1);
         if (eeprom->product)
             strcpy(eeprom->product, product);
     }
@@ -2585,7 +2586,7 @@ int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
     {
         if (eeprom->serial)
             free (eeprom->serial);
-        eeprom->serial = malloc(strlen(serial)+1);
+        eeprom->serial = (char *)malloc(strlen(serial)+1);
         if (eeprom->serial)
         {
             strcpy(eeprom->serial, serial);
@@ -2595,6 +2596,47 @@ int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
     return 0;
 }
 
+int ftdi_eeprom_get_strings(struct ftdi_context *ftdi,
+                            char *manufacturer, int mnf_len,
+                            char *product, int prod_len,
+                            char *serial, int serial_len)
+{
+    struct ftdi_eeprom *eeprom;
+
+    if (ftdi == NULL)
+        ftdi_error_return(-1, "No struct ftdi_context");
+
+    if (ftdi->eeprom == NULL)
+        ftdi_error_return(-2,"No struct ftdi_eeprom");
+
+    eeprom = ftdi->eeprom;
+
+    if (ftdi->usb_dev == NULL)
+        ftdi_error_return(-3, "No connected device or device not yet opened");
+
+    if (manufacturer)
+    {
+        strncpy(manufacturer, eeprom->manufacturer, mnf_len);
+        if (mnf_len > 0)
+            manufacturer[mnf_len - 1] = '\0';
+    }
+
+    if (product)
+    {
+        strncpy(product, eeprom->product, prod_len);
+        if (prod_len > 0)
+            product[prod_len - 1] = '\0';
+    }
+
+    if (serial)
+    {
+        strncpy(serial, eeprom->serial, serial_len);
+        if (serial_len > 0)
+            serial[serial_len - 1] = '\0';
+    }
+
+    return 0;
+}
 
 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal options*/
 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
@@ -3180,7 +3222,6 @@ int ftdi_eeprom_build(struct ftdi_context *ftdi)
             break;
         case TYPE_230X:
             output[0x00] = 0x80; /* Actually, leave the default value */
-            output[0x0a] = 0x08; /* Enable USB Serial Number */
             /*FIXME: Make DBUS & CBUS Control configurable*/
             output[0x0c] = 0;    /* DBUS drive 4mA, CBUS drive 4 mA like factory default */
             for (j = 0; j <= 6; j++)
@@ -3280,7 +3321,7 @@ static unsigned char bit2type(unsigned char bits)
 */
 static void print_inverted_bits(int invert)
 {
-    char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
+    const char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
     int i;
 
     fprintf(stdout,"Inverted bits:");
@@ -3368,7 +3409,7 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
         free(eeprom->manufacturer);
     if (manufacturer_size > 0)
     {
-        eeprom->manufacturer = malloc(manufacturer_size);
+        eeprom->manufacturer = (char *)malloc(manufacturer_size);
         if (eeprom->manufacturer)
         {
             // Decode manufacturer
@@ -3389,7 +3430,7 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
     product_size = buf[0x11]/2;
     if (product_size > 0)
     {
-        eeprom->product = malloc(product_size);
+        eeprom->product = (char *)malloc(product_size);
         if (eeprom->product)
         {
             // Decode product name
@@ -3410,7 +3451,7 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
     serial_size = buf[0x13]/2;
     if (serial_size > 0)
     {
-        eeprom->serial = malloc(serial_size);
+        eeprom->serial = (char *)malloc(serial_size);
         if (eeprom->serial)
         {
             // Decode serial
@@ -3566,7 +3607,7 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
 
     if (verbose)
     {
-        char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
+        const char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
         fprintf(stdout, "VID:     0x%04x\n",eeprom->vendor_id);
         fprintf(stdout, "PID:     0x%04x\n",eeprom->product_id);
         fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
@@ -3647,7 +3688,7 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
         }
         else if (ftdi->type == TYPE_232H)
         {
-            char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
+            const char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
                                  "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
                                  "CLK30","CLK15","CLK7_5"
                                 };
@@ -3668,7 +3709,7 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
         }
         else if (ftdi->type == TYPE_230X)
         {
-            char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
+            const char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
                                  "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
                                  "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
                                  "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
@@ -3694,11 +3735,11 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
 
         if (ftdi->type == TYPE_R)
         {
-            char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
+            const char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
                                 "SLEEP","CLK48","CLK24","CLK12","CLK6",
                                 "IOMODE","BB_WR","BB_RD"
                                };
-            char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
+            const char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
 
             if (eeprom->invert)
                 print_inverted_bits(eeprom->invert);
@@ -4487,7 +4528,7 @@ int ftdi_erase_eeprom(struct ftdi_context *ftdi)
 
     \retval Pointer to error string
 */
-char *ftdi_get_error_string (struct ftdi_context *ftdi)
+const char *ftdi_get_error_string (struct ftdi_context *ftdi)
 {
     if (ftdi == NULL)
         return "";